-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathserver.js
150 lines (127 loc) · 4.83 KB
/
server.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
const express = require('express')
const WebSocket = require('ws')
const generator = require('./model/generate.js')
const { alert } = require('./model/model.js')
const { partition, findLast } = require('./util/utils.js')
const web_service_port = 8080
const web_socket_port = 8090
const app = express()
app.use(express.json())
app.use(function(_, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, User-Agent");
res.header("Access-Control-Allow-Methods", "GET, POST, PATCH");
next();
});
app.use(express.static('static'))
const wss = new WebSocket.Server({ port: web_socket_port, path: '/warnings' })
let alert_id = 1
const create_alerts = predictions => {
const alerts = predictions.filter(generator.alertable).map((prediction, i) => alert(alert_id + i, 3, prediction))
alert_id += alerts.length
return alerts
}
const start_time = new Date()
const data = generator.generate_historic_data(start_time)
let forecast = generator.generate_forecast(start_time)
const alerts = create_alerts(forecast)
let historic_alerts = { [ start_time.getTime() ]: alerts.slice() }
const update_alerts = (forecast, time) => {
const match_alerts = alerts.map((alert, idx) => ({ idx, new_prediction: forecast.find(alert.matches)}))
const { positive: updated_alerts, negative: cancelled_alerts } = partition(t => t.new_prediction && generator.alertable(t.new_prediction))(match_alerts)
const unalerted = forecast.filter(p => !alerts.some(a => a.matches(p)))
cancelled_alerts.forEach(({idx}) => alerts[idx] = alerts[idx].cancelled())
updated_alerts.forEach(({idx, new_prediction}) => alerts[idx] = alerts[idx].updated(new_prediction))
alerts.push(...create_alerts(unalerted))
historic_alerts[time.getTime()] = alerts.slice()
}
const regenerate_forecast = () => {
const time = new Date()
const new_forecast = generator.regenerate_forecast(forecast)(time)
update_alerts(new_forecast, time)
return forecast = new_forecast
}
const warnings = alerts => ({ time: new Date(), warnings: alerts })
app.get('/data', (_, res) => {
res.send(data)
})
app.get('/data/:place', (req, res) => {
res.send(data.filter(({place}) => place === req.params.place))
})
app.post('/data', (req, res) => {
const new_data = [].concat(req.body)
data.push(...new_data)
res.status(201)
res.send(new_data)
})
app.get('/forecast', (_, res) => {
res.send(forecast)
})
app.get('/forecast/:place', (req, res) => {
res.send(forecast.filter(({place}) => place === req.params.place))
})
app.get('/warnings', (_, res) => {
res.send(warnings(alerts.filter(a => a.prediction)))
})
app.get('/warnings/:id', (req, res) => {
const alert = alerts.find(({id}) => id == req.params.id)
if (alert)
res.send(alert)
else {
res.status(404)
res.send()
}
})
app.get('/warnings/since/:time', (req, res) => {
const time = Date.parse(req.params.time)
if (time) {
const alert_time = findLast(t => t <= time)(Object.keys(historic_alerts))
if (alert_time) {
const old_alerts = historic_alerts[alert_time]
res.send(warnings(alerts.filter(a => !old_alerts.some(a.equals))))
} else {
res.send(warnings(alerts.filter(a => a.prediction)))
}
} else {
res.status(400)
res.send()
}
})
const update_frequency_seconds = process.argv[2] || 600
function update_periodically() {
setTimeout(() => {
regenerate_forecast()
update_periodically()
const all_alerts = Object.values(historic_alerts)
const old_alerts = all_alerts[all_alerts.length - 2]
if (old_alerts) {
alerts
.filter(a => !old_alerts.some(a.equals))
.forEach(alert =>
[...wss.clients]
.filter(client => client.readyState === WebSocket.OPEN && client.subscribed)
.forEach( client => client.send(JSON.stringify(alert)) )
)
}
}, update_frequency_seconds * 1000)
}
update_periodically()
wss.on('connection', (ws, req) => {
ws.on('message', message => {
switch(message.toString()) {
case 'subscribe':
if (!ws.subscribed) {
ws.subscribed = true
ws.send(JSON.stringify(warnings(alerts)))
}
break;
case 'unsubscribe':
ws.subscribed = false
break;
default:
console.error(`Incorrect message: '${message}' from ${req.socket.remoteAddress} (${req.socket.remoteFamily})`)
}
})
ws.on('close', () => ws.subscribed = false)
})
app.listen(web_service_port, () => console.log("Server started on", web_service_port, "at", start_time.toString()))