Skip to content

Commit

Permalink
find max nr of radar() results: tweak bbox algorithm ✅
Browse files Browse the repository at this point in the history
follow-up of b06c567
follow-up of 98f042d
related: #12
  • Loading branch information
derhuerst committed Feb 25, 2024
1 parent 4472a0e commit 2758f77
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 6 deletions.
34 changes: 28 additions & 6 deletions lib/find-max-radar-results.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

const debug = require('debug')('hafas-monitor-trips:find-max-radar-results')
const createDebug = require('debug')
const distance = require('@turf/distance').default
const dest = require('@turf/destination').default
const {ok} = require('assert')
Expand All @@ -18,7 +18,10 @@ const CACHE_TTL = 7 * 24 * 60 * 60 // 1 week
// Therefore, we introduce a minimum to make this case less likely.
const MIN_NR_OF_MOVEMENTS = 100

const findMaxRadarResults = async (hafas, initialBbox, onReqTime, onMovement) => {
const debug = createDebug('hafas-monitor-trips:find-max-radar-results')
const debugBbox = createDebug('hafas-monitor-trips:find-max-radar-results:bbox')

const expandingBoundingBoxes = function* (initialBbox) {
// this is accurate enough for our use case
const center = {
type: 'Point',
Expand All @@ -38,20 +41,37 @@ const findMaxRadarResults = async (hafas, initialBbox, onReqTime, onMovement) =>
debug('initial bbox', initialBbox)
debug('center', center.coordinates, 'width', w, 'height', h)

let prevResults = -Infinity, prevPrevResults = -Infinity
for (let it = 0, distFactor = .1; it < MAX_ITERATIONS; it++, distFactor *= 2) {
// this creates wrong bounding boxes when "crossing" the antimeridian or the poles
// todo: find a way to deal with this, but allow *initial* legitimate bounding boxes across them?
for (let it = 0; it < MAX_ITERATIONS; it++) {
const distFactor = .1 * Math.pow(1.5, it)
debugBbox(
'it', it,
'distFactor', distFactor.toFixed(2),
'width', (distFactor * w).toFixed(2),
'height', (distFactor * h).toFixed(2),
)

const _north = dest(center, distFactor * h / 2, 0).geometry.coordinates[1]
const _west = dest(center, distFactor * w / 2, 270).geometry.coordinates[0]
const _south = dest(center, distFactor * h / 2, 180).geometry.coordinates[1]
const _east = dest(center, distFactor * w / 2, 90).geometry.coordinates[0]
const bbox = {
yield {
north: Math.round(_north * 10000) / 10000,
west: Math.round(_west * 10000) / 10000,
south: Math.round(_south * 10000) / 10000,
east: Math.round(_east * 10000) / 10000,
}
}
}

debug('calling radar()', 'iteration', it, 'bbox', bbox)
const findMaxRadarResults = async (hafas, initialBbox, onReqTime, onMovement) => {
debug('initial bbox', initialBbox)

let it = 0
let prevResults = -Infinity, prevPrevResults = -Infinity
for (const bbox of expandingBoundingBoxes(initialBbox)) {
debug('calling radar()', 'iteration', it++, 'bbox', bbox)
const t0 = Date.now()
const movements = await hafas.radar(bbox, {
results: ENOUGH_RESULTS,
Expand Down Expand Up @@ -92,4 +112,6 @@ const cachedFindMaxRadarResults = async (hafas, bbox, redis, onReqTime, onMoveme
return maxResults
}

// todo [breaking]: export object
cachedFindMaxRadarResults.expandingBoundingBoxes = expandingBoundingBoxes
module.exports = cachedFindMaxRadarResults
25 changes: 25 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,33 @@ const createHafas = require('vbb-hafas')
const a = require('assert')
const {Registry} = require('prom-client')
const createMonitor = require('.')
const {expandingBoundingBoxes} = require('./lib/find-max-radar-results')
const fetchTrips = require('./fetch-trips')

a.deepStrictEqual(
Array.from(expandingBoundingBoxes({
north: 54.52,
west: 6.54,
south: 51.29,
east: 6.65,
}))
.map(({north, west, south, east}, i) => [north, west, south, east]),
[
[53.0665,6.5895,52.7435,6.6005],
[53.1473,6.5868,52.6628,6.6032],
[53.2684,6.5826,52.5416,6.6074],
[53.4501,6.5764,52.3599,6.6136],
[53.7226,6.5672,52.0874,6.6228],
[54.1314,6.5532,51.6786,6.6368],
[54.7446,6.5324,51.0654,6.6576],
[55.6644,6.501, 50.1456,6.689 ],
[57.0441,6.454, 48.7659,6.736 ],
[59.1136,6.3836,46.6964,6.8064],
[62.2179,6.2778,43.5921,6.9122],
[66.8744,6.1193,38.9356,7.0707],
],
)

const METRICS = [
'hafas_reqs_total',
'hafas_errors_total',
Expand Down

0 comments on commit 2758f77

Please sign in to comment.