From 1797b79f7bde6d8670b4e762a1831deb0f5e78ba Mon Sep 17 00:00:00 2001 From: chemavx Date: Thu, 11 Jun 2026 19:55:48 +0000 Subject: [PATCH] fix(api): aggregate metrics history by day so days=42 spans days, not minutes Co-Authored-By: Claude Fable 5 --- bot/data/db.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/bot/data/db.py b/bot/data/db.py index 0f8bd9f..4a7f21a 100644 --- a/bot/data/db.py +++ b/bot/data/db.py @@ -330,9 +330,21 @@ class Database: return result async def get_metrics_history(self, days: int = 42) -> list[dict]: + """Return the closing snapshot of each UTC day, newest day first. + + metrics_daily receives one snapshot per trading cycle (~1/min), so a + plain LIMIT over raw rows would cover minutes, not days. DISTINCT ON + collapses each calendar day to its last snapshot, making `days` bound + actual days. history[0] remains the most recent snapshot overall. + """ async with self._pool.acquire() as conn: rows = await conn.fetch( - "SELECT * FROM metrics_daily ORDER BY timestamp DESC LIMIT $1", days + """ + SELECT DISTINCT ON (timestamp::date) * + FROM metrics_daily + ORDER BY timestamp::date DESC, timestamp DESC + LIMIT $1 + """, days ) return [dict(r) for r in rows]