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: Included API handling for fetching from daily_node_metrics #71

Merged
merged 5 commits into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
9 changes: 9 additions & 0 deletions lib/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import {
fetchRetrievalSuccessRate
} from './stats-fetchers.js'

import { fetchDailyNodeMetrics } from './platform-stats-fetchers.js'

/**
* @param {object} args
* @param {import('pg').Pool} args.pgPool
Expand Down Expand Up @@ -68,6 +70,13 @@ const handler = async (req, res, pgPool) => {
res,
pgPool,
fetchMinersRSRSummary)
} else if (req.method === 'GET' && segs[0] === 'nodes' && segs[1] === 'daily' && segs.length === 2) {
await getStatsWithFilterAndCaching(
pathname,
searchParams,
res,
pgPool,
fetchDailyNodeMetrics)
} else if (req.method === 'GET' && segs.length === 0) {
// health check - required by Grafana datasources
res.end('OK')
Expand Down
16 changes: 16 additions & 0 deletions lib/platform-stats-fetchers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* @param {import('pg').Pool} pgPool
* @param {import('./typings').Filter} filter
*/
export const fetchDailyNodeMetrics = async (pgPool, filter) => {
const { rows } = await pgPool.query(`
SELECT day::TEXT, station_id
FROM daily_node_metrics
WHERE day >= $1 AND day <= $2
GROUP BY day, station_id
`, [
filter.from,
filter.to
])
return rows
}
32 changes: 32 additions & 0 deletions test/handler.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ describe('HTTP request handler', () => {
beforeEach(async () => {
await pgPool.query('DELETE FROM retrieval_stats')
await pgPool.query('DELETE FROM daily_participants')
await pgPool.query('DELETE FROM daily_node_metrics')
})

it('returns 200 for GET /', async () => {
Expand Down Expand Up @@ -244,6 +245,30 @@ describe('HTTP request handler', () => {
])
})
})

describe('GET /nodes/daily', () => {
it('returns daily node metrics for the given date range', async () => {
await givenDailyNodeMetrics(pgPool, '2024-01-10', 'station1')
await givenDailyNodeMetrics(pgPool, '2024-01-11', 'station2')
await givenDailyNodeMetrics(pgPool, '2024-01-12', 'station3')
await givenDailyNodeMetrics(pgPool, '2024-01-13', 'station1')

const res = await fetch(
new URL(
'/nodes/daily?from=2024-01-11&to=2024-01-12',
baseUrl
), {
redirect: 'manual'
}
)
await assertResponseStatus(res, 200)
const metrics = await res.json()
assert.deepStrictEqual(metrics, [
{ day: '2024-01-11', station_id: 'station2' },
{ day: '2024-01-12', station_id: 'station3' }
])
})
})
})

const assertResponseStatus = async (res, status) => {
Expand Down Expand Up @@ -274,3 +299,10 @@ const givenDailyParticipants = async (pgPool, day, participantAddresses) => {
ids
])
}

const givenDailyNodeMetrics = async (pgPool, day, stationId) => {
await pgPool.query(
'INSERT INTO daily_node_metrics (day, station_id) VALUES ($1, $2)',
[day, stationId]
)
}