-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
59 lines (52 loc) · 2.01 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
import {createCollectDeps} from 'hafas-collect-departures-at'
import {DateTime} from 'luxon'
import maxBy from 'lodash/maxBy.js'
import round from 'lodash/round.js'
import findDepsDurLimit from 'hafas-find-departures-duration-limit'
// Because this estimation only takes a single day into account, it is inaccurate.
// todo: improve it, e.g. using different days of the week or number of lines
const createEstimateStationWeight = (client, weights) => {
// todo: validate args
const collectDeps = createCollectDeps(client.departures)
const estimate = async (id, maxIterations = Infinity) => {
const startOfWeek = DateTime.fromMillis(Date.now(), {
zone: client.profile.timezone,
locale: client.profile.locale
}).startOf('week')
// next Monday 4 am
const start = startOfWeek.plus({weeks: 1, hours: 4}).valueOf()
// next Tuesday 4 am
const end = startOfWeek.plus({weeks: 1, days: 1, hours: 4}).valueOf()
let weight = 0
const onDep = (dep) => {
if (new Date(dep.when) > end || !dep.line || !dep.line.product) return
const p = dep.line.product
if ('number' === typeof weights[p]) weight += weights[p]
}
// Some HAFAS API do not support querying departures for more than
// ~1 day at once. Therefore, we split the time period into sections.
// todo: put this into hafas-client, see public-transport/hafas-client#14
const dur = await findDepsDurLimit(client, id)
const depsAt = collectDeps(id, start)
const iterator = depsAt[Symbol.asyncIterator]()
let iterations = 0
// eslint-disable-next-line no-constant-condition
while (true) {
iterations++
const deps = (await iterator.next(dur)).value
for (let dep of deps) onDep(dep)
const lastDep = maxBy(deps, dep => Date.parse(dep.when))
if (lastDep && Date.parse(lastDep.when) >= end) break
if (iterations > maxIterations) break
}
if (weight > 0) {
const decimals = Math.floor(5 - Math.log10(weight))
return round(weight, decimals)
}
return weight
}
return estimate
}
export {
createEstimateStationWeight as createEstimate,
}