Skip to content

Commit c251b8f

Browse files
committed
added selectmetrics functionality for health metrics. Tested front end to confirm it works correctly
1 parent c37c5b5 commit c251b8f

File tree

3 files changed

+54
-28
lines changed

3 files changed

+54
-28
lines changed

chronos_npm_package/controllers/mongo.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,8 @@ mongo.health = async ({ microservice, interval, mode }) => {
134134
* Runs instead of health if dockerized is true
135135
* Collects information on the docker container
136136
*/
137-
mongo.docker = ({ microservice, interval }) => {
137+
mongo.docker = ({ microservice, interval, mode }) => {
138+
138139
// Create collection using name of microservice
139140
const containerInfo = ContainerInfoFunc(`${microservice}-containerinfo`);
140141
dockerHelper.getDockerContainer(microservice)

chronos_npm_package/controllers/postgres.js

Lines changed: 47 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ postgres.connect = async ({ database }) => {
3535
postgres.services = ({ microservice, interval }) => {
3636
// Create services table if does not exist
3737
client.query(
38-
`CREATE TABLE IF NOT EXISTS services (
38+
`CREATE TABLE IF NOT EXISTS services (
3939
_id SERIAL PRIMARY KEY NOT NULL,
4040
microservice VARCHAR(248) NOT NULL UNIQUE,
4141
interval INTEGER NOT NULL)`,
@@ -46,8 +46,8 @@ postgres.services = ({ microservice, interval }) => {
4646
}
4747
);
4848

49-
client.query(`
50-
CREATE TABLE IF NOT EXISTS metrics (
49+
client.query(
50+
`CREATE TABLE IF NOT EXISTS metrics (
5151
_id SERIAL PRIMARY KEY NOT NULL,
5252
metric TEXT NOT NULL UNIQUE,
5353
selected BOOLEAN,
@@ -163,13 +163,14 @@ function createQueryString(numRows, serviceName) {
163163

164164
// Places the values being inserted into postgres into an array that will eventually
165165
// hydrate the parameterized query
166-
function createQueryArray(dataPointsArray) {
166+
function createQueryArray(dataPointsArray, currentMetricNames) {
167167
const queryArray = [];
168168
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
173174
}
174175
return queryArray;
175176
}
@@ -179,7 +180,12 @@ function createQueryArray(dataPointsArray) {
179180
* @param {string} microservice Microservice name
180181
* @param {number} interval Interval for continuous data collection
181182
*/
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+
183189
// Create table for the microservice if it doesn't exist yet
184190
const createTableQuery = `
185191
CREATE TABLE IF NOT EXISTS ${microservice} (
@@ -197,10 +203,14 @@ postgres.health = ({ microservice, interval }) => {
197203
// Save data point at every interval (ms)
198204
setInterval(() => {
199205
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;
202212
const queryString = createQueryString(numRows, microservice);
203-
const queryArray = createQueryArray(data);
213+
const queryArray = createQueryArray(documents);
204214
// console.log('POSTGRES QUERY STRING: ', queryString);
205215
// console.log('POSTGRES QUERY ARRAY', queryArray);
206216
return client.query(queryString, queryArray);
@@ -357,22 +367,8 @@ postgres.setQueryOnInterval = async (config) => {
357367
metricsQuery(config)
358368
.then(async (parsedArray) => {
359369
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);
372371
}
373-
return parsedArray;
374-
})
375-
.then(parsedArray => {
376372
const documents = [];
377373
for (const metric of parsedArray) {
378374
if (currentMetricNames[metric.metric]) documents.push(metric)
@@ -387,4 +383,28 @@ postgres.setQueryOnInterval = async (config) => {
387383
}, config.interval);
388384
}
389385

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+
390410
module.exports = postgres;

electron/routes/data.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,11 @@ ipcMain.on('updateSavedMetrics', async (message: Electron.IpcMainEvent, args: Ob
229229
})
230230
// let result = await MetricsModel.update();
231231
}
232+
if (currentDatabaseType === 'SQL' && args.length) {
233+
args.forEach(async (el: any) => {
234+
await pool.query(`UPDATE metrics SET selected=${el.selected} WHERE metric='${el.metric}'`)
235+
})
236+
}
232237
}
233238

234239
catch (err) {

0 commit comments

Comments
 (0)