forked from steemit/condenser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hardwarestats.js
55 lines (47 loc) · 1.3 KB
/
hardwarestats.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import cpuStat from 'cpu-stat';
import memStat from 'mem-stat';
import diskStat from 'disk-stat';
module.exports = hardwareStats;
let stats = {};
function handleError(err) {
// perpetually throws the same error down the chain for promises
throw err;
}
function startPromise() {
return new Promise(function(resolve, reject) {
resolve();
});
}
function getCpuUsage() {
return new Promise(function(resolve, reject) {
cpuStat.usagePercent(function(err, percent, seconds) {
if (err) return err;
stats.cpuPercent = percent;
resolve();
});
});
}
function getMemoryUsage() {
return new Promise(function(resolve, reject) {
stats.memoryStatsInGiB = memStat.allStats('GiB');
resolve();
});
}
function getDiskUsage() {
return new Promise(function(resolve, reject) {
stats.diskStats = diskStat.raw();
resolve();
});
}
function hardwareStats() {
return startPromise()
.then(getCpuUsage, handleError)
.then(getMemoryUsage, handleError)
.then(getDiskUsage, handleError)
.then(function() {
console.log(JSON.stringify(stats));
}, handleError)
.then(null, function(err) {
console.log('error getting hardware stats: ' + err);
});
}