Skip to content

Commit

Permalink
Accept 88-char station IDs instead, naming changes
Browse files Browse the repository at this point in the history
  • Loading branch information
PatrickNercessian committed Apr 29, 2024
1 parent 22a7cc8 commit f86658b
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 15 deletions.
8 changes: 4 additions & 4 deletions lib/platform-stats.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,22 @@ const debug = createDebug('spark:platform-stats')
* @param {import('./preprocess').Measurement[]} honestMeasurements
*/
export const updatePlatformStats = async (pgClient, honestMeasurements) => {
await updateDailyNodeMetrics(pgClient, honestMeasurements)
await updateDailyStationStats(pgClient, honestMeasurements)
}

/**
* @param {import('pg').Client} pgClient
* @param {import('./preprocess').Measurement[]} honestMeasurements
*/
export const updateDailyNodeMetrics = async (pgClient, honestMeasurements) => {
export const updateDailyStationStats = async (pgClient, honestMeasurements) => {
// TODO: when we add more fields, we will update the ON CONFLICT clause
// to update those fields, and we won't just use a Set for the stationIds
// which currently removes all granular measurement details
const stationIds = [...new Set(honestMeasurements.map(m => m.stationId))]
debug('Updating daily node metrics, unique_count=%s', stationIds.length)
debug('Updating daily station stats, unique_count=%s', stationIds.length)

await pgClient.query(`
INSERT INTO daily_node_metrics (station_id, day)
INSERT INTO daily_stations (station_id, day)
SELECT unnest($1::text[]), now()
ON CONFLICT (station_id, day) DO NOTHING
`, [stationIds])
Expand Down
6 changes: 5 additions & 1 deletion lib/preprocess.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,11 @@ const assertValidMeasurement = measurement => {
assert(typeof measurement.inet_group === 'string', 'valid inet group required')
assert(typeof measurement.finished_at === 'number', 'field `finished_at` must be set to a number')
if (measurement.stationId) {
assert(typeof measurement.stationId === 'string' && measurement.stationId.length === 64, 'stationId must be a string of 64 characters')
assert(
typeof measurement.stationId === 'string' &&
measurement.stationId.match(/^[0-9a-fA-F]{88}$/),
'stationId must be a hex string with 88 characters'
)
}
}

Expand Down
2 changes: 1 addition & 1 deletion migrations/006.do.daily-node-metrics.sql
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
CREATE TABLE daily_node_metrics (
CREATE TABLE daily_stations (
day DATE NOT NULL,
station_id TEXT NOT NULL,
PRIMARY KEY (day, station_id)
Expand Down
2 changes: 1 addition & 1 deletion test/helpers/test-data.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export const VALID_PARTICIPANT_ADDRESS = '0x000000000000000000000000000000000000dEaD'
export const VALID_STATION_ID = '6400000000000000000000000000000000000000000000000000000000000000'
export const VALID_STATION_ID = '8800000000000000000000000000000000000000000000000000000000000000000000000000000000000000'

export const VALID_TASK = {
cid: 'QmUuEoBdjC8D1PfWZCc7JCSK8nj7TV6HbXWDHYHzZHCVGS',
Expand Down
19 changes: 11 additions & 8 deletions test/platform-stats.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { beforeEach, describe, it } from 'mocha'
import { DATABASE_URL } from '../lib/config.js'
import { migrateWithPgClient } from '../lib/migrate.js'
import { VALID_MEASUREMENT, VALID_STATION_ID } from './helpers/test-data.js'
import { updateDailyNodeMetrics } from '../lib/platform-stats.js'
import { updateDailyStationStats } from '../lib/platform-stats.js'

const createPgClient = async () => {
const pgClient = new pg.Client({ connectionString: DATABASE_URL })
Expand All @@ -22,7 +22,7 @@ describe('platform-stats', () => {

let today
beforeEach(async () => {
await pgClient.query('DELETE FROM daily_node_metrics')
await pgClient.query('DELETE FROM daily_stations')

// Run all tests inside a transaction to ensure `now()` always returns the same value
// See https://dba.stackexchange.com/a/63549/125312
Expand All @@ -39,17 +39,20 @@ describe('platform-stats', () => {
await pgClient.end()
})

describe('updateDailyNodeMetrics', () => {
it('updates daily node metrics for today with multiple measurements', async () => {
describe('updateDailyStationStats', () => {
it('updates daily station stats for today with multiple measurements', async () => {
const validStationId2 = VALID_STATION_ID.slice(0, -1) + '1'
const honestMeasurements = [
{ ...VALID_MEASUREMENT, stationId: VALID_STATION_ID },
{ ...VALID_MEASUREMENT, stationId: validStationId2 }
]

await updateDailyNodeMetrics(pgClient, honestMeasurements)
await updateDailyStationStats(pgClient, honestMeasurements)

const { rows } = await pgClient.query('SELECT station_id, day::TEXT FROM daily_node_metrics ORDER BY station_id')
const { rows } = await pgClient.query(`
SELECT station_id, day::TEXT FROM daily_stations
ORDER BY station_id`
)
assert.strictEqual(rows.length, 2)
assert.deepStrictEqual(rows, [
{ station_id: VALID_STATION_ID, day: today },
Expand All @@ -63,9 +66,9 @@ describe('platform-stats', () => {
{ ...VALID_MEASUREMENT, stationId: VALID_STATION_ID }
]

await updateDailyNodeMetrics(pgClient, honestMeasurements)
await updateDailyStationStats(pgClient, honestMeasurements)

const { rows } = await pgClient.query('SELECT station_id, day::TEXT FROM daily_node_metrics')
const { rows } = await pgClient.query('SELECT station_id, day::TEXT FROM daily_stations')
assert.strictEqual(rows.length, 1)
assert.deepStrictEqual(rows, [{ station_id: VALID_STATION_ID, day: today }])
})
Expand Down

0 comments on commit f86658b

Please sign in to comment.