diff --git a/app/components/admin/stats/budget_balloting_component.rb b/app/components/admin/stats/budget_balloting_component.rb index 4d604cca3..9b9065c44 100644 --- a/app/components/admin/stats/budget_balloting_component.rb +++ b/app/components/admin/stats/budget_balloting_component.rb @@ -7,12 +7,20 @@ class Admin::Stats::BudgetBallotingComponent < ApplicationComponent 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 @@ class Admin::Stats::BudgetBallotingComponent < ApplicationComponent 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 5dde0b4af..249deed5c 100644 --- a/app/components/admin/stats/budget_supporting_component.rb +++ b/app/components/admin/stats/budget_supporting_component.rb @@ -7,30 +7,25 @@ class Admin::Stats::BudgetSupportingComponent < ApplicationComponent 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 2094bb1e8..bbf01de17 100644 --- a/app/models/budget/stats.rb +++ b/app/models/budget/stats.rb @@ -53,6 +53,10 @@ class Budget::Stats budget.investments.count end + def total_supports + supports(budget).count + end + def total_votes budget.ballots.pluck(:ballot_lines_count).sum end