-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
311 lines (272 loc) · 8.11 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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
import createDebug from 'debug'
import {DateTime} from 'luxon'
import {Readable} from 'node:stream'
import Queue from 'queue'
import speedometer from 'speedometer'
import {createRequestCounter as createReqCounter} from './lib/req-counter.js'
const debug = createDebug('hafas-discover-stations')
const minute = 60 * 1000
const createWalkAndDiscoverStations = (hafas) => {
if (!hafas || !hafas.profile) throw new Error('invalid hafas client')
if ('string' !== typeof hafas.profile.timezone) {
throw new Error('hafas.profile.timezone must be a string')
}
if ('string' !== typeof hafas.profile.locale) {
throw new Error('hafas.profile.locale must be a string')
}
if ('function' !== typeof hafas.locations) {
throw new Error('hafas.locations must be a function')
}
if ('function' !== typeof hafas.departures) {
throw new Error('hafas.departures must be a function')
}
if ('function' !== typeof hafas.journeys) {
throw new Error('hafas.journeys must be a function')
}
const walkAndDiscoverStations = (first = null, opt = {}) => {
if (first !== null && 'string' !== typeof first) {
opt = first
first = null
}
opt = {
concurrency: 4,
timeout: 30 * 1000,
parseStationId: id => id,
subStops: true,
linesOfStops: false,
shouldQueryDeparturesAt: (stopId, hops) => true,
...opt,
}
if (!('when' in opt) || opt.when === null) {
// beginning of next week 10 am
opt.when = DateTime.fromMillis(Date.now(), {
zone: hafas.profile.timezone,
locale: hafas.profile.locale
}).startOf('week').plus({weeks: 1, hours: 10}).toJSDate()
}
const out = new Readable({
objectMode: true,
read: () => {}
})
const queue = new Queue({
concurrency: opt.concurrency,
timeout: opt.timeout
})
const visitedStopsAndStations = Object.create(null) // by stop/station ID
const visitedTrips = Object.create(null) // by tripId
// by originId-targetId-when
const visitedJourneys = Object.create(null)
// by sourceID-targetID-duration-lineName
const visitedEdges = Object.create(null)
const stopsAndStationsSpeed = speedometer(30) // 30s window
let stopsAndStationsPerSecond = 0
let nrOfStopsAndStations = 0
let nrOfEdges = 0
// todo: count failed requests
const reqCounter = createReqCounter()
const stats = () => {
out.emit('stats', {
stopsAndStations: nrOfStopsAndStations,
stopsAndStationsPerSecond,
edges: nrOfEdges,
...reqCounter.getStats(),
queuedReqs: queue.length,
eta: (stopsAndStationsPerSecond === 0
? Infinity
: Math.ceil(queue.length / stopsAndStationsPerSecond)
),
})
}
const onStation = (station, hops) => {
const sId = opt.parseStationId(station.id)
if (visitedStopsAndStations[sId]) return;
visitedStopsAndStations[sId] = true
nrOfStopsAndStations++
out.push(station)
stopsAndStationsPerSecond = stopsAndStationsSpeed(1)
if (opt.shouldQueryDeparturesAt(sId, hops + 1)) {
queue.push(queryDepartures(sId, hops + 1))
}
}
const onStop = (stop, hops) => {
if (stop.station) onStation(stop.station, hops)
const sId = opt.parseStationId(stop.id)
if (visitedStopsAndStations[sId]) return;
visitedStopsAndStations[sId] = true
nrOfStopsAndStations++
out.push(stop)
stopsAndStationsPerSecond = stopsAndStationsSpeed(1)
if (opt.shouldQueryDeparturesAt(sId, hops + 1)) {
queue.push(queryDepartures(sId, hops + 1))
}
}
const onStopsAndStations = (stopsAndStations, hops) => {
for (const stopOrStation of stopsAndStations) {
if (stopOrStation.type === 'station') {
onStation(stopOrStation, hops)
} else {
onStop(stopOrStation, hops)
}
}
stats()
}
const onEdge = (source, target, duration, line, hops) => {
const signature = [
opt.parseStationId(source.id),
opt.parseStationId(target.id),
duration, line.name
].join('-')
if (visitedEdges[signature]) return;
visitedEdges[signature] = true
nrOfEdges++
out.emit('edge', {source, target, duration, line, hops})
}
const onLeg = (leg, hops) => {
if (!Array.isArray(leg.stopovers)) return // todo
for (let i = 1; i < leg.stopovers.length; i++) {
const st1 = leg.stopovers[i - 1]
const st2 = leg.stopovers[i]
// todo: planned{Arrival,Departure}
const start = st1.arrival || st1.departure // todo: swap?
const end = st2.arrival || st2.departure
if (!start || !end) continue
const duration = new Date(end) - new Date(start)
onEdge(st1.stop, st2.stop, duration, leg.line, hops)
}
const stops = leg.stopovers.map(st => st.stop)
onStopsAndStations(stops, hops)
}
const queryStopovers = (tripId, lineName, direction, when, originId, hops) => (cb) => {
debug('stopovers', tripId, lineName, direction, 'originId', originId, 'hops', hops)
const t0 = Date.now()
hafas.trip(tripId, lineName || 'foo', {
when,
subStops: !!opt.subStops,
linesOfStops: !!opt.linesOfStops,
})
.then(({trip}) => {
reqCounter.onReqTime(Date.now() - t0)
stats()
onLeg(trip, hops)
cb()
})
.catch((err) => {
if (!err || !err.isHafasError) throw err
debug(tripId, 'using locations() + journeys() as fallback for journeyLeg()')
const t0 = Date.now()
return hafas.locations(direction, {
addresses: false, poi: false,
results: 3,
subStops: !!opt.subStops,
linesOfStops: !!opt.linesOfStops,
})
.then((targets) => {
reqCounter.onReqTime(Date.now() - t0)
stats()
onStopsAndStations(targets, hops)
for (let target of targets) {
const tId = opt.parseStationId(target.id)
const sig = [
originId, tId, Math.round(when / minute)
].join('-')
if (!visitedJourneys[sig]) {
visitedJourneys[sig] = true
queue.unshift(queryJourneys(originId, tId, when, hops))
}
}
cb()
})
})
.catch(cb)
}
const queryDepartures = (id, hops) => (cb) => {
debug('departures', id, 'hops', hops)
const t0 = Date.now()
hafas.departures(id, {
when: opt.when,
duration: 60,
remarks: false,
subStops: !!opt.subStops,
linesOfStops: !!opt.linesOfStops,
})
.then(({departures: deps}) => {
reqCounter.onReqTime(Date.now() - t0)
stats()
onStopsAndStations(deps.map(dep => dep.stop), hops + 1)
for (let dep of deps) {
if (visitedTrips[dep.tripId]) continue
visitedTrips[dep.tripId] = true
const {tripId, direction} = dep
const lName = dep.line && dep.line.name || ''
const when = new Date(dep.when) - 2 * minute
const origId = opt.parseStationId(dep.stop.id)
queue.unshift(queryStopovers(tripId, lName, direction, when, origId, hops + 1))
}
cb()
})
.catch(cb)
}
const queryJourneys = (originId, target, when, hops) => (cb) => {
debug('journeys', originId, target, when, hops)
const t0 = Date.now()
hafas.journeys(originId, target, {
results: 1, startWithWalking: false,
departure: when,
stopovers: true, remarks: false,
subStops: !!opt.subStops,
linesOfStops: !!opt.linesOfStops,
})
.then(({journeys}) => {
reqCounter.onReqTime(Date.now() - t0)
stats()
for (let journey of journeys) {
for (let leg of journey.legs) onLeg(leg, hops)
}
cb()
})
.catch(cb)
}
queue.addEventListener('error', (ev) => {
const {error: err} = ev.detail || {}
debug('queue error', err)
if (err) {
if (err.isHafasError) {
out.emit('hafas-error', err)
} else {
queue.end(err)
}
} else {
queue.end()
}
})
queue.addEventListener('end', (ev) => {
const {error: err} = ev.detail || {}
debug('queue end', 'error?', err)
if (err) out.destroy(err)
else {
setTimeout(() => {
out.push(null) // end the stream
}, 0)
}
})
out.stop = () => {
debug('flushing the queue')
queue.end()
}
const markAsVisited = (stopId) => {
visitedStopsAndStations[stopId] = true
}
out.markAsVisited = markAsVisited
const queueStopId = (stopId) => {
queue.push(queryDepartures(stopId, 0))
if (!queue.running) queue.start()
}
out.queueStopId = queueStopId
if (first) setImmediate(queueStopId, first)
return out
}
return walkAndDiscoverStations
}
export {
createWalkAndDiscoverStations,
}