Skip to content

Commit d15f62e

Browse files
committed
Add tests for retrieval times
1 parent c4e3370 commit d15f62e

File tree

3 files changed

+77
-18
lines changed

3 files changed

+77
-18
lines changed

stats/lib/handler.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ import {
1616
fetchDailyRetrievalResultCodes,
1717
fetchDailyMinerRSRSummary,
1818
fetchDailyRetrievalTimes,
19-
fetchDailyMinerRetrievalTimes,
20-
fetchRetrievalTimesSummary
19+
fetchDailyMinerRetrievalTimes
2120
} from './stats-fetchers.js'
2221

2322
import { handlePlatformRoutes } from './platform-routes.js'
@@ -111,8 +110,6 @@ const handler = async (req, res, pgPools, SPARK_API_BASE_URL) => {
111110
await respond(fetchMinersRSRSummary)
112111
} else if (req.method === 'GET' && url === '/retrieval-result-codes/daily') {
113112
await respond(fetchDailyRetrievalResultCodes)
114-
} else if (req.method === 'GET' && url === '/retrieval-times/summary') {
115-
await respond(fetchRetrievalTimesSummary)
116113
} else if (req.method === 'GET' && url === '/retrieval-times/daily') {
117114
await respond(fetchDailyRetrievalTimes)
118115
} else if (req.method === 'GET' && segs[0] === 'miner' && segs[1] && segs[2] === 'retrieval-times' && segs[3] === 'summary') {

stats/lib/stats-fetchers.js

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -271,19 +271,6 @@ export const fetchDailyRetrievalResultCodes = async (pgPools, filter) => {
271271
return stats
272272
}
273273

274-
/**
275-
* Fetches global retrieval time statistics
276-
* @param {import('@filecoin-station/spark-stats-db').PgPools} pgPools
277-
* @param {import('./typings.js').DateRangeFilter} filter
278-
*/
279-
export const fetchRetrievalTimesSummary = async (pgPools, filter) => {
280-
const { rows } = await pgPools.evaluate.query(`
281-
SELECT percentile_cont(0.5) WITHIN GROUP (ORDER BY time_to_first_byte_p50) AS ttfb_p50
282-
FROM retrieval_times
283-
`)
284-
return rows
285-
}
286-
287274
/**
288275
* Fetches daily global retrieval time statistics
289276
* @param {import('@filecoin-station/spark-stats-db').PgPools} pgPools
@@ -319,7 +306,7 @@ export const fetchDailyMinerRetrievalTimes = async (pgPools, { from, to }, miner
319306
percentile_cont(0.5) WITHIN GROUP (ORDER BY time_to_first_byte_p50) AS ttfb_p50
320307
FROM retrieval_times
321308
WHERE miner_id = $1 AND day >= $2 AND day <= $3
322-
GROUP BY day, miner_id
309+
GROUP BY day, miner_id
323310
ORDER BY day
324311
`, [
325312
minerId,

stats/test/handler.test.js

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -709,6 +709,65 @@ describe('HTTP request handler', () => {
709709
])
710710
})
711711
})
712+
713+
describe('miner retrieval time stats', () => {
714+
beforeEach(async () => {
715+
// before the range
716+
await givenRetrievalTimes(pgPools.evaluate, { day: '2024-01-09', minerId: 'f1one', taskId: 'cidone::f1one::0', timeToFirstByteP50: 1000 })
717+
await givenRetrievalTimes(pgPools.evaluate, { day: '2024-01-09', minerId: 'f1two', taskId: 'cidone::f1two::0', timeToFirstByteP50: 1000 })
718+
// in the range
719+
await givenRetrievalTimes(pgPools.evaluate, { day: '2024-01-20', minerId: 'f1one', taskId: 'cidone::f1one::1', timeToFirstByteP50: 1000 })
720+
await givenRetrievalTimes(pgPools.evaluate, { day: '2024-01-20', minerId: 'f1two', taskId: 'cidone::f1two::1', timeToFirstByteP50: 1000 })
721+
722+
await givenRetrievalTimes(pgPools.evaluate, { day: '2024-01-10', minerId: 'f1one', taskId: 'cidone::f1one::2', timeToFirstByteP50: 3000 })
723+
await givenRetrievalTimes(pgPools.evaluate, { day: '2024-01-10', minerId: 'f1two', taskId: 'cidone::f1two::2', timeToFirstByteP50: 3000 })
724+
await givenRetrievalTimes(pgPools.evaluate, { day: '2024-01-10', minerId: 'f1one', taskId: 'cidone::f1one::3', timeToFirstByteP50: 1000 })
725+
await givenRetrievalTimes(pgPools.evaluate, { day: '2024-01-10', minerId: 'f1two', taskId: 'cidone::f1two::3', timeToFirstByteP50: 1000 })
726+
// after the range
727+
await givenRetrievalTimes(pgPools.evaluate, { day: '2024-01-21', minerId: 'f1one', taskId: 'cidone::f1one::4', timeToFirstByteP50: 1000 })
728+
await givenRetrievalTimes(pgPools.evaluate, { day: '2024-01-21', minerId: 'f1two', taskId: 'cidone::f1two::4', timeToFirstByteP50: 1000 })
729+
})
730+
731+
it('lists daily retrieval times in given date range', async () => {
732+
const res = await fetch(
733+
new URL(
734+
'/retrieval-times/daily?from=2024-01-10&to=2024-01-20',
735+
baseUrl
736+
), {
737+
redirect: 'manual'
738+
}
739+
)
740+
await assertResponseStatus(res, 200)
741+
742+
const stats = /** @type {{ day: string, success_rate: number }[]} */(
743+
await res.json()
744+
)
745+
assert.deepStrictEqual(stats, [
746+
{ day: '2024-01-10', ttfb_p50: 2000 },
747+
{ day: '2024-01-20', ttfb_p50: 1000 }
748+
])
749+
})
750+
751+
it('lists daily retrieval times summary for specified miner in given date range', async () => {
752+
const res = await fetch(
753+
new URL(
754+
'/miner/f1one/retrieval-times/summary?from=2024-01-10&to=2024-01-20',
755+
baseUrl
756+
), {
757+
redirect: 'manual'
758+
}
759+
)
760+
await assertResponseStatus(res, 200)
761+
762+
const stats = /** @type {{ day: string, success_rate: number }[]} */(
763+
await res.json()
764+
)
765+
assert.deepStrictEqual(stats, [
766+
{ day: '2024-01-10', miner_id: 'f1one', ttfb_p50: 2000 },
767+
{ day: '2024-01-20', miner_id: 'f1one', ttfb_p50: 1000 }
768+
])
769+
})
770+
})
712771
})
713772

714773
/**
@@ -784,3 +843,19 @@ const givenDailyDealStats = async (pgPool, {
784843
retrievable
785844
])
786845
}
846+
847+
/**
848+
*
849+
* @param {import('../lib/platform-stats-fetchers.js').Queryable} pgPool
850+
* @param {object} data
851+
* @param {string} data.day
852+
* @param {string} data.minerId
853+
* @param {string} data.taskId
854+
* @param {number} data.timeToFirstByteP50
855+
*/
856+
const givenRetrievalTimes = async (pgPool, { day, minerId, taskId, timeToFirstByteP50 }) => {
857+
await pgPool.query(
858+
'INSERT INTO retrieval_times (day, miner_id, task_id, time_to_first_byte_p50) VALUES ($1, $2, $3, $4)',
859+
[day, minerId ?? 'f1test', taskId ?? 'cidone::f1test::0', timeToFirstByteP50]
860+
)
861+
}

0 commit comments

Comments
 (0)