From 1939c36dfbe59028cb86483215d830bdb574ae2c Mon Sep 17 00:00:00 2001 From: Khalil Aliouich Date: Tue, 11 Aug 2026 10:28:23 +0000 Subject: [PATCH] fix(api): move FILTER onto the aggregate in /api/stats PostgreSQL only accepts FILTER on an aggregate call. It was attached to ROUND, so the whole query was rejected and /api/stats answered 500. The showcase reads that endpoint for its live creature stats, which meant the homepage displayed five dashes instead of numbers. FILTER now sits on AVG, with the rounding applied to the result. --- api/server.js | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/api/server.js b/api/server.js index 22c0ad7..2603b6d 100644 --- a/api/server.js +++ b/api/server.js @@ -336,9 +336,12 @@ app.get('/api/stats', async (req, res) => { SELECT COUNT(*) FILTER (WHERE is_alive = true) as alive_count, COUNT(*) FILTER (WHERE is_alive = false) as dead_count, - ROUND(AVG(hunger)::numeric, 1) FILTER (WHERE is_alive = true) as avg_hunger, - ROUND(AVG(happiness)::numeric, 1) FILTER (WHERE is_alive = true) as avg_happiness, - ROUND(AVG(energy)::numeric, 1) FILTER (WHERE is_alive = true) as avg_energy, + -- FILTER ne s'applique qu'a un agregat : accroche a ROUND, qui n'en + -- est pas un, PostgreSQL rejette toute la requete. Il doit porter sur + -- AVG, et l'arrondi vient ensuite. + ROUND(AVG(hunger) FILTER (WHERE is_alive = true)::numeric, 1) as avg_hunger, + ROUND(AVG(happiness) FILTER (WHERE is_alive = true)::numeric, 1) as avg_happiness, + ROUND(AVG(energy) FILTER (WHERE is_alive = true)::numeric, 1) as avg_energy, COUNT(*) FILTER (WHERE hunger > 80 AND is_alive = true) as starving_count, COUNT(*) FILTER (WHERE happiness < 20 AND is_alive = true) as sad_count FROM creatures