Revert "Extract class to manage GeozoneStats"
Back in commit383909e16, we said: > Even if this class looks very simple now, we're trying a few things > related to these stats. Having a class for it makes future changes > easier and, if there weren't any future changes, at least it makes > current experiments easier. Since there haven't been any changes in the last 5 years and we've found cases where using the GeozoneStats class results in a slightly worse performance, we're removing this class. The code is now a bit easier to read, and is consistent with the way we calculate participants by age. This reverts commit383909e16.
This commit is contained in:
@@ -1,24 +0,0 @@
|
|||||||
class GeozoneStats
|
|
||||||
attr_reader :geozone, :participants
|
|
||||||
|
|
||||||
def initialize(geozone, participants)
|
|
||||||
@geozone = geozone
|
|
||||||
@participants = participants
|
|
||||||
end
|
|
||||||
|
|
||||||
def geozone_participants
|
|
||||||
participants.where(geozone: geozone)
|
|
||||||
end
|
|
||||||
|
|
||||||
def name
|
|
||||||
geozone.name
|
|
||||||
end
|
|
||||||
|
|
||||||
def count
|
|
||||||
geozone_participants.count
|
|
||||||
end
|
|
||||||
|
|
||||||
def percentage
|
|
||||||
PercentageCalculator.calculate(count, participants.count)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
@@ -1,7 +0,0 @@
|
|||||||
module PercentageCalculator
|
|
||||||
def self.calculate(fraction, total)
|
|
||||||
return 0.0 if total.zero?
|
|
||||||
|
|
||||||
(fraction * 100.0 / total).round(3)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
@@ -114,19 +114,23 @@ module Statisticable
|
|||||||
end
|
end
|
||||||
|
|
||||||
def participants_by_geozone
|
def participants_by_geozone
|
||||||
geozone_stats.to_h do |stats|
|
geozones.to_h do |geozone|
|
||||||
|
count = participants.where(geozone: geozone).count
|
||||||
|
|
||||||
[
|
[
|
||||||
stats.name,
|
geozone.name,
|
||||||
{
|
{
|
||||||
count: stats.count,
|
count: count,
|
||||||
percentage: stats.percentage
|
percentage: calculate_percentage(count, total_participants)
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def calculate_percentage(fraction, total)
|
def calculate_percentage(fraction, total)
|
||||||
PercentageCalculator.calculate(fraction, total)
|
return 0.0 if total.zero?
|
||||||
|
|
||||||
|
(fraction * 100.0 / total).round(3)
|
||||||
end
|
end
|
||||||
|
|
||||||
def version
|
def version
|
||||||
@@ -178,10 +182,6 @@ module Statisticable
|
|||||||
Geozone.order("name")
|
Geozone.order("name")
|
||||||
end
|
end
|
||||||
|
|
||||||
def geozone_stats
|
|
||||||
geozones.map { |geozone| GeozoneStats.new(geozone, participants) }
|
|
||||||
end
|
|
||||||
|
|
||||||
def range_description(start, finish)
|
def range_description(start, finish)
|
||||||
if finish > 200
|
if finish > 200
|
||||||
I18n.t("stats.age_more_than", start: start)
|
I18n.t("stats.age_more_than", start: start)
|
||||||
|
|||||||
@@ -1,44 +0,0 @@
|
|||||||
require "rails_helper"
|
|
||||||
|
|
||||||
describe GeozoneStats do
|
|
||||||
let(:winterfell) { create(:geozone, name: "Winterfell") }
|
|
||||||
let(:riverlands) { create(:geozone, name: "Riverlands") }
|
|
||||||
|
|
||||||
describe "#name" do
|
|
||||||
let(:stats) { GeozoneStats.new(winterfell, []) }
|
|
||||||
|
|
||||||
it "returns the geozone name" do
|
|
||||||
expect(stats.name).to eq "Winterfell"
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
describe "#count" do
|
|
||||||
before do
|
|
||||||
2.times { create(:user, geozone: winterfell) }
|
|
||||||
1.times { create(:user, geozone: riverlands) }
|
|
||||||
end
|
|
||||||
|
|
||||||
let(:winterfell_stats) { GeozoneStats.new(winterfell, User.all) }
|
|
||||||
let(:riverlands_stats) { GeozoneStats.new(riverlands, User.all) }
|
|
||||||
|
|
||||||
it "counts participants from the geozone" do
|
|
||||||
expect(winterfell_stats.count).to eq 2
|
|
||||||
expect(riverlands_stats.count).to eq 1
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
describe "#percentage" do
|
|
||||||
before do
|
|
||||||
2.times { create(:user, geozone: winterfell) }
|
|
||||||
1.times { create(:user, geozone: riverlands) }
|
|
||||||
end
|
|
||||||
|
|
||||||
let(:winterfell_stats) { GeozoneStats.new(winterfell, User.all) }
|
|
||||||
let(:riverlands_stats) { GeozoneStats.new(riverlands, User.all) }
|
|
||||||
|
|
||||||
it "calculates percentage relative to the amount of participants" do
|
|
||||||
expect(winterfell_stats.percentage).to eq 66.667
|
|
||||||
expect(riverlands_stats.percentage).to eq 33.333
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
Reference in New Issue
Block a user