@@ -35,7 +35,7 @@ postgres.connect = async ({ database }) => {
35
35
postgres . services = ( { microservice, interval } ) => {
36
36
// Create services table if does not exist
37
37
client . query (
38
- `CREATE TABLE IF NOT EXISTS services (
38
+ `CREATE TABLE IF NOT EXISTS services (
39
39
_id SERIAL PRIMARY KEY NOT NULL,
40
40
microservice VARCHAR(248) NOT NULL UNIQUE,
41
41
interval INTEGER NOT NULL)` ,
@@ -46,8 +46,8 @@ postgres.services = ({ microservice, interval }) => {
46
46
}
47
47
) ;
48
48
49
- client . query ( `
50
- CREATE TABLE IF NOT EXISTS metrics (
49
+ client . query (
50
+ ` CREATE TABLE IF NOT EXISTS metrics (
51
51
_id SERIAL PRIMARY KEY NOT NULL,
52
52
metric TEXT NOT NULL UNIQUE,
53
53
selected BOOLEAN,
@@ -163,13 +163,14 @@ function createQueryString(numRows, serviceName) {
163
163
164
164
// Places the values being inserted into postgres into an array that will eventually
165
165
// hydrate the parameterized query
166
- function createQueryArray ( dataPointsArray ) {
166
+ function createQueryArray ( dataPointsArray , currentMetricNames ) {
167
167
const queryArray = [ ] ;
168
168
for ( const element of dataPointsArray ) {
169
- queryArray . push ( element . metric ) ;
170
- queryArray . push ( element . value ) ;
171
- queryArray . push ( element . category ) ;
172
- queryArray . push ( element . time / 1000 ) ; // Converts milliseconds to seconds to work with postgres
169
+ queryArray . push ( element . metric ) ;
170
+ queryArray . push ( element . value ) ;
171
+ queryArray . push ( element . category ) ;
172
+ queryArray . push ( element . time / 1000 ) ;
173
+ // Converts milliseconds to seconds to work with postgres
173
174
}
174
175
return queryArray ;
175
176
}
@@ -179,7 +180,12 @@ function createQueryArray(dataPointsArray) {
179
180
* @param {string } microservice Microservice name
180
181
* @param {number } interval Interval for continuous data collection
181
182
*/
182
- postgres . health = ( { microservice, interval } ) => {
183
+ postgres . health = async ( { microservice, interval, mode } ) => {
184
+ let l = 0 ;
185
+ const currentMetricNames = { } ;
186
+
187
+ l = await postgres . getSavedMetricsLength ( mode , currentMetricNames ) ;
188
+
183
189
// Create table for the microservice if it doesn't exist yet
184
190
const createTableQuery = `
185
191
CREATE TABLE IF NOT EXISTS ${ microservice } (
@@ -197,10 +203,14 @@ postgres.health = ({ microservice, interval }) => {
197
203
// Save data point at every interval (ms)
198
204
setInterval ( ( ) => {
199
205
collectHealthData ( )
200
- . then ( data => {
201
- const numRows = data . length ;
206
+ . then ( async ( data ) => {
207
+ if ( l !== data . length ) {
208
+ l = await postgres . addMetrics ( data , mode , currentMetricNames ) ;
209
+ }
210
+ const documents = data . filter ( el => ( el . metric in currentMetricNames ) ) ;
211
+ const numRows = documents . length ;
202
212
const queryString = createQueryString ( numRows , microservice ) ;
203
- const queryArray = createQueryArray ( data ) ;
213
+ const queryArray = createQueryArray ( documents ) ;
204
214
// console.log('POSTGRES QUERY STRING: ', queryString);
205
215
// console.log('POSTGRES QUERY ARRAY', queryArray);
206
216
return client . query ( queryString , queryArray ) ;
@@ -357,22 +367,8 @@ postgres.setQueryOnInterval = async (config) => {
357
367
metricsQuery ( config )
358
368
. then ( async ( parsedArray ) => {
359
369
if ( l !== parsedArray . length ) {
360
- console . log ( 'currentMetricNames is less than parsedArray length, new metrics available to track' ) ;
361
- console . log ( 'currentMetricNames has a length of: ' , l , ' and parsedArray.length is: ' , parsedArray . length ) ;
362
- let metricsQueryString = 'INSERT INTO metrics (metric, selected, mode) VALUES ' ;
363
- parsedArray . forEach ( el => {
364
- if ( ! ( el . metric in currentMetricNames ) ) {
365
- currentMetricNames [ el . metric ] = true ;
366
- metricsQueryString = metricsQueryString . concat ( `('${ el . metric } ', true, '${ config . mode } '), ` ) ;
367
- }
368
- } )
369
- metricsQueryString = metricsQueryString . slice ( 0 , metricsQueryString . lastIndexOf ( ', ' ) ) . concat ( ';' ) ;
370
- await client . query ( metricsQueryString ) ;
371
- l = parsedArray . length ;
370
+ l = await postgres . addMetrics ( parsedArray , config . mode , currentMetricNames ) ;
372
371
}
373
- return parsedArray ;
374
- } )
375
- . then ( parsedArray => {
376
372
const documents = [ ] ;
377
373
for ( const metric of parsedArray ) {
378
374
if ( currentMetricNames [ metric . metric ] ) documents . push ( metric )
@@ -387,4 +383,28 @@ postgres.setQueryOnInterval = async (config) => {
387
383
} , config . interval ) ;
388
384
}
389
385
386
+ postgres . getSavedMetricsLength = async ( mode , currentMetricNames ) => {
387
+ let currentMetrics = await client . query ( `SELECT * FROM metrics WHERE mode='${ mode } ';` ) ;
388
+ if ( currentMetrics . rows . length > 0 ) {
389
+ currentMetrics . rows . forEach ( el => {
390
+ const { metric, selected } = el ;
391
+ currentMetricNames [ metric ] = selected ;
392
+ } )
393
+ }
394
+ return currentMetrics . rows . length ? currentMetrics . rows . length : 0 ;
395
+ }
396
+
397
+ postgres . addMetrics = async ( arr , mode , currentMetricNames ) => {
398
+ let metricsQueryString = 'INSERT INTO metrics (metric, selected, mode) VALUES ' ;
399
+ arr . forEach ( el => {
400
+ if ( ! ( el . metric in currentMetricNames ) ) {
401
+ currentMetricNames [ el . metric ] = true ;
402
+ metricsQueryString = metricsQueryString . concat ( `('${ el . metric } ', true, '${ mode } '), ` ) ;
403
+ }
404
+ } )
405
+ metricsQueryString = metricsQueryString . slice ( 0 , metricsQueryString . lastIndexOf ( ', ' ) ) . concat ( ';' ) ;
406
+ await client . query ( metricsQueryString ) ;
407
+ return arr . length ;
408
+ }
409
+
390
410
module . exports = postgres ;
0 commit comments