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: Add HTTP-RSR to daily retrieval stats #440

Merged
merged 19 commits into from
Jan 9, 2025
Merged
Changes from 11 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
11 changes: 8 additions & 3 deletions lib/provider-retrieval-result-stats.js
Original file line number Diff line number Diff line change
@@ -16,15 +16,20 @@ const withPgClient = fn => async ({ createPgClient, ...args }) => {
}

export const build = committees => {
/** @type {Map<string, {total: number, successful: number}>} */
/** @type {Map<string, {total: number, successful: number, successfulHttp:number}>} */
const providerRetrievalResultStats = new Map()
for (const c of committees) {
// IMPORTANT: include minority results in the calculation
for (const m of c.measurements) {
const minerId = m.minerId
const retrievalStats = providerRetrievalResultStats.get(minerId) ?? { total: 0, successful: 0 }
const retrievalStats = providerRetrievalResultStats.get(minerId) ?? { total: 0, successful: 0, successfulHttp: 0 }
retrievalStats.total++
if (m.retrievalResult === 'OK') retrievalStats.successful++
if (m.retrievalResult === 'OK') {
retrievalStats.successful++
if (m.protocol && m.protocol.toLowerCase() === 'http') {
retrievalStats.successfulHttp++
}
}
providerRetrievalResultStats.set(minerId, retrievalStats)
}
}
15 changes: 9 additions & 6 deletions lib/public-stats.js
Original file line number Diff line number Diff line change
@@ -38,21 +38,24 @@ export const updatePublicStats = async ({ createPgClient, committees, honestMeas
* @param {object} stats
* @param {number} stats.total
* @param {number} stats.successful
* @param {number} stats.successfulHttp
*/
const updateRetrievalStats = async (pgClient, minerId, { total, successful }) => {
debug('Updating public retrieval stats for miner %s: total += %s successful += %s', minerId, total, successful)
const updateRetrievalStats = async (pgClient, minerId, { total, successful, successfulHttp }) => {
debug('Updating public retrieval stats for miner %s: total += %s successful += %s, successful_http += %s', minerId, total, successful, successfulHttp)
await pgClient.query(`
INSERT INTO retrieval_stats
(day, miner_id, total, successful)
(day, miner_id, total, successful, successful_http)
VALUES
(now(), $1, $2, $3)
(now(), $1, $2, $3, $4)
ON CONFLICT(day, miner_id) DO UPDATE SET
total = retrieval_stats.total + $2,
successful = retrieval_stats.successful + $3
successful = retrieval_stats.successful + $3,
successful_http = retrieval_stats.successful_http + $4
`, [
minerId,
total,
successful
successful,
successfulHttp
])
}

3 changes: 2 additions & 1 deletion migrations/001.do.retrieval-stats.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
CREATE TABLE retrieval_stats (
day DATE NOT NULL PRIMARY KEY,
total INT NOT NULL,
successful INT NOT NULL
successful INT NOT NULL,
successful_http INT NOT NULL
);
5 changes: 3 additions & 2 deletions test/evaluate.js
Original file line number Diff line number Diff line change
@@ -96,10 +96,11 @@ describe('evaluate', async function () {
day: today(),
miner_id: VALID_TASK.minerId,
total: 1,
successful: 1
successful: 1,
// None of the measurments use http
successful_http: 0
}])
})

it('handles empty rounds', async () => {
const round = new RoundData(0n)
const setScoresCalls = []
30 changes: 19 additions & 11 deletions test/provider-retrieval-result-stats.test.js
Original file line number Diff line number Diff line change
@@ -37,29 +37,33 @@ describe('Provider Retrieval Result Stats', () => {
measurements: [
{
minerId: '0',
retrievalResult: 'OK'
retrievalResult: 'OK',
protocol: 'http'
},
{
minerId: '1',
retrievalResult: 'TIMEOUT'
retrievalResult: 'TIMEOUT',
protocol: 'http'
}
]
}, {
measurements: [
{
minerId: '0',
retrievalResult: 'OK'
retrievalResult: 'OK',
protocol: 'bitswap'
},
{
// Should be able to handle and reject undefined protocol
minerId: '1',
retrievalResult: 'TIMEOUT'
}
]
}
])
assert.deepStrictEqual(stats, new Map([
['0', { total: 2, successful: 2 }],
['1', { total: 2, successful: 0 }]
['0', { total: 2, successful: 2, successfulHttp: 1 }],
['1', { total: 2, successful: 0, successfulHttp: 0 }]
]))
})
})
@@ -137,22 +141,26 @@ describe('Provider Retrieval Result Stats', () => {
measurements: [
{
minerId: '0',
retrievalResult: 'OK'
retrievalResult: 'OK',
protocol: 'http'
},
{
minerId: '1',
retrievalResult: 'TIMEOUT'
retrievalResult: 'TIMEOUT',
protocol: 'http'
}
]
}, {
measurements: [
{
minerId: '0',
retrievalResult: 'OK'
retrievalResult: 'OK',
protocol: 'bitswap'
},
{
minerId: '1',
retrievalResult: 'TIMEOUT'
retrievalResult: 'TIMEOUT',
protocol: 'bitswap'
}
]
}
@@ -168,8 +176,8 @@ describe('Provider Retrieval Result Stats', () => {
contract_address: ieContractAddress,
measurement_batches: round.measurementBatches,
provider_retrieval_result_stats: {
0: { successful: 2, total: 2 },
1: { successful: 0, total: 2 }
0: { successful: 2, total: 2, successfulHttp: 1 },
1: { successful: 0, total: 2, successfulHttp: 0 }
},
round_details: 'baguqeerawg5jfpiy2g5xp5d422uwa3mpyzkmiguoeecesds7q65mn2hdoa4q',
round_index: String(round.index),
48 changes: 46 additions & 2 deletions test/public-stats.test.js
Original file line number Diff line number Diff line change
@@ -87,6 +87,50 @@ describe('public-stats', () => {
{ day: today, total: 2 + 3, successful: 1 + 1 }
])
})
it('calculates successful http retrievals correctly', async () => {
/** @type {Measurement[]} */
const honestMeasurements = [
{ ...VALID_MEASUREMENT, protocol: 'http', retrievalResult: 'OK' },
{ ...VALID_MEASUREMENT, protocol: 'graphsync', retrievalResult: 'OK' },
{ ...VALID_MEASUREMENT, protocol: 'http', retrievalResult: 'HTTP_500' },
{ ...VALID_MEASUREMENT, protocol: 'graphsync', retrievalResult: 'LASSIE_500' }
]
const allMeasurements = honestMeasurements
let committees = buildEvaluatedCommitteesFromMeasurements(honestMeasurements)

await updatePublicStats({
createPgClient,
committees,
honestMeasurements,
allMeasurements,
findDealClients: (_minerId, _cid) => ['f0client']
})

const { rows: created } = await pgClient.query(
'SELECT day::TEXT, total, successful, successful_http FROM retrieval_stats'
)
assert.deepStrictEqual(created, [
{ day: today, total: 4, successful: 2, successful_http: 1 }
])

// Let's add another successful http retrieval to make sure the updating process works as expected
honestMeasurements.push({ ...VALID_MEASUREMENT, retrievalResult: 'OK', protocol: 'http' })
committees = buildEvaluatedCommitteesFromMeasurements(honestMeasurements)
await updatePublicStats({
createPgClient,
committees,
honestMeasurements,
allMeasurements,
findDealClients: (_minerId, _cid) => ['f0client']
})

const { rows: updated } = await pgClient.query(
'SELECT day::TEXT, total, successful, successful_http FROM retrieval_stats'
)
assert.deepStrictEqual(updated, [
{ day: today, total: 4 + 5, successful: 2 + 3, successful_http: 1 + 2 }
])
})

it('creates or updates the row for today - multiple miners', async () => {
/** @type {Measurement[]} */
@@ -164,10 +208,10 @@ describe('public-stats', () => {
})

const { rows: created } = await pgClient.query(
'SELECT day::TEXT, total, successful FROM retrieval_stats'
'SELECT day::TEXT, total, successful, successful_http FROM retrieval_stats'
)
assert.deepStrictEqual(created, [
{ day: today, total: 3, successful: 2 }
{ day: today, total: 3, successful: 2, successful_http: 0 }
])
})
})