@resolveio/server-lib 20.4.83 → 20.5.1

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 (37) hide show
  1. package/fixtures/init.d.ts +1 -1
  2. package/fixtures/init.js +1 -1
  3. package/fixtures/init.js.map +1 -1
  4. package/index.d.ts +8 -0
  5. package/index.js +1 -1
  6. package/index.js.map +1 -1
  7. package/managers/cron.manager.d.ts +1 -2
  8. package/managers/cron.manager.js +1 -1
  9. package/managers/cron.manager.js.map +1 -1
  10. package/managers/local-log.manager.d.ts +16 -0
  11. package/managers/local-log.manager.js +2 -0
  12. package/managers/local-log.manager.js.map +1 -0
  13. package/managers/method.manager.d.ts +2 -8
  14. package/managers/method.manager.js +1 -1
  15. package/managers/method.manager.js.map +1 -1
  16. package/managers/mongo.manager.d.ts +1 -2
  17. package/managers/mongo.manager.js +1 -1
  18. package/managers/mongo.manager.js.map +1 -1
  19. package/managers/monitor.manager.d.ts +2 -3
  20. package/managers/monitor.manager.js +1 -1
  21. package/managers/monitor.manager.js.map +1 -1
  22. package/managers/subscription.manager.d.ts +1 -2
  23. package/managers/subscription.manager.js +1 -1
  24. package/managers/subscription.manager.js.map +1 -1
  25. package/managers/websocket.manager.js.map +1 -1
  26. package/methods/logs.js +1 -1
  27. package/methods/logs.js.map +1 -1
  28. package/package.json +1 -1
  29. package/server-app.d.ts +1 -3
  30. package/server-app.js +1 -1
  31. package/server-app.js.map +1 -1
  32. package/collections/log-ts.collection.d.ts +0 -3
  33. package/collections/log-ts.collection.js +0 -2
  34. package/collections/log-ts.collection.js.map +0 -1
  35. package/managers/subscription.performance.d.ts +0 -1
  36. package/managers/subscription.performance.js +0 -2
  37. package/managers/subscription.performance.js.map +0 -1
@@ -1,9 +1,8 @@
1
1
  export declare class MonitorManager {
2
- private _mainServer;
3
- private _serverConfig;
4
2
  private _instanceHostname;
5
3
  private _monitorData;
6
- constructor(mainServer: any, serverConfig: any);
4
+ private _localLogManager;
5
+ constructor();
7
6
  cpuAverage(): {
8
7
  idle: number;
9
8
  total: 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:[]},u={next:verb(0),throw:verb(1),return:verb(2)};return"function"==typeof Symbol&&(u[Symbol.iterator]=function(){return this}),u;function verb(o){return function(t){var e=[o,t];if(r)throw new TypeError("Generator is already executing.");for(;c=u&&e[u=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"),os=require("os"),MAX_BATCH_SIZE=8388608,MonitorManager=function(){function MonitorManager(t,e){this._instanceHostname="",this._monitorData=[],this._mainServer=t,this._serverConfig=e,this._instanceHostname=os.hostname().replace(/\./g,"-"),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"===this._serverConfig.ROOT_URL||"http://localhost:4200"===this._serverConfig.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"===i._serverConfig.ROOT_URL||"http://localhost:4200"===i._serverConfig.ROOT_URL?monitor_cpu_collection_1.MonitorCPUs.create({_id:(0,mongo_manager_1.objectIdHexString)(),metadata:{instance:i._instanceHostname,client:i._serverConfig.CLIENT_NAME},date:t,app:o,system:os.loadavg()[0]}):i.pushMonitorData({metadata:{instance:i._instanceHostname,client:i._serverConfig.CLIENT_NAME},date:t,app:o,system:os.loadavg()[0],type:"cpu"}),n=e,process.memoryUsage());"https://resolveio.com"===i._serverConfig.ROOT_URL||"http://localhost:4200"===i._serverConfig.ROOT_URL?monitor_memory_collection_1.MonitorMemorys.create({_id:(0,mongo_manager_1.objectIdHexString)(),metadata:{instance:i._instanceHostname,client:i._serverConfig.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:i._serverConfig.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"===this._serverConfig.ROOT_URL||"http://localhost:4200"===this._serverConfig.ROOT_URL?t.collection.startsWith("monitor-")||monitor_mongo_collection_1.MonitorMongos.create({_id:(0,mongo_manager_1.objectIdHexString)(),metadata:{instance:this._instanceHostname,client:this._serverConfig.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&&"https://airdropxrp.io"!==this._serverConfig.ROOT_URL&&"https://xrptradereport.com"!==this._serverConfig.ROOT_URL&&this.pushMonitorData({metadata:{instance:this._instanceHostname,client:this._serverConfig.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(){return __awaiter(this,void 0,void 0,function(){var e,o,n,i,r;return __generator(this,function(t){switch(t.label){case 0:for(e=[],o=0;0<this._monitorData.length&&o<MAX_BATCH_SIZE;){if(n=this._monitorData.shift(),i=Buffer.byteLength(JSON.stringify(n)),MAX_BATCH_SIZE<o+i&&0<e.length){this._monitorData.unshift(n);break}e.push(n),o+=i}t.label=1;case 1:return t.trys.push([1,3,,4]),[4,axios_1.default.post("https://backend.resolveio.com/api/monitor",e)];case 2:return t.sent(),[3,4];case 3:return r=t.sent(),console.error(new Date,"MonitorManager","Failed to flush monitor data:",r),[3,4];case 4:return[2]}})})},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(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;
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","os","MAX_BATCH_SIZE","MonitorManager","mainServer","serverConfig","this","_instanceHostname","_monitorData","_mainServer","_serverConfig","hostname","replace","setupIntervals","setupEventLoop","prototype","cpuAverage","totalIdle","totalTick","cpus","i","len","length","type","cpu","times","idle","total","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","console","error","error_1","MonitorMongo","exports","_startTime","_method","_collection","_query","finish","endTime","mongoMonitor","ResolveIOServer","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,EACMM,GAAKN,QAAQ,IAAI,EACjBO,eAAiB,QAEvBC,eAAA,WAMC,SAAAA,eAAYC,EAAYC,GAHhBC,KAAAC,kBAAoB,GACpBD,KAAAE,aAAe,GAGtBF,KAAKG,YAAcL,EACnBE,KAAKI,cAAgBL,EACrBC,KAAKC,kBAAoBN,GAAGU,SAAQ,EAAGC,QAAQ,MAAO,GAAG,EAEzDN,KAAKO,eAAc,EACnBP,KAAKQ,eAAc,CACpB,CAsND,OAnNCX,eAAAY,UAAAC,WAAA,WAMC,IAJA,IAAIC,EAAY,EAAGC,EAAY,EAC3BC,EAAOlB,GAAGkB,KAAI,E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G,EACbhC,KAAKuI,SAAW,IAAI7F,KAEpB1C,KAAKoI,OAASzG,YAAY,WACzB,IAAIiH,EAAOlG,KAAKD,IAAG,EAAKZ,EAAK0G,SAASM,QAAO,EACzCD,EAAO/G,EAAKwG,WACfxG,EAAKwG,SAAWO,GAGjB/G,EAAK0G,SAAW,IAAI7F,IACrB,EAAG,GAAI,CACR,CAcD,OAZQ6E,gBAAA9G,UAAA8F,OAAP,WACKvG,KAAKoI,SACRU,cAAc9I,KAAKoI,MAAM,EACzBpI,KAAKoI,OAAS,MAGfpI,KAAKwG,QAAU,IAAI9D,KAEf1C,KAAKqI,QAGV,EACDd,eAAA,EAAC,GA7CYrB,QAAAqB,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';\nconst os = require('os');\nconst MAX_BATCH_SIZE = 8 * 1024 * 1024; // 8MB max batch size for safety\n\nexport class MonitorManager {\n\tprivate _mainServer: ResolveIOMainServer;\n\tprivate _serverConfig;\n\tprivate _instanceHostname = '';\n\tprivate _monitorData = [];\n\n\tconstructor(mainServer, serverConfig) {\n\t\tthis._mainServer = mainServer;\n\t\tthis._serverConfig = serverConfig;\n\t\tthis._instanceHostname = os.hostname().replace(/\\./g, '-');\n\n\t\tthis.setupIntervals();\n\t\tthis.setupEventLoop();\n\t}\n\n\t//Create function to get CPU information\n\tcpuAverage() {\n\t\t//Initialise sum of idle and time of cores and fetch CPU info\n\t\tvar totalIdle = 0, totalTick = 0;\n\t\tvar cpus = os.cpus();\n\t\n\t\t//Loop through CPU cores\n\t\tfor(var i = 0, len = cpus.length; i < len; i++) {\n\t\t\t//Select CPU core\n\t\t\tvar cpu = cpus[i];\n\t\t\n\t\t\t//Total up the time in the cores tick\n\t\t\tfor(let type in cpu.times) {\n\t\t\t\ttotalTick += cpu.times[type];\n\t\t\t}\n\t\t\n\t\t\t//Total up the idle time of the core\n\t\t\ttotalIdle += cpu.times.idle;\n\t\t}\n\t\n\t\t//Return the average Idle and Tick times\n\t\treturn {idle: totalIdle / cpus.length, total: totalTick / cpus.length};\n\t}\n\n\tasync setupIntervals() {\n\t\tif (this._serverConfig['ROOT_URL'] !== 'https://resolveio.com' && this._serverConfig['ROOT_URL'] !== 'http://localhost:4200') {\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' ? (<any>process.env.APP_VERSION).toString() : 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\t\t\n\t\tsetInterval(() => {\n\t\t\tlet now = new Date();\n\n\t\t\t//Grab second Measure\n\t\t\tvar endMeasure = this.cpuAverage(); \n\n\t\t\t//Calculate the difference in idle and total time between the measures\n\t\t\tvar idleDifference = endMeasure.idle - lastCPU.idle;\n\t\t\tvar totalDifference = endMeasure.total - lastCPU.total;\n\n\t\t\t//Calculate the average percentage CPU usage\n\t\t\tvar percentageCPU = 1 - idleDifference / totalDifference;\n\t\t\t\n\t\t\tif (this._serverConfig['ROOT_URL'] === 'https://resolveio.com' || this._serverConfig['ROOT_URL'] === 'http://localhost:4200') {\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: this._serverConfig['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: this._serverConfig['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 (this._serverConfig['ROOT_URL'] === 'https://resolveio.com' || this._serverConfig['ROOT_URL'] === 'http://localhost:4200') {\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: this._serverConfig['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: this._serverConfig['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 (this._serverConfig['ROOT_URL'] === 'https://resolveio.com' || this._serverConfig['ROOT_URL'] === 'http://localhost:4200') {\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: this._serverConfig['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 ? data.query : '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\tif (this._serverConfig['ROOT_URL'] !== 'https://airdropxrp.io' && this._serverConfig['ROOT_URL'] !== 'https://xrptradereport.com') {\n\t\t\t\t\tthis.pushMonitorData({\n\t\t\t\t\t\tmetadata: {\n\t\t\t\t\t\t\tinstance: this._instanceHostname,\n\t\t\t\t\t\t\tclient: this._serverConfig['CLIENT_NAME']\n\t\t\t\t\t\t},\n\t\t\t\t\t\tdate: new Date(data.date),\n\t\t\t\t\t\tmethod: data.method,\n\t\t\t\t\t\tdoc_collection: data.collection,\n\t\t\t\t\t\tduration: data.duration,\n\t\t\t\t\t\tquery: Buffer.byteLength(data.query, 'utf8') < 15800000 ? data.query : 'Too Big',\n\t\t\t\t\t\ttype: 'mongo'\n\t\t\t\t\t});\n\t\t\t\t}\n\t\t\t}\n\t\t}\t\n\t}\n\n\tprivate pushMonitorData(data: any) {\n\t\tthis._monitorData.push(data);\n\n\t\t// If data exceeds safe limit, flush immediately\n\t\tif (this.estimateBatchSize() >= MAX_BATCH_SIZE) {\n\t\t\tthis.flushMonitorData();\n\t\t}\n\t}\n\n\t// Estimate total batch size for current _monitorData\n\tprivate estimateBatchSize(): number {\n\t\treturn this._monitorData.reduce((total, item) => total + Buffer.byteLength(JSON.stringify(item)), 0);\n\t}\n\n\t// Method to periodically flush accumulated monitor data to main server\n\tsetupEventLoop() {\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); // Adjust interval as needed\n\t}\n\n\t// Flush data to the main server\n\tprivate async flushMonitorData() {\n\t\tconst batch = [];\n\n\t\tlet batchSize = 0;\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\t// Prevent adding a single oversized item that exceeds batch size\n\t\t\tif (batchSize + itemSize > MAX_BATCH_SIZE && batch.length > 0) {\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\tawait axios.post('https://backend.resolveio.com/api/monitor', batch);\n\t\t\t// if (this.getEnableDebug()) {\n\t\t\t// \tconsole.log(new Date(), 'MonitorManager', `Flushed ${batch.length} items to main server.`);\n\t\t\t// }\n\t\t} catch (error) {\n\t\t\tconsole.error(new Date(), 'MonitorManager', 'Failed to flush monitor data:', error);\n\t\t\t// Optional: retry logic or logging for persistent errors\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: {\n\t\t[id_socket: string]: MonitorFunction;\n\t} = {};\n\tprivate _functionLastCompleted: MonitorFunction;\n\n\tconstructor() {}\n\n\tstartMonitorFunction(functionType: MonitorFunctionType, functionName: string, user: string, id_socket: string, data: any) {\n\t\tlet newMonitorFunction = new MonitorFunction(this._functionCnt++, functionType, functionName, user, id_socket, data);\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\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(this._functions.map(a => a.id).indexOf(id), 1);\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\t\n\tgetLastCompletedMonitorFunctionWS() {\n\t\treturn this._functionLastCompletedWS;\n\t}\n}\n\nexport type MonitorFunctionType = 'Cron Method' | 'Internal Method' | 'Method' | 'User Specific Publication' | 'Publication';\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(id: number, functionType: MonitorFunctionType, functionName: string, user: string, id_socket: string, data: any) {\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\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// ResolveIOServer.getMainServer().getMethodManager().sendEmail('dev@resolveio.com', ResolveIOServer.getMainServer().getServerConfig()['CLIENT_NAME'] + ' - Slow ' + this._functionType + ' Detected', '', (this._user ? 'User: ' + this._user + '\\n' : '') + this._functionType + ': ' + this._functionName + '\\n' + 'During this function the event loop did not run for ' + this._maxDiff + ' ms (' + this._maxDiff / 1000 + ' sec)' + (this._data ? '\\n\\nData: ' + JSON.stringify(this._data, null, 2) : ''));\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","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,KAAK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AZQA,KAAAqI,OAAyB,KACzBrI,KAAAsI,SAAW,EAKZtI,KAAAuI,UAAkB,KAClBvI,KAAA0G,QAAgB,KAChB1G,KAAAwI,SAAiB,KACjBxI,KAAAsH,UAAY,GACZtH,KAAAyH,GAAK,EAUXzH,KAAKyH,GAAKA,EACVzH,KAAKuI,UAAY,IAAI3F,KACrB5C,KAAKyI,cAAgBtB,EACrBnH,KAAK0I,cAAgBtB,EACrBpH,KAAK2I,MAAQtB,EACbrH,KAAKsH,UAAYA,EACjBtH,KAAK4I,MAAQ1G,EACblC,KAAKwI,SAAW,IAAI5F,KAEpB5C,KAAKqI,OAASxG,YAAY,WACzB,IAAIgH,EAAOjG,KAAKD,IAAG,EAAKZ,EAAKyG,SAASM,QAAO,EAEzCD,EAAO9G,EAAKuG,WACfvG,EAAKuG,SAAWO,GAGjB9G,EAAKyG,SAAW,IAAI5F,IACrB,EAAG,GAAI,CACR,CAcD,OAZQ4E,gBAAA/G,UAAAgG,OAAP,WACKzG,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}"]}
@@ -3,7 +3,6 @@ import * as WebSocket from 'ws';
3
3
  import { LoggedInUserModel } from '../models/logged-in-users.model';
4
4
  import { MonitorManagerFunction } from './monitor.manager';
5
5
  export declare class SubscriptionManager {
6
- private _mainServer;
7
6
  private _websocketManager;
8
7
  private _publications;
9
8
  private _subscriptions;
@@ -33,7 +32,7 @@ export declare class SubscriptionManager {
33
32
  private latencyBuffer;
34
33
  private readonly LATENCY_UPDATE_INTERVAL;
35
34
  private readonly LATENCY_UPDATE_THRESHOLD_MS;
36
- constructor(mainServer: any, wss: WebSocket.Server, serverConfig: any, monitorManagerFunction: MonitorManagerFunction);
35
+ constructor(wss: WebSocket.Server, serverConfig: any, monitorManagerFunction: MonitorManagerFunction);
37
36
  private setCacheLimit;
38
37
  invalidatePubsCache(collection: string, type: string): Promise<void>;
39
38
  private delay;
@@ -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,i){var o=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._mainServer=e,this._websocketManager=this._mainServer.getWebSocketManager(),this._monitorManagerFunction=i,this._nodeCache=new NodeCache({stdTTL:0,checkperiod:0}),setInterval(function(){return o.flushThrottledLatencyUpdates()},this.LATENCY_UPDATE_INTERVAL),this.serverConfig=n,this._wss=t,(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(){o._oplogRetryCount=0},15e3),setInterval(function(){o.getEnableDebug()&&(console.log(new Date,"Sub Manager","Subs",o._subscriptions.length),console.log(new Date,"Sub Manager","Logged In Users",o._loggedInUsers.length),console.log(new Date,"Sub Manager","Mongo Queue",o._mongoQueue.length),console.log(new Date,"Sub Manager","Mongo Queue Hits",o._debugMongoQueueHits),console.log(new Date,"Sub Manager","Mongo Queue Collections",JSON.stringify(o._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",o._debugOplogHits),console.log(new Date,"Sub Manager","Oplog Collections",JSON.stringify(o._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",o._debugSendQueueHits),console.log(new Date,"Sub Manager","Sub Hits",o._debugSubHits),console.log(new Date,"Sub Manager","Sub Collections",JSON.stringify(o._debugSubCollections.sort(function(e,t){return e.publication.localeCompare(t.publication)}),null,2)),console.log(new Date,"Sub Manager","Unsub Hits",o._debugUnSubHits),console.log(new Date,"Sub Manager","Unsub All Hits",o._debugUnSubAllHits),console.log(new Date,"Sub Manager","Cache Cleanup Hits",o._debugRemoveCacheHits)),o._debugOplogHits=0,o._debugOplogCollections=[],o._debugSubCollections=[],o._debugMongoQueueHits=0,o._debugMongoQueueCollections=[],o._debugSendQueueHits=0,o._debugSubHits=0,o._debugUnSubHits=0,o._debugUnSubAllHits=0,o._debugRemoveCacheHits=0},6e4),setInterval(function(){return __awaiter(o,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?o._enableDebug=!0:o._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){this._mainServer.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)&&(this._mainServer.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&&(i._mainServer.getMethodManager().callMethodInternal.call(i._mainServer.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]),this._mainServer.getMethodManager().callMethodInternal.call(this._mainServer.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),this._mainServer.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&&this._mainServer.getMethodManager().callMethodInternal.call(this._mainServer.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]})})})),this._mainServer.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,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;
2
2
  //# sourceMappingURL=subscription.manager.js.map