import { Router, Request, Response } from "express"; import { getDbStats } from "../db/queries"; import { pool } from "../db/client"; export const healthRouter = Router(); // GET /api/health — Health check with DB stats healthRouter.get("/", async (_req: Request, res: Response) => { try { const start = Date.now(); const stats = await getDbStats(); const latencyMs = Date.now() - start; // Verification stats const verStats = await pool.query(` SELECT COUNT(*) FILTER (WHERE price_verified) AS price_verified, COUNT(*) FILTER (WHERE image_verified) AS image_verified, COUNT(*) FILTER (WHERE details_verified) AS details_verified, COUNT(*) FILTER (WHERE fully_verified) AS fully_verified, COUNT(*) AS total FROM transceivers `).catch(() => ({ rows: [{}] })); const v = verStats.rows[0] || {}; res.json({ success: true, status: "healthy", version: "0.3.0", uptime: process.uptime(), database: { connected: true, latency_ms: latencyMs, stats, }, verification: { price_verified: Number(v.price_verified || 0), image_verified: Number(v.image_verified || 0), details_verified: Number(v.details_verified || 0), fully_verified: Number(v.fully_verified || 0), total: Number(v.total || 0), price_coverage_pct: v.total ? Math.round(Number(v.price_verified) / Number(v.total) * 100) : 0, fully_verified_pct: v.total ? Math.round(Number(v.fully_verified) / Number(v.total) * 100) : 0, }, }); } catch (err) { res.status(503).json({ success: false, status: "unhealthy", database: { connected: false, error: String(err) }, }); } });