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
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion lib/preprocess.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export class Measurement {
this.inet_group = pointerize(m.inet_group)
this.finished_at = parseDateTime(m.finished_at)
this.provider_address = pointerize(m.provider_address)
this.protocol = pointerize(m.protocol)
this.protocol = pointerize(m.protocol?.toLowerCase())
this.byte_length = m.byte_length
this.start_at = parseDateTime(m.start_at)
this.first_byte_at = parseDateTime(m.first_byte_at)
Expand Down
9 changes: 6 additions & 3 deletions lib/provider-retrieval-result-stats.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,18 @@ 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 === 'http') { retrievalStats.successfulHttp++ }
bajtos marked this conversation as resolved.
Show resolved Hide resolved
}
providerRetrievalResultStats.set(minerId, retrievalStats)
}
}
Expand Down
15 changes: 9 additions & 6 deletions lib/public-stats.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
])
}

Expand Down
1 change: 1 addition & 0 deletions migrations/021.do.sucessful-http-retrieval-stats.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE retrieval_stats ADD COLUMN IF NOT EXISTS successful_http INT;
5 changes: 3 additions & 2 deletions test/evaluate.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,11 @@ describe('evaluate', async function () {
day: today(),
miner_id: VALID_TASK.minerId,
total: 1,
successful: 1
successful: 1,
// None of the measurements use http
successful_http: 0
}])
})

it('handles empty rounds', async () => {
const round = new RoundData(0n)
const setScoresCalls = []
Expand Down
30 changes: 19 additions & 11 deletions test/provider-retrieval-result-stats.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 }]
]))
})
})
Expand Down Expand Up @@ -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'
}
]
}
Expand All @@ -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),
Expand Down
48 changes: 46 additions & 2 deletions test/public-stats.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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[]} */
Expand Down Expand Up @@ -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 }
])
})
})
Expand Down
Loading