Skip to content
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

feat: request date keyword handling #174

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions stats/lib/platform-stats-fetchers.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import assert from 'http-assert'
import { getDailyDistinctCount, getMonthlyDistinctCount, today } from './request-helpers.js'
import {
getDailyDistinctCount,
getMonthlyDistinctCount,
today,
yesterday
} from './request-helpers.js'

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

Expand Down Expand Up @@ -50,8 +55,7 @@ export const fetchDailyStationAcceptedMeasurementCount = async (pgPool, filter)
*/
export const fetchParticipantsWithTopMeasurements = async (pgPool, filter) => {
assert(filter.to === filter.from, 400, 'Multi-day queries are not supported for this endpoint')
const yesterdayUTC = new Date(Date.now() - 24 * 60 * 60 * 1000).toISOString().split('T')[0]
assert(filter.to === yesterdayUTC, 400, 'filter.to must be set to yesterday, other values are not supported yet')
assert(filter.to === yesterday(), 400, 'filter.to must be set to yesterday, other values are not supported yet')
// Ignore the filter for this query
// Get the top measurement stations from the Materialized View
return (await pgPool.query('SELECT * FROM top_measurement_participants_yesterday_mv')).rows
Expand Down
19 changes: 19 additions & 0 deletions stats/lib/request-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import pg from 'pg'
export const getDayAsISOString = (d) => d.toISOString().split('T')[0]

export const today = () => getDayAsISOString(new Date())
export const yesterday = () => getDayAsISOString(new Date(Date.now() - 24 * 60 * 60 * 1000))

/** @typedef {import('@filecoin-station/spark-stats-db').PgPools} PgPools */
/**
Expand All @@ -23,6 +24,9 @@ export const getStatsWithFilterAndCaching = async (pathname, pathParams, searchP
const filter = Object.fromEntries(searchParams)
let shouldRedirect = false

filter.from = handleDateKeyword(filter.from)
filter.to = handleDateKeyword(filter.to)

// Provide default values for "from" and "to" when not specified

if (!filter.to) {
Expand Down Expand Up @@ -96,6 +100,21 @@ const setCacheControlForStatsResponse = (res, filter) => {
}
}

/**
* @param {string} date
* @returns {string}
*/
const handleDateKeyword = (date) => {
switch (date) {
case 'today':
return today()
case 'yesterday':
return yesterday()
default:
return date
}
}

/**
* @param {object} args
* @param {import('@filecoin-station/spark-stats-db').Queryable} args.pgPool
Expand Down
29 changes: 8 additions & 21 deletions stats/test/platform-routes.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { getPgPools } from '@filecoin-station/spark-stats-db'

import { assertResponseStatus, getPort } from './test-helpers.js'
import { createHandler } from '../lib/handler.js'
import { getDayAsISOString, today } from '../lib/request-helpers.js'
import { getDayAsISOString, today, yesterday } from '../lib/request-helpers.js'

const STATION_STATS = { stationId: 'station1', participantAddress: 'f1abcdef', inetGroup: 'group1' }

Expand Down Expand Up @@ -161,28 +161,21 @@ describe('Platform Routes HTTP request handler', () => {

describe('GET /participants/top-measurements', () => {
it('returns top measurement stations for the given date', async () => {
const today = new Date()
const yesterday = new Date()
yesterday.setDate(today.getDate() - 1)

const todayUTC = today.toISOString().split('T')[0]
const yesterdayUTC = yesterday.toISOString().split('T')[0]

await givenDailyStationMetrics(pgPools.evaluate, yesterdayUTC, [
await givenDailyStationMetrics(pgPools.evaluate, yesterday(), [
{ ...STATION_STATS, stationId: 's3', participantAddress: 'f1ghijkl', acceptedMeasurementCount: 50 },
{ ...STATION_STATS, acceptedMeasurementCount: 20 },
{ ...STATION_STATS, stationId: 's2', acceptedMeasurementCount: 30 },
{ ...STATION_STATS, stationId: 's2', inetGroup: 'group2', acceptedMeasurementCount: 40 }
])
await givenDailyStationMetrics(pgPools.evaluate, todayUTC, [
await givenDailyStationMetrics(pgPools.evaluate, today(), [
{ ...STATION_STATS, acceptedMeasurementCount: 10 }
])

await pgPools.evaluate.query('REFRESH MATERIALIZED VIEW top_measurement_participants_yesterday_mv')

const res = await fetch(
new URL(
`/participants/top-measurements?from=${yesterdayUTC}&to=${yesterdayUTC}`,
'/participants/top-measurements?from=yesterday&to=yesterday',
baseUrl
), {
redirect: 'manual'
Expand Down Expand Up @@ -251,23 +244,17 @@ describe('Platform Routes HTTP request handler', () => {
})

describe('GET /participants/top-earning', () => {
const yesterdayDate = new Date()
yesterdayDate.setDate(yesterdayDate.getDate() - 1)
const yesterday = getDayAsISOString(yesterdayDate)
console.log('yesterday', yesterday)

const oneWeekAgoDate = new Date()
oneWeekAgoDate.setDate(oneWeekAgoDate.getDate() - 7)
const oneWeekAgo = getDayAsISOString(oneWeekAgoDate)
console.log('oneWeekAgo', oneWeekAgo)

const setupScheduledRewardsData = async () => {
await pgPools.stats.query(`
INSERT INTO daily_scheduled_rewards (day, participant_address, scheduled_rewards)
VALUES
('${yesterday}', 'address1', 10),
('${yesterday}', 'address2', 20),
('${yesterday}', 'address3', 30),
('${yesterday()}', 'address1', 10),
('${yesterday()}', 'address2', 20),
('${yesterday()}', 'address3', 30),
('${today()}', 'address1', 15),
('${today()}', 'address2', 25),
('${today()}', 'address3', 35)
Expand Down Expand Up @@ -339,7 +326,7 @@ describe('Platform Routes HTTP request handler', () => {
it('returns 400 if the date range end is not today', async () => {
const res = await fetch(
new URL(
`/participants/top-earning?from=${oneWeekAgo}&to=${yesterday}`,
`/participants/top-earning?from=${oneWeekAgo}&to=yesterday`,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like to=yesterday 👏🏻

baseUrl
), {
redirect: 'manual'
Expand Down
Loading