Skip to content

Commit

Permalink
Fix: Migrate to dedicated station count endpoints (CheckerNetwork#93)
Browse files Browse the repository at this point in the history
* Migrate to dedicated station count endpoints

* Improved local helper function SQL security

* Renamed request helper methods
  • Loading branch information
PatrickNercessian committed May 12, 2024
1 parent 6cec80e commit 5827fef
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 6 deletions.
15 changes: 15 additions & 0 deletions lib/platform-stats-fetchers.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,18 @@ export const fetchMonthlyStationCount = async (pgPool, filter) => {
filter
})
}

/**
* @param {import('pg').Pool} pgPool
* @param {import('./typings').Filter} filter
*/
export const fetchDailyStationAcceptedMeasurementCount = async (pgPool, filter) => {
const { rows } = await pgPool.query(`
SELECT day::TEXT, SUM(accepted_measurement_count) as accepted_measurement_count
FROM daily_stations
WHERE day >= $1 AND day <= $2
GROUP BY day
ORDER BY day
`, [filter.from, filter.to])
return rows
}
58 changes: 52 additions & 6 deletions test/platform-routes.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,27 @@ describe('Platform Routes HTTP request handler', () => {
describe('GET /stations/monthly', () => {
it('returns monthly station metrics for the given date range ignoring the day number', async () => {
// before the date range
await givenDailyStationMetrics(pgPool, '2023-12-31', ['station1'])
await givenDailyStationMetrics(pgPool, '2023-12-31', [
{ station_id: 'station1', accepted_measurement_count: 1 }
])
// in the date range
await givenDailyStationMetrics(pgPool, '2024-01-10', ['station1'])
await givenDailyStationMetrics(pgPool, '2024-01-11', ['station2'])
await givenDailyStationMetrics(pgPool, '2024-01-12', ['station2', 'station3'])
await givenDailyStationMetrics(pgPool, '2024-02-13', ['station1'])
await givenDailyStationMetrics(pgPool, '2024-01-10', [
{ station_id: 'station1', accepted_measurement_count: 1 }
])
await givenDailyStationMetrics(pgPool, '2024-01-11', [
{ station_id: 'station2', accepted_measurement_count: 1 }
])
await givenDailyStationMetrics(pgPool, '2024-01-12', [
{ station_id: 'station2', accepted_measurement_count: 2 },
{ station_id: 'station3', accepted_measurement_count: 1 }
])
await givenDailyStationMetrics(pgPool, '2024-02-13', [
{ station_id: 'station1', accepted_measurement_count: 1 }
])
// after the date range
await givenDailyStationMetrics(pgPool, '2024-03-01', ['station1'])
await givenDailyStationMetrics(pgPool, '2024-03-01', [
{ station_id: 'station1', accepted_measurement_count: 1 }
])

const res = await fetch(
new URL(
Expand All @@ -107,6 +120,39 @@ describe('Platform Routes HTTP request handler', () => {
])
})
})

describe('GET /measurements/daily', () => {
it('returns daily total accepted measurement count for the given date range', async () => {
await givenDailyStationMetrics(pgPool, '2024-01-10', [
{ station_id: 'station1', accepted_measurement_count: 1 }
])
await givenDailyStationMetrics(pgPool, '2024-01-11', [
{ station_id: 'station2', accepted_measurement_count: 1 }
])
await givenDailyStationMetrics(pgPool, '2024-01-12', [
{ station_id: 'station2', accepted_measurement_count: 2 },
{ station_id: 'station3', accepted_measurement_count: 1 }
])
await givenDailyStationMetrics(pgPool, '2024-01-13', [
{ station_id: 'station1', accepted_measurement_count: 1 }
])

const res = await fetch(
new URL(
'/measurements/daily?from=2024-01-11&to=2024-01-12',
baseUrl
), {
redirect: 'manual'
}
)
await assertResponseStatus(res, 200)
const metrics = await res.json()
assert.deepStrictEqual(metrics, [
{ day: '2024-01-11', accepted_measurement_count: '1' },
{ day: '2024-01-12', accepted_measurement_count: '3' }
])
})
})
})

const givenDailyStationMetrics = async (pgPool, day, stationStats) => {
Expand Down

0 comments on commit 5827fef

Please sign in to comment.