@resolveio/server-lib 20.3.28 → 20.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,2 +1,2 @@
1
- "use strict";var __awaiter=this&&this.__awaiter||function(t,o,i,r){return new(i=i||Promise)(function(n,e){function fulfilled(t){try{step(r.next(t))}catch(t){e(t)}}function rejected(t){try{step(r.throw(t))}catch(t){e(t)}}function step(t){var e;t.done?n(t.value):((e=t.value)instanceof i?e:new i(function(t){t(e)})).then(fulfilled,rejected)}step((r=r.apply(t,o||[])).next())})},__generator=this&&this.__generator||function(o,i){var r,a,s,c={label:0,sent:function(){if(1&s[0])throw s[1];return s[1]},trys:[],ops:[]},u={next:verb(0),throw:verb(1),return:verb(2)};return"function"==typeof Symbol&&(u[Symbol.iterator]=function(){return this}),u;function verb(n){return function(t){var e=[n,t];if(r)throw new TypeError("Generator is already executing.");for(;c=u&&e[u=0]?0:c;)try{if(r=1,a&&(s=2&e[0]?a.return:e[0]?a.throw||((s=a.return)&&s.call(a),0):a.next)&&!(s=s.call(a,e[1])).done)return s;switch(a=0,(e=s?[2&e[0],s.value]:e)[0]){case 0:case 1:s=e;break;case 4:return c.label++,{value:e[1],done:!1};case 5:c.label++,a=e[1],e=[0];continue;case 7:e=c.ops.pop(),c.trys.pop();continue;default:if(!(s=0<(s=c.trys).length&&s[s.length-1])&&(6===e[0]||2===e[0])){c=0;continue}if(3===e[0]&&(!s||e[1]>s[0]&&e[1]<s[3]))c.label=e[1];else if(6===e[0]&&c.label<s[1])c.label=s[1],s=e;else{if(!(s&&c.label<s[2])){s[2]&&c.ops.pop(),c.trys.pop();continue}c.label=s[2],c.ops.push(e)}}e=i.call(o,c)}catch(t){e=[6,t],a=0}finally{r=s=0}if(5&e[0])throw e[1];return{value:e[0]?e[1]:void 0,done:!0}}}},axios_1=(Object.defineProperty(exports,"__esModule",{value:!0}),exports.MonitorFunction=exports.MonitorManagerFunction=exports.MonitorMongo=exports.MonitorManager=void 0,require("axios")),monitor_cpu_collection_1=require("../collections/monitor-cpu.collection"),monitor_memory_collection_1=require("../collections/monitor-memory.collection"),monitor_mongo_collection_1=require("../collections/monitor-mongo.collection"),index_1=require("../index"),mongo_manager_1=require("./mongo.manager"),os=require("os"),MonitorManager=function(){function MonitorManager(t,e){this._instanceHostname="",this._monitorData=[],this._mainServer=t,this._serverConfig=e,this._instanceHostname=os.hostname().replace(/\./g,"-"),this.setupIntervals(),this.setupEventLoop()}return MonitorManager.prototype.cpuAverage=function(){for(var t=0,e=0,n=os.cpus(),o=0,i=n.length;o<i;o++){var r,a=n[o];for(r in a.times)e+=a.times[r];t+=a.times.idle}return{idle:t/n.length,total:e/n.length}},MonitorManager.prototype.setupIntervals=function(){return __awaiter(this,void 0,void 0,function(){var e,o,i=this;return __generator(this,function(t){switch(t.label){case 0:if("https://resolveio.com"===this._serverConfig.ROOT_URL||"http://localhost:4200"===this._serverConfig.ROOT_URL)return[3,4];t.label=1;case 1:return t.trys.push([1,3,,4]),[4,axios_1.default.get("http://169.254.169.254/latest/meta-data/instance-id")];case 2:return e=t.sent(),setInterval(function(){return __awaiter(i,void 0,void 0,function(){return __generator(this,function(t){switch(t.label){case 0:return t.trys.push([0,2,,3]),[4,axios_1.default.post("https://backend.resolveio.com/api/health",{type:"application",id_aws_instance:e.data,version:"number"==typeof process.env.APP_VERSION?process.env.APP_VERSION.toString():process.env.APP_VERSION,version_key:process.env.APP_VERSION_KEY})];case 1:return t.sent(),[3,3];case 2:return t.sent(),[3,3];case 3:return[2]}})})},1e4),[3,4];case 3:return t.sent(),[3,4];case 4:return o=this.cpuAverage(),setInterval(function(){var t=new Date,e=i.cpuAverage(),n=1-(e.idle-o.idle)/(e.total-o.total),n=("https://resolveio.com"===i._serverConfig.ROOT_URL||"http://localhost:4200"===i._serverConfig.ROOT_URL?monitor_cpu_collection_1.MonitorCPUs.create({_id:(0,mongo_manager_1.objectIdHexString)(),metadata:{instance:i._instanceHostname,client:i._serverConfig.CLIENT_NAME},date:t,app:n,system:os.loadavg()[0]}):i._monitorData.push({metadata:{instance:i._instanceHostname,client:i._serverConfig.CLIENT_NAME},date:t,app:n,system:os.loadavg()[0],type:"cpu"}),o=e,process.memoryUsage());"https://resolveio.com"===i._serverConfig.ROOT_URL||"http://localhost:4200"===i._serverConfig.ROOT_URL?monitor_memory_collection_1.MonitorMemorys.create({_id:(0,mongo_manager_1.objectIdHexString)(),metadata:{instance:i._instanceHostname,client:i._serverConfig.CLIENT_NAME},date:t,physical_total:n.heapTotal,physical_used:n.heapUsed,physical_free:os.freemem(),virtual:n.rss,private:n.external,physical:os.totalmem()}):i._monitorData.push({metadata:{instance:i._instanceHostname,client:i._serverConfig.CLIENT_NAME},date:t,physical_total:n.heapTotal,physical_used:n.heapUsed,physical_free:os.freemem(),virtual:n.rss,private:n.external,physical:os.totalmem(),type:"memory"})},3e3),[2]}})})},MonitorManager.prototype.addMongoTracker=function(t){"https://resolveio.com"===this._serverConfig.ROOT_URL||"http://localhost:4200"===this._serverConfig.ROOT_URL?t.collection.startsWith("monitor-")||monitor_mongo_collection_1.MonitorMongos.create({_id:(0,mongo_manager_1.objectIdHexString)(),metadata:{instance:this._instanceHostname,client:this._serverConfig.CLIENT_NAME},date:new Date(t.date),method:t.method,doc_collection:t.collection,duration:t.duration,query:Buffer.byteLength(t.query,"utf8")<158e5?t.query:"Too Big"}):this._instanceHostname&&"https://airdropxrp.io"!==this._serverConfig.ROOT_URL&&"https://xrptradereport.com"!==this._serverConfig.ROOT_URL&&this._monitorData.push({metadata:{instance:this._instanceHostname,client:this._serverConfig.CLIENT_NAME},date:new Date(t.date),method:t.method,doc_collection:t.collection,duration:t.duration,query:Buffer.byteLength(t.query,"utf8")<158e5?t.query:"Too Big",type:"mongo"})},MonitorManager.prototype.setupEventLoop=function(){var t=this;setInterval(function(){return __awaiter(t,void 0,void 0,function(){var e;return __generator(this,function(t){switch(t.label){case 0:if(!this._monitorData.length)return[3,4];e=this._monitorData.splice(0,Math.min(25,this._monitorData.length)),t.label=1;case 1:return t.trys.push([1,3,,4]),[4,axios_1.default.post("https://backend.resolveio.com/api/monitor",e)];case 2:return t.sent(),[3,4];case 3:return t.sent(),[3,4];case 4:return[2]}})})},1e3)},MonitorManager}(),MonitorMongo=(exports.MonitorManager=MonitorManager,function(){function MonitorMongo(t,e,n){this._startTime=0,this._method="",this._collection="",this._query="",this._startTime=Date.now(),this._method=t,this._collection=e,this._query=n}return MonitorMongo.prototype.finish=function(){var t=Date.now(),t={date:new Date(t),method:this._method,collection:this._collection,query:this._query,duration:t-this._startTime};index_1.ResolveIOServer.getMainServer().getMonitorManager().addMongoTracker(t)},MonitorMongo}()),MonitorManagerFunction=(exports.MonitorMongo=MonitorMongo,function(){function MonitorManagerFunction(){this._functionCnt=0,this._functions=[],this._functionLastCompletedWS={}}return MonitorManagerFunction.prototype.startMonitorFunction=function(t,e,n,o,i){t=new MonitorFunction(this._functionCnt++,t,e,n,o,i);return this._functions.push(t),t.id},MonitorManagerFunction.prototype.finishMonitorFunction=function(e){var t=this._functions.filter(function(t){return t.id===e})[0];t&&(t.finish(),this._functionLastCompleted=t,this._functionLastCompletedWS[t.id_socket]=t,this._functions.splice(this._functions.map(function(t){return t.id}).indexOf(e),1))},MonitorManagerFunction.prototype.getActiveMonitorFunctions=function(){return this._functions},MonitorManagerFunction.prototype.getLastCompletedMonitorFunction=function(){return this._functionLastCompleted},MonitorManagerFunction.prototype.getLastCompletedMonitorFunctionWS=function(){return this._functionLastCompletedWS},MonitorManagerFunction}()),MonitorFunction=(exports.MonitorManagerFunction=MonitorManagerFunction,function(){function MonitorFunction(t,e,n,o,i,r){var a=this;this._timer=null,this._maxDiff=0,this.startTime=null,this.endTime=null,this.lastTime=null,this.id_socket="",this.id=0,this.id=t,this.startTime=new Date,this._functionType=e,this._functionName=n,this._user=o,this.id_socket=i,this._data=r,this.lastTime=new Date,this._timer=setInterval(function(){var t=Date.now()-a.lastTime.getTime();t>a._maxDiff&&(a._maxDiff=t),a.lastTime=new Date},1e3)}return MonitorFunction.prototype.finish=function(){this._timer&&(clearInterval(this._timer),this._timer=null),this.endTime=new Date,this._maxDiff},MonitorFunction}());exports.MonitorFunction=MonitorFunction;
1
+ "use strict";var __awaiter=this&&this.__awaiter||function(t,o,i,r){return new(i=i||Promise)(function(n,e){function fulfilled(t){try{step(r.next(t))}catch(t){e(t)}}function rejected(t){try{step(r.throw(t))}catch(t){e(t)}}function step(t){var e;t.done?n(t.value):((e=t.value)instanceof i?e:new i(function(t){t(e)})).then(fulfilled,rejected)}step((r=r.apply(t,o||[])).next())})},__generator=this&&this.__generator||function(o,i){var r,a,s,c={label:0,sent:function(){if(1&s[0])throw s[1];return s[1]},trys:[],ops:[]},u={next:verb(0),throw:verb(1),return:verb(2)};return"function"==typeof Symbol&&(u[Symbol.iterator]=function(){return this}),u;function verb(n){return function(t){var e=[n,t];if(r)throw new TypeError("Generator is already executing.");for(;c=u&&e[u=0]?0:c;)try{if(r=1,a&&(s=2&e[0]?a.return:e[0]?a.throw||((s=a.return)&&s.call(a),0):a.next)&&!(s=s.call(a,e[1])).done)return s;switch(a=0,(e=s?[2&e[0],s.value]:e)[0]){case 0:case 1:s=e;break;case 4:return c.label++,{value:e[1],done:!1};case 5:c.label++,a=e[1],e=[0];continue;case 7:e=c.ops.pop(),c.trys.pop();continue;default:if(!(s=0<(s=c.trys).length&&s[s.length-1])&&(6===e[0]||2===e[0])){c=0;continue}if(3===e[0]&&(!s||e[1]>s[0]&&e[1]<s[3]))c.label=e[1];else if(6===e[0]&&c.label<s[1])c.label=s[1],s=e;else{if(!(s&&c.label<s[2])){s[2]&&c.ops.pop(),c.trys.pop();continue}c.label=s[2],c.ops.push(e)}}e=i.call(o,c)}catch(t){e=[6,t],a=0}finally{r=s=0}if(5&e[0])throw e[1];return{value:e[0]?e[1]:void 0,done:!0}}}},axios_1=(Object.defineProperty(exports,"__esModule",{value:!0}),exports.MonitorFunction=exports.MonitorManagerFunction=exports.MonitorMongo=exports.MonitorManager=void 0,require("axios")),monitor_cpu_collection_1=require("../collections/monitor-cpu.collection"),monitor_memory_collection_1=require("../collections/monitor-memory.collection"),monitor_mongo_collection_1=require("../collections/monitor-mongo.collection"),index_1=require("../index"),mongo_manager_1=require("./mongo.manager"),os=require("os"),MonitorManager=function(){function MonitorManager(t,e){this._instanceHostname="",this._monitorData=[],this._mainServer=t,this._serverConfig=e,this._instanceHostname=os.hostname().replace(/\./g,"-"),this.setupIntervals(),this.setupEventLoop()}return MonitorManager.prototype.cpuAverage=function(){for(var t=0,e=0,n=os.cpus(),o=0,i=n.length;o<i;o++){var r,a=n[o];for(r in a.times)e+=a.times[r];t+=a.times.idle}return{idle:t/n.length,total:e/n.length}},MonitorManager.prototype.setupIntervals=function(){return __awaiter(this,void 0,void 0,function(){var e,o,i=this;return __generator(this,function(t){switch(t.label){case 0:if("https://resolveio.com"===this._serverConfig.ROOT_URL||"http://localhost:4200"===this._serverConfig.ROOT_URL)return[3,4];t.label=1;case 1:return t.trys.push([1,3,,4]),[4,axios_1.default.get("http://169.254.169.254/latest/meta-data/instance-id")];case 2:return e=t.sent(),setInterval(function(){return __awaiter(i,void 0,void 0,function(){return __generator(this,function(t){switch(t.label){case 0:return t.trys.push([0,2,,3]),[4,axios_1.default.post("https://backend.resolveio.com/api/health",{type:"application",id_aws_instance:e.data,version:"number"==typeof process.env.APP_VERSION?process.env.APP_VERSION.toString():process.env.APP_VERSION,version_key:process.env.APP_VERSION_KEY})];case 1:return t.sent(),[3,3];case 2:return t.sent(),[3,3];case 3:return[2]}})})},1e4),[3,4];case 3:return t.sent(),[3,4];case 4:return o=this.cpuAverage(),setInterval(function(){var t=new Date,e=i.cpuAverage(),n=1-(e.idle-o.idle)/(e.total-o.total),n=("https://resolveio.com"===i._serverConfig.ROOT_URL||"http://localhost:4200"===i._serverConfig.ROOT_URL?monitor_cpu_collection_1.MonitorCPUs.create({_id:(0,mongo_manager_1.objectIdHexString)(),metadata:{instance:i._instanceHostname,client:i._serverConfig.CLIENT_NAME},date:t,app:n,system:os.loadavg()[0]}):i._monitorData.push({metadata:{instance:i._instanceHostname,client:i._serverConfig.CLIENT_NAME},date:t,app:n,system:os.loadavg()[0],type:"cpu"}),o=e,process.memoryUsage());"https://resolveio.com"===i._serverConfig.ROOT_URL||"http://localhost:4200"===i._serverConfig.ROOT_URL?monitor_memory_collection_1.MonitorMemorys.create({_id:(0,mongo_manager_1.objectIdHexString)(),metadata:{instance:i._instanceHostname,client:i._serverConfig.CLIENT_NAME},date:t,physical_total:n.heapTotal,physical_used:n.heapUsed,physical_free:os.freemem(),virtual:n.rss,private:n.external,physical:os.totalmem()}):i._monitorData.push({metadata:{instance:i._instanceHostname,client:i._serverConfig.CLIENT_NAME},date:t,physical_total:n.heapTotal,physical_used:n.heapUsed,physical_free:os.freemem(),virtual:n.rss,private:n.external,physical:os.totalmem(),type:"memory"})},3e3),[2]}})})},MonitorManager.prototype.addMongoTracker=function(t){"https://resolveio.com"===this._serverConfig.ROOT_URL||"http://localhost:4200"===this._serverConfig.ROOT_URL?t.collection.startsWith("monitor-")||monitor_mongo_collection_1.MonitorMongos.create({_id:(0,mongo_manager_1.objectIdHexString)(),metadata:{instance:this._instanceHostname,client:this._serverConfig.CLIENT_NAME},date:new Date(t.date),method:t.method,doc_collection:t.collection,duration:t.duration,query:Buffer.byteLength(t.query,"utf8")<158e5?t.query:"Too Big"}):this._instanceHostname&&"https://airdropxrp.io"!==this._serverConfig.ROOT_URL&&"https://xrptradereport.com"!==this._serverConfig.ROOT_URL&&this._monitorData.push({metadata:{instance:this._instanceHostname,client:this._serverConfig.CLIENT_NAME},date:new Date(t.date),method:t.method,doc_collection:t.collection,duration:t.duration,query:Buffer.byteLength(t.query,"utf8")<158e5?t.query:"Too Big",type:"mongo"})},MonitorManager.prototype.setupEventLoop=function(){var t=this;setInterval(function(){return __awaiter(t,void 0,void 0,function(){var e;return __generator(this,function(t){switch(t.label){case 0:if(!this._monitorData.length)return[3,4];e=this._monitorData.splice(0,Math.min(25,this._monitorData.length)),t.label=1;case 1:return t.trys.push([1,3,,4]),[4,axios_1.default.post("https://backend.resolveio.com/api/monitor",e)];case 2:return t.sent(),[3,4];case 3:return t.sent(),[3,4];case 4:return[2]}})})},1e3)},MonitorManager}(),MonitorMongo=(exports.MonitorManager=MonitorManager,function(){function MonitorMongo(t,e,n){this._startTime=0,this._method="",this._collection="",this._query="",this._startTime=Date.now(),this._method=t,this._collection=e,this._query=n}return MonitorMongo.prototype.finish=function(){var t=Date.now(),t={date:new Date(t),method:this._method,collection:this._collection,query:this._query,duration:t-this._startTime};index_1.ResolveIOServer.getMainServer().getMonitorManager().addMongoTracker(t)},MonitorMongo}()),MonitorManagerFunction=(exports.MonitorMongo=MonitorMongo,function(){function MonitorManagerFunction(){this._functionCnt=0,this._functions=[],this._functionLastCompletedWS={}}return MonitorManagerFunction.prototype.startMonitorFunction=function(t,e,n,o,i){t=new MonitorFunction(this._functionCnt++,t,e,n,o,i);return this._functions.push(t),t.id},MonitorManagerFunction.prototype.finishMonitorFunction=function(e){var t=this._functions.find(function(t){return t.id===e});t&&(t.finish(),this._functionLastCompleted=t,this._functionLastCompletedWS[t.id_socket]=t,this._functions.splice(this._functions.map(function(t){return t.id}).indexOf(e),1))},MonitorManagerFunction.prototype.getActiveMonitorFunctions=function(){return this._functions},MonitorManagerFunction.prototype.getLastCompletedMonitorFunction=function(){return this._functionLastCompleted},MonitorManagerFunction.prototype.getLastCompletedMonitorFunctionWS=function(){return this._functionLastCompletedWS},MonitorManagerFunction}()),MonitorFunction=(exports.MonitorManagerFunction=MonitorManagerFunction,function(){function MonitorFunction(t,e,n,o,i,r){var a=this;this._timer=null,this._maxDiff=0,this.startTime=null,this.endTime=null,this.lastTime=null,this.id_socket="",this.id=0,this.id=t,this.startTime=new Date,this._functionType=e,this._functionName=n,this._user=o,this.id_socket=i,this._data=r,this.lastTime=new Date,this._timer=setInterval(function(){var t=Date.now()-a.lastTime.getTime();t>a._maxDiff&&(a._maxDiff=t),a.lastTime=new Date},1e3)}return MonitorFunction.prototype.finish=function(){this._timer&&(clearInterval(this._timer),this._timer=null),this.endTime=new Date,this._maxDiff},MonitorFunction}());exports.MonitorFunction=MonitorFunction;
2
2
  //# sourceMappingURL=monitor.manager.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/managers/monitor.manager.ts"],"names":["axios_1","require","monitor_cpu_collection_1","monitor_memory_collection_1","monitor_mongo_collection_1","index_1","mongo_manager_1","os","MonitorManager","mainServer","serverConfig","this","_instanceHostname","_monitorData","_mainServer","_serverConfig","hostname","replace","setupIntervals","setupEventLoop","prototype","cpuAverage","totalIdle","totalTick","cpus","i","len","length","type","cpu","times","idle","total","default","get","instanceId_1","_a","sent","setInterval","__awaiter","_this","post","id_aws_instance","data","version","process","env","APP_VERSION","toString","version_key","APP_VERSION_KEY","lastCPU","now","Date","endMeasure","percentageCPU","memUsage","MonitorCPUs","create","_id","objectIdHexString","metadata","instance","client","date","app","system","loadavg","push","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","monitorData","splice","Math","min","MonitorMongo","exports","_startTime","_method","_collection","_query","finish","endTime","mongoMonitor","ResolveIOServer","getMainServer","getMonitorManager","MonitorManagerFunction","_functionCnt","_functions","_functionLastCompletedWS","startMonitorFunction","functionType","functionName","user","id_socket","newMonitorFunction","MonitorFunction","id","finishMonitorFunction","monitor","filter","a","_functionLastCompleted","map","indexOf","getActiveMonitorFunctions","getLastCompletedMonitorFunction","getLastCompletedMonitorFunctionWS","_timer","_maxDiff","startTime","lastTime","_functionType","_functionName","_user","_data","diff","getTime","clearInterval"],"mappings":"k8CACAA,S,iKAAAC,QAAA,OAAA,GACAC,yBAAAD,QAAA,uCAAA,EACAE,4BAAAF,QAAA,0CAAA,EACAG,2BAAAH,QAAA,yCAAA,EACAI,QAAAJ,QAAA,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,IAAIjF,IACrB,EAAG,GAAI,CACR,CAcD,OAZQkE,gBAAAnG,UAAAmF,OAAP,WACK5F,KAAKwH,SACRU,cAAclI,KAAKwH,MAAM,EACzBxH,KAAKwH,OAAS,MAGfxH,KAAK6F,QAAU,IAAInD,KAEf1C,KAAKyH,QAGV,EACDb,eAAA,EAAC,GA7CYrB,QAAAqB,gBAAAA","file":"monitor.manager.js","sourcesContent":["import ResolveIOMainServer from '../server-app';\nimport axios from 'axios';\nimport { MonitorCPUs } from '../collections/monitor-cpu.collection';\nimport { MonitorMemorys } from '../collections/monitor-memory.collection';\nimport { MonitorMongos } from '../collections/monitor-mongo.collection';\nimport { ResolveIOServer } from '../index';\nimport { deepCopy } from '../util/common';\nimport { objectIdHexString } from './mongo.manager';\nconst os = require('os');\n\nexport class MonitorManager {\n\tprivate _mainServer: ResolveIOMainServer;\n\tprivate _serverConfig;\n\tprivate _instanceHostname = '';\n\tprivate _monitorData = [];\n\n\tconstructor(mainServer, serverConfig) {\n\t\tthis._mainServer = mainServer;\n\t\tthis._serverConfig = serverConfig;\n\t\tthis._instanceHostname = os.hostname().replace(/\\./g, '-');\n\n\t\tthis.setupIntervals();\n\t\tthis.setupEventLoop();\n\t}\n\n\t//Create function to get CPU information\n\tcpuAverage() {\n\t\t//Initialise sum of idle and time of cores and fetch CPU info\n\t\tvar totalIdle = 0, totalTick = 0;\n\t\tvar cpus = os.cpus();\n\t\n\t\t//Loop through CPU cores\n\t\tfor(var i = 0, len = cpus.length; i < len; i++) {\n\t\t\t//Select CPU core\n\t\t\tvar cpu = cpus[i];\n\t\t\n\t\t\t//Total up the time in the cores tick\n\t\t\tfor(let type in cpu.times) {\n\t\t\t\ttotalTick += cpu.times[type];\n\t\t\t}\n\t\t\n\t\t\t//Total up the idle time of the core\n\t\t\ttotalIdle += cpu.times.idle;\n\t\t}\n\t\n\t\t//Return the average Idle and Tick times\n\t\treturn {idle: totalIdle / cpus.length, total: totalTick / cpus.length};\n\t}\n\n\tasync setupIntervals() {\n\t\tif (this._serverConfig['ROOT_URL'] !== 'https://resolveio.com' && this._serverConfig['ROOT_URL'] !== 'http://localhost:4200') {\n\t\t\ttry {\n\t\t\t\tlet instanceId = await axios.get('http://169.254.169.254/latest/meta-data/instance-id');\n\n\t\t\t\tsetInterval(async () => {\n\t\t\t\t\ttry {\n\t\t\t\t\t\tawait axios.post('https://backend.resolveio.com/api/health', {\n\t\t\t\t\t\t\ttype: 'application',\n\t\t\t\t\t\t\tid_aws_instance: instanceId.data,\n\t\t\t\t\t\t\tversion: typeof process.env.APP_VERSION === 'number' ? (<any>process.env.APP_VERSION).toString() : process.env.APP_VERSION,\n\t\t\t\t\t\t\tversion_key: process.env.APP_VERSION_KEY\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\t\t\t\t\tcatch(e) {}\n\t\t\t\t}, 10000);\n\t\t\t}\n\t\t\tcatch(errHealth) {}\n\t\t}\n\n\t\tlet lastCPU = this.cpuAverage();\n\t\t\n\t\tsetInterval(() => {\n\t\t\tlet now = new Date();\n\n\t\t\t//Grab second Measure\n\t\t\tvar endMeasure = this.cpuAverage(); \n\n\t\t\t//Calculate the difference in idle and total time between the measures\n\t\t\tvar idleDifference = endMeasure.idle - lastCPU.idle;\n\t\t\tvar totalDifference = endMeasure.total - lastCPU.total;\n\n\t\t\t//Calculate the average percentage CPU usage\n\t\t\tvar percentageCPU = 1 - idleDifference / totalDifference;\n\t\t\t\n\t\t\tif (this._serverConfig['ROOT_URL'] === 'https://resolveio.com' || this._serverConfig['ROOT_URL'] === 'http://localhost:4200') {\n\t\t\t\tMonitorCPUs.create({\n\t\t\t\t\t_id: objectIdHexString(),\n\t\t\t\t\tmetadata: {\n\t\t\t\t\t\tinstance: this._instanceHostname,\n\t\t\t\t\t\tclient: this._serverConfig['CLIENT_NAME']\n\t\t\t\t\t},\n\t\t\t\t\tdate: now,\n\t\t\t\t\tapp: percentageCPU,\n\t\t\t\t\tsystem: os.loadavg()[0]\n\t\t\t\t});\n\t\t\t}\n\t\t\telse {\n\t\t\t\tthis._monitorData.push({\n\t\t\t\t\tmetadata: {\n\t\t\t\t\t\tinstance: this._instanceHostname,\n\t\t\t\t\t\tclient: this._serverConfig['CLIENT_NAME']\n\t\t\t\t\t},\n\t\t\t\t\tdate: now,\n\t\t\t\t\tapp: percentageCPU,\n\t\t\t\t\tsystem: os.loadavg()[0],\n\t\t\t\t\ttype: 'cpu'\n\t\t\t\t})\n\t\t\t}\n\n\t\t\tlastCPU = endMeasure;\n\n\t\t\tconst memUsage = process.memoryUsage();\n\n\t\t\tif (this._serverConfig['ROOT_URL'] === 'https://resolveio.com' || this._serverConfig['ROOT_URL'] === 'http://localhost:4200') {\n\t\t\t\tMonitorMemorys.create({\n\t\t\t\t\t_id: objectIdHexString(),\n\t\t\t\t\tmetadata: {\n\t\t\t\t\t\tinstance: this._instanceHostname,\n\t\t\t\t\t\tclient: this._serverConfig['CLIENT_NAME']\n\t\t\t\t\t},\n\t\t\t\t\tdate: now,\n\t\t\t\t\tphysical_total: memUsage.heapTotal,\n\t\t\t\t\tphysical_used: memUsage.heapUsed,\n\t\t\t\t\tphysical_free: os.freemem(),\n\t\t\t\t\tvirtual: memUsage.rss,\n\t\t\t\t\tprivate: memUsage.external,\n\t\t\t\t\tphysical: os.totalmem()\n\t\t\t\t});\n\t\t\t}\n\t\t\telse {\n\t\t\t\tthis._monitorData.push({\n\t\t\t\t\tmetadata: {\n\t\t\t\t\t\tinstance: this._instanceHostname,\n\t\t\t\t\t\tclient: this._serverConfig['CLIENT_NAME']\n\t\t\t\t\t},\n\t\t\t\t\tdate: now,\n\t\t\t\t\tphysical_total: memUsage.heapTotal,\n\t\t\t\t\tphysical_used: memUsage.heapUsed,\n\t\t\t\t\tphysical_free: os.freemem(),\n\t\t\t\t\tvirtual: memUsage.rss,\n\t\t\t\t\tprivate: memUsage.external,\n\t\t\t\t\tphysical: os.totalmem(),\n\t\t\t\t\ttype: 'memory'\n\t\t\t\t});\n\t\t\t}\n\t\t}, 3000);\n\t}\n\n\taddMongoTracker(data: MongoMonitorModel) {\n\t\tif (this._serverConfig['ROOT_URL'] === 'https://resolveio.com' || this._serverConfig['ROOT_URL'] === 'http://localhost:4200') {\n\t\t\tif (!data.collection.startsWith('monitor-')) {\n\t\t\t\tMonitorMongos.create({\n\t\t\t\t\t_id: objectIdHexString(),\n\t\t\t\t\tmetadata: {\n\t\t\t\t\t\tinstance: this._instanceHostname,\n\t\t\t\t\t\tclient: this._serverConfig['CLIENT_NAME']\n\t\t\t\t\t},\n\t\t\t\t\tdate: new Date(data.date),\n\t\t\t\t\tmethod: data.method,\n\t\t\t\t\tdoc_collection: data.collection,\n\t\t\t\t\tduration: data.duration,\n\t\t\t\t\tquery: Buffer.byteLength(data.query, 'utf8') < 15800000 ? data.query : 'Too Big'\n\t\t\t\t});\n\t\t\t}\n\t\t}\n\t\telse {\n\t\t\tif (this._instanceHostname) {\n\t\t\t\tif (this._serverConfig['ROOT_URL'] !== 'https://airdropxrp.io' && this._serverConfig['ROOT_URL'] !== 'https://xrptradereport.com') {\n\t\t\t\t\tthis._monitorData.push({\n\t\t\t\t\t\tmetadata: {\n\t\t\t\t\t\t\tinstance: this._instanceHostname,\n\t\t\t\t\t\t\tclient: this._serverConfig['CLIENT_NAME']\n\t\t\t\t\t\t},\n\t\t\t\t\t\tdate: new Date(data.date),\n\t\t\t\t\t\tmethod: data.method,\n\t\t\t\t\t\tdoc_collection: data.collection,\n\t\t\t\t\t\tduration: data.duration,\n\t\t\t\t\t\tquery: Buffer.byteLength(data.query, 'utf8') < 15800000 ? data.query : 'Too Big',\n\t\t\t\t\t\ttype: 'mongo'\n\t\t\t\t\t});\n\t\t\t\t}\n\t\t\t}\n\t\t}\t\n\t}\n\n\tsetupEventLoop() {\n\t\tsetInterval(async () => {\n\t\t\tif (this._monitorData.length) {\n\t\t\t\tlet monitorData = this._monitorData.splice(0, Math.min(25, this._monitorData.length));\n\n\t\t\t\ttry {\n\t\t\t\t\tawait axios.post('https://backend.resolveio.com/api/monitor', monitorData);\n\t\t\t\t}\n\t\t\t\tcatch (e) {}\n\t\t\t}\n\t\t}, 1000);\n\t}\n}\n\nexport interface MongoMonitorModel {\n\tdate: Date, \n\tmethod: string,\n\tcollection: string,\n\tduration: number,\n\tquery: string\n}\n\nexport class MonitorMongo {\n\tprivate _startTime = 0;\n\tprivate _method = '';\n\tprivate _collection = '';\n\tprivate _query = '';\n\n\tconstructor(method: string, collection: string, query: string) {\n\t\tthis._startTime = Date.now();\n\t\tthis._method = method;\n\t\tthis._collection = collection;\n\t\tthis._query = query;\n\t}\n\n\tpublic finish() {\n\t\tlet endTime = Date.now();\n\n\t\tlet mongoMonitor: MongoMonitorModel = {\n\t\t\tdate: new Date(endTime),\n\t\t\tmethod: this._method,\n\t\t\tcollection: this._collection,\n\t\t\tquery: this._query,\n\t\t\tduration: endTime - this._startTime\n\t\t};\n\n\t\tResolveIOServer.getMainServer().getMonitorManager().addMongoTracker(mongoMonitor);\n\t}\n}\n\nexport class MonitorManagerFunction {\n\tprivate _functionCnt = 0;\n\tprivate _functions: MonitorFunction[] = [];\n\tprivate _functionLastCompletedWS: {\n\t\t[id_socket: string]: MonitorFunction;\n\t} = {};\n\tprivate _functionLastCompleted: MonitorFunction;\n\n\tconstructor() {}\n\n\tstartMonitorFunction(functionType: MonitorFunctionType, functionName: string, user: string, id_socket: string, data: any) {\n\t\tlet newMonitorFunction = new MonitorFunction(this._functionCnt++, functionType, functionName, user, id_socket, data);\n\t\tthis._functions.push(newMonitorFunction);\n\t\treturn newMonitorFunction.id;\n\t}\n\n\tfinishMonitorFunction(id: number) {\n\t\tlet monitor = this._functions.filter(a => a.id === id)[0];\n\t\tif (monitor) {\n\t\t\tmonitor.finish();\n\t\t\tthis._functionLastCompleted = monitor;\n\t\t\tthis._functionLastCompletedWS[monitor.id_socket] = monitor;\n\t\t\tthis._functions.splice(this._functions.map(a => a.id).indexOf(id), 1);\n\t\t}\n\t}\n\n\tgetActiveMonitorFunctions() {\n\t\treturn this._functions;\n\t}\n\n\tgetLastCompletedMonitorFunction() {\n\t\treturn this._functionLastCompleted;\n\t}\n\t\n\tgetLastCompletedMonitorFunctionWS() {\n\t\treturn this._functionLastCompletedWS;\n\t}\n}\n\nexport type MonitorFunctionType = 'Cron Method' | 'Internal Method' | 'Method' | 'User Specific Publication' | 'Publication';\nexport class MonitorFunction {\n\tprivate _timer: NodeJS.Timeout = null;\n\tprivate _maxDiff = 0;\n\tprivate _functionType: MonitorFunctionType;\n\tprivate _functionName: string;\n\tprivate _user: string;\n\tprivate _data: any;\n\tpublic startTime: Date = null;\n\tpublic endTime: Date = null;\n\tpublic lastTime: Date = null;\n\tpublic id_socket = '';\n\tpublic id = 0;\n\n\tconstructor(id: number, functionType: MonitorFunctionType, functionName: string, user: string, id_socket: string, data: any) {\n\t\tthis.id = id;\n\t\tthis.startTime = new Date();\n\t\tthis._functionType = functionType;\n\t\tthis._functionName = functionName;\n\t\tthis._user = user;\n\t\tthis.id_socket = id_socket;\n\t\tthis._data = data;\n\t\tthis.lastTime = new Date();\n\n\t\tthis._timer = setInterval(() => {\n\t\t\tlet diff = Date.now() - this.lastTime.getTime();\n\t\t\tif (diff > this._maxDiff) {\n\t\t\t\tthis._maxDiff = diff;\n\t\t\t}\n\n\t\t\tthis.lastTime = new Date();\n\t\t}, 1000);\n\t}\n\n\tpublic finish() {\n\t\tif (this._timer) {\n\t\t\tclearInterval(this._timer);\n\t\t\tthis._timer = null;\n\t\t}\n\n\t\tthis.endTime = new Date();\n\n\t\tif (this._maxDiff >= 5000) {\n\t\t\t// ResolveIOServer.getMainServer().getMethodManager().sendEmail('dev@resolveio.com', ResolveIOServer.getMainServer().getServerConfig()['CLIENT_NAME'] + ' - Slow ' + this._functionType + ' Detected', '', (this._user ? 'User: ' + this._user + '\\n' : '') + this._functionType + ': ' + this._functionName + '\\n' + 'During this function the event loop did not run for ' + this._maxDiff + ' ms (' + this._maxDiff / 1000 + ' sec)' + (this._data ? '\\n\\nData: ' + JSON.stringify(this._data, null, 2) : ''));\n\t\t}\n\t}\n}"]}
1
+ {"version":3,"sources":["../../src/managers/monitor.manager.ts"],"names":["axios_1","require","monitor_cpu_collection_1","monitor_memory_collection_1","monitor_mongo_collection_1","index_1","mongo_manager_1","os","MonitorManager","mainServer","serverConfig","this","_instanceHostname","_monitorData","_mainServer","_serverConfig","hostname","replace","setupIntervals","setupEventLoop","prototype","cpuAverage","totalIdle","totalTick","cpus","i","len","length","type","cpu","times","idle","total","default","get","instanceId_1","_a","sent","setInterval","__awaiter","_this","post","id_aws_instance","data","version","process","env","APP_VERSION","toString","version_key","APP_VERSION_KEY","lastCPU","now","Date","endMeasure","percentageCPU","memUsage","MonitorCPUs","create","_id","objectIdHexString","metadata","instance","client","date","app","system","loadavg","push","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","monitorData","splice","Math","min","MonitorMongo","exports","_startTime","_method","_collection","_query","finish","endTime","mongoMonitor","ResolveIOServer","getMainServer","getMonitorManager","MonitorManagerFunction","_functionCnt","_functions","_functionLastCompletedWS","startMonitorFunction","functionType","functionName","user","id_socket","newMonitorFunction","MonitorFunction","id","finishMonitorFunction","monitor","find","a","_functionLastCompleted","map","indexOf","getActiveMonitorFunctions","getLastCompletedMonitorFunction","getLastCompletedMonitorFunctionWS","_timer","_maxDiff","startTime","lastTime","_functionType","_functionName","_user","_data","diff","getTime","clearInterval"],"mappings":"k8CACAA,S,iKAAAC,QAAA,OAAA,GACAC,yBAAAD,QAAA,uCAAA,EACAE,4BAAAF,QAAA,0CAAA,EACAG,2BAAAH,QAAA,yCAAA,EACAI,QAAAJ,QAAA,UAAA,EAEAK,gBAAAL,QAAA,iBAAA,EACMM,GAAKN,QAAQ,IAAI,EAEvBO,eAAA,WAMC,SAAAA,eAAYC,EAAYC,GAHhBC,KAAAC,kBAAoB,GACpBD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AGfxH,KAAK6F,QAAU,IAAInD,KAEf1C,KAAKyH,QAGV,EACDb,eAAA,EAAC,GA7CYrB,QAAAqB,gBAAAA","file":"monitor.manager.js","sourcesContent":["import ResolveIOMainServer from '../server-app';\nimport axios from 'axios';\nimport { MonitorCPUs } from '../collections/monitor-cpu.collection';\nimport { MonitorMemorys } from '../collections/monitor-memory.collection';\nimport { MonitorMongos } from '../collections/monitor-mongo.collection';\nimport { ResolveIOServer } from '../index';\nimport { deepCopy } from '../util/common';\nimport { objectIdHexString } from './mongo.manager';\nconst os = require('os');\n\nexport class MonitorManager {\n\tprivate _mainServer: ResolveIOMainServer;\n\tprivate _serverConfig;\n\tprivate _instanceHostname = '';\n\tprivate _monitorData = [];\n\n\tconstructor(mainServer, serverConfig) {\n\t\tthis._mainServer = mainServer;\n\t\tthis._serverConfig = serverConfig;\n\t\tthis._instanceHostname = os.hostname().replace(/\\./g, '-');\n\n\t\tthis.setupIntervals();\n\t\tthis.setupEventLoop();\n\t}\n\n\t//Create function to get CPU information\n\tcpuAverage() {\n\t\t//Initialise sum of idle and time of cores and fetch CPU info\n\t\tvar totalIdle = 0, totalTick = 0;\n\t\tvar cpus = os.cpus();\n\t\n\t\t//Loop through CPU cores\n\t\tfor(var i = 0, len = cpus.length; i < len; i++) {\n\t\t\t//Select CPU core\n\t\t\tvar cpu = cpus[i];\n\t\t\n\t\t\t//Total up the time in the cores tick\n\t\t\tfor(let type in cpu.times) {\n\t\t\t\ttotalTick += cpu.times[type];\n\t\t\t}\n\t\t\n\t\t\t//Total up the idle time of the core\n\t\t\ttotalIdle += cpu.times.idle;\n\t\t}\n\t\n\t\t//Return the average Idle and Tick times\n\t\treturn {idle: totalIdle / cpus.length, total: totalTick / cpus.length};\n\t}\n\n\tasync setupIntervals() {\n\t\tif (this._serverConfig['ROOT_URL'] !== 'https://resolveio.com' && this._serverConfig['ROOT_URL'] !== 'http://localhost:4200') {\n\t\t\ttry {\n\t\t\t\tlet instanceId = await axios.get('http://169.254.169.254/latest/meta-data/instance-id');\n\n\t\t\t\tsetInterval(async () => {\n\t\t\t\t\ttry {\n\t\t\t\t\t\tawait axios.post('https://backend.resolveio.com/api/health', {\n\t\t\t\t\t\t\ttype: 'application',\n\t\t\t\t\t\t\tid_aws_instance: instanceId.data,\n\t\t\t\t\t\t\tversion: typeof process.env.APP_VERSION === 'number' ? (<any>process.env.APP_VERSION).toString() : process.env.APP_VERSION,\n\t\t\t\t\t\t\tversion_key: process.env.APP_VERSION_KEY\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\t\t\t\t\tcatch(e) {}\n\t\t\t\t}, 10000);\n\t\t\t}\n\t\t\tcatch(errHealth) {}\n\t\t}\n\n\t\tlet lastCPU = this.cpuAverage();\n\t\t\n\t\tsetInterval(() => {\n\t\t\tlet now = new Date();\n\n\t\t\t//Grab second Measure\n\t\t\tvar endMeasure = this.cpuAverage(); \n\n\t\t\t//Calculate the difference in idle and total time between the measures\n\t\t\tvar idleDifference = endMeasure.idle - lastCPU.idle;\n\t\t\tvar totalDifference = endMeasure.total - lastCPU.total;\n\n\t\t\t//Calculate the average percentage CPU usage\n\t\t\tvar percentageCPU = 1 - idleDifference / totalDifference;\n\t\t\t\n\t\t\tif (this._serverConfig['ROOT_URL'] === 'https://resolveio.com' || this._serverConfig['ROOT_URL'] === 'http://localhost:4200') {\n\t\t\t\tMonitorCPUs.create({\n\t\t\t\t\t_id: objectIdHexString(),\n\t\t\t\t\tmetadata: {\n\t\t\t\t\t\tinstance: this._instanceHostname,\n\t\t\t\t\t\tclient: this._serverConfig['CLIENT_NAME']\n\t\t\t\t\t},\n\t\t\t\t\tdate: now,\n\t\t\t\t\tapp: percentageCPU,\n\t\t\t\t\tsystem: os.loadavg()[0]\n\t\t\t\t});\n\t\t\t}\n\t\t\telse {\n\t\t\t\tthis._monitorData.push({\n\t\t\t\t\tmetadata: {\n\t\t\t\t\t\tinstance: this._instanceHostname,\n\t\t\t\t\t\tclient: this._serverConfig['CLIENT_NAME']\n\t\t\t\t\t},\n\t\t\t\t\tdate: now,\n\t\t\t\t\tapp: percentageCPU,\n\t\t\t\t\tsystem: os.loadavg()[0],\n\t\t\t\t\ttype: 'cpu'\n\t\t\t\t})\n\t\t\t}\n\n\t\t\tlastCPU = endMeasure;\n\n\t\t\tconst memUsage = process.memoryUsage();\n\n\t\t\tif (this._serverConfig['ROOT_URL'] === 'https://resolveio.com' || this._serverConfig['ROOT_URL'] === 'http://localhost:4200') {\n\t\t\t\tMonitorMemorys.create({\n\t\t\t\t\t_id: objectIdHexString(),\n\t\t\t\t\tmetadata: {\n\t\t\t\t\t\tinstance: this._instanceHostname,\n\t\t\t\t\t\tclient: this._serverConfig['CLIENT_NAME']\n\t\t\t\t\t},\n\t\t\t\t\tdate: now,\n\t\t\t\t\tphysical_total: memUsage.heapTotal,\n\t\t\t\t\tphysical_used: memUsage.heapUsed,\n\t\t\t\t\tphysical_free: os.freemem(),\n\t\t\t\t\tvirtual: memUsage.rss,\n\t\t\t\t\tprivate: memUsage.external,\n\t\t\t\t\tphysical: os.totalmem()\n\t\t\t\t});\n\t\t\t}\n\t\t\telse {\n\t\t\t\tthis._monitorData.push({\n\t\t\t\t\tmetadata: {\n\t\t\t\t\t\tinstance: this._instanceHostname,\n\t\t\t\t\t\tclient: this._serverConfig['CLIENT_NAME']\n\t\t\t\t\t},\n\t\t\t\t\tdate: now,\n\t\t\t\t\tphysical_total: memUsage.heapTotal,\n\t\t\t\t\tphysical_used: memUsage.heapUsed,\n\t\t\t\t\tphysical_free: os.freemem(),\n\t\t\t\t\tvirtual: memUsage.rss,\n\t\t\t\t\tprivate: memUsage.external,\n\t\t\t\t\tphysical: os.totalmem(),\n\t\t\t\t\ttype: 'memory'\n\t\t\t\t});\n\t\t\t}\n\t\t}, 3000);\n\t}\n\n\taddMongoTracker(data: MongoMonitorModel) {\n\t\tif (this._serverConfig['ROOT_URL'] === 'https://resolveio.com' || this._serverConfig['ROOT_URL'] === 'http://localhost:4200') {\n\t\t\tif (!data.collection.startsWith('monitor-')) {\n\t\t\t\tMonitorMongos.create({\n\t\t\t\t\t_id: objectIdHexString(),\n\t\t\t\t\tmetadata: {\n\t\t\t\t\t\tinstance: this._instanceHostname,\n\t\t\t\t\t\tclient: this._serverConfig['CLIENT_NAME']\n\t\t\t\t\t},\n\t\t\t\t\tdate: new Date(data.date),\n\t\t\t\t\tmethod: data.method,\n\t\t\t\t\tdoc_collection: data.collection,\n\t\t\t\t\tduration: data.duration,\n\t\t\t\t\tquery: Buffer.byteLength(data.query, 'utf8') < 15800000 ? data.query : 'Too Big'\n\t\t\t\t});\n\t\t\t}\n\t\t}\n\t\telse {\n\t\t\tif (this._instanceHostname) {\n\t\t\t\tif (this._serverConfig['ROOT_URL'] !== 'https://airdropxrp.io' && this._serverConfig['ROOT_URL'] !== 'https://xrptradereport.com') {\n\t\t\t\t\tthis._monitorData.push({\n\t\t\t\t\t\tmetadata: {\n\t\t\t\t\t\t\tinstance: this._instanceHostname,\n\t\t\t\t\t\t\tclient: this._serverConfig['CLIENT_NAME']\n\t\t\t\t\t\t},\n\t\t\t\t\t\tdate: new Date(data.date),\n\t\t\t\t\t\tmethod: data.method,\n\t\t\t\t\t\tdoc_collection: data.collection,\n\t\t\t\t\t\tduration: data.duration,\n\t\t\t\t\t\tquery: Buffer.byteLength(data.query, 'utf8') < 15800000 ? data.query : 'Too Big',\n\t\t\t\t\t\ttype: 'mongo'\n\t\t\t\t\t});\n\t\t\t\t}\n\t\t\t}\n\t\t}\t\n\t}\n\n\tsetupEventLoop() {\n\t\tsetInterval(async () => {\n\t\t\tif (this._monitorData.length) {\n\t\t\t\tlet monitorData = this._monitorData.splice(0, Math.min(25, this._monitorData.length));\n\n\t\t\t\ttry {\n\t\t\t\t\tawait axios.post('https://backend.resolveio.com/api/monitor', monitorData);\n\t\t\t\t}\n\t\t\t\tcatch (e) {}\n\t\t\t}\n\t\t}, 1000);\n\t}\n}\n\nexport interface MongoMonitorModel {\n\tdate: Date, \n\tmethod: string,\n\tcollection: string,\n\tduration: number,\n\tquery: string\n}\n\nexport class MonitorMongo {\n\tprivate _startTime = 0;\n\tprivate _method = '';\n\tprivate _collection = '';\n\tprivate _query = '';\n\n\tconstructor(method: string, collection: string, query: string) {\n\t\tthis._startTime = Date.now();\n\t\tthis._method = method;\n\t\tthis._collection = collection;\n\t\tthis._query = query;\n\t}\n\n\tpublic finish() {\n\t\tlet endTime = Date.now();\n\n\t\tlet mongoMonitor: MongoMonitorModel = {\n\t\t\tdate: new Date(endTime),\n\t\t\tmethod: this._method,\n\t\t\tcollection: this._collection,\n\t\t\tquery: this._query,\n\t\t\tduration: endTime - this._startTime\n\t\t};\n\n\t\tResolveIOServer.getMainServer().getMonitorManager().addMongoTracker(mongoMonitor);\n\t}\n}\n\nexport class MonitorManagerFunction {\n\tprivate _functionCnt = 0;\n\tprivate _functions: MonitorFunction[] = [];\n\tprivate _functionLastCompletedWS: {\n\t\t[id_socket: string]: MonitorFunction;\n\t} = {};\n\tprivate _functionLastCompleted: MonitorFunction;\n\n\tconstructor() {}\n\n\tstartMonitorFunction(functionType: MonitorFunctionType, functionName: string, user: string, id_socket: string, data: any) {\n\t\tlet newMonitorFunction = new MonitorFunction(this._functionCnt++, functionType, functionName, user, id_socket, data);\n\t\tthis._functions.push(newMonitorFunction);\n\t\treturn newMonitorFunction.id;\n\t}\n\n\tfinishMonitorFunction(id: number) {\n\t\tlet monitor = this._functions.find(a => a.id === id);\n\t\tif (monitor) {\n\t\t\tmonitor.finish();\n\t\t\tthis._functionLastCompleted = monitor;\n\t\t\tthis._functionLastCompletedWS[monitor.id_socket] = monitor;\n\t\t\tthis._functions.splice(this._functions.map(a => a.id).indexOf(id), 1);\n\t\t}\n\t}\n\n\tgetActiveMonitorFunctions() {\n\t\treturn this._functions;\n\t}\n\n\tgetLastCompletedMonitorFunction() {\n\t\treturn this._functionLastCompleted;\n\t}\n\t\n\tgetLastCompletedMonitorFunctionWS() {\n\t\treturn this._functionLastCompletedWS;\n\t}\n}\n\nexport type MonitorFunctionType = 'Cron Method' | 'Internal Method' | 'Method' | 'User Specific Publication' | 'Publication';\nexport class MonitorFunction {\n\tprivate _timer: NodeJS.Timeout = null;\n\tprivate _maxDiff = 0;\n\tprivate _functionType: MonitorFunctionType;\n\tprivate _functionName: string;\n\tprivate _user: string;\n\tprivate _data: any;\n\tpublic startTime: Date = null;\n\tpublic endTime: Date = null;\n\tpublic lastTime: Date = null;\n\tpublic id_socket = '';\n\tpublic id = 0;\n\n\tconstructor(id: number, functionType: MonitorFunctionType, functionName: string, user: string, id_socket: string, data: any) {\n\t\tthis.id = id;\n\t\tthis.startTime = new Date();\n\t\tthis._functionType = functionType;\n\t\tthis._functionName = functionName;\n\t\tthis._user = user;\n\t\tthis.id_socket = id_socket;\n\t\tthis._data = data;\n\t\tthis.lastTime = new Date();\n\n\t\tthis._timer = setInterval(() => {\n\t\t\tlet diff = Date.now() - this.lastTime.getTime();\n\t\t\tif (diff > this._maxDiff) {\n\t\t\t\tthis._maxDiff = diff;\n\t\t\t}\n\n\t\t\tthis.lastTime = new Date();\n\t\t}, 1000);\n\t}\n\n\tpublic finish() {\n\t\tif (this._timer) {\n\t\t\tclearInterval(this._timer);\n\t\t\tthis._timer = null;\n\t\t}\n\n\t\tthis.endTime = new Date();\n\n\t\tif (this._maxDiff >= 5000) {\n\t\t\t// ResolveIOServer.getMainServer().getMethodManager().sendEmail('dev@resolveio.com', ResolveIOServer.getMainServer().getServerConfig()['CLIENT_NAME'] + ' - Slow ' + this._functionType + ' Detected', '', (this._user ? 'User: ' + this._user + '\\n' : '') + this._functionType + ': ' + this._functionName + '\\n' + 'During this function the event loop did not run for ' + this._maxDiff + ' ms (' + this._maxDiff / 1000 + ' sec)' + (this._data ? '\\n\\nData: ' + JSON.stringify(this._data, null, 2) : ''));\n\t\t}\n\t}\n}"]}
@@ -4,20 +4,19 @@ import { LoggedInUserModel } from '../models/logged-in-users.model';
4
4
  import { MonitorManagerFunction } from './monitor.manager';
5
5
  export declare class SubscriptionManager {
6
6
  private _mainServer;
7
+ private _websocketManager;
7
8
  private _publications;
8
9
  private _subscriptions;
9
10
  private _wss;
10
11
  private _loggedInUsers;
11
12
  private _mongoQueue;
12
13
  private _mongoQueueId;
13
- private _sendQueue;
14
- private _runningQueue;
15
14
  private _oplog$;
16
15
  private _nodeCache;
17
16
  private _cacheId;
18
- private serverConfig;
19
- private _heapStats;
17
+ private _heapSize;
20
18
  private _heapLimit;
19
+ private serverConfig;
21
20
  private _monitorManagerFunction;
22
21
  private _enableDebug;
23
22
  private _debugOplogCollections;
@@ -32,8 +31,8 @@ export declare class SubscriptionManager {
32
31
  private _debugRemoveCacheHits;
33
32
  private _oplogRetryCount;
34
33
  constructor(mainServer: any, wss: WebSocket.Server, serverConfig: any, monitorManagerFunction: MonitorManagerFunction);
35
- private addToQueue;
36
- invalidatePubsCache(collection: any, type: any): void;
34
+ private setCacheLimit;
35
+ invalidatePubsCache(collection: string, type: string): Promise<void>;
37
36
  publications(method: SubscriptionModel): void;
38
37
  loggedInLatency(ws: WebSocket): void;
39
38
  subscribe(messageRoute: string, messageDate: Date, ws: WebSocket, messageId: number, publication: string, subscriptionData: any[]): void;
@@ -43,6 +42,7 @@ export declare class SubscriptionManager {
43
42
  getActiveSubscriptions(): ActiveSubscriptionModel[];
44
43
  private getPublicationCollections;
45
44
  private tailOpLog;
45
+ private processSubscription;
46
46
  private sendDataToOne;
47
47
  private sendDataToAll;
48
48
  private sendWS;
@@ -1,2 +1,2 @@
1
- "use strict";var __awaiter=this&&this.__awaiter||function(e,n,o,s){return new(o=o||Promise)(function(t,i){function fulfilled(e){try{step(s.next(e))}catch(e){i(e)}}function rejected(e){try{step(s.throw(e))}catch(e){i(e)}}function step(e){var i;e.done?t(e.value):((i=e.value)instanceof o?i:new o(function(e){e(i)})).then(fulfilled,rejected)}step((s=s.apply(e,n||[])).next())})},__generator=this&&this.__generator||function(n,o){var s,r,u,c={label:0,sent:function(){if(1&u[0])throw u[1];return u[1]},trys:[],ops:[]},a={next:verb(0),throw:verb(1),return:verb(2)};return"function"==typeof Symbol&&(a[Symbol.iterator]=function(){return this}),a;function verb(t){return function(e){var i=[t,e];if(s)throw new TypeError("Generator is already executing.");for(;c=a&&i[a=0]?0:c;)try{if(s=1,r&&(u=2&i[0]?r.return:i[0]?r.throw||((u=r.return)&&u.call(r),0):r.next)&&!(u=u.call(r,i[1])).done)return u;switch(r=0,(i=u?[2&i[0],u.value]:i)[0]){case 0:case 1:u=i;break;case 4:return c.label++,{value:i[1],done:!1};case 5:c.label++,r=i[1],i=[0];continue;case 7:i=c.ops.pop(),c.trys.pop();continue;default:if(!(u=0<(u=c.trys).length&&u[u.length-1])&&(6===i[0]||2===i[0])){c=0;continue}if(3===i[0]&&(!u||i[1]>u[0]&&i[1]<u[3]))c.label=i[1];else if(6===i[0]&&c.label<u[1])c.label=u[1],u=i;else{if(!(u&&c.label<u[2])){u[2]&&c.ops.pop(),c.trys.pop();continue}c.label=u[2],c.ops.push(i)}}i=o.call(n,c)}catch(e){i=[6,e],r=0}finally{s=u=0}if(5&i[0])throw i[1];return{value:i[0]?i[1]:void 0,done:!0}}}},__spreadArray=this&&this.__spreadArray||function(e,i,t){if(t||2===arguments.length)for(var n,o=0,s=i.length;o<s;o++)!n&&o in i||((n=n||Array.prototype.slice.call(i,0,o))[o]=i[o]);return e.concat(n||Array.prototype.slice.call(i))},logs_1=(Object.defineProperty(exports,"__esModule",{value:!0}),exports.SubscriptionManager=void 0,require("../publications/logs")),app_status_1=require("../publications/app-status"),files_1=require("../publications/files"),super_admin_1=require("../publications/super-admin"),logged_in_users_collection_1=require("../collections/logged-in-users.collection"),cron_jobs_1=require("../publications/cron-jobs"),flags_1=require("../publications/flags"),method_responses_1=require("../publications/method-responses"),index_1=require("../index"),notifications_1=require("../publications/notifications"),report_builder_reports_1=require("../publications/report-builder-reports"),report_builder_libraries_1=require("../publications/report-builder-libraries"),user_groups_1=require("../publications/user-groups"),user_guides_1=require("../publications/user-guides"),report_builder_dashboard_builders_1=require("../publications/report-builder-dashboard-builders"),mongo_manager_1=require("./mongo.manager"),common_1=require("../util/common"),NodeCache=require("node-cache"),flag_collection_1=require("../collections/flag.collection"),os_1=require("os"),numCPUs=(0,os_1.cpus)().length,v8=require("v8"),SubscriptionManager=function(){function SubscriptionManager(e,i,t,n){var o=this;this._publications={},this._subscriptions=[],this._loggedInUsers=[],this._mongoQueue=[],this._mongoQueueId=0,this._sendQueue=[],this._runningQueue=!1,this._cacheId=1,this._heapStats=v8.getHeapStatistics(),this._heapLimit=this._heapStats.heap_size_limit/numCPUs/8,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._mainServer=e,this._monitorManagerFunction=n,this._nodeCache=new NodeCache({stdTTL:0,checkperiod:0}),this.serverConfig=t,this._wss=i,(0,super_admin_1.loadSuperAdminPublications)(this),(0,app_status_1.loadAppStatusPublications)(this),(0,logs_1.loadLogPublications)(this),(0,files_1.loadFilePublications)(this),(0,cron_jobs_1.loadCronJobPublications)(this),(0,flags_1.loadFlagsPublications)(this),(0,method_responses_1.loadMethodResponsePublications)(this),(0,notifications_1.loadNotificationPublications)(this),(0,report_builder_reports_1.loadReportBuilderReportPublications)(this),(0,report_builder_libraries_1.loadReportBuilderLibraryPublications)(this),(0,user_groups_1.loadUserGroupPublications)(this),(0,user_guides_1.loadUserGuidePublications)(this),(0,report_builder_dashboard_builders_1.loadReportBuilderDashboardBuilderPublications)(this),this.tailOpLog(),setInterval(function(){o._oplogRetryCount=0},15e3),setInterval(function(){o.getEnableDebug()&&(console.log(new Date,"Sub Manager","Subs",o._subscriptions.length),console.log(new Date,"Sub Manager","Logged In Users",o._loggedInUsers.length),console.log(new Date,"Sub Manager","Mongo Queue",o._mongoQueue.length),console.log(new Date,"Sub Manager","Mongo Queue Hits",o._debugMongoQueueHits),console.log(new Date,"Sub Manager","Mongo Queue Collections",JSON.stringify(o._debugMongoQueueCollections.sort(function(e,i){return e.collection.localeCompare(i.collection)||e.publication.localeCompare(i.publication)}),null,2)),console.log(new Date,"Sub Manager","Oplog Hits",o._debugOplogHits),console.log(new Date,"Sub Manager","Oplog Collections",JSON.stringify(o._debugOplogCollections.sort(function(e,i){return e.collection.localeCompare(i.collection)||e.type.localeCompare(i.type)}),null,2)),console.log(new Date,"Sub Manager","Send Queue Hits",o._debugSendQueueHits),console.log(new Date,"Sub Manager","Sub Hits",o._debugSubHits),console.log(new Date,"Sub Manager","Sub Collections",JSON.stringify(o._debugSubCollections.sort(function(e,i){return e.publication.localeCompare(i.publication)}),null,2)),console.log(new Date,"Sub Manager","Unsub Hits",o._debugUnSubHits),console.log(new Date,"Sub Manager","Unsub All Hits",o._debugUnSubAllHits),console.log(new Date,"Sub Manager","Cache Cleanup Hits",o._debugRemoveCacheHits)),o._debugOplogHits=0,o._debugOplogCollections=[],o._debugSubCollections=[],o._debugMongoQueueHits=0,o._debugMongoQueueCollections=[],o._debugSendQueueHits=0,o._debugSubHits=0,o._debugUnSubHits=0,o._debugUnSubAllHits=0,o._debugRemoveCacheHits=0},6e4),setInterval(function(){return __awaiter(o,void 0,void 0,function(){var i,n,o,t,s,r,u=this;return __generator(this,function(e){if(!this._runningQueue){if(this._runningQueue=!0,this._sendQueue.length)for(i=function(e){n._debugSendQueueHits+=1;var i,t=n._mainServer.getWS(n._sendQueue[e].id_ws);t&&t.readyState===t.OPEN?t.bufferedAmount<20480&&(i=n._sendQueue.pop(),t.send(JSON.stringify(i.data),function(e){e&&(u.getEnableDebug()&&console.log(new Date,"Sub Manager","Unsub WS",t.user,t.id_socket,1),u.unsubscribeAll(t))})):n._sendQueue.splice(e,1)},r=(n=this)._sendQueue.length-1;0<=r;r--)i(r);if((o=this._mongoQueue.filter(function(e){return!e.running})).length)for(o.forEach(function(e){e.running=!0}),t=function(e){s._debugMongoQueueHits+=1;var t=o[e];if(s._debugMongoQueueCollections.some(function(e){return e.collection===t.collection&&e.publication===t.subscription.publication})?s._debugMongoQueueCollections.filter(function(e){return e.collection===t.collection&&e.publication===t.subscription.publication})[0].hits+=1:s._debugMongoQueueCollections.push({collection:t.collection,publication:t.subscription.publication,hits:1}),s._publications[t.subscription.publication].user_specific){for(var n=[],i=0;i<t.subscription.clients.length;i++)!function(e){var i=t.subscription.clients[e],e=s._mainServer.getWS(i.id_socket);e&&e.id_socket===i.id_socket&&n.push(s.sendDataToOne(e,i.messageId,t.subscription,t.type,t.collection).catch(function(e){return u._mainServer.getMethodManager().sendEmail("dev@resolveio.com","SERVER - Error Detected - "+u.serverConfig.CLIENT_NAME,"Error Detected During sendDataToOne - User Specific - Socket: "+i.id_socket+", User: "+i.id_user+", MessageId: "+i.messageId+", Pub: "+t.subscription.publication+", Err: "+JSON.stringify(e,null,2)),null}))}(i);Promise.all(n).then(function(){t.run_again?(t.running=!1,t.run_again=!1):0<=u._mongoQueue.map(function(e){return e._id}).indexOf(t._id)&&u._mongoQueue.splice(u._mongoQueue.map(function(e){return e._id}).indexOf(t._id),1)})}else s.sendDataToAll(t).then(function(){t.run_again?(t.running=!1,t.run_again=!1):0<=u._mongoQueue.map(function(e){return e._id}).indexOf(t._id)&&u._mongoQueue.splice(u._mongoQueue.map(function(e){return e._id}).indexOf(t._id),1)},function(){0<=u._mongoQueue.map(function(e){return e._id}).indexOf(t._id)&&u._mongoQueue.splice(u._mongoQueue.map(function(e){return e._id}).indexOf(t._id),1)})},s=this,r=o.length-1;0<=r;r--)t(r);this._runningQueue=!1}return[2]})})},1),setInterval(function(){return __awaiter(o,void 0,void 0,function(){var i,n,t,o,s,r,u,c,a;return __generator(this,function(e){switch(e.label){case 0:return i=this,[4,logged_in_users_collection_1.LoggedInUsers.find()];case 1:for(i._loggedInUsers=e.sent(),n=(0,common_1.deepCopy)(this._loggedInUsers),t=function(e){var t=n[e];(!t.date||12e4<Date.now()-t.date.getTime())&&(o._mainServer.getWS(t.id_ws)?(o.getEnableDebug()&&console.log(new Date,"Sub Manager","Unsub WS",o._mainServer.getWS(t.id_ws).user,o._mainServer.getWS(t.id_ws).id_socket,2),o.unsubscribeAll(o._mainServer.getWS(t.id_ws))):(o._subscriptions.forEach(function(e){for(var i=e.clients.length-1;0<=i;i--)e.clients[i].id_socket===t.id_ws&&e.clients.splice(i,1)}),logged_in_users_collection_1.LoggedInUsers.deleteOne({_id:t._id}),0<=o._loggedInUsers.findIndex(function(e){return e._id===t._id})&&o._loggedInUsers.splice(o._loggedInUsers.findIndex(function(e){return e._id===t._id}),1)))},s=(o=this)._loggedInUsers.length-1;0<=s;s--)t(s);for(s=0;s<this._subscriptions.length;s++)for(r=this._subscriptions[s],u=function(e){var i=r.clients[e];c._loggedInUsers.some(function(e){return e.id_ws===i.id_socket})||r.clients.splice(e,1)},c=this,a=r.clients.length-1;0<=a;a--)u(a);return[2]}})})},3e4),flag_collection_1.Flags.findOne({type:"Enable Debug"}).then(function(e){e&&e.value?o._enableDebug=!0:o._enableDebug=!1})}return SubscriptionManager.prototype.addToQueue=function(i){var e=this._mongoQueue.filter(function(e){return e.subscription.publication===i.subscription.publication&&JSON.stringify(e.subscription.subscriptionData)===JSON.stringify(i.subscription.subscriptionData)})[0];e?e.running&&(e.run_again=!0):(i._id=this._mongoQueueId,this._mongoQueueId+=1,this._mongoQueue.splice(0,0,i))},SubscriptionManager.prototype.invalidatePubsCache=function(i,e){index_1.ResolveIOServer.getMongoManager().invalidateQueryCache(i);for(var t=this._subscriptions.filter(function(e){return e.collections.includes(i)}),n=t.length-1;0<=n;n--){var o=t[n];this.addToQueue({_id:0,type:e,collection:i,subscription:o,running:!1,run_again:!1})}},SubscriptionManager.prototype.publications=function(e){this._publications=Object.assign(this._publications,e)},SubscriptionManager.prototype.loggedInLatency=function(i){var t=this,e=this._loggedInUsers.filter(function(e){return e.id_ws===i.id_socket})[0];e&&(e.date=new Date,logged_in_users_collection_1.LoggedInUsers.updateOne({id_ws:i.id_socket},{$set:{latency:i.latency,date:e.date}}).then(function(e){e||(t.unsubscribeAll(i),t.getEnableDebug()&&console.log(new Date,"Sub Manager","Unsub WS",i.user,i.id_socket,3))},function(){t.unsubscribeAll(i),t.getEnableDebug()&&console.log(new Date,"Sub Manager","Unsub WS",i.user,i.id_socket,4)}))},SubscriptionManager.prototype.subscribe=function(i,e,t,n,o,s){var r=this,u=(this._debugSubHits+=1,this._debugSubCollections.some(function(e){return e.publication===o})?this._debugSubCollections.filter(function(e){return e.publication===o})[0].hits+=1:this._debugSubCollections.push({publication:o,hits:1}),this._publications[o]);if(u){if(1<s.length||s[0]){if(!u.check)return void console.error(new Date,"No Check Function For Pub "+o);if(!u.check._schema)return void console.error(new Date,"No Check Schema For Pub "+o);for(var c={},a=Object.keys(u.check._schema).filter(function(e){return!e.includes(".")}),l=0;l<s.length;l++)c[a[l]]=s[l];try{u.check.validate(c)}catch(e){if(e)return void console.error(new Date,"Error in Pub Check ("+o+")",e)}}"Bypass"!==i&&(u=i.split("/"),g="",d=u[0],""===u[0]&&(g="/",d=u[1]),g+=d,1<u.length&&(g+="/"),(d=this._subscriptions.filter(function(e){return e.clients.some(function(e){return e.id_socket===t.id_socket&&"Bypass"!==e.messageRoute&&"/"!==e.messageRoute&&e.messageRoute!==i&&!e.messageRoute.startsWith(g)})})).length)&&(this._mainServer.getMethodManager().sendEmail("dev@resolveio.com","SERVER - Detected Undestroyed Subscription - "+this.serverConfig.CLIENT_NAME,"USER: "+t.user+" (Socket: "+t.id_socket+") is on route: "+i+" but has the following subscriptions on other routes:"+JSON.stringify(d,null,2)),d.forEach(function(i){i.clients.filter(function(e){return e.id_socket===t.id_socket}).forEach(function(e){r.unsubscribe(e.messageRoute,new Date,t,e.messageId,i.publication,i.subscriptionData)})}));var g,d,_=this._subscriptions.filter(function(e){return e.publication===o&&JSON.stringify(e.subscriptionData)===JSON.stringify(s)})[0];_?_.clients.some(function(e){return e.id_socket===t.id_socket&&e.messageId===n})||_.clients.push({id_user:t.id_user,messageId:n,id_socket:t.id_socket,messageRoute:i}):this._subscriptions.push({publication:o,subscriptionData:s,collections:this.getPublicationCollections(o),clients:[{id_user:t.id_user,messageId:n,id_socket:t.id_socket,messageRoute:i}],cacheId:0}),(_=_||this._subscriptions.filter(function(e){return e.publication===o&&JSON.stringify(e.subscriptionData)===JSON.stringify(s)})[0]).cacheId||this._mongoQueue.some(function(e){return e.subscription.publication===_.publication&&JSON.stringify(e.subscription.subscriptionData)===JSON.stringify(_.subscriptionData)})?_.cacheId&&this.sendDataToOne(t,n,_,"newSub",o):this.addToQueue({_id:0,type:"newSub",collection:"",subscription:_,running:!1,run_again:!1})}else console.error(new Date,"No Publication: "+o)},SubscriptionManager.prototype.createLoggedInUser=function(n){return __awaiter(this,void 0,void 0,function(){var i=this;return __generator(this,function(e){return[2,new Promise(function(t,e){return __awaiter(i,void 0,void 0,function(){var i;return __generator(this,function(e){return(i=this._mainServer.getWS(n))?(i={_id:(0,mongo_manager_1.objectIdHexString)(),__v:0,date:new Date,id_user:i.id_user,user:i.user,id_ws:i.id_socket},this._loggedInUsers.push(i),logged_in_users_collection_1.LoggedInUsers.insertOne(i),t(i)):t(null),[2]})})})]})})},SubscriptionManager.prototype.unsubscribe=function(e,i,t,n,o,s){if(this._debugUnSubHits+=1,this._publications[o]){var r=this._subscriptions.filter(function(e){return e.publication===o&&JSON.stringify(e.subscriptionData)===JSON.stringify(s)})[0];if(r)for(var u=r.clients.length-1;0<=u;u--)r.clients[u].id_user===t.id_user&&r.clients[u].messageId===n&&r.clients[u].id_socket===t.id_socket&&r.clients.splice(u,1)}else console.log("No Publication: "+o)},SubscriptionManager.prototype.unsubscribeAll=function(r){return __awaiter(this,void 0,void 0,function(){var i,t,n,o,s;return __generator(this,function(e){switch(e.label){case 0:return(this._debugUnSubAllHits+=1,r)?[4,logged_in_users_collection_1.LoggedInUsers.findOne({id_ws:r.id_socket})]:[3,2];case 1:for((i=e.sent())&&(0<=this._loggedInUsers.map(function(e){return e._id}).indexOf(i._id)&&this._loggedInUsers.splice(this._loggedInUsers.map(function(e){return e._id}).indexOf(i._id),1),logged_in_users_collection_1.LoggedInUsers.deleteOne({_id:i._id})),t=this._subscriptions.filter(function(e){return e.clients.some(function(e){return e.id_user===r.id_user&&e.id_socket===r.id_socket})}),n=t.length-1;0<=n;n--)for(o=t[n],s=o.clients.length-1;0<=s;s--)o.clients[s].id_socket===r.id_socket&&o.clients.splice(s,1);r.close(),process.nextTick(function(){[r.OPEN,r.CLOSING].includes(r.readyState)&&r.terminate()}),e.label=2;case 2:return[2]}})})},SubscriptionManager.prototype.getActiveSubscriptions=function(){return this._subscriptions},SubscriptionManager.prototype.getPublicationCollections=function(e){return this._publications[e].collections},SubscriptionManager.prototype.tailOpLog=function(i){return __awaiter(this,void 0,void 0,function(){var t,n=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)),i){t=i;try{this._oplog$=index_1.ResolveIOServer.getMainDB().watch([],{resumeAfter:i})}catch(e){return this._oplog$&&(this._oplog$.removeAllListeners(),this._oplog$.close(),this._oplog$=null),this.tailOpLog(i),[2]}}else this._oplog$=index_1.ResolveIOServer.getMainDB().watch();console.log(new Date,"oplog started"),this._oplog$.on("change",function(i){var e;i.ns&&(e=i.ns.coll,n._debugOplogCollections.some(function(e){return e.collection===i.ns.coll&&e.type===i.operationType})?n._debugOplogCollections.filter(function(e){return e.collection===i.ns.coll&&e.type===i.operationType})[0].hits+=1:n._debugOplogCollections.push({collection:i.ns.coll,type:i.operationType,hits:1}),!e||e.endsWith(".versions")||e.startsWith("monitor-")||"logs"===e||"log-method-latencies"===e||"log-subscriptions"===e||(n._debugOplogHits+=1,"insert"===i.operationType?("support-tickets"===e&&"https://resolveio.com"===n.serverConfig.ROOT_URL&&(n._mainServer.getMethodManager().callMethodInternal.call(n._mainServer.getMethodManager(),"sendSupportTicketEmail",i.documentKey._id),n.invalidatePubsCache(e,"insert")),"method-responses"!==e&&n.invalidatePubsCache(e,"insert")):"update"===i.operationType||"replace"===i.operationType?"method-responses"!==e&&n.invalidatePubsCache(e,"update"):"delete"===i.operationType&&"method-responses"!==e&&n.invalidatePubsCache(e,"delete")),"flags"===e&&flag_collection_1.Flags.findOne({type:"Enable Debug"}).then(function(e){e&&e.value?n._enableDebug=!0:n._enableDebug=!1}),t=i._id)}),this._oplog$.on("error",function(e){console.log(new Date,"oplog error",e),n._oplog$.removeAllListeners(),n._oplog$.close(),n._oplog$=null,n.tailOpLog(t)}),this._oplog$.on("end",function(){console.log(new Date,"oplog end"),n._oplog$.removeAllListeners(),n._oplog$.close(),n._oplog$=null,n.tailOpLog(t)}),this._oplog$.on("close",function(){console.log(new Date,"oplog close"),n._oplog$.removeAllListeners(),n._oplog$=null,n.tailOpLog(t)})}return[2]}})})},SubscriptionManager.prototype.sendDataToOne=function(c,a,l,e,i){var t=this;return new Promise(function(r,u){return __awaiter(t,void 0,void 0,function(){var i,t,n,o,s=this;return __generator(this,function(e){if(this._publications[l.publication].user_specific)n=this._monitorManagerFunction.startMonitorFunction("User Specific Publication",l.publication,"","",l.subscriptionData),(o=this._publications[l.publication].function).call.apply(o,__spreadArray([Object.assign({},this,SubscriptionManager.prototype),c.id_user],l.subscriptionData,!1)).then(function(e){s._monitorManagerFunction.finishMonitorFunction(n),s.sendWS(c,{messageId:a,hasError:!1,data:e}),r(!0)},function(e){s._monitorManagerFunction.finishMonitorFunction(n),s.sendWS(c,{messageId:a,hasError:!0,data:e}),s._mainServer.getMethodManager().sendEmail("dev@resolveio.com","SERVER - Error Detected - "+s.serverConfig.CLIENT_NAME,"Error Detected During Subscription "+l.publication+" - (sendDataToOne - WS)\n\nData \n"+JSON.stringify(l.subscriptionData,null,2)+"\n\nErrors\n"+JSON.stringify(e,null,2)),u(e)});else if(l.cacheId){try{i=JSON.parse(this._nodeCache.get(l.cacheId),common_1.dateReviver),t={messageId:a,hasError:!1,data:i},this.sendWS(c,t)}catch(e){this._nodeCache.del(l.cacheId),l.cacheId=0,this.addToQueue({_id:0,type:"newSub",collection:"",subscription:l,running:!1,run_again:!1})}r(!0)}else this._mainServer.getMethodManager().sendEmail("dev@resolveio.com","SERVER - Error Detected - "+this.serverConfig.CLIENT_NAME,"Error Detected During sendDataToOne - No Cache! - "+l.publication+"\n\nData \n"+JSON.stringify(l.subscriptionData,null,2)),r(!1);return[2]})})})},SubscriptionManager.prototype.sendDataToAll=function(a){var e=this;return new Promise(function(c,n){return __awaiter(e,void 0,void 0,function(){var r,i,u=this;return __generator(this,function(e){return a.subscription.clients.length?("superadminAPM"!==a.subscription.publication&&"loggedInUsers"!==a.subscription.publication&&this._mainServer.getMethodManager().callMethodInternal.call(this._mainServer.getMethodManager(),"insertSubscriptionLog",a.type,a.subscription.publication,a.collection,JSON.stringify(a.subscription.subscriptionData)),r=this._monitorManagerFunction.startMonitorFunction("Publication",a.subscription.publication,"","",a.subscription.subscriptionData),(i=this._publications[a.subscription.publication].function).call.apply(i,__spreadArray([Object.assign({},this,SubscriptionManager.prototype)],a.subscription.subscriptionData,!1)).then(function(t){if(u._monitorManagerFunction.finishMonitorFunction(r),a.subscription.clients.length){if(!a.subscription.cacheId||u._nodeCache.get(a.subscription.cacheId)!==JSON.stringify(t)){if(a.subscription.clients.forEach(function(e){var i=u._mainServer.getWS(e.id_socket);i&&(e={messageId:e.messageId,hasError:!1,data:t},u.sendWS(i,e))}),(e=u._nodeCache.getStats().vsize)>u._heapLimit){for(i=0,n=u._subscriptions.filter(function(e){return e.cacheId&&!e.clients.length}),o=0;o<n.length&&(u._nodeCache.del(n[o].cacheId),n[o].cacheId=0,i+=1,!((e=u._nodeCache.getStats().vsize)<.75*u._heapLimit));o++);console.log("Sub Cache: Too Big - "+a.subscription.publication+" - Deleted: "+i+" - "+e)}a.subscription.cacheId&&u._nodeCache.del(a.subscription.cacheId);s=u._cacheId;u._cacheId+=1,u._nodeCache.set(s,JSON.stringify(t)),a.subscription.cacheId=s}c(!0)}else{var e;if((e=u._nodeCache.getStats().vsize)>u._heapLimit){for(var i=0,n=u._subscriptions.filter(function(e){return e.cacheId&&!e.clients.length}),o=0;o<n.length&&(u._debugRemoveCacheHits+=1,u._nodeCache.del(n[o].cacheId),n[o].cacheId=0,i+=1,!((e=u._nodeCache.getStats().vsize)<.75*u._heapLimit));o++);console.log("Sub Cache: Too Big - "+a.subscription.publication+" - Deleted: "+i+" - "+e)}a.subscription.cacheId&&u._nodeCache.del(a.subscription.cacheId);var s=u._cacheId;u._cacheId+=1,u._nodeCache.set(s,JSON.stringify(t)),a.subscription.cacheId=s,c(!1)}},function(t){u._monitorManagerFunction.finishMonitorFunction(r),u._mainServer.getMethodManager().sendEmail("dev@resolveio.com","SERVER - Error Detected - "+u.serverConfig.CLIENT_NAME,"Error Detected During Subscription "+a.subscription.publication+" - (sendPubData)\n\nData \n"+JSON.stringify(a.subscription.subscriptionData,null,2)+"\n\nErrors\n"+JSON.stringify(t,null,2)),a.subscription.clients.forEach(function(e){var i=u._mainServer.getWS(e.id_socket);i&&i.id_socket===e.id_socket&&(e={messageId:e.messageId,hasError:!0,data:t},u.sendWS(i,e))}),n(t)})):(a.subscription.cacheId&&this._nodeCache.del(a.subscription.cacheId),0<=this._subscriptions.findIndex(function(e){return e.publication===a.subscription.publication&&JSON.stringify(e.subscriptionData)===JSON.stringify(a.subscription.subscriptionData)})&&this._subscriptions.splice(this._subscriptions.findIndex(function(e){return e.publication===a.subscription.publication&&JSON.stringify(e.subscriptionData)===JSON.stringify(a.subscription.subscriptionData)}),1),c(!1)),[2]})})})},SubscriptionManager.prototype.sendWS=function(e,i){this._sendQueue.splice(0,0,{id_ws:e.id_socket,data:i})},SubscriptionManager.prototype.getEnableDebug=function(){return this._enableDebug},SubscriptionManager}();exports.SubscriptionManager=SubscriptionManager;
1
+ "use strict";var __awaiter=this&&this.__awaiter||function(e,n,o,s){return new(o=o||Promise)(function(i,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?i(e.value):((t=e.value)instanceof o?t:new o(function(e){e(t)})).then(fulfilled,rejected)}step((s=s.apply(e,n||[])).next())})},__generator=this&&this.__generator||function(n,o){var s,r,a,c={label:0,sent:function(){if(1&a[0])throw a[1];return a[1]},trys:[],ops:[]},u={next:verb(0),throw:verb(1),return:verb(2)};return"function"==typeof Symbol&&(u[Symbol.iterator]=function(){return this}),u;function verb(i){return function(e){var t=[i,e];if(s)throw new TypeError("Generator is already executing.");for(;c=u&&t[u=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(n,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}}}},__spreadArray=this&&this.__spreadArray||function(e,t,i){if(i||2===arguments.length)for(var n,o=0,s=t.length;o<s;o++)!n&&o in t||((n=n||Array.prototype.slice.call(t,0,o))[o]=t[o]);return e.concat(n||Array.prototype.slice.call(t))},logs_1=(Object.defineProperty(exports,"__esModule",{value:!0}),exports.SubscriptionManager=void 0,require("../publications/logs")),app_status_1=require("../publications/app-status"),files_1=require("../publications/files"),super_admin_1=require("../publications/super-admin"),logged_in_users_collection_1=require("../collections/logged-in-users.collection"),cron_jobs_1=require("../publications/cron-jobs"),flags_1=require("../publications/flags"),method_responses_1=require("../publications/method-responses"),index_1=require("../index"),notifications_1=require("../publications/notifications"),report_builder_reports_1=require("../publications/report-builder-reports"),report_builder_libraries_1=require("../publications/report-builder-libraries"),user_groups_1=require("../publications/user-groups"),user_guides_1=require("../publications/user-guides"),report_builder_dashboard_builders_1=require("../publications/report-builder-dashboard-builders"),mongo_manager_1=require("./mongo.manager"),common_1=require("../util/common"),NodeCache=require("node-cache"),flag_collection_1=require("../collections/flag.collection"),os_1=require("os"),numCPUs=(0,os_1.cpus)().length,v8=require("v8"),SubscriptionManager=function(){function SubscriptionManager(e,t,i,n){var o=this;this._publications={},this._subscriptions=[],this._loggedInUsers=[],this._mongoQueue=[],this._mongoQueueId=0,this._cacheId=1,this._heapSize=v8.getHeapStatistics()/numCPUs,this._enableDebug=!1,this._debugOplogCollections=[],this._debugOplogHits=0,this._debugSubCollections=[],this._debugSubHits=0,this._debugUnSubHits=0,this._debugUnSubAllHits=0,this._debugMongoQueueHits=0,this._debugMongoQueueCollections=[],this._debugSendQueueHits=0,this._debugRemoveCacheHits=0,this._oplogRetryCount=0,this._mainServer=e,this._websocketManager=this._mainServer.getWebSocketManager(),this._monitorManagerFunction=n,this._nodeCache=new NodeCache({stdTTL:0,checkperiod:0}),this.serverConfig=i,this._wss=t,(0,super_admin_1.loadSuperAdminPublications)(this),(0,app_status_1.loadAppStatusPublications)(this),(0,logs_1.loadLogPublications)(this),(0,files_1.loadFilePublications)(this),(0,cron_jobs_1.loadCronJobPublications)(this),(0,flags_1.loadFlagsPublications)(this),(0,method_responses_1.loadMethodResponsePublications)(this),(0,notifications_1.loadNotificationPublications)(this),(0,report_builder_reports_1.loadReportBuilderReportPublications)(this),(0,report_builder_libraries_1.loadReportBuilderLibraryPublications)(this),(0,user_groups_1.loadUserGroupPublications)(this),(0,user_guides_1.loadUserGuidePublications)(this),(0,report_builder_dashboard_builders_1.loadReportBuilderDashboardBuilderPublications)(this),this.tailOpLog(),setInterval(function(){o._oplogRetryCount=0},15e3),setInterval(function(){o.getEnableDebug()&&(console.log(new Date,"Sub Manager","Subs",o._subscriptions.length),console.log(new Date,"Sub Manager","Logged In Users",o._loggedInUsers.length),console.log(new Date,"Sub Manager","Mongo Queue",o._mongoQueue.length),console.log(new Date,"Sub Manager","Mongo Queue Hits",o._debugMongoQueueHits),console.log(new Date,"Sub Manager","Mongo Queue Collections",JSON.stringify(o._debugMongoQueueCollections.sort(function(e,t){return e.collection.localeCompare(t.collection)||e.publication.localeCompare(t.publication)}),null,2)),console.log(new Date,"Sub Manager","Oplog Hits",o._debugOplogHits),console.log(new Date,"Sub Manager","Oplog Collections",JSON.stringify(o._debugOplogCollections.sort(function(e,t){return e.collection.localeCompare(t.collection)||e.type.localeCompare(t.type)}),null,2)),console.log(new Date,"Sub Manager","Send Queue Hits",o._debugSendQueueHits),console.log(new Date,"Sub Manager","Sub Hits",o._debugSubHits),console.log(new Date,"Sub Manager","Sub Collections",JSON.stringify(o._debugSubCollections.sort(function(e,t){return e.publication.localeCompare(t.publication)}),null,2)),console.log(new Date,"Sub Manager","Unsub Hits",o._debugUnSubHits),console.log(new Date,"Sub Manager","Unsub All Hits",o._debugUnSubAllHits),console.log(new Date,"Sub Manager","Cache Cleanup Hits",o._debugRemoveCacheHits)),o._debugOplogHits=0,o._debugOplogCollections=[],o._debugSubCollections=[],o._debugMongoQueueHits=0,o._debugMongoQueueCollections=[],o._debugSendQueueHits=0,o._debugSubHits=0,o._debugUnSubHits=0,o._debugUnSubAllHits=0,o._debugRemoveCacheHits=0},6e4),setInterval(function(){return __awaiter(o,void 0,void 0,function(){var t,n,i,o,s,r,a,c,u;return __generator(this,function(e){switch(e.label){case 0:return t=this,[4,logged_in_users_collection_1.LoggedInUsers.find()];case 1:for(t._loggedInUsers=e.sent(),n=(0,common_1.deepCopy)(this._loggedInUsers),i=function(e){var i=n[e];(!i.date||12e4<Date.now()-i.date.getTime())&&(o._websocketManager.getWebSocket(i.id_ws)?(o.getEnableDebug()&&console.log(new Date,"Sub Manager","Unsub WS",o._websocketManager.getWebSocket(i.id_ws).user,o._websocketManager.getWebSocket(i.id_ws).id_socket,2),o.unsubscribeAll(o._websocketManager.getWebSocket(i.id_ws))):(o._subscriptions.forEach(function(e){for(var t=e.clients.length-1;0<=t;t--)e.clients[t].id_socket===i.id_ws&&e.clients.splice(t,1)}),logged_in_users_collection_1.LoggedInUsers.deleteOne({_id:i._id}),0<=o._loggedInUsers.findIndex(function(e){return e._id===i._id})&&o._loggedInUsers.splice(o._loggedInUsers.findIndex(function(e){return e._id===i._id}),1)))},s=(o=this)._loggedInUsers.length-1;0<=s;s--)i(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,u=r.clients.length-1;0<=u;u--)a(u);return[2]}})})},3e4),flag_collection_1.Flags.findOne({type:"Enable Debug"}).then(function(e){e&&e.value?o._enableDebug=!0:o._enableDebug=!1}),this.setCacheLimit()}return SubscriptionManager.prototype.setCacheLimit=function(){"true"===process.env.IS_WORKERS_ENABLED?this._heapLimit=.4*this._heapSize:this._heapLimit=.3*this._heapSize},SubscriptionManager.prototype.invalidatePubsCache=function(a,c){return __awaiter(this,void 0,void 0,function(){var t,i,n,o,s,r=this;return __generator(this,function(e){switch(e.label){case 0:index_1.ResolveIOServer.getMongoManager().invalidateQueryCache(a),o=this._subscriptions.filter(function(e){return e.collections.includes(a)}),t=function(o){return __generator(this,function(e){switch(e.label){case 0:return i._publications[o.publication].user_specific?[4,Promise.all(o.clients.map(function(n){return __awaiter(r,void 0,void 0,function(){var t,i;return __generator(this,function(e){switch(e.label){case 0:if(!(t=this._websocketManager.getWebSocket(n.id_socket))||t.readyState!==t.OPEN)return[3,4];e.label=1;case 1:return e.trys.push([1,3,,4]),[4,this.sendDataToOne(t,n.messageId,o,c,a)];case 2:return e.sent(),[3,4];case 3:return i=e.sent(),this._mainServer.getMethodManager().sendEmail("dev@resolveio.com","SERVER - Error Detected - "+this.serverConfig.CLIENT_NAME,"Error Detected During sendDataToOne - User Specific - Socket: "+n.id_socket+", User: "+n.id_user+", MessageId: "+n.messageId+", Pub: "+o.publication+", Err: "+JSON.stringify(i,null,2)),[3,4];case 4:return[2]}})})}))]:[3,2];case 1:return e.sent(),[3,4];case 2:return[4,i.sendDataToAll(o)];case 3:e.sent(),e.label=4;case 4:return[2]}})},i=this,n=0,o=o,e.label=1;case 1:return n<o.length?(s=o[n],[5,t(s)]):[3,4];case 2:e.sent(),e.label=3;case 3:return n++,[3,1];case 4:return[2]}})})},SubscriptionManager.prototype.publications=function(e){this._publications=Object.assign(this._publications,e)},SubscriptionManager.prototype.loggedInLatency=function(t){var i=this,e=this._loggedInUsers.find(function(e){return e.id_ws===t.id_socket});e&&(e.date=new Date,logged_in_users_collection_1.LoggedInUsers.updateOne({id_ws:t.id_socket},{$set:{latency:t.latency,date:e.date}}).then(function(e){e||(i.unsubscribeAll(t),i.getEnableDebug()&&console.log(new Date,"Sub Manager","Unsub WS",t.user,t.id_socket,3))},function(){i.unsubscribeAll(t),i.getEnableDebug()&&console.log(new Date,"Sub Manager","Unsub WS",t.user,t.id_socket,4)}))},SubscriptionManager.prototype.subscribe=function(t,e,i,n,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={},u=Object.keys(a.check._schema).filter(function(e){return!e.includes(".")}),l=0;l<s.length;l++)c[u[l]]=s[l];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===i.id_socket&&"Bypass"!==e.messageRoute&&"/"!==e.messageRoute&&e.messageRoute!==t&&!e.messageRoute.startsWith(g)})})).length)&&(this._mainServer.getMethodManager().sendEmail("dev@resolveio.com","SERVER - Detected Undestroyed Subscription - "+this.serverConfig.CLIENT_NAME,"USER: "+i.user+" (Socket: "+i.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===i.id_socket}).forEach(function(e){r.unsubscribe(e.messageRoute,new Date,i,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===i.id_socket&&e.messageId===n})||a.clients.push({id_user:i.id_user,messageId:n,id_socket:i.id_socket,messageRoute:t}):this._subscriptions.push({publication:o,subscriptionData:s,collections:this.getPublicationCollections(o),clients:[{id_user:i.id_user,messageId:n,id_socket:i.id_socket,messageRoute:t}],cacheId:0}),a=a||this._subscriptions.find(function(e){return e.publication===o&&JSON.stringify(e.subscriptionData)===JSON.stringify(s)}),this.processSubscription(a,i,n)}else console.error(new Date,"No Publication: "+o)},SubscriptionManager.prototype.createLoggedInUser=function(n){return __awaiter(this,void 0,void 0,function(){var t=this;return __generator(this,function(e){return[2,new Promise(function(i,e){return __awaiter(t,void 0,void 0,function(){var t;return __generator(this,function(e){return(t=this._websocketManager.getWebSocket(n))?(t={_id:(0,mongo_manager_1.objectIdHexString)(),__v:0,date:new Date,id_user:t.id_user,user:t.user,id_ws:t.id_socket},this._loggedInUsers.push(t),logged_in_users_collection_1.LoggedInUsers.insertOne(t),i(t)):i(null),[2]})})})]})})},SubscriptionManager.prototype.unsubscribe=function(e,t,i,n,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===i.id_user&&r.clients[a].messageId===n&&r.clients[a].id_socket===i.id_socket&&r.clients.splice(a,1)}else console.log("No Publication: "+o)},SubscriptionManager.prototype.unsubscribeAll=function(r){return __awaiter(this,void 0,void 0,function(){var t,i,n,o,s;return __generator(this,function(e){switch(e.label){case 0:return(this._debugUnSubAllHits+=1,r)?[4,logged_in_users_collection_1.LoggedInUsers.findOne({id_ws:r.id_socket})]:[3,2];case 1:for((t=e.sent())&&(0<=this._loggedInUsers.map(function(e){return e._id}).indexOf(t._id)&&this._loggedInUsers.splice(this._loggedInUsers.map(function(e){return e._id}).indexOf(t._id),1),logged_in_users_collection_1.LoggedInUsers.deleteOne({_id:t._id})),i=this._subscriptions.filter(function(e){return e.clients.some(function(e){return e.id_user===r.id_user&&e.id_socket===r.id_socket})}),n=i.length-1;0<=n;n--)for(o=i[n],s=o.clients.length-1;0<=s;s--)o.clients[s].id_socket===r.id_socket&&o.clients.splice(s,1);this._websocketManager.removeWebSocket(r),e.label=2;case 2:return[2]}})})},SubscriptionManager.prototype.getActiveSubscriptions=function(){return this._subscriptions},SubscriptionManager.prototype.getPublicationCollections=function(e){return this._publications[e].collections},SubscriptionManager.prototype.tailOpLog=function(t){return __awaiter(this,void 0,void 0,function(){var i,n=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){i=t;try{this._oplog$=index_1.ResolveIOServer.getMainDB().watch([],{resumeAfter:t})}catch(e){return this._oplog$&&(this._oplog$.removeAllListeners(),this._oplog$.close(),this._oplog$=null),this.tailOpLog(t),[2]}}else this._oplog$=index_1.ResolveIOServer.getMainDB().watch();console.log(new Date,"oplog started"),this._oplog$.on("change",function(t){var e;t.ns&&(e=t.ns.coll,n._debugOplogCollections.some(function(e){return e.collection===t.ns.coll&&e.type===t.operationType})?n._debugOplogCollections.find(function(e){return e.collection===t.ns.coll&&e.type===t.operationType}).hits+=1:n._debugOplogCollections.push({collection:t.ns.coll,type:t.operationType,hits:1}),!e||e.endsWith(".versions")||e.startsWith("monitor-")||"logs"===e||"log-method-latencies"===e||"log-subscriptions"===e||(n._debugOplogHits+=1,"insert"===t.operationType?("support-tickets"===e&&"https://resolveio.com"===n.serverConfig.ROOT_URL&&(n._mainServer.getMethodManager().callMethodInternal.call(n._mainServer.getMethodManager(),"sendSupportTicketEmail",t.documentKey._id),n.invalidatePubsCache(e,"insert")),"method-responses"!==e&&n.invalidatePubsCache(e,"insert")):"update"===t.operationType||"replace"===t.operationType?"method-responses"!==e&&n.invalidatePubsCache(e,"update"):"delete"===t.operationType&&"method-responses"!==e&&n.invalidatePubsCache(e,"delete")),"flags"===e&&flag_collection_1.Flags.findOne({type:"Enable Debug"}).then(function(e){e&&e.value?n._enableDebug=!0:n._enableDebug=!1}),i=t._id)}),this._oplog$.on("error",function(e){console.log(new Date,"oplog error",e),n._oplog$.removeAllListeners(),n._oplog$.close(),n._oplog$=null,n.tailOpLog(i)}),this._oplog$.on("end",function(){console.log(new Date,"oplog end"),n._oplog$.removeAllListeners(),n._oplog$.close(),n._oplog$=null,n.tailOpLog(i)}),this._oplog$.on("close",function(){console.log(new Date,"oplog close"),n._oplog$.removeAllListeners(),n._oplog$=null,n.tailOpLog(i)})}return[2]}})})},SubscriptionManager.prototype.processSubscription=function(i,n,o){return __awaiter(this,void 0,void 0,function(){var t;return __generator(this,function(e){switch(e.label){case 0:if(this._publications[i.publication].user_specific)return[3,8];if(!i.cacheId)return[3,5];e.label=1;case 1:return e.trys.push([1,2,,4]),t=JSON.parse(this._nodeCache.get(i.cacheId),common_1.dateReviver),t={messageId:o,hasError:!1,data:t},this.sendWS(n,t),[3,4];case 2:return e.sent(),this._nodeCache.del(i.cacheId),i.cacheId=0,[4,this.sendDataToAll(i)];case 3:return e.sent(),[3,4];case 4:return[3,7];case 5:return[4,this.sendDataToAll(i)];case 6:e.sent(),e.label=7;case 7:return[3,10];case 8:return[4,this.sendDataToOne(n,o,i,"newSub",i.collections[0])];case 9:e.sent(),e.label=10;case 10:return[2]}})})},SubscriptionManager.prototype.sendDataToOne=function(s,r,a,e,t){return __awaiter(this,void 0,void 0,function(){var t,i,n,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]),[4,(o=this._publications[a.publication].function).call.apply(o,__spreadArray([Object.assign({},this,SubscriptionManager.prototype),s.id_user],a.subscriptionData,!1))];case 2:return o=e.sent(),this._monitorManagerFunction.finishMonitorFunction(t),n={messageId:r,hasError:!1,data:o},this.sendWS(s,n),[3,4];case 3:return i=e.sent(),this._monitorManagerFunction.finishMonitorFunction(t),n={messageId:r,hasError:!0,data:i},this.sendWS(s,n),this._mainServer.getMethodManager().sendEmail("dev@resolveio.com","SERVER - Error Detected - "+this.serverConfig.CLIENT_NAME,"Error Detected During Subscription "+a.publication+" - (sendDataToOne - WS)\n\nData \n"+JSON.stringify(a.subscriptionData,null,2)+"\n\nErrors\n"+JSON.stringify(i,null,2)),[3,4];case 4:return[2]}})})},SubscriptionManager.prototype.sendDataToAll=function(l){return __awaiter(this,void 0,void 0,function(){var t,o,i,n,s,r,a,c,u=this;return __generator(this,function(e){switch(e.label){case 0:return l.clients.length?[3,1]:(l.cacheId&&this._nodeCache.del(l.cacheId),0<=(c=this._subscriptions.findIndex(function(e){return e.publication===l.publication&&JSON.stringify(e.subscriptionData)===JSON.stringify(l.subscriptionData)}))&&this._subscriptions.splice(c,1),[2]);case 1:t=this._monitorManagerFunction.startMonitorFunction("Publication",l.publication,"","",l.subscriptionData),e.label=2;case 2:return e.trys.push([2,5,,7]),[4,(c=this._publications[l.publication].function).call.apply(c,__spreadArray([Object.assign({},this,SubscriptionManager.prototype)],l.subscriptionData,!1))];case 3:return o=e.sent(),this._monitorManagerFunction.finishMonitorFunction(t),[4,Promise.all(l.clients.map(function(n){return __awaiter(u,void 0,void 0,function(){var t,i;return __generator(this,function(e){return(t=this._websocketManager.getWebSocket(n.id_socket))&&t.readyState===t.OPEN&&(i={messageId:n.messageId,hasError:!1,data:o},this.sendWS(t,i)),[2]})})}))];case 4:if(e.sent(),l.cacheId&&this._nodeCache.del(l.cacheId),i=this._cacheId,this._cacheId+=1,this._nodeCache.set(i,JSON.stringify(o)),l.cacheId=i,(i=this._nodeCache.getStats().vsize)>this._heapLimit){for(n=0,s=this._subscriptions.filter(function(e){return e.cacheId&&!e.clients.length}),r=0;r<s.length&&(this._debugRemoveCacheHits+=1,this._nodeCache.del(s[r].cacheId),s[r].cacheId=0,n+=1,!(this._nodeCache.getStats().vsize<.75*this._heapLimit));r++);this._enableDebug&&console.log("Sub Cache: Too Big - "+l.publication+" - Deleted: "+n+" - "+i)}return[3,7];case 5:return a=e.sent(),this._monitorManagerFunction.finishMonitorFunction(t),[4,Promise.all(l.clients.map(function(n){return __awaiter(u,void 0,void 0,function(){var t,i;return __generator(this,function(e){return(t=this._websocketManager.getWebSocket(n.id_socket))&&t.readyState===t.OPEN&&(i={messageId:n.messageId,hasError:!0,data:a},this.sendWS(t,i)),[2]})})}))];case 6:return e.sent(),this._mainServer.getMethodManager().sendEmail("dev@resolveio.com","SERVER - Error Detected - "+this.serverConfig.CLIENT_NAME,"Error Detected During Subscription "+l.publication+" - (sendPubData)\n\nData \n"+JSON.stringify(l.subscriptionData,null,2)+"\n\nErrors\n"+JSON.stringify(a,null,2)),[3,7];case 7: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