Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions lib/ask/respondent_stats.ex
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,6 @@ defmodule Ask.RespondentStats do
end
end

{:not_in_list, _, [value]} ->
quote do
unquote(quoted)
|> where([s], field(s, unquote(field)) not in unquote(value))
end

value ->
quote do
unquote(quoted)
Expand Down
27 changes: 13 additions & 14 deletions lib/ask_web/controllers/respondent_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -209,18 +209,14 @@ defmodule AskWeb.RespondentController do
defp stats(conn, survey, _) do
buckets = (survey |> Repo.preload(:quota_buckets)).quota_buckets

empty_bucket_ids =
buckets |> Enum.filter(fn bucket -> bucket.quota in [0, nil] end) |> Enum.map(& &1.id)

buckets = buckets |> Enum.reject(fn bucket -> bucket.quota in [0, nil] end)
respondent_count = Ask.RespondentStats.respondent_count(survey_id: ^survey.id)

stats(
conn,
survey,
respondent_count,
respondents_by_bucket_and_disposition(survey, empty_bucket_ids),
respondents_by_quota_bucket_and_completed_at(survey, empty_bucket_ids),
respondents_by_bucket_and_disposition(survey),
respondents_by_quota_bucket_and_completed_at(survey),
buckets,
buckets,
"quotas_stats.json"
Expand Down Expand Up @@ -276,7 +272,7 @@ defmodule AskWeb.RespondentController do
cumulative_percentages:
cumulative_percentages(references, grouped_respondents, survey, target, buckets),
percentages: percentages(survey),
completion_percentage: completed_or_partial / target * 100,
completion_percentage: percentage_or_100(completed_or_partial, target),
total_respondents: total_respondents,
target: target,
attempted_respondents: attempted_respondents
Expand Down Expand Up @@ -424,17 +420,21 @@ defmodule AskWeb.RespondentController do
|> Enum.into(%{})
end

defp percentage_or_100(_dividend, nil), do: 100.0
defp percentage_or_100(_dividend, 0), do: 100.0
defp percentage_or_100(dividend, divisor), do: dividend / divisor * 100

defp percent_provider(_group_id, [], target, []) do
fn count ->
count / target * 100
percentage_or_100(count, target)
end
end

defp percent_provider(group_id, [], _target, buckets) do
bucket = buckets |> Enum.find(fn bucket -> bucket.id == group_id end)

fn count ->
count / bucket.quota * 100
percentage_or_100(count, bucket.quota)
end
end

Expand All @@ -448,7 +448,7 @@ defmodule AskWeb.RespondentController do
end)

fn count ->
count / (comparison["ratio"] * target / 100) * 100
percentage_or_100(count, comparison["ratio"] * target / 100)
end
end

Expand Down Expand Up @@ -622,10 +622,9 @@ defmodule AskWeb.RespondentController do
Ask.RespondentStats.respondent_count(survey_id: ^survey.id, by: [:state, :disposition, :mode])
end

defp respondents_by_bucket_and_disposition(survey, bucket_ids) do
defp respondents_by_bucket_and_disposition(survey) do
Ask.RespondentStats.respondent_count(
survey_id: ^survey.id,
quota_bucket_id: not_in_list(^bucket_ids),
by: [:state, :disposition, :quota_bucket_id]
)
end
Expand Down Expand Up @@ -667,10 +666,10 @@ defmodule AskWeb.RespondentController do
end)
end

defp respondents_by_quota_bucket_and_completed_at(survey, bucket_ids) do
defp respondents_by_quota_bucket_and_completed_at(survey) do
Repo.all(
from r in CompletedRespondents,
where: r.survey_id == ^survey.id and r.quota_bucket_id not in ^bucket_ids,
where: r.survey_id == ^survey.id,
group_by: [r.quota_bucket_id, r.date],
order_by: r.date,
select: {r.quota_bucket_id, r.date, fragment("CAST(? AS UNSIGNED)", sum(r.count))}
Expand Down
20 changes: 14 additions & 6 deletions test/ask_web/controllers/respondent_controller_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -1427,7 +1427,7 @@ defmodule AskWeb.RespondentControllerTest do
},
%{
"condition" => [
%{"store" => "Smokes", "value" => "yes"},
%{"store" => "Smokes", "value" => "Yes"},
%{"store" => "Exercises", "value" => "No"}
],
"quota" => nil,
Expand Down Expand Up @@ -1460,6 +1460,10 @@ defmodule AskWeb.RespondentControllerTest do
from(q in QuotaBucket, where: q.condition == ^%{"Exercises" => "Yes", "Smokes" => "No"})
|> Repo.one()

qb3 =
from(q in QuotaBucket, where: q.condition == ^%{"Exercises" => "No", "Smokes" => "Yes"})
|> Repo.one()

qb4 =
from(q in QuotaBucket, where: q.condition == ^%{"Exercises" => "Yes", "Smokes" => "Yes"})
|> Repo.one()
Expand Down Expand Up @@ -1503,7 +1507,9 @@ defmodule AskWeb.RespondentControllerTest do
assert json_response(conn, 200) == %{
"data" => %{
"reference" => [
%{"name" => "Smokes: No - Exercises: No", "id" => qb1.id},
%{"name" => "Smokes: No - Exercises: Yes", "id" => qb2.id},
%{"name" => "Smokes: Yes - Exercises: No", "id" => qb3.id},
%{"name" => "Smokes: Yes - Exercises: Yes", "id" => qb4.id}
],
"completion_percentage" => 30.0,
Expand All @@ -1516,7 +1522,9 @@ defmodule AskWeb.RespondentControllerTest do
]
},
"cumulative_percentages" => %{
to_string(qb1.id) => [%{"date" => "2016-01-01", "percent" => 100.0}],
to_string(qb2.id) => [%{"date" => "2016-01-01", "percent" => 0.0}],
to_string(qb3.id) => [%{"date" => "2016-01-01", "percent" => 100.0}],
to_string(qb4.id) => [%{"date" => "2016-01-01", "percent" => 40.0}]
},
"id" => survey.id,
Expand All @@ -1534,13 +1542,13 @@ defmodule AskWeb.RespondentControllerTest do
"percent" => 20.0
},
"responsive" => %{
"count" => 2,
"count" => 3,
"detail" => %{
"breakoff" => %{"by_reference" => %{}, "count" => 0, "percent" => 0.0},
"completed" => %{
"by_reference" => %{"#{qb4.id}" => 2},
"count" => 2,
"percent" => 40.0
"by_reference" => %{"#{qb4.id}" => 2, "#{qb1.id}" => 1},
"count" => 3,
"percent" => 60.0
},
"ineligible" => %{"by_reference" => %{}, "count" => 0, "percent" => 0.0},
"partial" => %{"by_reference" => %{}, "count" => 0, "percent" => 0.0},
Expand All @@ -1553,7 +1561,7 @@ defmodule AskWeb.RespondentControllerTest do
"percent" => 0.0
}
},
"percent" => 40.0
"percent" => 60.0
},
"uncontacted" => %{
"count" => 1,
Expand Down