Cached Poll stats methods

This commit is contained in:
María Checa
2017-10-19 12:17:33 +02:00
parent 6e680c187f
commit f029cc2016

View File

@@ -16,110 +16,128 @@ class Poll
end end
private private
def total_participants def total_participants
total_participants_web + total_participants_booth stats_cache('total_participants') { total_participants_web + total_participants_booth }
end end
def total_participants_web def total_participants_web
total_web_valid + total_web_white + total_web_null stats_cache('total_participants_web') { total_web_valid + total_web_white + total_web_null }
end end
def total_participants_web_percentage def total_participants_web_percentage
(total_participants) == 0 ? 0 : total_participants_web * 100 / total_participants stats_cache('total_participants_web_percentage') {
(total_participants) == 0 ? 0 : total_participants_web * 100 / total_participants
}
end end
def total_participants_booth def total_participants_booth
voters.where(origin: 'booth').count stats_cache('total_participants_booth') { voters.where(origin: 'booth').count }
end end
def total_participants_booth_percentage def total_participants_booth_percentage
(total_participants) == 0 ? 0 : total_participants_booth * 100 / total_participants.to_f stats_cache('total_participants_booth_percentage') {
(total_participants) == 0 ? 0 : total_participants_booth * 100 / total_participants.to_f
}
end end
def total_web_valid def total_web_valid
voters.where(origin: 'web').count stats_cache('total_web_valid') { voters.where(origin: 'web').count }
end end
def valid_percentage_web def valid_percentage_web
(total_valid_votes) == 0 ? 0 : total_web_valid * 100 / total_valid_votes.to_f stats_cache('valid_percentage_web') {
(total_valid_votes) == 0 ? 0 : total_web_valid * 100 / total_valid_votes.to_f
}
end end
def total_web_white def total_web_white
0 stats_cache('total_web_white') { 0 }
end end
def white_percentage_web def white_percentage_web
0 stats_cache('white_percentage_web') { 0 }
end end
def total_web_null def total_web_null
0 stats_cache('total_web_null') { 0 }
end end
def null_percentage_web def null_percentage_web
0 stats_cache('null_percentage_web') { 0 }
end end
def total_booth_valid def total_booth_valid
recounts.sum(:total_amount) stats_cache('total_booth_valid') { recounts.sum(:total_amount) }
end end
def valid_percentage_booth def valid_percentage_booth
(total_valid_votes) == 0 ? 0 : total_booth_valid * 100 / total_valid_votes.to_f stats_cache('valid_percentage_booth') {
(total_valid_votes) == 0 ? 0 : total_booth_valid * 100 / total_valid_votes.to_f
}
end end
def total_booth_white def total_booth_white
recounts.sum(:white_amount) stats_cache('total_booth_white') { recounts.sum(:white_amount) }
end end
def white_percentage_booth def white_percentage_booth
(total_white_votes) == 0 ? 0 : total_booth_white * 100 / total_white_votes.to_f stats_cache('white_percentage_booth') {
(total_white_votes) == 0 ? 0 : total_booth_white * 100 / total_white_votes.to_f
}
end end
def total_booth_null def total_booth_null
recounts.sum(:null_amount) stats_cache('total_booth_null') { recounts.sum(:null_amount) }
end end
def null_percentage_booth def null_percentage_booth
(total_null_votes == 0) ? 0 : total_booth_null * 100 / total_null_votes.to_f stats_cache('null_percentage_booth') {
(total_null_votes == 0) ? 0 : total_booth_null * 100 / total_null_votes.to_f
}
end end
def total_valid_votes def total_valid_votes
total_web_valid + total_booth_valid stats_cache('total_valid_votes') { total_web_valid + total_booth_valid }
end end
def total_valid_percentage def total_valid_percentage
(total_participants) == 0 ? 0 : total_valid_votes * 100 / total_participants.to_f stats_cache('total_valid_percentage'){
(total_participants) == 0 ? 0 : total_valid_votes * 100 / total_participants.to_f
}
end end
def total_white_votes def total_white_votes
total_web_white + total_booth_white stats_cache('total_white_votes') { total_web_white + total_booth_white }
end end
def total_white_percentage def total_white_percentage
(total_participants) == 0 ? 0 : total_white_votes * 100 / total_participants.to_f stats_cache('total_white_percentage') {
(total_participants) == 0 ? 0 : total_white_votes * 100 / total_participants.to_f
}
end end
def total_null_votes def total_null_votes
total_web_null + total_booth_null stats_cache('total_null_votes') { total_web_null + total_booth_null }
end end
def total_null_percentage def total_null_percentage
(total_participants) == 0 ? 0 : total_null_votes * 100 / total_participants.to_f stats_cache('total_null_percentage') {
(total_participants) == 0 ? 0 : total_null_votes * 100 / total_participants.to_f
}
end end
def voters def voters
@poll.voters stats_cache('voters') { @poll.voters }
end end
def recounts def 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}/v7", &block) Rails.cache.fetch("polls_stats/#{@poll.id}/#{key}", &block)
end end
end end
end end