Skip to content

Commit 590173c

Browse files
authored
fix: sum deal stats over miners and clients (#216)
Signed-off-by: Miroslav Bajtoš <oss@bajtos.net>
1 parent dc49a62 commit 590173c

File tree

2 files changed

+87
-23
lines changed

2 files changed

+87
-23
lines changed

stats/lib/stats-fetchers.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,15 @@ export const fetchDailyDealStats = async (pgPools, filter) => {
3737
const { rows } = await pgPools.evaluate.query(`
3838
SELECT
3939
day::text,
40-
tested,
41-
index_majority_found AS "indexMajorityFound",
42-
indexed,
43-
indexed_http AS "indexedHttp",
44-
retrieval_majority_found AS "retrievalMajorityFound",
45-
retrievable
40+
SUM(tested) AS tested,
41+
SUM(index_majority_found) AS "indexMajorityFound",
42+
SUM(indexed) AS indexed,
43+
SUM(indexed_http) AS "indexedHttp",
44+
SUM(retrieval_majority_found) AS "retrievalMajorityFound",
45+
SUM(retrievable) AS retrievable
4646
FROM daily_deals
4747
WHERE day >= $1 AND day <= $2
48+
GROUP BY day
4849
ORDER BY day
4950
`, [
5051
filter.from,

stats/test/handler.test.js

Lines changed: 80 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -495,21 +495,77 @@ describe('HTTP request handler', () => {
495495
assert.deepStrictEqual(stats, [
496496
{
497497
day: '2024-01-11',
498-
tested: 20,
499-
indexMajorityFound: 10,
500-
indexed: 6,
501-
indexedHttp: 4,
502-
retrievalMajorityFound: 5,
503-
retrievable: 2
498+
tested: '20',
499+
indexMajorityFound: '10',
500+
indexed: '6',
501+
indexedHttp: '4',
502+
retrievalMajorityFound: '5',
503+
retrievable: '2'
504504
},
505505
{
506506
day: '2024-01-12',
507-
tested: 30,
508-
indexMajorityFound: 7,
509-
indexed: 7,
510-
indexedHttp: 7,
511-
retrievalMajorityFound: 3,
512-
retrievable: 3
507+
tested: '30',
508+
indexMajorityFound: '7',
509+
indexed: '7',
510+
indexedHttp: '7',
511+
retrievalMajorityFound: '3',
512+
retrievable: '3'
513+
}
514+
])
515+
})
516+
517+
it('aggregates stats over miners', async () => {
518+
await givenDailyDealStats(pgPools.evaluate, { day: '2024-01-11', minerId: 'f1aa', tested: 10 })
519+
await givenDailyDealStats(pgPools.evaluate, { day: '2024-01-11', minerId: 'f1bb', tested: 20 })
520+
await givenDailyDealStats(pgPools.evaluate, { day: '2024-01-12', minerId: 'f1aa', tested: 30 })
521+
await givenDailyDealStats(pgPools.evaluate, { day: '2024-01-12', minerId: 'f1bb', tested: 40 })
522+
523+
const res = await fetch(
524+
new URL(
525+
'/deals/daily?from=2024-01-11&to=2024-01-12',
526+
baseUrl
527+
), {
528+
redirect: 'manual'
529+
}
530+
)
531+
await assertResponseStatus(res, 200)
532+
const stats = /** @type {any[]} */(await res.json())
533+
assert.deepStrictEqual(stats.map(({ day, tested }) => ({ day, tested })), [
534+
{
535+
day: '2024-01-11',
536+
tested: String(10 + 20)
537+
},
538+
{
539+
day: '2024-01-12',
540+
tested: String(30 + 40)
541+
}
542+
])
543+
})
544+
545+
it('aggregates stats over clients', async () => {
546+
await givenDailyDealStats(pgPools.evaluate, { day: '2024-01-11', clientId: 'f1aa', tested: 10 })
547+
await givenDailyDealStats(pgPools.evaluate, { day: '2024-01-11', clientId: 'f1bb', tested: 20 })
548+
await givenDailyDealStats(pgPools.evaluate, { day: '2024-01-12', clientId: 'f1aa', tested: 30 })
549+
await givenDailyDealStats(pgPools.evaluate, { day: '2024-01-12', minerId: 'f1bb', tested: 40 })
550+
551+
const res = await fetch(
552+
new URL(
553+
'/deals/daily?from=2024-01-11&to=2024-01-12',
554+
baseUrl
555+
), {
556+
redirect: 'manual'
557+
}
558+
)
559+
await assertResponseStatus(res, 200)
560+
const stats = /** @type {any[]} */(await res.json())
561+
assert.deepStrictEqual(stats.map(({ day, tested }) => ({ day, tested })), [
562+
{
563+
day: '2024-01-11',
564+
tested: String(10 + 20)
565+
},
566+
{
567+
day: '2024-01-12',
568+
tested: String(30 + 40)
513569
}
514570
])
515571
})
@@ -617,10 +673,10 @@ const givenRetrievalStats = async (pgPool, { day, minerId, total, successful })
617673
* clientId?: string;
618674
* tested: number;
619675
* indexMajorityFound?: number;
620-
* indexed: number;
676+
* indexed?: number;
621677
* indexedHttp?: number;
622678
* retrievalMajorityFound?: number;
623-
* retrievable: number;
679+
* retrievable?: number;
624680
* }} stats
625681
*/
626682
const givenDailyDealStats = async (pgPool, {
@@ -634,6 +690,13 @@ const givenDailyDealStats = async (pgPool, {
634690
retrievalMajorityFound,
635691
retrievable
636692
}) => {
693+
indexed ??= tested
694+
indexedHttp ??= indexed
695+
indexMajorityFound ??= indexed
696+
697+
retrievable ??= tested
698+
retrievalMajorityFound ??= retrievable
699+
637700
await pgPool.query(`
638701
INSERT INTO daily_deals (
639702
day,
@@ -651,10 +714,10 @@ const givenDailyDealStats = async (pgPool, {
651714
minerId ?? 'f1miner',
652715
clientId ?? 'f1client',
653716
tested,
654-
indexMajorityFound ?? indexed,
717+
indexMajorityFound,
655718
indexed,
656-
indexedHttp ?? indexed,
657-
retrievalMajorityFound ?? retrievable,
719+
indexedHttp,
720+
retrievalMajorityFound,
658721
retrievable
659722
])
660723
}

0 commit comments

Comments
 (0)