-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmwMongo.js
163 lines (147 loc) · 3.65 KB
/
mwMongo.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
const mongoose = require('mongoose');
const si = require('systeminformation');
// Required to get rid of deprecation warnings
mongoose.set('useUnifiedTopology', true);
mongoose.set('useNewUrlParser', true);
const chronos = {};
let currentMicroservicePath;
chronos.connectDB = (userOwnedDB) => {
mongoose.connect(`${userOwnedDB}`,
() => {
console.log('Chronos database is connected...');
});
},
chronos.microCom = (userOwnedDB, currentMicroservice) => {
chronos.connectDB(userOwnedDB);
return function (req, res, next) {
currentMicroservicePath = currentMicroservice;
require('./Communication.js');
const Communication = mongoose.model('Communication');
const newCommunication = {
currentMicroservice: currentMicroservicePath,
targetedEndpoint: req.originalUrl,
reqType: req.method,
timeSent: Date.now(),
correlatingId: res.getHeaders()['x-correlation-id'],
};
res.on('finish', () => {
newCommunication.resStatus = res.statusCode;
newCommunication.resMessage = res.statusMessage;
const communication = new Communication(newCommunication);
communication.save()
.then(() => {
next();
})
.catch((err) => {
if (err) {
throw err;
}
});
});
next();
};
},
chronos.microHealth = (userOwnedDB, currentMicroservice) => {
chronos.connectDB(userOwnedDB);
require('./MicroserviceHealth.js');
const MicroserviceHealth = mongoose.model('MicroserviceHealth');
let cpuCurrentSpeed;
let cpuTemperature;
let cpuCurrentLoadPercentage;
let totalMemory;
let freeMemory;
let usedMemory;
let activeMemory;
let latency;
let timestamp;
let totalNumProcesses;
let numBlockedProcesses;
let numRunningProcesses;
let numSleepingProcesses;
setInterval(() => {
si.cpuCurrentspeed()
.then((data) => {
cpuCurrentSpeed = data.avg;
})
.catch((err) => {
if (err) {
throw err;
}
});
si.cpuTemperature()
.then((data) => {
cpuTemperature = data.main;
})
.catch((err) => {
if (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) => {
if (err) {
throw err;
}
});
si.processes()
.then((data) => {
totalNumProcesses = data.all;
numBlockedProcesses = data.blocked;
numRunningProcesses = data.running;
numSleepingProcesses = data.sleeping;
})
.catch((err) => {
if (err) {
throw err;
}
});
si.inetLatency()
.then((data) => {
latency = data;
})
.catch((err) => {
if (err) {
throw err;
}
});
const newHealthPoint = {
timestamp: Date.now(),
currentMicroservice: currentMicroservicePath,
cpuCurrentSpeed,
cpuTemperature,
cpuCurrentLoadPercentage,
totalMemory,
freeMemory,
usedMemory,
activeMemory,
totalNumProcesses,
numRunningProcesses,
numBlockedProcesses,
numSleepingProcesses,
latency,
};
const healthPoint = new MicroserviceHealth(newHealthPoint);
healthPoint
.save()
.then(() => {})
.catch((err) => {
if (err) {
throw err;
}
});
}, 1000);
};
module.exports = chronos;