Skip to content

Commit 6328d2f

Browse files
committed
mongo.health rewrite
refactoring the mongo.health function The data from health metrics was not being passed out and there is now an error with duplicate mongo entries
1 parent 8adbc25 commit 6328d2f

1 file changed

Lines changed: 48 additions & 19 deletions

File tree

chronos_npm_package/controllers/mongo.js

Lines changed: 48 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ mongo.communications = ({ microservice, slack, email }) => {
7171
request: req.method,
7272
correlatingid: res.getHeaders()['x-correlation-id'],
7373
};
74+
console.log("NEW COMMS",newComms)
7475

7576
res.on('finish', () => {
7677
/**
@@ -107,26 +108,35 @@ mongo.communications = ({ microservice, slack, email }) => {
107108
* @param {number} interval Interval for continuous data collection
108109
*/
109110
mongo.health = async ({ microservice, interval, mode }) => {
110-
let l = 0;
111-
const currentMetricNames = {};
112-
113-
l = await mongo.getSavedMetricsLength(mode, currentMetricNames);
114111

112+
const getCurrentMets = async () => await MetricsModel.find({ mode });
113+
const currentMetrics = async () => await getCurrentMets()
114+
currentMetrics()
115+
// const currentMetrics = await MetricsModel.find({mode})
116+
// const getCurrentMetsLength = async() =>
117+
// console.log("CURR METS",currentMetrics.length)
115118
setInterval(() => {
116119
collectHealthData()
117120
.then(async healthMetrics => {
118-
console.log("HEALTH METRICS",healthMetrics)
119-
if (l !== healthMetrics.length) {
120-
l = await mongo.addMetrics(healthMetrics, mode, currentMetricNames);
121-
}
121+
// console.log("HEALTH METRICS",healthMetrics)
122+
123+
// if (l !== healthMetrics.length) {
124+
// l = await mongo.addMetrics(healthMetrics, mode, currentMetricNames);
125+
// }
126+
// if(currentMetrics.length !== healthMetrics.length) {
127+
// const test = await mongo.mikesAdd(healthMetrics,currentMetrics,mode)
128+
// console.log("FIRST DB",test)
129+
// }
122130
const HealthModel = HealthModelFunc(`${microservice}`);
123-
return HealthModel.insertMany(healthMetrics);
131+
// await HealthModel.insertMany(healthMetrics);
132+
// return
133+
124134
})
125135
.then(() => {
126136
console.log('Health data recorded in MongoDB');
127137
})
128138
.catch(err => console.log('Error inserting health documents: ', err));
129-
}, interval);
139+
}, 10000);
130140
};
131141

132142
/**
@@ -272,9 +282,9 @@ mongo.setQueryOnInterval = async config => {
272282
};
273283

274284
mongo.getSavedMetricsLength = async (mode, currentMetricNames) => {
275-
console.log('mongo.getSavedMetricsLength',{mode,currentMetricNames})
285+
// console.log('mongo.getSavedMetricsLength',{mode,currentMetricNames})
276286
let currentMetrics = await MetricsModel.find({ mode: mode });
277-
console.log("CURRENT METRICS",currentMetrics)
287+
// console.log("CURRENT METRICS",currentMetrics)
278288
if (currentMetrics.length > 0) {
279289
currentMetrics.forEach(el => {
280290
const { metric, selected } = el;
@@ -284,22 +294,41 @@ mongo.getSavedMetricsLength = async (mode, currentMetricNames) => {
284294
return currentMetrics.length ? currentMetrics.length : 0;
285295
};
286296

287-
mongo.addMetrics = async (arr, mode, obj, model) => {
297+
mongo.addMetrics = async (healthMetrics, mode, currentMetricNames) => {
298+
//This function adds only the new metrics from metrics model to the metrics database
288299
const metrics = [];
289300
const newMets = [];
290-
for (let metric of arr) {
291-
if (!(metric.metric in obj)) {
301+
for (let metric of healthMetrics) {
302+
// console.log("OUR METRICS",metric)
303+
if (!(metric.metric in currentMetricNames)) {
292304
const name = metric.metric;
293305
newMets.push({ metric: name, mode: mode });
294306
metrics.push(metric);
295-
obj[metric.metric] = true;
307+
currentMetricNames[metric.metric] = true;
296308
}
297309
};
298-
await MetricsModel.create(newMets);
299-
await model.create(metrics);
300-
return arr.length;
310+
// console.log("NEW METS",newMets)
311+
await MetricsModel.create({newMets});
312+
// await model.create(metrics);
313+
return healthMetrics.length;
301314
};
302315

316+
mongo.mikesAdd = async(healthMetrics,currentMetrics,mode) => {
317+
// console.log("INSIDE MIKES ADD")
318+
const metricMetaData = []
319+
for(let healthMetric of healthMetrics) {
320+
if(!currentMetrics[healthMetric]) {
321+
const newMetric = {
322+
metric: healthMetric.metric,
323+
mode:mode
324+
}
325+
metricMetaData.push(newMetric)
326+
}
327+
}
328+
// console.log("META DATA",metricMetaData)
329+
const mikesTest = await MetricsModel.create({metricMetaData})
330+
return mikesTest
331+
}
303332
// 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.
304333
//
305334
// mongo.modifyMetrics = (config) => {

0 commit comments

Comments
 (0)