import {Log} from "fme-logger"; var L = new Log("routes"); var _ = require('underscore')._ var _m = require('moment'); var ntpsync = require("ntpsync"); var os = require ("os"); var util = require ("util"); var osCommands = ["hostname","type","arch","release","uptime","loadavg","totalmem","freemem","cpus","networkInterfaces"]; var cmd = require('child_process'); //var Analytics = require('../../tradechat-db/models/analytics.js'); import {TradechatServer} from "../tradechat-server"; export class Analytics { timestamp: number = 0; room :any = {}; server:any = {}; } export class PollController { messageKeepAlive = 60*24*1; //minutes to keep messages around tradechatServer = {} as TradechatServer; constructor() { } init = async (tradechatServer:TradechatServer) => { this.tradechatServer = tradechatServer; this.slowPoll(); } slowLoop = async () => { var talog = _m(); var offset = 0; L.debug(talog.format("YYYY-MM-DD:HH:mm:ss"),"Executing the low loop maintenance"); if (talog.day() == 0 || talog.day() == 6 ) { L.info("Chat Garbage Collection: We don't work on the weekends"); return; } var weekendOffset = 0; if (talog.day() == 1 ) weekendOffset = 60*24*2; // for monday add the weekend hours in var timeAgo = talog.subtract(this.messageKeepAlive - offset,"minutes"); var count = await this.tradechatServer.messages.messageDb.countDocuments({"timestamp": {$lt : timeAgo}}) L.debug("### message that meet the time of timeago",timeAgo,count); await this.tradechatServer.messages.messageDb.remove({"timestamp": {$lt : timeAgo}}) // // ==> Update room rosters in case rooms were added async or users // L.info("SlowLoop: Updating the room rosters"); await this.tradechatServer.roster.createAllRosters(); // // Loop through history buffers and clean out the old messages // for (var i=0; i resync the server time // ntpsync.ntpLocalClockDeltaPromise().then((iNTPData:any) => { console.log(`(Local Time - NTP Time) Delta = ${iNTPData.minimalNTPLatencyDelta} ms`); console.log(`Minimal Ping Latency was ${iNTPData.minimalNTPLatency} ms`); console.log(`Total ${iNTPData.totalSampleCount} successful NTP Pings`); }).catch((err:any) => { console.log(err); }); } /* fastLoop = () => { var ts = Date.now(); let S = new Analytics(); S.timestamp = ts; let roomCount = 0; var talog = new Date(); S.room = {}; for (var idx in Rooms) { S.room[idx] = {}; S.room[idx]["name"] = Rooms[idx].name; S.room[idx]["msgCount"] = Rooms[idx].msgCount; Rooms[idx].msgCount = 0; S.room[idx].userCount =_.size(Rooms[idx].members); } S.save(function(err) { if (err) { L.error("ERROR Saving stats",err); } }); } */ slowPoll = () => { setInterval(this.slowLoop, 1000 * 60 * 30) }; }