Add a common concern for budget and poll stats
This commit is contained in:
@@ -1,9 +1,6 @@
|
|||||||
class Budget
|
class Budget::Stats
|
||||||
class Stats
|
include Statisticable
|
||||||
|
alias_method :budget, :resource
|
||||||
def initialize(budget)
|
|
||||||
@budget = budget
|
|
||||||
end
|
|
||||||
|
|
||||||
def generate
|
def generate
|
||||||
stats = %w[total_participants total_participants_support_phase total_participants_vote_phase
|
stats = %w[total_participants total_participants_support_phase total_participants_vote_phase
|
||||||
@@ -39,19 +36,19 @@ class Budget
|
|||||||
end
|
end
|
||||||
|
|
||||||
def total_budget_investments
|
def total_budget_investments
|
||||||
stats_cache("total_budget_investments") { @budget.investments.count }
|
stats_cache("total_budget_investments") { budget.investments.count }
|
||||||
end
|
end
|
||||||
|
|
||||||
def total_votes
|
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
|
end
|
||||||
|
|
||||||
def total_selected_investments
|
def total_selected_investments
|
||||||
stats_cache("total_selected_investments") { @budget.investments.selected.count }
|
stats_cache("total_selected_investments") { budget.investments.selected.count }
|
||||||
end
|
end
|
||||||
|
|
||||||
def total_unfeasible_investments
|
def total_unfeasible_investments
|
||||||
stats_cache("total_unfeasible_investments") { @budget.investments.unfeasible.count }
|
stats_cache("total_unfeasible_investments") { budget.investments.unfeasible.count }
|
||||||
end
|
end
|
||||||
|
|
||||||
def total_male_participants
|
def total_male_participants
|
||||||
@@ -63,7 +60,7 @@ class Budget
|
|||||||
end
|
end
|
||||||
|
|
||||||
def total_supports
|
def total_supports
|
||||||
stats_cache("total_supports") { supports(@budget).count }
|
stats_cache("total_supports") { supports(budget).count }
|
||||||
end
|
end
|
||||||
|
|
||||||
def total_unknown_gender_or_age
|
def total_unknown_gender_or_age
|
||||||
@@ -113,20 +110,20 @@ class Budget
|
|||||||
end
|
end
|
||||||
|
|
||||||
def authors
|
def authors
|
||||||
stats_cache("authors") { @budget.investments.pluck(:author_id) }
|
stats_cache("authors") { budget.investments.pluck(:author_id) }
|
||||||
end
|
end
|
||||||
|
|
||||||
def voters
|
def voters
|
||||||
stats_cache("voters") { supports(@budget).pluck(:voter_id) }
|
stats_cache("voters") { supports(budget).pluck(:voter_id) }
|
||||||
end
|
end
|
||||||
|
|
||||||
def balloters
|
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
|
end
|
||||||
|
|
||||||
def poll_ballot_voters
|
def poll_ballot_voters
|
||||||
stats_cache("poll_ballot_voters") do
|
stats_cache("poll_ballot_voters") do
|
||||||
@budget&.poll ? @budget.poll.voters.pluck(:user_id) : []
|
budget&.poll ? budget.poll.voters.pluck(:user_id) : []
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -136,7 +133,7 @@ class Budget
|
|||||||
|
|
||||||
def balloters_by_heading(heading_id)
|
def balloters_by_heading(heading_id)
|
||||||
stats_cache("balloters_by_heading_#{heading_id}") do
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -149,7 +146,7 @@ class Budget
|
|||||||
def headings
|
def headings
|
||||||
stats_cache("headings") do
|
stats_cache("headings") do
|
||||||
groups = Hash.new(0)
|
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))
|
groups[heading.id] = Hash.new(0).merge(calculate_heading_totals(heading))
|
||||||
end
|
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_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
|
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))
|
groups[heading.id].merge!(calculate_heading_stats_with_totals(groups[heading.id], groups[:total], heading.population))
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -214,7 +211,6 @@ class Budget
|
|||||||
end
|
end
|
||||||
|
|
||||||
def stats_cache(key, &block)
|
def stats_cache(key, &block)
|
||||||
Rails.cache.fetch("budgets_stats/#{@budget.id}/#{key}/v10", &block)
|
Rails.cache.fetch("budgets_stats/#{budget.id}/#{key}/v10", &block)
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
11
app/models/concerns/statisticable.rb
Normal file
11
app/models/concerns/statisticable.rb
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
module Statisticable
|
||||||
|
extend ActiveSupport::Concern
|
||||||
|
|
||||||
|
included do
|
||||||
|
attr_reader :resource
|
||||||
|
|
||||||
|
def initialize(resource)
|
||||||
|
@resource = resource
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -1,10 +1,7 @@
|
|||||||
class Poll
|
class Poll::Stats
|
||||||
class Stats
|
include Statisticable
|
||||||
include StatsHelper
|
include StatsHelper
|
||||||
|
alias_method :poll, :resource
|
||||||
def initialize(poll)
|
|
||||||
@poll = poll
|
|
||||||
end
|
|
||||||
|
|
||||||
def generate
|
def generate
|
||||||
stats = %w[total_participants total_participants_web total_web_valid total_web_white total_web_null
|
stats = %w[total_participants total_participants_web total_web_valid total_web_white total_web_null
|
||||||
@@ -111,16 +108,15 @@ class Poll
|
|||||||
end
|
end
|
||||||
|
|
||||||
def voters
|
def voters
|
||||||
stats_cache("voters") { @poll.voters }
|
stats_cache("voters") { poll.voters }
|
||||||
end
|
end
|
||||||
|
|
||||||
def recounts
|
def recounts
|
||||||
stats_cache("recounts") { @poll.recounts }
|
stats_cache("recounts") { poll.recounts }
|
||||||
end
|
end
|
||||||
|
|
||||||
def stats_cache(key, &block)
|
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
|
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user