-
Notifications
You must be signed in to change notification settings - Fork 4
Add GET /participants/summary #248
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
Conversation
Sure! Don't use the A mock-up to illustrate what I mean: // router:
} else if (req.method === 'GET' && url === '/participants/summary') {
await getParticipantsSummary(req, res, pgPools.stats)
} // etc.
// implementation
// todo: add types
export const gatParticipantsSummary = async (req, res, pgPool) => {
const { rows } = await pgPool.query(`
SELECT COUNT(DISTINCT participant_id) FROM daily_participants
`)
// TODO: configure caching!
json(res, {
participant_count: rows[0].count
})
} |
stats/lib/platform-routes.js
Outdated
} else if (req.method === 'GET' && url === '/participants/top-earning') { | ||
await respond(pgPools.stats, fetchTopEarningParticipants) | ||
} else if (req.method === 'GET' && url === '/participants/summary') { | ||
json(res, await fetchParticipantsSummary(pgPools.evaluate)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hang on. The SQL query takes more than 400ms. I am not comfortable running that query on every request; we need to cache the result.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
stats/lib/platform-routes.js
Outdated
const summary = await fetchParticipantsSummary(pgPools.evaluate) | ||
res.setHeader('cache-control', `public, max-age=${24 * 3600 /* one day */}`) | ||
json(res, summary) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would you mind extracting these three lines into a new function? I would like to keep handlePlatformRoutes
at a single level of abstraction - match request method+path and call the appropriate route handler.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have refactored dc18234 but don't know where to put it. I also don't see a good way to refactor a generic getStatsWithCaching
function. Can you please advise in more detail how you envision this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new version looks great, thank you!
@bajtos please see my comment above: I don't know a good place to put the new function. Since it's not a data fetcher but a responder, it shouldn't be in the fetchers file |
I see what you mean now. Since the new function is a route handler, I would put it at the bottom of We are already using that pattern in stats/lib/handler.js. |
Like this? This function is so small that I think it makes the main route handler harder to read but I also value the consistency this has with other handlers |
Yeah, I find this new version harder to read too. I was thinking about what I described in #248 (comment)
To me, I like the router to be just a router delegating to other functions, with as little configuration as necessary. Then, the route handler function ( TBH, I think we are hitting diminishing returns here. I am fine with dc18234. I consider b1dd0f2 a bit worse, but I can live with that too. This code is very isolated and should be super easy to change in the future if needed. Unless you are concerned about setting up the right patterns for future contributors, in which case, I prefer the option I described at the top of this comment. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👏🏻
For CheckerNetwork/roadmap#154.
@bajtos currently tests are failing, since any request without from/to query parameters gets redirected to one with. This filter is meaningless in the context of this route, which currently exposes the total seen participant count. Do you have a proposal what to do about this?