Skip to content

Commit

Permalink
Included API handling for fetching from daily_node_metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
PatrickNercessian committed Apr 17, 2024
1 parent 906d6e2 commit a9e60ed
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 0 deletions.
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 metric_date::TEXT, station_id
FROM daily_node_metrics
WHERE metric_date >= $1 AND metric_date <= $2
GROUP BY metric_date, 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, [
{ metric_date: '2024-01-11', station_id: 'station2' },
{ metric_date: '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 (metric_date, station_id) VALUES ($1, $2)',
[day, stationId]
)
}

0 comments on commit a9e60ed

Please sign in to comment.