Remove complex poll stats
For now we think showing them would be showing too much data and it would be a bit confusing. I've been tempted to just remove the view and keep the methods in the model in case they're used by other institutions using CONSUL. However, it's probably better to wait until we're asked to re-implement them, and in the meantime we don't maintain code nobody uses. The code wasn't that great to start with (I know it because I wrote it).
This commit is contained in:
@@ -33,7 +33,18 @@ module Statisticable
|
||||
end
|
||||
|
||||
def participants_by_age
|
||||
participants_by_age_for(participants)
|
||||
age_groups.map do |start, finish|
|
||||
count = participants.between_ages(start, finish).count
|
||||
|
||||
[
|
||||
"#{start} - #{finish}",
|
||||
{
|
||||
range: range_description(start, finish),
|
||||
count: count,
|
||||
percentage: calculate_percentage(count, total_participants)
|
||||
}
|
||||
]
|
||||
end.to_h
|
||||
end
|
||||
|
||||
def participants_by_geozone
|
||||
@@ -79,26 +90,6 @@ module Statisticable
|
||||
]
|
||||
end
|
||||
|
||||
def participants_by_age_for(users, relative_to: :participants)
|
||||
age_groups.map do |start, finish|
|
||||
group_count = users.between_ages(start, finish).count
|
||||
total_count = if relative_to == :participants
|
||||
total_participants
|
||||
else
|
||||
send(relative_to, start, finish).count
|
||||
end
|
||||
|
||||
[
|
||||
"#{start} - #{finish}",
|
||||
{
|
||||
range: range_description(start, finish),
|
||||
count: group_count,
|
||||
percentage: calculate_percentage(group_count, total_count)
|
||||
}
|
||||
]
|
||||
end.to_h
|
||||
end
|
||||
|
||||
def participants_between_ages(from, to)
|
||||
participants.between_ages(from, to)
|
||||
end
|
||||
|
||||
@@ -14,16 +14,9 @@ class Poll::Stats
|
||||
total_participants_mail_percentage
|
||||
valid_percentage_web valid_percentage_booth valid_percentage_mail total_valid_percentage
|
||||
white_percentage_web white_percentage_booth white_percentage_mail total_white_percentage
|
||||
null_percentage_web null_percentage_booth null_percentage_mail total_null_percentage
|
||||
total_male_web total_male_booth total_male_mail
|
||||
total_female_web total_female_booth total_female_mail
|
||||
male_web_percentage male_booth_percentage male_mail_percentage
|
||||
female_web_percentage female_booth_percentage female_mail_percentage
|
||||
web_participants_by_age booth_participants_by_age mail_participants_by_age
|
||||
web_participants_by_geozone booth_participants_by_geozone mail_participants_by_geozone]
|
||||
null_percentage_web null_percentage_booth null_percentage_mail total_null_percentage]
|
||||
end
|
||||
|
||||
|
||||
def total_participants
|
||||
total_participants_web + total_participants_booth
|
||||
end
|
||||
@@ -38,41 +31,6 @@ class Poll::Stats
|
||||
define_method :"total_participants_#{channel}_percentage" do
|
||||
calculate_percentage(send(:"total_participants_#{channel}"), total_participants)
|
||||
end
|
||||
|
||||
define_method :"#{channel}_participants" do
|
||||
User.where(id: voters.where(origin: channel).pluck(:user_id))
|
||||
end
|
||||
|
||||
define_method :"#{channel}_participants_by_age" do
|
||||
participants_by_age_for(send(:"#{channel}_participants"),
|
||||
relative_to: :participants_between_ages)
|
||||
end
|
||||
|
||||
define_method :"#{channel}_participants_by_geozone" do
|
||||
geozones.map do |geozone|
|
||||
count = send(:"#{channel}_participants").where(geozone: geozone).count
|
||||
[
|
||||
geozone.name,
|
||||
{
|
||||
count: count,
|
||||
percentage: calculate_percentage(count, participants.where(geozone: geozone).count)
|
||||
}
|
||||
]
|
||||
end.to_h
|
||||
end
|
||||
|
||||
%i[male female].each do |gender|
|
||||
define_method :"total_#{gender}_#{channel}" do
|
||||
send(:"#{channel}_participants").public_send(gender).count
|
||||
end
|
||||
|
||||
define_method :"#{gender}_#{channel}_percentage" do
|
||||
calculate_percentage(
|
||||
send(:"total_#{gender}_#{channel}"),
|
||||
send(:"total_#{gender}_participants")
|
||||
)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def total_web_valid
|
||||
|
||||
@@ -14,15 +14,6 @@
|
||||
<li>
|
||||
<%= link_to t("stats.polls.by_channel"), "#stats_by_channel" %>
|
||||
</li>
|
||||
<li>
|
||||
<%= link_to t("stats.polls.by_gender_and_channel"), "#stats_by_gender_and_channel" %>
|
||||
</li>
|
||||
<li>
|
||||
<%= link_to t("stats.polls.by_age_and_channel"), "#stats_by_age_and_channel" %>
|
||||
</li>
|
||||
<li>
|
||||
<%= link_to t("stats.polls.by_geozone_and_channel"), "#stats_by_geozone_and_channel "%>
|
||||
</li>
|
||||
<li>
|
||||
<%= link_to t("stats.polls.vote_by_channel"), "#vote_stats_by_channel" %>
|
||||
</li>
|
||||
@@ -49,100 +40,6 @@
|
||||
<% end %>
|
||||
</div>
|
||||
|
||||
<div id="stats_by_gender_and_channel" class="stats-group">
|
||||
<h4><%= t("stats.polls.by_gender_and_channel") %></h4>
|
||||
|
||||
<table class="gender-and-channel">
|
||||
<thead>
|
||||
<tr>
|
||||
<th class="gender"></th>
|
||||
<% Poll::Stats::CHANNELS.each do |channel| %>
|
||||
<th class="<%= channel %>"><%= t("stats.polls.#{channel}") %></th>
|
||||
<% end %>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<% %i[male female].each do |gender| %>
|
||||
<tr>
|
||||
<td class="<%= gender %> gender"><%= t("stats.#{gender}") %></td>
|
||||
<% Poll::Stats::CHANNELS.each do |channel| %>
|
||||
<td>
|
||||
<%= "#{@stats[:"total_#{gender}_#{channel}"]}
|
||||
(#{number_to_stats_percentage(@stats[:"#{gender}_#{channel}_percentage"])})" %>
|
||||
</td>
|
||||
<% end %>
|
||||
</tr>
|
||||
<% end %>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
|
||||
<div id="stats_by_age_and_channel" class="stats-group">
|
||||
<h4><%= t("stats.polls.by_age_and_channel") %></h4>
|
||||
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<th><%= t("stats.age") %></th>
|
||||
<% Poll::Stats::CHANNELS.each do |channel| %>
|
||||
<th><%= t("stats.polls.#{channel}") %></th>
|
||||
<% end %>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<% @stats[:participants_by_age].keys.each do |age_range| %>
|
||||
<tr>
|
||||
<td><%= @stats[:participants_by_age][age_range][:range] %></td>
|
||||
|
||||
<% Poll::Stats::CHANNELS.each do |channel| %>
|
||||
<% group = @stats[:"#{channel}_participants_by_age"][age_range] %>
|
||||
<td>
|
||||
<div class="progress" tabindex="0">
|
||||
<span class="progress-meter" style="width: <%= group[:percentage] %>%">
|
||||
</span>
|
||||
</div>
|
||||
<%= "#{group[:count]} (#{number_to_stats_percentage(group[:percentage])})" %>
|
||||
</td>
|
||||
<% end %>
|
||||
</tr>
|
||||
<% end %>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
|
||||
<div id="stats_by_geozone_and_channel" class="stats-group">
|
||||
<h4><%= t("stats.polls.by_geozone_and_channel") %></h4>
|
||||
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<th><%= t("stats.geozone") %></th>
|
||||
<% Poll::Stats::CHANNELS.each do |channel| %>
|
||||
<th><%= t("stats.polls.#{channel}") %></th>
|
||||
<% end %>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<% @stats[:web_participants_by_geozone].keys.each do |name| %>
|
||||
<tr>
|
||||
<td><%= name %></td>
|
||||
|
||||
<% Poll::Stats::CHANNELS.each do |channel| %>
|
||||
<% group = @stats[:"#{channel}_participants_by_geozone"][name] %>
|
||||
<td>
|
||||
<div class="progress" tabindex="0">
|
||||
<span class="progress-meter" style="width: <%= group[:percentage] %>%">
|
||||
</span>
|
||||
</div>
|
||||
<%= "#{group[:count]} (#{number_to_stats_percentage(group[:percentage])})" %>
|
||||
</td>
|
||||
<% end %>
|
||||
</tr>
|
||||
<% end %>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
|
||||
<div id="vote_stats_by_channel" class="stats-group">
|
||||
<h4><%= t("stats.polls.vote_by_channel") %></h4>
|
||||
|
||||
|
||||
@@ -6,8 +6,6 @@ en:
|
||||
by_gender: "Participants by gender"
|
||||
by_age: "Participants by age"
|
||||
by_geozone: "Participants by district"
|
||||
male: "Male"
|
||||
female: "Female"
|
||||
men_percentage: "%{percentage} Men"
|
||||
women_percentage: "%{percentage} Women"
|
||||
age: "Age"
|
||||
@@ -37,13 +35,7 @@ en:
|
||||
heading_disclaimer: "*** Data about headings refer to the heading where each user voted, not necessarily the one that person is registered on."
|
||||
polls:
|
||||
by_channel: "Participants by channel"
|
||||
by_gender_and_channel: "Participants by gender and channel"
|
||||
by_age_and_channel: "Participants by age and channel"
|
||||
by_geozone_and_channel: "Participants by district and channel"
|
||||
vote_by_channel: "Vote type by channel"
|
||||
web: "Web"
|
||||
booth: "Booths"
|
||||
mail: "Mail"
|
||||
web_percentage: "%{percentage} Web"
|
||||
booth_percentage: "%{percentage} Booths"
|
||||
mail_percentage: "%{percentage} Mail"
|
||||
|
||||
@@ -8,8 +8,6 @@ es:
|
||||
by_geozone: "Participantes por distritos"
|
||||
men_percentage: "%{percentage} Hombres"
|
||||
women_percentage: "%{percentage} Mujeres"
|
||||
male: "Hombres"
|
||||
female: "Mujeres"
|
||||
age: "Edad"
|
||||
age_more_than: "De %{start} y más años"
|
||||
age_range: "De %{start} a %{finish} años"
|
||||
@@ -37,13 +35,7 @@ es:
|
||||
heading_disclaimer: "*** Los datos de distrito se refieren al distrito en el que el usuario ha votado, no necesariamente en el que está empadronado."
|
||||
polls:
|
||||
by_channel: "Participantes por medio"
|
||||
by_gender_and_channel: "Participantes por género y medio"
|
||||
by_age_and_channel: "Participantes por edad y medio"
|
||||
by_geozone_and_channel: "Participantes por distrito y medio"
|
||||
vote_by_channel: "Votos emitidos por medio"
|
||||
web: "Web"
|
||||
booth: "Urnas"
|
||||
mail: "Correo"
|
||||
web_percentage: "%{percentage} Web"
|
||||
booth_percentage: "%{percentage} Urnas"
|
||||
mail_percentage: "%{percentage} Correo"
|
||||
|
||||
@@ -182,126 +182,6 @@ describe Poll::Stats do
|
||||
|
||||
expect(stats.participants_by_geozone["Midgar"][:percentage]).to eq(33.333)
|
||||
end
|
||||
|
||||
describe "participants by gender and channel" do
|
||||
before do
|
||||
4.times do
|
||||
create :poll_voter, :from_web, poll: poll,
|
||||
user: create(:user, :level_two, gender: "female")
|
||||
end
|
||||
|
||||
3.times do
|
||||
create :poll_voter, :from_web, poll: poll,
|
||||
user: create(:user, :level_two, gender: "male")
|
||||
end
|
||||
|
||||
2.times do
|
||||
create :poll_voter, :from_booth, poll: poll,
|
||||
user: create(:user, :level_two, gender: "female")
|
||||
end
|
||||
|
||||
1.times do
|
||||
create :poll_voter, :from_booth, poll: poll,
|
||||
user: create(:user, :level_two, gender: "male")
|
||||
end
|
||||
end
|
||||
|
||||
it "calculates total participants" do
|
||||
expect(stats.total_female_web).to eq(4)
|
||||
expect(stats.total_male_web).to eq(3)
|
||||
expect(stats.total_female_booth).to eq(2)
|
||||
expect(stats.total_male_booth).to eq(1)
|
||||
end
|
||||
|
||||
it "calculates percentage relative to the participants for that gender" do
|
||||
expect(stats.female_web_percentage).to eq(66.667)
|
||||
expect(stats.female_booth_percentage).to eq(33.333)
|
||||
|
||||
expect(stats.male_web_percentage).to eq(75.0)
|
||||
expect(stats.male_booth_percentage).to eq(25.0)
|
||||
end
|
||||
end
|
||||
|
||||
describe "participants by age and channel" do
|
||||
before do
|
||||
4.times do
|
||||
create :poll_voter, :from_web, poll: poll,
|
||||
user: create(:user, :level_two, date_of_birth: 37.years.ago)
|
||||
end
|
||||
|
||||
3.times do
|
||||
create :poll_voter, :from_web, poll: poll,
|
||||
user: create(:user, :level_two, date_of_birth: 52.years.ago)
|
||||
end
|
||||
|
||||
2.times do
|
||||
create :poll_voter, :from_booth, poll: poll,
|
||||
user: create(:user, :level_two, date_of_birth: 37.years.ago)
|
||||
end
|
||||
|
||||
1.times do
|
||||
create :poll_voter, :from_booth, poll: poll,
|
||||
user: create(:user, :level_two, date_of_birth: 52.years.ago)
|
||||
end
|
||||
end
|
||||
|
||||
it "calculates the count of users by channel and age" do
|
||||
expect(stats.web_participants_by_age["35 - 39"][:count]).to eq(4)
|
||||
expect(stats.web_participants_by_age["50 - 54"][:count]).to eq(3)
|
||||
expect(stats.booth_participants_by_age["35 - 39"][:count]).to eq(2)
|
||||
expect(stats.booth_participants_by_age["50 - 54"][:count]).to eq(1)
|
||||
end
|
||||
|
||||
it "calculates percentage relative to the participants for that age" do
|
||||
expect(stats.web_participants_by_age["35 - 39"][:percentage]).to eq(66.667)
|
||||
expect(stats.booth_participants_by_age["35 - 39"][:percentage]).to eq(33.333)
|
||||
|
||||
expect(stats.web_participants_by_age["50 - 54"][:percentage]).to eq(75.0)
|
||||
expect(stats.booth_participants_by_age["50 - 54"][:percentage]).to eq(25.0)
|
||||
end
|
||||
end
|
||||
|
||||
describe "participants by geozone and channel" do
|
||||
before do
|
||||
atlantis = create(:geozone, name: "Atlantis")
|
||||
lemuria = create(:geozone, name: "Lemuria")
|
||||
|
||||
4.times do
|
||||
create :poll_voter, :from_booth, poll: poll,
|
||||
user: create(:user, :level_two, geozone: atlantis)
|
||||
end
|
||||
|
||||
3.times do
|
||||
create :poll_voter, :from_booth, poll: poll,
|
||||
user: create(:user, :level_two, geozone: lemuria)
|
||||
end
|
||||
|
||||
2.times do
|
||||
create :poll_voter, :from_web, poll: poll,
|
||||
user: create(:user, :level_two, geozone: atlantis)
|
||||
end
|
||||
|
||||
1.times do
|
||||
create :poll_voter, :from_web, poll: poll,
|
||||
user: create(:user, :level_two, geozone: lemuria)
|
||||
end
|
||||
end
|
||||
|
||||
it "calculates the count of users by channel and geozone" do
|
||||
expect(stats.booth_participants_by_geozone["Atlantis"][:count]).to eq(4)
|
||||
expect(stats.booth_participants_by_geozone["Lemuria"][:count]).to eq(3)
|
||||
expect(stats.web_participants_by_geozone["Atlantis"][:count]).to eq(2)
|
||||
expect(stats.web_participants_by_geozone["Lemuria"][:count]).to eq(1)
|
||||
end
|
||||
|
||||
it "calculates percentage relative to the participants for that geozone" do
|
||||
expect(stats.booth_participants_by_geozone["Atlantis"][:percentage]).to eq(66.667)
|
||||
expect(stats.web_participants_by_geozone["Atlantis"][:percentage]).to eq(33.333)
|
||||
|
||||
expect(stats.booth_participants_by_geozone["Lemuria"][:percentage]).to eq(75.0)
|
||||
expect(stats.web_participants_by_geozone["Lemuria"][:percentage]).to eq(25.0)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe "#generate" do
|
||||
|
||||
Reference in New Issue
Block a user