-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
57 lines (47 loc) · 1.48 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
require('dotenv').config();
const { API, Cache, helpers, logger } = require('./utils');
const config = require('./config.json');
const cache = new Cache();
/**
* Main function
* - Checks 538 simulations
* - Sends SMS updates if odds have updated
*/
const checkOdds = async () => {
try {
logger.info('Checking Simulations');
// Get current simulations
const simulations = await API.getFteSimulations();
// Caculate odds
const odds = {
national: helpers.calculateOdds(simulations),
state: helpers.calculateOdds(simulations.map((sim) => sim.states[config.state])),
};
// Compare new national & state odds to config threshold
const [alertNationalOdds, alertStateOdds] = [
cache.isPastThreshold(odds, 'national'),
cache.isPastThreshold(odds, 'state'),
];
if (alertNationalOdds || alertStateOdds) {
logger.info('Odds have changed, updating cache');
// Set cache
cache.biden = {
national: odds.national.Biden,
state: odds.state.Biden,
};
cache.trump = {
national: odds.national.Trump,
state: odds.state.Trump,
};
// Send SMS Update
await helpers.sendSMSUpdate(odds);
} else {
logger.info('Odds have not changed');
}
} catch (err) {
logger.error('An error occurred', err);
}
};
// Check simulations every X milliseconds
setInterval(checkOdds, config.intervalTime);
logger.info(`Set interval to check 538 odds every ${config.intervalTime}ms`);