@resolveio/server-lib 20.7.5 → 20.7.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (112) hide show
  1. package/client-server-app.js +1 -1
  2. package/client-server-app.js.map +1 -1
  3. package/collections/app-status.collection.js +1 -1
  4. package/collections/app-status.collection.js.map +1 -1
  5. package/collections/counter.collection.js +1 -1
  6. package/collections/counter.collection.js.map +1 -1
  7. package/collections/cron-job-history.collection.js +1 -1
  8. package/collections/cron-job-history.collection.js.map +1 -1
  9. package/collections/cron-job.collection.js +1 -1
  10. package/collections/cron-job.collection.js.map +1 -1
  11. package/collections/email-history.collection.js +1 -1
  12. package/collections/email-history.collection.js.map +1 -1
  13. package/collections/email-verified.collection.js +1 -1
  14. package/collections/email-verified.collection.js.map +1 -1
  15. package/collections/file.collection.js +1 -1
  16. package/collections/file.collection.js.map +1 -1
  17. package/collections/flag-update.collection.js +1 -1
  18. package/collections/flag-update.collection.js.map +1 -1
  19. package/collections/flag.collection.js +1 -1
  20. package/collections/flag.collection.js.map +1 -1
  21. package/collections/log-method-latency.collection.js +1 -1
  22. package/collections/log-method-latency.collection.js.map +1 -1
  23. package/collections/log-subscription.collection.js +1 -1
  24. package/collections/log-subscription.collection.js.map +1 -1
  25. package/collections/log.collection.js +1 -1
  26. package/collections/log.collection.js.map +1 -1
  27. package/collections/logged-in-users.collection.js +1 -1
  28. package/collections/logged-in-users.collection.js.map +1 -1
  29. package/collections/method-response.collection.js +1 -1
  30. package/collections/method-response.collection.js.map +1 -1
  31. package/collections/monitor-cpu.collection.js +1 -1
  32. package/collections/monitor-cpu.collection.js.map +1 -1
  33. package/collections/monitor-function.collection.js +1 -1
  34. package/collections/monitor-function.collection.js.map +1 -1
  35. package/collections/monitor-memory.collection.js +1 -1
  36. package/collections/monitor-memory.collection.js.map +1 -1
  37. package/collections/monitor-mongo.collection.js +1 -1
  38. package/collections/monitor-mongo.collection.js.map +1 -1
  39. package/collections/notification.collection.js +1 -1
  40. package/collections/notification.collection.js.map +1 -1
  41. package/collections/report-builder-dashboard-builder.collection.js +1 -1
  42. package/collections/report-builder-dashboard-builder.collection.js.map +1 -1
  43. package/collections/report-builder-library.collection.js +1 -1
  44. package/collections/report-builder-library.collection.js.map +1 -1
  45. package/collections/report-builder-report.collection.js +1 -1
  46. package/collections/report-builder-report.collection.js.map +1 -1
  47. package/collections/user-group.collection.js +1 -1
  48. package/collections/user-group.collection.js.map +1 -1
  49. package/collections/user-guide.collection.js +1 -1
  50. package/collections/user-guide.collection.js.map +1 -1
  51. package/collections/user.collection.js +1 -1
  52. package/collections/user.collection.js.map +1 -1
  53. package/fixtures/cron-jobs.js +1 -1
  54. package/fixtures/cron-jobs.js.map +1 -1
  55. package/fixtures/init.js +1 -1
  56. package/fixtures/init.js.map +1 -1
  57. package/http/auth.d.ts +2 -1
  58. package/http/auth.js +1 -1
  59. package/http/auth.js.map +1 -1
  60. package/http/home.js +1 -1
  61. package/http/home.js.map +1 -1
  62. package/managers/cron.manager.d.ts +3 -1
  63. package/managers/cron.manager.js +1 -1
  64. package/managers/cron.manager.js.map +1 -1
  65. package/managers/local-log.manager.d.ts +3 -1
  66. package/managers/local-log.manager.js +1 -1
  67. package/managers/local-log.manager.js.map +1 -1
  68. package/managers/method.manager.d.ts +12 -7
  69. package/managers/method.manager.js +1 -1
  70. package/managers/method.manager.js.map +1 -1
  71. package/managers/mongo.manager.d.ts +17 -12
  72. package/managers/mongo.manager.js +1 -1
  73. package/managers/mongo.manager.js.map +1 -1
  74. package/managers/monitor.manager.d.ts +15 -7
  75. package/managers/monitor.manager.js +1 -1
  76. package/managers/monitor.manager.js.map +1 -1
  77. package/managers/subscription.manager.d.ts +5 -3
  78. package/managers/subscription.manager.js +1 -1
  79. package/managers/subscription.manager.js.map +1 -1
  80. package/managers/websocket.manager.d.ts +3 -1
  81. package/managers/websocket.manager.js +1 -1
  82. package/managers/websocket.manager.js.map +1 -1
  83. package/methods/accounts.js +1 -1
  84. package/methods/accounts.js.map +1 -1
  85. package/methods/aws.js +1 -1
  86. package/methods/aws.js.map +1 -1
  87. package/methods/collections.js +1 -1
  88. package/methods/collections.js.map +1 -1
  89. package/methods/counters.js +1 -1
  90. package/methods/counters.js.map +1 -1
  91. package/methods/cron-jobs.js +1 -1
  92. package/methods/cron-jobs.js.map +1 -1
  93. package/methods/logs.js +1 -1
  94. package/methods/logs.js.map +1 -1
  95. package/methods/monitor.js +1 -1
  96. package/methods/monitor.js.map +1 -1
  97. package/methods/pdf.js +1 -1
  98. package/methods/pdf.js.map +1 -1
  99. package/methods/report-builder.js +1 -1
  100. package/methods/report-builder.js.map +1 -1
  101. package/methods/support.js +1 -1
  102. package/methods/support.js.map +1 -1
  103. package/models/method.model.d.ts +6 -1
  104. package/package.json +3 -2
  105. package/publications/logs.js +1 -1
  106. package/publications/logs.js.map +1 -1
  107. package/resolveio-server-app.d.ts +14 -10
  108. package/resolveio-server-app.js +1 -1
  109. package/resolveio-server-app.js.map +1 -1
  110. package/server-app.d.ts +5 -4
  111. package/server-app.js +1 -1
  112. package/server-app.js.map +1 -1
@@ -2,13 +2,15 @@ export declare class MonitorManager {
2
2
  private _instanceHostname;
3
3
  private _monitorData;
4
4
  constructor();
5
+ static create(): Promise<MonitorManager>;
6
+ private initialize;
5
7
  cpuAverage(): {
6
8
  idle: number;
7
9
  total: number;
8
10
  };
9
11
  setupIntervals(): Promise<void>;
10
- addMongoTracker(data: MongoMonitorModel): void;
11
- addFunctionTracker(data: MonitorFunction): void;
12
+ addMongoTracker(data: MongoMonitorModel): Promise<void>;
13
+ addFunctionTracker(data: MonitorFunction): Promise<void>;
12
14
  private pushMonitorData;
13
15
  private estimateBatchSize;
14
16
  setupEventLoop(): void;
@@ -26,8 +28,10 @@ export declare class MonitorMongo {
26
28
  private _method;
27
29
  private _collection;
28
30
  private _query;
29
- constructor(method: string, collection: string, query: string);
30
- finish(): void;
31
+ constructor();
32
+ static create(method: string, collection: string, query: string): MonitorMongo;
33
+ private initialize;
34
+ finish(): Promise<void>;
31
35
  }
32
36
  export declare class MonitorManagerFunction {
33
37
  private _functionCnt;
@@ -35,8 +39,10 @@ export declare class MonitorManagerFunction {
35
39
  private _functionLastCompletedWS;
36
40
  private _functionLastCompleted;
37
41
  constructor();
42
+ static create(): MonitorManagerFunction;
43
+ private initialize;
38
44
  startMonitorFunction(functionType: MonitorFunctionType, functionName: string, user: string, id_socket: string, data: any): number;
39
- finishMonitorFunction(id: number): void;
45
+ finishMonitorFunction(id: number): Promise<void>;
40
46
  getActiveMonitorFunctions(): MonitorFunction[];
41
47
  getLastCompletedMonitorFunction(): MonitorFunction;
42
48
  getLastCompletedMonitorFunctionWS(): {
@@ -56,6 +62,8 @@ export declare class MonitorFunction {
56
62
  lastTime: Date;
57
63
  id_socket: string;
58
64
  id: number;
59
- constructor(id: number, functionType: MonitorFunctionType, functionName: string, user: string, id_socket: string, data: any);
60
- finish(): void;
65
+ constructor();
66
+ static create(id: number, functionType: MonitorFunctionType, functionName: string, user: string, id_socket: string, data: any): MonitorFunction;
67
+ private initialize;
68
+ finish(): Promise<void>;
61
69
  }
@@ -1,2 +1,2 @@
1
- "use strict";var __awaiter=this&&this.__awaiter||function(e,n,r,i){return new(r=r||Promise)(function(o,t){function fulfilled(e){try{step(i.next(e))}catch(e){t(e)}}function rejected(e){try{step(i.throw(e))}catch(e){t(e)}}function step(e){var t;e.done?o(e.value):((t=e.value)instanceof r?t:new r(function(e){e(t)})).then(fulfilled,rejected)}step((i=i.apply(e,n||[])).next())})},__generator=this&&this.__generator||function(n,r){var i,a,s,c={label:0,sent:function(){if(1&s[0])throw s[1];return s[1]},trys:[],ops:[]},l={next:verb(0),throw:verb(1),return:verb(2)};return"function"==typeof Symbol&&(l[Symbol.iterator]=function(){return this}),l;function verb(o){return function(e){var t=[o,e];if(i)throw new TypeError("Generator is already executing.");for(;c=l&&t[l=0]?0:c;)try{if(i=1,a&&(s=2&t[0]?a.return:t[0]?a.throw||((s=a.return)&&s.call(a),0):a.next)&&!(s=s.call(a,t[1])).done)return s;switch(a=0,(t=s?[2&t[0],s.value]:t)[0]){case 0:case 1:s=t;break;case 4:return c.label++,{value:t[1],done:!1};case 5:c.label++,a=t[1],t=[0];continue;case 7:t=c.ops.pop(),c.trys.pop();continue;default:if(!(s=0<(s=c.trys).length&&s[s.length-1])&&(6===t[0]||2===t[0])){c=0;continue}if(3===t[0]&&(!s||t[1]>s[0]&&t[1]<s[3]))c.label=t[1];else if(6===t[0]&&c.label<s[1])c.label=s[1],s=t;else{if(!(s&&c.label<s[2])){s[2]&&c.ops.pop(),c.trys.pop();continue}c.label=s[2],c.ops.push(t)}}t=r.call(n,c)}catch(e){t=[6,e],a=0}finally{i=s=0}if(5&t[0])throw t[1];return{value:t[0]?t[1]:void 0,done:!0}}}},axios_1=(Object.defineProperty(exports,"__esModule",{value:!0}),exports.MonitorFunction=exports.MonitorManagerFunction=exports.MonitorMongo=exports.MonitorManager=void 0,require("axios")),monitor_cpu_collection_1=require("../collections/monitor-cpu.collection"),monitor_memory_collection_1=require("../collections/monitor-memory.collection"),monitor_mongo_collection_1=require("../collections/monitor-mongo.collection"),resolveio_server_app_1=require("../resolveio-server-app"),common_1=require("../util/common"),monitor_function_collection_1=require("../collections/monitor-function.collection"),os=require("os"),MAX_BATCH_SIZE=8388608,MonitorManager=function(){function MonitorManager(){this._instanceHostname="",this._monitorData=[],this._instanceHostname=os.hostname().replace(/\./g,"-"),this.setupIntervals(),this.setupEventLoop()}return MonitorManager.prototype.cpuAverage=function(){for(var e=0,t=0,o=os.cpus(),n=0,r=o.length;n<r;n++){var i,a=o[n];for(i in a.times)t+=a.times[i];e+=a.times.idle}return{idle:e/o.length,total:t/o.length}},MonitorManager.prototype.setupIntervals=function(){return __awaiter(this,void 0,void 0,function(){var t,n,r=this;return __generator(this,function(e){switch(e.label){case 0:if("https://resolveio.com"===resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL||"http://localhost:4200"===resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL)return[3,4];e.label=1;case 1:return e.trys.push([1,3,,4]),[4,axios_1.default.get("http://169.254.169.254/latest/meta-data/instance-id")];case 2:return t=e.sent(),setInterval(function(){return __awaiter(r,void 0,void 0,function(){return __generator(this,function(e){switch(e.label){case 0:return e.trys.push([0,2,,3]),[4,axios_1.default.post("https://backend.resolveio.com/api/health",{type:"application",id_aws_instance:t.data,version:"number"==typeof process.env.APP_VERSION?process.env.APP_VERSION.toString():process.env.APP_VERSION,version_key:process.env.APP_VERSION_KEY})];case 1:return e.sent(),[3,3];case 2:return e.sent(),[3,3];case 3:return[2]}})})},1e4),[3,4];case 3:return e.sent(),[3,4];case 4:return n=this.cpuAverage(),setInterval(function(){var e=new Date,t=r.cpuAverage(),o=1-(t.idle-n.idle)/(t.total-n.total),o=("https://resolveio.com"===resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL||"http://localhost:4200"===resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL?monitor_cpu_collection_1.MonitorCPUs.create({_id:(0,common_1.objectIdHexString)(),metadata:{instance:r._instanceHostname,client:resolveio_server_app_1.ResolveIOServer.getServerConfig().CLIENT_NAME},date:e,app:o,system:os.loadavg()[0]}):r.pushMonitorData({type:"monitor-cpu",data:{metadata:{instance:r._instanceHostname,client:resolveio_server_app_1.ResolveIOServer.getServerConfig().CLIENT_NAME},date:e,app:o,system:os.loadavg()[0]}}),n=t,process.memoryUsage());"https://resolveio.com"===resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL||"http://localhost:4200"===resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL?monitor_memory_collection_1.MonitorMemorys.create({_id:(0,common_1.objectIdHexString)(),metadata:{instance:r._instanceHostname,client:resolveio_server_app_1.ResolveIOServer.getServerConfig().CLIENT_NAME},date:e,physical_total:o.heapTotal,physical_used:o.heapUsed,physical_free:os.freemem(),virtual:o.rss,private:o.external,physical:os.totalmem()}):r.pushMonitorData({type:"monitor-memory",data:{metadata:{instance:r._instanceHostname,client:resolveio_server_app_1.ResolveIOServer.getServerConfig().CLIENT_NAME},date:e,physical_total:o.heapTotal,physical_used:o.heapUsed,physical_free:os.freemem(),virtual:o.rss,private:o.external,physical:os.totalmem(),type:"memory"}})},3e3),[2]}})})},MonitorManager.prototype.addMongoTracker=function(e){"https://resolveio.com"===resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL||"http://localhost:4200"===resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL?e.collection.startsWith("monitor-")||monitor_mongo_collection_1.MonitorMongos.create({_id:(0,common_1.objectIdHexString)(),metadata:{instance:this._instanceHostname,client:resolveio_server_app_1.ResolveIOServer.getServerConfig().CLIENT_NAME},date:new Date(e.date),method:e.method,doc_collection:e.collection,duration:e.duration,query:Buffer.byteLength(e.query,"utf8")<158e5?e.query:"Too Big"}):this._instanceHostname&&this.pushMonitorData({type:"monitor-mongo",data:{metadata:{instance:this._instanceHostname,client:resolveio_server_app_1.ResolveIOServer.getServerConfig().CLIENT_NAME},date:new Date(e.date),method:e.method,doc_collection:e.collection,duration:e.duration,query:Buffer.byteLength(e.query,"utf8")<158e5?e.query:"Too Big"}})},MonitorManager.prototype.addFunctionTracker=function(e){"string"!=typeof e.data&&(e.data=JSON.stringify(e.data)),"https://resolveio.com"===resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL||"http://localhost:4200"===resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL?monitor_function_collection_1.MonitorFunctions.create({_id:(0,common_1.objectIdHexString)(),metadata:{instance:this._instanceHostname,client:resolveio_server_app_1.ResolveIOServer.getServerConfig().CLIENT_NAME},date:new Date(e.startTime),type:e.functionType,name:e.functionName,user:e.user,duration:e.duration,data:Buffer.byteLength(e.data,"utf8")<158e5?e.data:"Too Big"}):this._instanceHostname&&this.pushMonitorData({type:"monitor-function",data:{metadata:{instance:this._instanceHostname,client:resolveio_server_app_1.ResolveIOServer.getServerConfig().CLIENT_NAME},date:new Date(e.startTime),type:e.functionType,name:e.functionName,user:e.user,duration:e.duration,data:Buffer.byteLength(e.data,"utf8")<158e5?e.data:"Too Big"}})},MonitorManager.prototype.pushMonitorData=function(e){this._monitorData.push(e),this.estimateBatchSize()>=MAX_BATCH_SIZE&&this.flushMonitorData()},MonitorManager.prototype.estimateBatchSize=function(){return this._monitorData.reduce(function(e,t){return e+Buffer.byteLength(JSON.stringify(t))},0)},MonitorManager.prototype.setupEventLoop=function(){var e=this;setInterval(function(){0<e._monitorData.length&&e.flushMonitorData()},2500)},MonitorManager.prototype.flushMonitorData=function(){for(var e=[],t=0;0<this._monitorData.length&&t<MAX_BATCH_SIZE;){var o=this._monitorData.shift(),n=Buffer.byteLength(JSON.stringify(o));if(MAX_BATCH_SIZE<t+n&&0<e.length){this._monitorData.unshift(o);break}e.push(o),t+=n}try{resolveio_server_app_1.ResolveIOServer.getLocalLogManager().writeLogs(e)}catch(e){console.error(new Date,"MonitorManager","Failed to flush monitor data to file:",e)}},MonitorManager}(),MonitorMongo=(exports.MonitorManager=MonitorManager,function(){function MonitorMongo(e,t,o){this._startTime=0,this._method="",this._collection="",this._query="",this._startTime=Date.now(),this._method=e,this._collection=t,this._query=o}return MonitorMongo.prototype.finish=function(){var e=Date.now(),e={date:new Date(e),method:this._method,collection:this._collection,query:this._query,duration:e-this._startTime};resolveio_server_app_1.ResolveIOServer.getMainServer().getMonitorManager().addMongoTracker(e)},MonitorMongo}()),MonitorManagerFunction=(exports.MonitorMongo=MonitorMongo,function(){function MonitorManagerFunction(){this._functionCnt=0,this._functions=[],this._functionLastCompletedWS={}}return MonitorManagerFunction.prototype.startMonitorFunction=function(e,t,o,n,r){e=new MonitorFunction(this._functionCnt++,e,t,o,n,r);return this._functions.push(e),e.id},MonitorManagerFunction.prototype.finishMonitorFunction=function(t){var e=this._functions.find(function(e){return e.id===t});e&&(e.finish(),this._functionLastCompleted=e,this._functionLastCompletedWS[e.id_socket]=e,this._functions.splice(this._functions.map(function(e){return e.id}).indexOf(t),1))},MonitorManagerFunction.prototype.getActiveMonitorFunctions=function(){return this._functions},MonitorManagerFunction.prototype.getLastCompletedMonitorFunction=function(){return this._functionLastCompleted},MonitorManagerFunction.prototype.getLastCompletedMonitorFunctionWS=function(){return this._functionLastCompletedWS},MonitorManagerFunction}()),MonitorFunction=(exports.MonitorManagerFunction=MonitorManagerFunction,function(){function MonitorFunction(e,t,o,n,r,i){var a=this;this._timer=null,this.duration=0,this.startTime=null,this.endTime=null,this.lastTime=null,this.id_socket="",this.id=0,this.id=e,this.startTime=new Date,this.functionType=t,this.functionName=o,this.user=n,this.id_socket=r,this.data=i,this.lastTime=new Date,this._timer=setInterval(function(){var e=Date.now()-a.lastTime.getTime();e>a.duration&&(a.duration=e),a.lastTime=new Date},1e3)}return MonitorFunction.prototype.finish=function(){this._timer&&(clearInterval(this._timer),this._timer=null),this.endTime=new Date,this.duration,resolveio_server_app_1.ResolveIOServer.getMainServer().getMonitorManager().addFunctionTracker(this)},MonitorFunction}());exports.MonitorFunction=MonitorFunction;
1
+ "use strict";var __awaiter=this&&this.__awaiter||function(e,n,r,i){return new(r=r||Promise)(function(o,t){function fulfilled(e){try{step(i.next(e))}catch(e){t(e)}}function rejected(e){try{step(i.throw(e))}catch(e){t(e)}}function step(e){var t;e.done?o(e.value):((t=e.value)instanceof r?t:new r(function(e){e(t)})).then(fulfilled,rejected)}step((i=i.apply(e,n||[])).next())})},__generator=this&&this.__generator||function(n,r){var i,a,s,c={label:0,sent:function(){if(1&s[0])throw s[1];return s[1]},trys:[],ops:[]},u={next:verb(0),throw:verb(1),return:verb(2)};return"function"==typeof Symbol&&(u[Symbol.iterator]=function(){return this}),u;function verb(o){return function(e){var t=[o,e];if(i)throw new TypeError("Generator is already executing.");for(;c=u&&t[u=0]?0:c;)try{if(i=1,a&&(s=2&t[0]?a.return:t[0]?a.throw||((s=a.return)&&s.call(a),0):a.next)&&!(s=s.call(a,t[1])).done)return s;switch(a=0,(t=s?[2&t[0],s.value]:t)[0]){case 0:case 1:s=t;break;case 4:return c.label++,{value:t[1],done:!1};case 5:c.label++,a=t[1],t=[0];continue;case 7:t=c.ops.pop(),c.trys.pop();continue;default:if(!(s=0<(s=c.trys).length&&s[s.length-1])&&(6===t[0]||2===t[0])){c=0;continue}if(3===t[0]&&(!s||t[1]>s[0]&&t[1]<s[3]))c.label=t[1];else if(6===t[0]&&c.label<s[1])c.label=s[1],s=t;else{if(!(s&&c.label<s[2])){s[2]&&c.ops.pop(),c.trys.pop();continue}c.label=s[2],c.ops.push(t)}}t=r.call(n,c)}catch(e){t=[6,e],a=0}finally{i=s=0}if(5&t[0])throw t[1];return{value:t[0]?t[1]:void 0,done:!0}}}},axios_1=(Object.defineProperty(exports,"__esModule",{value:!0}),exports.MonitorFunction=exports.MonitorManagerFunction=exports.MonitorMongo=exports.MonitorManager=void 0,require("axios")),monitor_cpu_collection_1=require("../collections/monitor-cpu.collection"),monitor_function_collection_1=require("../collections/monitor-function.collection"),monitor_memory_collection_1=require("../collections/monitor-memory.collection"),monitor_mongo_collection_1=require("../collections/monitor-mongo.collection"),resolveio_server_app_1=require("../resolveio-server-app"),common_1=require("../util/common"),os=require("os"),MAX_BATCH_SIZE=8388608,MonitorManager=function(){function MonitorManager(){this._instanceHostname="",this._monitorData=[]}return MonitorManager.create=function(){return __awaiter(this,void 0,void 0,function(){var t;return __generator(this,function(e){switch(e.label){case 0:return[4,(t=new MonitorManager).initialize()];case 1:return e.sent(),[2,t]}})})},MonitorManager.prototype.initialize=function(){return __awaiter(this,void 0,void 0,function(){return __generator(this,function(e){switch(e.label){case 0:return this._instanceHostname=os.hostname().replace(/\./g,"-"),[4,this.setupIntervals()];case 1:return e.sent(),this.setupEventLoop(),[2]}})})},MonitorManager.prototype.cpuAverage=function(){for(var e=0,t=0,o=os.cpus(),n=0,r=o.length;n<r;n++){var i,a=o[n];for(i in a.times)t+=a.times[i];e+=a.times.idle}return{idle:e/o.length,total:t/o.length}},MonitorManager.prototype.setupIntervals=function(){return __awaiter(this,void 0,void 0,function(){var t,a,o=this;return __generator(this,function(e){switch(e.label){case 0:if("https://resolveio.com"===resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL||"http://localhost:4200"===resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL)return[3,4];e.label=1;case 1:return e.trys.push([1,3,,4]),[4,axios_1.default.get("http://169.254.169.254/latest/meta-data/instance-id")];case 2:return t=e.sent(),setInterval(function(){return __awaiter(o,void 0,void 0,function(){return __generator(this,function(e){switch(e.label){case 0:return e.trys.push([0,2,,3]),[4,axios_1.default.post("https://backend.resolveio.com/api/health",{type:"application",id_aws_instance:t.data,version:"number"==typeof process.env.APP_VERSION?process.env.APP_VERSION.toString():process.env.APP_VERSION,version_key:process.env.APP_VERSION_KEY})];case 1:return e.sent(),[3,3];case 2:return e.sent(),[3,3];case 3:return[2]}})})},1e4),[3,4];case 3:return e.sent(),[3,4];case 4:return a=this.cpuAverage(),setInterval(function(){return __awaiter(o,void 0,void 0,function(){var t,o,n,r,i;return __generator(this,function(e){switch(e.label){case 0:return t=new Date,o=this.cpuAverage(),r=o.idle-a.idle,n=o.total-a.total,r=1-r/n,"https://resolveio.com"!==resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL&&"http://localhost:4200"!==resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL?[3,2]:[4,monitor_cpu_collection_1.MonitorCPUs.create({_id:(0,common_1.objectIdHexString)(),metadata:{instance:this._instanceHostname,client:resolveio_server_app_1.ResolveIOServer.getServerConfig().CLIENT_NAME},date:t,app:r,system:os.loadavg()[0]})];case 1:return e.sent(),[3,3];case 2:this.pushMonitorData({type:"monitor-cpu",data:{metadata:{instance:this._instanceHostname,client:resolveio_server_app_1.ResolveIOServer.getServerConfig().CLIENT_NAME},date:t,app:r,system:os.loadavg()[0]}}),e.label=3;case 3:return a=o,i=process.memoryUsage(),"https://resolveio.com"!==resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL&&"http://localhost:4200"!==resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL?[3,5]:[4,monitor_memory_collection_1.MonitorMemorys.create({_id:(0,common_1.objectIdHexString)(),metadata:{instance:this._instanceHostname,client:resolveio_server_app_1.ResolveIOServer.getServerConfig().CLIENT_NAME},date:t,physical_total:i.heapTotal,physical_used:i.heapUsed,physical_free:os.freemem(),virtual:i.rss,private:i.external,physical:os.totalmem()})];case 4:return e.sent(),[3,6];case 5:this.pushMonitorData({type:"monitor-memory",data:{metadata:{instance:this._instanceHostname,client:resolveio_server_app_1.ResolveIOServer.getServerConfig().CLIENT_NAME},date:t,physical_total:i.heapTotal,physical_used:i.heapUsed,physical_free:os.freemem(),virtual:i.rss,private:i.external,physical:os.totalmem(),type:"memory"}}),e.label=6;case 6:return[2]}})})},3e3),[2]}})})},MonitorManager.prototype.addMongoTracker=function(t){return __awaiter(this,void 0,void 0,function(){return __generator(this,function(e){switch(e.label){case 0:return"https://resolveio.com"!==resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL&&"http://localhost:4200"!==resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL?[3,3]:t.collection.startsWith("monitor-")?[3,2]:[4,monitor_mongo_collection_1.MonitorMongos.create({_id:(0,common_1.objectIdHexString)(),metadata:{instance:this._instanceHostname,client:resolveio_server_app_1.ResolveIOServer.getServerConfig().CLIENT_NAME},date:new Date(t.date),method:t.method,doc_collection:t.collection,duration:t.duration,query:Buffer.byteLength(t.query,"utf8")<158e5?t.query:"Too Big"})];case 1:e.sent(),e.label=2;case 2:return[3,4];case 3:this._instanceHostname&&this.pushMonitorData({type:"monitor-mongo",data:{metadata:{instance:this._instanceHostname,client:resolveio_server_app_1.ResolveIOServer.getServerConfig().CLIENT_NAME},date:new Date(t.date),method:t.method,doc_collection:t.collection,duration:t.duration,query:Buffer.byteLength(t.query,"utf8")<158e5?t.query:"Too Big"}}),e.label=4;case 4:return[2]}})})},MonitorManager.prototype.addFunctionTracker=function(t){return __awaiter(this,void 0,void 0,function(){return __generator(this,function(e){switch(e.label){case 0:return"string"!=typeof t.data&&(t.data=JSON.stringify(t.data)),"https://resolveio.com"!==resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL&&"http://localhost:4200"!==resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL?[3,2]:[4,monitor_function_collection_1.MonitorFunctions.create({_id:(0,common_1.objectIdHexString)(),metadata:{instance:this._instanceHostname,client:resolveio_server_app_1.ResolveIOServer.getServerConfig().CLIENT_NAME},date:new Date(t.startTime),type:t.functionType,name:t.functionName,user:t.user,duration:t.duration,data:Buffer.byteLength(t.data,"utf8")<158e5?t.data:"Too Big"})];case 1:return e.sent(),[3,3];case 2:this._instanceHostname&&this.pushMonitorData({type:"monitor-function",data:{metadata:{instance:this._instanceHostname,client:resolveio_server_app_1.ResolveIOServer.getServerConfig().CLIENT_NAME},date:new Date(t.startTime),type:t.functionType,name:t.functionName,user:t.user,duration:t.duration,data:Buffer.byteLength(t.data,"utf8")<158e5?t.data:"Too Big"}}),e.label=3;case 3:return[2]}})})},MonitorManager.prototype.pushMonitorData=function(e){this._monitorData.push(e),this.estimateBatchSize()>=MAX_BATCH_SIZE&&this.flushMonitorData()},MonitorManager.prototype.estimateBatchSize=function(){return this._monitorData.reduce(function(e,t){return e+Buffer.byteLength(JSON.stringify(t))},0)},MonitorManager.prototype.setupEventLoop=function(){var e=this;setInterval(function(){0<e._monitorData.length&&e.flushMonitorData()},2500)},MonitorManager.prototype.flushMonitorData=function(){for(var e=[],t=0;0<this._monitorData.length&&t<MAX_BATCH_SIZE;){var o=this._monitorData.shift(),n=Buffer.byteLength(JSON.stringify(o));if(MAX_BATCH_SIZE<t+n&&0<e.length){this._monitorData.unshift(o);break}e.push(o),t+=n}try{resolveio_server_app_1.ResolveIOServer.getLocalLogManager().writeLogs(e)}catch(e){console.error(new Date,"MonitorManager","Failed to flush monitor data to file:",e)}},MonitorManager}(),MonitorMongo=(exports.MonitorManager=MonitorManager,function(){function MonitorMongo(){this._startTime=0,this._method="",this._collection="",this._query=""}return MonitorMongo.create=function(e,t,o){var n=new MonitorMongo;return n.initialize(e,t,o),n},MonitorMongo.prototype.initialize=function(e,t,o){this._startTime=Date.now(),this._method=e,this._collection=t,this._query=o},MonitorMongo.prototype.finish=function(){return __awaiter(this,void 0,void 0,function(){var t;return __generator(this,function(e){switch(e.label){case 0:return t=Date.now(),t={date:new Date(t),method:this._method,collection:this._collection,query:this._query,duration:t-this._startTime},[4,resolveio_server_app_1.ResolveIOServer.getMainServer().getMonitorManager().addMongoTracker(t)];case 1:return e.sent(),[2]}})})},MonitorMongo}()),MonitorManagerFunction=(exports.MonitorMongo=MonitorMongo,function(){function MonitorManagerFunction(){this._functionCnt=0,this._functions=[],this._functionLastCompletedWS={}}return MonitorManagerFunction.create=function(){var e=new MonitorManagerFunction;return e.initialize(),e},MonitorManagerFunction.prototype.initialize=function(){},MonitorManagerFunction.prototype.startMonitorFunction=function(e,t,o,n,r){e=MonitorFunction.create(this._functionCnt++,e,t,o,n,r);return this._functions.push(e),e.id},MonitorManagerFunction.prototype.finishMonitorFunction=function(o){return __awaiter(this,void 0,void 0,function(){var t;return __generator(this,function(e){switch(e.label){case 0:return(t=this._functions.find(function(e){return e.id===o}))?[4,t.finish()]:[3,2];case 1:e.sent(),this._functionLastCompleted=t,this._functionLastCompletedWS[t.id_socket]=t,this._functions.splice(this._functions.map(function(e){return e.id}).indexOf(o),1),e.label=2;case 2:return[2]}})})},MonitorManagerFunction.prototype.getActiveMonitorFunctions=function(){return this._functions},MonitorManagerFunction.prototype.getLastCompletedMonitorFunction=function(){return this._functionLastCompleted},MonitorManagerFunction.prototype.getLastCompletedMonitorFunctionWS=function(){return this._functionLastCompletedWS},MonitorManagerFunction}()),MonitorFunction=(exports.MonitorManagerFunction=MonitorManagerFunction,function(){function MonitorFunction(){this._timer=null,this.duration=0,this.startTime=null,this.endTime=null,this.lastTime=null,this.id_socket="",this.id=0}return MonitorFunction.create=function(e,t,o,n,r,i){var a=new MonitorFunction;return a.initialize(e,t,o,n,r,i),a},MonitorFunction.prototype.initialize=function(e,t,o,n,r,i){var a=this;this.id=e,this.startTime=new Date,this.functionType=t,this.functionName=o,this.user=n,this.id_socket=r,this.data=i,this.lastTime=new Date,this._timer=setInterval(function(){var e=Date.now()-a.lastTime.getTime();e>a.duration&&(a.duration=e),a.lastTime=new Date},1e3)},MonitorFunction.prototype.finish=function(){return __awaiter(this,void 0,void 0,function(){return __generator(this,function(e){switch(e.label){case 0:return this._timer&&(clearInterval(this._timer),this._timer=null),this.endTime=new Date,this.duration,[4,resolveio_server_app_1.ResolveIOServer.getMainServer().getMonitorManager().addFunctionTracker(this)];case 1:return e.sent(),[2]}})})},MonitorFunction}());exports.MonitorFunction=MonitorFunction;
2
2
  //# sourceMappingURL=monitor.manager.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/managers/monitor.manager.ts"],"names":["axios_1","require","monitor_cpu_collection_1","monitor_memory_collection_1","monitor_mongo_collection_1","resolveio_server_app_1","common_1","monitor_function_collection_1","os","MAX_BATCH_SIZE","MonitorManager","this","_instanceHostname","_monitorData","hostname","replace","setupIntervals","setupEventLoop","prototype","cpuAverage","totalIdle","totalTick","cpus","i","len","length","type","cpu","times","idle","total","ResolveIOServer","getServerConfig","default","get","instanceId_1","_a","sent","setInterval","__awaiter","_this","post","id_aws_instance","data","version","process","env","APP_VERSION","toString","version_key","APP_VERSION_KEY","lastCPU","now","Date","endMeasure","percentageCPU","memUsage","MonitorCPUs","create","_id","objectIdHexString","metadata","instance","client","date","app","system","loadavg","pushMonitorData","memoryUsage","MonitorMemorys","physical_total","heapTotal","physical_used","heapUsed","physical_free","freemem","virtual","rss","private","external","physical","totalmem","addMongoTracker","collection","startsWith","MonitorMongos","method","doc_collection","duration","query","Buffer","byteLength","addFunctionTracker","JSON","stringify","MonitorFunctions","startTime","functionType","name","functionName","user","push","estimateBatchSize","flushMonitorData","reduce","item","batch","batchSize","shift","itemSize","unshift","getLocalLogManager","writeLogs","error","console","MonitorMongo","exports","_startTime","_method","_collection","_query","finish","endTime","mongoMonitor","getMainServer","getMonitorManager","MonitorManagerFunction","_functionCnt","_functions","_functionLastCompletedWS","startMonitorFunction","id_socket","newMonitorFunction","MonitorFunction","id","finishMonitorFunction","monitor","find","a","_functionLastCompleted","splice","map","indexOf","getActiveMonitorFunctions","getLastCompletedMonitorFunction","getLastCompletedMonitorFunctionWS","_timer","lastTime","diff","getTime","clearInterval"],"mappings":"k8CAAAA,S,iKAAAC,QAAA,OAAA,GACAC,yBAAAD,QAAA,uCAAA,EACAE,4BAAAF,QAAA,0CAAA,EACAG,2BAAAH,QAAA,yCAAA,EACAI,uBAAAJ,QAAA,yBAAA,EACAK,SAAAL,QAAA,gBAAA,EAEAM,8BAAAN,QAAA,4CAAA,EAEMO,GAAKP,QAAQ,IAAI,EACjBQ,eAAiB,QAEvBC,eAAA,WAIC,SAAAA,iBAHQC,KAAAC,kBAAoB,GACpBD,KAAAE,aAAgC,GAGvCF,KAAKC,kBAAoBJ,GAAGM,SAAQ,EAAGC,QAAQ,MAAO,GAAG,EAEzDJ,KAAKK,eAAc,EACnBL,KAAKM,eAAc,CACpB,CAkRD,OAhRCP,eAAAQ,UAAAC,WAAA,WAIC,IAHA,IAAIC,EAAY,EAAGC,EAAY,EAC3BC,EAAOd,GAAGc,KAAI,EAETC,EAAI,EAAGC,EAAMF,EAAKG,OAAQF,EAAIC,EAAKD,CAAC,GAAI,CAChD,IAESG,EAFLC,EAAML,EAAKC,GAEf,IAASG,KAAQC,EAAIC,MACpBP,GAAaM,EAAIC,MAAMF,GAExBN,GAAaO,EAAIC,MAAMC,I,CAGxB,MAAO,CACNA,KAAMT,EAAYE,EAAKG,OACvBK,MAAOT,EAAYC,EAAKG,M,CAE1B,EAEMf,eAAAQ,UAAAF,eAAN,W,4HAGoD,0BAAlDX,uBAAA0B,gBAAgBC,gBAAe,EAAa,UACQ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uBAAA0B,gBAAgB8F,cAAa,EAAGC,kBAAiB,EAAGjC,mBAAmBlF,IAAI,CAC5E,EACD2H,eAAA,EAAC,GAvDYjB,QAAAiB,gBAAAA","file":"monitor.manager.js","sourcesContent":["import axios from 'axios';\nimport { MonitorCPUs } from '../collections/monitor-cpu.collection';\nimport { MonitorMemorys } from '../collections/monitor-memory.collection';\nimport { MonitorMongos } from '../collections/monitor-mongo.collection';\nimport { ResolveIOServer } from '../resolveio-server-app';\nimport { objectIdHexString } from '../util/common';\nimport { LocalLogModel } from './local-log.manager';\nimport { MonitorFunctions } from '../collections/monitor-function.collection';\n\nconst os = require('os');\nconst MAX_BATCH_SIZE = 8 * 1024 * 1024; // 8MB max for safety\n\nexport class MonitorManager {\n\tprivate _instanceHostname = '';\n\tprivate _monitorData: LocalLogModel[] = [];\n\n\tconstructor() {\n\t\tthis._instanceHostname = os.hostname().replace(/\\./g, '-');\n\n\t\tthis.setupIntervals();\n\t\tthis.setupEventLoop();\n\t}\n\n\tcpuAverage() {\n\t\tvar totalIdle = 0, totalTick = 0;\n\t\tvar cpus = os.cpus();\n\n\t\tfor (var i = 0, len = cpus.length; i < len; i++) {\n\t\t\tvar cpu = cpus[i];\n\n\t\t\tfor (let type in cpu.times) {\n\t\t\t\ttotalTick += cpu.times[type];\n\t\t\t}\n\t\t\ttotalIdle += cpu.times.idle;\n\t\t}\n\n\t\treturn {\n\t\t\tidle: totalIdle / cpus.length,\n\t\t\ttotal: totalTick / cpus.length\n\t\t};\n\t}\n\n\tasync setupIntervals() {\n\t\t// Example: If not on production or localhost, do a health check\n\t\tif (\n\t\t\tResolveIOServer.getServerConfig()['ROOT_URL'] !== 'https://resolveio.com'\n\t\t&& ResolveIOServer.getServerConfig()['ROOT_URL'] !== 'http://localhost:4200'\n\t\t) {\n\t\t\ttry {\n\t\t\t\tlet instanceId = await axios.get('http://169.254.169.254/latest/meta-data/instance-id');\n\n\t\t\t\tsetInterval(async () => {\n\t\t\t\t\ttry {\n\t\t\t\t\t\tawait axios.post('https://backend.resolveio.com/api/health', {\n\t\t\t\t\t\t\ttype: 'application',\n\t\t\t\t\t\t\tid_aws_instance: instanceId.data,\n\t\t\t\t\t\t\tversion: typeof process.env.APP_VERSION === 'number'\n\t\t\t\t\t\t\t\t? (<any>process.env.APP_VERSION).toString()\n\t\t\t\t\t\t\t\t: process.env.APP_VERSION,\n\t\t\t\t\t\t\tversion_key: process.env.APP_VERSION_KEY\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\t\t\t\t\tcatch(e) {}\n\t\t\t\t}, 10000);\n\t\t\t}\n\t\t\tcatch(errHealth) {}\n\t\t}\n\n\t\tlet lastCPU = this.cpuAverage();\n\n\t\tsetInterval(() => {\n\t\t\tlet now = new Date();\n\t\t\tvar endMeasure = this.cpuAverage();\n\t\t\tvar idleDifference = endMeasure.idle - lastCPU.idle;\n\t\t\tvar totalDifference = endMeasure.total - lastCPU.total;\n\t\t\tvar percentageCPU = 1 - idleDifference / totalDifference;\n\n\t\t\t// Original local code\n\t\t\tif (\n\t\t\t\tResolveIOServer.getServerConfig()['ROOT_URL'] === 'https://resolveio.com'\n\t\t\t|| ResolveIOServer.getServerConfig()['ROOT_URL'] === 'http://localhost:4200'\n\t\t\t) {\n\t\t\t\tMonitorCPUs.create({\n\t\t\t\t\t_id: objectIdHexString(),\n\t\t\t\t\tmetadata: {\n\t\t\t\t\t\tinstance: this._instanceHostname,\n\t\t\t\t\t\tclient: ResolveIOServer.getServerConfig()['CLIENT_NAME']\n\t\t\t\t\t},\n\t\t\t\t\tdate: now,\n\t\t\t\t\tapp: percentageCPU,\n\t\t\t\t\tsystem: os.loadavg()[0]\n\t\t\t\t});\n\t\t\t}\n\t\t\telse {\n\t\t\t\tthis.pushMonitorData({\n\t\t\t\t\ttype: 'monitor-cpu',\n\t\t\t\t\tdata: {\n\t\t\t\t\t\tmetadata: {\n\t\t\t\t\t\t\tinstance: this._instanceHostname,\n\t\t\t\t\t\t\tclient: ResolveIOServer.getServerConfig()['CLIENT_NAME']\n\t\t\t\t\t\t},\n\t\t\t\t\t\tdate: now,\n\t\t\t\t\t\tapp: percentageCPU,\n\t\t\t\t\t\tsystem: os.loadavg()[0]\n\t\t\t\t\t}\n\t\t\t\t});\n\t\t\t}\n\n\t\t\tlastCPU = endMeasure;\n\n\t\t\tconst memUsage = process.memoryUsage();\n\n\t\t\tif (\n\t\t\t\tResolveIOServer.getServerConfig()['ROOT_URL'] === 'https://resolveio.com'\n\t\t\t|| ResolveIOServer.getServerConfig()['ROOT_URL'] === 'http://localhost:4200'\n\t\t\t) {\n\t\t\t\tMonitorMemorys.create({\n\t\t\t\t\t_id: objectIdHexString(),\n\t\t\t\t\tmetadata: {\n\t\t\t\t\t\tinstance: this._instanceHostname,\n\t\t\t\t\t\tclient: ResolveIOServer.getServerConfig()['CLIENT_NAME']\n\t\t\t\t\t},\n\t\t\t\t\tdate: now,\n\t\t\t\t\tphysical_total: memUsage.heapTotal,\n\t\t\t\t\tphysical_used: memUsage.heapUsed,\n\t\t\t\t\tphysical_free: os.freemem(),\n\t\t\t\t\tvirtual: memUsage.rss,\n\t\t\t\t\tprivate: memUsage.external,\n\t\t\t\t\tphysical: os.totalmem()\n\t\t\t\t});\n\t\t\t}\n\t\t\telse {\n\t\t\t\tthis.pushMonitorData({\n\t\t\t\t\ttype: 'monitor-memory',\n\t\t\t\t\tdata: {\n\t\t\t\t\t\tmetadata: {\n\t\t\t\t\t\t\tinstance: this._instanceHostname,\n\t\t\t\t\t\t\tclient: ResolveIOServer.getServerConfig()['CLIENT_NAME']\n\t\t\t\t\t\t},\n\t\t\t\t\t\tdate: now,\n\t\t\t\t\t\tphysical_total: memUsage.heapTotal,\n\t\t\t\t\t\tphysical_used: memUsage.heapUsed,\n\t\t\t\t\t\tphysical_free: os.freemem(),\n\t\t\t\t\t\tvirtual: memUsage.rss,\n\t\t\t\t\t\tprivate: memUsage.external,\n\t\t\t\t\t\tphysical: os.totalmem(),\n\t\t\t\t\t\ttype: 'memory'\n\t\t\t\t\t}\n\t\t\t\t});\n\t\t\t}\n\t\t}, 3000);\n\t}\n\n\taddMongoTracker(data: MongoMonitorModel) {\n\t\tif (\n\t\t\tResolveIOServer.getServerConfig()['ROOT_URL'] === 'https://resolveio.com'\n\t\t\t|| ResolveIOServer.getServerConfig()['ROOT_URL'] === 'http://localhost:4200'\n\t\t) {\n\t\t\tif (!data.collection.startsWith('monitor-')) {\n\t\t\t\tMonitorMongos.create({\n\t\t\t\t\t_id: objectIdHexString(),\n\t\t\t\t\tmetadata: {\n\t\t\t\t\t\tinstance: this._instanceHostname,\n\t\t\t\t\t\tclient: ResolveIOServer.getServerConfig()['CLIENT_NAME']\n\t\t\t\t\t},\n\t\t\t\t\tdate: new Date(data.date),\n\t\t\t\t\tmethod: data.method,\n\t\t\t\t\tdoc_collection: data.collection,\n\t\t\t\t\tduration: data.duration,\n\t\t\t\t\tquery: Buffer.byteLength(data.query, 'utf8') < 15800000\n\t\t\t\t\t\t? data.query\n\t\t\t\t\t\t: 'Too Big'\n\t\t\t\t});\n\t\t\t}\n\t\t}\n\t\telse {\n\t\t\tif (this._instanceHostname) {\n\t\t\t\tthis.pushMonitorData({\n\t\t\t\t\ttype: 'monitor-mongo',\n\t\t\t\t\tdata: {\n\t\t\t\t\t\tmetadata: {\n\t\t\t\t\t\t\tinstance: this._instanceHostname,\n\t\t\t\t\t\t\tclient: ResolveIOServer.getServerConfig()['CLIENT_NAME']\n\t\t\t\t\t\t},\n\t\t\t\t\t\tdate: new Date(data.date),\n\t\t\t\t\t\tmethod: data.method,\n\t\t\t\t\t\tdoc_collection: data.collection,\n\t\t\t\t\t\tduration: data.duration,\n\t\t\t\t\t\tquery: Buffer.byteLength(data.query, 'utf8') < 15800000\n\t\t\t\t\t\t\t? data.query\n\t\t\t\t\t\t\t: 'Too Big'\n\t\t\t\t\t}\n\t\t\t\t});\n\t\t\t}\n\t\t}\n\t}\n\n\taddFunctionTracker(data: MonitorFunction) {\n\t\tif (typeof(data.data) !== 'string') {\n\t\t\tdata.data = JSON.stringify(data.data);\n\t\t}\n\n\t\tif (\n\t\t\tResolveIOServer.getServerConfig()['ROOT_URL'] === 'https://resolveio.com'\n\t\t\t|| ResolveIOServer.getServerConfig()['ROOT_URL'] === 'http://localhost:4200'\n\t\t) {\n\t\t\tMonitorFunctions.create({\n\t\t\t\t_id: objectIdHexString(),\n\t\t\t\tmetadata: {\n\t\t\t\t\tinstance: this._instanceHostname,\n\t\t\t\t\tclient: ResolveIOServer.getServerConfig()['CLIENT_NAME']\n\t\t\t\t},\n\t\t\t\tdate: new Date(data.startTime),\n\t\t\t\ttype: data.functionType,\n\t\t\t\tname: data.functionName,\n\t\t\t\tuser: data.user,\n\t\t\t\tduration: data.duration,\n\t\t\t\tdata: Buffer.byteLength(data.data, 'utf8') < 15800000\n\t\t\t\t\t? data.data\n\t\t\t\t\t: 'Too Big'\n\t\t\t});\n\t\t}\n\t\telse {\n\t\t\tif (this._instanceHostname) {\n\t\t\t\tthis.pushMonitorData({\n\t\t\t\t\ttype: 'monitor-function',\n\t\t\t\t\tdata: {\n\t\t\t\t\t\tmetadata: {\n\t\t\t\t\t\t\tinstance: this._instanceHostname,\n\t\t\t\t\t\t\tclient: ResolveIOServer.getServerConfig()['CLIENT_NAME']\n\t\t\t\t\t\t},\n\t\t\t\t\t\tdate: new Date(data.startTime),\n\t\t\t\t\t\ttype: data.functionType,\n\t\t\t\t\t\tname: data.functionName,\n\t\t\t\t\t\tuser: data.user,\n\t\t\t\t\t\tduration: data.duration,\n\t\t\t\t\t\tdata: Buffer.byteLength(data.data, 'utf8') < 15800000\n\t\t\t\t\t\t\t? data.data\n\t\t\t\t\t\t\t: 'Too Big'\n\t\t\t\t\t}\n\t\t\t\t});\n\t\t\t}\n\t\t}\n\t}\n\n\tprivate pushMonitorData(data: LocalLogModel) {\n\t\tthis._monitorData.push(data);\n\n\t\t// If data is huge, flush immediately to file\n\t\tif (this.estimateBatchSize() >= MAX_BATCH_SIZE) {\n\t\t\tthis.flushMonitorData();\n\t\t}\n\t}\n\n\tprivate estimateBatchSize(): number {\n\t\treturn this._monitorData.reduce((total, item) => {\n\t\t\treturn total + Buffer.byteLength(JSON.stringify(item));\n\t\t}, 0);\n\t}\n\n\tsetupEventLoop() {\n\t\t// Flush every 2.5 seconds\n\t\tsetInterval(() => {\n\t\t\tif (this._monitorData.length > 0) {\n\t\t\t\tthis.flushMonitorData();\n\t\t\t}\n\t\t}, 2500);\n\t}\n\n\tprivate flushMonitorData() {\n\t\tconst batch = [];\n\t\tlet batchSize = 0;\n\n\t\twhile (this._monitorData.length > 0 && batchSize < MAX_BATCH_SIZE) {\n\t\t\tconst item = this._monitorData.shift();\n\t\t\tconst itemSize = Buffer.byteLength(JSON.stringify(item));\n\n\t\t\tif ((batchSize + itemSize) > MAX_BATCH_SIZE && batch.length > 0) {\n\t\t\t\t// put the item back for next round\n\t\t\t\tthis._monitorData.unshift(item);\n\t\t\t\tbreak;\n\t\t\t}\n\n\t\t\tbatch.push(item);\n\t\t\tbatchSize += itemSize;\n\t\t}\n\n\t\ttry {\n\t\t\t// Now, instead of calling an external HTTP endpoint, we write the batch to disk:\n\t\t\tResolveIOServer.getLocalLogManager().writeLogs(batch);\n\t\t}\n\t\tcatch (error) {\n\t\t\tconsole.error(new Date(), 'MonitorManager', 'Failed to flush monitor data to file:', error);\n\t\t}\n\t}\n}\n\nexport interface MongoMonitorModel {\n\tdate: Date;\n\tmethod: string;\n\tcollection: string;\n\tduration: number;\n\tquery: string;\n}\n\nexport class MonitorMongo {\n\tprivate _startTime = 0;\n\tprivate _method = '';\n\tprivate _collection = '';\n\tprivate _query = '';\n\n\tconstructor(method: string, collection: string, query: string) {\n\t\tthis._startTime = Date.now();\n\t\tthis._method = method;\n\t\tthis._collection = collection;\n\t\tthis._query = query;\n\t}\n\n\tpublic finish() {\n\t\tlet endTime = Date.now();\n\n\t\tlet mongoMonitor: MongoMonitorModel = {\n\t\t\tdate: new Date(endTime),\n\t\t\tmethod: this._method,\n\t\t\tcollection: this._collection,\n\t\t\tquery: this._query,\n\t\t\tduration: endTime - this._startTime\n\t\t};\n\n\t\tResolveIOServer.getMainServer().getMonitorManager().addMongoTracker(mongoMonitor);\n\t}\n}\n\nexport class MonitorManagerFunction {\n\tprivate _functionCnt = 0;\n\tprivate _functions: MonitorFunction[] = [];\n\tprivate _functionLastCompletedWS: { [id_socket: string]: MonitorFunction; } = {};\n\tprivate _functionLastCompleted: MonitorFunction;\n\n\tconstructor() {}\n\n\tstartMonitorFunction(\n\t\tfunctionType: MonitorFunctionType,\n\t\tfunctionName: string,\n\t\tuser: string,\n\t\tid_socket: string,\n\t\tdata: any\n\t) {\n\t\tlet newMonitorFunction = new MonitorFunction(\n\t\t\tthis._functionCnt++,\n\t\t\tfunctionType,\n\t\t\tfunctionName,\n\t\t\tuser,\n\t\t\tid_socket,\n\t\t\tdata\n\t\t);\n\n\t\tthis._functions.push(newMonitorFunction);\n\t\treturn newMonitorFunction.id;\n\t}\n\n\tfinishMonitorFunction(id: number) {\n\t\tlet monitor = this._functions.find(a => a.id === id);\n\n\t\tif (monitor) {\n\t\t\tmonitor.finish();\n\t\t\tthis._functionLastCompleted = monitor;\n\t\t\tthis._functionLastCompletedWS[monitor.id_socket] = monitor;\n\t\t\tthis._functions.splice(\n\t\t\t\tthis._functions.map(a => a.id).indexOf(id),\n\t\t\t\t1\n\t\t\t);\n\t\t}\n\t}\n\n\tgetActiveMonitorFunctions() {\n\t\treturn this._functions;\n\t}\n\n\tgetLastCompletedMonitorFunction() {\n\t\treturn this._functionLastCompleted;\n\t}\n\n\tgetLastCompletedMonitorFunctionWS() {\n\t\treturn this._functionLastCompletedWS;\n\t}\n}\n\nexport type MonitorFunctionType =\n\t'Cron Method'\n\t| 'Method'\n\t| 'User Specific Publication'\n\t| 'Publication';\n\nexport class MonitorFunction {\n\tprivate _timer: NodeJS.Timeout = null;\n\tpublic duration = 0;\n\tpublic functionType: MonitorFunctionType;\n\tpublic functionName: string;\n\tpublic user: string;\n\tpublic data: any;\n\tpublic startTime: Date = null;\n\tpublic endTime: Date = null;\n\tpublic lastTime: Date = null;\n\tpublic id_socket = '';\n\tpublic id = 0;\n\n\tconstructor(\n\t\tid: number,\n\t\tfunctionType: MonitorFunctionType,\n\t\tfunctionName: string,\n\t\tuser: string,\n\t\tid_socket: string,\n\t\tdata: any\n\t) {\n\t\tthis.id = id;\n\t\tthis.startTime = new Date();\n\t\tthis.functionType = functionType;\n\t\tthis.functionName = functionName;\n\t\tthis.user = user;\n\t\tthis.id_socket = id_socket;\n\t\tthis.data = data;\n\t\tthis.lastTime = new Date();\n\n\t\tthis._timer = setInterval(() => {\n\t\t\tlet diff = Date.now() - this.lastTime.getTime();\n\n\t\t\tif (diff > this.duration) {\n\t\t\t\tthis.duration = diff;\n\t\t\t}\n\n\t\t\tthis.lastTime = new Date();\n\t\t}, 1000);\n\t}\n\n\tpublic finish() {\n\t\tif (this._timer) {\n\t\t\tclearInterval(this._timer);\n\t\t\tthis._timer = null;\n\t\t}\n\n\t\tthis.endTime = new Date();\n\n\t\tif (this.duration >= 5000) {\n\t\t\t// Optional slow function logging or email\n\t\t}\n\n\t\tResolveIOServer.getMainServer().getMonitorManager().addFunctionTracker(this);\n\t}\n}"]}
1
+ {"version":3,"sources":["../../src/managers/monitor.manager.ts"],"names":["axios_1","require","monitor_cpu_collection_1","monitor_function_collection_1","monitor_memory_collection_1","monitor_mongo_collection_1","resolveio_server_app_1","common_1","os","MAX_BATCH_SIZE","MonitorManager","this","_instanceHostname","_monitorData","create","monitorManager","initialize","_a","sent","prototype","hostname","replace","setupIntervals","setupEventLoop","cpuAverage","totalIdle","totalTick","cpus","i","len","length","type","cpu","times","idle","total","ResolveIOServer","getServerConfig","default","get","instanceId_1","setInterval","__awaiter","_this","post","id_aws_instance","data","version","process","env","APP_VERSION","toString","version_key","APP_VERSION_KEY","lastCPU","now","Date","endMeasure","idleDifference","totalDifference","percentageCPU","MonitorCPUs","_id","objectIdHexString","metadata","instance","client","date","app","system","loadavg","pushMonitorData","memUsage","memoryUsage","MonitorMemorys","physical_total","heapTotal","physical_used","heapUsed","physical_free","freemem","virtual","rss","private","external","physical","totalmem","addMongoTracker","collection","startsWith","MonitorMongos","method","doc_collection","duration","query","Buffer","byteLength","addFunctionTracker","JSON","stringify","MonitorFunctions","startTime","functionType","name","functionName","user","push","estimateBatchSize","flushMonitorData","reduce","item","batch","batchSize","shift","itemSize","unshift","getLocalLogManager","writeLogs","error","console","MonitorMongo","exports","_startTime","_method","_collection","_query","monitorMongo","finish","endTime","mongoMonitor","getMainServer","getMonitorManager","MonitorManagerFunction","_functionCnt","_functions","_functionLastCompletedWS","monitorManagerFunction","startMonitorFunction","id_socket","newMonitorFunction","MonitorFunction","id","finishMonitorFunction","monitor","find","a","_functionLastCompleted","splice","map","indexOf","getActiveMonitorFunctions","getLastCompletedMonitorFunction","getLastCompletedMonitorFunctionWS","_timer","lastTime","monitorFunction","diff","getTime","clearInterval"],"mappings":"k8CAAAA,S,iKAAAC,QAAA,OAAA,GACAC,yBAAAD,QAAA,uCAAA,EACAE,8BAAAF,QAAA,4CAAA,EACAG,4BAAAH,QAAA,0CAAA,EACAI,2BAAAJ,QAAA,yCAAA,EACAK,uBAAAL,QAAA,yBAAA,EACAM,SAAAN,QAAA,gBAAA,EAGMO,GAAKP,QAAQ,IAAI,EACjBQ,eAAiB,QAEvBC,eAAA,WAIC,SAAAA,iBAHQC,KAAAC,kBAAoB,GACpBD,KAAAE,aAAgC,EAEzB,CA+RhB,OA7RcH,eAAAI,OAAb,W,gHAEC,MAAA,CAAA,GADMC,EAAiB,IAAIL,gBACNM,WAAU,G,OAC/B,OADAC,EAAAC,KAAA,EACA,CAAA,EAAOH,E,OAGML,eAAAS,UAAAH,WAAd,W,0GAGC,OAFAL,KAAKC,kBAAoBJ,GAAGY,SAAQ,EAAGC,QAAQ,MAAO,GAAG,EAEzD,CAAA,EAAMV,KAAKW,eAAc,G,cAAzBL,EAAAC,KAAA,EACAP,KAAK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aoF,gBAAAzH,UAAA4G,OAAb,W,0GAYC,OAXIpH,KAAK8I,SACRK,cAAcnJ,KAAK8I,MAAM,EACzB9I,KAAK8I,OAAS,MAGf9I,KAAKqH,QAAU,IAAIxE,KAEf7C,KAAKkF,SAIT,CAAA,EAAMvF,uBAAA8B,gBAAgB8F,cAAa,EAAGC,kBAAiB,EAAGlC,mBAAmBtF,IAAI,G,cAAjFM,EAAAC,KAAA,E,UAEF0H,eAAA,EAAC,GAtEYnB,QAAAmB,gBAAAA","file":"monitor.manager.js","sourcesContent":["import axios from 'axios';\nimport { MonitorCPUs } from '../collections/monitor-cpu.collection';\nimport { MonitorFunctions } from '../collections/monitor-function.collection';\nimport { MonitorMemorys } from '../collections/monitor-memory.collection';\nimport { MonitorMongos } from '../collections/monitor-mongo.collection';\nimport { ResolveIOServer } from '../resolveio-server-app';\nimport { objectIdHexString } from '../util/common';\nimport { LocalLogModel } from './local-log.manager';\n\nconst os = require('os');\nconst MAX_BATCH_SIZE = 8 * 1024 * 1024; // 8MB max for safety\n\nexport class MonitorManager {\n\tprivate _instanceHostname = '';\n\tprivate _monitorData: LocalLogModel[] = [];\n\n\tconstructor() {}\n\n\tstatic async create() {\n\t\tconst monitorManager = new MonitorManager();\n\t\tawait monitorManager.initialize();\n\t\treturn monitorManager;\n\t}\n\n\tprivate async initialize() {\n\t\tthis._instanceHostname = os.hostname().replace(/\\./g, '-');\n\n\t\tawait this.setupIntervals();\n\t\tthis.setupEventLoop();\n\t}\n\n\tcpuAverage() {\n\t\tvar totalIdle = 0, totalTick = 0;\n\t\tvar cpus = os.cpus();\n\n\t\tfor (var i = 0, len = cpus.length; i < len; i++) {\n\t\t\tvar cpu = cpus[i];\n\n\t\t\tfor (let type in cpu.times) {\n\t\t\t\ttotalTick += cpu.times[type];\n\t\t\t}\n\t\t\ttotalIdle += cpu.times.idle;\n\t\t}\n\n\t\treturn {\n\t\t\tidle: totalIdle / cpus.length,\n\t\t\ttotal: totalTick / cpus.length\n\t\t};\n\t}\n\n\tasync setupIntervals() {\n\t\t// Example: If not on production or localhost, do a health check\n\t\tif (\n\t\t\tResolveIOServer.getServerConfig()['ROOT_URL'] !== 'https://resolveio.com'\n\t\t&& ResolveIOServer.getServerConfig()['ROOT_URL'] !== 'http://localhost:4200'\n\t\t) {\n\t\t\ttry {\n\t\t\t\tlet instanceId = await axios.get('http://169.254.169.254/latest/meta-data/instance-id');\n\n\t\t\t\tsetInterval(async () => {\n\t\t\t\t\ttry {\n\t\t\t\t\t\tawait axios.post('https://backend.resolveio.com/api/health', {\n\t\t\t\t\t\t\ttype: 'application',\n\t\t\t\t\t\t\tid_aws_instance: instanceId.data,\n\t\t\t\t\t\t\tversion: typeof process.env.APP_VERSION === 'number'\n\t\t\t\t\t\t\t\t? (<any>process.env.APP_VERSION).toString()\n\t\t\t\t\t\t\t\t: process.env.APP_VERSION,\n\t\t\t\t\t\t\tversion_key: process.env.APP_VERSION_KEY\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\t\t\t\t\tcatch(e) {}\n\t\t\t\t}, 10000);\n\t\t\t}\n\t\t\tcatch(errHealth) {}\n\t\t}\n\n\t\tlet lastCPU = this.cpuAverage();\n\n\t\tsetInterval(async () => {\n\t\t\tlet now = new Date();\n\t\t\tvar endMeasure = this.cpuAverage();\n\t\t\tvar idleDifference = endMeasure.idle - lastCPU.idle;\n\t\t\tvar totalDifference = endMeasure.total - lastCPU.total;\n\t\t\tvar percentageCPU = 1 - idleDifference / totalDifference;\n\n\t\t\t// Original local code\n\t\t\tif (\n\t\t\t\tResolveIOServer.getServerConfig()['ROOT_URL'] === 'https://resolveio.com'\n\t\t\t|| ResolveIOServer.getServerConfig()['ROOT_URL'] === 'http://localhost:4200'\n\t\t\t) {\n\t\t\t\tawait MonitorCPUs.create({\n\t\t\t\t\t_id: objectIdHexString(),\n\t\t\t\t\tmetadata: {\n\t\t\t\t\t\tinstance: this._instanceHostname,\n\t\t\t\t\t\tclient: ResolveIOServer.getServerConfig()['CLIENT_NAME']\n\t\t\t\t\t},\n\t\t\t\t\tdate: now,\n\t\t\t\t\tapp: percentageCPU,\n\t\t\t\t\tsystem: os.loadavg()[0]\n\t\t\t\t});\n\t\t\t}\n\t\t\telse {\n\t\t\t\tthis.pushMonitorData({\n\t\t\t\t\ttype: 'monitor-cpu',\n\t\t\t\t\tdata: {\n\t\t\t\t\t\tmetadata: {\n\t\t\t\t\t\t\tinstance: this._instanceHostname,\n\t\t\t\t\t\t\tclient: ResolveIOServer.getServerConfig()['CLIENT_NAME']\n\t\t\t\t\t\t},\n\t\t\t\t\t\tdate: now,\n\t\t\t\t\t\tapp: percentageCPU,\n\t\t\t\t\t\tsystem: os.loadavg()[0]\n\t\t\t\t\t}\n\t\t\t\t});\n\t\t\t}\n\n\t\t\tlastCPU = endMeasure;\n\n\t\t\tconst memUsage = process.memoryUsage();\n\n\t\t\tif (\n\t\t\t\tResolveIOServer.getServerConfig()['ROOT_URL'] === 'https://resolveio.com'\n\t\t\t|| ResolveIOServer.getServerConfig()['ROOT_URL'] === 'http://localhost:4200'\n\t\t\t) {\n\t\t\t\tawait MonitorMemorys.create({\n\t\t\t\t\t_id: objectIdHexString(),\n\t\t\t\t\tmetadata: {\n\t\t\t\t\t\tinstance: this._instanceHostname,\n\t\t\t\t\t\tclient: ResolveIOServer.getServerConfig()['CLIENT_NAME']\n\t\t\t\t\t},\n\t\t\t\t\tdate: now,\n\t\t\t\t\tphysical_total: memUsage.heapTotal,\n\t\t\t\t\tphysical_used: memUsage.heapUsed,\n\t\t\t\t\tphysical_free: os.freemem(),\n\t\t\t\t\tvirtual: memUsage.rss,\n\t\t\t\t\tprivate: memUsage.external,\n\t\t\t\t\tphysical: os.totalmem()\n\t\t\t\t});\n\t\t\t}\n\t\t\telse {\n\t\t\t\tthis.pushMonitorData({\n\t\t\t\t\ttype: 'monitor-memory',\n\t\t\t\t\tdata: {\n\t\t\t\t\t\tmetadata: {\n\t\t\t\t\t\t\tinstance: this._instanceHostname,\n\t\t\t\t\t\t\tclient: ResolveIOServer.getServerConfig()['CLIENT_NAME']\n\t\t\t\t\t\t},\n\t\t\t\t\t\tdate: now,\n\t\t\t\t\t\tphysical_total: memUsage.heapTotal,\n\t\t\t\t\t\tphysical_used: memUsage.heapUsed,\n\t\t\t\t\t\tphysical_free: os.freemem(),\n\t\t\t\t\t\tvirtual: memUsage.rss,\n\t\t\t\t\t\tprivate: memUsage.external,\n\t\t\t\t\t\tphysical: os.totalmem(),\n\t\t\t\t\t\ttype: 'memory'\n\t\t\t\t\t}\n\t\t\t\t});\n\t\t\t}\n\t\t}, 3000);\n\t}\n\n\tasync addMongoTracker(data: MongoMonitorModel) {\n\t\tif (\n\t\t\tResolveIOServer.getServerConfig()['ROOT_URL'] === 'https://resolveio.com'\n\t\t\t|| ResolveIOServer.getServerConfig()['ROOT_URL'] === 'http://localhost:4200'\n\t\t) {\n\t\t\tif (!data.collection.startsWith('monitor-')) {\n\t\t\t\tawait MonitorMongos.create({\n\t\t\t\t\t_id: objectIdHexString(),\n\t\t\t\t\tmetadata: {\n\t\t\t\t\t\tinstance: this._instanceHostname,\n\t\t\t\t\t\tclient: ResolveIOServer.getServerConfig()['CLIENT_NAME']\n\t\t\t\t\t},\n\t\t\t\t\tdate: new Date(data.date),\n\t\t\t\t\tmethod: data.method,\n\t\t\t\t\tdoc_collection: data.collection,\n\t\t\t\t\tduration: data.duration,\n\t\t\t\t\tquery: Buffer.byteLength(data.query, 'utf8') < 15800000\n\t\t\t\t\t\t? data.query\n\t\t\t\t\t\t: 'Too Big'\n\t\t\t\t});\n\t\t\t}\n\t\t}\n\t\telse {\n\t\t\tif (this._instanceHostname) {\n\t\t\t\tthis.pushMonitorData({\n\t\t\t\t\ttype: 'monitor-mongo',\n\t\t\t\t\tdata: {\n\t\t\t\t\t\tmetadata: {\n\t\t\t\t\t\t\tinstance: this._instanceHostname,\n\t\t\t\t\t\t\tclient: ResolveIOServer.getServerConfig()['CLIENT_NAME']\n\t\t\t\t\t\t},\n\t\t\t\t\t\tdate: new Date(data.date),\n\t\t\t\t\t\tmethod: data.method,\n\t\t\t\t\t\tdoc_collection: data.collection,\n\t\t\t\t\t\tduration: data.duration,\n\t\t\t\t\t\tquery: Buffer.byteLength(data.query, 'utf8') < 15800000\n\t\t\t\t\t\t\t? data.query\n\t\t\t\t\t\t\t: 'Too Big'\n\t\t\t\t\t}\n\t\t\t\t});\n\t\t\t}\n\t\t}\n\t}\n\n\tasync addFunctionTracker(data: MonitorFunction) {\n\t\tif (typeof(data.data) !== 'string') {\n\t\t\tdata.data = JSON.stringify(data.data);\n\t\t}\n\n\t\tif (\n\t\t\tResolveIOServer.getServerConfig()['ROOT_URL'] === 'https://resolveio.com'\n\t\t\t|| ResolveIOServer.getServerConfig()['ROOT_URL'] === 'http://localhost:4200'\n\t\t) {\n\t\t\tawait MonitorFunctions.create({\n\t\t\t\t_id: objectIdHexString(),\n\t\t\t\tmetadata: {\n\t\t\t\t\tinstance: this._instanceHostname,\n\t\t\t\t\tclient: ResolveIOServer.getServerConfig()['CLIENT_NAME']\n\t\t\t\t},\n\t\t\t\tdate: new Date(data.startTime),\n\t\t\t\ttype: data.functionType,\n\t\t\t\tname: data.functionName,\n\t\t\t\tuser: data.user,\n\t\t\t\tduration: data.duration,\n\t\t\t\tdata: Buffer.byteLength(data.data, 'utf8') < 15800000\n\t\t\t\t\t? data.data\n\t\t\t\t\t: 'Too Big'\n\t\t\t});\n\t\t}\n\t\telse {\n\t\t\tif (this._instanceHostname) {\n\t\t\t\tthis.pushMonitorData({\n\t\t\t\t\ttype: 'monitor-function',\n\t\t\t\t\tdata: {\n\t\t\t\t\t\tmetadata: {\n\t\t\t\t\t\t\tinstance: this._instanceHostname,\n\t\t\t\t\t\t\tclient: ResolveIOServer.getServerConfig()['CLIENT_NAME']\n\t\t\t\t\t\t},\n\t\t\t\t\t\tdate: new Date(data.startTime),\n\t\t\t\t\t\ttype: data.functionType,\n\t\t\t\t\t\tname: data.functionName,\n\t\t\t\t\t\tuser: data.user,\n\t\t\t\t\t\tduration: data.duration,\n\t\t\t\t\t\tdata: Buffer.byteLength(data.data, 'utf8') < 15800000\n\t\t\t\t\t\t\t? data.data\n\t\t\t\t\t\t\t: 'Too Big'\n\t\t\t\t\t}\n\t\t\t\t});\n\t\t\t}\n\t\t}\n\t}\n\n\tprivate pushMonitorData(data: LocalLogModel) {\n\t\tthis._monitorData.push(data);\n\n\t\t// If data is huge, flush immediately to file\n\t\tif (this.estimateBatchSize() >= MAX_BATCH_SIZE) {\n\t\t\tthis.flushMonitorData();\n\t\t}\n\t}\n\n\tprivate estimateBatchSize(): number {\n\t\treturn this._monitorData.reduce((total, item) => {\n\t\t\treturn total + Buffer.byteLength(JSON.stringify(item));\n\t\t}, 0);\n\t}\n\n\tsetupEventLoop() {\n\t\t// Flush every 2.5 seconds\n\t\tsetInterval(() => {\n\t\t\tif (this._monitorData.length > 0) {\n\t\t\t\tthis.flushMonitorData();\n\t\t\t}\n\t\t}, 2500);\n\t}\n\n\tprivate flushMonitorData() {\n\t\tconst batch = [];\n\t\tlet batchSize = 0;\n\n\t\twhile (this._monitorData.length > 0 && batchSize < MAX_BATCH_SIZE) {\n\t\t\tconst item = this._monitorData.shift();\n\t\t\tconst itemSize = Buffer.byteLength(JSON.stringify(item));\n\n\t\t\tif ((batchSize + itemSize) > MAX_BATCH_SIZE && batch.length > 0) {\n\t\t\t\t// put the item back for next round\n\t\t\t\tthis._monitorData.unshift(item);\n\t\t\t\tbreak;\n\t\t\t}\n\n\t\t\tbatch.push(item);\n\t\t\tbatchSize += itemSize;\n\t\t}\n\n\t\ttry {\n\t\t\t// Now, instead of calling an external HTTP endpoint, we write the batch to disk:\n\t\t\tResolveIOServer.getLocalLogManager().writeLogs(batch);\n\t\t}\n\t\tcatch (error) {\n\t\t\tconsole.error(new Date(), 'MonitorManager', 'Failed to flush monitor data to file:', error);\n\t\t}\n\t}\n}\n\nexport interface MongoMonitorModel {\n\tdate: Date;\n\tmethod: string;\n\tcollection: string;\n\tduration: number;\n\tquery: string;\n}\n\nexport class MonitorMongo {\n\tprivate _startTime = 0;\n\tprivate _method = '';\n\tprivate _collection = '';\n\tprivate _query = '';\n\n\tconstructor() {}\n\n\tstatic create(method: string, collection: string, query: string) {\n\t\tconst monitorMongo = new MonitorMongo();\n\t\tmonitorMongo.initialize(method, collection, query);\n\t\treturn monitorMongo;\n\t}\n\n\tprivate initialize(method: string, collection: string, query: string) {\n\t\tthis._startTime = Date.now();\n\t\tthis._method = method;\n\t\tthis._collection = collection;\n\t\tthis._query = query;\n\t}\n\n\tpublic async finish() {\n\t\tlet endTime = Date.now();\n\n\t\tlet mongoMonitor: MongoMonitorModel = {\n\t\t\tdate: new Date(endTime),\n\t\t\tmethod: this._method,\n\t\t\tcollection: this._collection,\n\t\t\tquery: this._query,\n\t\t\tduration: endTime - this._startTime\n\t\t};\n\n\t\tawait ResolveIOServer.getMainServer().getMonitorManager().addMongoTracker(mongoMonitor);\n\t}\n}\n\nexport class MonitorManagerFunction {\n\tprivate _functionCnt = 0;\n\tprivate _functions: MonitorFunction[] = [];\n\tprivate _functionLastCompletedWS: { [id_socket: string]: MonitorFunction; } = {};\n\tprivate _functionLastCompleted: MonitorFunction;\n\n\tconstructor() {}\n\n\tstatic create() {\n\t\tconst monitorManagerFunction = new MonitorManagerFunction();\n\t\tmonitorManagerFunction.initialize();\n\t\treturn monitorManagerFunction;\n\t}\n\n\tprivate initialize() {\n\n\t}\n\n\tstartMonitorFunction(\n\t\tfunctionType: MonitorFunctionType,\n\t\tfunctionName: string,\n\t\tuser: string,\n\t\tid_socket: string,\n\t\tdata: any\n\t) {\n\t\tlet newMonitorFunction = MonitorFunction.create(\n\t\t\tthis._functionCnt++,\n\t\t\tfunctionType,\n\t\t\tfunctionName,\n\t\t\tuser,\n\t\t\tid_socket,\n\t\t\tdata\n\t\t);\n\n\t\tthis._functions.push(newMonitorFunction);\n\t\treturn newMonitorFunction.id;\n\t}\n\n\tasync finishMonitorFunction(id: number) {\n\t\tlet monitor = this._functions.find(a => a.id === id);\n\n\t\tif (monitor) {\n\t\t\tawait monitor.finish();\n\t\t\tthis._functionLastCompleted = monitor;\n\t\t\tthis._functionLastCompletedWS[monitor.id_socket] = monitor;\n\t\t\tthis._functions.splice(\n\t\t\t\tthis._functions.map(a => a.id).indexOf(id),\n\t\t\t\t1\n\t\t\t);\n\t\t}\n\t}\n\n\tgetActiveMonitorFunctions() {\n\t\treturn this._functions;\n\t}\n\n\tgetLastCompletedMonitorFunction() {\n\t\treturn this._functionLastCompleted;\n\t}\n\n\tgetLastCompletedMonitorFunctionWS() {\n\t\treturn this._functionLastCompletedWS;\n\t}\n}\n\nexport type MonitorFunctionType =\n\t'Cron Method'\n\t| 'Method'\n\t| 'User Specific Publication'\n\t| 'Publication';\n\nexport class MonitorFunction {\n\tprivate _timer: NodeJS.Timeout = null;\n\tpublic duration = 0;\n\tpublic functionType: MonitorFunctionType;\n\tpublic functionName: string;\n\tpublic user: string;\n\tpublic data: any;\n\tpublic startTime: Date = null;\n\tpublic endTime: Date = null;\n\tpublic lastTime: Date = null;\n\tpublic id_socket = '';\n\tpublic id = 0;\n\n\tconstructor() {}\n\n\tstatic create(\n\t\tid: number,\n\t\tfunctionType: MonitorFunctionType,\n\t\tfunctionName: string,\n\t\tuser: string,\n\t\tid_socket: string,\n\t\tdata: any\n\t) {\n\t\tconst monitorFunction = new MonitorFunction();\n\t\tmonitorFunction.initialize(id, functionType, functionName, user, id_socket, data);\n\t\treturn monitorFunction;\n\t}\n\n\tprivate initialize(\n\t\tid: number,\n\t\tfunctionType: MonitorFunctionType,\n\t\tfunctionName: string,\n\t\tuser: string,\n\t\tid_socket: string,\n\t\tdata: any\n\t) {\n\t\tthis.id = id;\n\t\tthis.startTime = new Date();\n\t\tthis.functionType = functionType;\n\t\tthis.functionName = functionName;\n\t\tthis.user = user;\n\t\tthis.id_socket = id_socket;\n\t\tthis.data = data;\n\t\tthis.lastTime = new Date();\n\n\t\tthis._timer = setInterval(() => {\n\t\t\tlet diff = Date.now() - this.lastTime.getTime();\n\n\t\t\tif (diff > this.duration) {\n\t\t\t\tthis.duration = diff;\n\t\t\t}\n\n\t\t\tthis.lastTime = new Date();\n\t\t}, 1000);\n\t}\n\n\tpublic async finish() {\n\t\tif (this._timer) {\n\t\t\tclearInterval(this._timer);\n\t\t\tthis._timer = null;\n\t\t}\n\n\t\tthis.endTime = new Date();\n\n\t\tif (this.duration >= 5000) {\n\t\t\t// Optional slow function logging or email\n\t\t}\n\n\t\tawait ResolveIOServer.getMainServer().getMonitorManager().addFunctionTracker(this);\n\t}\n}"]}
@@ -1,6 +1,6 @@
1
- import { ActiveSubscriptionModel, SubscriptionModel } from '../models/subscription.model';
2
1
  import * as WebSocket from 'ws';
3
2
  import { LoggedInUserModel } from '../models/logged-in-users.model';
3
+ import { ActiveSubscriptionModel, SubscriptionModel } from '../models/subscription.model';
4
4
  import { MonitorManagerFunction } from './monitor.manager';
5
5
  export declare class SubscriptionManager {
6
6
  private _websocketManager;
@@ -36,7 +36,9 @@ export declare class SubscriptionManager {
36
36
  private _invalidationPendingTimestamps;
37
37
  private readonly DEBOUNCE_DELAY;
38
38
  private readonly MAX_WAIT_TIME;
39
- constructor(wss: WebSocket.Server, serverConfig: any, monitorManagerFunction: MonitorManagerFunction);
39
+ constructor();
40
+ static create(wss: WebSocket.Server, serverConfig: any, monitorManagerFunction: MonitorManagerFunction): SubscriptionManager;
41
+ private initialize;
40
42
  private setCacheLimit;
41
43
  invalidatePubsCache(collection: string, type: string): Promise<void>;
42
44
  private _executeInvalidation;
@@ -46,7 +48,7 @@ export declare class SubscriptionManager {
46
48
  publications(method: SubscriptionModel): void;
47
49
  loggedInLatency(ws: WebSocket): void;
48
50
  private flushThrottledLatencyUpdates;
49
- subscribe(messageRoute: string, messageDate: Date, ws: WebSocket, messageId: number, publication: string, subscriptionData: any[]): void;
51
+ subscribe(messageRoute: string, messageDate: Date, ws: WebSocket, messageId: number, publication: string, subscriptionData: any[]): Promise<void>;
50
52
  createLoggedInUser(id_ws: string): Promise<LoggedInUserModel>;
51
53
  unsubscribe(messageRoute: string, messageDate: Date, ws: WebSocket, messageId: number, publication: string, subscriptionData: any[]): void;
52
54
  unsubscribeAll(ws: WebSocket): Promise<void>;
@@ -1,2 +1,2 @@
1
- "use strict";var __awaiter=this&&this.__awaiter||function(e,i,o,s){return new(o=o||Promise)(function(n,t){function fulfilled(e){try{step(s.next(e))}catch(e){t(e)}}function rejected(e){try{step(s.throw(e))}catch(e){t(e)}}function step(e){var t;e.done?n(e.value):((t=e.value)instanceof o?t:new o(function(e){e(t)})).then(fulfilled,rejected)}step((s=s.apply(e,i||[])).next())})},__generator=this&&this.__generator||function(i,o){var s,r,a,l={label:0,sent:function(){if(1&a[0])throw a[1];return a[1]},trys:[],ops:[]},c={next:verb(0),throw:verb(1),return:verb(2)};return"function"==typeof Symbol&&(c[Symbol.iterator]=function(){return this}),c;function verb(n){return function(e){var t=[n,e];if(s)throw new TypeError("Generator is already executing.");for(;l=c&&t[c=0]?0:l;)try{if(s=1,r&&(a=2&t[0]?r.return:t[0]?r.throw||((a=r.return)&&a.call(r),0):r.next)&&!(a=a.call(r,t[1])).done)return a;switch(r=0,(t=a?[2&t[0],a.value]:t)[0]){case 0:case 1:a=t;break;case 4:return l.label++,{value:t[1],done:!1};case 5:l.label++,r=t[1],t=[0];continue;case 7:t=l.ops.pop(),l.trys.pop();continue;default:if(!(a=0<(a=l.trys).length&&a[a.length-1])&&(6===t[0]||2===t[0])){l=0;continue}if(3===t[0]&&(!a||t[1]>a[0]&&t[1]<a[3]))l.label=t[1];else if(6===t[0]&&l.label<a[1])l.label=a[1],a=t;else{if(!(a&&l.label<a[2])){a[2]&&l.ops.pop(),l.trys.pop();continue}l.label=a[2],l.ops.push(t)}}t=o.call(i,l)}catch(e){t=[6,e],r=0}finally{s=a=0}if(5&t[0])throw t[1];return{value:t[0]?t[1]:void 0,done:!0}}}},__values=this&&this.__values||function(e){var t="function"==typeof Symbol&&Symbol.iterator,n=t&&e[t],i=0;if(n)return n.call(e);if(e&&"number"==typeof e.length)return{next:function(){return{value:(e=e&&i>=e.length?void 0:e)&&e[i++],done:!e}}};throw new TypeError(t?"Object is not iterable.":"Symbol.iterator is not defined.")},__read=this&&this.__read||function(e,t){var n="function"==typeof Symbol&&e[Symbol.iterator];if(!n)return e;var i,o,s=n.call(e),r=[];try{for(;(void 0===t||0<t--)&&!(i=s.next()).done;)r.push(i.value)}catch(e){o={error:e}}finally{try{i&&!i.done&&(n=s.return)&&n.call(s)}finally{if(o)throw o.error}}return r},__spreadArray=this&&this.__spreadArray||function(e,t,n){if(n||2===arguments.length)for(var i,o=0,s=t.length;o<s;o++)!i&&o in t||((i=i||Array.prototype.slice.call(t,0,o))[o]=t[o]);return e.concat(i||Array.prototype.slice.call(t))},logs_1=(Object.defineProperty(exports,"__esModule",{value:!0}),exports.SubscriptionManager=void 0,require("../publications/logs")),app_status_1=require("../publications/app-status"),files_1=require("../publications/files"),super_admin_1=require("../publications/super-admin"),logged_in_users_collection_1=require("../collections/logged-in-users.collection"),cron_jobs_1=require("../publications/cron-jobs"),flags_1=require("../publications/flags"),method_responses_1=require("../publications/method-responses"),resolveio_server_app_1=require("../resolveio-server-app"),notifications_1=require("../publications/notifications"),report_builder_reports_1=require("../publications/report-builder-reports"),report_builder_libraries_1=require("../publications/report-builder-libraries"),user_groups_1=require("../publications/user-groups"),user_guides_1=require("../publications/user-guides"),report_builder_dashboard_builders_1=require("../publications/report-builder-dashboard-builders"),common_1=require("../util/common"),NodeCache=require("node-cache"),flag_collection_1=require("../collections/flag.collection"),os_1=require("os"),flags_update_1=require("../publications/flags-update"),numCPUs=(0,os_1.cpus)().length,v8=require("v8"),SubscriptionManager=function(){function SubscriptionManager(e,t,n){var i=this;this._publications={},this._subscriptions=[],this._loggedInUsers=[],this._mongoQueue=[],this._mongoQueueId=0,this._cacheId=1,this._heapSize=v8.getHeapStatistics()/numCPUs,this._enableDebug=!1,this._debugOplogCollections=[],this._debugOplogHits=0,this._debugSubCollections=[],this._debugSubHits=0,this._debugUnSubHits=0,this._debugUnSubAllHits=0,this._debugMongoQueueHits=0,this._debugMongoQueueCollections=[],this._debugSendQueueHits=0,this._debugRemoveCacheHits=0,this._oplogRetryCount=0,this.latencyBuffer=new Map,this.LATENCY_UPDATE_INTERVAL=6e4,this.LATENCY_UPDATE_THRESHOLD_MS=3e4,this._invalidationDebounceTimers=new Map,this._invalidationPendingTimestamps=new Map,this.DEBOUNCE_DELAY=100,this.MAX_WAIT_TIME=500,this._websocketManager=resolveio_server_app_1.ResolveIOServer.getMainServer().getWebSocketManager(),this._monitorManagerFunction=n,this._nodeCache=new NodeCache({stdTTL:0,checkperiod:0}),setInterval(function(){return i.flushThrottledLatencyUpdates()},this.LATENCY_UPDATE_INTERVAL),this.serverConfig=t,this._wss=e,(0,super_admin_1.loadSuperAdminPublications)(this),(0,app_status_1.loadAppStatusPublications)(this),(0,logs_1.loadLogPublications)(this),(0,files_1.loadFilePublications)(this),(0,cron_jobs_1.loadCronJobPublications)(this),(0,flags_update_1.loadFlagsUpdatePublications)(this),(0,flags_1.loadFlagsPublications)(this),(0,method_responses_1.loadMethodResponsePublications)(this),(0,notifications_1.loadNotificationPublications)(this),(0,report_builder_reports_1.loadReportBuilderReportPublications)(this),(0,report_builder_libraries_1.loadReportBuilderLibraryPublications)(this),(0,user_groups_1.loadUserGroupPublications)(this),(0,user_guides_1.loadUserGuidePublications)(this),(0,report_builder_dashboard_builders_1.loadReportBuilderDashboardBuilderPublications)(this),this.tailOpLog(),setInterval(function(){i._oplogRetryCount=0},15e3),setInterval(function(){i.getEnableDebug()&&(console.log(new Date,"Sub Manager","Subs",i._subscriptions.length),console.log(new Date,"Sub Manager","Logged In Users",i._loggedInUsers.length),console.log(new Date,"Sub Manager","Mongo Queue",i._mongoQueue.length),console.log(new Date,"Sub Manager","Mongo Queue Hits",i._debugMongoQueueHits),console.log(new Date,"Sub Manager","Mongo Queue Collections",JSON.stringify(i._debugMongoQueueCollections.sort(function(e,t){return e.collection.localeCompare(t.collection)||e.publication.localeCompare(t.publication)}),null,2)),console.log(new Date,"Sub Manager","Oplog Hits",i._debugOplogHits),console.log(new Date,"Sub Manager","Oplog Collections",JSON.stringify(i._debugOplogCollections.sort(function(e,t){return e.collection.localeCompare(t.collection)||e.type.localeCompare(t.type)}),null,2)),console.log(new Date,"Sub Manager","Send Queue Hits",i._debugSendQueueHits),console.log(new Date,"Sub Manager","Sub Hits",i._debugSubHits),console.log(new Date,"Sub Manager","Sub Collections",JSON.stringify(i._debugSubCollections.sort(function(e,t){return e.publication.localeCompare(t.publication)}),null,2)),console.log(new Date,"Sub Manager","Unsub Hits",i._debugUnSubHits),console.log(new Date,"Sub Manager","Unsub All Hits",i._debugUnSubAllHits),console.log(new Date,"Sub Manager","Cache Cleanup Hits",i._debugRemoveCacheHits)),i._debugOplogHits=0,i._debugOplogCollections=[],i._debugSubCollections=[],i._debugMongoQueueHits=0,i._debugMongoQueueCollections=[],i._debugSendQueueHits=0,i._debugSubHits=0,i._debugUnSubHits=0,i._debugUnSubAllHits=0,i._debugRemoveCacheHits=0},6e4),setInterval(function(){return __awaiter(i,void 0,void 0,function(){var t,i,n,o,s,r,a,l,c;return __generator(this,function(e){switch(e.label){case 0:return t=this,[4,logged_in_users_collection_1.LoggedInUsers.find()];case 1:for(t._loggedInUsers=e.sent(),i=(0,common_1.deepCopy)(this._loggedInUsers),n=function(e){var n=i[e];(!n.date||12e4<Date.now()-n.date.getTime())&&(o._websocketManager.getWebSocket(n.id_ws)?(o.getEnableDebug()&&console.log(new Date,"Sub Manager","Unsub WS",o._websocketManager.getWebSocket(n.id_ws).user,o._websocketManager.getWebSocket(n.id_ws).id_socket,2),o.unsubscribeAll(o._websocketManager.getWebSocket(n.id_ws))):(o._subscriptions.forEach(function(e){for(var t=e.clients.length-1;0<=t;t--)e.clients[t].id_socket===n.id_ws&&e.clients.splice(t,1)}),logged_in_users_collection_1.LoggedInUsers.deleteOne({_id:n._id}),0<=o._loggedInUsers.findIndex(function(e){return e._id===n._id})&&o._loggedInUsers.splice(o._loggedInUsers.findIndex(function(e){return e._id===n._id}),1)))},s=(o=this)._loggedInUsers.length-1;0<=s;s--)n(s);for(s=0;s<this._subscriptions.length;s++)for(r=this._subscriptions[s],a=function(e){var t=r.clients[e];l._loggedInUsers.some(function(e){return e.id_ws===t.id_socket})||r.clients.splice(e,1)},l=this,c=r.clients.length-1;0<=c;c--)a(c);return[2]}})})},3e4),flag_collection_1.Flags.findOne({type:"Enable Debug"}).then(function(e){e&&e.value?i._enableDebug=!0:i._enableDebug=!1}),this.setCacheLimit()}return SubscriptionManager.prototype.setCacheLimit=function(){"true"===process.env.IS_WORKERS_ENABLED?this._heapLimit=.4*this._heapSize:this._heapLimit=.3*this._heapSize},SubscriptionManager.prototype.invalidatePubsCache=function(s,r){return __awaiter(this,void 0,void 0,function(){var t,n,i,o=this;return __generator(this,function(e){switch(e.label){case 0:return(t=s,n=Date.now(),i=this._invalidationPendingTimestamps.get(t)||n,this._invalidationPendingTimestamps.set(t,i),this._invalidationDebounceTimers.has(t)&&clearTimeout(this._invalidationDebounceTimers.get(t)),n-i>=this.MAX_WAIT_TIME)?(this._invalidationPendingTimestamps.delete(t),[4,this._executeInvalidation(s,r)]):[3,2];case 1:return e.sent(),[3,3];case 2:this._invalidationDebounceTimers.set(t,setTimeout(function(){o._invalidationPendingTimestamps.delete(t),o._executeInvalidation(s,r)},this.DEBOUNCE_DELAY)),e.label=3;case 3:return[2]}})})},SubscriptionManager.prototype._executeInvalidation=function(u,_){return __awaiter(this,void 0,void 0,function(){var t,n,o,i,s,r,a,l,c=this;return __generator(this,function(e){this._invalidationDebounceTimers.has(u)&&(clearTimeout(this._invalidationDebounceTimers.get(u)),this._invalidationDebounceTimers.delete(u)),resolveio_server_app_1.ResolveIOServer.getMongoManager().invalidateQueryCache(u),t=this._subscriptions.filter(function(e){return e.collections.includes(u)}),n=function(i){if(o._enableDebug&&console.log(new Date,"Invalidate Sub",i.publication,i.running,i.runAgain),i.running)return i.runAgain=!0,"continue";o._publications[i.publication].user_specific?i.clients.map(function(n){return __awaiter(c,void 0,void 0,function(){var t;return __generator(this,function(e){if((null==(t=this._websocketManager.getWebSocket(n.id_socket))?void 0:t.readyState)===t.OPEN)try{this.sendDataToOneWithRetry(t,n.messageId,i,u,_)}catch(e){}return[2]})})}):o.sendDataToAllWithRetry(i,u,_)},o=this;try{for(i=__values(t),s=i.next();!s.done;s=i.next())r=s.value,n(r)}catch(e){a={error:e}}finally{try{s&&!s.done&&(l=i.return)&&l.call(i)}finally{if(a)throw a.error}}return[2]})})},SubscriptionManager.prototype.delay=function(t){return __awaiter(this,void 0,void 0,function(){return __generator(this,function(e){return[2,new Promise(function(e){return setTimeout(e,t)})]})})},SubscriptionManager.prototype.sendDataToAllWithRetry=function(t,n,i){return __awaiter(this,void 0,void 0,function(){return __generator(this,function(e){switch(e.label){case 0:t.running=!0,e.label=1;case 1:return this._enableDebug&&console.log(new Date,"Running sendDataToAll Sub",t.publication),t.runAgain=!1,[4,this.sendDataToAll(t,n,i)];case 2:return(e.sent(),this._enableDebug&&console.log(new Date,"Done sendDataToAll Sub",t.publication,t.runAgain),t.runAgain)?[4,this.delay(500)]:[3,4];case 3:e.sent(),e.label=4;case 4:if(t.runAgain)return[3,1];e.label=5;case 5:return t.running=!1,[2]}})})},SubscriptionManager.prototype.sendDataToOneWithRetry=function(t,n,i,o,s){return __awaiter(this,void 0,void 0,function(){return __generator(this,function(e){switch(e.label){case 0:i.running=!0,e.label=1;case 1:return this._enableDebug&&console.log(new Date,"Running sendDataToOne Sub",i.publication),i.runAgain=!1,[4,this.sendDataToOne(t,n,i,o,s)];case 2:return(e.sent(),this._enableDebug&&console.log(new Date,"Done sendDataToOne Sub",i.publication,i.runAgain),i.runAgain)?[4,this.delay(500)]:[3,4];case 3:e.sent(),e.label=4;case 4:if(i.runAgain)return[3,1];e.label=5;case 5:return i.running=!1,[2]}})})},SubscriptionManager.prototype.publications=function(e){this._publications=Object.assign(this._publications,e)},SubscriptionManager.prototype.loggedInLatency=function(t){var e,n,i,o=this._loggedInUsers.find(function(e){return e.id_ws===t.id_socket});o&&(e=new Date,n=this.latencyBuffer.get(t.id_socket),i=t.latency,!n||e.getTime()-n.lastUpdate.getTime()>=this.LATENCY_UPDATE_THRESHOLD_MS||100<Math.abs(i-n.latency))&&(o.date=e,this.latencyBuffer.set(t.id_socket,{latency:i,lastUpdate:e}))},SubscriptionManager.prototype.flushThrottledLatencyUpdates=function(){return __awaiter(this,void 0,void 0,function(){var t,n;return __generator(this,function(e){switch(e.label){case 0:if(0===this.latencyBuffer.size)return[2];t=Array.from(this.latencyBuffer.entries()).map(function(e){var e=__read(e,2),t=e[0],e=e[1];return{updateOne:{filter:{id_ws:t},update:{$set:{latency:e.latency,date:e.lastUpdate}}}}}),e.label=1;case 1:return e.trys.push([1,3,,4]),[4,logged_in_users_collection_1.LoggedInUsers.bulkWrite(t)];case 2:return e.sent(),this.latencyBuffer.clear(),this.getEnableDebug()&&console.log(new Date,"Sub Manager","Throttled latency batch update successful",t.length),[3,4];case 3:return n=e.sent(),console.error(new Date,"Sub Manager","Throttled latency batch update failed",n),[3,4];case 4:return[2]}})})},SubscriptionManager.prototype.subscribe=function(t,e,n,i,o,s){var r=this,a=(this._debugSubHits+=1,this._debugSubCollections.some(function(e){return e.publication===o})?this._debugSubCollections.find(function(e){return e.publication===o}).hits+=1:this._debugSubCollections.push({publication:o,hits:1}),this._publications[o]);if(a){if(1<s.length||s[0]){if(!a.check)return void console.error(new Date,"No Check Function For Pub "+o);if(!a.check._schema)return void console.error(new Date,"No Check Schema For Pub "+o);for(var l={},c=Object.keys(a.check._schema).filter(function(e){return!e.includes(".")}),u=0;u<s.length;u++)l[c[u]]=s[u];try{a.check.validate(l)}catch(e){if(e)return void console.error(new Date,"Error in Pub Check ("+o+")",e)}}"Bypass"!==t&&(a=t.split("/"),_="",g=a[0],""===a[0]&&(_="/",g=a[1]),_+=g,1<a.length&&(_+="/"),(g=this._subscriptions.filter(function(e){return e.clients.some(function(e){return e.id_socket===n.id_socket&&"Bypass"!==e.messageRoute&&"/"!==e.messageRoute&&e.messageRoute!==t&&!e.messageRoute.startsWith(_)})})).length)&&g.forEach(function(t){t.clients.filter(function(e){return e.id_socket===n.id_socket}).forEach(function(e){r.unsubscribe(e.messageRoute,new Date,n,e.messageId,t.publication,t.subscriptionData)})});var _,g,a=this._subscriptions.find(function(e){return e.publication===o&&JSON.stringify(e.subscriptionData)===JSON.stringify(s)});a?a.clients.some(function(e){return e.id_socket===n.id_socket&&e.messageId===i})||a.clients.push({id_user:n.id_user,messageId:i,id_socket:n.id_socket,messageRoute:t}):this._subscriptions.push({publication:o,subscriptionData:s,collections:this.getPublicationCollections(o),clients:[{id_user:n.id_user,messageId:i,id_socket:n.id_socket,messageRoute:t}],cacheId:0,running:!1,runAgain:!1}),a=a||this._subscriptions.find(function(e){return e.publication===o&&JSON.stringify(e.subscriptionData)===JSON.stringify(s)}),this._enableDebug&&console.log(new Date,"New Sub",a.publication,a.running,a.runAgain,a.clients.length),this.processSubscription(a,n,i)}else console.error(new Date,"No Publication: "+o)},SubscriptionManager.prototype.createLoggedInUser=function(i){return __awaiter(this,void 0,void 0,function(){var t=this;return __generator(this,function(e){return[2,new Promise(function(n,e){return __awaiter(t,void 0,void 0,function(){var t;return __generator(this,function(e){return(t=this._websocketManager.getWebSocket(i))?(t={_id:(0,common_1.objectIdHexString)(),__v:0,date:new Date,id_user:t.id_user,user:t.user,id_ws:t.id_socket},this._loggedInUsers.push(t),logged_in_users_collection_1.LoggedInUsers.insertOne(t),n(t)):n(null),[2]})})})]})})},SubscriptionManager.prototype.unsubscribe=function(e,t,n,i,o,s){if(this._debugUnSubHits+=1,this._publications[o]){var r=this._subscriptions.find(function(e){return e.publication===o&&JSON.stringify(e.subscriptionData)===JSON.stringify(s)});if(r){for(var a=r.clients.length-1;0<=a;a--)r.clients[a].id_user===n.id_user&&r.clients[a].messageId===i&&r.clients[a].id_socket===n.id_socket&&r.clients.splice(a,1);this._enableDebug&&console.log(new Date,"Unsub",r.publication,r.running,r.runAgain,r.clients.length)}}else console.log("No Publication: "+o)},SubscriptionManager.prototype.unsubscribeAll=function(s){return __awaiter(this,void 0,void 0,function(){var t,n,i,o;return __generator(this,function(e){if(this._debugUnSubAllHits+=1,s){if(s.isUnsubscribed)return[2];for(s.isUnsubscribed=!0,0<=this._loggedInUsers.map(function(e){return e.id_ws}).indexOf(s.id_socket)&&this._loggedInUsers.splice(this._loggedInUsers.map(function(e){return e.id_ws}).indexOf(s.id_socket),1),logged_in_users_collection_1.LoggedInUsers.deleteOne({id_ws:s.id_socket}),t=this._subscriptions.filter(function(e){return e.clients.some(function(e){return e.id_user===s.id_user&&e.id_socket===s.id_socket})}),n=t.length-1;0<=n;n--)for(i=t[n],o=i.clients.length-1;0<=o;o--)i.clients[o].id_socket===s.id_socket&&i.clients.splice(o,1);this._websocketManager.removeWebSocket(s)}return[2]})})},SubscriptionManager.prototype.getActiveSubscriptions=function(){return this._subscriptions},SubscriptionManager.prototype.getPublicationCollections=function(e){return this._publications[e].collections},SubscriptionManager.prototype.tailOpLog=function(o){return __awaiter(this,void 0,void 0,function(){var t,n,i=this;return __generator(this,function(e){switch(e.label){case 0:return this._oplog$&&!this._oplog$.closed&&(this._oplog$.removeAllListeners(),this._oplog$.close(),this._oplog$=null),[4,new Promise(function(e){return setTimeout(e,1e3)})];case 1:if(e.sent(),!this._oplog$||this._oplog$.closed){if(this._oplogRetryCount+=1,5<this._oplogRetryCount&&(console.error("****************** TAIL OPLOG ERROR, RETRYING A BUNCH OF TIMES, KILLING PROCESS **************************"),process.exit(1)),t=[{$match:{$and:[{"ns.coll":{$nin:["log-method-latencies","log-subscriptions","logs","counters","cron-job-histories","email-histories","qb-soap-request-histories","qb-soap-request-responses","qb-soap-requests","qb-soap-retries"]}},{"ns.coll":{$not:/.*\.versions$/}},{"ns.coll":{$not:/^monitor-/}}]}}],o){n=o;try{this._oplog$=resolveio_server_app_1.ResolveIOServer.getMainDB().watch(t,{resumeAfter:o})}catch(e){return this._oplog$&&(this._oplog$.removeAllListeners(),this._oplog$.close(),this._oplog$=null),this.tailOpLog(o),[2]}}else this._oplog$=resolveio_server_app_1.ResolveIOServer.getMainDB().watch(t);console.log(new Date,"oplog started"),this._oplog$.on("change",function(t){var e;t.ns&&(i._enableDebug&&console.log(new Date,"Oplog Hit",t.ns),e=t.ns.coll,i._debugOplogCollections.some(function(e){return e.collection===t.ns.coll&&e.type===t.operationType})?i._debugOplogCollections.find(function(e){return e.collection===t.ns.coll&&e.type===t.operationType}).hits+=1:i._debugOplogCollections.push({collection:t.ns.coll,type:t.operationType,hits:1}),e&&(i._debugOplogHits+=1,"insert"===t.operationType?("support-tickets"===e&&"https://resolveio.com"===i.serverConfig.ROOT_URL&&(resolveio_server_app_1.ResolveIOServer.getMainServer().getMethodManager().callMethod.call(resolveio_server_app_1.ResolveIOServer.getMainServer().getMethodManager(),"sendSupportTicketEmail",t.documentKey._id),i.invalidatePubsCache(e,"insert")),"method-responses"!==e&&i.invalidatePubsCache(e,"insert")):"update"===t.operationType||"replace"===t.operationType?"method-responses"!==e&&i.invalidatePubsCache(e,"update"):"delete"===t.operationType&&"method-responses"!==e&&i.invalidatePubsCache(e,"delete")),"flags"===e&&flag_collection_1.Flags.findOne({type:"Enable Debug"}).then(function(e){e&&e.value?i._enableDebug=!0:i._enableDebug=!1}),n=t._id,process.env.NODE_APP_INSTANCE&&"0"!==process.env.NODE_APP_INSTANCE||"false"!==process.env.IS_WORKERS_ENABLED&&"true"===process.env.IS_WORKER_INSTANCE&&process.env.WORKER_INDEX)}),this._oplog$.on("error",function(e){console.log(new Date,"oplog error",e),i._oplog$.removeAllListeners(),i._oplog$.close(),i._oplog$=null,i.tailOpLog(n)}),this._oplog$.on("end",function(){console.log(new Date,"oplog end"),i._oplog$.removeAllListeners(),i._oplog$.close(),i._oplog$=null,i.tailOpLog(n)}),this._oplog$.on("close",function(){console.log(new Date,"oplog close"),i._oplog$.removeAllListeners(),i._oplog$=null,i.tailOpLog(n)})}return[2]}})})},SubscriptionManager.prototype.processSubscription=function(i,o,s){return __awaiter(this,void 0,void 0,function(){var t,n;return __generator(this,function(e){if(this._publications[i.publication].user_specific){if(this._enableDebug&&console.log(new Date,"Process Sub Specific, Non - Cache",i.publication,i.running),i.running)return[2];this.sendDataToOneWithRetry(o,s,i,"","newSub")}else if(i.cacheId)try{t=JSON.parse(this._nodeCache.get(i.cacheId),common_1.dateReviver),n={messageId:s,hasError:!1,data:t},this._enableDebug&&console.log(new Date,"Process Sub, Cache",i.publication),this.sendWS(o,n)}catch(e){this._nodeCache.del(i.cacheId),i.cacheId=0,this.sendDataToAllWithRetry(i,"","newSub")}else{if(this._enableDebug&&console.log(new Date,"Process Sub, Non - Cache",i.publication,i.running),i.running)return[2];this.sendDataToAllWithRetry(i,"","newSub")}return[2]})})},SubscriptionManager.prototype.sendDataToOne=function(s,r,a,l,c){return __awaiter(this,void 0,void 0,function(){var t,n,i,o;return __generator(this,function(e){switch(e.label){case 0:t=this._monitorManagerFunction.startMonitorFunction("User Specific Publication",a.publication,"","",a.subscriptionData),e.label=1;case 1:return e.trys.push([1,3,,4]),resolveio_server_app_1.ResolveIOServer.getMainServer().getMethodManager().callMethod.call(resolveio_server_app_1.ResolveIOServer.getMainServer().getMethodManager(),"insertSubscriptionLog",c,a.publication,l,JSON.stringify(a.subscriptionData)),[4,(o=this._publications[a.publication].function).call.apply(o,__spreadArray([Object.assign({},this,SubscriptionManager.prototype),s.id_user],__read(a.subscriptionData),!1))];case 2:return o=e.sent(),this._monitorManagerFunction.finishMonitorFunction(t),i={messageId:r,hasError:!1,data:o},this.sendWS(s,i),[3,4];case 3:return n=e.sent(),this._monitorManagerFunction.finishMonitorFunction(t),i={messageId:r,hasError:!0,data:n},this.sendWS(s,i),resolveio_server_app_1.ResolveIOServer.getMainServer().getMethodManager().sendEmail("dev@resolveio.com","SERVER - Error Detected - "+this.serverConfig.CLIENT_NAME,"Error Detected During Subscription "+a.publication+" - (sendDataToOne - WS)\n\nData \n"+JSON.stringify(a.subscriptionData,null,2)+"\n\nErrors\n"+JSON.stringify(n,null,2)),[3,4];case 4:return[2]}})})},SubscriptionManager.prototype.sendDataToAll=function(_,g,d){return __awaiter(this,void 0,void 0,function(){var t,o,n,i,s,r,a,l,c,u=this;return __generator(this,function(e){switch(e.label){case 0:return _.clients.length?[3,1]:(_.cacheId&&(this._nodeCache.del(_.cacheId),_.cacheId=0),0<=(c=this._subscriptions.findIndex(function(e){return e.publication===_.publication&&JSON.stringify(e.subscriptionData)===JSON.stringify(_.subscriptionData)}))&&this._subscriptions.splice(c,1),[2]);case 1:t=this._monitorManagerFunction.startMonitorFunction("Publication",_.publication,"","",_.subscriptionData),e.label=2;case 2:return e.trys.push([2,4,,5]),"superadminAPM"!==_.publication&&"loggedInUsers"!==_.publication&&resolveio_server_app_1.ResolveIOServer.getMainServer().getMethodManager().callMethod.call(resolveio_server_app_1.ResolveIOServer.getMainServer().getMethodManager(),"insertSubscriptionLog",d,_.publication,g,JSON.stringify(_.subscriptionData)),[4,(c=this._publications[_.publication].function).call.apply(c,__spreadArray([Object.assign({},this,SubscriptionManager.prototype)],__read(_.subscriptionData),!1))];case 3:if(o=e.sent(),this._monitorManagerFunction.finishMonitorFunction(t),_.cacheId)try{n=JSON.parse(this._nodeCache.get(_.cacheId),common_1.dateReviver),JSON.stringify(n)!==JSON.stringify(o)&&(Promise.all(_.clients.map(function(i){return __awaiter(u,void 0,void 0,function(){var t,n;return __generator(this,function(e){return(t=this._websocketManager.getWebSocket(i.id_socket))&&t.readyState===t.OPEN&&(n={messageId:i.messageId,hasError:!1,data:o},this.sendWS(t,n)),[2]})})})),this._nodeCache.del(_.cacheId),(0,common_1.getBinarySize)(JSON.stringify(o))<1e6&&!_.collections.includes("logs")&&!_.collections.find(function(e){return e.endsWith(".versions")})&&!_.collections.find(function(e){return e.startsWith("monitor-")})?this._nodeCache.set(_.cacheId,JSON.stringify(o)):_.cacheId=0)}catch(e){Promise.all(_.clients.map(function(i){return __awaiter(u,void 0,void 0,function(){var t,n;return __generator(this,function(e){return(t=this._websocketManager.getWebSocket(i.id_socket))&&t.readyState===t.OPEN&&(n={messageId:i.messageId,hasError:!1,data:o},this.sendWS(t,n)),[2]})})})),this._nodeCache.del(_.cacheId),(0,common_1.getBinarySize)(JSON.stringify(o))<1e6&&!_.collections.includes("logs")&&!_.collections.find(function(e){return e.endsWith(".versions")})&&!_.collections.find(function(e){return e.startsWith("monitor-")})?this._nodeCache.set(_.cacheId,JSON.stringify(o)):_.cacheId=0}else if(Promise.all(_.clients.map(function(i){return __awaiter(u,void 0,void 0,function(){var t,n;return __generator(this,function(e){return(t=this._websocketManager.getWebSocket(i.id_socket))&&t.readyState===t.OPEN&&(n={messageId:i.messageId,hasError:!1,data:o},this.sendWS(t,n)),[2]})})})),(0,common_1.getBinarySize)(JSON.stringify(o))<1e6&&!_.collections.includes("logs")&&!_.collections.find(function(e){return e.endsWith(".versions")})&&!_.collections.find(function(e){return e.startsWith("monitor-")})){if(_.cacheId=this._cacheId++,this._nodeCache.set(_.cacheId,JSON.stringify(o)),(i=this._nodeCache.getStats().vsize)>this._heapLimit){for(s=0,r=this._subscriptions.filter(function(e){return e.cacheId&&!e.clients.length}),a=0;a<r.length&&(this._debugRemoveCacheHits+=1,this._nodeCache.del(r[a].cacheId),r[a].cacheId=0,s+=1,!(this._nodeCache.getStats().vsize<.75*this._heapLimit));a++);this._enableDebug&&console.log("Sub Cache: Too Big - "+_.publication+" - Deleted: "+s+" - "+i)}}else _.cacheId=0;return[3,5];case 4:return l=e.sent(),this._monitorManagerFunction.finishMonitorFunction(t),Promise.all(_.clients.map(function(i){return __awaiter(u,void 0,void 0,function(){var t,n;return __generator(this,function(e){return(t=this._websocketManager.getWebSocket(i.id_socket))&&t.readyState===t.OPEN&&(n={messageId:i.messageId,hasError:!0,data:l},this.sendWS(t,n)),[2]})})})),resolveio_server_app_1.ResolveIOServer.getMainServer().getMethodManager().sendEmail("dev@resolveio.com","SERVER - Error Detected - "+this.serverConfig.CLIENT_NAME,"Error Detected During Subscription "+_.publication+" - (sendPubData)\n\nData \n"+JSON.stringify(_.subscriptionData,null,2)+"\n\nErrors\n"+JSON.stringify(l,null,2)),[3,5];case 5:return[2]}})})},SubscriptionManager.prototype.sendWS=function(e,t){this._websocketManager.send(e,t)},SubscriptionManager.prototype.getEnableDebug=function(){return this._enableDebug},SubscriptionManager}();exports.SubscriptionManager=SubscriptionManager;
1
+ "use strict";var __awaiter=this&&this.__awaiter||function(e,i,s,r){return new(s=s||Promise)(function(n,t){function fulfilled(e){try{step(r.next(e))}catch(e){t(e)}}function rejected(e){try{step(r.throw(e))}catch(e){t(e)}}function step(e){var t;e.done?n(e.value):((t=e.value)instanceof s?t:new s(function(e){e(t)})).then(fulfilled,rejected)}step((r=r.apply(e,i||[])).next())})},__generator=this&&this.__generator||function(i,s){var r,o,a,c={label:0,sent:function(){if(1&a[0])throw a[1];return a[1]},trys:[],ops:[]},u={next:verb(0),throw:verb(1),return:verb(2)};return"function"==typeof Symbol&&(u[Symbol.iterator]=function(){return this}),u;function verb(n){return function(e){var t=[n,e];if(r)throw new TypeError("Generator is already executing.");for(;c=u&&t[u=0]?0:c;)try{if(r=1,o&&(a=2&t[0]?o.return:t[0]?o.throw||((a=o.return)&&a.call(o),0):o.next)&&!(a=a.call(o,t[1])).done)return a;switch(o=0,(t=a?[2&t[0],a.value]:t)[0]){case 0:case 1:a=t;break;case 4:return c.label++,{value:t[1],done:!1};case 5:c.label++,o=t[1],t=[0];continue;case 7:t=c.ops.pop(),c.trys.pop();continue;default:if(!(a=0<(a=c.trys).length&&a[a.length-1])&&(6===t[0]||2===t[0])){c=0;continue}if(3===t[0]&&(!a||t[1]>a[0]&&t[1]<a[3]))c.label=t[1];else if(6===t[0]&&c.label<a[1])c.label=a[1],a=t;else{if(!(a&&c.label<a[2])){a[2]&&c.ops.pop(),c.trys.pop();continue}c.label=a[2],c.ops.push(t)}}t=s.call(i,c)}catch(e){t=[6,e],o=0}finally{r=a=0}if(5&t[0])throw t[1];return{value:t[0]?t[1]:void 0,done:!0}}}},__values=this&&this.__values||function(e){var t="function"==typeof Symbol&&Symbol.iterator,n=t&&e[t],i=0;if(n)return n.call(e);if(e&&"number"==typeof e.length)return{next:function(){return{value:(e=e&&i>=e.length?void 0:e)&&e[i++],done:!e}}};throw new TypeError(t?"Object is not iterable.":"Symbol.iterator is not defined.")},__read=this&&this.__read||function(e,t){var n="function"==typeof Symbol&&e[Symbol.iterator];if(!n)return e;var i,s,r=n.call(e),o=[];try{for(;(void 0===t||0<t--)&&!(i=r.next()).done;)o.push(i.value)}catch(e){s={error:e}}finally{try{i&&!i.done&&(n=r.return)&&n.call(r)}finally{if(s)throw s.error}}return o},__spreadArray=this&&this.__spreadArray||function(e,t,n){if(n||2===arguments.length)for(var i,s=0,r=t.length;s<r;s++)!i&&s in t||((i=i||Array.prototype.slice.call(t,0,s))[s]=t[s]);return e.concat(i||Array.prototype.slice.call(t))},logged_in_users_collection_1=(Object.defineProperty(exports,"__esModule",{value:!0}),exports.SubscriptionManager=void 0,require("../collections/logged-in-users.collection")),app_status_1=require("../publications/app-status"),cron_jobs_1=require("../publications/cron-jobs"),files_1=require("../publications/files"),flags_1=require("../publications/flags"),logs_1=require("../publications/logs"),method_responses_1=require("../publications/method-responses"),notifications_1=require("../publications/notifications"),report_builder_reports_1=require("../publications/report-builder-reports"),super_admin_1=require("../publications/super-admin"),resolveio_server_app_1=require("../resolveio-server-app"),NodeCache=require("node-cache"),os_1=require("os"),flag_collection_1=require("../collections/flag.collection"),flags_update_1=require("../publications/flags-update"),report_builder_dashboard_builders_1=require("../publications/report-builder-dashboard-builders"),report_builder_libraries_1=require("../publications/report-builder-libraries"),user_groups_1=require("../publications/user-groups"),user_guides_1=require("../publications/user-guides"),common_1=require("../util/common"),numCPUs=(0,os_1.cpus)().length,v8=require("v8"),SubscriptionManager=function(){function SubscriptionManager(){this._publications={},this._subscriptions=[],this._loggedInUsers=[],this._mongoQueue=[],this._mongoQueueId=0,this._cacheId=1,this._heapSize=v8.getHeapStatistics()/numCPUs,this._enableDebug=!1,this._debugOplogCollections=[],this._debugOplogHits=0,this._debugSubCollections=[],this._debugSubHits=0,this._debugUnSubHits=0,this._debugUnSubAllHits=0,this._debugMongoQueueHits=0,this._debugMongoQueueCollections=[],this._debugSendQueueHits=0,this._debugRemoveCacheHits=0,this._oplogRetryCount=0,this.latencyBuffer=new Map,this.LATENCY_UPDATE_INTERVAL=6e4,this.LATENCY_UPDATE_THRESHOLD_MS=3e4,this._invalidationDebounceTimers=new Map,this._invalidationPendingTimestamps=new Map,this.DEBOUNCE_DELAY=100,this.MAX_WAIT_TIME=500}return SubscriptionManager.create=function(t,n,i){var e=this,s=new SubscriptionManager;return setImmediate(function(){return __awaiter(e,void 0,void 0,function(){return __generator(this,function(e){switch(e.label){case 0:return[4,s.initialize(t,n,i)];case 1:return e.sent(),[2]}})})}),s},SubscriptionManager.prototype.initialize=function(i,s,r){return __awaiter(this,void 0,void 0,function(){var t,n=this;return __generator(this,function(e){switch(e.label){case 0:return this._websocketManager=resolveio_server_app_1.ResolveIOServer.getMainServer().getWebSocketManager(),this._monitorManagerFunction=r,this._nodeCache=new NodeCache({stdTTL:0,checkperiod:0}),setInterval(function(){return n.flushThrottledLatencyUpdates()},this.LATENCY_UPDATE_INTERVAL),this.serverConfig=s,this._wss=i,(0,super_admin_1.loadSuperAdminPublications)(this),(0,app_status_1.loadAppStatusPublications)(this),(0,logs_1.loadLogPublications)(this),(0,files_1.loadFilePublications)(this),(0,cron_jobs_1.loadCronJobPublications)(this),(0,flags_update_1.loadFlagsUpdatePublications)(this),(0,flags_1.loadFlagsPublications)(this),(0,method_responses_1.loadMethodResponsePublications)(this),(0,notifications_1.loadNotificationPublications)(this),(0,report_builder_reports_1.loadReportBuilderReportPublications)(this),(0,report_builder_libraries_1.loadReportBuilderLibraryPublications)(this),(0,user_groups_1.loadUserGroupPublications)(this),(0,user_guides_1.loadUserGuidePublications)(this),(0,report_builder_dashboard_builders_1.loadReportBuilderDashboardBuilderPublications)(this),[4,this.tailOpLog()];case 1:return e.sent(),setInterval(function(){n._oplogRetryCount=0},15e3),setInterval(function(){n.getEnableDebug()&&(console.log(new Date,"Sub Manager","Subs",n._subscriptions.length),console.log(new Date,"Sub Manager","Logged In Users",n._loggedInUsers.length),console.log(new Date,"Sub Manager","Mongo Queue",n._mongoQueue.length),console.log(new Date,"Sub Manager","Mongo Queue Hits",n._debugMongoQueueHits),console.log(new Date,"Sub Manager","Mongo Queue Collections",JSON.stringify(n._debugMongoQueueCollections.sort(function(e,t){return e.collection.localeCompare(t.collection)||e.publication.localeCompare(t.publication)}),null,2)),console.log(new Date,"Sub Manager","Oplog Hits",n._debugOplogHits),console.log(new Date,"Sub Manager","Oplog Collections",JSON.stringify(n._debugOplogCollections.sort(function(e,t){return e.collection.localeCompare(t.collection)||e.type.localeCompare(t.type)}),null,2)),console.log(new Date,"Sub Manager","Send Queue Hits",n._debugSendQueueHits),console.log(new Date,"Sub Manager","Sub Hits",n._debugSubHits),console.log(new Date,"Sub Manager","Sub Collections",JSON.stringify(n._debugSubCollections.sort(function(e,t){return e.publication.localeCompare(t.publication)}),null,2)),console.log(new Date,"Sub Manager","Unsub Hits",n._debugUnSubHits),console.log(new Date,"Sub Manager","Unsub All Hits",n._debugUnSubAllHits),console.log(new Date,"Sub Manager","Cache Cleanup Hits",n._debugRemoveCacheHits)),n._debugOplogHits=0,n._debugOplogCollections=[],n._debugSubCollections=[],n._debugMongoQueueHits=0,n._debugMongoQueueCollections=[],n._debugSendQueueHits=0,n._debugSubHits=0,n._debugUnSubHits=0,n._debugUnSubAllHits=0,n._debugRemoveCacheHits=0},6e4),setInterval(function(){return __awaiter(n,void 0,void 0,function(){var t,i,n,s,r,o,a,c,u;return __generator(this,function(e){switch(e.label){case 0:return t=this,[4,logged_in_users_collection_1.LoggedInUsers.find()];case 1:t._loggedInUsers=e.sent(),i=(0,common_1.deepCopy)(this._loggedInUsers),n=function(t){var n;return __generator(this,function(e){switch(e.label){case 0:return!(n=i[t]).date||12e4<Date.now()-n.date.getTime()?s._websocketManager.getWebSocket(n.id_ws)?(s.getEnableDebug()&&console.log(new Date,"Sub Manager","Unsub WS",s._websocketManager.getWebSocket(n.id_ws).user,s._websocketManager.getWebSocket(n.id_ws).id_socket,2),[4,s.unsubscribeAll(s._websocketManager.getWebSocket(n.id_ws))]):[3,2]:[3,4];case 1:return e.sent(),[3,4];case 2:return s._subscriptions.forEach(function(e){for(var t=e.clients.length-1;0<=t;t--)e.clients[t].id_socket===n.id_ws&&e.clients.splice(t,1)}),[4,logged_in_users_collection_1.LoggedInUsers.deleteOne({_id:n._id})];case 3:e.sent(),0<=s._loggedInUsers.findIndex(function(e){return e._id===n._id})&&s._loggedInUsers.splice(s._loggedInUsers.findIndex(function(e){return e._id===n._id}),1),e.label=4;case 4:return[2]}})},r=(s=this)._loggedInUsers.length-1,e.label=2;case 2:return 0<=r?[5,n(r)]:[3,5];case 3:e.sent(),e.label=4;case 4:return r--,[3,2];case 5:for(r=0;r<this._subscriptions.length;r++)for(o=this._subscriptions[r],a=function(e){var t=o.clients[e];c._loggedInUsers.some(function(e){return e.id_ws===t.id_socket})||o.clients.splice(e,1)},c=this,u=o.clients.length-1;0<=u;u--)a(u);return[2]}})})},3e4),[4,flag_collection_1.Flags.findOne({type:"Enable Debug"})];case 2:return(t=e.sent())&&t.value?this._enableDebug=!0:this._enableDebug=!1,this.setCacheLimit(),[2]}})})},SubscriptionManager.prototype.setCacheLimit=function(){"true"===process.env.IS_WORKERS_ENABLED?this._heapLimit=.4*this._heapSize:this._heapLimit=.3*this._heapSize},SubscriptionManager.prototype.invalidatePubsCache=function(r,o){return __awaiter(this,void 0,void 0,function(){var t,n,i,s=this;return __generator(this,function(e){switch(e.label){case 0:return(t=r,n=Date.now(),i=this._invalidationPendingTimestamps.get(t)||n,this._invalidationPendingTimestamps.set(t,i),this._invalidationDebounceTimers.has(t)&&clearTimeout(this._invalidationDebounceTimers.get(t)),n-i>=this.MAX_WAIT_TIME)?(this._invalidationPendingTimestamps.delete(t),[4,this._executeInvalidation(r,o)]):[3,2];case 1:return e.sent(),[3,3];case 2:this._invalidationDebounceTimers.set(t,setTimeout(function(){return __awaiter(s,void 0,void 0,function(){return __generator(this,function(e){switch(e.label){case 0:return this._invalidationPendingTimestamps.delete(t),[4,this._executeInvalidation(r,o)];case 1:return e.sent(),[2]}})})},this.DEBOUNCE_DELAY)),e.label=3;case 3:return[2]}})})},SubscriptionManager.prototype._executeInvalidation=function(h,b){return __awaiter(this,void 0,void 0,function(){var t,n,i,s,r,o,a,c,u,l,_,g,d;return __generator(this,function(e){switch(e.label){case 0:this._invalidationDebounceTimers.has(h)&&(clearTimeout(this._invalidationDebounceTimers.get(h)),this._invalidationDebounceTimers.delete(h)),resolveio_server_app_1.ResolveIOServer.getMongoManager().invalidateQueryCache(h),t=this._subscriptions.filter(function(e){return e.collections.includes(h)}),e.label=1;case 1:e.trys.push([1,17,18,19]),n=__values(t),i=n.next(),e.label=2;case 2:if(i.done)return[3,16];if(s=i.value,this._enableDebug&&console.log(new Date,"Invalidate Sub",s.publication,s.running,s.runAgain),s.running)return s.runAgain=!0,[3,15];if(!this._publications[s.publication].user_specific)return[3,13];e.label=3;case 3:e.trys.push([3,10,11,12]),g=void 0,r=__values(s.clients),o=r.next(),e.label=4;case 4:if(o.done)return[3,9];if(a=o.value,(null==(c=this._websocketManager.getWebSocket(a.id_socket))?void 0:c.readyState)!==c.OPEN)return[3,8];e.label=5;case 5:return e.trys.push([5,7,,8]),[4,this.sendDataToOneWithRetry(c,a.messageId,s,h,b)];case 6:return e.sent(),[3,8];case 7:return e.sent(),[3,8];case 8:return o=r.next(),[3,4];case 9:return[3,12];case 10:return u=e.sent(),g={error:u},[3,12];case 11:try{o&&!o.done&&(d=r.return)&&d.call(r)}finally{if(g)throw g.error}return[7];case 12:return[3,15];case 13:return[4,this.sendDataToAllWithRetry(s,h,b)];case 14:e.sent(),e.label=15;case 15:return i=n.next(),[3,2];case 16:return[3,19];case 17:return u=e.sent(),l={error:u},[3,19];case 18:try{i&&!i.done&&(_=n.return)&&_.call(n)}finally{if(l)throw l.error}return[7];case 19:return[2]}})})},SubscriptionManager.prototype.delay=function(t){return __awaiter(this,void 0,void 0,function(){return __generator(this,function(e){return[2,new Promise(function(e){return setTimeout(e,t)})]})})},SubscriptionManager.prototype.sendDataToAllWithRetry=function(t,n,i){return __awaiter(this,void 0,void 0,function(){return __generator(this,function(e){switch(e.label){case 0:t.running=!0,e.label=1;case 1:return this._enableDebug&&console.log(new Date,"Running sendDataToAll Sub",t.publication),t.runAgain=!1,[4,this.sendDataToAll(t,n,i)];case 2:return(e.sent(),this._enableDebug&&console.log(new Date,"Done sendDataToAll Sub",t.publication,t.runAgain),t.runAgain)?[4,this.delay(500)]:[3,4];case 3:e.sent(),e.label=4;case 4:if(t.runAgain)return[3,1];e.label=5;case 5:return t.running=!1,[2]}})})},SubscriptionManager.prototype.sendDataToOneWithRetry=function(t,n,i,s,r){return __awaiter(this,void 0,void 0,function(){return __generator(this,function(e){switch(e.label){case 0:i.running=!0,e.label=1;case 1:return this._enableDebug&&console.log(new Date,"Running sendDataToOne Sub",i.publication),i.runAgain=!1,[4,this.sendDataToOne(t,n,i,s,r)];case 2:return(e.sent(),this._enableDebug&&console.log(new Date,"Done sendDataToOne Sub",i.publication,i.runAgain),i.runAgain)?[4,this.delay(500)]:[3,4];case 3:e.sent(),e.label=4;case 4:if(i.runAgain)return[3,1];e.label=5;case 5:return i.running=!1,[2]}})})},SubscriptionManager.prototype.publications=function(e){this._publications=Object.assign(this._publications,e)},SubscriptionManager.prototype.loggedInLatency=function(t){var e,n,i,s=this._loggedInUsers.find(function(e){return e.id_ws===t.id_socket});s&&(e=new Date,n=this.latencyBuffer.get(t.id_socket),i=t.latency,!n||e.getTime()-n.lastUpdate.getTime()>=this.LATENCY_UPDATE_THRESHOLD_MS||100<Math.abs(i-n.latency))&&(s.date=e,this.latencyBuffer.set(t.id_socket,{latency:i,lastUpdate:e}))},SubscriptionManager.prototype.flushThrottledLatencyUpdates=function(){return __awaiter(this,void 0,void 0,function(){var t,n;return __generator(this,function(e){switch(e.label){case 0:if(0===this.latencyBuffer.size)return[2];t=Array.from(this.latencyBuffer.entries()).map(function(e){var e=__read(e,2),t=e[0],e=e[1];return{updateOne:{filter:{id_ws:t},update:{$set:{latency:e.latency,date:e.lastUpdate}}}}}),e.label=1;case 1:return e.trys.push([1,3,,4]),[4,logged_in_users_collection_1.LoggedInUsers.bulkWrite(t)];case 2:return e.sent(),this.latencyBuffer.clear(),this.getEnableDebug()&&console.log(new Date,"Sub Manager","Throttled latency batch update successful",t.length),[3,4];case 3:return n=e.sent(),console.error(new Date,"Sub Manager","Throttled latency batch update failed",n),[3,4];case 4:return[2]}})})},SubscriptionManager.prototype.subscribe=function(u,e,l,_,g,d){return __awaiter(this,void 0,void 0,function(){var t,n,i,s,r,o,a,c=this;return __generator(this,function(e){switch(e.label){case 0:return(this._debugSubHits+=1,this._debugSubCollections.some(function(e){return e.publication===g})?this._debugSubCollections.find(function(e){return e.publication===g}).hits+=1:this._debugSubCollections.push({publication:g,hits:1}),t=this._publications[g])?[3,1]:(console.error(new Date,"No Publication: "+g),[2]);case 1:if(1<d.length||d[0]){if(!t.check)return console.error(new Date,"No Check Function For Pub "+g),[2];if(!t.check._schema)return console.error(new Date,"No Check Schema For Pub "+g),[2];for(n={},r=Object.keys(t.check._schema),i=r.filter(function(e){return!e.includes(".")}),s=0;s<d.length;s++)n[i[s]]=d[s];try{t.check.validate(n)}catch(e){if(e)return console.error(new Date,"Error in Pub Check ("+g+")",e),[2]}}return"Bypass"!==u&&(r=u.split("/"),o="",a=r[0],""===r[0]&&(o="/",a=r[1]),o+=a,1<r.length&&(o+="/"),(a=this._subscriptions.filter(function(e){return e.clients.some(function(e){return e.id_socket===l.id_socket&&"Bypass"!==e.messageRoute&&"/"!==e.messageRoute&&e.messageRoute!==u&&!e.messageRoute.startsWith(o)})})).length)&&a.forEach(function(t){t.clients.filter(function(e){return e.id_socket===l.id_socket}).forEach(function(e){c.unsubscribe(e.messageRoute,new Date,l,e.messageId,t.publication,t.subscriptionData)})}),(a=this._subscriptions.find(function(e){return e.publication===g&&JSON.stringify(e.subscriptionData)===JSON.stringify(d)}))?a.clients.some(function(e){return e.id_socket===l.id_socket&&e.messageId===_})||a.clients.push({id_user:l.id_user,messageId:_,id_socket:l.id_socket,messageRoute:u}):this._subscriptions.push({publication:g,subscriptionData:d,collections:this.getPublicationCollections(g),clients:[{id_user:l.id_user,messageId:_,id_socket:l.id_socket,messageRoute:u}],cacheId:0,running:!1,runAgain:!1}),a=a||this._subscriptions.find(function(e){return e.publication===g&&JSON.stringify(e.subscriptionData)===JSON.stringify(d)}),this._enableDebug&&console.log(new Date,"New Sub",a.publication,a.running,a.runAgain,a.clients.length),[4,this.processSubscription(a,l,_)];case 2:e.sent(),e.label=3;case 3:return[2]}})})},SubscriptionManager.prototype.createLoggedInUser=function(i){return __awaiter(this,void 0,void 0,function(){var t,n;return __generator(this,function(e){switch(e.label){case 0:return(t=this._websocketManager.getWebSocket(i))?(n={_id:(0,common_1.objectIdHexString)(),__v:0,date:new Date,id_user:t.id_user,user:t.user,id_ws:t.id_socket},this._loggedInUsers.push(n),[4,logged_in_users_collection_1.LoggedInUsers.insertOne(n)]):[3,2];case 1:return e.sent(),[2,n];case 2:throw new Error("Error in Create Logged In User: No WS")}})})},SubscriptionManager.prototype.unsubscribe=function(e,t,n,i,s,r){if(this._debugUnSubHits+=1,this._publications[s]){var o=this._subscriptions.find(function(e){return e.publication===s&&JSON.stringify(e.subscriptionData)===JSON.stringify(r)});if(o){for(var a=o.clients.length-1;0<=a;a--)o.clients[a].id_user===n.id_user&&o.clients[a].messageId===i&&o.clients[a].id_socket===n.id_socket&&o.clients.splice(a,1);this._enableDebug&&console.log(new Date,"Unsub",o.publication,o.running,o.runAgain,o.clients.length)}}else console.log("No Publication: "+s)},SubscriptionManager.prototype.unsubscribeAll=function(r){return __awaiter(this,void 0,void 0,function(){var t,n,i,s;return __generator(this,function(e){switch(e.label){case 0:return(this._debugUnSubAllHits+=1,r)?r.isUnsubscribed?[2]:(r.isUnsubscribed=!0,0<=this._loggedInUsers.map(function(e){return e.id_ws}).indexOf(r.id_socket)&&this._loggedInUsers.splice(this._loggedInUsers.map(function(e){return e.id_ws}).indexOf(r.id_socket),1),[4,logged_in_users_collection_1.LoggedInUsers.deleteOne({id_ws:r.id_socket})]):[3,2];case 1:for(e.sent(),t=this._subscriptions.filter(function(e){return e.clients.some(function(e){return e.id_user===r.id_user&&e.id_socket===r.id_socket})}),n=t.length-1;0<=n;n--)for(i=t[n],s=i.clients.length-1;0<=s;s--)i.clients[s].id_socket===r.id_socket&&i.clients.splice(s,1);this._websocketManager.removeWebSocket(r),e.label=2;case 2:return[2]}})})},SubscriptionManager.prototype.getActiveSubscriptions=function(){return this._subscriptions},SubscriptionManager.prototype.getPublicationCollections=function(e){return this._publications[e].collections},SubscriptionManager.prototype.tailOpLog=function(i){return __awaiter(this,void 0,void 0,function(){var t,s,n=this;return __generator(this,function(e){switch(e.label){case 0:return!this._oplog$||this._oplog$.closed?[3,2]:(this._oplog$.removeAllListeners(),[4,this._oplog$.close()]);case 1:e.sent(),this._oplog$=null,e.label=2;case 2:return[4,new Promise(function(e){return setTimeout(e,1e3)})];case 3:if(e.sent(),this._oplog$&&!this._oplog$.closed)return[3,12];if(this._oplogRetryCount+=1,5<this._oplogRetryCount&&(console.error("****************** TAIL OPLOG ERROR, RETRYING A BUNCH OF TIMES, KILLING PROCESS **************************"),process.exit(1)),t=[{$match:{$and:[{"ns.coll":{$nin:["log-method-latencies","log-subscriptions","logs","counters","cron-job-histories","email-histories","qb-soap-request-histories","qb-soap-request-responses","qb-soap-requests","qb-soap-retries"]}},{"ns.coll":{$not:/.*\.versions$/}},{"ns.coll":{$not:/^monitor-/}}]}}],!i)return[3,10];s=i,e.label=4;case 4:return e.trys.push([4,5,,9]),this._oplog$=resolveio_server_app_1.ResolveIOServer.getMainDB().watch(t,{resumeAfter:i}),[3,9];case 5:return(e.sent(),this._oplog$)?(this._oplog$.removeAllListeners(),[4,this._oplog$.close()]):[3,7];case 6:e.sent(),this._oplog$=null,e.label=7;case 7:return[4,this.tailOpLog(i)];case 8:return e.sent(),[2];case 9:return[3,11];case 10:this._oplog$=resolveio_server_app_1.ResolveIOServer.getMainDB().watch(t),e.label=11;case 11:console.log(new Date,"oplog started"),this._oplog$.on("change",function(i){return __awaiter(n,void 0,void 0,function(){var t,n;return __generator(this,function(e){switch(e.label){case 0:return i.ns?(this._enableDebug&&console.log(new Date,"Oplog Hit",i.ns),t=i.ns.coll,this._debugOplogCollections.some(function(e){return e.collection===i.ns.coll&&e.type===i.operationType})?this._debugOplogCollections.find(function(e){return e.collection===i.ns.coll&&e.type===i.operationType}).hits+=1:this._debugOplogCollections.push({collection:i.ns.coll,type:i.operationType,hits:1}),t?(this._debugOplogHits+=1,"insert"!==i.operationType?[3,5]:"support-tickets"!==t||"https://resolveio.com"!==this.serverConfig.ROOT_URL?[3,2]:(resolveio_server_app_1.ResolveIOServer.getMainServer().getMethodManager().callMethod.call(resolveio_server_app_1.ResolveIOServer.getMainServer().getMethodManager(),"sendSupportTicketEmail",i.documentKey._id),[4,this.invalidatePubsCache(t,"insert")])):[3,10]):[3,13];case 1:e.sent(),e.label=2;case 2:return"method-responses"===t?[3,4]:[4,this.invalidatePubsCache(t,"insert")];case 3:e.sent(),e.label=4;case 4:return[3,10];case 5:return"update"!==i.operationType&&"replace"!==i.operationType?[3,8]:"method-responses"===t?[3,7]:[4,this.invalidatePubsCache(t,"update")];case 6:e.sent(),e.label=7;case 7:return[3,10];case 8:return"delete"!==i.operationType?[3,10]:"method-responses"===t?[3,10]:[4,this.invalidatePubsCache(t,"delete")];case 9:e.sent(),e.label=10;case 10:return"flags"!==t?[3,12]:[4,flag_collection_1.Flags.findOne({type:"Enable Debug"})];case 11:(n=e.sent())&&n.value?this._enableDebug=!0:this._enableDebug=!1,e.label=12;case 12:s=i._id,process.env.NODE_APP_INSTANCE&&"0"!==process.env.NODE_APP_INSTANCE||"false"!==process.env.IS_WORKERS_ENABLED&&"true"===process.env.IS_WORKER_INSTANCE&&process.env.WORKER_INDEX,e.label=13;case 13:return[2]}})})}),this._oplog$.on("error",function(t){return __awaiter(n,void 0,void 0,function(){return __generator(this,function(e){switch(e.label){case 0:return console.log(new Date,"oplog error",t),[4,this._oplog$.close()];case 1:return e.sent(),[2]}})})}),this._oplog$.on("end",function(){return __awaiter(n,void 0,void 0,function(){return __generator(this,function(e){switch(e.label){case 0:return console.log(new Date,"oplog end"),[4,this._oplog$.close()];case 1:return e.sent(),[2]}})})}),this._oplog$.on("close",function(){return __awaiter(n,void 0,void 0,function(){return __generator(this,function(e){switch(e.label){case 0:return console.log(new Date,"oplog close"),this._oplog$.removeAllListeners(),this._oplog$=null,[4,this.tailOpLog(s)];case 1:return e.sent(),[2]}})})}),e.label=12;case 12:return[2]}})})},SubscriptionManager.prototype.processSubscription=function(n,i,s){return __awaiter(this,void 0,void 0,function(){var t;return __generator(this,function(e){switch(e.label){case 0:if(this._publications[n.publication].user_specific)return[3,8];if(!n.cacheId)return[3,5];e.label=1;case 1:return e.trys.push([1,2,,4]),t=JSON.parse(this._nodeCache.get(n.cacheId),common_1.dateReviver),t={messageId:s,hasError:!1,data:t},this._enableDebug&&console.log(new Date,"Process Sub, Cache",n.publication),this.sendWS(i,t),[3,4];case 2:return e.sent(),this._nodeCache.del(n.cacheId),n.cacheId=0,[4,this.sendDataToAllWithRetry(n,"","newSub")];case 3:return e.sent(),[3,4];case 4:return[3,7];case 5:return this._enableDebug&&console.log(new Date,"Process Sub, Non - Cache",n.publication,n.running),n.running?[2]:[4,this.sendDataToAllWithRetry(n,"","newSub")];case 6:e.sent(),e.label=7;case 7:return[3,10];case 8:return this._enableDebug&&console.log(new Date,"Process Sub Specific, Non - Cache",n.publication,n.running),n.running?[2]:[4,this.sendDataToOneWithRetry(i,s,n,"","newSub")];case 9:e.sent(),e.label=10;case 10:return[2]}})})},SubscriptionManager.prototype.sendDataToOne=function(r,o,a,c,u){return __awaiter(this,void 0,void 0,function(){var t,n,i,s;return __generator(this,function(e){switch(e.label){case 0:t=this._monitorManagerFunction.startMonitorFunction("User Specific Publication",a.publication,"","",a.subscriptionData),e.label=1;case 1:return e.trys.push([1,3,5,7]),resolveio_server_app_1.ResolveIOServer.getMainServer().getMethodManager().callMethod.call(resolveio_server_app_1.ResolveIOServer.getMainServer().getMethodManager(),"insertSubscriptionLog",u,a.publication,c,JSON.stringify(a.subscriptionData)),[4,(s=this._publications[a.publication].function).call.apply(s,__spreadArray([Object.assign({},this,SubscriptionManager.prototype),r.id_user],__read(a.subscriptionData),!1))];case 2:return s=e.sent(),i={messageId:o,hasError:!1,data:s},this.sendWS(r,i),[3,7];case 3:return n=e.sent(),i={messageId:o,hasError:!0,data:n},this.sendWS(r,i),[4,resolveio_server_app_1.ResolveIOServer.getMainServer().getMethodManager().sendEmail("dev@resolveio.com","SERVER - Error Detected - "+this.serverConfig.CLIENT_NAME,"Error Detected During Subscription "+a.publication+" - (sendDataToOne - WS)\n\nData \n"+JSON.stringify(a.subscriptionData,null,2)+"\n\nErrors\n"+JSON.stringify(n,null,2))];case 4:return e.sent(),[3,7];case 5:return[4,this._monitorManagerFunction.finishMonitorFunction(t)];case 6:return e.sent(),[7];case 7:return[2]}})})},SubscriptionManager.prototype.sendDataToAll=function(O,C,E){return __awaiter(this,void 0,void 0,function(){var t,n,i,s,r,o,a,c,u,l,_,g,d,h,b,p,f,v,S,y,m,w,D,M,I=this;return __generator(this,function(e){switch(e.label){case 0:return O.clients.length?[3,1]:(O.cacheId&&(this._nodeCache.del(O.cacheId),O.cacheId=0),0<=(v=this._subscriptions.findIndex(function(e){return e.publication===O.publication&&JSON.stringify(e.subscriptionData)===JSON.stringify(O.subscriptionData)}))&&this._subscriptions.splice(v,1),[2]);case 1:t=this._monitorManagerFunction.startMonitorFunction("Publication",O.publication,"","",O.subscriptionData),e.label=2;case 2:return e.trys.push([2,4,6,8]),"superadminAPM"!==O.publication&&"loggedInUsers"!==O.publication&&resolveio_server_app_1.ResolveIOServer.getMainServer().getMethodManager().callMethod.call(resolveio_server_app_1.ResolveIOServer.getMainServer().getMethodManager(),"insertSubscriptionLog",E,O.publication,C,JSON.stringify(O.subscriptionData)),[4,(v=this._publications[O.publication].function).call.apply(v,__spreadArray([Object.assign({},this,SubscriptionManager.prototype)],__read(O.subscriptionData),!1))];case 3:if(n=e.sent(),O.cacheId)try{if(i=JSON.parse(this._nodeCache.get(O.cacheId),common_1.dateReviver),JSON.stringify(i)!==JSON.stringify(n)){try{for(s=__values(O.clients),r=s.next();!r.done;r=s.next())b=r.value,(p=this._websocketManager.getWebSocket(b.id_socket))&&p.readyState===p.OPEN&&(f={messageId:b.messageId,hasError:!1,data:n},this.sendWS(p,f))}catch(e){S={error:e}}finally{try{r&&!r.done&&(y=s.return)&&y.call(s)}finally{if(S)throw S.error}}this._nodeCache.del(O.cacheId),(0,common_1.getBinarySize)(JSON.stringify(n))<1e6&&!O.collections.includes("logs")&&!O.collections.find(function(e){return e.endsWith(".versions")})&&!O.collections.find(function(e){return e.startsWith("monitor-")})?this._nodeCache.set(O.cacheId,JSON.stringify(n)):O.cacheId=0}}catch(e){O.clients.map(function(e){var t=I._websocketManager.getWebSocket(e.id_socket);t&&t.readyState===t.OPEN&&(e={messageId:e.messageId,hasError:!1,data:n},I.sendWS(t,e))}),this._nodeCache.del(O.cacheId),(0,common_1.getBinarySize)(JSON.stringify(n))<1e6&&!O.collections.includes("logs")&&!O.collections.find(function(e){return e.endsWith(".versions")})&&!O.collections.find(function(e){return e.startsWith("monitor-")})?this._nodeCache.set(O.cacheId,JSON.stringify(n)):O.cacheId=0}else{try{for(o=__values(O.clients),a=o.next();!a.done;a=o.next())b=a.value,(p=this._websocketManager.getWebSocket(b.id_socket))&&p.readyState===p.OPEN&&(f={messageId:b.messageId,hasError:!1,data:n},this.sendWS(p,f))}catch(e){m={error:e}}finally{try{a&&!a.done&&(w=o.return)&&w.call(o)}finally{if(m)throw m.error}}if((0,common_1.getBinarySize)(JSON.stringify(n))<1e6&&!O.collections.includes("logs")&&!O.collections.find(function(e){return e.endsWith(".versions")})&&!O.collections.find(function(e){return e.startsWith("monitor-")})){if(O.cacheId=this._cacheId++,this._nodeCache.set(O.cacheId,JSON.stringify(n)),(c=this._nodeCache.getStats().vsize)>this._heapLimit){for(u=0,l=this._subscriptions.filter(function(e){return e.cacheId&&!e.clients.length}),_=0;_<l.length&&(this._debugRemoveCacheHits+=1,this._nodeCache.del(l[_].cacheId),l[_].cacheId=0,u+=1,!(this._nodeCache.getStats().vsize<.75*this._heapLimit));_++);this._enableDebug&&console.log("Sub Cache: Too Big - "+O.publication+" - Deleted: "+u+" - "+c)}}else O.cacheId=0}return[3,8];case 4:g=e.sent();try{for(d=__values(O.clients),h=d.next();!h.done;h=d.next())b=h.value,(p=this._websocketManager.getWebSocket(b.id_socket))&&p.readyState===p.OPEN&&(f={messageId:b.messageId,hasError:!0,data:g},this.sendWS(p,f))}catch(e){D={error:e}}finally{try{h&&!h.done&&(M=d.return)&&M.call(d)}finally{if(D)throw D.error}}return[4,resolveio_server_app_1.ResolveIOServer.getMainServer().getMethodManager().sendEmail("dev@resolveio.com","SERVER - Error Detected - "+this.serverConfig.CLIENT_NAME,"Error Detected During Subscription "+O.publication+" - (sendPubData)\n\nData \n"+JSON.stringify(O.subscriptionData,null,2)+"\n\nErrors\n"+JSON.stringify(g,null,2))];case 5:return e.sent(),[3,8];case 6:return[4,this._monitorManagerFunction.finishMonitorFunction(t)];case 7:return e.sent(),[7];case 8:return[2]}})})},SubscriptionManager.prototype.sendWS=function(e,t){this._websocketManager.send(e,t)},SubscriptionManager.prototype.getEnableDebug=function(){return this._enableDebug},SubscriptionManager}();exports.SubscriptionManager=SubscriptionManager;
2
2
  //# sourceMappingURL=subscription.manager.js.map