Commit 89ee9bb1 authored by priit's avatar priit

Wrapperi statistika

parent d4dd3d40
var logger = require('log4js').getLogger('statistics_controller');
var express = require('express');
var router = express.Router();
var statisticsService = require(__base + '/src/service/statisticsService');
router.get('/', function(req, res) {
statisticsService.getServerStatistics( req, function (err, data) {
if(err){
res.code(520);
return res.send(err);
}
return res.send(data);
});
});
module.exports = router;
......@@ -3,6 +3,7 @@ var router = express.Router();
var config = require(__base + 'config');
router.use('/api/v1/service', require(__base + 'controllers/api/v1/service'));
router.use('/api/v1/statistics', require(__base + 'controllers/api/v1/statistics'));
/* GET home page. Service description from config */
router.get('/', function( req, res ) {
......
/**
* Created by priit on 2.10.15.
*/
var logger = require('log4js').getLogger('statistics_service');
var async = require('async');
var os = require('os');
var njds = require('nodejs-disks');
function StatisticsService() {
var self = this;
this.getServerStatistics = function(req, cb) {
var summary = {};
async.waterfall([
function (callback) {
self.getMemoryStatistics(function (err, data) {
summary.memory = data;
callback();
});
},
function (callback) {
self.getDrivesStatistics(function (err, drives) {
summary.drives = drives;
callback();
});
},
function (callback) {
self.getCPULoad(function (err, data) {
summary.load = data;
callback();
});
}
],
function (err) {
cb(err, summary);
}
);
};
this.getMemoryStatistics = function (callback) {
var total = Math.round( os.totalmem() / 10737418,24 ) / 100;
var used = Math.round( (os.totalmem() - os.freemem()) / 10737418,24 ) / 100;
var systemMemory = {
total: total + ' GB',
used: used + ' GB'
};
callback( null, systemMemory);
};
this.getDrivesStatistics = function ( callback ) {
var drivesSummary = [];
njds.drives( function (err, drives) {
njds.drivesDetail(
drives,
function (err, data) {
for(var i in data) {
var drive = {
name: data[i].drive,
total: data[i].total,
used: data[i].used
};
drivesSummary.push(drive);
}
callback(null, drivesSummary);
}
);
}
)
};
this.getCPULoad = function (callback) {
var loads = os.loadavg();
for(i in loads){
var load = loads[i];
loads[i] = Math.round(load * 100) / 100;
}
loads.push( os.cpus().length );
callback( null, loads);
}
}
module.exports = new StatisticsService();
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment