-
Notifications
You must be signed in to change notification settings - Fork 0
/
webserver.js
38 lines (30 loc) · 1021 Bytes
/
webserver.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
import { WebSocketServer } from 'ws';
import {createClient} from 'redis';
import { TimeSeriesDuplicatePolicies, TimeSeriesEncoding } from '@redis/time-series';
const wsserver = new WebSocketServer({ port: 8080 });
const client = createClient({
url: 'redis://127.0.0.1:9876'
}); // Redis write instance for data ingestion
await client.connect();
const key = 'performancedata';
const tsExists = await client.exists(key);
const labels = ['perf'];
if(!tsExists){
await client.ts.create(key, {
RETENTION: 86400000,
ENCODING: TimeSeriesEncoding.UNCOMPRESSED,
DUPLICATE_POLICY: TimeSeriesDuplicatePolicies.BLOCK
});
}
wsserver.on('connection', (socket) => {
socket.on('message', async (data) => {
var curTime = new Date().getTime();
var perfData = JSON.parse(data);
var num = Number(perfData.systemMemoryUsage);
await client.ts.add(key, curTime, num);
});
});
wsserver.on('close', () => {
client.quit();
});
console.log('Service is now running ... ');