First Commit

This commit is contained in:
Luke Berndt
2020-06-09 22:11:11 -04:00
parent 8f49aa463c
commit 3b0d3f290f
551 changed files with 193130 additions and 2 deletions

31
mongo/Dockerfile Normal file
View File

@@ -0,0 +1,31 @@
# Create proxy container for www.example.com
#
# docker build -t mongo .
FROM mongo:3.4
MAINTAINER Luke Berndt <lukekb@gmail.com>
# Set timezone
RUN echo "America/New_York" > /etc/timezone \
&& dpkg-reconfigure -f noninteractive tzdata
# Add main NGINX config
RUN mkdir -p /app
ADD trim.js /app
ADD totals.js /app
ADD init_test_db.js /app
ADD upgrade_db_admin.js /app
ADD permissions.js /app
ADD errors.js /app
ADD clean.js /app
# Add crontab file in the cron directory
#ADD crontab /etc/cron.d/trim-cron
# Give execution rights on the cron job
#RUN chmod 0644 /etc/cron.d/trim-cron
# Run the command on container startup
#CMD cron
WORKDIR /app

11
mongo/clean.js Normal file
View File

@@ -0,0 +1,11 @@
db = db.getMongo().getDB( "scanner" );
var bulkRemove = db.calls.initializeUnorderedBulkOp()
var date = new Date();
date.setMonth(date.getMonth() - 1);
bulkRemove.find({"time":{$lt: date}}).remove();
var results = bulkRemove.execute();
print("Removed: " + results.nRemoved);

4
mongo/crontab Normal file
View File

@@ -0,0 +1,4 @@
# Jobs:
# m h dom mon dow command
* 3 * * * root mongo /app/trim.js
# An empty line is required at the end of this file for a valid cron file.

51
mongo/errors.js Normal file
View File

@@ -0,0 +1,51 @@
db = db.getMongo().getDB( "scanner" );
var start = new Date();
start.setHours(start.getHours() - 1);
var results = db.calls.aggregate([{
$match: {
time: {
$gte: start
},
shortName: 'dcfd'
}
},
{
"$unwind": "$freqList"
},
{
"$project": {
"freqList": 1,
"errorRatio": {
$cond: [{
$eq: ["$freqList.len", 0]
}, 0, {
"$multiply": [{
"$divide": ["$freqList.errors", "$freqList.len"]
}, 100]
}]
},
"time": "$time"
}
},
{
"$group": {
"_id": "$freqList.freq",
"values": {
"$push": {
"time": "$time",
"errors": "$errorRatio",
"spikes": "$freqList.spikes"
}
}
}
},
{
$sort: {
_id: -1
}
}]);
results.forEach(function(document) {
printjson(document);
});

4
mongo/init_test_db.js Normal file
View File

@@ -0,0 +1,4 @@
db = db.getMongo().getDB( "scanner" );
db.users.insertOne({ "email" : "test@test.com", "admin" : true, "location" : "somewhere", "lastName" : "Account", "firstName" : "Test", "lastLogin" : ISODate("2017-01-15T13:31:02.148Z"), "local" : { "password" : "$2a$08$8F/8dlU4XJUSMe4OcGVVAeCVAlygxerXteLRXB5SsbkQkoS8l2xG6", "email" : "test@test.com" } });
db.systems.insertOne({ "key" : "ed8b444011047a67445f16d73088ef34ecec2b46b959a3b6857dd004a5d17bc7", "user" : "test@test.com", "description" : "Radio system for DC Fire", "state" : "DC", "city" : "Washingtond", "shortName" : "dcfd", "name" : "DC Fire and EMS", "active" : true });

33
mongo/permissions.js Normal file
View File

@@ -0,0 +1,33 @@
db = db.getMongo().getDB( "scanner" );
var requests = [];
var permissions = [];
db.systems.find().forEach(system => {
printjson(system);
if (system && system.user) {
var user = db.users.findOne({'local.email': system.user})
if (user) {
requests.push( {
'updateOne': {
'filter': { '_id': system._id },
'update': { '$set': { 'owner': user._id } }
}
});
permissions.push({
'replaceOne':
{
"filter": {'userId':user._id, 'systemId': system._id},
"replacement" : { '$set': {'userId': user._id, 'systemId': system._id, 'shortName': system.shortName, 'role': 20}},
"upsert" : true
}
}
)
}
}
});
if(requests.length > 0) {
db.systems.bulkWrite(requests);
db.permissions.bulkWrite(permissions);
}

28
mongo/totals.js Normal file
View File

@@ -0,0 +1,28 @@
db = db.getMongo().getDB( "scanner" );
var results = db.calls.aggregate(
[ {
$project: {
len: true,
year: {$year: "$time"},
month: {$month: "$time"},
dayOfMonth: {$dayOfMonth: "$time"}
}
},
{
$group:
{
_id: {
year: '$year',
month: '$month',
dayOfMonth: '$dayOfMonth'
},
totalAudio: { $sum: "$len" },
count: { $sum: 1 }
}
}
]
)
results.forEach(function(document) {
printjson(document);
});

25
mongo/trim.js Normal file
View File

@@ -0,0 +1,25 @@
db = db.getMongo().getDB( "scanner" );
var bulkRemove = db.calls.initializeUnorderedBulkOp()
var short_date = new Date();
db.systems.find().forEach(
function(system){
var date = new Date();
var total =0;
if (system.planType == 10){
date.setMonth(date.getDate() - 30);
} else {
date.setDate(date.getDate() - 7);
}
print(system.shortName);
bulkRemove.find({"time":{$lt: date}, "shortName": system.shortName}).remove();
});
//bulkInsert.execute()
var results = bulkRemove.execute();
print("Removed: " + results.nRemoved);

15
mongo/upgrade_db_admin.js Normal file
View File

@@ -0,0 +1,15 @@
db = db.getMongo().getDB( "scanner" );
db.users.find().forEach(
function(user){
db.systems.updateMany(
{ "user": { $eq: user.email } },
{
$set: { "userId": user._id, "showDisplayName": false, "confirmEmail": true },
}
);
print("Upgrading: " + user.email);
});