Skip to content

Commit

Permalink
Use budget stats model to calculate admin stats
Browse files Browse the repository at this point in the history
This way we reduce duplication.
  • Loading branch information
javierm committed Feb 20, 2023
1 parent b536a7c commit e51e034
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 19 deletions.
16 changes: 11 additions & 5 deletions app/components/admin/stats/budget_balloting_component.rb
Expand Up @@ -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
Expand All @@ -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
23 changes: 9 additions & 14 deletions app/components/admin/stats/budget_supporting_component.rb
Expand Up @@ -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
4 changes: 4 additions & 0 deletions app/models/budget/stats.rb
Expand Up @@ -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
Expand Down

0 comments on commit e51e034

Please sign in to comment.