Files
grecia/app/components/admin/stats/budget_balloting_component.rb
Julian Herrero 16f844595d Don't use the cache in admin budget stats
In commit e51e03446, we started using the same code to show stats in the
public area and in the admin area. However, in doing so we introduced a
bug, since stats in the public area are only shown after a certain part
of the process has finished, meaning the stats appearing on the page
never change (in theory), so it's perfectly fine to cache them. However,
in the admin area stats can be accessed while the process is still
ongoing, so caching the stats will lead to the wrong results being
displayed.

We've thought about expiring the cache when new supports or ballot lines
are added; however, that means the methods calculating the stats for the
supporting phase would expire when supports are added/removed but the
methods calculating the stats for the voting phase would expire when
ballot lines are added/removed. It gets even more complex because the
`headings` method calculates stats for both the supporting and the
voting phases.

So, since loading stats in the admin section is fast even without the
cache because they only load very basic statistics, we're taking the
simple approach of disabling the cache in this case, so everything works
the same way it did before commit e51e03446.

Co-authored-by: Javi Martín <javim@elretirao.net>
2024-05-20 16:19:41 +02:00

36 lines
778 B
Ruby

class Admin::Stats::BudgetBallotingComponent < ApplicationComponent
attr_reader :budget
def initialize(budget)
@budget = budget
end
private
def stats
@stats ||= Budget::Stats.new(budget, cache: false)
end
def headings_stats
@headings_stats ||= stats.headings
end
def vote_count
stats.total_votes
end
def user_count
stats.total_participants_vote_phase
end
def vote_count_by_heading
budget.lines.group(:heading_id).count.map { |k, v| [Budget::Heading.find(k).name, v] }.sort
end
def user_count_by_heading
budget.headings.map do |heading|
[heading.name, headings_stats[heading.id][:total_participants_vote_phase]]
end.select { |_, count| count > 0 }.sort
end
end