-
Notifications
You must be signed in to change notification settings - Fork 3
/
compute-service-breaks.js
52 lines (43 loc) · 1.16 KB
/
compute-service-breaks.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
'use strict'
// const {gte, lte} = require('sorted-array-functions')
const inMemoryStore = require('./lib/in-memory-store')
const computeServiceBreaks = async function* (connections, opt = {}) {
const {
createStore,
minLength,
} = {
createStore: inMemoryStore,
// todo: filters, threshold option, find threshold automatically
minLength: 10 * 60, // in seconds
...opt,
}
// fromStopId-toStopId => [dep, routeId, serviceId]
const prevDeps = createStore()
// todo: handle "breaks" at the beginning of the time frame
// todo: handle "breaks" at the end of the time frame
for (let i = 0; i < connections.length; i++) {
const {
routeId, serviceId,
fromStop, toStop, departure,
} = connections[i]
const sig = fromStop + '-' + toStop
const prevDep = await prevDeps.get(sig)
if (prevDep && (departure - prevDep[0]) >= minLength) {
// emit service break
yield {
fromStop, toStop,
start: prevDep[0],
end: departure,
duration: departure - prevDep[0],
routeId: prevDep[1],
serviceId: prevDep[2],
}
}
await prevDeps.set(sig, [
departure,
routeId,
serviceId,
])
}
}
module.exports = computeServiceBreaks