Skip to content

Commit

Permalink
Merge pull request #19 from Elizabeth-Warren/onlylimitedqueriesforhig…
Browse files Browse the repository at this point in the history
…hpriorityandnearby

Limit query for low-priority events
  • Loading branch information
jasonkb authored Jun 21, 2019
2 parents 26aba43 + 4568348 commit 7dc0a03
Showing 1 changed file with 28 additions and 14 deletions.
42 changes: 28 additions & 14 deletions src/models/Event.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,31 +60,45 @@ module.exports = (db) => {
* Get a list of high-priority events that haven't happened yet,
* ordered by how soon they will happen.
*
* Returns at most 'eventLimit' plus the number of high-priority
* events.
*
* @return {Array<Object>}
*/
async function _getUpcomingHighPriorityAndNearbyEvents(originLon, originLat) {
const eventsCursor = await collection.find({
const eventsCursorHighPriority = await collection.find({
startTime: {
$gte : new Date(),
},
highPriority: true,
loc: {
$near: {
$geometry: {
type: 'Point' ,
coordinates: [ originLon, originLat ]
}
}
}
});
coordinates: [ originLon, originLat ],
},
},
},
}).limit(eventLimit);
const highPriorityEvents = await eventsCursorHighPriority.toArray();

const eventsCursorNearby = await collection.find({
startTime: {
$gte : new Date(),
},
highPriority: false,
loc: {
$near: {
$geometry: {
type: 'Point' ,
coordinates: [ originLon, originLat ],
},
},
},
}).limit(eventLimit);
const nearbyEvents = await eventsCursorNearby.toArray();

// Stable sort by priority, so the result is sorted by priority then by geographic proximity.
// We limit to 'eventLimit' after fetching all events because we are re-sorting.
const events = await eventsCursor.toArray();
return events
.map((e, index) => ({e, index}))
.sort((a, b) => (b.e.highPriority - a.e.highPriority) || (a.index - b.index))
.map(({e}) => e)
.slice(0, eventLimit)
return highPriorityEvents.concat(nearbyEvents)
}

const getUpcomingHighPriorityAndNearbyEvents = asyncWrap(_getUpcomingHighPriorityAndNearbyEvents);
Expand Down

0 comments on commit 7dc0a03

Please sign in to comment.