Add a common concern for budget and poll stats

This commit is contained in:
Javi Martín
2018-12-10 14:15:31 +01:00
parent b8dbdaf9a7
commit 62a97f9003
3 changed files with 343 additions and 340 deletions

View File

@@ -1,9 +1,6 @@
class Budget
class Stats
def initialize(budget)
@budget = budget
end
class Budget::Stats
include Statisticable
alias_method :budget, :resource
def generate
stats = %w[total_participants total_participants_support_phase total_participants_vote_phase
@@ -39,19 +36,19 @@ class Budget
end
def total_budget_investments
stats_cache("total_budget_investments") { @budget.investments.count }
stats_cache("total_budget_investments") { budget.investments.count }
end
def total_votes
stats_cache("total_votes") { @budget.ballots.pluck(:ballot_lines_count).inject(0) { |sum, x| sum + x } }
stats_cache("total_votes") { budget.ballots.pluck(:ballot_lines_count).inject(0) { |sum, x| sum + x } }
end
def total_selected_investments
stats_cache("total_selected_investments") { @budget.investments.selected.count }
stats_cache("total_selected_investments") { budget.investments.selected.count }
end
def total_unfeasible_investments
stats_cache("total_unfeasible_investments") { @budget.investments.unfeasible.count }
stats_cache("total_unfeasible_investments") { budget.investments.unfeasible.count }
end
def total_male_participants
@@ -63,7 +60,7 @@ class Budget
end
def total_supports
stats_cache("total_supports") { supports(@budget).count }
stats_cache("total_supports") { supports(budget).count }
end
def total_unknown_gender_or_age
@@ -113,20 +110,20 @@ class Budget
end
def authors
stats_cache("authors") { @budget.investments.pluck(:author_id) }
stats_cache("authors") { budget.investments.pluck(:author_id) }
end
def voters
stats_cache("voters") { supports(@budget).pluck(:voter_id) }
stats_cache("voters") { supports(budget).pluck(:voter_id) }
end
def balloters
stats_cache("balloters") { @budget.ballots.where("ballot_lines_count > ?", 0).pluck(:user_id) }
stats_cache("balloters") { budget.ballots.where("ballot_lines_count > ?", 0).pluck(:user_id) }
end
def poll_ballot_voters
stats_cache("poll_ballot_voters") do
@budget&.poll ? @budget.poll.voters.pluck(:user_id) : []
budget&.poll ? budget.poll.voters.pluck(:user_id) : []
end
end
@@ -136,7 +133,7 @@ class Budget
def balloters_by_heading(heading_id)
stats_cache("balloters_by_heading_#{heading_id}") do
@budget.ballots.joins(:lines).where(budget_ballot_lines: {heading_id: heading_id}).pluck(:user_id)
budget.ballots.joins(:lines).where(budget_ballot_lines: {heading_id: heading_id}).pluck(:user_id)
end
end
@@ -149,7 +146,7 @@ class Budget
def headings
stats_cache("headings") do
groups = Hash.new(0)
@budget.headings.order("id ASC").each do |heading|
budget.headings.order("id ASC").each do |heading|
groups[heading.id] = Hash.new(0).merge(calculate_heading_totals(heading))
end
@@ -159,7 +156,7 @@ class Budget
groups[:total][:total_participants_vote_phase] = groups.collect {|_k, v| v[:total_participants_vote_phase]}.sum
groups[:total][:total_participants_all_phase] = groups.collect {|_k, v| v[:total_participants_all_phase]}.sum
@budget.headings.each do |heading|
budget.headings.each do |heading|
groups[heading.id].merge!(calculate_heading_stats_with_totals(groups[heading.id], groups[:total], heading.population))
end
@@ -214,7 +211,6 @@ class Budget
end
def stats_cache(key, &block)
Rails.cache.fetch("budgets_stats/#{@budget.id}/#{key}/v10", &block)
end
Rails.cache.fetch("budgets_stats/#{budget.id}/#{key}/v10", &block)
end
end

View File

@@ -0,0 +1,11 @@
module Statisticable
extend ActiveSupport::Concern
included do
attr_reader :resource
def initialize(resource)
@resource = resource
end
end
end

View File

@@ -1,10 +1,7 @@
class Poll
class Stats
class Poll::Stats
include Statisticable
include StatsHelper
def initialize(poll)
@poll = poll
end
alias_method :poll, :resource
def generate
stats = %w[total_participants total_participants_web total_web_valid total_web_white total_web_null
@@ -111,16 +108,15 @@ class Poll
end
def voters
stats_cache("voters") { @poll.voters }
stats_cache("voters") { poll.voters }
end
def recounts
stats_cache("recounts") { @poll.recounts }
stats_cache("recounts") { poll.recounts }
end
def stats_cache(key, &block)
Rails.cache.fetch("polls_stats/#{@poll.id}/#{key}", &block)
Rails.cache.fetch("polls_stats/#{poll.id}/#{key}/v12", &block)
end
end
end