-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
57 lines (44 loc) · 1.22 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
/* eslint-disable no-console */
const influx = require('./influx');
const themeparks = require('./themeparks');
const INTERVAL_MINS = parseInt(process.env.INTERVAL_MINS, 10) || 5;
async function getWaitTimePoints(themepark) {
const startDate = new Date();
const rides = await themepark.GetWaitTimes();
console.info(
`Retrieved wait times for ${themepark.Name} in ${Date.now() -
startDate.getTime()}ms`
);
return rides.map(ride => {
let timestamp = startDate;
if (typeof ride.lastUpdate === 'string') {
timestamp = new Date(ride.lastUpdate);
}
return {
measurement: 'wait_time',
fields: {
value: ride.waitTime,
},
tags: {
active: ride.active,
park_name: themepark.Name,
ride_name: ride.name,
},
timestamp,
};
});
}
async function saveWaitTimes() {
try {
const pointsArrays = await Promise.all(
Object.values(themeparks).map(getWaitTimePoints)
);
const points = pointsArrays.flat();
await influx.writePoints(points);
console.log(`Correctly saved ${points.length} wait times`);
} catch (err) {
console.error(err);
}
}
setInterval(saveWaitTimes, INTERVAL_MINS * 60 * 1000);
saveWaitTimes();