fix(api): move FILTER onto the aggregate in /api/stats
CI/CD Pipeline / test (push) Successful in 1m38s
CI/CD Pipeline / build-push-deploy (push) Successful in 23s

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.
This commit is contained in:
2026-08-11 10:28:23 +00:00
parent b5d444d326
commit 1939c36dfb
+6 -3
View File
@@ -336,9 +336,12 @@ app.get('/api/stats', async (req, res) => {
SELECT SELECT
COUNT(*) FILTER (WHERE is_alive = true) as alive_count, COUNT(*) FILTER (WHERE is_alive = true) as alive_count,
COUNT(*) FILTER (WHERE is_alive = false) as dead_count, COUNT(*) FILTER (WHERE is_alive = false) as dead_count,
ROUND(AVG(hunger)::numeric, 1) FILTER (WHERE is_alive = true) as avg_hunger, -- FILTER ne s'applique qu'a un agregat : accroche a ROUND, qui n'en
ROUND(AVG(happiness)::numeric, 1) FILTER (WHERE is_alive = true) as avg_happiness, -- est pas un, PostgreSQL rejette toute la requete. Il doit porter sur
ROUND(AVG(energy)::numeric, 1) FILTER (WHERE is_alive = true) as avg_energy, -- 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 hunger > 80 AND is_alive = true) as starving_count,
COUNT(*) FILTER (WHERE happiness < 20 AND is_alive = true) as sad_count COUNT(*) FILTER (WHERE happiness < 20 AND is_alive = true) as sad_count
FROM creatures FROM creatures