@@ -39,12 +39,10 @@ mongo.connect = async ({ database }) => {
3939 * @param {string } microservice Microservice name
4040 * @param {number } interval Interval to collect data
4141 */
42+
4243mongo . 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 */
137134mongo . 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
238243mongo . 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
249254mongo . 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