|
| 1 | +var fs = require('fs'); |
| 2 | +var os = require('os'); |
| 3 | +var express = require('express'); |
| 4 | +var async = require('async'); |
| 5 | +var format = require('format-number')(); |
| 6 | +var filesize = require('filesize'); |
| 7 | +var router = express.Router(); |
| 8 | +var Request = require('./models/Request'); |
| 9 | +var io = require('socket.io')(settings.socket); |
| 10 | + |
| 11 | +io.on('connection', socket => { |
| 12 | + socket.on('stats', msg => { |
| 13 | + async.parallel([ |
| 14 | + function(cb){ |
| 15 | + Request.findOne({date: getDateTime()}, (err, obj) => { |
| 16 | + if (obj !== null) { |
| 17 | + cb(err, {total: format(obj.total), bandwidth: filesize(obj.bandwidth)}); |
| 18 | + } else { |
| 19 | + cb(err, {total: 0, bandwidth: filesize(0)}); |
| 20 | + } |
| 21 | + }); |
| 22 | + }, |
| 23 | + function(cb){ |
| 24 | + Request.aggregate([ |
| 25 | + {"$group": { |
| 26 | + "_id": 'total', |
| 27 | + "total": { $sum: "$total" }, |
| 28 | + "bandwidth": { $sum: "$bandwidth" } |
| 29 | + }}, |
| 30 | + { "$sort": { |
| 31 | + "total": -1 |
| 32 | + }} |
| 33 | + ],function(err, result) { |
| 34 | + if (result.length !== 0) { |
| 35 | + cb(err, {total: format(result[0].total), bandwidth: filesize(result[0].bandwidth, {round: 4})}); |
| 36 | + } else { |
| 37 | + cb(err, {total: 0, bandwidth: filesize(0)}); |
| 38 | + } |
| 39 | + }); |
| 40 | + }, |
| 41 | + function(cb){ |
| 42 | + Request.aggregate([ |
| 43 | + { |
| 44 | + "$sort": { "date": -1} |
| 45 | + }, |
| 46 | + { "$limit": 30 }, |
| 47 | + {"$group": { |
| 48 | + "_id": 'total', |
| 49 | + "total": { $sum: "$total" }, |
| 50 | + "bandwidth": { $sum: "$bandwidth" } |
| 51 | + }} |
| 52 | + ],function(err, result) { |
| 53 | + if (result.length !== 0) { |
| 54 | + cb(err, {total: format(Math.round(result[0].total/30)), bandwidth: filesize(result[0].bandwidth)}); |
| 55 | + } else { |
| 56 | + cb(err, {total: 0, bandwidth: filesize(0)}); |
| 57 | + } |
| 58 | + }); |
| 59 | + } |
| 60 | + ], |
| 61 | + // optional callback |
| 62 | + function(err, results) { |
| 63 | + socket.emit('statsResults', { |
| 64 | + today: results[0], |
| 65 | + all: results[1], |
| 66 | + 30: results[2], |
| 67 | + load: Math.round(os.loadavg()[0]*100) + "%" |
| 68 | + }); |
| 69 | + }); |
| 70 | + }); |
| 71 | +}); |
| 72 | + |
| 73 | +function getDateTime(daysBack) { |
| 74 | + daysBack = daysBack || 0; |
| 75 | + var date = new Date(new Date().getTime()-86400000*daysBack); |
| 76 | + |
| 77 | + var year = date.getFullYear(); |
| 78 | + |
| 79 | + var month = date.getMonth() + 1; |
| 80 | + month = (month < 10 ? '0' : '') + month; |
| 81 | + |
| 82 | + var day = date.getDate(); |
| 83 | + day = (day < 10 ? '0' : '') + day; |
| 84 | + |
| 85 | + return year + '-' + pad(month, 2) + '-' + pad(day, 2); |
| 86 | + |
| 87 | + function pad(n, width, z) { |
| 88 | + z = z || '0'; |
| 89 | + n = n + ''; |
| 90 | + return n.length >= width ? n : new Array(width - n.length + 1).join(z) + n; |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +function simpleDate(date) { |
| 95 | + var a = new Date(date); |
| 96 | + return pad(a.getMonth()+1, 2) + "." + pad(a.getUTCDate(), 2); |
| 97 | +} |
| 98 | + |
| 99 | +module.exports = io; |
0 commit comments