Skip to content

Commit 43054a0

Browse files
committed
Add per-miner RSR
1 parent 058f4c9 commit 43054a0

File tree

4 files changed

+70
-1
lines changed

4 files changed

+70
-1
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ Base URL: http://stats.filspark.com/
2020

2121
http://stats.filspark.com/miners/retrieval-success-rate/summary
2222

23+
- `GET /miner/:id/retrieval-success-rate/summary?from=<day>&to=<day>`
24+
25+
http://stats.filspark.com/miner/f0814049/retrieval-success-rate/summary
26+
2327
- `GET /miner/:id/deals/eligible/summary`
2428

2529
http://stats.filspark.com/miner/f0814049/deals/eligible/summary

stats/lib/handler.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ import {
1313
fetchParticipantRewardTransfers,
1414
fetchRetrievalSuccessRate,
1515
fetchDealSummary,
16-
fetchDailyRetrievalResultCodes
16+
fetchDailyRetrievalResultCodes,
17+
fetchDailyMinerRSRSummary
1718
} from './stats-fetchers.js'
1819

1920
import { handlePlatformRoutes } from './platform-routes.js'
@@ -107,6 +108,8 @@ const handler = async (req, res, pgPools, SPARK_API_BASE_URL) => {
107108
await respond(fetchMinersRSRSummary)
108109
} else if (req.method === 'GET' && url === '/retrieval-result-codes/daily') {
109110
await respond(fetchDailyRetrievalResultCodes)
111+
} else if (req.method === 'GET' && segs[0] === 'miner' && segs[1] && segs[2] === 'retrieval-success-rate' && segs[3] === 'summary') {
112+
await respond(fetchDailyMinerRSRSummary, segs[1])
110113
} else if (req.method === 'GET' && segs[0] === 'miner' && segs[1] && segs[2] === 'deals' && segs[3] === 'eligible' && segs[4] === 'summary') {
111114
redirectToSparkApi(req, res, SPARK_API_BASE_URL)
112115
} else if (req.method === 'GET' && segs[0] === 'client' && segs[1] && segs[2] === 'deals' && segs[3] === 'eligible' && segs[4] === 'summary') {

stats/lib/stats-fetchers.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ export const fetchParticipantRewardTransfers = async (pgPools, { from, to }, add
201201
}
202202

203203
/**
204+
* Fetches the retrieval stats summary for all miners for given date range.
204205
* @param {PgPools} pgPools
205206
* @param {import('./typings.js').DateRangeFilter} filter
206207
*/
@@ -223,6 +224,33 @@ export const fetchMinersRSRSummary = async (pgPools, filter) => {
223224
return stats
224225
}
225226

227+
/**
228+
* Fetches the retrieval stats summary for a single miner for given date range.
229+
* @param {PgPools} pgPools
230+
* @param {import('./typings.js').DateRangeFilter} filter
231+
* @param {string} minerId
232+
*/
233+
export const fetchDailyMinerRSRSummary = async (pgPools, { from, to }, minerId) => {
234+
const { rows } = await pgPools.evaluate.query(`
235+
SELECT day::TEXT, SUM(total) as total, SUM(successful) as successful
236+
FROM retrieval_stats
237+
WHERE miner_id = $1 AND day >= $2 AND day <= $3
238+
GROUP BY day
239+
ORDER BY day
240+
`, [
241+
minerId,
242+
from,
243+
to
244+
])
245+
const stats = rows.map(r => ({
246+
day: r.day,
247+
total: r.total,
248+
successful: r.successful,
249+
success_rate: r.total > 0 ? r.successful / r.total : null
250+
}))
251+
return stats
252+
}
253+
226254
export const fetchDailyRetrievalResultCodes = async (pgPools, filter) => {
227255
const { rows } = await pgPools.stats.query(`
228256
SELECT day::TEXT, code, rate

stats/test/handler.test.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -674,6 +674,40 @@ describe('HTTP request handler', () => {
674674
assert.strictEqual(res.headers.get('access-control-allow-origin'), 'http://localhost:3000')
675675
})
676676
})
677+
678+
describe('GET /miner/{id}/retrieval-success-rate/summary', () => {
679+
it('lists daily retrieval stats summary for specified miner in given date range', async () => {
680+
// before the range
681+
await givenRetrievalStats(pgPools.evaluate, { day: '2024-01-09', minerId: 'f1one', total: 10, successful: 1 })
682+
await givenRetrievalStats(pgPools.evaluate, { day: '2024-01-09', minerId: 'f1two', total: 100, successful: 20 })
683+
// in the range
684+
await givenRetrievalStats(pgPools.evaluate, { day: '2024-01-20', minerId: 'f1one', total: 20, successful: 1 })
685+
await givenRetrievalStats(pgPools.evaluate, { day: '2024-01-20', minerId: 'f1two', total: 200, successful: 60 })
686+
await givenRetrievalStats(pgPools.evaluate, { day: '2024-01-10', minerId: 'f1one', total: 10, successful: 1 })
687+
await givenRetrievalStats(pgPools.evaluate, { day: '2024-01-10', minerId: 'f1two', total: 100, successful: 50 })
688+
// after the range
689+
await givenRetrievalStats(pgPools.evaluate, { day: '2024-01-21', minerId: 'f1one', total: 30, successful: 1 })
690+
await givenRetrievalStats(pgPools.evaluate, { day: '2024-01-21', minerId: 'f1two', total: 300, successful: 60 })
691+
692+
const res = await fetch(
693+
new URL(
694+
'/miner/f1one/retrieval-success-rate/summary?from=2024-01-10&to=2024-01-20',
695+
baseUrl
696+
), {
697+
redirect: 'manual'
698+
}
699+
)
700+
await assertResponseStatus(res, 200)
701+
702+
const stats = /** @type {{ day: string, success_rate: number }[]} */(
703+
await res.json()
704+
)
705+
assert.deepStrictEqual(stats, [
706+
{ day: '2024-01-10', success_rate: 1 / 10, total: '10', successful: '1' },
707+
{ day: '2024-01-20', success_rate: 1 / 20, total: '20', successful: '1' }
708+
])
709+
})
710+
})
677711
})
678712

679713
/**

0 commit comments

Comments
 (0)