1
1
import KafkaModel from '../models/KafkaModel' ;
2
2
import HealthModelFunc from '../models/HealthModel' ;
3
+ import { Pool } from 'pg' ;
3
4
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
+ }
5
9
6
- const aggregator = [
10
+ const aggregator : any [ ] = [
7
11
{
8
12
$setWindowFields : {
9
13
partitionBy : '$metric' ,
@@ -24,10 +28,9 @@ const aggregator = [
24
28
} ,
25
29
] ;
26
30
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 > => {
31
34
try {
32
35
const testModel = HealthModelFunc ( serviceName ) ;
33
36
let result = await testModel . aggregate ( aggregator ) ;
@@ -38,17 +41,18 @@ fetchData.mongoFetch = async function (serviceName) {
38
41
}
39
42
} ;
40
43
41
- fetchData . postgresFetch = async function ( serviceName , pool ) {
44
+ const postgresFetch = async (
45
+ serviceName : string ,
46
+ pool : Pool
47
+ ) : Promise < Array < { [ key : string ] : any [ ] } > | undefined > => {
42
48
const query = `
43
- WITH
44
- temp
45
- AS (
49
+ WITH temp AS (
46
50
SELECT
47
51
metric, value, category, time,
48
52
row_number() OVER(PARTITION BY metric ORDER BY time DESC) AS rowNumber
49
53
FROM
50
54
${ serviceName }
51
- )
55
+ )
52
56
SELECT
53
57
metric, value, category, time
54
58
FROM
@@ -57,12 +61,19 @@ fetchData.postgresFetch = async function (serviceName, pool) {
57
61
rowNumber <= 50
58
62
;` ;
59
63
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,
66
77
} ;
67
78
68
79
export { fetchData } ;
0 commit comments