switched everything over to Mongoose

This commit is contained in:
Luke Berndt
2023-04-10 22:51:57 -04:00
parent 0062c404bf
commit 6fbd97bbb6
9 changed files with 211 additions and 259 deletions

View File

@@ -1,76 +1,65 @@
var util = require("util");
var db = require('./db');
const SystemStat = require("./models/system_stat");
var System = require("./models/system");
var talkgroupStats = {};
var callTotals = {};
var uploadErrors = {};
let decodeErrorsFreq = {};
const timePeriod = 15; // in minutes
var spots = (24*60) / timePeriod; // the number of spots needed to keep track of 24 hours of stats
var spots = (24 * 60) / timePeriod; // the number of spots needed to keep track of 24 hours of stats
async function updateActiveSystems() {
// Go through all of the Systems
const sysCollection = db.get().collection('systems');
var cursor = sysCollection.find();
await cursor.forEach(function(item) {
// go through all the systems
// if you have recieved some calls during that last period, make the system active
if ((callTotals[item.shortName] != undefined) && (callTotals[item.shortName][0] > 0)) {
const active = true;
const callAvg = callTotals[item.shortName][0] / timePeriod;
const query = { shortName: item.shortName};
const update = { $set: {"active": active,"callAvg": callAvg,"lastActive": new Date() }};
const options = {upsert: true};
sysCollection.updateOne(query, update, options);
} else {
const query = { shortName: item.shortName};
const update = { $set: {"active": false, "callAvg": 0}};
const options = {upsert: true};
for await (const item of System.find()) {
// go through all the systems
// if you have recieved some calls during that last period, make the system active
if ((callTotals[item.shortName] != undefined) && (callTotals[item.shortName][0] > 0)) {
item.active = true;
item.callAvg = callTotals[item.shortName][0] / timePeriod;
item.lastActive = new Date();
await item.Save();
} else {
item.active = false;
item.callAvg = 0;
await item.save();
}
sysCollection.updateOne(query, update, options);
}
});
};
}
exports.initStats = async function() {
exports.initStats = async function () {
// get the System Stats collection
db.get().createCollection("system_stats").catch((err)=>{console.log("System Stats collection is ready")});
const statsCollection = db.get().collection('system_stats');
var cursor = statsCollection.find();
// go through each of the Systems in Stats
await cursor.forEach(function(item) {
// Talkgroup Stats
if (item.talkgroupStats != undefined) {
talkgroupStats[item.shortName] = item.talkgroupStats;
}
for await (const item of SystemStat.find()) {
// Talkgroup Stats
if (item.talkgroupStats != undefined) {
talkgroupStats[item.shortName] = item.talkgroupStats;
}
// Decode Errors
if (item.decodeErrorsFreq != undefined) {
decodeErrorsFreq[item.shortName] = item.decodeErrorsFreq;
}
// Decode Errors
if (item.decodeErrorsFreq != undefined) {
decodeErrorsFreq[item.shortName] = item.decodeErrorsFreq;
}
// Upload Error Totals
if (item.uploadErrors != undefined) {
uploadErrors[item.shortName] = item.uploadErrors;
}
// Upload Error Totals
if (item.uploadErrors != undefined) {
uploadErrors[item.shortName] = item.uploadErrors;
}
if (item.callTotals != undefined) {
callTotals[item.shortName] = item.callTotals;
}
});
if (item.callTotals != undefined) {
callTotals[item.shortName] = item.callTotals;
}
};
}
// keeps track of the number of calls that get uploaded with an audio file
exports.addError = function(call) {
exports.addError = function (call) {
// if there is no Array associated with the system.
if (uploadErrors[call.shortName] == undefined) {
@@ -85,10 +74,7 @@ exports.addError = function(call) {
}
// Keeps track of the number of calls for each talkgroup for a system
exports.addCall = function(call) {
exports.addCall = function (call) {
// if you haven't started keeping track of stats for the System yet
if (talkgroupStats[call.shortName] == undefined) {
talkgroupStats[call.shortName] = {};
@@ -139,147 +125,147 @@ exports.addCall = function(call) {
// This gets called when a Time Period is up
exports.shiftStats = async function() {
const statsCollection = db.get().collection('system_stats');
exports.shiftStats = async function () {
// for all the systems in Error Stats
for (var shortName in uploadErrors) {
if (uploadErrors.hasOwnProperty(shortName)) {
// Update the DB with Error Stats and Error Totals
const query = { shortName: shortName};
const update = { $set: { "uploadErrors": uploadErrors[shortName]}};
const options = {upsert: true};
// for all the systems in Error Stats
for (var shortName in uploadErrors) {
if (uploadErrors.hasOwnProperty(shortName)) {
statsCollection.updateOne(query, update, options);
// move everything back one after updating
for (var j = spots - 1; j > 0; j--) {
uploadErrors[shortName][j] = uploadErrors[shortName][j - 1];
}
// reset the first spot back to 0, so you can count the next period
uploadErrors[shortName][0] = 0;
// Update the DB with Error Stats and Error Totals
const query = { shortName: shortName };
const update = { $set: { "uploadErrors": uploadErrors[shortName] } };
const options = { upsert: true };
await SystemStat.updateOne(query, update, options);
// move everything back one after updating
for (var j = spots - 1; j > 0; j--) {
uploadErrors[shortName][j] = uploadErrors[shortName][j - 1];
}
// reset the first spot back to 0, so you can count the next period
uploadErrors[shortName][0] = 0;
}
}
// for each system in decodeErrorsFreq
for (let shortName in decodeErrorsFreq) {
// if the system is in decodeErrorsFreq
if (decodeErrorsFreq.hasOwnProperty(shortName)) {
var sysErrors = decodeErrorsFreq[shortName];
// for each system in decodeErrorsFreq
for (let shortName in decodeErrorsFreq) {
// for each freq in that systems stats
for (var freqNum in sysErrors ) {
if (sysErrors.hasOwnProperty(freqNum)) {
var freqErrors = sysErrors[freqNum];
// if the system is in decodeErrorsFreq
if (decodeErrorsFreq.hasOwnProperty(shortName)) {
var sysErrors = decodeErrorsFreq[shortName];
// move the history for that freq back
for (let j = spots - 1; j > 0; j--) {
let i = j - 1;
freqErrors.errorHistory[j] = freqErrors.errorHistory[i];
freqErrors.spikeHistory[j] = freqErrors.spikeHistory[i];
}
// for each freq in that systems stats
for (var freqNum in sysErrors) {
if (sysErrors.hasOwnProperty(freqNum)) {
var freqErrors = sysErrors[freqNum];
if (freqErrors.totalLen > 0) {
freqErrors.errorHistory[0] = (freqErrors.errors / freqErrors.totalLen);
freqErrors.spikeHistory[0] = (freqErrors.spikes / freqErrors.totalLen);
} else {
freqErrors.errorHistory[0] = 0;
freqErrors.spikeHistory[0] = 0;
}
freqErrors.totalLen = 0;
freqErrors.errors = 0;
freqErrors.spikes = 0;
// move the history for that freq back
for (let j = spots - 1; j > 0; j--) {
let i = j - 1;
freqErrors.errorHistory[j] = freqErrors.errorHistory[i];
freqErrors.spikeHistory[j] = freqErrors.spikeHistory[i];
}
if (freqErrors.totalLen > 0) {
freqErrors.errorHistory[0] = (freqErrors.errors / freqErrors.totalLen);
freqErrors.spikeHistory[0] = (freqErrors.spikes / freqErrors.totalLen);
} else {
freqErrors.errorHistory[0] = 0;
freqErrors.spikeHistory[0] = 0;
}
freqErrors.totalLen = 0;
freqErrors.errors = 0;
freqErrors.spikes = 0;
}
}
const query = { shortName: shortName };
const update = { $set: { "decodeErrorsFreq": decodeErrorsFreq[shortName] } };
const options = { upsert: true };
await SystemStat.updateOne(query, update, options);
}
}
// for each system in talkgroupStats
for (let shortName in talkgroupStats) {
let callTotal = 0;
// if the system is in stats
if (talkgroupStats.hasOwnProperty(shortName)) {
var sysTalkgroupStats = talkgroupStats[shortName];
// for each talkgroup in that systems stats
for (var talkgroupNum in sysTalkgroupStats) {
if (sysTalkgroupStats.hasOwnProperty(talkgroupNum)) {
var tg = sysTalkgroupStats[talkgroupNum];
var tgHistoryTotal = 0;
// move the history for that talkgroup back
for (let j = spots - 1; j > 0; j--) {
let i = j - 1;
tgHistoryTotal += tg.callCountHistory[i];
tg.callCountHistory[j] = tg.callCountHistory[i];
tg.callAvgLenHistory[j] = tg.callAvgLenHistory[i];
}
// add to the total for that group
callTotal += tg.calls;
// figure out the history for current period in the history for this talkgroup;
tg.callCountHistory[0] = tg.calls;
tgHistoryTotal += tg.calls;
if (tg.calls > 0) {
tg.callAvgLenHistory[0] = Math.floor(tg.totalLen / tg.calls);
} else {
tg.callAvgLenHistory[0] = 0;
}
tg.calls = 0;
tg.totalLen = 0;
if (tgHistoryTotal == 0) {
// there has been no recent activity on this talkgroup. remove it from the stats.
delete stat[talkgroupNum];
}
}
const query = { shortName: shortName};
const update = { $set: { "decodeErrorsFreq": decodeErrorsFreq[shortName]}};
const options = {upsert: true};
statsCollection.updateOne(query, update, options);
}
}
// for each system in talkgroupStats
for (let shortName in talkgroupStats) {
let callTotal = 0;
// if the system is in stats
if (talkgroupStats.hasOwnProperty(shortName)) {
var sysTalkgroupStats = talkgroupStats[shortName];
// for each talkgroup in that systems stats
for (var talkgroupNum in sysTalkgroupStats ) {
if (sysTalkgroupStats.hasOwnProperty(talkgroupNum)) {
var tg = sysTalkgroupStats[talkgroupNum];
var tgHistoryTotal = 0;
// move the history for that talkgroup back
for (let j = spots - 1; j > 0; j--) {
let i = j - 1;
tgHistoryTotal += tg.callCountHistory[i];
tg.callCountHistory[j] = tg.callCountHistory[i];
tg.callAvgLenHistory[j] = tg.callAvgLenHistory[i];
}
// add to the total for that group
callTotal += tg.calls;
// figure out the history for current period in the history for this talkgroup;
tg.callCountHistory[0] = tg.calls;
tgHistoryTotal += tg.calls;
if (tg.calls > 0) {
tg.callAvgLenHistory[0] = Math.floor(tg.totalLen / tg.calls);
} else {
tg.callAvgLenHistory[0] = 0;
}
tg.calls = 0;
tg.totalLen = 0;
if (tgHistoryTotal ==0 ) {
// there has been no recent activity on this talkgroup. remove it from the stats.
delete stat[talkgroupNum];
}
}
// figure out call totals for this shortName/Sys
if (callTotals[shortName] == undefined) {
callTotals[shortName] = new Array();
for (var j = 0; j < spots; j++) {
callTotals[shortName][j] = 0;
}
// figure out call totals for this shortName/Sys
if (callTotals[shortName] == undefined) {
callTotals[shortName] = new Array();
for (var j = 0; j < spots; j++) {
callTotals[shortName][j] = 0;
}
}
for (var j = spots - 1; j > 0; j--) {
callTotals[shortName][j] = callTotals[shortName][j - 1];
}
callTotals[shortName][0] = callTotal;
const query = { shortName: shortName};
const update = { $set: { "talkgroupStats": talkgroupStats[shortName], "callTotals": callTotals[shortName] }};
const options = {upsert: true};
statsCollection.updateOne(query, update, options);
}
for (var j = spots - 1; j > 0; j--) {
callTotals[shortName][j] = callTotals[shortName][j - 1];
}
callTotals[shortName][0] = callTotal;
const query = { shortName: shortName };
const update = { $set: { "talkgroupStats": talkgroupStats[shortName], "callTotals": callTotals[shortName] } };
const options = { upsert: true };
await SystemStat.updateOne(query, update, options);
}
}
updateActiveSystems();
}
exports.callTotals = function(shortName) {
exports.callTotals = function (shortName) {
return callTotals[shortName];
}
exports.talkgroupStats = function(shortName) {
exports.talkgroupStats = function (shortName) {
return talkgroupStats[shortName];
}
exports.uploadErrors = function(shortName) {
exports.uploadErrors = function (shortName) {
return uploadErrors[shortName];
}
exports.decodeErrorsFreq = function(shortName) {
exports.decodeErrorsFreq = function (shortName) {
return decodeErrorsFreq[shortName];
}