From e51e034468044e6d23ba57010dc16a9bb0eecf36 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Mon, 6 Feb 2023 16:58:15 +0100 Subject: [PATCH] Use budget stats model to calculate admin stats This way we reduce duplication. --- .../admin/stats/budget_balloting_component.rb | 16 +++++++++---- .../stats/budget_supporting_component.rb | 23 ++++++++----------- app/models/budget/stats.rb | 4 ++++ 3 files changed, 24 insertions(+), 19 deletions(-) diff --git a/app/components/admin/stats/budget_balloting_component.rb b/app/components/admin/stats/budget_balloting_component.rb index 4d604cca3d4..9b9065c447c 100644 --- a/app/components/admin/stats/budget_balloting_component.rb +++ b/app/components/admin/stats/budget_balloting_component.rb @@ -7,12 +7,20 @@ def initialize(budget) private + def stats + @stats ||= Budget::Stats.new(budget) + end + + def headings_stats + @headings_stats ||= stats.headings + end + def vote_count - budget.lines.count + stats.total_votes end def user_count - budget.ballots.select { |ballot| ballot.lines.any? }.count + stats.total_participants_vote_phase end def vote_count_by_heading @@ -21,9 +29,7 @@ def vote_count_by_heading def user_count_by_heading budget.headings.map do |heading| - ballots = budget.ballots.joins(:lines).where(budget_ballot_lines: { heading_id: heading }) - - [heading.name, ballots.select(:user_id).distinct.count] + [heading.name, headings_stats[heading.id][:total_participants_vote_phase]] end.select { |_, count| count > 0 }.sort end end diff --git a/app/components/admin/stats/budget_supporting_component.rb b/app/components/admin/stats/budget_supporting_component.rb index 5dde0b4aff3..249deed5cb2 100644 --- a/app/components/admin/stats/budget_supporting_component.rb +++ b/app/components/admin/stats/budget_supporting_component.rb @@ -7,30 +7,25 @@ def initialize(budget) private - def votes - Vote.where(votable_type: "Budget::Investment") - .includes(:budget_investment) - .where(budget_investments: { heading_id: budget.heading_ids }) + def stats + @stats ||= Budget::Stats.new(budget) + end + + def headings_stats + @headings_stats ||= stats.headings end def vote_count - votes.count + stats.total_supports end def user_count - votes.select(:voter_id).distinct.count + stats.total_participants_support_phase end def user_count_by_heading budget.headings.map do |heading| - [heading, voters_in_heading(heading)] + [heading, headings_stats[heading.id][:total_participants_support_phase]] end end - - def voters_in_heading(heading) - Vote.where(votable_type: "Budget::Investment") - .includes(:budget_investment) - .where(budget_investments: { heading_id: heading.id }) - .select("votes.voter_id").distinct.count - end end diff --git a/app/models/budget/stats.rb b/app/models/budget/stats.rb index 2094bb1e863..bbf01de17c9 100644 --- a/app/models/budget/stats.rb +++ b/app/models/budget/stats.rb @@ -53,6 +53,10 @@ def total_budget_investments budget.investments.count end + def total_supports + supports(budget).count + end + def total_votes budget.ballots.pluck(:ballot_lines_count).sum end