@resolveio/server-lib 20.5.13 → 20.5.15

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 (51) hide show
  1. package/collections/log-method-latency.collection.js +1 -1
  2. package/collections/log-method-latency.collection.js.map +1 -1
  3. package/collections/log-subscription.collection.js +1 -1
  4. package/collections/log-subscription.collection.js.map +1 -1
  5. package/collections/log.collection.js +1 -1
  6. package/collections/log.collection.js.map +1 -1
  7. package/collections/monitor-function.collection.d.ts +3 -0
  8. package/collections/monitor-function.collection.js +2 -0
  9. package/collections/monitor-function.collection.js.map +1 -0
  10. package/http/auth.js +1 -1
  11. package/http/auth.js.map +1 -1
  12. package/managers/local-log.manager.d.ts +1 -1
  13. package/managers/local-log.manager.js.map +1 -1
  14. package/managers/method.manager.d.ts +2 -4
  15. package/managers/method.manager.js +1 -1
  16. package/managers/method.manager.js.map +1 -1
  17. package/managers/mongo.manager.js +1 -1
  18. package/managers/mongo.manager.js.map +1 -1
  19. package/managers/monitor.manager.d.ts +7 -6
  20. package/managers/monitor.manager.js +1 -1
  21. package/managers/monitor.manager.js.map +1 -1
  22. package/managers/subscription.manager.js +1 -1
  23. package/managers/subscription.manager.js.map +1 -1
  24. package/methods/aws.js +1 -1
  25. package/methods/aws.js.map +1 -1
  26. package/methods/cron-jobs.js +1 -1
  27. package/methods/cron-jobs.js.map +1 -1
  28. package/methods/logs.js +1 -1
  29. package/methods/logs.js.map +1 -1
  30. package/methods/pdf.js +1 -1
  31. package/methods/pdf.js.map +1 -1
  32. package/methods.ts +0 -3
  33. package/models/log.model.d.ts +1 -1
  34. package/models/monitor-function.model.d.ts +14 -0
  35. package/models/{worker-task-request.model.js → monitor-function.model.js} +1 -1
  36. package/models/monitor-function.model.js.map +1 -0
  37. package/package.json +1 -1
  38. package/server-app.d.ts +33 -2
  39. package/server-app.js +1 -1
  40. package/server-app.js.map +1 -1
  41. package/collections/worker-task-request.collection.d.ts +0 -3
  42. package/collections/worker-task-request.collection.js +0 -2
  43. package/collections/worker-task-request.collection.js.map +0 -1
  44. package/collections/worker-task-response.collection.d.ts +0 -3
  45. package/collections/worker-task-response.collection.js +0 -2
  46. package/collections/worker-task-response.collection.js.map +0 -1
  47. package/models/worker-task-request.model.d.ts +0 -10
  48. package/models/worker-task-request.model.js.map +0 -1
  49. package/models/worker-task-response.model.d.ts +0 -9
  50. package/models/worker-task-response.model.js +0 -2
  51. package/models/worker-task-response.model.js.map +0 -1
@@ -8,6 +8,7 @@ export declare class MonitorManager {
8
8
  };
9
9
  setupIntervals(): Promise<void>;
10
10
  addMongoTracker(data: MongoMonitorModel): void;
11
+ addFunctionTracker(data: MonitorFunction): void;
11
12
  private pushMonitorData;
12
13
  private estimateBatchSize;
13
14
  setupEventLoop(): void;
@@ -42,14 +43,14 @@ export declare class MonitorManagerFunction {
42
43
  [id_socket: string]: MonitorFunction;
43
44
  };
44
45
  }
45
- export type MonitorFunctionType = 'Cron Method' | 'Internal Method' | 'Method' | 'User Specific Publication' | 'Publication';
46
+ export type MonitorFunctionType = 'Cron Method' | 'Method' | 'User Specific Publication' | 'Publication';
46
47
  export declare class MonitorFunction {
47
48
  private _timer;
48
- private _maxDiff;
49
- private _functionType;
50
- private _functionName;
51
- private _user;
52
- private _data;
49
+ duration: number;
50
+ functionType: MonitorFunctionType;
51
+ functionName: string;
52
+ user: string;
53
+ data: any;
53
54
  startTime: Date;
54
55
  endTime: Date;
55
56
  lastTime: Date;
@@ -1,2 +1,2 @@
1
- "use strict";var __awaiter=this&&this.__awaiter||function(e,n,r,i){return new(r=r||Promise)(function(o,t){function fulfilled(e){try{step(i.next(e))}catch(e){t(e)}}function rejected(e){try{step(i.throw(e))}catch(e){t(e)}}function step(e){var t;e.done?o(e.value):((t=e.value)instanceof r?t:new r(function(e){e(t)})).then(fulfilled,rejected)}step((i=i.apply(e,n||[])).next())})},__generator=this&&this.__generator||function(n,r){var i,a,s,c={label:0,sent:function(){if(1&s[0])throw s[1];return s[1]},trys:[],ops:[]},l={next:verb(0),throw:verb(1),return:verb(2)};return"function"==typeof Symbol&&(l[Symbol.iterator]=function(){return this}),l;function verb(o){return function(e){var t=[o,e];if(i)throw new TypeError("Generator is already executing.");for(;c=l&&t[l=0]?0:c;)try{if(i=1,a&&(s=2&t[0]?a.return:t[0]?a.throw||((s=a.return)&&s.call(a),0):a.next)&&!(s=s.call(a,t[1])).done)return s;switch(a=0,(t=s?[2&t[0],s.value]:t)[0]){case 0:case 1:s=t;break;case 4:return c.label++,{value:t[1],done:!1};case 5:c.label++,a=t[1],t=[0];continue;case 7:t=c.ops.pop(),c.trys.pop();continue;default:if(!(s=0<(s=c.trys).length&&s[s.length-1])&&(6===t[0]||2===t[0])){c=0;continue}if(3===t[0]&&(!s||t[1]>s[0]&&t[1]<s[3]))c.label=t[1];else if(6===t[0]&&c.label<s[1])c.label=s[1],s=t;else{if(!(s&&c.label<s[2])){s[2]&&c.ops.pop(),c.trys.pop();continue}c.label=s[2],c.ops.push(t)}}t=r.call(n,c)}catch(e){t=[6,e],a=0}finally{i=s=0}if(5&t[0])throw t[1];return{value:t[0]?t[1]:void 0,done:!0}}}},axios_1=(Object.defineProperty(exports,"__esModule",{value:!0}),exports.MonitorFunction=exports.MonitorManagerFunction=exports.MonitorMongo=exports.MonitorManager=void 0,require("axios")),monitor_cpu_collection_1=require("../collections/monitor-cpu.collection"),monitor_memory_collection_1=require("../collections/monitor-memory.collection"),monitor_mongo_collection_1=require("../collections/monitor-mongo.collection"),resolveio_server_app_1=require("../resolveio-server-app"),common_1=require("../util/common"),os=require("os"),MAX_BATCH_SIZE=8388608,MonitorManager=function(){function MonitorManager(){this._instanceHostname="",this._monitorData=[],this._instanceHostname=os.hostname().replace(/\./g,"-"),this.setupIntervals(),this.setupEventLoop()}return MonitorManager.prototype.cpuAverage=function(){for(var e=0,t=0,o=os.cpus(),n=0,r=o.length;n<r;n++){var i,a=o[n];for(i in a.times)t+=a.times[i];e+=a.times.idle}return{idle:e/o.length,total:t/o.length}},MonitorManager.prototype.setupIntervals=function(){return __awaiter(this,void 0,void 0,function(){var t,n,r=this;return __generator(this,function(e){switch(e.label){case 0:if("https://resolveio.com"===resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL||"http://localhost:4200"===resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL)return[3,4];e.label=1;case 1:return e.trys.push([1,3,,4]),[4,axios_1.default.get("http://169.254.169.254/latest/meta-data/instance-id")];case 2:return t=e.sent(),setInterval(function(){return __awaiter(r,void 0,void 0,function(){return __generator(this,function(e){switch(e.label){case 0:return e.trys.push([0,2,,3]),[4,axios_1.default.post("https://backend.resolveio.com/api/health",{type:"application",id_aws_instance:t.data,version:"number"==typeof process.env.APP_VERSION?process.env.APP_VERSION.toString():process.env.APP_VERSION,version_key:process.env.APP_VERSION_KEY})];case 1:return e.sent(),[3,3];case 2:return e.sent(),[3,3];case 3:return[2]}})})},1e4),[3,4];case 3:return e.sent(),[3,4];case 4:return n=this.cpuAverage(),setInterval(function(){var e=new Date,t=r.cpuAverage(),o=1-(t.idle-n.idle)/(t.total-n.total),o=("https://resolveio.com"===resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL||"http://localhost:4200"===resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL?monitor_cpu_collection_1.MonitorCPUs.create({_id:(0,common_1.objectIdHexString)(),metadata:{instance:r._instanceHostname,client:resolveio_server_app_1.ResolveIOServer.getServerConfig().CLIENT_NAME},date:e,app:o,system:os.loadavg()[0]}):r.pushMonitorData({type:"monitor-cpu",data:{metadata:{instance:r._instanceHostname,client:resolveio_server_app_1.ResolveIOServer.getServerConfig().CLIENT_NAME},date:e,app:o,system:os.loadavg()[0]}}),n=t,process.memoryUsage());"https://resolveio.com"===resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL||"http://localhost:4200"===resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL?monitor_memory_collection_1.MonitorMemorys.create({_id:(0,common_1.objectIdHexString)(),metadata:{instance:r._instanceHostname,client:resolveio_server_app_1.ResolveIOServer.getServerConfig().CLIENT_NAME},date:e,physical_total:o.heapTotal,physical_used:o.heapUsed,physical_free:os.freemem(),virtual:o.rss,private:o.external,physical:os.totalmem()}):r.pushMonitorData({type:"monitor-memory",data:{metadata:{instance:r._instanceHostname,client:resolveio_server_app_1.ResolveIOServer.getServerConfig().CLIENT_NAME},date:e,physical_total:o.heapTotal,physical_used:o.heapUsed,physical_free:os.freemem(),virtual:o.rss,private:o.external,physical:os.totalmem(),type:"memory"}})},3e3),[2]}})})},MonitorManager.prototype.addMongoTracker=function(e){"https://resolveio.com"===resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL||"http://localhost:4200"===resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL?e.collection.startsWith("monitor-")||monitor_mongo_collection_1.MonitorMongos.create({_id:(0,common_1.objectIdHexString)(),metadata:{instance:this._instanceHostname,client:resolveio_server_app_1.ResolveIOServer.getServerConfig().CLIENT_NAME},date:new Date(e.date),method:e.method,doc_collection:e.collection,duration:e.duration,query:Buffer.byteLength(e.query,"utf8")<158e5?e.query:"Too Big"}):this._instanceHostname&&this.pushMonitorData({type:"monitor-mongo",data:{metadata:{instance:this._instanceHostname,client:resolveio_server_app_1.ResolveIOServer.getServerConfig().CLIENT_NAME},date:new Date(e.date),method:e.method,doc_collection:e.collection,duration:e.duration,query:Buffer.byteLength(e.query,"utf8")<158e5?e.query:"Too Big"}})},MonitorManager.prototype.pushMonitorData=function(e){this._monitorData.push(e),this.estimateBatchSize()>=MAX_BATCH_SIZE&&this.flushMonitorData()},MonitorManager.prototype.estimateBatchSize=function(){return this._monitorData.reduce(function(e,t){return e+Buffer.byteLength(JSON.stringify(t))},0)},MonitorManager.prototype.setupEventLoop=function(){var e=this;setInterval(function(){0<e._monitorData.length&&e.flushMonitorData()},2500)},MonitorManager.prototype.flushMonitorData=function(){for(var e=[],t=0;0<this._monitorData.length&&t<MAX_BATCH_SIZE;){var o=this._monitorData.shift(),n=Buffer.byteLength(JSON.stringify(o));if(MAX_BATCH_SIZE<t+n&&0<e.length){this._monitorData.unshift(o);break}e.push(o),t+=n}try{resolveio_server_app_1.ResolveIOServer.getLocalLogManager().writeLogs(e)}catch(e){console.error(new Date,"MonitorManager","Failed to flush monitor data to file:",e)}},MonitorManager}(),MonitorMongo=(exports.MonitorManager=MonitorManager,function(){function MonitorMongo(e,t,o){this._startTime=0,this._method="",this._collection="",this._query="",this._startTime=Date.now(),this._method=e,this._collection=t,this._query=o}return MonitorMongo.prototype.finish=function(){var e=Date.now(),e={date:new Date(e),method:this._method,collection:this._collection,query:this._query,duration:e-this._startTime};resolveio_server_app_1.ResolveIOServer.getMainServer().getMonitorManager().addMongoTracker(e)},MonitorMongo}()),MonitorManagerFunction=(exports.MonitorMongo=MonitorMongo,function(){function MonitorManagerFunction(){this._functionCnt=0,this._functions=[],this._functionLastCompletedWS={}}return MonitorManagerFunction.prototype.startMonitorFunction=function(e,t,o,n,r){e=new MonitorFunction(this._functionCnt++,e,t,o,n,r);return this._functions.push(e),e.id},MonitorManagerFunction.prototype.finishMonitorFunction=function(t){var e=this._functions.find(function(e){return e.id===t});e&&(e.finish(),this._functionLastCompleted=e,this._functionLastCompletedWS[e.id_socket]=e,this._functions.splice(this._functions.map(function(e){return e.id}).indexOf(t),1))},MonitorManagerFunction.prototype.getActiveMonitorFunctions=function(){return this._functions},MonitorManagerFunction.prototype.getLastCompletedMonitorFunction=function(){return this._functionLastCompleted},MonitorManagerFunction.prototype.getLastCompletedMonitorFunctionWS=function(){return this._functionLastCompletedWS},MonitorManagerFunction}()),MonitorFunction=(exports.MonitorManagerFunction=MonitorManagerFunction,function(){function MonitorFunction(e,t,o,n,r,i){var a=this;this._timer=null,this._maxDiff=0,this.startTime=null,this.endTime=null,this.lastTime=null,this.id_socket="",this.id=0,this.id=e,this.startTime=new Date,this._functionType=t,this._functionName=o,this._user=n,this.id_socket=r,this._data=i,this.lastTime=new Date,this._timer=setInterval(function(){var e=Date.now()-a.lastTime.getTime();e>a._maxDiff&&(a._maxDiff=e),a.lastTime=new Date},1e3)}return MonitorFunction.prototype.finish=function(){this._timer&&(clearInterval(this._timer),this._timer=null),this.endTime=new Date,this._maxDiff},MonitorFunction}());exports.MonitorFunction=MonitorFunction;
1
+ "use strict";var __awaiter=this&&this.__awaiter||function(e,n,r,i){return new(r=r||Promise)(function(o,t){function fulfilled(e){try{step(i.next(e))}catch(e){t(e)}}function rejected(e){try{step(i.throw(e))}catch(e){t(e)}}function step(e){var t;e.done?o(e.value):((t=e.value)instanceof r?t:new r(function(e){e(t)})).then(fulfilled,rejected)}step((i=i.apply(e,n||[])).next())})},__generator=this&&this.__generator||function(n,r){var i,a,s,c={label:0,sent:function(){if(1&s[0])throw s[1];return s[1]},trys:[],ops:[]},l={next:verb(0),throw:verb(1),return:verb(2)};return"function"==typeof Symbol&&(l[Symbol.iterator]=function(){return this}),l;function verb(o){return function(e){var t=[o,e];if(i)throw new TypeError("Generator is already executing.");for(;c=l&&t[l=0]?0:c;)try{if(i=1,a&&(s=2&t[0]?a.return:t[0]?a.throw||((s=a.return)&&s.call(a),0):a.next)&&!(s=s.call(a,t[1])).done)return s;switch(a=0,(t=s?[2&t[0],s.value]:t)[0]){case 0:case 1:s=t;break;case 4:return c.label++,{value:t[1],done:!1};case 5:c.label++,a=t[1],t=[0];continue;case 7:t=c.ops.pop(),c.trys.pop();continue;default:if(!(s=0<(s=c.trys).length&&s[s.length-1])&&(6===t[0]||2===t[0])){c=0;continue}if(3===t[0]&&(!s||t[1]>s[0]&&t[1]<s[3]))c.label=t[1];else if(6===t[0]&&c.label<s[1])c.label=s[1],s=t;else{if(!(s&&c.label<s[2])){s[2]&&c.ops.pop(),c.trys.pop();continue}c.label=s[2],c.ops.push(t)}}t=r.call(n,c)}catch(e){t=[6,e],a=0}finally{i=s=0}if(5&t[0])throw t[1];return{value:t[0]?t[1]:void 0,done:!0}}}},axios_1=(Object.defineProperty(exports,"__esModule",{value:!0}),exports.MonitorFunction=exports.MonitorManagerFunction=exports.MonitorMongo=exports.MonitorManager=void 0,require("axios")),monitor_cpu_collection_1=require("../collections/monitor-cpu.collection"),monitor_memory_collection_1=require("../collections/monitor-memory.collection"),monitor_mongo_collection_1=require("../collections/monitor-mongo.collection"),resolveio_server_app_1=require("../resolveio-server-app"),common_1=require("../util/common"),monitor_function_collection_1=require("../collections/monitor-function.collection"),os=require("os"),MAX_BATCH_SIZE=8388608,MonitorManager=function(){function MonitorManager(){this._instanceHostname="",this._monitorData=[],this._instanceHostname=os.hostname().replace(/\./g,"-"),this.setupIntervals(),this.setupEventLoop()}return MonitorManager.prototype.cpuAverage=function(){for(var e=0,t=0,o=os.cpus(),n=0,r=o.length;n<r;n++){var i,a=o[n];for(i in a.times)t+=a.times[i];e+=a.times.idle}return{idle:e/o.length,total:t/o.length}},MonitorManager.prototype.setupIntervals=function(){return __awaiter(this,void 0,void 0,function(){var t,n,r=this;return __generator(this,function(e){switch(e.label){case 0:if("https://resolveio.com"===resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL||"http://localhost:4200"===resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL)return[3,4];e.label=1;case 1:return e.trys.push([1,3,,4]),[4,axios_1.default.get("http://169.254.169.254/latest/meta-data/instance-id")];case 2:return t=e.sent(),setInterval(function(){return __awaiter(r,void 0,void 0,function(){return __generator(this,function(e){switch(e.label){case 0:return e.trys.push([0,2,,3]),[4,axios_1.default.post("https://backend.resolveio.com/api/health",{type:"application",id_aws_instance:t.data,version:"number"==typeof process.env.APP_VERSION?process.env.APP_VERSION.toString():process.env.APP_VERSION,version_key:process.env.APP_VERSION_KEY})];case 1:return e.sent(),[3,3];case 2:return e.sent(),[3,3];case 3:return[2]}})})},1e4),[3,4];case 3:return e.sent(),[3,4];case 4:return n=this.cpuAverage(),setInterval(function(){var e=new Date,t=r.cpuAverage(),o=1-(t.idle-n.idle)/(t.total-n.total),o=("https://resolveio.com"===resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL||"http://localhost:4200"===resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL?monitor_cpu_collection_1.MonitorCPUs.create({_id:(0,common_1.objectIdHexString)(),metadata:{instance:r._instanceHostname,client:resolveio_server_app_1.ResolveIOServer.getServerConfig().CLIENT_NAME},date:e,app:o,system:os.loadavg()[0]}):r.pushMonitorData({type:"monitor-cpu",data:{metadata:{instance:r._instanceHostname,client:resolveio_server_app_1.ResolveIOServer.getServerConfig().CLIENT_NAME},date:e,app:o,system:os.loadavg()[0]}}),n=t,process.memoryUsage());"https://resolveio.com"===resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL||"http://localhost:4200"===resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL?monitor_memory_collection_1.MonitorMemorys.create({_id:(0,common_1.objectIdHexString)(),metadata:{instance:r._instanceHostname,client:resolveio_server_app_1.ResolveIOServer.getServerConfig().CLIENT_NAME},date:e,physical_total:o.heapTotal,physical_used:o.heapUsed,physical_free:os.freemem(),virtual:o.rss,private:o.external,physical:os.totalmem()}):r.pushMonitorData({type:"monitor-memory",data:{metadata:{instance:r._instanceHostname,client:resolveio_server_app_1.ResolveIOServer.getServerConfig().CLIENT_NAME},date:e,physical_total:o.heapTotal,physical_used:o.heapUsed,physical_free:os.freemem(),virtual:o.rss,private:o.external,physical:os.totalmem(),type:"memory"}})},3e3),[2]}})})},MonitorManager.prototype.addMongoTracker=function(e){"https://resolveio.com"===resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL||"http://localhost:4200"===resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL?e.collection.startsWith("monitor-")||monitor_mongo_collection_1.MonitorMongos.create({_id:(0,common_1.objectIdHexString)(),metadata:{instance:this._instanceHostname,client:resolveio_server_app_1.ResolveIOServer.getServerConfig().CLIENT_NAME},date:new Date(e.date),method:e.method,doc_collection:e.collection,duration:e.duration,query:Buffer.byteLength(e.query,"utf8")<158e5?e.query:"Too Big"}):this._instanceHostname&&this.pushMonitorData({type:"monitor-mongo",data:{metadata:{instance:this._instanceHostname,client:resolveio_server_app_1.ResolveIOServer.getServerConfig().CLIENT_NAME},date:new Date(e.date),method:e.method,doc_collection:e.collection,duration:e.duration,query:Buffer.byteLength(e.query,"utf8")<158e5?e.query:"Too Big"}})},MonitorManager.prototype.addFunctionTracker=function(e){"https://resolveio.com"===resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL||"http://localhost:4200"===resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL?monitor_function_collection_1.MonitorFunctions.create({_id:(0,common_1.objectIdHexString)(),metadata:{instance:this._instanceHostname,client:resolveio_server_app_1.ResolveIOServer.getServerConfig().CLIENT_NAME},date:new Date(e.startTime),type:e.functionType,name:e.functionName,user:e.user,duration:e.duration,data:Buffer.byteLength(e.data,"utf8")<158e5?e.data:"Too Big"}):this._instanceHostname&&this.pushMonitorData({type:"monitor-function",data:{metadata:{instance:this._instanceHostname,client:resolveio_server_app_1.ResolveIOServer.getServerConfig().CLIENT_NAME},date:new Date(e.startTime),type:e.functionType,name:e.functionName,user:e.user,duration:e.duration,data:Buffer.byteLength(e.data,"utf8")<158e5?e.data:"Too Big"}})},MonitorManager.prototype.pushMonitorData=function(e){this._monitorData.push(e),this.estimateBatchSize()>=MAX_BATCH_SIZE&&this.flushMonitorData()},MonitorManager.prototype.estimateBatchSize=function(){return this._monitorData.reduce(function(e,t){return e+Buffer.byteLength(JSON.stringify(t))},0)},MonitorManager.prototype.setupEventLoop=function(){var e=this;setInterval(function(){0<e._monitorData.length&&e.flushMonitorData()},2500)},MonitorManager.prototype.flushMonitorData=function(){for(var e=[],t=0;0<this._monitorData.length&&t<MAX_BATCH_SIZE;){var o=this._monitorData.shift(),n=Buffer.byteLength(JSON.stringify(o));if(MAX_BATCH_SIZE<t+n&&0<e.length){this._monitorData.unshift(o);break}e.push(o),t+=n}try{resolveio_server_app_1.ResolveIOServer.getLocalLogManager().writeLogs(e)}catch(e){console.error(new Date,"MonitorManager","Failed to flush monitor data to file:",e)}},MonitorManager}(),MonitorMongo=(exports.MonitorManager=MonitorManager,function(){function MonitorMongo(e,t,o){this._startTime=0,this._method="",this._collection="",this._query="",this._startTime=Date.now(),this._method=e,this._collection=t,this._query=o}return MonitorMongo.prototype.finish=function(){var e=Date.now(),e={date:new Date(e),method:this._method,collection:this._collection,query:this._query,duration:e-this._startTime};resolveio_server_app_1.ResolveIOServer.getMainServer().getMonitorManager().addMongoTracker(e)},MonitorMongo}()),MonitorManagerFunction=(exports.MonitorMongo=MonitorMongo,function(){function MonitorManagerFunction(){this._functionCnt=0,this._functions=[],this._functionLastCompletedWS={}}return MonitorManagerFunction.prototype.startMonitorFunction=function(e,t,o,n,r){e=new MonitorFunction(this._functionCnt++,e,t,o,n,r);return this._functions.push(e),e.id},MonitorManagerFunction.prototype.finishMonitorFunction=function(t){var e=this._functions.find(function(e){return e.id===t});e&&(e.finish(),this._functionLastCompleted=e,this._functionLastCompletedWS[e.id_socket]=e,this._functions.splice(this._functions.map(function(e){return e.id}).indexOf(t),1))},MonitorManagerFunction.prototype.getActiveMonitorFunctions=function(){return this._functions},MonitorManagerFunction.prototype.getLastCompletedMonitorFunction=function(){return this._functionLastCompleted},MonitorManagerFunction.prototype.getLastCompletedMonitorFunctionWS=function(){return this._functionLastCompletedWS},MonitorManagerFunction}()),MonitorFunction=(exports.MonitorManagerFunction=MonitorManagerFunction,function(){function MonitorFunction(e,t,o,n,r,i){var a=this;this._timer=null,this.duration=0,this.startTime=null,this.endTime=null,this.lastTime=null,this.id_socket="",this.id=0,this.id=e,this.startTime=new Date,this.functionType=t,this.functionName=o,this.user=n,this.id_socket=r,this.data=i,this.lastTime=new Date,this._timer=setInterval(function(){var e=Date.now()-a.lastTime.getTime();e>a.duration&&(a.duration=e),a.lastTime=new Date},1e3)}return MonitorFunction.prototype.finish=function(){this._timer&&(clearInterval(this._timer),this._timer=null),this.endTime=new Date,this.duration,resolveio_server_app_1.ResolveIOServer.getMainServer().getMonitorManager().addFunctionTracker(this)},MonitorFunction}());exports.MonitorFunction=MonitorFunction;
2
2
  //# sourceMappingURL=monitor.manager.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/managers/monitor.manager.ts"],"names":["axios_1","require","monitor_cpu_collection_1","monitor_memory_collection_1","monitor_mongo_collection_1","resolveio_server_app_1","common_1","os","MAX_BATCH_SIZE","MonitorManager","this","_instanceHostname","_monitorData","hostname","replace","setupIntervals","setupEventLoop","prototype","cpuAverage","totalIdle","totalTick","cpus","i","len","length","type","cpu","times","idle","total","ResolveIOServer","getServerConfig","default","get","instanceId_1","_a","sent","setInterval","__awaiter","_this","post","id_aws_instance","data","version","process","env","APP_VERSION","toString","version_key","APP_VERSION_KEY","lastCPU","now","Date","endMeasure","percentageCPU","memUsage","MonitorCPUs","create","_id","objectIdHexString","metadata","instance","client","date","app","system","loadavg","pushMonitorData","memoryUsage","MonitorMemorys","physical_total","heapTotal","physical_used","heapUsed","physical_free","freemem","virtual","rss","private","external","physical","totalmem","addMongoTracker","collection","startsWith","MonitorMongos","method","doc_collection","duration","query","Buffer","byteLength","push","estimateBatchSize","flushMonitorData","reduce","item","JSON","stringify","batch","batchSize","shift","itemSize","unshift","getLocalLogManager","writeLogs","error","console","MonitorMongo","exports","_startTime","_method","_collection","_query","finish","endTime","mongoMonitor","getMainServer","getMonitorManager","MonitorManagerFunction","_functionCnt","_functions","_functionLastCompletedWS","startMonitorFunction","functionType","functionName","user","id_socket","newMonitorFunction","MonitorFunction","id","finishMonitorFunction","monitor","find","a","_functionLastCompleted","splice","map","indexOf","getActiveMonitorFunctions","getLastCompletedMonitorFunction","getLastCompletedMonitorFunctionWS","_timer","_maxDiff","startTime","lastTime","_functionType","_functionName","_user","_data","diff","getTime","clearInterval"],"mappings":"k8CAAAA,S,iKAAAC,QAAA,OAAA,GACAC,yBAAAD,QAAA,uCAAA,EACAE,4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AA,WACC,OAAOjI,KAAK+G,UACb,EAEAF,uBAAAtG,UAAA2H,gCAAA,WACC,OAAOlI,KAAK6H,sBACb,EAEAhB,uBAAAtG,UAAA4H,kCAAA,WACC,OAAOnI,KAAKgH,wBACb,EACDH,sBAAA,EAAC,GASDU,iBA9DapB,QAAAU,uBAAAA,uBA8Db,WAaC,SAAAU,gBACCC,EACAN,EACAC,EACAC,EACAC,EACArF,GAND,IAAAH,EAAA7B,KAZQA,KAAAoI,OAAyB,KACzBpI,KAAAqI,SAAW,EAKZrI,KAAAsI,UAAkB,KAClBtI,KAAAyG,QAAgB,KAChBzG,KAAAuI,SAAiB,KACjBvI,KAAAqH,UAAY,GACZrH,KAAAwH,GAAK,EAUXxH,KAAKwH,GAAKA,EACVxH,KAAKsI,UAAY,IAAI5F,KACrB1C,KAAKwI,cAAgBtB,EACrBlH,KAAKyI,cAAgBtB,EACrBnH,KAAK0I,MAAQtB,EACbpH,KAAKqH,UAAYA,EACjBrH,KAAK2I,MAAQ3G,EACbhC,KAAKuI,SAAW,IAAI7F,KAEpB1C,KAAKoI,OAASzG,YAAY,WACzB,IAAIiH,EAAOlG,KAAKD,IAAG,EAAKZ,EAAK0G,SAASM,QAAO,EAEzCD,EAAO/G,EAAKwG,WACfxG,EAAKwG,SAAWO,GAGjB/G,EAAK0G,SAAW,IAAI7F,IACrB,EAAG,GAAI,CACR,CAcD,OAZQ6E,gBAAAhH,UAAAiG,OAAP,WACKxG,KAAKoI,SACRU,cAAc9I,KAAKoI,MAAM,EACzBpI,KAAKoI,OAAS,MAGfpI,KAAKyG,QAAU,IAAI/D,KAEf1C,KAAKqI,QAGV,EACDd,eAAA,EAAC,GArDYpB,QAAAoB,gBAAAA","file":"monitor.manager.js","sourcesContent":["import axios from 'axios';\nimport { MonitorCPUs } from '../collections/monitor-cpu.collection';\nimport { MonitorMemorys } from '../collections/monitor-memory.collection';\nimport { MonitorMongos } from '../collections/monitor-mongo.collection';\nimport { ResolveIOServer } from '../resolveio-server-app';\nimport { objectIdHexString } from '../util/common';\nimport { LocalLogModel } from './local-log.manager';\n\nconst os = require('os');\nconst MAX_BATCH_SIZE = 8 * 1024 * 1024; // 8MB max for safety\n\nexport class MonitorManager {\n\tprivate _instanceHostname = '';\n\tprivate _monitorData: LocalLogModel[] = [];\n\n\tconstructor() {\n\t\tthis._instanceHostname = os.hostname().replace(/\\./g, '-');\n\n\t\tthis.setupIntervals();\n\t\tthis.setupEventLoop();\n\t}\n\n\tcpuAverage() {\n\t\tvar totalIdle = 0, totalTick = 0;\n\t\tvar cpus = os.cpus();\n\n\t\tfor (var i = 0, len = cpus.length; i < len; i++) {\n\t\t\tvar cpu = cpus[i];\n\n\t\t\tfor (let type in cpu.times) {\n\t\t\t\ttotalTick += cpu.times[type];\n\t\t\t}\n\t\t\ttotalIdle += cpu.times.idle;\n\t\t}\n\n\t\treturn {\n\t\t\tidle: totalIdle / cpus.length,\n\t\t\ttotal: totalTick / cpus.length\n\t\t};\n\t}\n\n\tasync setupIntervals() {\n\t\t// Example: If not on production or localhost, do a health check\n\t\tif (\n\t\t\tResolveIOServer.getServerConfig()['ROOT_URL'] !== 'https://resolveio.com'\n\t\t&& ResolveIOServer.getServerConfig()['ROOT_URL'] !== 'http://localhost:4200'\n\t\t) {\n\t\t\ttry {\n\t\t\t\tlet instanceId = await axios.get('http://169.254.169.254/latest/meta-data/instance-id');\n\n\t\t\t\tsetInterval(async () => {\n\t\t\t\t\ttry {\n\t\t\t\t\t\tawait axios.post('https://backend.resolveio.com/api/health', {\n\t\t\t\t\t\t\ttype: 'application',\n\t\t\t\t\t\t\tid_aws_instance: instanceId.data,\n\t\t\t\t\t\t\tversion: typeof process.env.APP_VERSION === 'number'\n\t\t\t\t\t\t\t\t? (<any>process.env.APP_VERSION).toString()\n\t\t\t\t\t\t\t\t: process.env.APP_VERSION,\n\t\t\t\t\t\t\tversion_key: process.env.APP_VERSION_KEY\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\t\t\t\t\tcatch(e) {}\n\t\t\t\t}, 10000);\n\t\t\t}\n\t\t\tcatch(errHealth) {}\n\t\t}\n\n\t\tlet lastCPU = this.cpuAverage();\n\n\t\tsetInterval(() => {\n\t\t\tlet now = new Date();\n\t\t\tvar endMeasure = this.cpuAverage();\n\t\t\tvar idleDifference = endMeasure.idle - lastCPU.idle;\n\t\t\tvar totalDifference = endMeasure.total - lastCPU.total;\n\t\t\tvar percentageCPU = 1 - idleDifference / totalDifference;\n\n\t\t\t// Original local code\n\t\t\tif (\n\t\t\t\tResolveIOServer.getServerConfig()['ROOT_URL'] === 'https://resolveio.com'\n\t\t\t|| ResolveIOServer.getServerConfig()['ROOT_URL'] === 'http://localhost:4200'\n\t\t\t) {\n\t\t\t\tMonitorCPUs.create({\n\t\t\t\t\t_id: objectIdHexString(),\n\t\t\t\t\tmetadata: {\n\t\t\t\t\t\tinstance: this._instanceHostname,\n\t\t\t\t\t\tclient: ResolveIOServer.getServerConfig()['CLIENT_NAME']\n\t\t\t\t\t},\n\t\t\t\t\tdate: now,\n\t\t\t\t\tapp: percentageCPU,\n\t\t\t\t\tsystem: os.loadavg()[0]\n\t\t\t\t});\n\t\t\t}\n\t\t\telse {\n\t\t\t\tthis.pushMonitorData({\n\t\t\t\t\ttype: 'monitor-cpu',\n\t\t\t\t\tdata: {\n\t\t\t\t\t\tmetadata: {\n\t\t\t\t\t\t\tinstance: this._instanceHostname,\n\t\t\t\t\t\t\tclient: ResolveIOServer.getServerConfig()['CLIENT_NAME']\n\t\t\t\t\t\t},\n\t\t\t\t\t\tdate: now,\n\t\t\t\t\t\tapp: percentageCPU,\n\t\t\t\t\t\tsystem: os.loadavg()[0]\n\t\t\t\t\t}\n\t\t\t\t});\n\t\t\t}\n\n\t\t\tlastCPU = endMeasure;\n\n\t\t\tconst memUsage = process.memoryUsage();\n\n\t\t\tif (\n\t\t\t\tResolveIOServer.getServerConfig()['ROOT_URL'] === 'https://resolveio.com'\n\t\t\t|| ResolveIOServer.getServerConfig()['ROOT_URL'] === 'http://localhost:4200'\n\t\t\t) {\n\t\t\t\tMonitorMemorys.create({\n\t\t\t\t\t_id: objectIdHexString(),\n\t\t\t\t\tmetadata: {\n\t\t\t\t\t\tinstance: this._instanceHostname,\n\t\t\t\t\t\tclient: ResolveIOServer.getServerConfig()['CLIENT_NAME']\n\t\t\t\t\t},\n\t\t\t\t\tdate: now,\n\t\t\t\t\tphysical_total: memUsage.heapTotal,\n\t\t\t\t\tphysical_used: memUsage.heapUsed,\n\t\t\t\t\tphysical_free: os.freemem(),\n\t\t\t\t\tvirtual: memUsage.rss,\n\t\t\t\t\tprivate: memUsage.external,\n\t\t\t\t\tphysical: os.totalmem()\n\t\t\t\t});\n\t\t\t}\n\t\t\telse {\n\t\t\t\tthis.pushMonitorData({\n\t\t\t\t\ttype: 'monitor-memory',\n\t\t\t\t\tdata: {\n\t\t\t\t\t\tmetadata: {\n\t\t\t\t\t\t\tinstance: this._instanceHostname,\n\t\t\t\t\t\t\tclient: ResolveIOServer.getServerConfig()['CLIENT_NAME']\n\t\t\t\t\t\t},\n\t\t\t\t\t\tdate: now,\n\t\t\t\t\t\tphysical_total: memUsage.heapTotal,\n\t\t\t\t\t\tphysical_used: memUsage.heapUsed,\n\t\t\t\t\t\tphysical_free: os.freemem(),\n\t\t\t\t\t\tvirtual: memUsage.rss,\n\t\t\t\t\t\tprivate: memUsage.external,\n\t\t\t\t\t\tphysical: os.totalmem(),\n\t\t\t\t\t\ttype: 'memory'\n\t\t\t\t\t}\n\t\t\t\t});\n\t\t\t}\n\t\t}, 3000);\n\t}\n\n\taddMongoTracker(data: MongoMonitorModel) {\n\t\tif (\n\t\t\tResolveIOServer.getServerConfig()['ROOT_URL'] === 'https://resolveio.com'\n\t\t\t|| ResolveIOServer.getServerConfig()['ROOT_URL'] === 'http://localhost:4200'\n\t\t) {\n\t\t\tif (!data.collection.startsWith('monitor-')) {\n\t\t\t\tMonitorMongos.create({\n\t\t\t\t\t_id: objectIdHexString(),\n\t\t\t\t\tmetadata: {\n\t\t\t\t\t\tinstance: this._instanceHostname,\n\t\t\t\t\t\tclient: ResolveIOServer.getServerConfig()['CLIENT_NAME']\n\t\t\t\t\t},\n\t\t\t\t\tdate: new Date(data.date),\n\t\t\t\t\tmethod: data.method,\n\t\t\t\t\tdoc_collection: data.collection,\n\t\t\t\t\tduration: data.duration,\n\t\t\t\t\tquery: Buffer.byteLength(data.query, 'utf8') < 15800000\n\t\t\t\t\t\t? data.query\n\t\t\t\t\t\t: 'Too Big'\n\t\t\t\t});\n\t\t\t}\n\t\t}\n\t\telse {\n\t\t\tif (this._instanceHostname) {\n\t\t\t\tthis.pushMonitorData({\n\t\t\t\t\ttype: 'monitor-mongo',\n\t\t\t\t\tdata: {\n\t\t\t\t\t\tmetadata: {\n\t\t\t\t\t\t\tinstance: this._instanceHostname,\n\t\t\t\t\t\t\tclient: ResolveIOServer.getServerConfig()['CLIENT_NAME']\n\t\t\t\t\t\t},\n\t\t\t\t\t\tdate: new Date(data.date),\n\t\t\t\t\t\tmethod: data.method,\n\t\t\t\t\t\tdoc_collection: data.collection,\n\t\t\t\t\t\tduration: data.duration,\n\t\t\t\t\t\tquery: Buffer.byteLength(data.query, 'utf8') < 15800000\n\t\t\t\t\t\t\t? data.query\n\t\t\t\t\t\t\t: 'Too Big'\n\t\t\t\t\t}\n\t\t\t\t});\n\t\t\t}\n\t\t}\n\t}\n\n\tprivate pushMonitorData(data: LocalLogModel) {\n\t\tthis._monitorData.push(data);\n\n\t\t// If data is huge, flush immediately to file\n\t\tif (this.estimateBatchSize() >= MAX_BATCH_SIZE) {\n\t\t\tthis.flushMonitorData();\n\t\t}\n\t}\n\n\tprivate estimateBatchSize(): number {\n\t\treturn this._monitorData.reduce((total, item) => {\n\t\t\treturn total + Buffer.byteLength(JSON.stringify(item));\n\t\t}, 0);\n\t}\n\n\tsetupEventLoop() {\n\t\t// Flush every 2.5 seconds\n\t\tsetInterval(() => {\n\t\t\tif (this._monitorData.length > 0) {\n\t\t\t\tthis.flushMonitorData();\n\t\t\t}\n\t\t}, 2500);\n\t}\n\n\tprivate flushMonitorData() {\n\t\tconst batch = [];\n\t\tlet batchSize = 0;\n\n\t\twhile (this._monitorData.length > 0 && batchSize < MAX_BATCH_SIZE) {\n\t\t\tconst item = this._monitorData.shift();\n\t\t\tconst itemSize = Buffer.byteLength(JSON.stringify(item));\n\n\t\t\tif ((batchSize + itemSize) > MAX_BATCH_SIZE && batch.length > 0) {\n\t\t\t\t// put the item back for next round\n\t\t\t\tthis._monitorData.unshift(item);\n\t\t\t\tbreak;\n\t\t\t}\n\n\t\t\tbatch.push(item);\n\t\t\tbatchSize += itemSize;\n\t\t}\n\n\t\ttry {\n\t\t\t// Now, instead of calling an external HTTP endpoint, we write the batch to disk:\n\t\t\tResolveIOServer.getLocalLogManager().writeLogs(batch);\n\t\t}\n\t\tcatch (error) {\n\t\t\tconsole.error(new Date(), 'MonitorManager', 'Failed to flush monitor data to file:', error);\n\t\t}\n\t}\n}\n\nexport interface MongoMonitorModel {\n\tdate: Date;\n\tmethod: string;\n\tcollection: string;\n\tduration: number;\n\tquery: string;\n}\n\nexport class MonitorMongo {\n\tprivate _startTime = 0;\n\tprivate _method = '';\n\tprivate _collection = '';\n\tprivate _query = '';\n\n\tconstructor(method: string, collection: string, query: string) {\n\t\tthis._startTime = Date.now();\n\t\tthis._method = method;\n\t\tthis._collection = collection;\n\t\tthis._query = query;\n\t}\n\n\tpublic finish() {\n\t\tlet endTime = Date.now();\n\n\t\tlet mongoMonitor: MongoMonitorModel = {\n\t\t\tdate: new Date(endTime),\n\t\t\tmethod: this._method,\n\t\t\tcollection: this._collection,\n\t\t\tquery: this._query,\n\t\t\tduration: endTime - this._startTime\n\t\t};\n\n\t\tResolveIOServer.getMainServer().getMonitorManager().addMongoTracker(mongoMonitor);\n\t}\n}\n\nexport class MonitorManagerFunction {\n\tprivate _functionCnt = 0;\n\tprivate _functions: MonitorFunction[] = [];\n\tprivate _functionLastCompletedWS: { [id_socket: string]: MonitorFunction; } = {};\n\tprivate _functionLastCompleted: MonitorFunction;\n\n\tconstructor() {}\n\n\tstartMonitorFunction(\n\t\tfunctionType: MonitorFunctionType,\n\t\tfunctionName: string,\n\t\tuser: string,\n\t\tid_socket: string,\n\t\tdata: any\n\t) {\n\t\tlet newMonitorFunction = new MonitorFunction(\n\t\t\tthis._functionCnt++,\n\t\t\tfunctionType,\n\t\t\tfunctionName,\n\t\t\tuser,\n\t\t\tid_socket,\n\t\t\tdata\n\t\t);\n\n\t\tthis._functions.push(newMonitorFunction);\n\t\treturn newMonitorFunction.id;\n\t}\n\n\tfinishMonitorFunction(id: number) {\n\t\tlet monitor = this._functions.find(a => a.id === id);\n\n\t\tif (monitor) {\n\t\t\tmonitor.finish();\n\t\t\tthis._functionLastCompleted = monitor;\n\t\t\tthis._functionLastCompletedWS[monitor.id_socket] = monitor;\n\t\t\tthis._functions.splice(\n\t\t\t\tthis._functions.map(a => a.id).indexOf(id),\n\t\t\t\t1\n\t\t\t);\n\t\t}\n\t}\n\n\tgetActiveMonitorFunctions() {\n\t\treturn this._functions;\n\t}\n\n\tgetLastCompletedMonitorFunction() {\n\t\treturn this._functionLastCompleted;\n\t}\n\n\tgetLastCompletedMonitorFunctionWS() {\n\t\treturn this._functionLastCompletedWS;\n\t}\n}\n\nexport type MonitorFunctionType =\n\t'Cron Method'\n\t| 'Internal Method'\n\t| 'Method'\n\t| 'User Specific Publication'\n\t| 'Publication';\n\nexport class MonitorFunction {\n\tprivate _timer: NodeJS.Timeout = null;\n\tprivate _maxDiff = 0;\n\tprivate _functionType: MonitorFunctionType;\n\tprivate _functionName: string;\n\tprivate _user: string;\n\tprivate _data: any;\n\tpublic startTime: Date = null;\n\tpublic endTime: Date = null;\n\tpublic lastTime: Date = null;\n\tpublic id_socket = '';\n\tpublic id = 0;\n\n\tconstructor(\n\t\tid: number,\n\t\tfunctionType: MonitorFunctionType,\n\t\tfunctionName: string,\n\t\tuser: string,\n\t\tid_socket: string,\n\t\tdata: any\n\t) {\n\t\tthis.id = id;\n\t\tthis.startTime = new Date();\n\t\tthis._functionType = functionType;\n\t\tthis._functionName = functionName;\n\t\tthis._user = user;\n\t\tthis.id_socket = id_socket;\n\t\tthis._data = data;\n\t\tthis.lastTime = new Date();\n\n\t\tthis._timer = setInterval(() => {\n\t\t\tlet diff = Date.now() - this.lastTime.getTime();\n\n\t\t\tif (diff > this._maxDiff) {\n\t\t\t\tthis._maxDiff = diff;\n\t\t\t}\n\n\t\t\tthis.lastTime = new Date();\n\t\t}, 1000);\n\t}\n\n\tpublic finish() {\n\t\tif (this._timer) {\n\t\t\tclearInterval(this._timer);\n\t\t\tthis._timer = null;\n\t\t}\n\n\t\tthis.endTime = new Date();\n\n\t\tif (this._maxDiff >= 5000) {\n\t\t\t// Optional slow function logging or email\n\t\t}\n\t}\n}"]}
1
+ {"version":3,"sources":["../../src/managers/monitor.manager.ts"],"names":["axios_1","require","monitor_cpu_collection_1","monitor_memory_collection_1","monitor_mongo_collection_1","resolveio_server_app_1","common_1","monitor_function_collection_1","os","MAX_BATCH_SIZE","MonitorManager","this","_instanceHostname","_monitorData","hostname","replace","setupIntervals","setupEventLoop","prototype","cpuAverage","totalIdle","totalTick","cpus","i","len","length","type","cpu","times","idle","total","ResolveIOServer","getServerConfig","default","get","instanceId_1","_a","sent","setInterval","__awaiter","_this","post","id_aws_instance","data","version","process","env","APP_VERSION","toString","version_key","APP_VERSION_KEY","lastCPU","now","Date","endMeasure","percentageCPU","memUsage","MonitorCPUs","create","_id","objectIdHexString","metadata","instance","client","date","app","system","loadavg","pushMonitorData","memoryUsage","MonitorMemorys","physical_total","heapTotal","physical_used","heapUsed","physical_free","freemem","virtual","rss","private","external","physical","totalmem","addMongoTracker","collection","startsWith","MonitorMongos","method","doc_collection","duration","query","Buffer","byteLength","addFunctionTracker","MonitorFunctions","startTime","functionType","name","functionName","user","push","estimateBatchSize","flushMonitorData","reduce","item","JSON","stringify","batch","batchSize","shift","itemSize","unshift","getLocalLogManager","writeLogs","error","console","MonitorMongo","exports","_startTime","_method","_collection","_query","finish","endTime","mongoMonitor","getMainServer","getMonitorManager","MonitorManagerFunction","_functionCnt","_functions","_functionLastCompletedWS","startMonitorFunction","id_socket","newMonitorFunction","MonitorFunction","id","finishMonitorFunction","monitor","find","a","_functionLastCompleted","splice","map","indexOf","getActiveMonitorFunctions","getLastCompletedMonitorFunction","getLastCompletedMonitorFunctionWS","_timer","lastTime","diff","getTime","clearInterval"],"mappings":"k8CAAAA,S,iKAAAC,QAAA,OAAA,GACAC,yBAAAD,QAAA,uCAAA,EACAE,4BAAAF,QAAA,0CAAA,EACAG,2BAAAH,QAAA,yCAAA,EACAI,uBAAAJ,QAAA,yBAAA,EACAK,SAAAL,QAAA,gBAAA,EAEAM,8BAAAN,QAAA,4CAAA,EAEMO,GAAKP,QAAQ,IAAI,EACjBQ,eAAiB,QAEvBC,eAAA,WAIC,SAAAA,iBAHQC,KAAAC,kBAAoB,GACpBD,KAAAE,aAAgC,GAGvCF,KAAKC,kBAAoBJ,GAAGM,SAAQ,EAAGC,QAAQ,MAAO,GAAG,EAEzDJ,KAAKK,eAAc,EACnBL,KAAKM,eAAc,CACpB,CA8QD,OA5QCP,eAAAQ,UAAAC,WAAA,WAIC,IAHA,IAAIC,EAAY,EAAGC,EAAY,EAC3BC,EAAOd,GAAGc,KAAI,EAETC,EAAI,EAAGC,EAAMF,EAAKG,OAAQF,EAAIC,EAAKD,CAAC,GAAI,CAChD,IAESG,EAFLC,EAAML,EAAKC,GAEf,IAASG,KAAQC,EAAIC,MACpBP,GAAaM,EAAIC,MAAMF,GAExBN,GAAaO,EAAIC,MAAMC,I,CAGxB,MAAO,CACNA,KAAMT,EAAYE,EAAKG,OACvBK,MAAOT,EAAYC,EAAKG,M,CAE1B,EAEMf,eAAAQ,UAAAF,eAAN,W,4HAGoD,0BAAlDX,uBAAA0B,gBAAgBC,gBAAe,EAAa,UACQ,0BAAlD3B,uBAAA0B,gBAAgBC,gBAAe,EAAa,SAD9C,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ager.js","sourcesContent":["import axios from 'axios';\nimport { MonitorCPUs } from '../collections/monitor-cpu.collection';\nimport { MonitorMemorys } from '../collections/monitor-memory.collection';\nimport { MonitorMongos } from '../collections/monitor-mongo.collection';\nimport { ResolveIOServer } from '../resolveio-server-app';\nimport { objectIdHexString } from '../util/common';\nimport { LocalLogModel } from './local-log.manager';\nimport { MonitorFunctions } from '../collections/monitor-function.collection';\n\nconst os = require('os');\nconst MAX_BATCH_SIZE = 8 * 1024 * 1024; // 8MB max for safety\n\nexport class MonitorManager {\n\tprivate _instanceHostname = '';\n\tprivate _monitorData: LocalLogModel[] = [];\n\n\tconstructor() {\n\t\tthis._instanceHostname = os.hostname().replace(/\\./g, '-');\n\n\t\tthis.setupIntervals();\n\t\tthis.setupEventLoop();\n\t}\n\n\tcpuAverage() {\n\t\tvar totalIdle = 0, totalTick = 0;\n\t\tvar cpus = os.cpus();\n\n\t\tfor (var i = 0, len = cpus.length; i < len; i++) {\n\t\t\tvar cpu = cpus[i];\n\n\t\t\tfor (let type in cpu.times) {\n\t\t\t\ttotalTick += cpu.times[type];\n\t\t\t}\n\t\t\ttotalIdle += cpu.times.idle;\n\t\t}\n\n\t\treturn {\n\t\t\tidle: totalIdle / cpus.length,\n\t\t\ttotal: totalTick / cpus.length\n\t\t};\n\t}\n\n\tasync setupIntervals() {\n\t\t// Example: If not on production or localhost, do a health check\n\t\tif (\n\t\t\tResolveIOServer.getServerConfig()['ROOT_URL'] !== 'https://resolveio.com'\n\t\t&& ResolveIOServer.getServerConfig()['ROOT_URL'] !== 'http://localhost:4200'\n\t\t) {\n\t\t\ttry {\n\t\t\t\tlet instanceId = await axios.get('http://169.254.169.254/latest/meta-data/instance-id');\n\n\t\t\t\tsetInterval(async () => {\n\t\t\t\t\ttry {\n\t\t\t\t\t\tawait axios.post('https://backend.resolveio.com/api/health', {\n\t\t\t\t\t\t\ttype: 'application',\n\t\t\t\t\t\t\tid_aws_instance: instanceId.data,\n\t\t\t\t\t\t\tversion: typeof process.env.APP_VERSION === 'number'\n\t\t\t\t\t\t\t\t? (<any>process.env.APP_VERSION).toString()\n\t\t\t\t\t\t\t\t: process.env.APP_VERSION,\n\t\t\t\t\t\t\tversion_key: process.env.APP_VERSION_KEY\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\t\t\t\t\tcatch(e) {}\n\t\t\t\t}, 10000);\n\t\t\t}\n\t\t\tcatch(errHealth) {}\n\t\t}\n\n\t\tlet lastCPU = this.cpuAverage();\n\n\t\tsetInterval(() => {\n\t\t\tlet now = new Date();\n\t\t\tvar endMeasure = this.cpuAverage();\n\t\t\tvar idleDifference = endMeasure.idle - lastCPU.idle;\n\t\t\tvar totalDifference = endMeasure.total - lastCPU.total;\n\t\t\tvar percentageCPU = 1 - idleDifference / totalDifference;\n\n\t\t\t// Original local code\n\t\t\tif (\n\t\t\t\tResolveIOServer.getServerConfig()['ROOT_URL'] === 'https://resolveio.com'\n\t\t\t|| ResolveIOServer.getServerConfig()['ROOT_URL'] === 'http://localhost:4200'\n\t\t\t) {\n\t\t\t\tMonitorCPUs.create({\n\t\t\t\t\t_id: objectIdHexString(),\n\t\t\t\t\tmetadata: {\n\t\t\t\t\t\tinstance: this._instanceHostname,\n\t\t\t\t\t\tclient: ResolveIOServer.getServerConfig()['CLIENT_NAME']\n\t\t\t\t\t},\n\t\t\t\t\tdate: now,\n\t\t\t\t\tapp: percentageCPU,\n\t\t\t\t\tsystem: os.loadavg()[0]\n\t\t\t\t});\n\t\t\t}\n\t\t\telse {\n\t\t\t\tthis.pushMonitorData({\n\t\t\t\t\ttype: 'monitor-cpu',\n\t\t\t\t\tdata: {\n\t\t\t\t\t\tmetadata: {\n\t\t\t\t\t\t\tinstance: this._instanceHostname,\n\t\t\t\t\t\t\tclient: ResolveIOServer.getServerConfig()['CLIENT_NAME']\n\t\t\t\t\t\t},\n\t\t\t\t\t\tdate: now,\n\t\t\t\t\t\tapp: percentageCPU,\n\t\t\t\t\t\tsystem: os.loadavg()[0]\n\t\t\t\t\t}\n\t\t\t\t});\n\t\t\t}\n\n\t\t\tlastCPU = endMeasure;\n\n\t\t\tconst memUsage = process.memoryUsage();\n\n\t\t\tif (\n\t\t\t\tResolveIOServer.getServerConfig()['ROOT_URL'] === 'https://resolveio.com'\n\t\t\t|| ResolveIOServer.getServerConfig()['ROOT_URL'] === 'http://localhost:4200'\n\t\t\t) {\n\t\t\t\tMonitorMemorys.create({\n\t\t\t\t\t_id: objectIdHexString(),\n\t\t\t\t\tmetadata: {\n\t\t\t\t\t\tinstance: this._instanceHostname,\n\t\t\t\t\t\tclient: ResolveIOServer.getServerConfig()['CLIENT_NAME']\n\t\t\t\t\t},\n\t\t\t\t\tdate: now,\n\t\t\t\t\tphysical_total: memUsage.heapTotal,\n\t\t\t\t\tphysical_used: memUsage.heapUsed,\n\t\t\t\t\tphysical_free: os.freemem(),\n\t\t\t\t\tvirtual: memUsage.rss,\n\t\t\t\t\tprivate: memUsage.external,\n\t\t\t\t\tphysical: os.totalmem()\n\t\t\t\t});\n\t\t\t}\n\t\t\telse {\n\t\t\t\tthis.pushMonitorData({\n\t\t\t\t\ttype: 'monitor-memory',\n\t\t\t\t\tdata: {\n\t\t\t\t\t\tmetadata: {\n\t\t\t\t\t\t\tinstance: this._instanceHostname,\n\t\t\t\t\t\t\tclient: ResolveIOServer.getServerConfig()['CLIENT_NAME']\n\t\t\t\t\t\t},\n\t\t\t\t\t\tdate: now,\n\t\t\t\t\t\tphysical_total: memUsage.heapTotal,\n\t\t\t\t\t\tphysical_used: memUsage.heapUsed,\n\t\t\t\t\t\tphysical_free: os.freemem(),\n\t\t\t\t\t\tvirtual: memUsage.rss,\n\t\t\t\t\t\tprivate: memUsage.external,\n\t\t\t\t\t\tphysical: os.totalmem(),\n\t\t\t\t\t\ttype: 'memory'\n\t\t\t\t\t}\n\t\t\t\t});\n\t\t\t}\n\t\t}, 3000);\n\t}\n\n\taddMongoTracker(data: MongoMonitorModel) {\n\t\tif (\n\t\t\tResolveIOServer.getServerConfig()['ROOT_URL'] === 'https://resolveio.com'\n\t\t\t|| ResolveIOServer.getServerConfig()['ROOT_URL'] === 'http://localhost:4200'\n\t\t) {\n\t\t\tif (!data.collection.startsWith('monitor-')) {\n\t\t\t\tMonitorMongos.create({\n\t\t\t\t\t_id: objectIdHexString(),\n\t\t\t\t\tmetadata: {\n\t\t\t\t\t\tinstance: this._instanceHostname,\n\t\t\t\t\t\tclient: ResolveIOServer.getServerConfig()['CLIENT_NAME']\n\t\t\t\t\t},\n\t\t\t\t\tdate: new Date(data.date),\n\t\t\t\t\tmethod: data.method,\n\t\t\t\t\tdoc_collection: data.collection,\n\t\t\t\t\tduration: data.duration,\n\t\t\t\t\tquery: Buffer.byteLength(data.query, 'utf8') < 15800000\n\t\t\t\t\t\t? data.query\n\t\t\t\t\t\t: 'Too Big'\n\t\t\t\t});\n\t\t\t}\n\t\t}\n\t\telse {\n\t\t\tif (this._instanceHostname) {\n\t\t\t\tthis.pushMonitorData({\n\t\t\t\t\ttype: 'monitor-mongo',\n\t\t\t\t\tdata: {\n\t\t\t\t\t\tmetadata: {\n\t\t\t\t\t\t\tinstance: this._instanceHostname,\n\t\t\t\t\t\t\tclient: ResolveIOServer.getServerConfig()['CLIENT_NAME']\n\t\t\t\t\t\t},\n\t\t\t\t\t\tdate: new Date(data.date),\n\t\t\t\t\t\tmethod: data.method,\n\t\t\t\t\t\tdoc_collection: data.collection,\n\t\t\t\t\t\tduration: data.duration,\n\t\t\t\t\t\tquery: Buffer.byteLength(data.query, 'utf8') < 15800000\n\t\t\t\t\t\t\t? data.query\n\t\t\t\t\t\t\t: 'Too Big'\n\t\t\t\t\t}\n\t\t\t\t});\n\t\t\t}\n\t\t}\n\t}\n\n\taddFunctionTracker(data: MonitorFunction) {\n\t\tif (\n\t\t\tResolveIOServer.getServerConfig()['ROOT_URL'] === 'https://resolveio.com'\n\t\t\t|| ResolveIOServer.getServerConfig()['ROOT_URL'] === 'http://localhost:4200'\n\t\t) {\n\t\t\tMonitorFunctions.create({\n\t\t\t\t_id: objectIdHexString(),\n\t\t\t\tmetadata: {\n\t\t\t\t\tinstance: this._instanceHostname,\n\t\t\t\t\tclient: ResolveIOServer.getServerConfig()['CLIENT_NAME']\n\t\t\t\t},\n\t\t\t\tdate: new Date(data.startTime),\n\t\t\t\ttype: data.functionType,\n\t\t\t\tname: data.functionName,\n\t\t\t\tuser: data.user,\n\t\t\t\tduration: data.duration,\n\t\t\t\tdata: Buffer.byteLength(data.data, 'utf8') < 15800000\n\t\t\t\t\t? data.data\n\t\t\t\t\t: 'Too Big'\n\t\t\t});\n\t\t}\n\t\telse {\n\t\t\tif (this._instanceHostname) {\n\t\t\t\tthis.pushMonitorData({\n\t\t\t\t\ttype: 'monitor-function',\n\t\t\t\t\tdata: {\n\t\t\t\t\t\tmetadata: {\n\t\t\t\t\t\t\tinstance: this._instanceHostname,\n\t\t\t\t\t\t\tclient: ResolveIOServer.getServerConfig()['CLIENT_NAME']\n\t\t\t\t\t\t},\n\t\t\t\t\t\tdate: new Date(data.startTime),\n\t\t\t\t\t\ttype: data.functionType,\n\t\t\t\t\t\tname: data.functionName,\n\t\t\t\t\t\tuser: data.user,\n\t\t\t\t\t\tduration: data.duration,\n\t\t\t\t\t\tdata: Buffer.byteLength(data.data, 'utf8') < 15800000\n\t\t\t\t\t\t\t? data.data\n\t\t\t\t\t\t\t: 'Too Big'\n\t\t\t\t\t}\n\t\t\t\t});\n\t\t\t}\n\t\t}\n\t}\n\n\tprivate pushMonitorData(data: LocalLogModel) {\n\t\tthis._monitorData.push(data);\n\n\t\t// If data is huge, flush immediately to file\n\t\tif (this.estimateBatchSize() >= MAX_BATCH_SIZE) {\n\t\t\tthis.flushMonitorData();\n\t\t}\n\t}\n\n\tprivate estimateBatchSize(): number {\n\t\treturn this._monitorData.reduce((total, item) => {\n\t\t\treturn total + Buffer.byteLength(JSON.stringify(item));\n\t\t}, 0);\n\t}\n\n\tsetupEventLoop() {\n\t\t// Flush every 2.5 seconds\n\t\tsetInterval(() => {\n\t\t\tif (this._monitorData.length > 0) {\n\t\t\t\tthis.flushMonitorData();\n\t\t\t}\n\t\t}, 2500);\n\t}\n\n\tprivate flushMonitorData() {\n\t\tconst batch = [];\n\t\tlet batchSize = 0;\n\n\t\twhile (this._monitorData.length > 0 && batchSize < MAX_BATCH_SIZE) {\n\t\t\tconst item = this._monitorData.shift();\n\t\t\tconst itemSize = Buffer.byteLength(JSON.stringify(item));\n\n\t\t\tif ((batchSize + itemSize) > MAX_BATCH_SIZE && batch.length > 0) {\n\t\t\t\t// put the item back for next round\n\t\t\t\tthis._monitorData.unshift(item);\n\t\t\t\tbreak;\n\t\t\t}\n\n\t\t\tbatch.push(item);\n\t\t\tbatchSize += itemSize;\n\t\t}\n\n\t\ttry {\n\t\t\t// Now, instead of calling an external HTTP endpoint, we write the batch to disk:\n\t\t\tResolveIOServer.getLocalLogManager().writeLogs(batch);\n\t\t}\n\t\tcatch (error) {\n\t\t\tconsole.error(new Date(), 'MonitorManager', 'Failed to flush monitor data to file:', error);\n\t\t}\n\t}\n}\n\nexport interface MongoMonitorModel {\n\tdate: Date;\n\tmethod: string;\n\tcollection: string;\n\tduration: number;\n\tquery: string;\n}\n\nexport class MonitorMongo {\n\tprivate _startTime = 0;\n\tprivate _method = '';\n\tprivate _collection = '';\n\tprivate _query = '';\n\n\tconstructor(method: string, collection: string, query: string) {\n\t\tthis._startTime = Date.now();\n\t\tthis._method = method;\n\t\tthis._collection = collection;\n\t\tthis._query = query;\n\t}\n\n\tpublic finish() {\n\t\tlet endTime = Date.now();\n\n\t\tlet mongoMonitor: MongoMonitorModel = {\n\t\t\tdate: new Date(endTime),\n\t\t\tmethod: this._method,\n\t\t\tcollection: this._collection,\n\t\t\tquery: this._query,\n\t\t\tduration: endTime - this._startTime\n\t\t};\n\n\t\tResolveIOServer.getMainServer().getMonitorManager().addMongoTracker(mongoMonitor);\n\t}\n}\n\nexport class MonitorManagerFunction {\n\tprivate _functionCnt = 0;\n\tprivate _functions: MonitorFunction[] = [];\n\tprivate _functionLastCompletedWS: { [id_socket: string]: MonitorFunction; } = {};\n\tprivate _functionLastCompleted: MonitorFunction;\n\n\tconstructor() {}\n\n\tstartMonitorFunction(\n\t\tfunctionType: MonitorFunctionType,\n\t\tfunctionName: string,\n\t\tuser: string,\n\t\tid_socket: string,\n\t\tdata: any\n\t) {\n\t\tlet newMonitorFunction = new MonitorFunction(\n\t\t\tthis._functionCnt++,\n\t\t\tfunctionType,\n\t\t\tfunctionName,\n\t\t\tuser,\n\t\t\tid_socket,\n\t\t\tdata\n\t\t);\n\n\t\tthis._functions.push(newMonitorFunction);\n\t\treturn newMonitorFunction.id;\n\t}\n\n\tfinishMonitorFunction(id: number) {\n\t\tlet monitor = this._functions.find(a => a.id === id);\n\n\t\tif (monitor) {\n\t\t\tmonitor.finish();\n\t\t\tthis._functionLastCompleted = monitor;\n\t\t\tthis._functionLastCompletedWS[monitor.id_socket] = monitor;\n\t\t\tthis._functions.splice(\n\t\t\t\tthis._functions.map(a => a.id).indexOf(id),\n\t\t\t\t1\n\t\t\t);\n\t\t}\n\t}\n\n\tgetActiveMonitorFunctions() {\n\t\treturn this._functions;\n\t}\n\n\tgetLastCompletedMonitorFunction() {\n\t\treturn this._functionLastCompleted;\n\t}\n\n\tgetLastCompletedMonitorFunctionWS() {\n\t\treturn this._functionLastCompletedWS;\n\t}\n}\n\nexport type MonitorFunctionType =\n\t'Cron Method'\n\t| 'Method'\n\t| 'User Specific Publication'\n\t| 'Publication';\n\nexport class MonitorFunction {\n\tprivate _timer: NodeJS.Timeout = null;\n\tpublic duration = 0;\n\tpublic functionType: MonitorFunctionType;\n\tpublic functionName: string;\n\tpublic user: string;\n\tpublic data: any;\n\tpublic startTime: Date = null;\n\tpublic endTime: Date = null;\n\tpublic lastTime: Date = null;\n\tpublic id_socket = '';\n\tpublic id = 0;\n\n\tconstructor(\n\t\tid: number,\n\t\tfunctionType: MonitorFunctionType,\n\t\tfunctionName: string,\n\t\tuser: string,\n\t\tid_socket: string,\n\t\tdata: any\n\t) {\n\t\tthis.id = id;\n\t\tthis.startTime = new Date();\n\t\tthis.functionType = functionType;\n\t\tthis.functionName = functionName;\n\t\tthis.user = user;\n\t\tthis.id_socket = id_socket;\n\t\tthis.data = data;\n\t\tthis.lastTime = new Date();\n\n\t\tthis._timer = setInterval(() => {\n\t\t\tlet diff = Date.now() - this.lastTime.getTime();\n\n\t\t\tif (diff > this.duration) {\n\t\t\t\tthis.duration = diff;\n\t\t\t}\n\n\t\t\tthis.lastTime = new Date();\n\t\t}, 1000);\n\t}\n\n\tpublic finish() {\n\t\tif (this._timer) {\n\t\t\tclearInterval(this._timer);\n\t\t\tthis._timer = null;\n\t\t}\n\n\t\tthis.endTime = new Date();\n\n\t\tif (this.duration >= 5000) {\n\t\t\t// Optional slow function logging or email\n\t\t}\n\n\t\tResolveIOServer.getMainServer().getMonitorManager().addFunctionTracker(this);\n\t}\n}"]}
@@ -1,2 +1,2 @@
1
- "use strict";var __awaiter=this&&this.__awaiter||function(e,i,o,s){return new(o=o||Promise)(function(n,t){function fulfilled(e){try{step(s.next(e))}catch(e){t(e)}}function rejected(e){try{step(s.throw(e))}catch(e){t(e)}}function step(e){var t;e.done?n(e.value):((t=e.value)instanceof o?t:new o(function(e){e(t)})).then(fulfilled,rejected)}step((s=s.apply(e,i||[])).next())})},__generator=this&&this.__generator||function(i,o){var s,r,a,l={label:0,sent:function(){if(1&a[0])throw a[1];return a[1]},trys:[],ops:[]},c={next:verb(0),throw:verb(1),return:verb(2)};return"function"==typeof Symbol&&(c[Symbol.iterator]=function(){return this}),c;function verb(n){return function(e){var t=[n,e];if(s)throw new TypeError("Generator is already executing.");for(;l=c&&t[c=0]?0:l;)try{if(s=1,r&&(a=2&t[0]?r.return:t[0]?r.throw||((a=r.return)&&a.call(r),0):r.next)&&!(a=a.call(r,t[1])).done)return a;switch(r=0,(t=a?[2&t[0],a.value]:t)[0]){case 0:case 1:a=t;break;case 4:return l.label++,{value:t[1],done:!1};case 5:l.label++,r=t[1],t=[0];continue;case 7:t=l.ops.pop(),l.trys.pop();continue;default:if(!(a=0<(a=l.trys).length&&a[a.length-1])&&(6===t[0]||2===t[0])){l=0;continue}if(3===t[0]&&(!a||t[1]>a[0]&&t[1]<a[3]))l.label=t[1];else if(6===t[0]&&l.label<a[1])l.label=a[1],a=t;else{if(!(a&&l.label<a[2])){a[2]&&l.ops.pop(),l.trys.pop();continue}l.label=a[2],l.ops.push(t)}}t=o.call(i,l)}catch(e){t=[6,e],r=0}finally{s=a=0}if(5&t[0])throw t[1];return{value:t[0]?t[1]:void 0,done:!0}}}},__values=this&&this.__values||function(e){var t="function"==typeof Symbol&&Symbol.iterator,n=t&&e[t],i=0;if(n)return n.call(e);if(e&&"number"==typeof e.length)return{next:function(){return{value:(e=e&&i>=e.length?void 0:e)&&e[i++],done:!e}}};throw new TypeError(t?"Object is not iterable.":"Symbol.iterator is not defined.")},__read=this&&this.__read||function(e,t){var n="function"==typeof Symbol&&e[Symbol.iterator];if(!n)return e;var i,o,s=n.call(e),r=[];try{for(;(void 0===t||0<t--)&&!(i=s.next()).done;)r.push(i.value)}catch(e){o={error:e}}finally{try{i&&!i.done&&(n=s.return)&&n.call(s)}finally{if(o)throw o.error}}return r},__spreadArray=this&&this.__spreadArray||function(e,t,n){if(n||2===arguments.length)for(var i,o=0,s=t.length;o<s;o++)!i&&o in t||((i=i||Array.prototype.slice.call(t,0,o))[o]=t[o]);return e.concat(i||Array.prototype.slice.call(t))},logs_1=(Object.defineProperty(exports,"__esModule",{value:!0}),exports.SubscriptionManager=void 0,require("../publications/logs")),app_status_1=require("../publications/app-status"),files_1=require("../publications/files"),super_admin_1=require("../publications/super-admin"),logged_in_users_collection_1=require("../collections/logged-in-users.collection"),cron_jobs_1=require("../publications/cron-jobs"),flags_1=require("../publications/flags"),method_responses_1=require("../publications/method-responses"),resolveio_server_app_1=require("../resolveio-server-app"),notifications_1=require("../publications/notifications"),report_builder_reports_1=require("../publications/report-builder-reports"),report_builder_libraries_1=require("../publications/report-builder-libraries"),user_groups_1=require("../publications/user-groups"),user_guides_1=require("../publications/user-guides"),report_builder_dashboard_builders_1=require("../publications/report-builder-dashboard-builders"),common_1=require("../util/common"),NodeCache=require("node-cache"),flag_collection_1=require("../collections/flag.collection"),os_1=require("os"),flags_update_1=require("../publications/flags-update"),numCPUs=(0,os_1.cpus)().length,v8=require("v8"),SubscriptionManager=function(){function SubscriptionManager(e,t,n){var i=this;this._publications={},this._subscriptions=[],this._loggedInUsers=[],this._mongoQueue=[],this._mongoQueueId=0,this._cacheId=1,this._heapSize=v8.getHeapStatistics()/numCPUs,this._enableDebug=!1,this._debugOplogCollections=[],this._debugOplogHits=0,this._debugSubCollections=[],this._debugSubHits=0,this._debugUnSubHits=0,this._debugUnSubAllHits=0,this._debugMongoQueueHits=0,this._debugMongoQueueCollections=[],this._debugSendQueueHits=0,this._debugRemoveCacheHits=0,this._oplogRetryCount=0,this.latencyBuffer=new Map,this.LATENCY_UPDATE_INTERVAL=6e4,this.LATENCY_UPDATE_THRESHOLD_MS=3e4,this._websocketManager=resolveio_server_app_1.ResolveIOServer.getMainServer().getWebSocketManager(),this._monitorManagerFunction=n,this._nodeCache=new NodeCache({stdTTL:0,checkperiod:0}),setInterval(function(){return i.flushThrottledLatencyUpdates()},this.LATENCY_UPDATE_INTERVAL),this.serverConfig=t,this._wss=e,(0,super_admin_1.loadSuperAdminPublications)(this),(0,app_status_1.loadAppStatusPublications)(this),(0,logs_1.loadLogPublications)(this),(0,files_1.loadFilePublications)(this),(0,cron_jobs_1.loadCronJobPublications)(this),(0,flags_update_1.loadFlagsUpdatePublications)(this),(0,flags_1.loadFlagsPublications)(this),(0,method_responses_1.loadMethodResponsePublications)(this),(0,notifications_1.loadNotificationPublications)(this),(0,report_builder_reports_1.loadReportBuilderReportPublications)(this),(0,report_builder_libraries_1.loadReportBuilderLibraryPublications)(this),(0,user_groups_1.loadUserGroupPublications)(this),(0,user_guides_1.loadUserGuidePublications)(this),(0,report_builder_dashboard_builders_1.loadReportBuilderDashboardBuilderPublications)(this),this.tailOpLog(),setInterval(function(){i._oplogRetryCount=0},15e3),setInterval(function(){i.getEnableDebug()&&(console.log(new Date,"Sub Manager","Subs",i._subscriptions.length),console.log(new Date,"Sub Manager","Logged In Users",i._loggedInUsers.length),console.log(new Date,"Sub Manager","Mongo Queue",i._mongoQueue.length),console.log(new Date,"Sub Manager","Mongo Queue Hits",i._debugMongoQueueHits),console.log(new Date,"Sub Manager","Mongo Queue Collections",JSON.stringify(i._debugMongoQueueCollections.sort(function(e,t){return e.collection.localeCompare(t.collection)||e.publication.localeCompare(t.publication)}),null,2)),console.log(new Date,"Sub Manager","Oplog Hits",i._debugOplogHits),console.log(new Date,"Sub Manager","Oplog Collections",JSON.stringify(i._debugOplogCollections.sort(function(e,t){return e.collection.localeCompare(t.collection)||e.type.localeCompare(t.type)}),null,2)),console.log(new Date,"Sub Manager","Send Queue Hits",i._debugSendQueueHits),console.log(new Date,"Sub Manager","Sub Hits",i._debugSubHits),console.log(new Date,"Sub Manager","Sub Collections",JSON.stringify(i._debugSubCollections.sort(function(e,t){return e.publication.localeCompare(t.publication)}),null,2)),console.log(new Date,"Sub Manager","Unsub Hits",i._debugUnSubHits),console.log(new Date,"Sub Manager","Unsub All Hits",i._debugUnSubAllHits),console.log(new Date,"Sub Manager","Cache Cleanup Hits",i._debugRemoveCacheHits)),i._debugOplogHits=0,i._debugOplogCollections=[],i._debugSubCollections=[],i._debugMongoQueueHits=0,i._debugMongoQueueCollections=[],i._debugSendQueueHits=0,i._debugSubHits=0,i._debugUnSubHits=0,i._debugUnSubAllHits=0,i._debugRemoveCacheHits=0},6e4),setInterval(function(){return __awaiter(i,void 0,void 0,function(){var t,i,n,o,s,r,a,l,c;return __generator(this,function(e){switch(e.label){case 0:return t=this,[4,logged_in_users_collection_1.LoggedInUsers.find()];case 1:for(t._loggedInUsers=e.sent(),i=(0,common_1.deepCopy)(this._loggedInUsers),n=function(e){var n=i[e];(!n.date||12e4<Date.now()-n.date.getTime())&&(o._websocketManager.getWebSocket(n.id_ws)?(o.getEnableDebug()&&console.log(new Date,"Sub Manager","Unsub WS",o._websocketManager.getWebSocket(n.id_ws).user,o._websocketManager.getWebSocket(n.id_ws).id_socket,2),o.unsubscribeAll(o._websocketManager.getWebSocket(n.id_ws))):(o._subscriptions.forEach(function(e){for(var t=e.clients.length-1;0<=t;t--)e.clients[t].id_socket===n.id_ws&&e.clients.splice(t,1)}),logged_in_users_collection_1.LoggedInUsers.deleteOne({_id:n._id}),0<=o._loggedInUsers.findIndex(function(e){return e._id===n._id})&&o._loggedInUsers.splice(o._loggedInUsers.findIndex(function(e){return e._id===n._id}),1)))},s=(o=this)._loggedInUsers.length-1;0<=s;s--)n(s);for(s=0;s<this._subscriptions.length;s++)for(r=this._subscriptions[s],a=function(e){var t=r.clients[e];l._loggedInUsers.some(function(e){return e.id_ws===t.id_socket})||r.clients.splice(e,1)},l=this,c=r.clients.length-1;0<=c;c--)a(c);return[2]}})})},3e4),flag_collection_1.Flags.findOne({type:"Enable Debug"}).then(function(e){e&&e.value?i._enableDebug=!0:i._enableDebug=!1}),this.setCacheLimit()}return SubscriptionManager.prototype.setCacheLimit=function(){"true"===process.env.IS_WORKERS_ENABLED?this._heapLimit=.4*this._heapSize:this._heapLimit=.3*this._heapSize},SubscriptionManager.prototype.invalidatePubsCache=function(u,g){return __awaiter(this,void 0,void 0,function(){var t,n,o,i,s,r,a,l,c=this;return __generator(this,function(e){resolveio_server_app_1.ResolveIOServer.getMongoManager().invalidateQueryCache(u),t=this._subscriptions.filter(function(e){return e.collections.includes(u)}),n=function(i){if(o._enableDebug&&console.log(new Date,"Invalidate Sub",i.publication,i.running,i.runAgain),i.running)return i.runAgain=!0,"continue";o._publications[i.publication].user_specific?Promise.all(i.clients.map(function(n){return __awaiter(c,void 0,void 0,function(){var t;return __generator(this,function(e){if((t=this._websocketManager.getWebSocket(n.id_socket))&&t.readyState===t.OPEN)try{this.sendDataToOneWithRetry(t,n.messageId,i,u,g)}catch(e){resolveio_server_app_1.ResolveIOServer.getMainServer().getMethodManager().sendEmail("dev@resolveio.com","SERVER - Error Detected - "+this.serverConfig.CLIENT_NAME,"Error Detected During sendDataToOne - User Specific - Socket: "+n.id_socket+", User: "+n.id_user+", MessageId: "+n.messageId+", Pub: "+i.publication+", Err: "+JSON.stringify(e,null,2))}return[2]})})})):o.sendDataToAllWithRetry(i,u,g)},o=this;try{for(i=__values(t),s=i.next();!s.done;s=i.next())r=s.value,n(r)}catch(e){a={error:e}}finally{try{s&&!s.done&&(l=i.return)&&l.call(i)}finally{if(a)throw a.error}}return[2]})})},SubscriptionManager.prototype.delay=function(t){return __awaiter(this,void 0,void 0,function(){return __generator(this,function(e){return[2,new Promise(function(e){return setTimeout(e,t)})]})})},SubscriptionManager.prototype.sendDataToAllWithRetry=function(t,n,i){return __awaiter(this,void 0,void 0,function(){return __generator(this,function(e){switch(e.label){case 0:t.running=!0,e.label=1;case 1:return this._enableDebug&&console.log(new Date,"Running sendDataToAll Sub",t.publication),t.runAgain=!1,[4,this.sendDataToAll(t,n,i)];case 2:return(e.sent(),this._enableDebug&&console.log(new Date,"Done sendDataToAll Sub",t.publication,t.runAgain),t.runAgain)?[4,this.delay(500)]:[3,4];case 3:e.sent(),e.label=4;case 4:if(t.runAgain)return[3,1];e.label=5;case 5:return t.running=!1,[2]}})})},SubscriptionManager.prototype.sendDataToOneWithRetry=function(t,n,i,o,s){return __awaiter(this,void 0,void 0,function(){return __generator(this,function(e){switch(e.label){case 0:i.running=!0,e.label=1;case 1:return this._enableDebug&&console.log(new Date,"Running sendDataToOne Sub",i.publication),i.runAgain=!1,[4,this.sendDataToOne(t,n,i,o,s)];case 2:return(e.sent(),this._enableDebug&&console.log(new Date,"Done sendDataToOne Sub",i.publication,i.runAgain),i.runAgain)?[4,this.delay(500)]:[3,4];case 3:e.sent(),e.label=4;case 4:if(i.runAgain)return[3,1];e.label=5;case 5:return i.running=!1,[2]}})})},SubscriptionManager.prototype.publications=function(e){this._publications=Object.assign(this._publications,e)},SubscriptionManager.prototype.loggedInLatency=function(t){var e,n,i,o=this._loggedInUsers.find(function(e){return e.id_ws===t.id_socket});o&&(e=new Date,n=this.latencyBuffer.get(t.id_socket),i=t.latency,!n||e.getTime()-n.lastUpdate.getTime()>=this.LATENCY_UPDATE_THRESHOLD_MS||100<Math.abs(i-n.latency))&&(o.date=e,this.latencyBuffer.set(t.id_socket,{latency:i,lastUpdate:e}))},SubscriptionManager.prototype.flushThrottledLatencyUpdates=function(){return __awaiter(this,void 0,void 0,function(){var t,n;return __generator(this,function(e){switch(e.label){case 0:if(0===this.latencyBuffer.size)return[2];t=Array.from(this.latencyBuffer.entries()).map(function(e){var e=__read(e,2),t=e[0],e=e[1];return{updateOne:{filter:{id_ws:t},update:{$set:{latency:e.latency,date:e.lastUpdate}}}}}),e.label=1;case 1:return e.trys.push([1,3,,4]),[4,logged_in_users_collection_1.LoggedInUsers.bulkWrite(t)];case 2:return e.sent(),this.latencyBuffer.clear(),this.getEnableDebug()&&console.log(new Date,"Sub Manager","Throttled latency batch update successful",t.length),[3,4];case 3:return n=e.sent(),console.error(new Date,"Sub Manager","Throttled latency batch update failed",n),[3,4];case 4:return[2]}})})},SubscriptionManager.prototype.subscribe=function(t,e,n,i,o,s){var r=this,a=(this._debugSubHits+=1,this._debugSubCollections.some(function(e){return e.publication===o})?this._debugSubCollections.find(function(e){return e.publication===o}).hits+=1:this._debugSubCollections.push({publication:o,hits:1}),this._publications[o]);if(a){if(1<s.length||s[0]){if(!a.check)return void console.error(new Date,"No Check Function For Pub "+o);if(!a.check._schema)return void console.error(new Date,"No Check Schema For Pub "+o);for(var l={},c=Object.keys(a.check._schema).filter(function(e){return!e.includes(".")}),u=0;u<s.length;u++)l[c[u]]=s[u];try{a.check.validate(l)}catch(e){if(e)return void console.error(new Date,"Error in Pub Check ("+o+")",e)}}"Bypass"!==t&&(a=t.split("/"),g="",_=a[0],""===a[0]&&(g="/",_=a[1]),g+=_,1<a.length&&(g+="/"),(_=this._subscriptions.filter(function(e){return e.clients.some(function(e){return e.id_socket===n.id_socket&&"Bypass"!==e.messageRoute&&"/"!==e.messageRoute&&e.messageRoute!==t&&!e.messageRoute.startsWith(g)})})).length)&&(resolveio_server_app_1.ResolveIOServer.getMainServer().getMethodManager().sendEmail("dev@resolveio.com","SERVER - Detected Undestroyed Subscription - "+this.serverConfig.CLIENT_NAME,"USER: "+n.user+" (Socket: "+n.id_socket+") is on route: "+t+" but has the following subscriptions on other routes:"+JSON.stringify(_,null,2)),_.forEach(function(t){t.clients.filter(function(e){return e.id_socket===n.id_socket}).forEach(function(e){r.unsubscribe(e.messageRoute,new Date,n,e.messageId,t.publication,t.subscriptionData)})}));var g,_,a=this._subscriptions.find(function(e){return e.publication===o&&JSON.stringify(e.subscriptionData)===JSON.stringify(s)});a?a.clients.some(function(e){return e.id_socket===n.id_socket&&e.messageId===i})||a.clients.push({id_user:n.id_user,messageId:i,id_socket:n.id_socket,messageRoute:t}):this._subscriptions.push({publication:o,subscriptionData:s,collections:this.getPublicationCollections(o),clients:[{id_user:n.id_user,messageId:i,id_socket:n.id_socket,messageRoute:t}],cacheId:0,running:!1,runAgain:!1}),a=a||this._subscriptions.find(function(e){return e.publication===o&&JSON.stringify(e.subscriptionData)===JSON.stringify(s)}),this._enableDebug&&console.log(new Date,"New Sub",a.publication,a.running,a.runAgain,a.clients.length),this.processSubscription(a,n,i)}else console.error(new Date,"No Publication: "+o)},SubscriptionManager.prototype.createLoggedInUser=function(i){return __awaiter(this,void 0,void 0,function(){var t=this;return __generator(this,function(e){return[2,new Promise(function(n,e){return __awaiter(t,void 0,void 0,function(){var t;return __generator(this,function(e){return(t=this._websocketManager.getWebSocket(i))?(t={_id:(0,common_1.objectIdHexString)(),__v:0,date:new Date,id_user:t.id_user,user:t.user,id_ws:t.id_socket},this._loggedInUsers.push(t),logged_in_users_collection_1.LoggedInUsers.insertOne(t),n(t)):n(null),[2]})})})]})})},SubscriptionManager.prototype.unsubscribe=function(e,t,n,i,o,s){if(this._debugUnSubHits+=1,this._publications[o]){var r=this._subscriptions.find(function(e){return e.publication===o&&JSON.stringify(e.subscriptionData)===JSON.stringify(s)});if(r){for(var a=r.clients.length-1;0<=a;a--)r.clients[a].id_user===n.id_user&&r.clients[a].messageId===i&&r.clients[a].id_socket===n.id_socket&&r.clients.splice(a,1);this._enableDebug&&console.log(new Date,"Unsub",r.publication,r.running,r.runAgain,r.clients.length)}}else console.log("No Publication: "+o)},SubscriptionManager.prototype.unsubscribeAll=function(s){return __awaiter(this,void 0,void 0,function(){var t,n,i,o;return __generator(this,function(e){if(this._debugUnSubAllHits+=1,s){if(s.isUnsubscribed)return[2];for(s.isUnsubscribed=!0,0<=this._loggedInUsers.map(function(e){return e.id_ws}).indexOf(s.id_socket)&&this._loggedInUsers.splice(this._loggedInUsers.map(function(e){return e.id_ws}).indexOf(s.id_socket),1),logged_in_users_collection_1.LoggedInUsers.deleteOne({id_ws:s.id_socket}),t=this._subscriptions.filter(function(e){return e.clients.some(function(e){return e.id_user===s.id_user&&e.id_socket===s.id_socket})}),n=t.length-1;0<=n;n--)for(i=t[n],o=i.clients.length-1;0<=o;o--)i.clients[o].id_socket===s.id_socket&&i.clients.splice(o,1);this._websocketManager.removeWebSocket(s)}return[2]})})},SubscriptionManager.prototype.getActiveSubscriptions=function(){return this._subscriptions},SubscriptionManager.prototype.getPublicationCollections=function(e){return this._publications[e].collections},SubscriptionManager.prototype.tailOpLog=function(o){return __awaiter(this,void 0,void 0,function(){var t,n,i=this;return __generator(this,function(e){switch(e.label){case 0:return this._oplog$&&!this._oplog$.closed&&(this._oplog$.removeAllListeners(),this._oplog$.close(),this._oplog$=null),[4,new Promise(function(e){return setTimeout(e,1e3)})];case 1:if(e.sent(),!this._oplog$||this._oplog$.closed){if(this._oplogRetryCount+=1,5<this._oplogRetryCount&&(console.error("****************** TAIL OPLOG ERROR, RETRYING A BUNCH OF TIMES, KILLING PROCESS **************************"),process.exit(1)),t=[{$match:{$and:[{"ns.coll":{$nin:["logs","log-method-latencies","log-subscriptions"]}},{"ns.coll":{$not:/.*\.versions$/}},{"ns.coll":{$not:/^monitor-/}}]}}],o){n=o;try{this._oplog$=resolveio_server_app_1.ResolveIOServer.getMainDB().watch(t,{resumeAfter:o})}catch(e){return this._oplog$&&(this._oplog$.removeAllListeners(),this._oplog$.close(),this._oplog$=null),this.tailOpLog(o),[2]}}else this._oplog$=resolveio_server_app_1.ResolveIOServer.getMainDB().watch(t);console.log(new Date,"oplog started"),this._oplog$.on("change",function(t){var e;t.ns&&(i._enableDebug&&console.log(new Date,"Oplog Hit",t.ns),e=t.ns.coll,i._debugOplogCollections.some(function(e){return e.collection===t.ns.coll&&e.type===t.operationType})?i._debugOplogCollections.find(function(e){return e.collection===t.ns.coll&&e.type===t.operationType}).hits+=1:i._debugOplogCollections.push({collection:t.ns.coll,type:t.operationType,hits:1}),e&&(i._debugOplogHits+=1,"insert"===t.operationType?("support-tickets"===e&&"https://resolveio.com"===i.serverConfig.ROOT_URL&&(resolveio_server_app_1.ResolveIOServer.getMainServer().getMethodManager().callMethodInternal.call(resolveio_server_app_1.ResolveIOServer.getMainServer().getMethodManager(),"sendSupportTicketEmail",t.documentKey._id),i.invalidatePubsCache(e,"insert")),"method-responses"!==e&&i.invalidatePubsCache(e,"insert")):"update"===t.operationType||"replace"===t.operationType?"method-responses"!==e&&i.invalidatePubsCache(e,"update"):"delete"===t.operationType&&"method-responses"!==e&&i.invalidatePubsCache(e,"delete")),"flags"===e&&flag_collection_1.Flags.findOne({type:"Enable Debug"}).then(function(e){e&&e.value?i._enableDebug=!0:i._enableDebug=!1}),n=t._id,process.env.NODE_APP_INSTANCE&&"0"!==process.env.NODE_APP_INSTANCE||"false"!==process.env.IS_WORKERS_ENABLED&&"true"===process.env.IS_WORKER_INSTANCE&&process.env.WORKER_INDEX)}),this._oplog$.on("error",function(e){console.log(new Date,"oplog error",e),i._oplog$.removeAllListeners(),i._oplog$.close(),i._oplog$=null,i.tailOpLog(n)}),this._oplog$.on("end",function(){console.log(new Date,"oplog end"),i._oplog$.removeAllListeners(),i._oplog$.close(),i._oplog$=null,i.tailOpLog(n)}),this._oplog$.on("close",function(){console.log(new Date,"oplog close"),i._oplog$.removeAllListeners(),i._oplog$=null,i.tailOpLog(n)})}return[2]}})})},SubscriptionManager.prototype.processSubscription=function(i,o,s){return __awaiter(this,void 0,void 0,function(){var t,n;return __generator(this,function(e){if(this._publications[i.publication].user_specific){if(this._enableDebug&&console.log(new Date,"Process Sub Specific, Non - Cache",i.publication,i.running),i.running)return[2];this.sendDataToOneWithRetry(o,s,i,"","newSub")}else if(i.cacheId)try{t=JSON.parse(this._nodeCache.get(i.cacheId),common_1.dateReviver),n={messageId:s,hasError:!1,data:t},this._enableDebug&&console.log(new Date,"Process Sub, Cache",i.publication),this.sendWS(o,n)}catch(e){this._nodeCache.del(i.cacheId),i.cacheId=0,this.sendDataToAllWithRetry(i,"","newSub")}else{if(this._enableDebug&&console.log(new Date,"Process Sub, Non - Cache",i.publication,i.running),i.running)return[2];this.sendDataToAllWithRetry(i,"","newSub")}return[2]})})},SubscriptionManager.prototype.sendDataToOne=function(s,r,a,l,c){return __awaiter(this,void 0,void 0,function(){var t,n,i,o;return __generator(this,function(e){switch(e.label){case 0:t=this._monitorManagerFunction.startMonitorFunction("User Specific Publication",a.publication,"","",a.subscriptionData),e.label=1;case 1:return e.trys.push([1,3,,4]),resolveio_server_app_1.ResolveIOServer.getMainServer().getMethodManager().callMethodInternal.call(resolveio_server_app_1.ResolveIOServer.getMainServer().getMethodManager(),"insertSubscriptionLog",c,a.publication,l,JSON.stringify(a.subscriptionData)),[4,(o=this._publications[a.publication].function).call.apply(o,__spreadArray([Object.assign({},this,SubscriptionManager.prototype),s.id_user],__read(a.subscriptionData),!1))];case 2:return o=e.sent(),this._monitorManagerFunction.finishMonitorFunction(t),i={messageId:r,hasError:!1,data:o},this.sendWS(s,i),[3,4];case 3:return n=e.sent(),this._monitorManagerFunction.finishMonitorFunction(t),i={messageId:r,hasError:!0,data:n},this.sendWS(s,i),resolveio_server_app_1.ResolveIOServer.getMainServer().getMethodManager().sendEmail("dev@resolveio.com","SERVER - Error Detected - "+this.serverConfig.CLIENT_NAME,"Error Detected During Subscription "+a.publication+" - (sendDataToOne - WS)\n\nData \n"+JSON.stringify(a.subscriptionData,null,2)+"\n\nErrors\n"+JSON.stringify(n,null,2)),[3,4];case 4:return[2]}})})},SubscriptionManager.prototype.sendDataToAll=function(g,_,d){return __awaiter(this,void 0,void 0,function(){var t,o,n,i,s,r,a,l,c,u=this;return __generator(this,function(e){switch(e.label){case 0:return g.clients.length?[3,1]:(g.cacheId&&(this._nodeCache.del(g.cacheId),g.cacheId=0),0<=(c=this._subscriptions.findIndex(function(e){return e.publication===g.publication&&JSON.stringify(e.subscriptionData)===JSON.stringify(g.subscriptionData)}))&&this._subscriptions.splice(c,1),[2]);case 1:t=this._monitorManagerFunction.startMonitorFunction("Publication",g.publication,"","",g.subscriptionData),e.label=2;case 2:return e.trys.push([2,4,,5]),"superadminAPM"!==g.publication&&"loggedInUsers"!==g.publication&&resolveio_server_app_1.ResolveIOServer.getMainServer().getMethodManager().callMethodInternal.call(resolveio_server_app_1.ResolveIOServer.getMainServer().getMethodManager(),"insertSubscriptionLog",d,g.publication,_,JSON.stringify(g.subscriptionData)),[4,(c=this._publications[g.publication].function).call.apply(c,__spreadArray([Object.assign({},this,SubscriptionManager.prototype)],__read(g.subscriptionData),!1))];case 3:if(o=e.sent(),this._monitorManagerFunction.finishMonitorFunction(t),g.cacheId)try{n=JSON.parse(this._nodeCache.get(g.cacheId),common_1.dateReviver),JSON.stringify(n)!==JSON.stringify(o)&&(Promise.all(g.clients.map(function(i){return __awaiter(u,void 0,void 0,function(){var t,n;return __generator(this,function(e){return(t=this._websocketManager.getWebSocket(i.id_socket))&&t.readyState===t.OPEN&&(n={messageId:i.messageId,hasError:!1,data:o},this.sendWS(t,n)),[2]})})})),this._nodeCache.del(g.cacheId),!((0,common_1.getBinarySize)(JSON.stringify(o))<1e6)||g.collections.includes("logs")||g.collections.includes("log-method-latencies")||g.collections.includes("log-subscriptions")||g.collections.find(function(e){return e.endsWith(".versions")})||g.collections.find(function(e){return e.startsWith("monitor-")})?g.cacheId=0:this._nodeCache.set(g.cacheId,JSON.stringify(o)))}catch(e){Promise.all(g.clients.map(function(i){return __awaiter(u,void 0,void 0,function(){var t,n;return __generator(this,function(e){return(t=this._websocketManager.getWebSocket(i.id_socket))&&t.readyState===t.OPEN&&(n={messageId:i.messageId,hasError:!1,data:o},this.sendWS(t,n)),[2]})})})),this._nodeCache.del(g.cacheId),!((0,common_1.getBinarySize)(JSON.stringify(o))<1e6)||g.collections.includes("logs")||g.collections.includes("log-method-latencies")||g.collections.includes("log-subscriptions")||g.collections.find(function(e){return e.endsWith(".versions")})||g.collections.find(function(e){return e.startsWith("monitor-")})?g.cacheId=0:this._nodeCache.set(g.cacheId,JSON.stringify(o))}else if(Promise.all(g.clients.map(function(i){return __awaiter(u,void 0,void 0,function(){var t,n;return __generator(this,function(e){return(t=this._websocketManager.getWebSocket(i.id_socket))&&t.readyState===t.OPEN&&(n={messageId:i.messageId,hasError:!1,data:o},this.sendWS(t,n)),[2]})})})),!((0,common_1.getBinarySize)(JSON.stringify(o))<1e6)||g.collections.includes("logs")||g.collections.includes("log-method-latencies")||g.collections.includes("log-subscriptions")||g.collections.find(function(e){return e.endsWith(".versions")})||g.collections.find(function(e){return e.startsWith("monitor-")}))g.cacheId=0;else if(g.cacheId=this._cacheId++,this._nodeCache.set(g.cacheId,JSON.stringify(o)),(i=this._nodeCache.getStats().vsize)>this._heapLimit){for(s=0,r=this._subscriptions.filter(function(e){return e.cacheId&&!e.clients.length}),a=0;a<r.length&&(this._debugRemoveCacheHits+=1,this._nodeCache.del(r[a].cacheId),r[a].cacheId=0,s+=1,!(this._nodeCache.getStats().vsize<.75*this._heapLimit));a++);this._enableDebug&&console.log("Sub Cache: Too Big - "+g.publication+" - Deleted: "+s+" - "+i)}return[3,5];case 4:return l=e.sent(),this._monitorManagerFunction.finishMonitorFunction(t),Promise.all(g.clients.map(function(i){return __awaiter(u,void 0,void 0,function(){var t,n;return __generator(this,function(e){return(t=this._websocketManager.getWebSocket(i.id_socket))&&t.readyState===t.OPEN&&(n={messageId:i.messageId,hasError:!0,data:l},this.sendWS(t,n)),[2]})})})),resolveio_server_app_1.ResolveIOServer.getMainServer().getMethodManager().sendEmail("dev@resolveio.com","SERVER - Error Detected - "+this.serverConfig.CLIENT_NAME,"Error Detected During Subscription "+g.publication+" - (sendPubData)\n\nData \n"+JSON.stringify(g.subscriptionData,null,2)+"\n\nErrors\n"+JSON.stringify(l,null,2)),[3,5];case 5:return[2]}})})},SubscriptionManager.prototype.sendWS=function(e,t){this._websocketManager.send(e,t)},SubscriptionManager.prototype.getEnableDebug=function(){return this._enableDebug},SubscriptionManager}();exports.SubscriptionManager=SubscriptionManager;
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"),resolveio_server_app_1=require("../resolveio-server-app"),notifications_1=require("../publications/notifications"),report_builder_reports_1=require("../publications/report-builder-reports"),report_builder_libraries_1=require("../publications/report-builder-libraries"),user_groups_1=require("../publications/user-groups"),user_guides_1=require("../publications/user-guides"),report_builder_dashboard_builders_1=require("../publications/report-builder-dashboard-builders"),common_1=require("../util/common"),NodeCache=require("node-cache"),flag_collection_1=require("../collections/flag.collection"),os_1=require("os"),flags_update_1=require("../publications/flags-update"),numCPUs=(0,os_1.cpus)().length,v8=require("v8"),SubscriptionManager=function(){function SubscriptionManager(e,t,n){var i=this;this._publications={},this._subscriptions=[],this._loggedInUsers=[],this._mongoQueue=[],this._mongoQueueId=0,this._cacheId=1,this._heapSize=v8.getHeapStatistics()/numCPUs,this._enableDebug=!1,this._debugOplogCollections=[],this._debugOplogHits=0,this._debugSubCollections=[],this._debugSubHits=0,this._debugUnSubHits=0,this._debugUnSubAllHits=0,this._debugMongoQueueHits=0,this._debugMongoQueueCollections=[],this._debugSendQueueHits=0,this._debugRemoveCacheHits=0,this._oplogRetryCount=0,this.latencyBuffer=new Map,this.LATENCY_UPDATE_INTERVAL=6e4,this.LATENCY_UPDATE_THRESHOLD_MS=3e4,this._websocketManager=resolveio_server_app_1.ResolveIOServer.getMainServer().getWebSocketManager(),this._monitorManagerFunction=n,this._nodeCache=new NodeCache({stdTTL:0,checkperiod:0}),setInterval(function(){return i.flushThrottledLatencyUpdates()},this.LATENCY_UPDATE_INTERVAL),this.serverConfig=t,this._wss=e,(0,super_admin_1.loadSuperAdminPublications)(this),(0,app_status_1.loadAppStatusPublications)(this),(0,logs_1.loadLogPublications)(this),(0,files_1.loadFilePublications)(this),(0,cron_jobs_1.loadCronJobPublications)(this),(0,flags_update_1.loadFlagsUpdatePublications)(this),(0,flags_1.loadFlagsPublications)(this),(0,method_responses_1.loadMethodResponsePublications)(this),(0,notifications_1.loadNotificationPublications)(this),(0,report_builder_reports_1.loadReportBuilderReportPublications)(this),(0,report_builder_libraries_1.loadReportBuilderLibraryPublications)(this),(0,user_groups_1.loadUserGroupPublications)(this),(0,user_guides_1.loadUserGuidePublications)(this),(0,report_builder_dashboard_builders_1.loadReportBuilderDashboardBuilderPublications)(this),this.tailOpLog(),setInterval(function(){i._oplogRetryCount=0},15e3),setInterval(function(){i.getEnableDebug()&&(console.log(new Date,"Sub Manager","Subs",i._subscriptions.length),console.log(new Date,"Sub Manager","Logged In Users",i._loggedInUsers.length),console.log(new Date,"Sub Manager","Mongo Queue",i._mongoQueue.length),console.log(new Date,"Sub Manager","Mongo Queue Hits",i._debugMongoQueueHits),console.log(new Date,"Sub Manager","Mongo Queue Collections",JSON.stringify(i._debugMongoQueueCollections.sort(function(e,t){return e.collection.localeCompare(t.collection)||e.publication.localeCompare(t.publication)}),null,2)),console.log(new Date,"Sub Manager","Oplog Hits",i._debugOplogHits),console.log(new Date,"Sub Manager","Oplog Collections",JSON.stringify(i._debugOplogCollections.sort(function(e,t){return e.collection.localeCompare(t.collection)||e.type.localeCompare(t.type)}),null,2)),console.log(new Date,"Sub Manager","Send Queue Hits",i._debugSendQueueHits),console.log(new Date,"Sub Manager","Sub Hits",i._debugSubHits),console.log(new Date,"Sub Manager","Sub Collections",JSON.stringify(i._debugSubCollections.sort(function(e,t){return e.publication.localeCompare(t.publication)}),null,2)),console.log(new Date,"Sub Manager","Unsub Hits",i._debugUnSubHits),console.log(new Date,"Sub Manager","Unsub All Hits",i._debugUnSubAllHits),console.log(new Date,"Sub Manager","Cache Cleanup Hits",i._debugRemoveCacheHits)),i._debugOplogHits=0,i._debugOplogCollections=[],i._debugSubCollections=[],i._debugMongoQueueHits=0,i._debugMongoQueueCollections=[],i._debugSendQueueHits=0,i._debugSubHits=0,i._debugUnSubHits=0,i._debugUnSubAllHits=0,i._debugRemoveCacheHits=0},6e4),setInterval(function(){return __awaiter(i,void 0,void 0,function(){var t,i,n,o,s,r,a,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){resolveio_server_app_1.ResolveIOServer.getMongoManager().invalidateQueryCache(u),t=this._subscriptions.filter(function(e){return e.collections.includes(u)}),n=function(i){if(o._enableDebug&&console.log(new Date,"Invalidate Sub",i.publication,i.running,i.runAgain),i.running)return i.runAgain=!0,"continue";o._publications[i.publication].user_specific?Promise.all(i.clients.map(function(n){return __awaiter(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){resolveio_server_app_1.ResolveIOServer.getMainServer().getMethodManager().sendEmail("dev@resolveio.com","SERVER - Error Detected - "+this.serverConfig.CLIENT_NAME,"Error Detected During sendDataToOne - User Specific - Socket: "+n.id_socket+", User: "+n.id_user+", MessageId: "+n.messageId+", Pub: "+i.publication+", Err: "+JSON.stringify(e,null,2))}return[2]})})})):o.sendDataToAllWithRetry(i,u,g)},o=this;try{for(i=__values(t),s=i.next();!s.done;s=i.next())r=s.value,n(r)}catch(e){a={error:e}}finally{try{s&&!s.done&&(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="",_=a[0],""===a[0]&&(g="/",_=a[1]),g+=_,1<a.length&&(g+="/"),(_=this._subscriptions.filter(function(e){return e.clients.some(function(e){return e.id_socket===n.id_socket&&"Bypass"!==e.messageRoute&&"/"!==e.messageRoute&&e.messageRoute!==t&&!e.messageRoute.startsWith(g)})})).length)&&(resolveio_server_app_1.ResolveIOServer.getMainServer().getMethodManager().sendEmail("dev@resolveio.com","SERVER - Detected Undestroyed Subscription - "+this.serverConfig.CLIENT_NAME,"USER: "+n.user+" (Socket: "+n.id_socket+") is on route: "+t+" but has the following subscriptions on other routes:"+JSON.stringify(_,null,2)),_.forEach(function(t){t.clients.filter(function(e){return e.id_socket===n.id_socket}).forEach(function(e){r.unsubscribe(e.messageRoute,new Date,n,e.messageId,t.publication,t.subscriptionData)})}));var g,_,a=this._subscriptions.find(function(e){return e.publication===o&&JSON.stringify(e.subscriptionData)===JSON.stringify(s)});a?a.clients.some(function(e){return e.id_socket===n.id_socket&&e.messageId===i})||a.clients.push({id_user:n.id_user,messageId:i,id_socket:n.id_socket,messageRoute:t}):this._subscriptions.push({publication:o,subscriptionData:s,collections:this.getPublicationCollections(o),clients:[{id_user:n.id_user,messageId:i,id_socket:n.id_socket,messageRoute:t}],cacheId:0,running:!1,runAgain:!1}),a=a||this._subscriptions.find(function(e){return e.publication===o&&JSON.stringify(e.subscriptionData)===JSON.stringify(s)}),this._enableDebug&&console.log(new Date,"New Sub",a.publication,a.running,a.runAgain,a.clients.length),this.processSubscription(a,n,i)}else console.error(new Date,"No Publication: "+o)},SubscriptionManager.prototype.createLoggedInUser=function(i){return __awaiter(this,void 0,void 0,function(){var t=this;return __generator(this,function(e){return[2,new Promise(function(n,e){return __awaiter(t,void 0,void 0,function(){var t;return __generator(this,function(e){return(t=this._websocketManager.getWebSocket(i))?(t={_id:(0,common_1.objectIdHexString)(),__v:0,date:new Date,id_user:t.id_user,user:t.user,id_ws:t.id_socket},this._loggedInUsers.push(t),logged_in_users_collection_1.LoggedInUsers.insertOne(t),n(t)):n(null),[2]})})})]})})},SubscriptionManager.prototype.unsubscribe=function(e,t,n,i,o,s){if(this._debugUnSubHits+=1,this._publications[o]){var r=this._subscriptions.find(function(e){return e.publication===o&&JSON.stringify(e.subscriptionData)===JSON.stringify(s)});if(r){for(var a=r.clients.length-1;0<=a;a--)r.clients[a].id_user===n.id_user&&r.clients[a].messageId===i&&r.clients[a].id_socket===n.id_socket&&r.clients.splice(a,1);this._enableDebug&&console.log(new Date,"Unsub",r.publication,r.running,r.runAgain,r.clients.length)}}else console.log("No Publication: "+o)},SubscriptionManager.prototype.unsubscribeAll=function(s){return __awaiter(this,void 0,void 0,function(){var t,n,i,o;return __generator(this,function(e){if(this._debugUnSubAllHits+=1,s){if(s.isUnsubscribed)return[2];for(s.isUnsubscribed=!0,0<=this._loggedInUsers.map(function(e){return e.id_ws}).indexOf(s.id_socket)&&this._loggedInUsers.splice(this._loggedInUsers.map(function(e){return e.id_ws}).indexOf(s.id_socket),1),logged_in_users_collection_1.LoggedInUsers.deleteOne({id_ws:s.id_socket}),t=this._subscriptions.filter(function(e){return e.clients.some(function(e){return e.id_user===s.id_user&&e.id_socket===s.id_socket})}),n=t.length-1;0<=n;n--)for(i=t[n],o=i.clients.length-1;0<=o;o--)i.clients[o].id_socket===s.id_socket&&i.clients.splice(o,1);this._websocketManager.removeWebSocket(s)}return[2]})})},SubscriptionManager.prototype.getActiveSubscriptions=function(){return this._subscriptions},SubscriptionManager.prototype.getPublicationCollections=function(e){return this._publications[e].collections},SubscriptionManager.prototype.tailOpLog=function(o){return __awaiter(this,void 0,void 0,function(){var t,n,i=this;return __generator(this,function(e){switch(e.label){case 0:return this._oplog$&&!this._oplog$.closed&&(this._oplog$.removeAllListeners(),this._oplog$.close(),this._oplog$=null),[4,new Promise(function(e){return setTimeout(e,1e3)})];case 1:if(e.sent(),!this._oplog$||this._oplog$.closed){if(this._oplogRetryCount+=1,5<this._oplogRetryCount&&(console.error("****************** TAIL OPLOG ERROR, RETRYING A BUNCH OF TIMES, KILLING PROCESS **************************"),process.exit(1)),t=[{$match:{$and:[{"ns.coll":{$nin:["logs"]}},{"ns.coll":{$not:/.*\.versions$/}},{"ns.coll":{$not:/^monitor-/}}]}}],o){n=o;try{this._oplog$=resolveio_server_app_1.ResolveIOServer.getMainDB().watch(t,{resumeAfter:o})}catch(e){return this._oplog$&&(this._oplog$.removeAllListeners(),this._oplog$.close(),this._oplog$=null),this.tailOpLog(o),[2]}}else this._oplog$=resolveio_server_app_1.ResolveIOServer.getMainDB().watch(t);console.log(new Date,"oplog started"),this._oplog$.on("change",function(t){var e;t.ns&&(i._enableDebug&&console.log(new Date,"Oplog Hit",t.ns),e=t.ns.coll,i._debugOplogCollections.some(function(e){return e.collection===t.ns.coll&&e.type===t.operationType})?i._debugOplogCollections.find(function(e){return e.collection===t.ns.coll&&e.type===t.operationType}).hits+=1:i._debugOplogCollections.push({collection:t.ns.coll,type:t.operationType,hits:1}),e&&(i._debugOplogHits+=1,"insert"===t.operationType?("support-tickets"===e&&"https://resolveio.com"===i.serverConfig.ROOT_URL&&(resolveio_server_app_1.ResolveIOServer.getMainServer().getMethodManager().callMethod.call(resolveio_server_app_1.ResolveIOServer.getMainServer().getMethodManager(),"sendSupportTicketEmail",t.documentKey._id),i.invalidatePubsCache(e,"insert")),"method-responses"!==e&&i.invalidatePubsCache(e,"insert")):"update"===t.operationType||"replace"===t.operationType?"method-responses"!==e&&i.invalidatePubsCache(e,"update"):"delete"===t.operationType&&"method-responses"!==e&&i.invalidatePubsCache(e,"delete")),"flags"===e&&flag_collection_1.Flags.findOne({type:"Enable Debug"}).then(function(e){e&&e.value?i._enableDebug=!0:i._enableDebug=!1}),n=t._id,process.env.NODE_APP_INSTANCE&&"0"!==process.env.NODE_APP_INSTANCE||"false"!==process.env.IS_WORKERS_ENABLED&&"true"===process.env.IS_WORKER_INSTANCE&&process.env.WORKER_INDEX)}),this._oplog$.on("error",function(e){console.log(new Date,"oplog error",e),i._oplog$.removeAllListeners(),i._oplog$.close(),i._oplog$=null,i.tailOpLog(n)}),this._oplog$.on("end",function(){console.log(new Date,"oplog end"),i._oplog$.removeAllListeners(),i._oplog$.close(),i._oplog$=null,i.tailOpLog(n)}),this._oplog$.on("close",function(){console.log(new Date,"oplog close"),i._oplog$.removeAllListeners(),i._oplog$=null,i.tailOpLog(n)})}return[2]}})})},SubscriptionManager.prototype.processSubscription=function(i,o,s){return __awaiter(this,void 0,void 0,function(){var t,n;return __generator(this,function(e){if(this._publications[i.publication].user_specific){if(this._enableDebug&&console.log(new Date,"Process Sub Specific, Non - Cache",i.publication,i.running),i.running)return[2];this.sendDataToOneWithRetry(o,s,i,"","newSub")}else if(i.cacheId)try{t=JSON.parse(this._nodeCache.get(i.cacheId),common_1.dateReviver),n={messageId:s,hasError:!1,data:t},this._enableDebug&&console.log(new Date,"Process Sub, Cache",i.publication),this.sendWS(o,n)}catch(e){this._nodeCache.del(i.cacheId),i.cacheId=0,this.sendDataToAllWithRetry(i,"","newSub")}else{if(this._enableDebug&&console.log(new Date,"Process Sub, Non - Cache",i.publication,i.running),i.running)return[2];this.sendDataToAllWithRetry(i,"","newSub")}return[2]})})},SubscriptionManager.prototype.sendDataToOne=function(s,r,a,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]),resolveio_server_app_1.ResolveIOServer.getMainServer().getMethodManager().callMethod.call(resolveio_server_app_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),resolveio_server_app_1.ResolveIOServer.getMainServer().getMethodManager().sendEmail("dev@resolveio.com","SERVER - Error Detected - "+this.serverConfig.CLIENT_NAME,"Error Detected During Subscription "+a.publication+" - (sendDataToOne - WS)\n\nData \n"+JSON.stringify(a.subscriptionData,null,2)+"\n\nErrors\n"+JSON.stringify(n,null,2)),[3,4];case 4:return[2]}})})},SubscriptionManager.prototype.sendDataToAll=function(g,_,d){return __awaiter(this,void 0,void 0,function(){var t,o,n,i,s,r,a,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&&resolveio_server_app_1.ResolveIOServer.getMainServer().getMethodManager().callMethod.call(resolveio_server_app_1.ResolveIOServer.getMainServer().getMethodManager(),"insertSubscriptionLog",d,g.publication,_,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.find(function(e){return e.endsWith(".versions")})&&!g.collections.find(function(e){return e.startsWith("monitor-")})?this._nodeCache.set(g.cacheId,JSON.stringify(o)):g.cacheId=0)}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.find(function(e){return e.endsWith(".versions")})&&!g.collections.find(function(e){return e.startsWith("monitor-")})?this._nodeCache.set(g.cacheId,JSON.stringify(o)):g.cacheId=0}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.find(function(e){return e.endsWith(".versions")})&&!g.collections.find(function(e){return e.startsWith("monitor-")})){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)}}else g.cacheId=0;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]})})})),resolveio_server_app_1.ResolveIOServer.getMainServer().getMethodManager().sendEmail("dev@resolveio.com","SERVER - Error Detected - "+this.serverConfig.CLIENT_NAME,"Error Detected During Subscription "+g.publication+" - (sendPubData)\n\nData \n"+JSON.stringify(g.subscriptionData,null,2)+"\n\nErrors\n"+JSON.stringify(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