@resolveio/server-lib 20.5.1 → 20.5.4

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 (113) hide show
  1. package/collections/app-status.collection.js +1 -1
  2. package/collections/app-status.collection.js.map +1 -1
  3. package/collections/counter.collection.js +1 -1
  4. package/collections/counter.collection.js.map +1 -1
  5. package/collections/cron-job-history.collection.js +1 -1
  6. package/collections/cron-job-history.collection.js.map +1 -1
  7. package/collections/cron-job.collection.js +1 -1
  8. package/collections/cron-job.collection.js.map +1 -1
  9. package/collections/email-history.collection.js +1 -1
  10. package/collections/email-history.collection.js.map +1 -1
  11. package/collections/email-verified.collection.js +1 -1
  12. package/collections/email-verified.collection.js.map +1 -1
  13. package/collections/file.collection.js +1 -1
  14. package/collections/file.collection.js.map +1 -1
  15. package/collections/flag-update.collection.js +1 -1
  16. package/collections/flag-update.collection.js.map +1 -1
  17. package/collections/flag.collection.js +1 -1
  18. package/collections/flag.collection.js.map +1 -1
  19. package/collections/log-method-latency.collection.js +1 -1
  20. package/collections/log-method-latency.collection.js.map +1 -1
  21. package/collections/log-subscription.collection.js +1 -1
  22. package/collections/log-subscription.collection.js.map +1 -1
  23. package/collections/log.collection.js +1 -1
  24. package/collections/log.collection.js.map +1 -1
  25. package/collections/logged-in-users.collection.js +1 -1
  26. package/collections/logged-in-users.collection.js.map +1 -1
  27. package/collections/method-response.collection.js +1 -1
  28. package/collections/method-response.collection.js.map +1 -1
  29. package/collections/monitor-cpu.collection.js +1 -1
  30. package/collections/monitor-cpu.collection.js.map +1 -1
  31. package/collections/monitor-memory.collection.js +1 -1
  32. package/collections/monitor-memory.collection.js.map +1 -1
  33. package/collections/monitor-mongo.collection.js +1 -1
  34. package/collections/monitor-mongo.collection.js.map +1 -1
  35. package/collections/notification.collection.js +1 -1
  36. package/collections/notification.collection.js.map +1 -1
  37. package/collections/report-builder-dashboard-builder.collection.js +1 -1
  38. package/collections/report-builder-dashboard-builder.collection.js.map +1 -1
  39. package/collections/report-builder-library.collection.js +1 -1
  40. package/collections/report-builder-library.collection.js.map +1 -1
  41. package/collections/report-builder-report.collection.js +1 -1
  42. package/collections/report-builder-report.collection.js.map +1 -1
  43. package/collections/user-group.collection.js +1 -1
  44. package/collections/user-group.collection.js.map +1 -1
  45. package/collections/user-guide.collection.js +1 -1
  46. package/collections/user-guide.collection.js.map +1 -1
  47. package/collections/user.collection.js +1 -1
  48. package/collections/user.collection.js.map +1 -1
  49. package/collections/worker-task-request.collection.js +1 -1
  50. package/collections/worker-task-request.collection.js.map +1 -1
  51. package/collections/worker-task-response.collection.js +1 -1
  52. package/collections/worker-task-response.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 +1 -2
  58. package/http/auth.js +1 -1
  59. package/http/auth.js.map +1 -1
  60. package/http/home.d.ts +1 -2
  61. package/http/home.js +1 -1
  62. package/http/home.js.map +1 -1
  63. package/index.d.ts +0 -30
  64. package/index.js +1 -1
  65. package/index.js.map +1 -1
  66. package/managers/cron.manager.js +1 -1
  67. package/managers/cron.manager.js.map +1 -1
  68. package/managers/local-log.manager.js +1 -1
  69. package/managers/local-log.manager.js.map +1 -1
  70. package/managers/method.manager.js +1 -1
  71. package/managers/method.manager.js.map +1 -1
  72. package/managers/mongo.manager.d.ts +1 -2
  73. package/managers/mongo.manager.js +1 -1
  74. package/managers/mongo.manager.js.map +1 -1
  75. package/managers/monitor.manager.d.ts +0 -1
  76. package/managers/monitor.manager.js +1 -1
  77. package/managers/monitor.manager.js.map +1 -1
  78. package/managers/subscription.manager.js +1 -1
  79. package/managers/subscription.manager.js.map +1 -1
  80. package/managers/websocket.manager.d.ts +1 -2
  81. package/managers/websocket.manager.js.map +1 -1
  82. package/methods/accounts.js +1 -1
  83. package/methods/accounts.js.map +1 -1
  84. package/methods/aws.js +1 -1
  85. package/methods/aws.js.map +1 -1
  86. package/methods/collections.js +1 -1
  87. package/methods/collections.js.map +1 -1
  88. package/methods/counters.js +1 -1
  89. package/methods/counters.js.map +1 -1
  90. package/methods/logs.js +1 -1
  91. package/methods/logs.js.map +1 -1
  92. package/methods/monitor.js +1 -1
  93. package/methods/monitor.js.map +1 -1
  94. package/methods/report-builder.js +1 -1
  95. package/methods/report-builder.js.map +1 -1
  96. package/package.json +1 -1
  97. package/public_api.d.ts +1 -0
  98. package/public_api.js +1 -1
  99. package/public_api.js.map +1 -1
  100. package/resolveio-server-app.d.ts +32 -0
  101. package/resolveio-server-app.js +2 -0
  102. package/resolveio-server-app.js.map +1 -0
  103. package/server-app.d.ts +2 -1
  104. package/server-app.js +1 -1
  105. package/server-app.js.map +1 -1
  106. package/util/common.d.ts +1 -0
  107. package/util/common.js +1 -1
  108. package/util/common.js.map +1 -1
  109. package/util/schema-report-builder.js +1 -1
  110. package/util/schema-report-builder.js.map +1 -1
  111. package/client-server-app.d.ts +0 -1
  112. package/client-server-app.js +0 -2
  113. package/client-server-app.js.map +0 -1
@@ -1,7 +1,6 @@
1
1
  export declare class MonitorManager {
2
2
  private _instanceHostname;
3
3
  private _monitorData;
4
- private _localLogManager;
5
4
  constructor();
6
5
  cpuAverage(): {
7
6
  idle: number;
@@ -1,2 +1,2 @@
1
- "use strict";var __awaiter=this&&this.__awaiter||function(t,n,i,r){return new(i=i||Promise)(function(o,e){function fulfilled(t){try{step(r.next(t))}catch(t){e(t)}}function rejected(t){try{step(r.throw(t))}catch(t){e(t)}}function step(t){var e;t.done?o(t.value):((e=t.value)instanceof i?e:new i(function(t){t(e)})).then(fulfilled,rejected)}step((r=r.apply(t,n||[])).next())})},__generator=this&&this.__generator||function(n,i){var r,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(t){var e=[o,t];if(r)throw new TypeError("Generator is already executing.");for(;c=l&&e[l=0]?0:c;)try{if(r=1,a&&(s=2&e[0]?a.return:e[0]?a.throw||((s=a.return)&&s.call(a),0):a.next)&&!(s=s.call(a,e[1])).done)return s;switch(a=0,(e=s?[2&e[0],s.value]:e)[0]){case 0:case 1:s=e;break;case 4:return c.label++,{value:e[1],done:!1};case 5:c.label++,a=e[1],e=[0];continue;case 7:e=c.ops.pop(),c.trys.pop();continue;default:if(!(s=0<(s=c.trys).length&&s[s.length-1])&&(6===e[0]||2===e[0])){c=0;continue}if(3===e[0]&&(!s||e[1]>s[0]&&e[1]<s[3]))c.label=e[1];else if(6===e[0]&&c.label<s[1])c.label=s[1],s=e;else{if(!(s&&c.label<s[2])){s[2]&&c.ops.pop(),c.trys.pop();continue}c.label=s[2],c.ops.push(e)}}e=i.call(n,c)}catch(t){e=[6,t],a=0}finally{r=s=0}if(5&e[0])throw e[1];return{value:e[0]?e[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"),index_1=require("../index"),mongo_manager_1=require("./mongo.manager"),local_log_manager_1=require("./local-log.manager"),os=require("os"),MAX_BATCH_SIZE=8388608,MonitorManager=function(){function MonitorManager(){this._instanceHostname="",this._monitorData=[],this._instanceHostname=os.hostname().replace(/\./g,"-"),this._localLogManager=new local_log_manager_1.LocalLogManager,this.setupIntervals(),this.setupEventLoop()}return MonitorManager.prototype.cpuAverage=function(){for(var t=0,e=0,o=os.cpus(),n=0,i=o.length;n<i;n++){var r,a=o[n];for(r in a.times)e+=a.times[r];t+=a.times.idle}return{idle:t/o.length,total:e/o.length}},MonitorManager.prototype.setupIntervals=function(){return __awaiter(this,void 0,void 0,function(){var e,n,i=this;return __generator(this,function(t){switch(t.label){case 0:if("https://resolveio.com"===index_1.ResolveIOServer.getServerConfig().ROOT_URL||"http://localhost:4200"===index_1.ResolveIOServer.getServerConfig().ROOT_URL)return[3,4];t.label=1;case 1:return t.trys.push([1,3,,4]),[4,axios_1.default.get("http://169.254.169.254/latest/meta-data/instance-id")];case 2:return e=t.sent(),setInterval(function(){return __awaiter(i,void 0,void 0,function(){return __generator(this,function(t){switch(t.label){case 0:return t.trys.push([0,2,,3]),[4,axios_1.default.post("https://backend.resolveio.com/api/health",{type:"application",id_aws_instance:e.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 t.sent(),[3,3];case 2:return t.sent(),[3,3];case 3:return[2]}})})},1e4),[3,4];case 3:return t.sent(),[3,4];case 4:return n=this.cpuAverage(),setInterval(function(){var t=new Date,e=i.cpuAverage(),o=1-(e.idle-n.idle)/(e.total-n.total),o=("https://resolveio.com"===index_1.ResolveIOServer.getServerConfig().ROOT_URL||"http://localhost:4200"===index_1.ResolveIOServer.getServerConfig().ROOT_URL?monitor_cpu_collection_1.MonitorCPUs.create({_id:(0,mongo_manager_1.objectIdHexString)(),metadata:{instance:i._instanceHostname,client:index_1.ResolveIOServer.getServerConfig().CLIENT_NAME},date:t,app:o,system:os.loadavg()[0]}):i.pushMonitorData({metadata:{instance:i._instanceHostname,client:index_1.ResolveIOServer.getServerConfig().CLIENT_NAME},date:t,app:o,system:os.loadavg()[0],type:"cpu"}),n=e,process.memoryUsage());"https://resolveio.com"===index_1.ResolveIOServer.getServerConfig().ROOT_URL||"http://localhost:4200"===index_1.ResolveIOServer.getServerConfig().ROOT_URL?monitor_memory_collection_1.MonitorMemorys.create({_id:(0,mongo_manager_1.objectIdHexString)(),metadata:{instance:i._instanceHostname,client:index_1.ResolveIOServer.getServerConfig().CLIENT_NAME},date:t,physical_total:o.heapTotal,physical_used:o.heapUsed,physical_free:os.freemem(),virtual:o.rss,private:o.external,physical:os.totalmem()}):i.pushMonitorData({metadata:{instance:i._instanceHostname,client:index_1.ResolveIOServer.getServerConfig().CLIENT_NAME},date:t,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(t){"https://resolveio.com"===index_1.ResolveIOServer.getServerConfig().ROOT_URL||"http://localhost:4200"===index_1.ResolveIOServer.getServerConfig().ROOT_URL?t.collection.startsWith("monitor-")||monitor_mongo_collection_1.MonitorMongos.create({_id:(0,mongo_manager_1.objectIdHexString)(),metadata:{instance:this._instanceHostname,client:index_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"}):this._instanceHostname&&this.pushMonitorData({metadata:{instance:this._instanceHostname,client:index_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",type:"mongo"})},MonitorManager.prototype.pushMonitorData=function(t){this._monitorData.push(t),this.estimateBatchSize()>=MAX_BATCH_SIZE&&this.flushMonitorData()},MonitorManager.prototype.estimateBatchSize=function(){return this._monitorData.reduce(function(t,e){return t+Buffer.byteLength(JSON.stringify(e))},0)},MonitorManager.prototype.setupEventLoop=function(){var t=this;setInterval(function(){0<t._monitorData.length&&t.flushMonitorData()},2500)},MonitorManager.prototype.flushMonitorData=function(){for(var t=[],e=0;0<this._monitorData.length&&e<MAX_BATCH_SIZE;){var o=this._monitorData.shift(),n=Buffer.byteLength(JSON.stringify(o));if(MAX_BATCH_SIZE<e+n&&0<t.length){this._monitorData.unshift(o);break}t.push(o),e+=n}try{this._localLogManager.writeLogs(t)}catch(t){console.error(new Date,"MonitorManager","Failed to flush monitor data to file:",t)}},MonitorManager}(),MonitorMongo=(exports.MonitorManager=MonitorManager,function(){function MonitorMongo(t,e,o){this._startTime=0,this._method="",this._collection="",this._query="",this._startTime=Date.now(),this._method=t,this._collection=e,this._query=o}return MonitorMongo.prototype.finish=function(){var t=Date.now(),t={date:new Date(t),method:this._method,collection:this._collection,query:this._query,duration:t-this._startTime};index_1.ResolveIOServer.getMainServer().getMonitorManager().addMongoTracker(t)},MonitorMongo}()),MonitorManagerFunction=(exports.MonitorMongo=MonitorMongo,function(){function MonitorManagerFunction(){this._functionCnt=0,this._functions=[],this._functionLastCompletedWS={}}return MonitorManagerFunction.prototype.startMonitorFunction=function(t,e,o,n,i){t=new MonitorFunction(this._functionCnt++,t,e,o,n,i);return this._functions.push(t),t.id},MonitorManagerFunction.prototype.finishMonitorFunction=function(e){var t=this._functions.find(function(t){return t.id===e});t&&(t.finish(),this._functionLastCompleted=t,this._functionLastCompletedWS[t.id_socket]=t,this._functions.splice(this._functions.map(function(t){return t.id}).indexOf(e),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(t,e,o,n,i,r){var a=this;this._timer=null,this._maxDiff=0,this.startTime=null,this.endTime=null,this.lastTime=null,this.id_socket="",this.id=0,this.id=t,this.startTime=new Date,this._functionType=e,this._functionName=o,this._user=n,this.id_socket=i,this._data=r,this.lastTime=new Date,this._timer=setInterval(function(){var t=Date.now()-a.lastTime.getTime();t>a._maxDiff&&(a._maxDiff=t),a.lastTime=new Date},1e3)}return MonitorFunction.prototype.finish=function(){this._timer&&(clearInterval(this._timer),this._timer=null),this.endTime=new Date,this._maxDiff},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:[]},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"),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({metadata:{instance:r._instanceHostname,client:resolveio_server_app_1.ResolveIOServer.getServerConfig().CLIENT_NAME},date:e,app:o,system:os.loadavg()[0],type:"cpu"}),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({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({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",type:"mongo"})},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._maxDiff=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._maxDiff&&(a._maxDiff=e),a.lastTime=new Date},1e3)}return MonitorFunction.prototype.finish=function(){this._timer&&(clearInterval(this._timer),this._timer=null),this.endTime=new Date,this._maxDiff},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","index_1","mongo_manager_1","local_log_manager_1","os","MAX_BATCH_SIZE","MonitorManager","this","_instanceHostname","_monitorData","hostname","replace","_localLogManager","LocalLogManager","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","push","estimateBatchSize","flushMonitorData","reduce","item","JSON","stringify","batch","batchSize","shift","itemSize","unshift","writeLogs","error","console","MonitorMongo","exports","_startTime","_method","_collection","_query","finish","endTime","mongoMonitor","getMainServer","getMonitorManager","MonitorManagerFunction","_functionCnt","_functions","_functionLastCompletedWS","startMonitorFunction","functionType","functionName","user","id_socket","newMonitorFunction","MonitorFunction","id","finishMonitorFunction","monitor","find","a","_functionLastCompleted","splice","map","indexOf","getActiveMonitorFunctions","getLastCompletedMonitorFunction","getLastCompletedMonitorFunctionWS","_timer","_maxDiff","startTime","lastTime","_functionType","_functionName","_user","_data","diff","getTime","clearInterval"],"mappings":"k8CACAA,S,iKAAAC,QAAA,OAAA,GACAC,yBAAAD,QAAA,uCAAA,EACAE,4BAAAF,QAAA,0CAAA,EACAG,2BAAAH,QAAA,yCAAA,EACAI,QAAAJ,QAAA,UAAA,EACAK,gBAAAL,QAAA,iBAAA,EACAM,oBAAAN,QAAA,qBAAA,EAEMO,GAAKP,QAAQ,IAAI,EACjBQ,eAAiB,QAEvBC,eAAA,WAOC,SAAAA,iBANQC,KAAAC,kBAAoB,GACpBD,KAAAE,aAAe,GAMtBF,KAAKC,kBAAoBJ,GAAGM,SAAQ,EAAGC,QAAQ,MAAO,GAAG,EAEzDJ,KAAKK,iBAAmB,IAAIT,oBAAAU,gBAE5BN,KAAKO,eAAc,EACnBP,KAAKQ,eAAc,CACpB,CA2ND,OAzNCT,eAAAU,UAAAC,WAAA,WAIC,IAHA,IAAIC,EAAY,EAAGC,EAAY,EAC3BC,EAAOhB,GAAGgB,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,EAEMjB,eAAAU,UAAAF,eAAN,W,4HAGoD,0BAAlDb,QAAA4B,gBAAgBC,gBAAe,EAAa,UACQ,0BAAlD7B,QAA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KAAKqI,SACRU,cAAc/I,KAAKqI,MAAM,EACzBrI,KAAKqI,OAAS,MAGfrI,KAAK0G,QAAU,IAAI9D,KAEf5C,KAAKsI,QAGV,EACDd,eAAA,EAAC,GArDYpB,QAAAoB,gBAAAA","file":"monitor.manager.js","sourcesContent":["import ResolveIOMainServer from '../server-app';\nimport 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 '../index';\nimport { objectIdHexString } from './mongo.manager';\nimport { LocalLogManager } 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 = [];\n\t\n\t// Create one instance of LocalLogManager for all logs\n\tprivate _localLogManager: LocalLogManager;\n\n\tconstructor() {\n\t\tthis._instanceHostname = os.hostname().replace(/\\./g, '-');\n\n\t\tthis._localLogManager = new LocalLogManager();\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\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\ttype: 'cpu'\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\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\ttype: 'memory'\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\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\ttype: 'mongo'\n\t\t\t\t});\n\t\t\t}\n\t\t}\n\t}\n\n\tprivate pushMonitorData(data: any) {\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\tthis._localLogManager.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| 'Internal Method'\n\t| 'Method'\n\t| 'User Specific Publication'\n\t| 'Publication';\n\nexport class MonitorFunction {\n\tprivate _timer: NodeJS.Timeout = null;\n\tprivate _maxDiff = 0;\n\tprivate _functionType: MonitorFunctionType;\n\tprivate _functionName: string;\n\tprivate _user: string;\n\tprivate _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._maxDiff) {\n\t\t\t\tthis._maxDiff = 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._maxDiff >= 5000) {\n\t\t\t// Optional slow function logging or email\n\t\t}\n\t}\n}"]}
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","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","push","estimateBatchSize","flushMonitorData","reduce","item","JSON","stringify","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","functionType","functionName","user","id_socket","newMonitorFunction","MonitorFunction","id","finishMonitorFunction","monitor","find","a","_functionLastCompleted","splice","map","indexOf","getActiveMonitorFunctions","getLastCompletedMonitorFunction","getLastCompletedMonitorFunctionWS","_timer","_maxDiff","startTime","lastTime","_functionType","_functionName","_user","_data","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,EAEMM,GAAKN,QAAQ,IAAI,EACjBO,eAAiB,QAEvBC,eAAA,WAIC,SAAAA,iBAHQC,KAAAC,kBAAoB,GACpBD,KAAAE,aAAe,GAGtBF,KAAKC,kBAAoBJ,GAAGM,SAAQ,EAAGC,QAAQ,MAAO,GAAG,EAEzDJ,KAAKK,eAAc,EACnBL,KAAKM,eAAc,CACpB,CA2ND,OAzNCP,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,EA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AEzCD,EAAO/G,EAAKwG,WACfxG,EAAKwG,SAAWO,GAGjB/G,EAAK0G,SAAW,IAAI7F,IACrB,EAAG,GAAI,CACR,CAcD,OAZQ6E,gBAAAhH,UAAAiG,OAAP,WACKxG,KAAKoI,SACRU,cAAc9I,KAAKoI,MAAM,EACzBpI,KAAKoI,OAAS,MAGfpI,KAAKyG,QAAU,IAAI/D,KAEf1C,KAAKqI,QAGV,EACDd,eAAA,EAAC,GArDYpB,QAAAoB,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';\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 = [];\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\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\ttype: 'cpu'\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\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\ttype: 'memory'\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\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\ttype: 'mongo'\n\t\t\t\t});\n\t\t\t}\n\t\t}\n\t}\n\n\tprivate pushMonitorData(data: any) {\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| 'Internal Method'\n\t| 'Method'\n\t| 'User Specific Publication'\n\t| 'Publication';\n\nexport class MonitorFunction {\n\tprivate _timer: NodeJS.Timeout = null;\n\tprivate _maxDiff = 0;\n\tprivate _functionType: MonitorFunctionType;\n\tprivate _functionName: string;\n\tprivate _user: string;\n\tprivate _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._maxDiff) {\n\t\t\t\tthis._maxDiff = 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._maxDiff >= 5000) {\n\t\t\t// Optional slow function logging or email\n\t\t}\n\t}\n}"]}
@@ -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,c={label:0,sent:function(){if(1&a[0])throw a[1];return a[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(n){return function(e){var t=[n,e];if(s)throw new TypeError("Generator is already executing.");for(;c=l&&t[l=0]?0:c;)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 c.label++,{value:t[1],done:!1};case 5:c.label++,r=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=o.call(i,c)}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"),index_1=require("../index"),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"),mongo_manager_1=require("./mongo.manager"),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._websocketManager=index_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,c,l;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];c._loggedInUsers.some(function(e){return e.id_ws===t.id_socket})||r.clients.splice(e,1)},c=this,l=r.clients.length-1;0<=l;l--)a(l);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(u,g){return __awaiter(this,void 0,void 0,function(){var t,n,o,i,s,r,a,c,l=this;return __generator(this,function(e){index_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?Promise.all(i.clients.map(function(n){return __awaiter(l,void 0,void 0,function(){var t;return __generator(this,function(e){if((t=this._websocketManager.getWebSocket(n.id_socket))&&t.readyState===t.OPEN)try{this.sendDataToOneWithRetry(t,n.messageId,i,u,g)}catch(e){index_1.ResolveIOServer.getMainServer().getMethodManager().sendEmail("dev@resolveio.com","SERVER - Error Detected - "+this.serverConfig.CLIENT_NAME,"Error Detected During sendDataToOne - User Specific - Socket: "+n.id_socket+", User: "+n.id_user+", MessageId: "+n.messageId+", Pub: "+i.publication+", Err: "+JSON.stringify(e,null,2))}return[2]})})})):o.sendDataToAllWithRetry(i,u,g)},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&&(c=i.return)&&c.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 c={},l=Object.keys(a.check._schema).filter(function(e){return!e.includes(".")}),u=0;u<s.length;u++)c[l[u]]=s[u];try{a.check.validate(c)}catch(e){if(e)return void console.error(new Date,"Error in Pub Check ("+o+")",e)}}"Bypass"!==t&&(a=t.split("/"),g="",d=a[0],""===a[0]&&(g="/",d=a[1]),g+=d,1<a.length&&(g+="/"),(d=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(g)})})).length)&&(index_1.ResolveIOServer.getMainServer().getMethodManager().sendEmail("dev@resolveio.com","SERVER - Detected Undestroyed Subscription - "+this.serverConfig.CLIENT_NAME,"USER: "+n.user+" (Socket: "+n.id_socket+") is on route: "+t+" but has the following subscriptions on other routes:"+JSON.stringify(d,null,2)),d.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,d,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,mongo_manager_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:["logs","log-method-latencies","log-subscriptions"]}},{"ns.coll":{$not:/.*\.versions$/}},{"ns.coll":{$not:/^monitor-/}}]}}],o){n=o;try{this._oplog$=index_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$=index_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&&(index_1.ResolveIOServer.getMainServer().getMethodManager().callMethodInternal.call(index_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,c,l){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]),index_1.ResolveIOServer.getMainServer().getMethodManager().callMethodInternal.call(index_1.ResolveIOServer.getMainServer().getMethodManager(),"insertSubscriptionLog",l,a.publication,c,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),index_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,c,l,u=this;return __generator(this,function(e){switch(e.label){case 0:return g.clients.length?[3,1]:(g.cacheId&&(this._nodeCache.del(g.cacheId),g.cacheId=0),0<=(l=this._subscriptions.findIndex(function(e){return e.publication===g.publication&&JSON.stringify(e.subscriptionData)===JSON.stringify(g.subscriptionData)}))&&this._subscriptions.splice(l,1),[2]);case 1:t=this._monitorManagerFunction.startMonitorFunction("Publication",g.publication,"","",g.subscriptionData),e.label=2;case 2:return e.trys.push([2,4,,5]),"superadminAPM"!==g.publication&&"loggedInUsers"!==g.publication&&index_1.ResolveIOServer.getMainServer().getMethodManager().callMethodInternal.call(index_1.ResolveIOServer.getMainServer().getMethodManager(),"insertSubscriptionLog",_,g.publication,d,JSON.stringify(g.subscriptionData)),[4,(l=this._publications[g.publication].function).call.apply(l,__spreadArray([Object.assign({},this,SubscriptionManager.prototype)],__read(g.subscriptionData),!1))];case 3:if(o=e.sent(),this._monitorManagerFunction.finishMonitorFunction(t),g.cacheId)try{n=JSON.parse(this._nodeCache.get(g.cacheId),common_1.dateReviver),JSON.stringify(n)!==JSON.stringify(o)&&(Promise.all(g.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(g.cacheId),!((0,common_1.getBinarySize)(JSON.stringify(o))<1e6)||g.collections.includes("logs")||g.collections.includes("log-method-latencies")||g.collections.includes("log-subscriptions")||g.collections.find(function(e){return e.endsWith(".versions")})||g.collections.find(function(e){return e.startsWith("monitor-")})?g.cacheId=0:this._nodeCache.set(g.cacheId,JSON.stringify(o)))}catch(e){Promise.all(g.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(g.cacheId),!((0,common_1.getBinarySize)(JSON.stringify(o))<1e6)||g.collections.includes("logs")||g.collections.includes("log-method-latencies")||g.collections.includes("log-subscriptions")||g.collections.find(function(e){return e.endsWith(".versions")})||g.collections.find(function(e){return e.startsWith("monitor-")})?g.cacheId=0:this._nodeCache.set(g.cacheId,JSON.stringify(o))}else if(Promise.all(g.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)||g.collections.includes("logs")||g.collections.includes("log-method-latencies")||g.collections.includes("log-subscriptions")||g.collections.find(function(e){return e.endsWith(".versions")})||g.collections.find(function(e){return e.startsWith("monitor-")}))g.cacheId=0;else if(g.cacheId=this._cacheId++,this._nodeCache.set(g.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 - "+g.publication+" - Deleted: "+s+" - "+i)}return[3,5];case 4:return c=e.sent(),this._monitorManagerFunction.finishMonitorFunction(t),Promise.all(g.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:c},this.sendWS(t,n)),[2]})})})),index_1.ResolveIOServer.getMainServer().getMethodManager().sendEmail("dev@resolveio.com","SERVER - Error Detected - "+this.serverConfig.CLIENT_NAME,"Error Detected During Subscription "+g.publication+" - (sendPubData)\n\nData \n"+JSON.stringify(g.subscriptionData,null,2)+"\n\nErrors\n"+JSON.stringify(c,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,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._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(u,g){return __awaiter(this,void 0,void 0,function(){var t,n,o,i,s,r,a,l,c=this;return __generator(this,function(e){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?Promise.all(i.clients.map(function(n){return __awaiter(c,void 0,void 0,function(){var t;return __generator(this,function(e){if((t=this._websocketManager.getWebSocket(n.id_socket))&&t.readyState===t.OPEN)try{this.sendDataToOneWithRetry(t,n.messageId,i,u,g)}catch(e){resolveio_server_app_1.ResolveIOServer.getMainServer().getMethodManager().sendEmail("dev@resolveio.com","SERVER - Error Detected - "+this.serverConfig.CLIENT_NAME,"Error Detected During sendDataToOne - User Specific - Socket: "+n.id_socket+", User: "+n.id_user+", MessageId: "+n.messageId+", Pub: "+i.publication+", Err: "+JSON.stringify(e,null,2))}return[2]})})})):o.sendDataToAllWithRetry(i,u,g)},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(g)})})).length)&&(resolveio_server_app_1.ResolveIOServer.getMainServer().getMethodManager().sendEmail("dev@resolveio.com","SERVER - Detected Undestroyed Subscription - "+this.serverConfig.CLIENT_NAME,"USER: "+n.user+" (Socket: "+n.id_socket+") is on route: "+t+" but has the following subscriptions on other routes:"+JSON.stringify(_,null,2)),_.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:["logs","log-method-latencies","log-subscriptions"]}},{"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().callMethodInternal.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().callMethodInternal.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 g.clients.length?[3,1]:(g.cacheId&&(this._nodeCache.del(g.cacheId),g.cacheId=0),0<=(c=this._subscriptions.findIndex(function(e){return e.publication===g.publication&&JSON.stringify(e.subscriptionData)===JSON.stringify(g.subscriptionData)}))&&this._subscriptions.splice(c,1),[2]);case 1:t=this._monitorManagerFunction.startMonitorFunction("Publication",g.publication,"","",g.subscriptionData),e.label=2;case 2:return e.trys.push([2,4,,5]),"superadminAPM"!==g.publication&&"loggedInUsers"!==g.publication&&resolveio_server_app_1.ResolveIOServer.getMainServer().getMethodManager().callMethodInternal.call(resolveio_server_app_1.ResolveIOServer.getMainServer().getMethodManager(),"insertSubscriptionLog",d,g.publication,_,JSON.stringify(g.subscriptionData)),[4,(c=this._publications[g.publication].function).call.apply(c,__spreadArray([Object.assign({},this,SubscriptionManager.prototype)],__read(g.subscriptionData),!1))];case 3:if(o=e.sent(),this._monitorManagerFunction.finishMonitorFunction(t),g.cacheId)try{n=JSON.parse(this._nodeCache.get(g.cacheId),common_1.dateReviver),JSON.stringify(n)!==JSON.stringify(o)&&(Promise.all(g.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(g.cacheId),!((0,common_1.getBinarySize)(JSON.stringify(o))<1e6)||g.collections.includes("logs")||g.collections.includes("log-method-latencies")||g.collections.includes("log-subscriptions")||g.collections.find(function(e){return e.endsWith(".versions")})||g.collections.find(function(e){return e.startsWith("monitor-")})?g.cacheId=0:this._nodeCache.set(g.cacheId,JSON.stringify(o)))}catch(e){Promise.all(g.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(g.cacheId),!((0,common_1.getBinarySize)(JSON.stringify(o))<1e6)||g.collections.includes("logs")||g.collections.includes("log-method-latencies")||g.collections.includes("log-subscriptions")||g.collections.find(function(e){return e.endsWith(".versions")})||g.collections.find(function(e){return e.startsWith("monitor-")})?g.cacheId=0:this._nodeCache.set(g.cacheId,JSON.stringify(o))}else if(Promise.all(g.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)||g.collections.includes("logs")||g.collections.includes("log-method-latencies")||g.collections.includes("log-subscriptions")||g.collections.find(function(e){return e.endsWith(".versions")})||g.collections.find(function(e){return e.startsWith("monitor-")}))g.cacheId=0;else if(g.cacheId=this._cacheId++,this._nodeCache.set(g.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 - "+g.publication+" - Deleted: "+s+" - "+i)}return[3,5];case 4:return l=e.sent(),this._monitorManagerFunction.finishMonitorFunction(t),Promise.all(g.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 "+g.publication+" - (sendPubData)\n\nData \n"+JSON.stringify(g.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;
2
2
  //# sourceMappingURL=subscription.manager.js.map