Skip to content

Commit c37c5b5

Browse files
committed
functionalized repeated code to make code more DRY. Added selectMetricsc functionality to Mongo health and docker methods
1 parent 3a01464 commit c37c5b5

File tree

1 file changed

+41
-30
lines changed

1 file changed

+41
-30
lines changed

chronos_npm_package/controllers/mongo.js

Lines changed: 41 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,18 @@ mongo.communications = ({ microservice, slack, email }) => {
108108
* @param {string} microservice Microservice name
109109
* @param {number} interval Interval for continuous data collection
110110
*/
111-
mongo.health = ({ microservice, interval }) => {
111+
mongo.health = async ({ microservice, interval, mode }) => {
112+
let l = 0;
113+
const currentMetricNames = {};
114+
115+
l = await mongo.getSavedMetricsLength(mode, currentMetricNames);
116+
112117
setInterval(() => {
113118
collectHealthData()
114-
.then(healthMetrics => {
119+
.then(async (healthMetrics) => {
120+
if (l !== healthMetrics.length) {
121+
l = await mongo.addMetrics(healthMetrics, mode, currentMetricNames);
122+
}
115123
const HealthModel = HealthModelFunc(`${microservice}`);
116124
return HealthModel.insertMany(healthMetrics);
117125
})
@@ -182,7 +190,7 @@ mongo.saveService = (config) => {
182190
mongo.setQueryOnInterval = async (config) => {
183191
let model;
184192
let metricsQuery;
185-
let currentMetrics;
193+
186194
let l = 0;
187195
const currentMetricNames = {};
188196

@@ -198,39 +206,18 @@ mongo.setQueryOnInterval = async (config) => {
198206
// When querying for currentMetrics, we narrow down the result to only include metrics for the current service being used.
199207
// 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
200208

201-
currentMetrics = await MetricsModel.find({mode: config.mode});
202-
if (currentMetrics.length > 0) {
203-
currentMetrics.forEach(el => {
204-
const { metric, selected } = el;
205-
currentMetricNames[metric] = selected;
206-
l = currentMetrics.length;
207-
})
208-
}
209+
l = await mongo.getSavedMetricsLength(config.mode, currentMetricNames);
210+
211+
console.log('currentMetricNames is: ', Object.keys(currentMetricNames).length)
209212
// Use setInterval to send queries to metrics server and then pipe responses to database
210213
setInterval(() => {
211214
metricsQuery(config)
212215
// 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.
213216
.then(async (parsedArray) => {
214217
// This conditional would be used if new metrics are available to be tracked.
215218
if (l !== parsedArray.length) {
216-
console.log('currentMetricNames is less than parsedArray length, new metrics available to track');
217-
console.log('currentMetricNames has a length of: ', l, ' and parsedArray.length is: ', parsedArray.length);
218-
const newMets = [];
219-
parsedArray.forEach(el => {
220-
if (!(el.metric in currentMetricNames)) {
221-
const { metric } = el;
222-
newMets.push(MetricsModel({metric: metric, mode: config.mode}))
223-
currentMetricNames[el.metric] = true;
224-
}
225-
})
226-
await MetricsModel.insertMany(newMets, (err) => {
227-
if (err) console.error(err)
228-
})
229-
l = parsedArray.length;
219+
l = await mongo.addMetrics(parsedArray, config.mode, currentMetricNames);
230220
}
231-
return parsedArray;
232-
})
233-
.then(parsedArray => {
234221
const documents = [];
235222
for (const metric of parsedArray) {
236223
// This will check if the current metric in the parsed array evaluates to true within the currentMetricNames object.
@@ -247,8 +234,33 @@ mongo.setQueryOnInterval = async (config) => {
247234
}, config.interval);
248235
}
249236

237+
mongo.getSavedMetricsLength = async (mode, currentMetricNames) => {
238+
let currentMetrics = await MetricsModel.find({mode: mode});
239+
if (currentMetrics.length > 0) {
240+
currentMetrics.forEach(el => {
241+
const { metric, selected } = el;
242+
currentMetricNames[metric] = selected;
243+
})
244+
}
245+
return currentMetrics.length ? currentMetrics.length : 0;
246+
}
247+
248+
mongo.addMetrics = async (arr, mode, obj) => {
249+
const newMets = [];
250+
arr.forEach(el => {
251+
if (!(el.metric in obj)) {
252+
const { metric } = el;
253+
newMets.push({metric: metric, mode: mode})
254+
obj[el.metric] = true;
255+
}
256+
})
257+
await MetricsModel.insertMany(newMets, (err) => {
258+
if (err) console.error(err)
259+
})
260+
return arr.length;
261+
}
250262

251-
// This middleware could be used if the user would like to update their chronos data, but they would have to expose a URL/port to be queried for the Electron front end.
263+
// 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.
252264
//
253265
// mongo.modifyMetrics = (config) => {
254266
// return function (req, res, next) {
@@ -262,5 +274,4 @@ mongo.setQueryOnInterval = async (config) => {
262274
// };
263275
// }
264276

265-
266277
module.exports = mongo;

0 commit comments

Comments
 (0)