Skip to content

Commit

Permalink
Track load
Browse files Browse the repository at this point in the history
  • Loading branch information
keitharm committed Sep 30, 2019
1 parent 3ac4b70 commit ac60cd5
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 21 deletions.
14 changes: 14 additions & 0 deletions models/Load.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const mongoose = require('mongoose');

const loadSchema = mongoose.Schema({
date: {
type: Date,
unique: true
},
'load': Number
});
loadSchema.index({date: 1}, {unique: true});

const Load = mongoose.model('Load', loadSchema);

module.exports = Load;
31 changes: 10 additions & 21 deletions sockets.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const async = require('async');
const format = require('format-number')();
const filesize = require('filesize');
const Request = require('./models/Request');
const Load = require('./models/Load');
const store = require('./store');
const util = require('./util');

Expand All @@ -14,6 +15,8 @@ let stats = {
load: "0%",
};

let lastMinCheck = -1;

updateStats();
setInterval(updateStats, 2500);
function updateStats() {
Expand Down Expand Up @@ -89,35 +92,21 @@ function updateStats() {
});
}
],
(err, results) => {
async (err, results) => {
stats = {
today: results[0],
all: results[1],
30: results[2],
load: Math.round(os.loadavg()[0] * 100) + "%"
};
store.set('stats', stats);
});
}

function getDateTime(daysBack) {
daysBack = daysBack || 0;
const date = new Date(new Date().getTime()-86400000*daysBack);
const year = date.getFullYear();

let month = date.getMonth() + 1;
month = (month < 10 ? '0' : '') + month;

let day = date.getDate();
day = (day < 10 ? '0' : '') + day;

return year + '-' + pad(month, 2) + '-' + pad(day, 2);
}

function pad(n, width, z) {
z = z || '0';
n = n + '';
return n.length >= width ? n : new Array(width - n.length + 1).join(z) + n;
let mins = new Date().getMinutes();
if (mins !== lastMinCheck) {
lastMinCheck = mins;
await Load.create({date: Date.now(), load: stats.load.slice(0, -1)});
}
});
}

module.exports = (server) => {
Expand Down

0 comments on commit ac60cd5

Please sign in to comment.