Created
September 15, 2021 01:10
-
-
Save dfellis/294dd034a6777137115e37dfa8c78be8 to your computer and use it in GitHub Desktop.
Remote Execution Blog Snippets
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const trips = await query(`select * from trips;`); | |
const userTripsInOrder = trips | |
.filter((t) => t.userId === userId) | |
.map((t) => ({ | |
type: t.type, | |
price: t.price, | |
serviceProvider: t.serviceProvider, | |
origin: { | |
lat: t.originLat, | |
lng: t.originLng, | |
time: new Date(t.startTime), | |
}, | |
destination: { | |
lat: t.destLat, | |
lng: t.destLng, | |
time: new Date(t.endTime), | |
}, | |
}) | |
.sort((a, b) => a.origin.time - b.origin.time); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const userTripsInOrder = await query(` | |
select * from trips | |
where trips.userId == $0 | |
order by trips.startTime desc; | |
`, [userId]); | |
const outputUserTripsInOrder = userTripsInOrder.map((t) => ({ | |
type: t.type, | |
price: t.price, | |
serviceProvider, t.serviceProvider, | |
origin: { | |
lat: t.originLat, | |
lng: t.originLng, | |
time: new Date(t.startTime), | |
}, | |
destination: { | |
lat: t.destLat, | |
lng: t.destLng, | |
time: new Date(t.endTime), | |
}, | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const degsToRads = (deg) => Math.PI / 180 * deg; | |
const haversine = (origin, dest) => Math.acos( | |
Math.sin(degsToRads(origin.lat)) * Math.sin(degsToRads(dest.lat)) + | |
Math.cos(degsToRads(origin.lat)) * Math.cos(degsToRads(dest.lat)) * Math.cos(degsToRads(dest.lng - origin.lng)) | |
); | |
const notablePlaces = await query(`select * from notablePlaces`); | |
const relevantPlaces = notablePlaces | |
.map((p) => ({ | |
name: p.name, | |
img: p.img, | |
desc: p.desc, | |
distance: haversine({ | |
lat: p.lat, | |
lng: p.lng, | |
}, { | |
lat: trip.destLat, | |
lng: trip.destLng, | |
}), | |
})) | |
.sort((a, b) => b.distance - a.distance) | |
.slice(0, 4); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const degsToRads = (deg) => Math.PI / 180 * deg; | |
const haversine = (origin, dest) => Math.acos( | |
Math.sin(degsToRads(origin.lat)) * Math.sin(degsToRads(dest.lat)) + | |
Math.cos(degsToRads(origin.lat)) * Math.cos(degsToRads(dest.lat)) * Math.cos(degsToRads(dest.lng - origin.lng)) | |
); | |
let bbox = 0.1; | |
for (; (await query(` | |
select count(*) from notablePlaces | |
where notablePlaces.lat < $0 + $2 | |
and notablePlaces.lat > $0 - $2 | |
and notablePlaces.lng < $1 + $2 | |
and notablePlaces.lng > $1 - $2 | |
`, [trip.destLat, trip.destLng, bbox]))[0].count < 4; bbox *= 2) {} | |
const closeNotablePlaces = await query(` | |
select * from notablePlaces | |
where notablePlaces.lat < $0 + $2 | |
and notablePlaces.lat > $0 - $2 | |
and notablePlaces.lng < $1 + $2 | |
and notablePlaces.lng > $1 - $2 | |
`, [trip.destLat, trip.destLng, bbox]); | |
const relevantPlaces = closeNotablePlaces | |
.map((p) => ({ | |
name: p.name, | |
img: p.img, | |
desc: p.desc, | |
distance: haversine({ | |
lat: p.lat, | |
lng: p.lng, | |
}, { | |
lat: trip.destLat, | |
lng: trip.destLng, | |
}), | |
})) | |
.sort((a, b) => b.distance - a.distance) | |
.slice(0, 4); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment