upgraded most of the Backend

This commit is contained in:
Luke Berndt
2023-04-08 12:57:12 -04:00
parent 329dc7f47b
commit 6ae3f25215
14 changed files with 977 additions and 3068 deletions

View File

@@ -28,7 +28,7 @@ const System = () => {
const { data: systemsData, isSuccess: isSystemsSuccess } = useGetSystemsQuery(); const { data: systemsData, isSuccess: isSystemsSuccess } = useGetSystemsQuery();
const { data: talkgroupsData, isSuccess: isTalkgroupsSuccess } = useGetTalkgroupsQuery(shortName); const { data: talkgroupsData, isSuccess: isTalkgroupsSuccess } = useGetTalkgroupsQuery(shortName);
const { data: groupsData } = useGetGroupsQuery(shortName); const { data: groupsData } = useGetGroupsQuery(shortName);
const { data: systemErrorData, isSuccess: isErrorsSuccess } = useGetErrorsQuery(shortName); //const { data: systemErrorData, isSuccess: isErrorsSuccess } = useGetErrorsQuery(shortName);
const [deleteGroupAPI] = useDeleteGroupMutation(); const [deleteGroupAPI] = useDeleteGroupMutation();
const [reorderGroupsAPI] = useSaveGroupOrderMutation(); const [reorderGroupsAPI] = useSaveGroupOrderMutation();
const [importTalkgroupsAPI] = useImportTalkgroupsMutation(); const [importTalkgroupsAPI] = useImportTalkgroupsMutation();
@@ -89,7 +89,10 @@ const System = () => {
const processStatistics = (statistic) => { const processStatistics = (statistic) => {
var callTotals = []; var callTotals = [];
var errorTotals = []; var uploadErrors= [];
let talkgroupStats = [];
let decodeErrorsFreq = [];
const now = new Date(); const now = new Date();
var maxDate = now; var maxDate = now;
var minDate = now; var minDate = now;
@@ -105,13 +108,13 @@ const System = () => {
maxValue = statistic.callTotals[j]; maxValue = statistic.callTotals[j];
callTotals.push({ y: statistic.callTotals[j], x: time }); callTotals.push({ y: statistic.callTotals[j], x: time });
} }
for (let j = 0; j < statistic.errorTotals.length; j++) { for (let j = 0; j < statistic.uploadErrors.length; j++) {
let spotsBack = statistic.errorTotals.length - j; let spotsBack = statistic.uploadErrors.length - j;
let time = new Date(now - spotsBack * 15 * MS_PER_MINUTE); let time = new Date(now - spotsBack * 15 * MS_PER_MINUTE);
if (time < minDate) minDate = time; if (time < minDate) minDate = time;
if (statistic.errorTotals[j] > maxValue) if (statistic.uploadErrors[j] > maxValue)
maxValue = statistic.errorTotals[j]; maxValue = statistic.uploadErrors[j];
errorTotals.push({ x: time, y: statistic.errorTotals[j] }); uploadErrors.push({ x: time, y: statistic.uploadErrors[j] });
} }
const callData = { const callData = {
minDate: minDate, minDate: minDate,
@@ -119,7 +122,7 @@ const System = () => {
minValue: minValue, minValue: minValue,
maxValue: maxValue, maxValue: maxValue,
callTotals: callTotals, callTotals: callTotals,
errorTotals: errorTotals errorTotals: uploadErrors
}; };
setCallData(callData); setCallData(callData);
@@ -234,12 +237,12 @@ const System = () => {
processStatistics(systemsData.stats[shortName]) processStatistics(systemsData.stats[shortName])
} }
}, [isSystemsSuccess]); }, [isSystemsSuccess]);
/*
useEffect(() => { useEffect(() => {
if (isErrorsSuccess) { if (isErrorsSuccess) {
processErrors(systemErrorData) processErrors(systemErrorData)
} }
}, [systemErrorData]); }, [systemErrorData]);*/
let fileInput = null; let fileInput = null;
var location = ""; var location = "";

View File

@@ -1,11 +1,7 @@
var path = require("path"); var path = require("path");
var express = require("express"); var express = require("express");
var session = require("express-session");
var bodyParser = require("body-parser"); var bodyParser = require("body-parser");
var connectMongo = require("connect-mongo");
var secrets = require("./secrets");
const MongoStore = connectMongo(session)
var cookie_domain = process.env['REACT_APP_COOKIE_DOMAIN'] != null ? process.env['REACT_APP_COOKIE_DOMAIN'] : '.openmhz.com'; //'https://s3.amazonaws.com/robotastic'; var cookie_domain = process.env['REACT_APP_COOKIE_DOMAIN'] != null ? process.env['REACT_APP_COOKIE_DOMAIN'] : '.openmhz.com'; //'https://s3.amazonaws.com/robotastic';
var backend_server = process.env['REACT_APP_BACKEND_SERVER'] != null ? process.env['REACT_APP_BACKEND_SERVER'] : 'https://api.openmhz.com'; var backend_server = process.env['REACT_APP_BACKEND_SERVER'] != null ? process.env['REACT_APP_BACKEND_SERVER'] : 'https://api.openmhz.com';
var frontend_server = process.env['REACT_APP_FRONTEND_SERVER'] != null ? process.env['REACT_APP_FRONTEND_SERVER'] : 'https://openmhz.com'; var frontend_server = process.env['REACT_APP_FRONTEND_SERVER'] != null ? process.env['REACT_APP_FRONTEND_SERVER'] : 'https://openmhz.com';
@@ -14,7 +10,7 @@ var admin_server = process.env['REACT_APP_ADMIN_SERVER'] != null ? process.env['
var dev_server = frontend_server + ":3000" var dev_server = frontend_server + ":3000"
module.exports = function(app, passport) { module.exports = function(app) {
app.set("port", 3005) app.set("port", 3005)
//app.set('trust proxy', true); //app.set('trust proxy', true);
// X-Powered-By header has no functional value. // X-Powered-By header has no functional value.
@@ -26,24 +22,6 @@ module.exports = function(app, passport) {
app.use(bodyParser.urlencoded({ extended: true })) app.use(bodyParser.urlencoded({ extended: true }))
app.use(express.static(path.join(process.cwd(), 'public'))); app.use(express.static(path.join(process.cwd(), 'public')));
const sess = {
resave: true,
saveUninitialized: true,
secret: secrets.sessionSecret,
proxy: true,
name: "sessionId",
cookie: {
httpOnly: true,
secure: false,
domain: cookie_domain
},
store: new MongoStore({
url: secrets.db,
autoReconnect: true
})
}
var node_env = process.env.NODE_ENV; var node_env = process.env.NODE_ENV;
console.log('--------------------------'); console.log('--------------------------');
console.log('===> 😊 Starting Server . . .'); console.log('===> 😊 Starting Server . . .');
@@ -51,15 +29,8 @@ module.exports = function(app, passport) {
if(node_env === 'production') { if(node_env === 'production') {
console.log('===> 🚦 Note: In order for authentication to work in production'); console.log('===> 🚦 Note: In order for authentication to work in production');
console.log('===> you will need a secure HTTPS connection'); console.log('===> you will need a secure HTTPS connection');
sess.cookie.secure = true; // Serve secure cookies
} }
app.use(session(sess))
app.use(passport.initialize())
app.use(passport.session())
app.use('/*', function(req, res, next) { app.use('/*', function(req, res, next) {
var allowedOrigins = [ admin_server]; var allowedOrigins = [ admin_server];
allowedOrigins.push(frontend_server); allowedOrigins.push(frontend_server);

View File

@@ -1,31 +0,0 @@
var LocalStrategy = require('passport-local').Strategy;
var User = require("../../models/user");
/*
By default, LocalStrategy expects to find credentials in parameters named username and password.
If your site prefers to name these fields differently, options are available to change the defaults.
*/
module.exports = new LocalStrategy({
usernameField: "email"
}, (email, password, done) => {
User.findOne({
$or: [{
email
}, {
local: {
email
}
}]
}, (err, user) => {
if(!user) return done(null, false, { message: `Email ${email} not found` })
user.comparePassword(password, (err, isMatch) => {
if (isMatch) {
return done(null, user)
} else {
return done(null, false, { message: "Invalid email or password" })
}
})
})
})

View File

@@ -1,25 +0,0 @@
/* Initializing PassportJS */
var User = require("../models/user");
var local = require("./passport-strategies/local");
module.exports =function (app, passport) {
// Configure Passport authenticated session persistence.
//
// In order to restore authentication state across HTTP requests, Passport needs
// to serialize users into and deserialize users out of the session. The
// typical implementation of this is as simple as supplying the user ID when
// serializing, and querying the user record by ID from the database when
// deserializing.
passport.serializeUser((user, done) => {
done(null, user.id)
})
passport.deserializeUser((id, done) => {
User.findById(id, (err, user) => {
done(err, user)
})
})
// use the following strategies
passport.use(local)
}

View File

@@ -2,7 +2,7 @@
var ObjectID = require('mongodb').ObjectID; var ObjectID = require('mongodb').ObjectID;
var db = require('../db'); var db = require('../db');
var mongoose = require("mongoose"); var mongoose = require("mongoose");
var {callModel:Call} = require("../models/call"); var { callModel: Call } = require("../models/call");
var Star = require("../models/star"); var Star = require("../models/star");
var defaultNumResults = 50; var defaultNumResults = 50;
@@ -11,7 +11,7 @@ var defaultNumResults = 50;
var channels = {}; var channels = {};
function get_calls(query, numResults, res) { async function get_calls(query, numResults, res) {
var calls = []; var calls = [];
var fields = { var fields = {
@@ -27,42 +27,40 @@ function get_calls(query, numResults, res) {
url: true url: true
}; };
db.get().collection('calls', function(err, transCollection) { const transCollection = db.get().collection('calls');
transCollection.find(query.filter, fields, function(err, cursor) { const sort = { length: -1 };
if (err) { try {
console.warn("Error - get_calls() Could not find item " + err + " filter: " + query.filter); let cursor = transCollection.find(query.filter, fields).sort(sort).limit(numResults);
res.send(404, 'Sorry, we cannot find that!'); await cursor.forEach(function (item) {
return;
} call = {
cursor.sort(query.sort_order).limit(numResults).each(function(err, item) { _id: item._id.toHexString(),
if (item) { talkgroupNum: item.talkgroupNum,
call = { url: item.url,
_id: item._id.toHexString(), filename: item.path + item.name,
talkgroupNum: item.talkgroupNum, time: item.time,
url: item.url, srcList: item.srcList,
filename: item.path + item.name, star: item.star,
time: item.time, freq: item.freq,
srcList: item.srcList, len: Math.round(item.len)
star: item.star, };
freq: item.freq, calls.push(call);
len: Math.round(item.len)
};
calls.push(call);
} else {
res.contentType('json');
res.send(JSON.stringify({
calls: calls,
direction: query.direction
}));
}
});
}); });
});
res.contentType('json');
res.send(JSON.stringify({
calls: calls,
direction: query.direction
}));
} catch (err) {
console.warn("Error - get_calls() Could not find item " + err + " filter: " + query.filter);
res.send(404, 'Sorry, we cannot find that!');
return;
};
} }
function build_filter(filter_type, code, start_time, direction, shortName, numResults, starred, res) { async function build_filter(filter_type, code, start_time, direction, shortName, numResults, starred, res) {
var filter = {}; var filter = {};
var query = {}; var query = {};
var FilterType = { var FilterType = {
@@ -71,12 +69,7 @@ function build_filter(filter_type, code, start_time, direction, shortName, numRe
Group: 2, Group: 2,
Unit: 3 Unit: 3
}; };
/* removed to see if it helps queries
if (starred) {
filter.star = {
$gt: 0
};
}*/
if (start_time) { if (start_time) {
var start = new Date(start_time); var start = new Date(start_time);
@@ -112,45 +105,32 @@ function build_filter(filter_type, code, start_time, direction, shortName, numRe
if (filter_type) { if (filter_type) {
if ((filter_type == "group") && code && (code.indexOf(',') == -1)) { if ((filter_type == "group") && code && (code.indexOf(',') == -1)) {
db.get().collection("groups", function(err, groupCollection) { const groupCollection = db.get().collection("groups");
if (err || !groupCollection) {
console.warn("[" + shortName + "] Error - build_filter() Group Collection does not exist " + err);
res.contentType('json');
res.send(JSON.stringify( {
message: 'That Group ID doesnt exist.'
}));
return;
}
groupCollection.findOne({
'shortName': shortName,
'_id': ObjectID.createFromHexString(code)
}, function(err, group) {
if (err) {
console.warn("[" + shortName + "] Error - build_filter() Group ID does not exist " + err);
res.contentType('json');
res.send(JSON.stringify( {
message: 'That Group ID doesnt exist.'
}));
} else if (!group) {
console.warn("[" + shortName + "] Error - build_filter() group is null " + err);
res.contentType('json');
res.send(JSON.stringify({
message: 'That Group ID doesnt exist.'
}));
} else{
filter.talkgroupNum = {
$in: group.talkgroups
};
query['filter'] = filter;
get_calls(query, numResults, res);
} const group = await groupCollection.findOne({
}); 'shortName': shortName,
'_id': ObjectID.createFromHexString(code)
}); });
if (!group) {
console.warn("[" + shortName + "] Error - build_filter() group is null " + err);
res.contentType('json');
res.status(404);
res.send(JSON.stringify({
message: 'That Group ID doesnt exist.'
}));
}
filter.talkgroupNum = {
$in: group.talkgroups
};
query['filter'] = filter;
get_calls(query, numResults, res);
} else { } else {
if ((filter_type == "talkgroup") || (filter_type == "group")) { if ((filter_type == "talkgroup") || (filter_type == "group")) {
if (code) { if (code) {
var codeArray = code.split(',').map(function(item) { var codeArray = code.split(',').map(function (item) {
return parseInt(item, 10); return parseInt(item, 10);
}); });
filter.talkgroupNum = { filter.talkgroupNum = {
@@ -161,7 +141,7 @@ function build_filter(filter_type, code, start_time, direction, shortName, numRe
if (filter_type == "unit") { if (filter_type == "unit") {
if (code) { if (code) {
var codeArray = code.split(',').map(function(item) { var codeArray = code.split(',').map(function (item) {
return parseInt(item, 10); return parseInt(item, 10);
}); });
filter.srcListv = { filter.srcListv = {
@@ -180,7 +160,7 @@ function build_filter(filter_type, code, start_time, direction, shortName, numRe
} }
exports.get_card = function(req, res) { exports.get_card = async function (req, res) {
var objectId = req.params.id; var objectId = req.params.id;
try { try {
var o_id = ObjectID.createFromHexString(objectId); var o_id = ObjectID.createFromHexString(objectId);
@@ -193,28 +173,24 @@ exports.get_card = function(req, res) {
})); }));
return; return;
} }
db.get().collection('calls', function(err, transCollection) { const transCollection = db.get().collection('calls');
transCollection.findOne({ const item = await transCollection.findOne({ '_id': o_id });
'_id': o_id
}, //console.log(util.inspect(item));
function(err, item) { if (item) {
//console.log(util.inspect(item)); var time = new Date(item.time);
if (item) { var timeString = time.toLocaleTimeString("en-US");
var time = new Date(item.time); var dateString = time.toDateString();
var timeString = time.toLocaleTimeString("en-US"); res.render('card', {
var dateString = time.toDateString(); item: item,
res.render('card', { channel: channels[item.talkgroupNum],
item: item, time: timeString,
channel: channels[item.talkgroupNum], date: dateString
time: timeString, });
date: dateString } else {
}); console.warn("Error - /card/:id Could not find Item " + err);
} else { res.send(404, 'Sorry, we cannot find that!');
console.warn("Error - /card/:id Could not find Item " + err); }
res.send(404, 'Sorry, we cannot find that!');
}
});
});
} }
function package_call(item) { function package_call(item) {
@@ -240,7 +216,7 @@ function package_call(item) {
return call; return call;
} }
exports.remove_star = function(req, res, next) { exports.remove_star = async function (req, res, next) {
var objectId = req.params.id; var objectId = req.params.id;
try { try {
var o_id = ObjectID.createFromHexString(objectId); var o_id = ObjectID.createFromHexString(objectId);
@@ -254,28 +230,26 @@ exports.remove_star = function(req, res, next) {
})); }));
return; return;
} }
Call.findOneAndUpdate({ _id: objectId }, { $inc: { star: -1 } }, {new: true },function(err, item) { const item = await Call.findOneAndUpdate({ _id: objectId }, { $inc: { star: -1 } }, { new: true });
if (err) { if (!item) {
res.status(500); res.status(500);
res.send(JSON.stringify({ res.send(JSON.stringify({
success: false, success: false,
message: err, "_id": objectId
"_id": objectId }));
})); } else {
} else { var call = package_call(item);
var call = package_call(item); req.call = call;
req.call = call; res.send(JSON.stringify({
res.send(JSON.stringify({ success: true,
success: true, call: call
call: call }));
})); next();
next(); }
}
})
} }
exports.add_star = function(req, res, next) { exports.add_star = async function (req, res, next) {
var objectId = req.params.id; var objectId = req.params.id;
try { try {
var o_id = ObjectID.createFromHexString(objectId); var o_id = ObjectID.createFromHexString(objectId);
@@ -289,28 +263,27 @@ exports.add_star = function(req, res, next) {
})); }));
return; return;
} }
Call.findOneAndUpdate({ _id: objectId }, { $inc: { star: 1 } }, {new: true },function(err, item) { const item = await Call.findOneAndUpdate({ _id: objectId }, { $inc: { star: 1 } }, { new: true });
if (err) { if (!item) {
res.status(500); res.status(500);
res.send(JSON.stringify({ res.send(JSON.stringify({
success: false, success: false,
message: err, "_id": objectId
"_id": objectId }));
})); } else {
} else { var call = package_call(item);
var call = package_call(item); req.call = call;
req.call = call; res.send(JSON.stringify({
res.send(JSON.stringify({ success: true,
success: true, call: call
call: call }));
})); next();
next(); }
}
})
} }
exports.get_call = function(req, res) { exports.get_call = async function (req, res) {
var objectId = req.params.id; var objectId = req.params.id;
try { try {
var o_id = ObjectID.createFromHexString(objectId); var o_id = ObjectID.createFromHexString(objectId);
@@ -324,37 +297,34 @@ exports.get_call = function(req, res) {
})); }));
return; return;
} }
db.get().collection('calls', function(err, transCollection) { const transCollection = db.get().collection('calls');
transCollection.findOne({ const item = await transCollection.findOne({ '_id': o_id });
'_id': o_id
}, if (item) {
function(err, item) { var call = package_call(item);
if (item) { res.contentType('json');
var call = package_call(item); res.send(JSON.stringify({
res.contentType('json'); success: true,
res.send(JSON.stringify({ call: call
success: true, })
call: call}) );
);
} else {
console.warn("[" + req.params.shortName + "] Error - /:shortName/call/:id Could not find item " + err + " ID: " + objectId);
res.status(404);
res.send(JSON.stringify({
success: false,
"_id": objectId
}));
}
} else {
console.warn("[" + req.params.shortName + "] Error - /:shortName/call/:id Could not find item " + err + " ID: " + objectId);
res.status(404);
res.send(JSON.stringify({
success: false,
error: err,
"_id": objectId
}));
}
});
});
} }
exports.get_latest_calls = function(req, res) { exports.get_latest_calls = function (req, res) {
var filter_code = req.query["filter-code"]; var filter_code = req.query["filter-code"];
var filter_type = req.query["filter-type"]; var filter_type = req.query["filter-type"];
var starred = req.query["filter-starred"] === 'true'?true:false; var starred = req.query["filter-starred"] === 'true' ? true : false;
var short_name = req.params.shortName.toLowerCase(); var short_name = req.params.shortName.toLowerCase();
//console.log("[" + short_name + "] Latest - Call Get Filter code: " + filter_code + " Filter Type: " + filter_type ); //console.log("[" + short_name + "] Latest - Call Get Filter code: " + filter_code + " Filter Type: " + filter_type );
@@ -362,10 +332,10 @@ exports.get_latest_calls = function(req, res) {
} }
exports.get_next_calls = function(req, res) { exports.get_next_calls = function (req, res) {
var filter_code = req.query["filter-code"]; var filter_code = req.query["filter-code"];
var filter_type = req.query["filter-type"]; var filter_type = req.query["filter-type"];
var starred = req.query["filter-starred"] === 'true'?true:false; var starred = req.query["filter-starred"] === 'true' ? true : false;
var start_time = parseInt(req.query["time"]); var start_time = parseInt(req.query["time"]);
var short_name = req.params.shortName.toLowerCase(); var short_name = req.params.shortName.toLowerCase();
//console.log("[" + short_name + "] Next Calls - time: " + start_time + " Filter code: " + filter_code + " Filter Type: " + filter_type); //console.log("[" + short_name + "] Next Calls - time: " + start_time + " Filter code: " + filter_code + " Filter Type: " + filter_type);
@@ -373,10 +343,10 @@ exports.get_next_calls = function(req, res) {
build_filter(filter_type, filter_code, start_time, 'newer', short_name, 1, starred, res); build_filter(filter_type, filter_code, start_time, 'newer', short_name, 1, starred, res);
} }
exports.get_newer_calls = function(req, res) { exports.get_newer_calls = function (req, res) {
var filter_code = req.query["filter-code"]; var filter_code = req.query["filter-code"];
var filter_type = req.query["filter-type"]; var filter_type = req.query["filter-type"];
var starred = req.query["filter-starred"] === 'true'?true:false; var starred = req.query["filter-starred"] === 'true' ? true : false;
var start_time = parseInt(req.query["time"]); var start_time = parseInt(req.query["time"]);
var short_name = req.params.shortName.toLowerCase(); var short_name = req.params.shortName.toLowerCase();
//console.log("[" + short_name + "] Newer Calls - time: " + start_time + " Filter code: " + filter_code + " Filter Type: " + filter_type ); //console.log("[" + short_name + "] Newer Calls - time: " + start_time + " Filter code: " + filter_code + " Filter Type: " + filter_type );
@@ -384,10 +354,10 @@ exports.get_newer_calls = function(req, res) {
build_filter(filter_type, filter_code, start_time, 'newer', short_name, defaultNumResults, starred, res); build_filter(filter_type, filter_code, start_time, 'newer', short_name, defaultNumResults, starred, res);
} }
exports.get_older_calls = function(req, res) { exports.get_older_calls = function (req, res) {
var filter_code = req.query["filter-code"]; var filter_code = req.query["filter-code"];
var filter_type = req.query["filter-type"]; var filter_type = req.query["filter-type"];
var starred = req.query["filter-starred"] === 'true'?true:false; var starred = req.query["filter-starred"] === 'true' ? true : false;
var start_time = parseInt(req.query["time"]); var start_time = parseInt(req.query["time"]);
var short_name = req.params.shortName.toLowerCase(); var short_name = req.params.shortName.toLowerCase();
//console.log("[" + short_name + "] Older Calls - time: " + start_time + " Filter code: " + filter_code + " Filter Type: " + filter_type); //console.log("[" + short_name + "] Older Calls - time: " + start_time + " Filter code: " + filter_code + " Filter Type: " + filter_type);
@@ -397,10 +367,10 @@ exports.get_older_calls = function(req, res) {
//Delete this after I fix the iPhone app //Delete this after I fix the iPhone app
exports.get_iphone_calls = function(req, res) { exports.get_iphone_calls = function (req, res) {
var filter_code = req.query["filter-code"]; var filter_code = req.query["filter-code"];
var filter_type = req.query["filter-type"]; var filter_type = req.query["filter-type"];
var starred = req.query["filter-starred"] === 'true'?true:false; var starred = req.query["filter-starred"] === 'true' ? true : false;
var start_time = parseInt(req.params.time); var start_time = parseInt(req.params.time);
var short_name = req.params.shortName.toLowerCase(); var short_name = req.params.shortName.toLowerCase();
//console.log("[" + short_name + "] iPhone Newer Calls - time: " + start_time + " Filter code: " + filter_code + " Filter Type: " + filter_type); //console.log("[" + short_name + "] iPhone Newer Calls - time: " + start_time + " Filter code: " + filter_code + " Filter Type: " + filter_type);
@@ -408,10 +378,10 @@ exports.get_iphone_calls = function(req, res) {
build_filter(filter_type, filter_code, start_time, 'older', short_name, defaultNumResults, starred, res); build_filter(filter_type, filter_code, start_time, 'older', short_name, defaultNumResults, starred, res);
} }
exports.get_calls = function(req, res) { exports.get_calls = function (req, res) {
var filter_code = req.query["filter-code"]; var filter_code = req.query["filter-code"];
var filter_type = req.query["filter-type"]; var filter_type = req.query["filter-type"];
var starred = req.query["filter-starred"] === 'true'?true:false; var starred = req.query["filter-starred"] === 'true' ? true : false;
var short_name = req.params.shortName.toLowerCase(); var short_name = req.params.shortName.toLowerCase();
//console.log("[" + short_name + "] Inital Calls - Call Get Filter code: " + filter_code + " Filter Type: " + filter_type); //console.log("[" + short_name + "] Inital Calls - Call Get Filter code: " + filter_code + " Filter Type: " + filter_type);

View File

@@ -1,12 +1,11 @@
var db = require('../db'); var db = require('../db');
exports.get_groups = function(req, res) { exports.get_groups = async function (req, res) {
db.get().collection('groups', function(err, grp_coll) {
grp_coll.find({ const grp_coll = db.get().collection('groups');
'shortName': req.params.shortName.toLowerCase() const cursor = grp_coll.find({ 'shortName': req.params.shortName.toLowerCase() });
}).toArray(function(err, grp_results) { const grp_results = await cursor.toArray();
res.contentType('json');
res.send(JSON.stringify(grp_results)); res.contentType('json');
}); res.send(JSON.stringify(grp_results));
});
} }

View File

@@ -2,18 +2,16 @@ var mongoose = require("mongoose");
var User = require("../models/user"); var User = require("../models/user");
var System = require("../models/system"); var System = require("../models/system");
exports.get_systems = function (req, res) { exports.get_systems = async function (req, res) {
let fromDate = new Date(Date.now() - 60 * 60 * 24 * 30 * 1000); let fromDate = new Date(Date.now() - 60 * 60 * 24 * 30 * 1000);
System.find({lastActive: {$gte: fromDate}}).populate('userId', "screenName"). const results = await System.find({lastActive: {$gte: fromDate}}).populate('userId', "screenName").catch( err => {
exec(function (err, results) { console.error("Error - get_systems: " + err.message);
if (err) {
console.error("Error - get_systems: " + err.message);
res.json({ res.json({
success: false, success: false,
message: err message: err
}); });
return; return;
} });
var systemList = []; var systemList = [];
for (var result in results) { for (var result in results) {
@@ -47,6 +45,5 @@ exports.get_systems = function (req, res) {
success: true, success: true,
systems: systemList systems: systemList
})); }));
}); }
}

View File

@@ -1,18 +1,15 @@
var db = require('../db'); var db = require('../db');
exports.get_talkgroups = function(req, res) { exports.get_talkgroups = async function (req, res) {
db.get().collection('talkgroups', function(err, tg_coll) { const tg_coll = db.get().collection('talkgroups');
tg_coll.find({ const cursor = tg_coll.find({ 'shortName': req.params.shortName.toLowerCase() })
'shortName': req.params.shortName.toLowerCase() const tg_results = await cursor.toArray();
}).toArray(function(err, tg_results) { var talkgroups = {};
var talkgroups = {}; for (var tg in tg_results) {
for (var tg in tg_results) { talkgroups[tg_results[tg].num] = tg_results[tg];
talkgroups[tg_results[tg].num] = tg_results[tg]; }
} res.contentType('json');
res.contentType('json'); res.send(JSON.stringify({
res.send(JSON.stringify({ talkgroups: talkgroups
talkgroups: talkgroups }));
}));
});
});
} }

View File

@@ -11,33 +11,24 @@ var state = {
db: null, db: null,
} }
exports.connect = function(done) { exports.connect = async function (done) {
if (state.db) return done() if (state.db) return done()
var url = 'mongodb://' + host + ':' + port + '/scanner'; var url = 'mongodb://' + host + ':' + port + '/scanner';
MongoClient.connect(url, function(err, db) { const client = new MongoClient(url);
if (err) return done(err) await client.connect();
if (dbUser) { state.db = client.db();
db.authenticate(dbUser, dbPass, function (err, result) { done();
//test.equal(true, result);
state.db = db;
done();
});
} else {
state.db = db;
done();
}
})
} }
exports.get = function() { exports.get = function () {
return state.db return state.db
} }
exports.close = function(done) { exports.close = function (done) {
if (state.db) { if (state.db) {
state.db.close(function(err, result) { state.db.close(function (err, result) {
state.db = null state.db = null
state.mode = null state.mode = null
done(err) done(err)

View File

@@ -4,7 +4,6 @@ var passport = require("passport");
var util = require("util"); var util = require("util");
var secrets = require("./config/secrets"); var secrets = require("./config/secrets");
var configurePassport = require("./config/passport");
var configureExpress = require("./config/express"); var configureExpress = require("./config/express");
var calls = require("./controllers/calls"); var calls = require("./controllers/calls");
@@ -21,7 +20,6 @@ var ObjectID = require('mongodb').ObjectID;
var db = require('./db'); var db = require('./db');
var System = require("./models/system"); var System = require("./models/system");
var Permission = require("./models/permission");
var multer = require('multer'); var multer = require('multer');
// ------------------------------------------- // -------------------------------------------
@@ -37,105 +35,34 @@ const io = require('socket.io')(server, {
} }
}); });
//var Twitter = require('twitter-lite');
//io.origins('*:*');
/* /*
io.origins((origin, callback) => {
//if (origin !== 'https://foo.example.com') {
// return callback('origin not allowed', false);
//}
callback(null, true);
});*/
io.on('connection', function (socket) { io.on('connection', function (socket) {
//console.log('a user connected'); console.log('a user connected');
socket.on('disconnect', function () { socket.on('disconnect', function () {
//console.log('user disconnected'); console.log('user disconnected');
}); });
}); });*/
// This section of code enable a Twitter client to tweet out messages when a new call comes in
/*
const client = new Twitter({
subdomain: "api", // "api" is the default (change for other subdomains)
version: "1.1", // version "1.1" is the default (change for other subdomains)
consumer_key: process.env.TWITTER_CONSUMER_KEY, // from Twitter.
consumer_secret: process.env.TWITTER_CONSUMER_SECRET, // from Twitter.
access_token_key: process.env.TWITTER_ACCESS_TOKEN_KEY, // from your User (oauth_token)
access_token_secret: process.env.TWITTER_ACCESS_TOKEN_SECRET // from your User (oauth_token_secret)
});
client
.get("account/verify_credentials")
.then(results => {
console.log("Connected to Twitter");
})
.catch(console.error);
*/
function getRole(req) {
if (req.params.shortName && req.user) {
var short_name = req.params.shortName.toLowerCase();
var userId = req.user._id;
Permission.findOne({ 'userId': userId, 'shortName': short_name }, function (err, permission) {
if (err) {
console.error('Error - getRole userId: ' + userId + ' shortName: ' + short_name + ' error: ' + err);
return -1;
}
console.log('Found - getRole userId: ' + userId + ' shortName: ' + short_name + ' role: ' + permission.role);
return permission.role;
});
}
}
function isUser(req, res, next) {
if (req.params.shortName) {
var short_name = req.params.shortName.toLowerCase();
System.findOne({ 'shortName': short_name }, function (err, system) {
if (err) {
console.error('Error - System not found: ' + short_name);
res.status(505).send({
success: false,
message: "System Not Found"
});
}
if (!system.private) return next();
// Looks like the system is private...
if (getRole(req) > 0) return next();
res.status(401).send({
success: false,
message: "Insufficent Permission."
});
});
}
}
// ------------------------------------------- // -------------------------------------------
configurePassport(app, passport)
configureExpress(app, passport) configureExpress(app)
// ------------------------------------------- // -------------------------------------------
const connect = () => { const connect = () => {
mongoose.connect(secrets.db, (err, res) => { mongoose.connect(secrets.db).then((result) => { // Successfully connected
if (err) { console.log("connected to Mongo");
console.log(`Error connecting to ${secrets.db}. ${err}`)
} else {
console.log(`Successfully connected to ${secrets.db}.`)
}
}) })
.catch((err) => {
// Catch any potential error
console.log(mongoose.version);
console.log("Unable to connect to MongoDB. Error: " + err);
});
} }
connect() connect();
mongoose.connection.on("error", console.error) mongoose.connection.on("error", console.error)
mongoose.connection.on("disconnected", connect) mongoose.connection.on("disconnected", connect)
@@ -185,21 +112,6 @@ app.get('/:shortName/calls', calls.get_calls);
/*------ UPLOADS ---------- upload.single('call'), uploads.upload,*/ /*------ UPLOADS ---------- upload.single('call'), uploads.upload,*/
app.post('/:shortName/upload', upload.single('call'), uploads.upload, async function (req, res) { app.post('/:shortName/upload', upload.single('call'), uploads.upload, async function (req, res) {
notify_clients(req.call); notify_clients(req.call);
// This section is for sending out tweets when new calls come in
/*
if ((req.call.shortName == "dcfd") && (req.call.talkgroupNum == 101)) {
var call_time = new Date(req.call.time);
var call_link = "https://openmhz.com/system/dcfd?call-id=" + req.call._id + "&time=" + call_time.getTime();
var call_time_label = call_time.toLocaleDateString('en-US', { timeZone: "America/New_York" }) + " " + call_time.toLocaleTimeString('en-US', { timeZone: "America/New_York" })
var status = req.call.len + " sec transmission\n" + call_time_label + "\n" + call_link;
const response = await client.post('statuses/update', {
status: status,
});
//console.log("Tweet Response: " + response);
}*/
}); });
/*------ SYSTEMS ----------*/ /*------ SYSTEMS ----------*/
@@ -431,8 +343,4 @@ var calcSched = schedule.scheduleJob(calcRule, function() {
}); });
*/ */
module.exports = app; module.exports = app;

3256
backend/package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -4,33 +4,33 @@
"main": "index.js", "main": "index.js",
"dependencies": { "dependencies": {
"adm-zip": "^0.5.10", "adm-zip": "^0.5.10",
"aws-sdk": "^2.5", "aws-sdk": "^2.1353",
"axios": "0.18.0", "axios": "1.3.5",
"bcrypt-nodejs": "0.0.3", "bcrypt-nodejs": "0.0.3",
"body-parser": "1.18.3", "body-parser": "1.20.2",
"bson": "1.0.4", "bson": "5.2.0",
"connect-mongo": "2.0.1", "connect-mongo": "5.0.0",
"cookie-parser": "^1.4.0", "cookie-parser": "^1.4.6",
"csv": "1.1.1", "csv": "6.2.8",
"csv-write-stream": "^2.0.0", "csv-write-stream": "^2.0.0",
"express": "4.16.4", "express": "4.18.2",
"express-session": "1.15.6", "express-session": "1.17.3",
"ffprobe": "^1.1.0", "ffprobe": "^1.1.2",
"ffprobe-static": "2.0.0", "ffprobe-static": "3.1.0",
"find-remove": "1.0.1", "find-remove": "4.1.0",
"get-folder-size": "^1.0.0", "get-folder-size": "^4.0.0",
"mkdirp": "^0.5.1", "mkdirp": "^2.1.6",
"mongodb": "2.2.36", "mongodb": "5.2.0",
"mongoose": "5.12.3", "mongoose": "7.0.3",
"multer": "1.3.0", "multer": "1.3.0",
"node-ffprobe": "https://github.com/ListenerApproved/node-ffprobe", "node-ffprobe": "https://github.com/ListenerApproved/node-ffprobe",
"node-schedule": "1.2.4", "node-schedule": "2.1.1",
"passport": "0.4.0", "passport": "0.6.0",
"passport-local": "1.0.0", "passport-local": "1.0.0",
"socket.io": "^4.0.0", "socket.io": "^4.6.1",
"twitter-lite": "1.1.0", "twitter-lite": "1.1.0",
"websocket": "^1.0.24", "websocket": "^1.0.34",
"ws": "^3.1.0" "ws": "^8.13.0"
}, },
"scripts": { "scripts": {
"test": "mocha --reporter spec" "test": "mocha --reporter spec"

View File

@@ -8,71 +8,46 @@ 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
function updateActiveSystems() { async function updateActiveSystems() {
// Go through all of the Systems // Go through all of the Systems
db.get().collection('systems', function(err, sysCollection) { const sysCollection = db.get().collection('systems');
var cursor = sysCollection.find(); var cursor = sysCollection.find();
cursor.each(function(err, item) { await cursor.forEach(function(item) {
// go through all the systems // go through all the systems
// While there are still Systems to work with
if (item) {
// if you have recieved some calls during that last period, make the system active // if you have recieved some calls during that last period, make the system active
if ((callTotals[item.shortName] != undefined) && (callTotals[item.shortName][0] > 0)) { if ((callTotals[item.shortName] != undefined) && (callTotals[item.shortName][0] > 0)) {
const active = true; const active = true;
const callAvg = callTotals[item.shortName][0] / timePeriod; const callAvg = callTotals[item.shortName][0] / timePeriod;
sysCollection.update({
shortName: item.shortName const query = { shortName: item.shortName};
}, { const update = { $set: {"active": active,"callAvg": callAvg,"lastActive": new Date() }};
$set: { const options = {upsert: true};
"active": active,
"callAvg": callAvg, sysCollection.updateOne(query, update, options);
"lastActive": new Date()
}
}, {
upsert: true
}, function(err, objects) {
if (err) {
console.log("Shortname: " + item.shortName);
console.warn(err.message);
}
});
} else { } else {
sysCollection.update({ const query = { shortName: item.shortName};
shortName: item.shortName const update = { $set: {"active": false, "callAvg": 0}};
}, { const options = {upsert: true};
$set: {
"active": false, sysCollection.updateOne(query, update, options);
"callAvg": 0
}
}, {
upsert: true
}, function(err, objects) {
if (err) {
console.log("Shortname: " + item.shortName);
console.warn(err.message);
}
});
} }
}
}); });
});
} }
exports.initStats = function() { exports.initStats = async function() {
// get the System Stats collection // get the System Stats collection
db.get().createCollection("system_stats")
db.get().collection('system_stats', function(err, statsCollection) { 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(); var cursor = statsCollection.find();
// go through each of the Systems in Stats // go through each of the Systems in Stats
cursor.each(function(err, item) { await cursor.forEach(function(item) {
// If you are not at the end...
if (item) {
//console.log("Loading stats for: " + item.shortName);
// Talkgroup Stats // Talkgroup Stats
if (item.talkgroupStats != undefined) { if (item.talkgroupStats != undefined) {
talkgroupStats[item.shortName] = item.talkgroupStats; talkgroupStats[item.shortName] = item.talkgroupStats;
@@ -91,11 +66,7 @@ exports.initStats = function() {
if (item.callTotals != undefined) { if (item.callTotals != undefined) {
callTotals[item.shortName] = item.callTotals; callTotals[item.shortName] = item.callTotals;
} }
} else {
}
}); });
});
} }
// keeps track of the number of calls that get uploaded with an audio file // keeps track of the number of calls that get uploaded with an audio file
@@ -168,8 +139,8 @@ exports.addCall = function(call) {
// This gets called when a Time Period is up // This gets called when a Time Period is up
exports.shiftStats = function() { exports.shiftStats = async function() {
db.get().collection('system_stats', function(err, statsCollection) { const statsCollection = db.get().collection('system_stats');
// for all the systems in Error Stats // for all the systems in Error Stats
for (var shortName in uploadErrors) { for (var shortName in uploadErrors) {
@@ -234,21 +205,11 @@ exports.shiftStats = function() {
} }
} }
statsCollection.update({ const query = { shortName: item.shortName};
shortName: shortName const update = { $set: { "decodeErrorsFreq": decodeErrorsFreq[shortName]}};
}, { const options = {upsert: true};
$set: {
"decodeErrorsFreq": decodeErrorsFreq[shortName], sysCollection.updateOne(query, update, options);
}
}, {
upsert: true
}, function(err, objects) {
if (err){
console.log("Shortname: " + shortName);
console.log("decodeErrorsFreq: " + util.inspect(decodeErrorsFreq[shortName]));
console.warn(err.message);
}
});
} }
} }
@@ -310,27 +271,15 @@ exports.shiftStats = function() {
} }
callTotals[shortName][0] = callTotal; callTotals[shortName][0] = callTotal;
statsCollection.update({
shortName: shortName
}, {
$set: {
"talkgroupStats": talkgroupStats[shortName],
"callTotals": callTotals[shortName]
}
}, {
upsert: true
}, function(err, objects) {
if (err){
console.log("Shortname: " + shortName);
console.log("TgStats: " + util.inspect(talkgroupStats[shortName]));
console.log("Call Totals: " + util.inspect(callTotals[shortName]));
console.warn(err.message);
}
});
const query = { shortName: item.shortName};
const update = { $set: { "talkgroupStats": talkgroupStats[shortName], "callTotals": callTotals[shortName] }};
const options = {upsert: true};
sysCollection.updateOne(query, update, options);
} }
} }
});
updateActiveSystems(); updateActiveSystems();
} }
exports.callTotals = function(shortName) { exports.callTotals = function(shortName) {

View File

@@ -1,3 +1,3 @@
source test.env source test.env
echo "Docker Compose Command: " $@ echo "Docker Compose Command: " $@
docker-compose -f docker-compose.yml -f test-compose.yml $@ docker compose -f docker-compose.yml -f test-compose.yml $@