Skip to content

Commit 03fcc30

Browse files
Simplify unused sample DB query (#2393)
After what we discussed with @ggiraldez in #2391
1 parent c28a70e commit 03fcc30

1 file changed

Lines changed: 5 additions & 12 deletions

File tree

‎lib/ask_web/controllers/survey_controller.ex‎

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -66,19 +66,12 @@ defmodule AskWeb.SurveyController do
6666
Repo.all(
6767
from s in Survey,
6868
left_join: r in Respondent,
69-
on: r.survey_id == s.id,
69+
on: r.survey_id == s.id and r.disposition == :registered,
7070
where: s.project_id == ^project.id and s.state == :terminated,
71-
# we could mix a `count` with a `where` clause filtering for respondent disposition
72-
# instead of doing the sum+if, but that wouldn't return surveys with 0 respondents available
73-
select: %{survey_id: s.id, name: s.name, ended_at: s.ended_at, respondents: sum(fragment("if(?, ?, ?)", r.disposition == :registered, 1, 0))},
74-
group_by: [s.id]
75-
) |> Enum.map(fn s -> %{
76-
survey_id: s.survey_id,
77-
name: s.name,
78-
ended_at: s.ended_at,
79-
respondents: s.respondents |> Decimal.to_integer
80-
} end)
81-
|> Enum.sort_by(fn s -> - s.respondents end)
71+
select: %{survey_id: s.id, name: s.name, ended_at: s.ended_at, respondents: count(r.id)},
72+
group_by: [s.id],
73+
order_by: [desc: count(r.id)]
74+
)
8275

8376
render(conn, "unused_sample.json", surveys: surveys)
8477
end

0 commit comments

Comments
 (0)