Skip to content

Commit ac60cd5

Browse files
committed
Track load
1 parent 3ac4b70 commit ac60cd5

2 files changed

Lines changed: 24 additions & 21 deletions

File tree

models/Load.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const mongoose = require('mongoose');
2+
3+
const loadSchema = mongoose.Schema({
4+
date: {
5+
type: Date,
6+
unique: true
7+
},
8+
'load': Number
9+
});
10+
loadSchema.index({date: 1}, {unique: true});
11+
12+
const Load = mongoose.model('Load', loadSchema);
13+
14+
module.exports = Load;

sockets.js

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const async = require('async');
33
const format = require('format-number')();
44
const filesize = require('filesize');
55
const Request = require('./models/Request');
6+
const Load = require('./models/Load');
67
const store = require('./store');
78
const util = require('./util');
89

@@ -14,6 +15,8 @@ let stats = {
1415
load: "0%",
1516
};
1617

18+
let lastMinCheck = -1;
19+
1720
updateStats();
1821
setInterval(updateStats, 2500);
1922
function updateStats() {
@@ -89,35 +92,21 @@ function updateStats() {
8992
});
9093
}
9194
],
92-
(err, results) => {
95+
async (err, results) => {
9396
stats = {
9497
today: results[0],
9598
all: results[1],
9699
30: results[2],
97100
load: Math.round(os.loadavg()[0] * 100) + "%"
98101
};
99102
store.set('stats', stats);
100-
});
101-
}
102-
103-
function getDateTime(daysBack) {
104-
daysBack = daysBack || 0;
105-
const date = new Date(new Date().getTime()-86400000*daysBack);
106-
const year = date.getFullYear();
107-
108-
let month = date.getMonth() + 1;
109-
month = (month < 10 ? '0' : '') + month;
110103

111-
let day = date.getDate();
112-
day = (day < 10 ? '0' : '') + day;
113-
114-
return year + '-' + pad(month, 2) + '-' + pad(day, 2);
115-
}
116-
117-
function pad(n, width, z) {
118-
z = z || '0';
119-
n = n + '';
120-
return n.length >= width ? n : new Array(width - n.length + 1).join(z) + n;
104+
let mins = new Date().getMinutes();
105+
if (mins !== lastMinCheck) {
106+
lastMinCheck = mins;
107+
await Load.create({date: Date.now(), load: stats.load.slice(0, -1)});
108+
}
109+
});
121110
}
122111

123112
module.exports = (server) => {

0 commit comments

Comments
 (0)