-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmwSQL.js
236 lines (214 loc) · 6.17 KB
/
mwSQL.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
// NPM package that gathers health information
const si = require('systeminformation');
const chronos = {};
// queryObj determines the setInterval needed for microHealth based on queryFreq parameter provided by user
const queryObj = {
s: 1000, // 1000 milliseconds per second
m: 60000, // 1000 milliseconds * 60 seconds
h: 3600000, // 60 seconds * 60 minutes per hour * 1000 milliseconds per second
d: 86400000, // 60 sec. * 60 min * 1000 ms per sec * 24 hours per day
w: 604800000, // 60 sec/min * 60 min/hr * 1000 ms/sec * 24 hours/day * 7 days per week
};
// microCom
chronos.microCom = (userOwnedDB, userInputMSName, req, res, next) => {
// create connection to user owned database
// we're using PostgreSQL and need to require pg
const { Client } = require('pg');
// grabbed from user input
const uri = userOwnedDB;
// creates a new instance of client
const client = new Client({
connectionString: uri,
});
// connects to DB, else throws error
client.connect((err, client, release) => {
if (err) {
throw new Error('Issue connecting to db');
}
});
// query created DB and create table if it doesn't already exist and create the columns. Throws error if needed.
client.query(
`CREATE TABLE IF NOT EXISTS communications(
id serial PRIMARY KEY,
currentMicroservice varchar(500) NOT NULL,
targetedEndpoint varchar(500) NOT NULL,
reqType varchar(500),
resStatus integer,
resMessage varchar(500),
timeSent timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
correlatingId varchar(500)
)`,
(err, results) => {
if (err) {
throw err;
}
},
);
return (req, res, next) => {
// correlating id that will persist thru the request from one server to the next
const correlatingId = res.getHeaders()['x-correlation-id'];
// user-input name for current microservice
const currentMicroservice = userInputMSName;
// What was our endpoint? Grabbed from request object
const targetedEndpoint = req.originalUrl;
// Type of request. Grabbed from request object
const reqType = req.method;
const queryString = 'INSERT INTO communications(currentMicroservice, targetedEndpoint, reqType, resStatus, resMessage, correlatingId) VALUES ($1, $2, $3, $4, $5, $6)';
// Waits for response to finish before pushing information into database
res.on('finish', () => {
// Grabs status code from response object
resStatus = res.statusCode;
// Grabs status message from response object
resMessage = res.statusMessage;
values = [
currentMicroservice,
targetedEndpoint,
reqType,
resStatus,
resMessage,
correlatingId,
];
client.query(queryString, values, (err, result) => {
if (err) {
throw err;
}
});
});
next();
};
},
chronos.microHealth = (userOwnedDB, userInputMSName, queryFreq) => {
const { Client } = require('pg');
const uri = userOwnedDB;
const client = new Client({
connectionString: uri,
});
client.connect((err, client, release) => {
if (err) {
console.log('Issue connecting to db');
}
});
let cpuCurrentSpeed;
let cpuTemperature;
let cpuCurrentLoadPercentage;
let totalMemory;
let freeMemory;
let usedMemory;
let activeMemory;
let latency;
let currentMicroservice;
let totalNumProcesses;
let numBlockedProcesses;
let numRunningProcesses;
let numSleepingProcesses;
currentMicroservice = userInputMSName;
client.query(
`CREATE TABLE IF NOT EXISTS healthInfo (
id SERIAL PRIMARY KEY,
time timestamp DEFAULT CURRENT_TIMESTAMP,
currentMicroservice varchar(500),
cpuCurrentSpeed float DEFAULT 0.0,
cpuCurrentLoadPercentage float DEFAULT 0.00,
cpuTemperature float DEFAULT 0.0,
totalMemory real DEFAULT 0,
freeMemory real DEFAULT 0,
usedMemory real DEFAULT 0,
activeMemory real DEFAULT 0,
totalNumProcesses real DEFAULT 0,
numRunningProcesses real DEFAULT 0,
numBlockedProcesses real DEFAULT 0,
numSleepingProcesses real DEFAULT 0,
latency float DEFAULT 0.0
)`,
(err, results) => {
if (err) {
throw err;
}
},
);
setInterval(() => {
si.cpuCurrentspeed()
.then((data) => {
cpuCurrentSpeed = data.avg;
})
.catch((err) => {
throw err;
});
si.cpuTemperature()
.then((data) => {
cpuTemperature = data.main;
})
.catch((err) => {
throw err;
});
si.currentLoad()
.then((data) => {
cpuCurrentLoadPercentage = data.currentload;
})
.catch((err) => {
throw err;
});
si.mem()
.then((data) => {
totalMemory = data.total;
freeMemory = data.free;
usedMemory = data.used;
activeMemory = data.active;
})
.catch((err) => {
throw err;
});
si.processes()
.then((data) => {
totalNumProcesses = data.all;
numBlockedProcesses = data.blocked;
numRunningProcesses = data.running;
numSleepingProcesses = data.sleeping;
})
.catch((err) => {
throw err;
});
si.inetLatency()
.then((data) => {
latency = data;
})
.catch((err) => {
throw err;
});
const queryString = `INSERT INTO healthInfo(
currentMicroservice,
cpuCurrentSpeed,
cpuTemperature,
cpuCurrentLoadPercentage,
totalMemory,
freeMemory,
usedMemory,
activeMemory,
totalNumProcesses,
numRunningProcesses,
numBlockedProcesses,
numSleepingProcesses,
latency) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13)`;
const values = [
currentMicroservice,
cpuCurrentSpeed,
cpuTemperature,
cpuCurrentLoadPercentage,
totalMemory,
freeMemory,
usedMemory,
activeMemory,
totalNumProcesses,
numRunningProcesses,
numBlockedProcesses,
numSleepingProcesses,
latency,
];
client.query(queryString, values, (err, results) => {
if (err) {
throw err;
}
});
}, queryObj[queryFreq]);
};
module.exports = chronos;