Skip to content

Commit dc18234

Browse files
committed
refactor
1 parent 410b9cc commit dc18234

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

stats/lib/platform-routes.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@ import {
66
fetchTopEarningParticipants,
77
fetchParticipantsWithTopMeasurements,
88
fetchDailyStationMeasurementCounts,
9-
fetchParticipantsSummary
9+
respondWithParticipantsSummary
1010
} from './platform-stats-fetchers.js'
11-
import { json } from 'http-responders'
1211

1312
const createRespondWithFetchFn = (pathname, searchParams, res) => (pgPool, fetchFn) => {
1413
return getStatsWithFilterAndCaching(
@@ -39,9 +38,7 @@ export const handlePlatformRoutes = async (req, res, pgPools) => {
3938
} else if (req.method === 'GET' && url === '/participants/top-earning') {
4039
await respond(pgPools.stats, fetchTopEarningParticipants)
4140
} else if (req.method === 'GET' && url === '/participants/summary') {
42-
const summary = await fetchParticipantsSummary(pgPools.evaluate)
43-
res.setHeader('cache-control', `public, max-age=${24 * 3600 /* one day */}`)
44-
json(res, summary)
41+
await respondWithParticipantsSummary(res, pgPools.evaluate)
4542
} else if (req.method === 'GET' && url === '/transfers/daily') {
4643
await respond(pgPools.stats, fetchDailyRewardTransfers)
4744
} else {

stats/lib/platform-stats-fetchers.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import assert from 'http-assert'
22
import { today, yesterday } from './request-helpers.js'
3+
import { json } from 'http-responders'
34

45
/** @typedef {import('@filecoin-station/spark-stats-db').Queryable} Queryable */
56

@@ -116,3 +117,13 @@ export const fetchParticipantsSummary = async (pgPool) => {
116117
participant_count: Number(rows[0].count)
117118
}
118119
}
120+
121+
/**
122+
* @param {import('http').ServerResponse} res
123+
* @param {Queryable} pgPool
124+
*/
125+
export const respondWithParticipantsSummary = async (res, pgPool) => {
126+
const summary = await fetchParticipantsSummary(pgPool)
127+
res.setHeader('cache-control', `public, max-age=${24 * 3600 /* one day */}`)
128+
json(res, summary)
129+
}

0 commit comments

Comments
 (0)