-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Created daily_node_metrics table and received/stored station_id
- Loading branch information
1 parent
971848a
commit 38a3f2a
Showing
7 changed files
with
91 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import createDebug from 'debug' | ||
|
||
const debug = createDebug('spark:platform-stats') | ||
|
||
/** | ||
* @param {import('pg').Client} pgClient | ||
* @param {import('./preprocess').Measurement[]} honestMeasurements | ||
*/ | ||
export const updateDailyNodeMetrics = async (pgClient, honestMeasurements) => { | ||
debug('Updating daily node metrics, count=%s', honestMeasurements.length) | ||
for (const m of honestMeasurements) { | ||
await pgClient.query(` | ||
INSERT INTO daily_node_metrics (station_id, metric_date) | ||
VALUES ($1, now()::date) | ||
ON CONFLICT (station_id, metric_date) DO NOTHING | ||
`, [m.station_id]) // TODO: when we add more fields, we should update the ON CONFLICT clause to update the fields | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
CREATE TABLE daily_node_metrics ( | ||
metric_date DATE NOT NULL, | ||
station_id TEXT NOT NULL, | ||
PRIMARY KEY (metric_date, station_id) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import assert from 'node:assert' | ||
import pg from 'pg' | ||
import { beforeEach, describe, it } from 'mocha' | ||
|
||
import { DATABASE_URL } from '../lib/config.js' | ||
import { migrateWithPgClient } from '../lib/migrate.js' | ||
import { VALID_MEASUREMENT } from './helpers/test-data.js' | ||
import { updateDailyNodeMetrics } from '../lib/platform-stats.js' | ||
|
||
const createPgClient = async () => { | ||
const pgClient = new pg.Client({ connectionString: DATABASE_URL }) | ||
await pgClient.connect() | ||
return pgClient | ||
} | ||
|
||
describe('platform-stats', () => { | ||
let pgClient | ||
|
||
before(async () => { | ||
pgClient = await createPgClient() | ||
await migrateWithPgClient(pgClient) | ||
}) | ||
|
||
beforeEach(async () => { | ||
await pgClient.query('DELETE FROM daily_node_metrics') | ||
await pgClient.query('BEGIN TRANSACTION') | ||
}) | ||
|
||
afterEach(async () => { | ||
await pgClient.query('END TRANSACTION') | ||
}) | ||
|
||
after(async () => { | ||
await pgClient.end() | ||
}) | ||
|
||
it('updates daily node metrics with new measurements', async () => { | ||
const honestMeasurements = [ | ||
{ ...VALID_MEASUREMENT, station_id: 'station1' }, | ||
{ ...VALID_MEASUREMENT, station_id: 'station2' } | ||
] | ||
|
||
await updateDailyNodeMetrics(pgClient, honestMeasurements) | ||
|
||
const { rows } = await pgClient.query('SELECT station_id FROM daily_node_metrics') | ||
assert.strictEqual(rows.length, 2) | ||
assert.deepStrictEqual(rows.map(row => row.station_id).sort(), ['station1', 'station2']) | ||
}) | ||
|
||
it('ignores duplicate measurements for the same station on the same day', async () => { | ||
const honestMeasurements = [ | ||
{ ...VALID_MEASUREMENT, station_id: 'station1' }, | ||
{ ...VALID_MEASUREMENT, station_id: 'station1' } // Duplicate station_id | ||
] | ||
|
||
await updateDailyNodeMetrics(pgClient, honestMeasurements) | ||
|
||
const { rows } = await pgClient.query('SELECT station_id FROM daily_node_metrics') | ||
assert.strictEqual(rows.length, 1) | ||
assert.strictEqual(rows[0].station_id, 'station1') | ||
}) | ||
}) |