Skip to content

Commit cc0aad4

Browse files
authored
Merge pull request #12 from oslabs-beta/Lucie/Add_comments
Lucie/add comments
2 parents 14a1351 + ecd0421 commit cc0aad4

File tree

8 files changed

+292
-206
lines changed

8 files changed

+292
-206
lines changed

chronos_npm_package/chronos.js

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -149,11 +149,7 @@ class Chronos {
149149

150150
ServerWrapper(server, proto, methods) {
151151
/**
152-
* Wraps the gRPC server object to automatically write logs to user configed DB
153-
*
154-
* If the provided database is MongoDB, connection will be made to the Mongodb Atlas
155-
*
156-
* If the provided database is PostgreSQL, connection will be made to PostgreSQL client
152+
* Wraps the gRPC server object to automatically write logs to provided DB
157153
* @param {*} server
158154
* @param {*} proto
159155
* @param {*} methods
@@ -171,11 +167,7 @@ class Chronos {
171167

172168
ClientWrapper(client, service) {
173169
/**
174-
* Wraps the gRPC client to automatically write logs to user configed DB
175-
*
176-
* If the provided database is MongoDB, connection will be made to the Mongodb Atlas
177-
*
178-
* If the provided database is PostgreSQL, connection will be made to PostgreSQL client
170+
* Wraps the gRPC client to automatically write logs to provided DB
179171
*
180172
* @param {*} client
181173
* @param {*} service

chronos_npm_package/controllers/awsHelpers.js

Whitespace-only changes.

chronos_npm_package/controllers/dockerHelper.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
const si = require('systeminformation');
22

3+
/**
4+
* Finds the data pt with containerName that matches microservice and extracts container ID, name, platform, and start time.
5+
* @param {*} microservice
6+
* @returns array of active containers (ea. container = an obj).
7+
*/
38
async function getDockerContainer(microservice) {
49
try {
5-
// dockerContainers() return an arr of active containers (ea. container = an obj).
6-
// Find the data pt with containerName that matches microservice.
7-
// Extract container ID, name, platform, and start time.
10+
811
const containers = await si.dockerContainers();
912
const out = {};;
1013
let found = false;
@@ -34,7 +37,6 @@ async function getDockerContainer(microservice) {
3437

3538

3639
async function readDockerContainer(input) {
37-
// We are going to take the input object and add more data to it;
3840
const out = {...input};
3941
try {
4042
const data = await si.dockerContainerStats(input.containerid);

chronos_npm_package/controllers/healthHelpers.js

Lines changed: 41 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ const si = require('systeminformation');
22

33
const healthHelpers = {};
44

5-
/* This object contains all systeminformation methods, metric names, and corresponding points */
5+
/**
6+
* This object contains all systeminformation methods,
7+
* metric names, and corresponding data points
8+
*/
69

710
const collectedMetrics = {
811
cpu: {
@@ -50,10 +53,18 @@ const collectedMetrics = {
5053
inetLatency: 'all data collected',
5154
};
5255

56+
/**
57+
* collectHealthData scrapes metrics for microservices
58+
* @returns Promise array with each metric in an object
59+
*/
60+
5361
healthHelpers.collectHealthData = () => {
5462
const healthDataCollection = [];
5563
const time = Date.now();
5664

65+
/** obtains core CPU metrics and creates and pushes object with
66+
* metric name and value to the healthDataCollection array
67+
*/
5768
si.cpu()
5869
.then(data => {
5970
const siMethodName = 'cpu';
@@ -72,6 +83,9 @@ healthHelpers.collectHealthData = () => {
7283
}
7384
});
7485

86+
/** obtains CPU speed metrics and creates and pushes object with
87+
* metric name and value to the healthDataCollection array
88+
*/
7589
si.cpuCurrentSpeed()
7690
.then(data => {
7791
const siMethodName = 'cpuCurrentSpeed';
@@ -90,6 +104,10 @@ healthHelpers.collectHealthData = () => {
90104
}
91105
});
92106

107+
/** obtains CPU temperature metrics and creates and pushes object with
108+
* metric name and value to the healthDataCollection array
109+
*/
110+
93111
si.cpuTemperature()
94112
.then(data => {
95113
const siMethodName = 'cpuTemperature';
@@ -108,6 +126,10 @@ healthHelpers.collectHealthData = () => {
108126
}
109127
});
110128

129+
/**
130+
* obtains metrics relating to current load and creates and pushes object with
131+
* metric name and value to the healthDataCollection array
132+
*/
111133
si.currentLoad()
112134
.then(data => {
113135
const siMethodName = 'currentLoad';
@@ -126,6 +148,10 @@ healthHelpers.collectHealthData = () => {
126148
}
127149
});
128150

151+
/**
152+
* obtains metrics relating to memory and creates and pushes object with
153+
* metric name and value to the healthDataCollection array
154+
*/
129155
si.mem()
130156
.then(data => {
131157
const siMethodName = 'mem';
@@ -144,6 +170,9 @@ healthHelpers.collectHealthData = () => {
144170
}
145171
});
146172

173+
/** obtains metrics relating to current processes and creates and pushes object with
174+
* metric name and value to the healthDataCollection array
175+
*/
147176
si.processes()
148177
.then(data => {
149178
const siMethodName = 'processes';
@@ -162,6 +191,9 @@ healthHelpers.collectHealthData = () => {
162191
}
163192
});
164193

194+
/** obtains latency and creates and pushes object with
195+
* metric name and value to the healthDataCollection array
196+
*/
165197
si.inetLatency()
166198
.then(data => {
167199
const siMethodName = 'inetLatency';
@@ -178,22 +210,14 @@ healthHelpers.collectHealthData = () => {
178210
}
179211
});
180212

181-
// Return a promise that resolves to an array of all of the data points unnested
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-
)
213+
/** Return a promise that resolves to an array of all of the data points
214+
* and removes any empty strings, NaN, or "NaN" from values prevent database errors
215+
*/
216+
return Promise.all(healthDataCollection).then(array =>
217+
array.filter(metric => {
218+
if (isNaN(metric.value) || metric.value === 'NaN' || metric.value === '') return false;
219+
else return true;
220+
})
197221
);
198222
};
199223

chronos_npm_package/controllers/mongo.js

Lines changed: 65 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,10 @@ mongo.connect = async ({ database }) => {
3939
* @param {string} microservice Microservice name
4040
* @param {number} interval Interval to collect data
4141
*/
42+
4243
mongo.services = ({ microservice, interval }) => {
4344
console.log(`Saving "${microservice}" to services...`);
44-
// Create newService object to store microservice information
4545
const newService = { microservice, interval };
46-
47-
// Create MongoDB document from newService
4846
const service = new ServicesModel(newService);
4947

5048
service
@@ -84,11 +82,10 @@ mongo.communications = ({ microservice, slack, email }) => {
8482
if (email) alert.sendEmail(res.statusCode, res.statusMessage, email);
8583
}
8684

87-
// Add status code and message to newComms
85+
/** Add status code and message to newComms */
8886
newComms.responsestatus = res.statusCode;
8987
newComms.responsemessage = res.statusMessage;
9088

91-
// Create MongoDB document from newComms
9289
const communication = new CommunicationModel(newComms);
9390
communication
9491
.save()
@@ -116,7 +113,7 @@ mongo.health = async ({ microservice, interval, mode }) => {
116113

117114
setInterval(() => {
118115
collectHealthData()
119-
.then(async (healthMetrics) => {
116+
.then(async healthMetrics => {
120117
if (l !== healthMetrics.length) {
121118
l = await mongo.addMetrics(healthMetrics, mode, currentMetricNames);
122119
}
@@ -135,40 +132,43 @@ mongo.health = async ({ microservice, interval, mode }) => {
135132
* Collects information on the docker container
136133
*/
137134
mongo.docker = ({ microservice, interval, mode }) => {
138-
139135
// Create collection using name of microservice
140136
const containerInfo = ContainerInfoFunc(`${microservice}-containerinfo`);
141-
dockerHelper.getDockerContainer(microservice)
142-
.then((containerData) => {
137+
dockerHelper
138+
.getDockerContainer(microservice)
139+
.then(containerData => {
143140
setInterval(() => {
144-
dockerHelper.readDockerContainer(containerData)
145-
.then((data) => {
141+
dockerHelper
142+
.readDockerContainer(containerData)
143+
.then(data => {
146144
return containerInfo.create(data);
147145
})
148-
.then((_) => console.log(`Docker data recorded in MongoDB collection ${microservice}-containerinfo`))
149-
.catch((err) => {
150-
throw new Error(err)
146+
.then(_ =>
147+
console.log(`Docker data recorded in MongoDB collection ${microservice}-containerinfo`)
148+
)
149+
.catch(err => {
150+
throw new Error(err);
151151
});
152-
}, interval)
153-
})
154-
155-
.catch((error) => {
156-
if (error.constructor.name === 'Error') throw error
157-
else throw new Error(error);
158-
})
152+
}, interval);
153+
})
154+
155+
.catch(error => {
156+
if (error.constructor.name === 'Error') throw error;
157+
else throw new Error(error);
158+
});
159159
};
160160

161161
/*
162162
This function takes as a parameter the promise returned from the kafkaFetch().
163163
It then takes the returned array of metrics, turns them into documents based on
164164
KafkaModel.js, and inserts them into the db at the provided uri with insertMany()
165165
*/
166-
mongo.serverQuery = (config) => {
166+
mongo.serverQuery = config => {
167167
mongo.saveService(config);
168168
mongo.setQueryOnInterval(config);
169-
}
169+
};
170170

171-
mongo.saveService = (config) => {
171+
mongo.saveService = config => {
172172
let microservice;
173173
if (config.mode === 'kafka') {
174174
microservice = 'kafkametrics';
@@ -183,16 +183,19 @@ mongo.saveService = (config) => {
183183
interval: config.interval,
184184
});
185185

186-
service.save()
186+
service
187+
.save()
187188
.then(() => console.log(`Adding "${microservice}" to the services table`))
188-
.catch(err => console.log(`Error saving "${microservice}" to the services table: `, err.message));
189-
}
189+
.catch(err =>
190+
console.log(`Error saving "${microservice}" to the services table: `, err.message)
191+
);
192+
};
190193

191-
mongo.setQueryOnInterval = async (config) => {
194+
mongo.setQueryOnInterval = async config => {
192195
let model;
193196
let metricsQuery;
194197

195-
let l = 0;
198+
let length = 0;
196199
const currentMetricNames = {};
197200

198201
if (config.mode === 'kafka') {
@@ -204,62 +207,64 @@ mongo.setQueryOnInterval = async (config) => {
204207
} else {
205208
throw new Error('Unrecognized mode');
206209
}
207-
// When querying for currentMetrics, we narrow down the result to only include metrics for the current service being used.
208-
// This way, when we go to compare parsedArray to currentMetricNames, the length of the arrays should match up unless there are new metrics available to view
209210

210-
l = await mongo.getSavedMetricsLength(config.mode, currentMetricNames);
211+
length = await mongo.getSavedMetricsLength(config.mode, currentMetricNames);
211212

212-
console.log('currentMetricNames is: ', Object.keys(currentMetricNames).length)
213+
console.log('currentMetricNames is: ', Object.keys(currentMetricNames).length);
213214
// Use setInterval to send queries to metrics server and then pipe responses to database
214215
setInterval(() => {
215216
metricsQuery(config)
216217
// This updates the Metrics Model with all chosen metrics. If there are no chosen metrics it sets all available metrics as chosen metrics within the metrics model.
217-
.then(async (parsedArray) => {
218+
.then(async parsedArray => {
218219
// This conditional would be used if new metrics are available to be tracked.
219-
if (l !== parsedArray.length) {
220-
l = await mongo.addMetrics(parsedArray, config.mode, currentMetricNames);
220+
if (length !== parsedArray.length) {
221+
length = await mongo.addMetrics(parsedArray, config.mode, currentMetricNames);
221222
}
222223
const documents = [];
223224
for (const metric of parsedArray) {
224-
// This will check if the current metric in the parsed array evaluates to true within the currentMetricNames object.
225-
// The currentMetricNames object is updated by the user when they select/deselect metrics on the electron app, so only the
226-
// requested metrics will actually be populated in the database, which helps to avoid overloading the db with unnecessary data.
225+
/**
226+
* This will check if the current metric in the parsed array
227+
* evaluates to true within the currentMetricNames object
228+
* which is updated by the user when they select/deselect metrics on the electron app
229+
* helping to avoid overloading the db with unnecessary data.
230+
*/
231+
227232
if (currentMetricNames[metric.metric]) documents.push(model(metric));
228233
}
229-
return model.insertMany(documents, (err) => {
234+
return model.insertMany(documents, err => {
230235
if (err) console.error(err);
231-
})
236+
});
232237
})
233238
.then(() => console.log(`${config.mode} metrics recorded in MongoDB`))
234239
.catch(err => console.log(`Error inserting ${config.mode} documents in MongoDB: `, err));
235240
}, config.interval);
236-
}
241+
};
237242

238243
mongo.getSavedMetricsLength = async (mode, currentMetricNames) => {
239-
let currentMetrics = await MetricsModel.find({mode: mode});
244+
let currentMetrics = await MetricsModel.find({ mode: mode });
240245
if (currentMetrics.length > 0) {
241246
currentMetrics.forEach(el => {
242-
const { metric, selected } = el;
243-
currentMetricNames[metric] = selected;
244-
})
247+
const { metric, selected } = el;
248+
currentMetricNames[metric] = selected;
249+
});
245250
}
246251
return currentMetrics.length ? currentMetrics.length : 0;
247-
}
252+
};
248253

249254
mongo.addMetrics = async (arr, mode, obj) => {
250-
const newMets = [];
251-
arr.forEach(el => {
252-
if (!(el.metric in obj)) {
253-
const { metric } = el;
254-
newMets.push({metric: metric, mode: mode})
255-
obj[el.metric] = true;
256-
}
257-
})
258-
await MetricsModel.insertMany(newMets, (err) => {
259-
if (err) console.error(err)
260-
})
261-
return arr.length;
262-
}
255+
const newMets = [];
256+
arr.forEach(el => {
257+
if (!(el.metric in obj)) {
258+
const { metric } = el;
259+
newMets.push({ metric: metric, mode: mode });
260+
obj[el.metric] = true;
261+
}
262+
});
263+
await MetricsModel.insertMany(newMets, err => {
264+
if (err) console.error(err);
265+
});
266+
return arr.length;
267+
};
263268

264269
// This middleware could be used if the user would like to update their chronos data in real time (immediately after updating saved metrics on the Chronos desktop app), but they would have to expose a URL/port to be queried for the Electron front end.
265270
//

0 commit comments

Comments
 (0)