Skip to content

Commit

Permalink
Merge branch 'development' of https://github.com/xcjs/blur-monitor
Browse files Browse the repository at this point in the history
  • Loading branch information
xcjs committed Sep 7, 2016
2 parents 5d42dbd + 0f5dc90 commit 3a7441f
Show file tree
Hide file tree
Showing 496 changed files with 41,772 additions and 91 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,9 @@ Please report issues and enhancements. This is a project seeking to change and g
## License

<a href=/LICENSE.txt target="_blank">MIT</a> license.

## Art Licenses

Included icons are courtesy of the former Flattr icon project: https://github.com/NitruxSA/luv-icon-theme

Additional icons courtesy of KDA Web Technologies: http://icons.kdaweb.com/
16 changes: 7 additions & 9 deletions blurmonitor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ var Inert = require('inert');
var path = require('path');

var env = require('./util/environment');
var startup = require('./util/startup');

var server = new Hapi.Server({
connections: {
Expand All @@ -34,7 +33,8 @@ var routers = [
require('./routes/disks'),
require('./routes/network'),
require('./routes/processes'),
require('./routes/bandwidth')
require('./routes/bandwidth'),
require('./routes/assets')
];

routers.forEach(function (router) {
Expand All @@ -43,12 +43,10 @@ routers.forEach(function (router) {
});
});

startup.run(function() {
server.start(function(err) {
if (err) {
throw err;
}
server.start(function(err) {
if (err) {
throw err;
}

console.log('Server running at:', server.info.uri);
});
console.log('Server running at:', server.info.uri);
});
29 changes: 29 additions & 0 deletions blurmonitor/routes/assets.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*jslint node: true */

'use strict';

var assets = require('../services/assets');

module.exports = getRoutes();

function getRoutes() {
var routes = [];

routes.push({
method: 'GET',
path: '/api/assets/apps',
handler: function (request, reply) {
return reply(assets.getAssets('apps'));
}
});

routes.push({
method: 'GET',
path: '/api/assets/distros',
handler: function (request, reply) {
return reply(assets.getAssets('distros'));
}
});

return routes;
}
50 changes: 10 additions & 40 deletions blurmonitor/routes/system.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
'use strict';

var os = require('os');
var exec = require('child_process').exec;
var lsbRelease = require('../services/lsb-release');

module.exports = getRoutes();

Expand All @@ -24,8 +24,15 @@ function getRoutes() {
};

var promise = new Promise(function (resolve) {
getDistro().then(function (result) {
response.distro = result;
lsbRelease.getRelease.then(function (release) {
release = lsbRelease.parse(release);

response.distro = {};
response.distro.id = release.DISTRIB_ID;
response.distro.release = release.DISTRIB_RELEASE;
response.distro.codeName = release.DISTRIB_CODENAME;
response.distro.description = release.DISTRIB_DESCRIPTION;

resolve(response);
});
});
Expand All @@ -36,40 +43,3 @@ function getRoutes() {

return routes;
}

function getDistro() {
var promise = new Promise(function (resolve) {
// Unescaped command: cat /etc/lsb-release | grep "DISTRIB_DESCRIPTION" | sed -e 's/[^"]*"\([^"]*\)".*/\1/'
var command =
"cat /etc/lsb-release | grep \"DISTRIB_DESCRIPTION\" | sed -e 's/[^\"]*\"\\([^\"]*\\)\".*/\\1/'";

var cb = function (out) {
resolve(out);
};

shell(command, cb);
});

return promise;
}

function shell(command, callback) {
exec(command, function (error, stdout, stderr) {
if (error) {
// If for some reason the command fails, fallback to os.type.
callback(os.type());
return;
}

var eolLength = os.EOL.length;
var finalNewLine = stdout.substr(
stdout.length - eolLength,
stdout.length);

if (finalNewLine === os.EOL) {
stdout = stdout.substr(0, stdout.length - eolLength);
}

callback(stdout, stderr, error);
});
}
48 changes: 48 additions & 0 deletions blurmonitor/services/assets.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*jslint node: true */

'use strict';

var fs = require('fs');
var path = require('path');
var env = require('../util/environment');

module.exports = {
getAssets: getAssets
};

function getAssets(type) {
var assetPath = Array.isArray(env.staticRoot) ?
path.join('./', env.staticRoot[1], 'assets/img/app') :
path.join('./', env.staticRoot, 'assets/img/app');

switch(type) {
case 'apps': {
return listDirectory(path.join(assetPath, type));
break;
}

case 'distros': {
return listDirectory(path.join(assetPath, type));
break;
}
}
}

function listDirectory(path) {
var promise = new Promise(function (resolve, reject) {
fs.readdir(path, function(err, assets) {
if(!err) {
var assets = assets.map(function(asset) {
return(asset.substr(0, asset.lastIndexOf('.')));
});

resolve(assets);
} else {
console.error(err);
reject(err);
}
});
});

return promise;
}
35 changes: 35 additions & 0 deletions blurmonitor/services/lsb-release.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*jslint node: true */
/*jslint esversion: 6 */

'use strict';

var shell = require('./shell');

module.exports = {
getRelease: getRelease(),
parse: parseRelease
};

function getRelease() {
return shell.spawn('cat', ['/etc/lsb-release']);
}

function parseRelease(stdout) {
var lines = stdout.toString().split(/\n/g);
var lsbRelease = {};

lines.forEach(function(line) {
var key = line.substr(0, line.indexOf('='));
var value = line.substr(line.indexOf('=') + 1, line.length -1);

if(key && value) {
while(value.indexOf('"') > -1) {
value = value.replace('"', '');
}

lsbRelease[key] = value;
}
});

return lsbRelease;
}
57 changes: 57 additions & 0 deletions blurmonitor/services/shell.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*jslint node: true */
/*jslint esversion: 6 */

'use strict';

var childProcess = require('child_process');

var exec = childProcess.exec;
var spawn = childProcess.spawn;

module.exports = {
exec: execWrapper,
spawn: spawnWrapper
};

function execWrapper(command) {
var promise = new Promise(function(resolve, reject) {
exec(command, function(error, stdout, stderr) {
if (error) {
reject('Command "' + command + '" failed. Please check for earlier errors');
return;
}

if(stderr) {
console.error(stderr);
}

resolve(stdout);
});
});

return promise;
}

function spawnWrapper(command, args) {
var process = spawn(command, args);

var promise = new Promise(function(resolve, reject) {
process.stdout.setEncoding("utf8");

process.stdout.on('data', function(stdout) {
resolve(stdout);
});

process.stderr.on('data', function(stderr) {
console.error(stderr);
});

process.on('close', function(code) {
if (code !== 0) {
reject('Command "' + command + '" failed. Please check for earlier errors');
}
});
});

return promise;
}
32 changes: 0 additions & 32 deletions blurmonitor/util/startup.js

This file was deleted.

7 changes: 7 additions & 0 deletions src/app/assets/assets.module.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
(function () {
'use strict';

angular.module('BlurMonitor.assets', [
'ngResource'
]);
})();
14 changes: 14 additions & 0 deletions src/app/assets/assetsResource.service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
(function() {
'use strict';

angular.module('BlurMonitor.assets').service('AssetsResource',
['$resource', AssetsResource]);

function AssetsResource($resource) {
var AppAssets = $resource('/api/assets/apps');
var DistroAssets = $resource('/api/assets/distros');

this.getApps = AppAssets.query;
this.getDistros = DistroAssets.query;
}
})();
4 changes: 2 additions & 2 deletions src/app/pages/blurmonitor-dashboard/dashboard.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
<tr>
<th class="black-muted-bg">Distro</th>
<td>
<img src="/assets/img/distributor-logo.png" img-load-error alt="Distributor Logo">
{{vm.system.distro}}
<img ng-src="/assets/img/app/distros/{{vm.system.distro.id}}.svg" img-load-error alt="Distributor Logo">
{{vm.system.distro.description}}
</td>
</tr>
<tr>
Expand Down
Loading

0 comments on commit 3a7441f

Please sign in to comment.