Skip to content

Commit f2fc7ff

Browse files
author
Lucienne Seidler
committed
comments added to dockerHelper, mongo.js, communicationModel and ContainerInfo. Deleted awsHelpers.js
1 parent c3682ff commit f2fc7ff

File tree

6 files changed

+82
-69
lines changed

6 files changed

+82
-69
lines changed

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: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,9 @@ healthHelpers.collectHealthData = () => {
126126
}
127127
});
128128

129-
/** obtains metrics relating to current load and creates and pushes object with
130-
* metric name and value to the healthDataCollection array
129+
/**
130+
* obtains metrics relating to current load and creates and pushes object with
131+
* metric name and value to the healthDataCollection array
131132
*/
132133
si.currentLoad()
133134
.then(data => {
@@ -147,8 +148,9 @@ healthHelpers.collectHealthData = () => {
147148
}
148149
});
149150

150-
/** obtains metrics relating to memory and creates and pushes object with
151-
* metric name and value to the healthDataCollection array
151+
/**
152+
* obtains metrics relating to memory and creates and pushes object with
153+
* metric name and value to the healthDataCollection array
152154
*/
153155
si.mem()
154156
.then(data => {

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
//

chronos_npm_package/models/CommunicationModel.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ const mongoose = require('mongoose');
22

33
const { Schema } = mongoose;
44

5+
/**
6+
* This model is used for storing alerts and communications to developers
7+
*/
8+
59
const CommunicationsSchema = new Schema({
610
microservice: {
711
type: String,

chronos_npm_package/models/ContainerInfo.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ const ContainerSchema = new Schema({
4545
time: {
4646
type: Date,
4747
default: Date.now(),
48-
}
48+
},
4949
});
5050

5151
module.exports = ContainerName => mongoose.model(ContainerName, ContainerSchema);

0 commit comments

Comments
 (0)