From e3063cd24fc84cd59d00af4d0c7b3f93d93e3d73 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Fri, 29 Mar 2019 14:03:47 +0100 Subject: [PATCH] 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). --- app/models/concerns/statisticable.rb | 33 +++----- app/models/poll/stats.rb | 44 +--------- app/views/polls/stats.html.erb | 103 ----------------------- config/locales/en/stats.yml | 8 -- config/locales/es/stats.yml | 8 -- spec/models/poll/stats_spec.rb | 120 --------------------------- 6 files changed, 13 insertions(+), 303 deletions(-) diff --git a/app/models/concerns/statisticable.rb b/app/models/concerns/statisticable.rb index 5aa952bb4..980bda637 100644 --- a/app/models/concerns/statisticable.rb +++ b/app/models/concerns/statisticable.rb @@ -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 diff --git a/app/models/poll/stats.rb b/app/models/poll/stats.rb index c171d1900..191139d5a 100644 --- a/app/models/poll/stats.rb +++ b/app/models/poll/stats.rb @@ -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 diff --git a/app/views/polls/stats.html.erb b/app/views/polls/stats.html.erb index 37d878c0b..9ace784dd 100644 --- a/app/views/polls/stats.html.erb +++ b/app/views/polls/stats.html.erb @@ -14,15 +14,6 @@
  • <%= link_to t("stats.polls.by_channel"), "#stats_by_channel" %>
  • -
  • - <%= link_to t("stats.polls.by_gender_and_channel"), "#stats_by_gender_and_channel" %> -
  • -
  • - <%= link_to t("stats.polls.by_age_and_channel"), "#stats_by_age_and_channel" %> -
  • -
  • - <%= link_to t("stats.polls.by_geozone_and_channel"), "#stats_by_geozone_and_channel "%> -
  • <%= link_to t("stats.polls.vote_by_channel"), "#vote_stats_by_channel" %>
  • @@ -49,100 +40,6 @@ <% end %> -
    -

    <%= t("stats.polls.by_gender_and_channel") %>

    - - - - - - <% Poll::Stats::CHANNELS.each do |channel| %> - - <% end %> - - - - <% %i[male female].each do |gender| %> - - - <% Poll::Stats::CHANNELS.each do |channel| %> - - <% end %> - - <% end %> - -
    <%= t("stats.polls.#{channel}") %>
    <%= t("stats.#{gender}") %> - <%= "#{@stats[:"total_#{gender}_#{channel}"]} - (#{number_to_stats_percentage(@stats[:"#{gender}_#{channel}_percentage"])})" %> -
    -
    - -
    -

    <%= t("stats.polls.by_age_and_channel") %>

    - - - - - - <% Poll::Stats::CHANNELS.each do |channel| %> - - <% end %> - - - - <% @stats[:participants_by_age].keys.each do |age_range| %> - - - - <% Poll::Stats::CHANNELS.each do |channel| %> - <% group = @stats[:"#{channel}_participants_by_age"][age_range] %> - - <% end %> - - <% end %> - -
    <%= t("stats.age") %><%= t("stats.polls.#{channel}") %>
    <%= @stats[:participants_by_age][age_range][:range] %> -
    - - -
    - <%= "#{group[:count]} (#{number_to_stats_percentage(group[:percentage])})" %> -
    -
    - -
    -

    <%= t("stats.polls.by_geozone_and_channel") %>

    - - - - - - <% Poll::Stats::CHANNELS.each do |channel| %> - - <% end %> - - - - <% @stats[:web_participants_by_geozone].keys.each do |name| %> - - - - <% Poll::Stats::CHANNELS.each do |channel| %> - <% group = @stats[:"#{channel}_participants_by_geozone"][name] %> - - <% end %> - - <% end %> - -
    <%= t("stats.geozone") %><%= t("stats.polls.#{channel}") %>
    <%= name %> -
    - - -
    - <%= "#{group[:count]} (#{number_to_stats_percentage(group[:percentage])})" %> -
    -
    -

    <%= t("stats.polls.vote_by_channel") %>

    diff --git a/config/locales/en/stats.yml b/config/locales/en/stats.yml index f6126efb0..2c64e70b3 100644 --- a/config/locales/en/stats.yml +++ b/config/locales/en/stats.yml @@ -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" diff --git a/config/locales/es/stats.yml b/config/locales/es/stats.yml index 99a0c9ae3..1e6827a97 100644 --- a/config/locales/es/stats.yml +++ b/config/locales/es/stats.yml @@ -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" diff --git a/spec/models/poll/stats_spec.rb b/spec/models/poll/stats_spec.rb index 3b064ccf4..2b2fe6c7b 100644 --- a/spec/models/poll/stats_spec.rb +++ b/spec/models/poll/stats_spec.rb @@ -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