Skip to content

Commit acf3322

Browse files
authored
Merge pull request #5 from oslabs-beta/jeff
Converted dataHelpers.js to dataHelpers.tsx
2 parents 34797fb + 7bb52e2 commit acf3322

File tree

1 file changed

+28
-17
lines changed

1 file changed

+28
-17
lines changed
Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
import KafkaModel from '../models/KafkaModel';
22
import HealthModelFunc from '../models/HealthModel';
3+
import { Pool } from 'pg';
34

4-
const fetchData = {};
5+
interface fetchData {
6+
mongoFetch: (serviceName: string) => Promise<Array<{ [key: string]: any[] }>>;
7+
postgresFetch: (serviceName: string, pool: Pool) => Promise<Array<{ [key: string]: any[] }>>;
8+
}
59

6-
const aggregator = [
10+
const aggregator: any[] = [
711
{
812
$setWindowFields: {
913
partitionBy: '$metric',
@@ -24,10 +28,9 @@ const aggregator = [
2428
},
2529
];
2630

27-
// healthModelFunc creates a model based on the serviceName
28-
// Create an aggregator based on the aggregator variable
29-
// return the result
30-
fetchData.mongoFetch = async function (serviceName) {
31+
const mongoFetch = async (
32+
serviceName: string
33+
): Promise<Array<{ [key: string]: any[] }> | undefined> => {
3134
try {
3235
const testModel = HealthModelFunc(serviceName);
3336
let result = await testModel.aggregate(aggregator);
@@ -38,17 +41,18 @@ fetchData.mongoFetch = async function (serviceName) {
3841
}
3942
};
4043

41-
fetchData.postgresFetch = async function (serviceName, pool) {
44+
const postgresFetch = async (
45+
serviceName: string,
46+
pool: Pool
47+
): Promise<Array<{ [key: string]: any[] }> | undefined> => {
4248
const query = `
43-
WITH
44-
temp
45-
AS (
49+
WITH temp AS (
4650
SELECT
4751
metric, value, category, time,
4852
row_number() OVER(PARTITION BY metric ORDER BY time DESC) AS rowNumber
4953
FROM
5054
${serviceName}
51-
)
55+
)
5256
SELECT
5357
metric, value, category, time
5458
FROM
@@ -57,12 +61,19 @@ fetchData.postgresFetch = async function (serviceName, pool) {
5761
rowNumber <= 50
5862
;`;
5963

60-
let result = await pool.query(query);
61-
// console.log('result.rows in dataHelpers postgresFetch:', JSON.stringify(result.rows));
62-
result = result.rows;
63-
result = [{ [serviceName]: result }];
64-
// console.log('result with servicename in dataHelpers postgresFetch:', JSON.stringify(result));
65-
return result;
64+
try {
65+
let result = await pool.query(query);
66+
result = result.rows;
67+
result = [{ [serviceName]: result }];
68+
return result;
69+
} catch (error) {
70+
console.log('Query error in postgresFetch(): ', error);
71+
}
72+
};
73+
74+
const fetchData = {
75+
mongoFetch,
76+
postgresFetch,
6677
};
6778

6879
export { fetchData };

0 commit comments

Comments
 (0)