Use budget stats model to calculate admin stats

This way we reduce duplication.
This commit is contained in:
Javi Martín
2023-02-06 16:58:15 +01:00
parent b536a7cb77
commit e51e034468
3 changed files with 24 additions and 19 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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