Skip to content

Commit e027bf7

Browse files
authored
Merge pull request #7 from oslabs-beta/Lucie/code-review
Lucie/npm package optimization
2 parents 040d547 + 3e58f12 commit e027bf7

File tree

1 file changed

+180
-192
lines changed

1 file changed

+180
-192
lines changed

chronos_npm_package/controllers/healthHelpers.js

Lines changed: 180 additions & 192 deletions
Original file line numberDiff line numberDiff line change
@@ -2,211 +2,199 @@ const si = require('systeminformation');
22

33
const healthHelpers = {};
44

5-
/*
6-
This method returns an promise that resolves to an array of
7-
si data points.
8-
*/
5+
/* This object contains all systeminformation methods, metric names, and corresponding points */
6+
7+
const collectedMetrics = {
8+
cpu: {
9+
speed_in_GHz: 'speed',
10+
speedMax_in_GHz: 'speedMax',
11+
num_of_cores: 'cores',
12+
num_of_processors: 'processors',
13+
'cache.l1d in bytes': 'cache.l1d',
14+
'cache.l1i in bytes': 'cache.l1i',
15+
'cache.l2 in bytes': 'cache.l2',
16+
'cache.l3 in bytes': 'cache.l3',
17+
},
18+
cpuCurrentSpeed: {
19+
average_CPU_speed_in_GHz: 'avg',
20+
minimum_CPU_speed_in_GHz: 'min',
21+
maximum_CPU_speed_in_GHz: 'max',
22+
},
23+
cpuTemperature: {
24+
average_temperature: 'main',
25+
max_temperature: 'max',
26+
},
27+
currentLoad: {
28+
average_CPU_load_percent: 'avg',
29+
current_CPU_load_percent: 'currentLoad',
30+
current_CPU_load_user_percent: 'currentLoadUser',
31+
current_CPU_load__system_percent: 'currentLoadSystem',
32+
current_CPU_load_nice_percent: 'currentLoadNice',
33+
current_CPU_load_idle_percent: 'currentLoadIdle',
34+
current_CPU_load_raw_ticks: 'rawCurrentLoad',
35+
},
36+
mem: {
37+
totalmemory_in_bytes: 'total',
38+
freememory_in_bytes: 'free',
39+
usedmemory_in_bytes: 'used',
40+
activememory_in_bytes: 'active',
41+
buffers_plus_cache_in_bytes: 'buffcache',
42+
available_memory: 'available',
43+
},
44+
processes: {
45+
totalprocesses: 'all',
46+
blockedprocesses: 'blocked',
47+
runningprocesses: 'running',
48+
sleepingprocesses: 'sleeping',
49+
},
50+
inetLatency: 'all data collected',
51+
};
952

1053
healthHelpers.collectHealthData = () => {
11-
// Create an array to hold the promises returned by si
12-
const promises = [];
13-
// for use with every object
54+
const healthDataCollection = [];
1455
const time = Date.now();
15-
// Fill the array of promises
16-
promises.push(
17-
si
18-
.cpu()
19-
.then(data => ({
20-
speed_in_GHz: data.speed,
21-
speedMax_in_GHz: data.speedMax,
22-
num_of_cores: data.cores,
23-
num_of_processors: data.processors,
24-
'cache.l1d in bytes': data.cache.l1d,
25-
'cache.l1i in bytes': data.cache.l1i,
26-
'cache.l2 in bytes': data.cache.l2,
27-
'cache.l3 in bytes': data.cache.l3,
28-
}))
29-
.then(data => {
30-
const cpuMetrics = [];
31-
for (const metric in data) {
32-
cpuMetrics.push({
33-
metric,
34-
value: data[metric],
35-
category: 'CPU',
36-
time,
37-
});
38-
}
39-
return cpuMetrics;
40-
})
41-
.catch(err => {
42-
if (err) {
43-
throw err;
44-
}
45-
})
46-
);
47-
promises.push(
48-
si
49-
.cpuCurrentSpeed()
50-
.then(data => ({
51-
average_CPU_speed_in_GHz: data.avg,
52-
minimum_CPU_speed_in_GHz: data.min,
53-
maximum_CPU_speed_in_GHz: data.max,
54-
}))
55-
.then(data => {
56-
const cpuSpeedMetrics = [];
57-
for (const metric in data) {
58-
cpuSpeedMetrics.push({
59-
metric,
60-
value: data[metric],
61-
category: 'CPU',
62-
time,
63-
});
64-
}
65-
return cpuSpeedMetrics;
66-
})
67-
.catch(err => {
68-
if (err) {
69-
throw err;
70-
}
71-
})
72-
);
7356

74-
promises.push(
75-
si
76-
.cpuTemperature()
77-
.then(data => ({
78-
average_temperature: data.main,
79-
max_temperature: data.max,
80-
}))
81-
.then(data => {
82-
const cpuTemperatureMetrics = [];
83-
for (const metric in data) {
84-
cpuTemperatureMetrics.push({
85-
metric,
86-
value: data[metric],
87-
category: 'CPU',
88-
time,
89-
});
90-
}
91-
return cpuTemperatureMetrics;
92-
})
93-
.catch(err => {
94-
if (err) {
95-
throw err;
96-
}
97-
})
98-
);
57+
si.cpu()
58+
.then(data => {
59+
const siMethodName = 'cpu';
60+
for (let metricName in collectedMetrics[siMethodName]) {
61+
healthDataCollection.push({
62+
metric: metricName,
63+
value: data[collectedMetrics[siMethodName][metricName]],
64+
category: 'CPU',
65+
time,
66+
});
67+
}
68+
})
69+
.catch(err => {
70+
if (err) {
71+
throw err;
72+
}
73+
});
9974

100-
promises.push(
101-
si
102-
.currentLoad()
103-
.then(data => ({
104-
average_CPU_load_percent: data.avg,
105-
current_CPU_load_percent: data.currentLoad,
106-
current_CPU_load_user_percent: data.currentLoadUser,
107-
current_CPU_load__system_percent: data.currentLoadSystem,
108-
current_CPU_load_nice_percent: data.currentLoadNice,
109-
current_CPU_load_idle_percent: data.currentLoadIdle,
110-
current_CPU_load_raw_ticks: data.rawCurrentLoad,
111-
}))
112-
.then(data => {
113-
const cpuLoadMetrics = [];
114-
for (const metric in data) {
115-
cpuLoadMetrics.push({
116-
metric,
117-
value: data[metric],
118-
category: 'CPU',
119-
time,
120-
});
121-
}
122-
return cpuLoadMetrics;
123-
})
124-
.catch(err => {
75+
si.cpuCurrentSpeed()
76+
.then(data => {
77+
const siMethodName = 'cpuCurrentSpeed';
78+
for (let metricName in collectedMetrics[siMethodName]) {
79+
healthDataCollection.push({
80+
metric: metricName,
81+
value: data[collectedMetrics[siMethodName][metricName]],
82+
category: 'CPU',
83+
time,
84+
});
85+
}
86+
})
87+
.catch(err => {
88+
if (err) {
12589
throw err;
126-
})
127-
);
90+
}
91+
});
12892

129-
promises.push(
130-
si
131-
.mem()
132-
.then(data => ({
133-
totalmemory_in_bytes: data.total,
134-
freememory_in_bytes: data.free,
135-
usedmemory_in_bytes: data.used,
136-
activememory_in_bytes: data.active,
137-
buffers_plus_cache_in_bytes: data.buffcache,
138-
available_memory: data.available,
139-
}))
140-
.then(data => {
141-
const memMetrics = [];
142-
for (const metric in data) {
143-
memMetrics.push({
144-
metric,
145-
value: data[metric],
146-
category: 'Memory',
147-
time,
148-
});
149-
}
150-
return memMetrics;
151-
})
152-
.catch(err => {
153-
if (err) {
154-
throw err;
155-
}
156-
})
157-
);
93+
si.cpuTemperature()
94+
.then(data => {
95+
const siMethodName = 'cpuTemperature';
96+
for (let metricName in collectedMetrics[siMethodName]) {
97+
healthDataCollection.push({
98+
metric: metricName,
99+
value: data[collectedMetrics[siMethodName][metricName]],
100+
category: 'CPU',
101+
time,
102+
});
103+
}
104+
})
105+
.catch(err => {
106+
if (err) {
107+
throw err;
108+
}
109+
});
158110

159-
promises.push(
160-
si
161-
.processes()
162-
.then(data => ({
163-
totalprocesses: data.all,
164-
blockedprocesses: data.blocked,
165-
runningprocesses: data.running,
166-
sleepingprocesses: data.sleeping,
167-
}))
168-
.then(data => {
169-
const processMetrics = [];
170-
for (const metric in data) {
171-
processMetrics.push({
172-
metric,
173-
value: data[metric],
174-
category: 'Processes',
175-
time,
176-
});
177-
}
178-
return processMetrics;
179-
})
180-
.catch(err => {
181-
if (err) {
182-
throw err;
183-
}
184-
})
185-
);
111+
si.currentLoad()
112+
.then(data => {
113+
const siMethodName = 'currentLoad';
114+
for (let metricName in collectedMetrics[siMethodName]) {
115+
healthDataCollection.push({
116+
metric: metricName,
117+
value: data[collectedMetrics[siMethodName][metricName]],
118+
category: 'CPU',
119+
time,
120+
});
121+
}
122+
})
123+
.catch(err => {
124+
if (err) {
125+
throw err;
126+
}
127+
});
128+
129+
si.mem()
130+
.then(data => {
131+
const siMethodName = 'mem';
132+
for (let metricName in collectedMetrics[siMethodName]) {
133+
healthDataCollection.push({
134+
metric: metricName,
135+
value: data[collectedMetrics[siMethodName][metricName]],
136+
category: 'Memory',
137+
time,
138+
});
139+
}
140+
})
141+
.catch(err => {
142+
if (err) {
143+
throw err;
144+
}
145+
});
146+
147+
si.processes()
148+
.then(data => {
149+
const siMethodName = 'processes';
150+
for (let metricName in collectedMetrics[siMethodName]) {
151+
healthDataCollection.push({
152+
metric: metricName,
153+
value: data[collectedMetrics[siMethodName][metricName]],
154+
category: 'Processes',
155+
time,
156+
});
157+
}
158+
})
159+
.catch(err => {
160+
if (err) {
161+
throw err;
162+
}
163+
});
186164

187-
promises.push(
188-
si
189-
.inetLatency()
190-
.then(data => ({
165+
si.inetLatency()
166+
.then(data => {
167+
const siMethodName = 'inetLatency';
168+
healthDataCollection.push({
191169
metric: 'latency',
192170
value: data,
193-
category: 'Latency',
171+
category: 'Memory',
194172
time,
195-
}))
196-
.catch(err => {
197-
if (err) {
198-
throw err;
199-
}
200-
})
201-
);
173+
});
174+
})
175+
.catch(err => {
176+
if (err) {
177+
throw err;
178+
}
179+
});
180+
202181
// Return a promise that resolves to an array of all of the data points unnested
203-
return Promise.all(promises)
204-
.then(array => array.flat())
205-
// Remove any empty strings, NaN, or "NaN" from values prevent database errors
206-
.then((array) => array.filter((a) => {
207-
if (isNaN(a.value) || a.value === 'NaN' || a.value === '') return false;
208-
else return true;
209-
}));
182+
return (
183+
Promise.all(healthDataCollection)
184+
// Remove any empty strings, NaN, or "NaN" from values prevent database errors
185+
.then(array =>
186+
array.filter(metric => {
187+
if (
188+
isNaN(metric.value) ||
189+
metric.value === 'NaN' ||
190+
metric.value === '' ||
191+
metric.value === null
192+
)
193+
return false;
194+
else return true;
195+
})
196+
)
197+
);
210198
};
211199

212200
module.exports = healthHelpers;

0 commit comments

Comments
 (0)