From 7a79c36137f3b972fbc1b5783ac6aadce38c8b52 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Wed, 20 Mar 2019 13:55:49 +0100 Subject: [PATCH] Select only distinct voters/balloters in stats It is way more efficient because we're caching the result of that method, and this way we only store each voter once in the cache. We were storing many voters several times and then we were filtering them with `uniq`. --- app/models/budget/stats.rb | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/app/models/budget/stats.rb b/app/models/budget/stats.rb index a61013fe6..7cd4d40f1 100644 --- a/app/models/budget/stats.rb +++ b/app/models/budget/stats.rb @@ -18,7 +18,7 @@ class Budget::Stats end def total_participants_support_phase - voters.uniq.count + voters.count end def total_participants_vote_phase @@ -71,11 +71,11 @@ class Budget::Stats end def voters - supports(budget).pluck(:voter_id) + supports(budget).distinct.pluck(:voter_id) end def balloters - budget.ballots.where("ballot_lines_count > ?", 0).pluck(:user_id).compact + budget.ballots.where("ballot_lines_count > ?", 0).distinct.pluck(:user_id).compact end def poll_ballot_voters @@ -84,21 +84,23 @@ class Budget::Stats def balloters_by_heading(heading_id) stats_cache("balloters_by_heading_#{heading_id}") do - budget.ballots.joins(:lines).where(budget_ballot_lines: {heading_id: heading_id}).pluck(:user_id) + budget.ballots.joins(:lines) + .where(budget_ballot_lines: { heading_id: heading_id} ) + .distinct.pluck(:user_id) end end def voters_by_heading(heading) stats_cache("voters_by_heading_#{heading.id}") do - supports(heading).pluck(:voter_id) + supports(heading).distinct.pluck(:voter_id) end end def calculate_heading_totals(heading) { total_investments_count: heading.investments.count, - total_participants_support_phase: voters_by_heading(heading).uniq.count, - total_participants_vote_phase: balloters_by_heading(heading.id).uniq.count, + total_participants_support_phase: voters_by_heading(heading).count, + total_participants_vote_phase: balloters_by_heading(heading.id).count, total_participants_all_phase: voters_and_balloters_by_heading(heading) } end