From f0729d8233d4b887d008408b36dc08e48cedaa7d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mat=C3=ADas=20Garc=C3=ADa=20Isa=C3=ADa?= Date: Wed, 3 Sep 2025 21:19:30 -0300 Subject: [PATCH] Simplify unused sample DB query After what we discussed with @ggiraldez in #2391 --- lib/ask_web/controllers/survey_controller.ex | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/lib/ask_web/controllers/survey_controller.ex b/lib/ask_web/controllers/survey_controller.ex index c2b012eb3..759f76d53 100644 --- a/lib/ask_web/controllers/survey_controller.ex +++ b/lib/ask_web/controllers/survey_controller.ex @@ -66,19 +66,12 @@ defmodule AskWeb.SurveyController do Repo.all( from s in Survey, left_join: r in Respondent, - on: r.survey_id == s.id, + on: r.survey_id == s.id and r.disposition == :registered, where: s.project_id == ^project.id and s.state == :terminated, - # we could mix a `count` with a `where` clause filtering for respondent disposition - # instead of doing the sum+if, but that wouldn't return surveys with 0 respondents available - select: %{survey_id: s.id, name: s.name, ended_at: s.ended_at, respondents: sum(fragment("if(?, ?, ?)", r.disposition == :registered, 1, 0))}, - group_by: [s.id] - ) |> Enum.map(fn s -> %{ - survey_id: s.survey_id, - name: s.name, - ended_at: s.ended_at, - respondents: s.respondents |> Decimal.to_integer - } end) - |> Enum.sort_by(fn s -> - s.respondents end) + select: %{survey_id: s.id, name: s.name, ended_at: s.ended_at, respondents: count(r.id)}, + group_by: [s.id], + order_by: [desc: count(r.id)] + ) render(conn, "unused_sample.json", surveys: surveys) end