Skip to content

Commit

Permalink
feat: GET /deals/daily (#146)
Browse files Browse the repository at this point in the history
Signed-off-by: Miroslav Bajtoš <[email protected]>
Co-authored-by: Julian Gruber <[email protected]>
  • Loading branch information
bajtos and juliangruber authored Jun 13, 2024
1 parent 648189b commit 24d4ec9
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ Base URL: http://stats.filspark.com/

http://stats.filspark.com/measurements/daily

- `GET /deals/daily?from=2024-01-01&to=2024-01-31`

http://stats.filspark.com/deals/daily

## Development

### Database
Expand Down
2 changes: 2 additions & 0 deletions stats/lib/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as Sentry from '@sentry/node'
import { getStatsWithFilterAndCaching } from './request-helpers.js'

import {
fetchDailyDealStats,
fetchDailyParticipants,
fetchMinersRSRSummary,
fetchMonthlyParticipants,
Expand Down Expand Up @@ -46,6 +47,7 @@ const handler = async (req, res, pgPools) => {
const segs = pathname.split('/').filter(Boolean)

const fetchFunctionMap = {
'deals/daily': fetchDailyDealStats,
'retrieval-success-rate': fetchRetrievalSuccessRate,
'participants/daily': fetchDailyParticipants,
'participants/monthly': fetchMonthlyParticipants,
Expand Down
19 changes: 19 additions & 0 deletions stats/lib/stats-fetchers.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,25 @@ export const fetchRetrievalSuccessRate = async (pgPools, filter) => {
return stats
}

/**
* @param {import('@filecoin-station/spark-stats-db').pgPools} pgPools
* @param {import('./typings').DateRangeFilter} filter
*/
export const fetchDailyDealStats = async (pgPools, filter) => {
// Fetch the "day" (DATE) as a string (TEXT) to prevent node-postgres from converting it into
// a JavaScript Date with a timezone, as that could change the date one day forward or back.
const { rows } = await pgPools.evaluate.query(`
SELECT day::text, total, indexed, retrievable
FROM daily_deals
WHERE day >= $1 AND day <= $2
ORDER BY day
`, [
filter.from,
filter.to
])
return rows
}

export const fetchDailyParticipants = async (pgPools, filter) => {
return await getDailyDistinctCount({
pgPool: pgPools.evaluate,
Expand Down
32 changes: 32 additions & 0 deletions stats/test/handler.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ describe('HTTP request handler', () => {
beforeEach(async () => {
await pgPool.query('DELETE FROM retrieval_stats')
await pgPool.query('DELETE FROM daily_participants')
await pgPool.query('DELETE FROM daily_deals')
})

it('returns 200 for GET /', async () => {
Expand Down Expand Up @@ -381,6 +382,30 @@ describe('HTTP request handler', () => {
])
})
})

describe('GET /deals/daily', () => {
it('returns daily deal stats for the given date range', async () => {
await givenDailyDealStats(pgPool, { day: '2024-01-10', total: 10, indexed: 5, retrievable: 1 })
await givenDailyDealStats(pgPool, { day: '2024-01-11', total: 20, indexed: 6, retrievable: 2 })
await givenDailyDealStats(pgPool, { day: '2024-01-12', total: 30, indexed: 7, retrievable: 3 })
await givenDailyDealStats(pgPool, { day: '2024-01-13', total: 40, indexed: 8, retrievable: 4 })

const res = await fetch(
new URL(
'/deals/daily?from=2024-01-11&to=2024-01-12',
baseUrl
), {
redirect: 'manual'
}
)
await assertResponseStatus(res, 200)
const stats = await res.json()
assert.deepStrictEqual(stats, [
{ day: '2024-01-11', total: 20, indexed: 6, retrievable: 2 },
{ day: '2024-01-12', total: 30, indexed: 7, retrievable: 3 }
])
})
})
})

const givenRetrievalStats = async (pgPool, { day, minerId, total, successful }) => {
Expand All @@ -389,3 +414,10 @@ const givenRetrievalStats = async (pgPool, { day, minerId, total, successful })
[day, minerId ?? 'f1test', total, successful]
)
}

const givenDailyDealStats = async (pgPool, { day, total, indexed, retrievable }) => {
await pgPool.query(`
INSERT INTO daily_deals (day, total, indexed, retrievable)
VALUES ($1, $2, $3, $4)
`, [day, total, indexed, retrievable])
}

0 comments on commit 24d4ec9

Please sign in to comment.