feat(speedtest): results table + repo

This commit is contained in:
root
2026-06-02 22:48:03 +10:00
parent 449e849f4d
commit c59d2407ed
3 changed files with 36 additions and 0 deletions

View File

@@ -0,0 +1,9 @@
-- 013_speedtest.sql
CREATE TABLE speedtest_results (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
down_mbps numeric NOT NULL,
up_mbps numeric NOT NULL,
ping_ms numeric,
ran_at timestamptz NOT NULL DEFAULT now()
);
CREATE INDEX idx_speedtest_ran_at ON speedtest_results (ran_at DESC);

12
lib/db/repos/speedtest.js Normal file
View File

@@ -0,0 +1,12 @@
import { pool } from '../pool.js';
export async function record({ down_mbps, up_mbps, ping_ms = null }) {
const { rows } = await pool.query(
`INSERT INTO speedtest_results (down_mbps, up_mbps, ping_ms) VALUES ($1,$2,$3) RETURNING *`,
[down_mbps, up_mbps, ping_ms]);
return rows[0];
}
export async function history(limit = 30) {
const { rows } = await pool.query(
`SELECT * FROM speedtest_results ORDER BY ran_at DESC LIMIT $1`, [limit]);
return rows;
}

View File

@@ -0,0 +1,15 @@
import { describe, it, expect, beforeAll } from 'vitest';
import { resetDb } from '../helpers/db.js';
import { migrateUp } from '../../lib/db/migrate.js';
import * as repo from '../../lib/db/repos/speedtest.js';
beforeAll(async () => { await resetDb(); await migrateUp(); });
describe('speedtest repo', () => {
it('records and lists newest-first', async () => {
await repo.record({ down_mbps: 100, up_mbps: 20, ping_ms: 8 });
await repo.record({ down_mbps: 110, up_mbps: 22, ping_ms: 7 });
const hist = await repo.history(30);
expect(hist.length).toBe(2);
expect(Number(hist[0].down_mbps)).toBe(110); // newest first
});
});