feat(health): service_status cache table + repo
This commit is contained in:
8
lib/db/migrations/014_service_status.sql
Normal file
8
lib/db/migrations/014_service_status.sql
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
-- 014_service_status.sql
|
||||||
|
CREATE TABLE service_status (
|
||||||
|
service_id text PRIMARY KEY,
|
||||||
|
status text NOT NULL CHECK (status IN ('ok','warn','down','unknown')),
|
||||||
|
latency_ms integer,
|
||||||
|
detail text,
|
||||||
|
checked_at timestamptz NOT NULL DEFAULT now()
|
||||||
|
);
|
||||||
14
lib/db/repos/service_status.js
Normal file
14
lib/db/repos/service_status.js
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
import { pool } from '../pool.js';
|
||||||
|
export async function upsert({ service_id, status, latency_ms = null, detail = null }) {
|
||||||
|
await pool.query(
|
||||||
|
`INSERT INTO service_status (service_id, status, latency_ms, detail, checked_at)
|
||||||
|
VALUES ($1,$2,$3,$4, now())
|
||||||
|
ON CONFLICT (service_id) DO UPDATE
|
||||||
|
SET status=EXCLUDED.status, latency_ms=EXCLUDED.latency_ms,
|
||||||
|
detail=EXCLUDED.detail, checked_at=now()`,
|
||||||
|
[service_id, status, latency_ms, detail]);
|
||||||
|
}
|
||||||
|
export async function all() {
|
||||||
|
const { rows } = await pool.query(`SELECT * FROM service_status`);
|
||||||
|
return rows;
|
||||||
|
}
|
||||||
14
tests/repos/service_status.test.js
Normal file
14
tests/repos/service_status.test.js
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
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/service_status.js';
|
||||||
|
|
||||||
|
beforeAll(async () => { await resetDb(); await migrateUp(); });
|
||||||
|
describe('service_status repo', () => {
|
||||||
|
it('upserts and reads all', async () => {
|
||||||
|
await repo.upsert({ service_id: 'gitea', status: 'ok', latency_ms: 12, detail: '200' });
|
||||||
|
await repo.upsert({ service_id: 'gitea', status: 'down', latency_ms: null, detail: 'ECONN' });
|
||||||
|
const all = await repo.all();
|
||||||
|
expect(all.find(r => r.service_id === 'gitea').status).toBe('down');
|
||||||
|
});
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user