feat(health): service_status cache table + repo

This commit is contained in:
root
2026-06-02 22:53:39 +10:00
parent 3ea34d9907
commit 5b05fd4730
3 changed files with 36 additions and 0 deletions

View 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()
);

View 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;
}

View 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');
});
});