Skip to content

Commit 556c793

Browse files
committed
fix: remove debugging info
1 parent 267afa4 commit 556c793

File tree

7 files changed

+50
-82
lines changed

7 files changed

+50
-82
lines changed

packages/simulator/lib/models/citizen.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ class Citizen {
115115
case 'lunch':
116116
return of(this.workplace.position).pipe(
117117
mergeMap((position) =>
118-
pelias.search('restaurang', position, 'venue')
118+
pelias.searchOne('restaurang', position, 'venue')
119119
),
120120
retryWhen((errors) => errors.pipe(delay(1000), take(3))), // retry 3 times - all lunch searches happens at the same time
121121
filter((position) => position != null),

packages/simulator/lib/osrm.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ module.exports = {
2525
[from.lon, from.lat],
2626
[to.lon, to.lat],
2727
].join(';')
28-
process.stdout.write('r')
2928
return (
3029
fetch(
3130
`${osrmUrl}/route/v1/driving/${coordinates}?steps=true&alternatives=false&overview=full&annotations=true`

packages/simulator/lib/pelias.js

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ const peliasUrl =
55
process.env.PELIAS_URL || 'https://pelias.predictivemovement.se'
66

77
info('Pelias URL', peliasUrl)
8-
module.exports = {
9-
nearest(position, layers = 'address,venue') {
8+
9+
const nearest = (position, layers = 'address,venue') => {
1010
const { lon, lat } = position
1111

1212
const url = `${peliasUrl}/v1/reverse?point.lat=${lat}&point.lon=${lon}&size=1&layers=${layers}`
@@ -46,43 +46,44 @@ module.exports = {
4646
})
4747

4848
return promise
49-
},
50-
search(name, near = null, layers = 'address,venue') {
49+
}
50+
const search = (name, near = null, layers = 'address,venue', size = 1000) => {
51+
const encodedName = encodeURIComponent(name)
5152
const focus = near
52-
? `&focus.point.lat=${encodeURIComponent(
53-
near.lat
54-
)}&focus.point.lon=${encodeURIComponent(
55-
near.lon
56-
)}}&layers=${encodeURIComponent(layers)}`
53+
? `&focus.point.lat=${near.lat}&focus.point.lon=${near.lon}}&layers=${layers}`
5754
: ''
58-
const url = `${peliasUrl}/v1/search?text=${encodeURIComponent(
59-
name
60-
)}${focus}&size=1`
61-
debug('Pelias -> Search', url)
55+
const url = `${peliasUrl}/v1/search?text=${encodedName}${focus}&size=${size}`
6256
process.stdout.write('p')
6357
return fetch(url)
6458
.then((response) => {
6559
if (!response.ok) throw 'pelias error: ' + response.statusText
6660
return response.json()
6761
})
68-
.then((p) => {
69-
process.stdout.write('pa')
70-
debug('Pelias -> Search', p)
71-
72-
return p.features[0]?.geometry?.coordinates?.length
73-
? p
74-
: Promise.reject('No coordinates found')
75-
})
76-
.then(({ features: [{ geometry, properties } = {}] = [] }) => ({
77-
...properties,
78-
position: new Position({
79-
lon: geometry.coordinates[0],
80-
lat: geometry.coordinates[1],
81-
}),
82-
}))
62+
.then((results) =>
63+
results.features
64+
.map(({ geometry, properties } = {}) => ({
65+
...properties,
66+
position: new Position({
67+
lon: geometry.coordinates[0],
68+
lat: geometry.coordinates[1],
69+
}),
70+
}))
71+
.filter((p) => p.position.isValid())
72+
)
8373
.catch((e) => {
8474
const peliasError = new Error().stack
8575
error(`Error in pelias search\n${peliasError}\n${e}\n\n`)
8676
})
8777
},
88-
}
78+
79+
const searchOne = async (name, near = null, layers = 'address,venue') => {
80+
const results = await pelias.search(name, near, layers, 1)
81+
return results[0]
82+
}
83+
84+
85+
module.exports = {
86+
nearest,
87+
search,
88+
searchOne,
89+
}

packages/simulator/simulator/citizens.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ const getCitizensInSquare = (
1515
mergeAll()
1616
)
1717
return zip([
18-
addresses,
18+
addresses.pipe(take(nrOfCitizens)),
1919
randomNames.pipe(take(nrOfCitizens)),
20-
workplaces,
20+
workplaces.pipe(take(nrOfCitizens)),
2121
]).pipe(
2222
map(([home, name, workplace]) => {
2323
return (

packages/simulator/streams/kommuner.js

Lines changed: 13 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@ const {
77
filter,
88
reduce,
99
mergeMap,
10-
tap,
1110
mergeAll,
11+
defaultIfEmpty,
12+
toArray,
13+
take,
1214
repeat,
1315
} = require('rxjs/operators')
1416
const Kommun = require('../lib/kommun')
@@ -27,6 +29,7 @@ const Position = require('../lib/models/position')
2729
const { getAddressesInArea } = require('../simulator/address')
2830
const { includedMunicipalities } = require('../config')
2931
const { info, debug } = require('../lib/log')
32+
const assert = require('assert')
3033

3134
const bookings = {
3235
hm: require('../streams/orders/hm.js'),
@@ -67,47 +70,14 @@ function getMeasureStations(kommunName) {
6770
)
6871
}
6972

70-
async function centerPoint(namn, retries = 0) {
71-
try {
72-
return await Pelias.search(namn).then((res) => res.position)
73-
} catch (err) {
74-
if (retries < 3) {
75-
info(
76-
"Couldn't find center point for",
77-
namn,
78-
`retrying ${retries + 1}/3...`
79-
)
80-
return centerPoint(namn, retries + 1)
81-
}
82-
console.error('Could not find center point for', namn)
83-
return { lat: 0, lon: 0 }
84-
}
85-
}
86-
87-
function getWorkplaces(commercialAreas) {
88-
return commercialAreas.pipe(
89-
mergeMap(async (commercialArea) => {
90-
const {
91-
X_koord: x,
92-
Y_koord: y,
93-
AREA_HA: area,
94-
ARBST_DETH: nrOfWorkplaces,
95-
} = commercialArea.properties
96-
const position = new Position(
97-
convertPosition(coords.toLatLng(y.toString(), x.toString()))
98-
)
99-
const adresses = await getAddressesInArea(position, area, nrOfWorkplaces)
100-
return adresses.map((a) => ({ ...a, position: new Position(a.position) }))
101-
}, 1),
102-
mergeAll(),
103-
shareReplay()
104-
)
73+
async function getWorkplaces(position, nrOfWorkplaces = 100) {
74+
const area = 10000
75+
const adresses = await getAddressesInArea(position, area, nrOfWorkplaces)
76+
return adresses.map((a) => ({ ...a, position: new Position(a.position) }))
10577
}
10678

10779
// function read() {
10880
function read({ fleets }) {
109-
const workplaces = getWorkplaces(commercialAreas)
110-
11181
return from(data).pipe(
11282
filter(({ namn }) =>
11383
includedMunicipalities.some((name) => namn.startsWith(name))
@@ -133,15 +103,13 @@ function read({ fleets }) {
133103
}) => {
134104
const squares = getPopulationSquares({ geometry })
135105
const commercialAreas = getCommercialAreas(kod)
136-
const center = await centerPoint(name)
137-
const nearbyWorkplaces = workplaces.pipe(
138-
// TODO: calculate based on a normal distribution and size of the municipality?
139-
// or other data from SCB?
140-
filter(
141-
(workplace) => workplace?.position?.distanceTo(center) < 100_000
142-
),
106+
const center = await Pelias.searchOne(name).then((res) => res.position)
107+
const nearbyWorkplaces = from(getWorkplaces(center)).pipe(
108+
mergeAll(),
109+
take(100),
143110
repeat()
144111
)
112+
145113
const citizens = squares.pipe(
146114
mergeMap(
147115
(square) => getCitizensInSquare(square, nearbyWorkplaces, name),

packages/simulator/streams/orders/hm.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const {
1414
const moment = require('moment')
1515
const { readCsv } = require('../../adapters/csv')
1616
const { default: fetch } = require('node-fetch')
17-
const { search } = require('../../lib/pelias')
17+
const { searchOne } = require('../../lib/pelias')
1818
const Position = require('../../lib/models/position')
1919
const Booking = require('../../lib/models/booking')
2020
const { error } = require('../../lib/log')
@@ -99,7 +99,7 @@ function read() {
9999
pickup = distributionCenters[i % 4] // TODO: Improve handling of origins outside of Sweden.
100100
}
101101

102-
return search(distributionCenters[i % 4]).then(({ name, position }) =>
102+
return searchOne(distributionCenters[i % 4]).then(({ name, position }) =>
103103
rows.map((row) => ({ pickup: { name, position }, ...row }))
104104
)
105105
}, 1),

packages/simulator/streams/orders/ikea.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const {
1313
const moment = require('moment')
1414
const { readCsv } = require('../../adapters/csv')
1515
const { default: fetch } = require('node-fetch')
16-
const { search } = require('../../lib/pelias')
16+
const { searchOne } = require('../../lib/pelias')
1717
const Position = require('../../lib/models/position')
1818
const Booking = require('../../lib/models/booking')
1919
const { error } = require('../../lib/log')
@@ -136,7 +136,7 @@ function read() {
136136
'Strandbadsvägen 7, Helsingborg', // TNT.
137137
]
138138

139-
return search(distributionCenters[i % 4]).then(({ name, position }) =>
139+
return searchOne(distributionCenters[i % 4]).then(({ name, position }) =>
140140
rows.map((row) => ({ pickup: { name, position }, ...row }))
141141
)
142142
}, 1),

0 commit comments

Comments
 (0)