Skip to content

Commit 4a71046

Browse files
authored
Merge pull request #25 from oslabs-beta/james/electron-main-refactor
James/electron main refactor
2 parents 7019460 + 921d96f commit 4a71046

File tree

4 files changed

+21
-42
lines changed

4 files changed

+21
-42
lines changed

chronos_npm_package/controllers/mongo.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,14 @@ It then takes the returned array of metrics, turns them into documents based on
218218
KafkaModel.js, and inserts them into the db at the provided uri with insertMany()
219219
*/
220220
chronos.kafka = function (userConfig) {
221+
// ensure that kafkametrics exists in the services table
222+
const service = new ServicesModel({ service: 'kafkametrics', interval: userConfig.interval });
223+
224+
service
225+
.save()
226+
.then(() => console.log(`Adding "kafkametrics" to the services table`))
227+
.catch(err => console.log(`Error saving "kafkametrics" to the services table: `, err.message));
228+
221229
// fetch the data from Kafka with kafkaFetch()
222230
// then take turn each result in the returned array into a kafkaModel doc
223231
// insertMany into the the KafkaModel

chronos_npm_package/controllers/postgres.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,8 @@ chronos.docker = function ({ microservice, interval }) {
298298
};
299299

300300
chronos.kafka = function (userConfig) {
301+
// Ensure that kafkametrics are a part of the services table
302+
chronos.services({ microservice: 'kafkametrics', interval: userConfig.interval });
301303
// create kafkametrics table if it does not exist
302304
const createTableQuery = `
303305
CREATE TABLE IF NOT EXISTS kafkametrics (

electron/routes/data.ts

Lines changed: 4 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@ import DockerModelFunc from '../models/DockerModel';
1212
import KafkaModel from '../models/KafkaModel';
1313
import fetch, { FetchError } from 'electron-fetch';
1414
import { lightWhite } from 'material-ui/styles/colors';
15-
import {fetchData} from './dataHelpers';
15+
//import { postgresFetch, mongoFetch } from './dataHelpers';
16+
import { fetchData } from './dataHelpers';
1617

17-
const postgresFetch = fetchData.postgresFetch;
1818
const mongoFetch = fetchData.mongoFetch;
19+
const postgresFetch = fetchData.postgresFetch;
1920

2021
require('dotenv').config();
2122
console.log('test console log work');
@@ -132,48 +133,12 @@ ipcMain.on('healthRequest', async (message: Electron.IpcMainEvent, service: stri
132133

133134
// Mongo Database
134135
if (currentDatabaseType === 'MongoDB') {
135-
// // Get document count
136-
let num = await HealthModelFunc(service).countDocuments({});
137-
// Get last 50 documents. If less than 50 documents, get all
138-
num = Math.max(num, 10);
139-
result = await HealthModelFunc(service)
140-
.find(
141-
{},
142-
{
143-
cpuspeed: 1,
144-
cputemp: 1,
145-
cpuloadpercent: 1,
146-
totalmemory: 1,
147-
freememory: 1,
148-
usedmemory: 1,
149-
activememory: 1,
150-
totalprocesses: 1,
151-
runningprocesses: 1,
152-
blockedprocesses: 1,
153-
sleepingprocesses: 1,
154-
latency: 1,
155-
time: 1,
156-
__v: 1,
157-
service,
158-
}
159-
)
160-
.skip(num - 50);
161-
136+
result = await mongoFetch(service);
162137
}
163138

164-
/**
165-
* `
166-
SELECT *, 'customers' as service FROM customers
167-
ORDER BY _id DESC
168-
LIMIT 50
169-
`;
170-
*
171-
*/
172-
173139
// SQL Database
174140
if (currentDatabaseType === 'SQL') {
175141
// Get last 50 documents. If less than 50 get all
176-
177142
result = await postgresFetch(service, pool);
178143
console.log("result-health in data.ts:", JSON.stringify(result));
179144
}

electron/routes/dataHelpers.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,11 @@ const aggregator = [
2626
// TCreate a model based on the serviceName
2727
// Create an aggregator based on the aggregator variable
2828
// return the result
29-
fetchData.mongoFetch = function (serviceName) {
30-
29+
fetchData.mongoFetch = async function (serviceName) {
30+
const testModel = HealthModelFunc(serviceName);
31+
let result = await testModel.aggregate(aggregator);
32+
result = [{ [serviceName]: result }];
33+
return result;
3134
};
3235

3336
fetchData.postgresFetch = async function (serviceName, pool) {
@@ -57,4 +60,5 @@ fetchData.postgresFetch = async function (serviceName, pool) {
5760
return result;
5861
};
5962

60-
export {fetchData};
63+
export { fetchData };
64+

0 commit comments

Comments
 (0)