@@ -39,12 +39,10 @@ mongo.connect = async ({ database }) => {
39
39
* @param {string } microservice Microservice name
40
40
* @param {number } interval Interval to collect data
41
41
*/
42
+
42
43
mongo . services = ( { microservice, interval } ) => {
43
44
console . log ( `Saving "${ microservice } " to services...` ) ;
44
- // Create newService object to store microservice information
45
45
const newService = { microservice, interval } ;
46
-
47
- // Create MongoDB document from newService
48
46
const service = new ServicesModel ( newService ) ;
49
47
50
48
service
@@ -84,11 +82,10 @@ mongo.communications = ({ microservice, slack, email }) => {
84
82
if ( email ) alert . sendEmail ( res . statusCode , res . statusMessage , email ) ;
85
83
}
86
84
87
- // Add status code and message to newComms
85
+ /** Add status code and message to newComms */
88
86
newComms . responsestatus = res . statusCode ;
89
87
newComms . responsemessage = res . statusMessage ;
90
88
91
- // Create MongoDB document from newComms
92
89
const communication = new CommunicationModel ( newComms ) ;
93
90
communication
94
91
. save ( )
@@ -116,7 +113,7 @@ mongo.health = async ({ microservice, interval, mode }) => {
116
113
117
114
setInterval ( ( ) => {
118
115
collectHealthData ( )
119
- . then ( async ( healthMetrics ) => {
116
+ . then ( async healthMetrics => {
120
117
if ( l !== healthMetrics . length ) {
121
118
l = await mongo . addMetrics ( healthMetrics , mode , currentMetricNames ) ;
122
119
}
@@ -135,40 +132,43 @@ mongo.health = async ({ microservice, interval, mode }) => {
135
132
* Collects information on the docker container
136
133
*/
137
134
mongo . docker = ( { microservice, interval, mode } ) => {
138
-
139
135
// Create collection using name of microservice
140
136
const containerInfo = ContainerInfoFunc ( `${ microservice } -containerinfo` ) ;
141
- dockerHelper . getDockerContainer ( microservice )
142
- . then ( ( containerData ) => {
137
+ dockerHelper
138
+ . getDockerContainer ( microservice )
139
+ . then ( containerData => {
143
140
setInterval ( ( ) => {
144
- dockerHelper . readDockerContainer ( containerData )
145
- . then ( ( data ) => {
141
+ dockerHelper
142
+ . readDockerContainer ( containerData )
143
+ . then ( data => {
146
144
return containerInfo . create ( data ) ;
147
145
} )
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 ) ;
151
151
} ) ;
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
+ } ) ;
159
159
} ;
160
160
161
161
/*
162
162
This function takes as a parameter the promise returned from the kafkaFetch().
163
163
It then takes the returned array of metrics, turns them into documents based on
164
164
KafkaModel.js, and inserts them into the db at the provided uri with insertMany()
165
165
*/
166
- mongo . serverQuery = ( config ) => {
166
+ mongo . serverQuery = config => {
167
167
mongo . saveService ( config ) ;
168
168
mongo . setQueryOnInterval ( config ) ;
169
- }
169
+ } ;
170
170
171
- mongo . saveService = ( config ) => {
171
+ mongo . saveService = config => {
172
172
let microservice ;
173
173
if ( config . mode === 'kafka' ) {
174
174
microservice = 'kafkametrics' ;
@@ -183,16 +183,19 @@ mongo.saveService = (config) => {
183
183
interval : config . interval ,
184
184
} ) ;
185
185
186
- service . save ( )
186
+ service
187
+ . save ( )
187
188
. 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
+ } ;
190
193
191
- mongo . setQueryOnInterval = async ( config ) => {
194
+ mongo . setQueryOnInterval = async config => {
192
195
let model ;
193
196
let metricsQuery ;
194
197
195
- let l = 0 ;
198
+ let length = 0 ;
196
199
const currentMetricNames = { } ;
197
200
198
201
if ( config . mode === 'kafka' ) {
@@ -204,62 +207,64 @@ mongo.setQueryOnInterval = async (config) => {
204
207
} else {
205
208
throw new Error ( 'Unrecognized mode' ) ;
206
209
}
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
209
210
210
- l = await mongo . getSavedMetricsLength ( config . mode , currentMetricNames ) ;
211
+ length = await mongo . getSavedMetricsLength ( config . mode , currentMetricNames ) ;
211
212
212
- console . log ( 'currentMetricNames is: ' , Object . keys ( currentMetricNames ) . length )
213
+ console . log ( 'currentMetricNames is: ' , Object . keys ( currentMetricNames ) . length ) ;
213
214
// Use setInterval to send queries to metrics server and then pipe responses to database
214
215
setInterval ( ( ) => {
215
216
metricsQuery ( config )
216
217
// 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 => {
218
219
// 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 ) ;
221
222
}
222
223
const documents = [ ] ;
223
224
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
+
227
232
if ( currentMetricNames [ metric . metric ] ) documents . push ( model ( metric ) ) ;
228
233
}
229
- return model . insertMany ( documents , ( err ) => {
234
+ return model . insertMany ( documents , err => {
230
235
if ( err ) console . error ( err ) ;
231
- } )
236
+ } ) ;
232
237
} )
233
238
. then ( ( ) => console . log ( `${ config . mode } metrics recorded in MongoDB` ) )
234
239
. catch ( err => console . log ( `Error inserting ${ config . mode } documents in MongoDB: ` , err ) ) ;
235
240
} , config . interval ) ;
236
- }
241
+ } ;
237
242
238
243
mongo . getSavedMetricsLength = async ( mode , currentMetricNames ) => {
239
- let currentMetrics = await MetricsModel . find ( { mode : mode } ) ;
244
+ let currentMetrics = await MetricsModel . find ( { mode : mode } ) ;
240
245
if ( currentMetrics . length > 0 ) {
241
246
currentMetrics . forEach ( el => {
242
- const { metric, selected } = el ;
243
- currentMetricNames [ metric ] = selected ;
244
- } )
247
+ const { metric, selected } = el ;
248
+ currentMetricNames [ metric ] = selected ;
249
+ } ) ;
245
250
}
246
251
return currentMetrics . length ? currentMetrics . length : 0 ;
247
- }
252
+ } ;
248
253
249
254
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
+ } ;
263
268
264
269
// 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.
265
270
//
0 commit comments