-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
236 lines (200 loc) · 6.51 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
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
const express = require('express')
const helmet = require('helmet')
const is = require('is')
const prometheus = require('prom-client')
const low = require('lowdb')
const FileAsync = require('lowdb/adapters/FileAsync')
const vigicrues = require('./metrics/vigicrues')
const darksky = require('./metrics/darksky')
const app = express()
const adapter = new FileAsync('db.json')
const citiesRef = require('./cities.json')
const stationsRef = require('./stations.json')
const Temperatures = new prometheus.Gauge({
name: 'temperatures',
help: 'Températures',
labelNames: ['city']
})
const Hauteurs = new prometheus.Gauge({
name: 'hauteurs',
help: 'Hauteurs d\'eau',
labelNames: ['station']
})
const Debits = new prometheus.Gauge({
name: 'debits',
help: 'Débits d\'eau',
labelNames: ['station']
})
const makeArray = (query, field) => {
let result = []
if (is.string(query[field])) {
result = [query[field]]
} else if (is.array(query[field])) {
result = query[field]
}
return result
}
const port = process.env.PORT || 5050
low(adapter).then(db => {
db._.mixin({
upsert(collection, objs, ...keys) {
objs.forEach(obj => {
keys = keys || ['time']
for (let i = 0; i < collection.length; i++) {
const el = collection[i]
if (keys.every(key => el[key] === obj[key])) {
collection[i] = obj
return collection
}
}
collection.push(obj)
})
}
})
app.use(helmet())
app.use(express.static('public'))
app.get('/data/cities', (req, res) => {
res.json(citiesRef)
})
app.get('/data/stations', (req, res) => {
res.json(stationsRef.stationsQ)
})
app.get('/data/station/:station', (req, res) => {
res.json(stationsRef.stationsQ.find(elt => req.params.station === elt.station))
})
app.get('/latest/hauteurs', (req, res) => {
const stations = makeArray(req.query, 'stations')
res.json(stations.map(station => db.get('current.hauteurs').find({station}).value()))
})
app.get('/latest/debits', (req, res) => {
const stations = makeArray(req.query, 'stations')
res.json(stations.map(station => {
const historic =
db.get('historic.debits')
.filter(elt => elt.station === station)
.map(elt => {
return {time: elt.time, meas: elt.meas}
}).value()
return Object.assign({}, db.get('current.debits').find({station}).value(), {
historic
})
}))
})
app.get('/metrics/hauteurs', (req, res) => {
const stations = makeArray(req.query, 'stations')
Promise.all(stations.map(station =>
vigicrues.observations(station, 'H')
.then(vig => {
const [last] = vig.Serie.ObssHydro.slice(-1)
const arr = vig.Serie.ObssHydro.map(obs => {
return {station, time: obs.DtObsHydro, meas: obs.ResObsHydro}
})
db.get('historic.hauteurs').upsert(arr, 'station', 'time').write()
if (last) {
db.get('current.hauteurs').upsert([{station, label: vig.Serie.LbStationHydro, time: last.DtObsHydro, meas: last.ResObsHydro}], 'station').write()
Hauteurs.labels(station).set(last.ResObsHydro, last.DtObsHydro)
}
})
))
.then(() => {
res.set('Content-Type', prometheus.register.contentType)
res.end(prometheus.register.getSingleMetricAsString('hauteurs'))
})
.catch(err => {
console.error(err)
res.status(500).send(`${err}`)
})
})
app.get('/metrics/debits', (req, res) => {
const stations = makeArray(req.query, 'stations')
Promise.all(stations.map(station =>
vigicrues.observations(station, 'Q')
.then(vig => {
const [last] = vig.Serie.ObssHydro.slice(-1)
const arr = vig.Serie.ObssHydro.map(obs => {
return {station, time: obs.DtObsHydro, meas: obs.ResObsHydro}
})
db.get('historic.debits').upsert(arr, 'station', 'time').write()
if (last) {
db.get('current.debits').upsert([{station, label: vig.Serie.LbStationHydro, time: last.DtObsHydro, meas: last.ResObsHydro}], 'station').write()
Debits.labels(station).set(last.ResObsHydro, last.DtObsHydro)
}
})
))
.then(() => {
res.set('Content-Type', prometheus.register.contentType)
res.end(prometheus.register.getSingleMetricAsString('debits'))
})
.catch(err => {
console.error(err)
res.status(500).send(`${err}`)
})
})
app.get('/latest/temperatures', (req, res) => {
const cities = makeArray(req.query, 'cities')
res.json(cities.map(city => db.get('current.temperatures').find({city}).value()))
})
app.get('/metrics/temperatures', (req, res) => {
const cities = makeArray(req.query, 'cities')
Promise.all(cities.map(city =>
darksky.meteo(city)
.then(dark => {
const curr = dark.currently
Temperatures.labels(city).set(curr.temperature, curr.time * 1000)
const hourly = dark.hourly
db.get('current.temperatures').upsert([{
city,
time: curr.time * 1000,
summary: curr.summary,
icon: curr.icon,
meas: {
temperature: curr.temperature,
humidity: curr.humidity,
pressure: curr.pressure,
windSpeed: curr.windSpeed
}
}], 'city').write()
const arr = hourly.data.map(meas => {
return {
city,
time: meas.time * 1000,
summary: meas.summary,
icon: meas.icon,
meas: {
temperature: meas.temperature,
humidity: meas.humidity,
pressure: meas.pressure,
windSpeed: meas.windSpeed
}
}
})
db.get('historic.temperatures').upsert(arr, 'city', 'time').write()
})
))
.then(() => {
res.set('Content-Type', prometheus.register.contentType)
res.end(prometheus.register.getSingleMetricAsString('temperatures'))
})
.catch(err => {
console.error(err)
res.status(500).send(`${err}`)
})
})
app.get('/:city/:station', (req, res) => {
const query = req.query
const oldQueryParams = Object.keys(query).map(key => `${key}=${encodeURIComponent(query[key])}`).join('&')
res.redirect(`/?city=${req.params.city}&station=${req.params.station}&${oldQueryParams}`)
})
return db.defaults({
historic: {
hauteurs: [],
debits: [],
temperatures: []
},
current: {
hauteurs: [],
debits: [],
temperatures: []
}
}).write()
}).then(() => app.listen(port))