-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
70 lines (56 loc) · 1.9 KB
/
index.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
import 'dotenv/config'
import * as http from 'http'
import { GAME_ID, LANGUAGE, getLiveStreams } from './twitch.js'
const PORT = 3000;
const requestHandler = async (request, response) => {
switch (request.url) {
case '/metrics':
response.writeHead(200, {
'Content-Type': 'text/plain',
'Cache-Control': 'no-cache'
});
response.write(await getMetrics());
response.end();
break;
default:
response.end('<a href="/metrics">Metrics</a>');
break;
}
};
const server = http.createServer(requestHandler);
server.listen(PORT, (err) => {
if (err) {
return console.log('Error starting webserver', err);
}
console.log(`Webserver is listening on http://localhost:${PORT}`);
});
async function getMetrics() {
let liveStreams = await getLiveStreams(GAME_ID, LANGUAGE)
let liveUsers = liveStreams
.map((item) => {
return {
username: item.user_login,
viewers: item.viewer_count,
language: item.language
}
})
// De-dedupe streams
let uniqueLiveUsers = liveUsers.reduce((accumulator, current) => {
if (!accumulator.find((item) => item.username === current.username)) {
accumulator.push(current);
}
return accumulator;
}, []);
// Sort live users alphabetically
uniqueLiveUsers = uniqueLiveUsers.sort((a, b) => {
return a.username.localeCompare(b.username);
})
console.log(uniqueLiveUsers)
console.log("Number of people live:", uniqueLiveUsers.length)
let metricName = `twitch_livestream`
let metric = `# TYPE ${metricName} gauge\n`;
for (const user of uniqueLiveUsers) {
metric += `twitch_livestream{user="${user.username}",language="${user.language}"} ${user.viewers}\n`;
}
return metric
}