Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add /events route: monitor events in a bounding box #8

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/vbb.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const config = {
docsLink: 'http://example.org/docs',
logging: true,
aboutPage: true,
events: true,
healthCheck: async () => {
const stop = await hafas.stop('900000100001')
return !!stop
Expand Down
5 changes: 4 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const defaultConfig = {
aboutPage: true,
logging: false,
healthCheck: null,
events: false,
addHafasOpts: () => {}
}

Expand Down Expand Up @@ -51,7 +52,9 @@ const createApi = (hafas, config, attachMiddleware) => {
if ('docsLink' in config) assertNonEmptyString(config, 'docsLink')

const api = express()
api.locals.logger = pino()
api.locals.logger = pino({
level: process.env.LOGGING_LEVEL || 'info'
})

if (config.cors) {
const cors = createCors()
Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
"date-fns": "^2.12.0",
"express": "^4.16.2",
"express-pino-logger": "^4.0.0",
"hafas-monitor-trips": "^3.0.0",
"hafas-monitor-trips-server": "^1.0.0",
"hsts": "^2.1.0",
"http-link-header": "^1.0.2",
"lodash": "^4.17.15",
Expand All @@ -43,6 +45,7 @@
"parse-human-relative-time": "^2.0.2",
"pino": "^5.11.3",
"shorthash": "0.0.2",
"ssestream": "^1.0.1",
"stringify-entities": "^3.0.0"
},
"devDependencies": {
Expand Down
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ key | description | mandatory? | default value
`aboutPage` | Enable the about page on `GET /`? | ✗ | `true`
`description` | Used for the about page. | ✗ | –
`docsLink` | Used for the about page. | ✗ | –
`events` | Enable the `/events` route. | ✗ | `false`
`addHafasOpts` | Computes additional `hafas-client` opts. `(opt, hafasClientMethod, httpReq) => additionaOpts` | ✗ | –

*Pro Tip:* Use [`hafas-client-health-check`](https://github.com/public-transport/hafas-client-health-check) for `config.healthCheck`.
Expand Down
106 changes: 106 additions & 0 deletions routes/events.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
'use strict'

const createServer = require('hafas-monitor-trips-server')
const createMonitor = require('hafas-monitor-trips')
const SSE = require('ssestream').default
const throttle = require('lodash/throttle')
const {parseNumber} = require('../lib/parse')

const INTERVAL = parseInt(process.env.MONITOR_INTERVAL || 60) * 1000
const EVENT_STREAM = 'text/event-stream'

const EVENTS = [
'trip', 'new-trip', 'trip-obsolete',
'stopover',
'position',
// todo: stats?
]

const err400 = (msg) => {
const err = new Error(msg)
err.statusCode = 400
return err
}

const sseWriter = (req, res, evNames) => {
const sse = new SSE(req)
sse.pipe(res)

const createHandler = (evName) => {
const sendEvent = (data) => {
sse.write({event: evName, data})
}
return [evName, sendEvent]
}
return [
...evNames.map(createHandler),
['error', (err) => {
sse.write({event: 'error', data: err.message || (err + '')})
}]
]
}

const createEventsRoute = (hafas, config) => {
const monitor = createServer(hafas, {
createMonitor: (hafas, bbox) => {
return createMonitor(hafas, bbox, INTERVAL)
}
})

const events = (req, res, next) => {
const {logger} = req.app.locals

const bbox = {}
for (const param of ['north', 'west', 'south', 'east']) {
if (param in req.query) {
bbox[param] = parseNumber(param, req.query[param])
} else {
return next(err400(`missing ${param} query parameter`))
}
}

const events = EVENTS.filter(evName => req.query[evName] === 'true')
if (events.length === 0) return next(err400('0 events selected'))

if (req.accepts(EVENT_STREAM) !== EVENT_STREAM) {
return next(err400(`only ${EVENT_STREAM}`))
}

const handlers = sseWriter(req, res, events)
const sub = monitor.subscribe(bbox)
handlers
.filter(([evName]) => evName !== 'error')
.forEach(([evName, handler]) => {
sub.on(evName, (data) => {
logger.debug({event: evName, data, bbox})
handler(data)
})
})

logger.info({bbox}, 'starting monitor')
const logInfos = throttle((stats) => {
logger.info({...stats, bbox})
}, 10 * 1000)
sub.on('stats', logInfos)
res.once('close', () => {
logger.info({bbox}, 'stopping monitor')
sub.removeListener('stats', logInfos)
sub.destroy()
})

const onError = handlers.find(([evName]) => evName === 'error')[1]
sub.on('error', (err) => {
logger.error({bbox}, err)
onError(err)
})
}

events.cache = false
events.queryParameters = [
...EVENTS,
'north', 'west', 'south', 'east',
]
return events
}

module.exports = createEventsRoute
4 changes: 4 additions & 0 deletions routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ const getRoutes = (hafas, config) => {
routes['/trips/:id'] = trip(hafas, config)
}
routes['/locations'] = locations(hafas, config)
if (config.events) {
const events = require('./events')
routes['/events'] = events(hafas, config)
}
if (hafas.radar) {
const radar = require('./radar')
routes['/radar'] = radar(hafas, config)
Expand Down