Skip to content

Commit

Permalink
[DEMAD-441] Estadísticas
Browse files Browse the repository at this point in the history
  • Loading branch information
vanessasotob committed Jul 14, 2022
1 parent bdd8c80 commit 7ae50da
Show file tree
Hide file tree
Showing 12 changed files with 230 additions and 221 deletions.
35 changes: 6 additions & 29 deletions app/models/budget/stats.rb
Expand Up @@ -2,26 +2,6 @@ class Budget::Stats
include Statisticable
alias_method :budget, :resource

def self.stats_methods
super + support_phase_methods + vote_phase_methods + every_phase_methods
end

def self.support_phase_methods
%i[total_participants_support_phase total_budget_investments
total_selected_investments total_unfeasible_investments headings]
end

def self.vote_phase_methods
%i[total_votes total_participants_vote_phase]
end
def self.every_phase_methods
%i[total_participants_every_phase]
end

def stats_methods
super + base_stats_methods + participation_methods + phase_methods
end

def phases
%w[accepting support vote].select { |phase| send("#{phase}_phase_finished?") }
end
Expand Down Expand Up @@ -49,7 +29,7 @@ def total_participants
end

def total_participants_accepting_phase
User.where("users.id in (?)",authors).pluck(:id).uniq.count
authors.uniq.count
end

def total_participants_accepting_gender
Expand All @@ -59,6 +39,9 @@ def total_participants_accepting_gender
[male,female,other]
end

def participants
@participants ||= User.unscoped.where(id: participant_ids)
end

def total_participants_support_phase
voters.count
Expand Down Expand Up @@ -194,8 +177,7 @@ def accepting_by_heading(heading)

def poll_voters_by_heading(heading)
if !budget.poll.blank?
budget.poll.voters.where(votable: heading.investments)
.distinct.pluck(:user_id)
budget.poll.voters.distinct.pluck(:user_id)
else
[]
end
Expand Down Expand Up @@ -240,7 +222,7 @@ def calculate_heading_stats_with_totals(heading_totals, groups_totals, populatio
end

def voters_and_balloters_by_heading(heading)
(accepting_by_heading(heading).distinct.pluck(:id) + voters_by_heading(heading) + balloters_by_heading(heading.id))
(accepting_by_heading(heading) + voters_by_heading(heading) + balloters_by_heading(heading.id))
end

def participants_percent(heading_totals, groups_totals, phase)
Expand All @@ -256,9 +238,4 @@ def supports(supportable)
Vote.where(votable: supportable.investments)
end

# stats_cache(*stats_methods)

# def stats_cache(key, &block)
# Rails.cache.fetch("budgets_stats/#{budget.id}/#{phases.join}/#{key}/#{version}", &block)
# end
end
4 changes: 2 additions & 2 deletions app/models/concerns/statisticable.rb
Expand Up @@ -63,7 +63,7 @@ def participants_by_age
count = participants.between_ages(start, finish).count
female = participants.female.between_ages(start, finish).count
male = participants.male.between_ages(start, finish).count
other = participants.where("gender IS NULL OR date_of_birth IS NULL OR geozone_id IS NULL").between_ages(start, finish).count
other = participants.where("gender IS NULL OR gender = ''").between_ages(start, finish).count

[
"#{start} - #{finish}",
Expand All @@ -87,7 +87,7 @@ def participants_by_geozone
count: stats.count,
female: stats.geozone_participants.female.count,
male: stats.geozone_participants.male.count,
other: stats.geozone_participants.where("gender IS NULL OR date_of_birth IS NULL OR geozone_id IS NULL").count,
other: stats.geozone_participants.where("gender IS NULL OR gender = ''").count,
percentage: stats.percentage
}
]
Expand Down
55 changes: 23 additions & 32 deletions app/views/budgets/stats/_advanced_stats.html.erb
Expand Up @@ -5,20 +5,21 @@
<h4><%= t("stats.budgets.total_investments") %></h4>

<%= number_with_info_tags(
stats.total_budget_investments,
budget_stats.find_by(name:'total_budget_investments', budget_id: @budget.id).try(:value).to_i,
t("stats.budgets.total_investments"),
html_class: "total-investments"
) %>
<%if @budget.id == 5%>
<%= number_with_info_tags(265,
t("stats.budgets.total_unfeasible_investments")) %>
<%else%>
<%= number_with_info_tags(stats.total_unfeasible_investments,
<%= number_with_info_tags(budget_stats.find_by(name:'total_unfeasible_investments', budget_id: @budget.id).try(:value).to_i,
t("stats.budgets.total_unfeasible_investments")) %>
<%end%>
<%= number_with_info_tags(stats.total_selected_investments,
<%= number_with_info_tags(budget_stats.find_by(name:'total_selected_investments', budget_id: @budget.id).try(:value).to_i,

t("stats.budgets.total_selected_investments")) %>
</div>

Expand All @@ -27,28 +28,18 @@

<% stats.phases.each do |phase| %>
<h5><%= t("stats.budgets.participants_#{phase}_phase") %></h5>
<%= number_with_info_tags(
stats.send("total_participants_#{phase}_phase"),
t("stats.budgets.participants_#{phase}_phase")
) %>
<%= number_with_info_tags(budget_stats.find_by(name:"total_participants_#{phase}_phase", budget_id: @budget.id).try(:value).to_i,
t("stats.budgets.participants_#{phase}_phase"))%>
<div id="participants_by_gender<%=phase%>" class="stats-group">
<h5><%= t("stats.by_gender_#{phase}") %></h5>
<%= number_with_info_tags(
stats.send("total_participants_#{phase}_gender")[0],
t("stats.men_percentage", percentage: number_to_stats_percentage( stats.send("total_participants_#{phase}_gender")[0].to_f/stats.send("total_participants_#{phase}_phase").to_f*100)),
html_class: "participants male"
) %>
<%['male','female', 'other'].each do |sex|%>
<%= number_with_info_tags(
budget_stats.find_by(name:"total_participants_#{phase}_gender_#{sex}", budget_id: @budget.id).try(:value).to_i,
t("stats.#{sex}_percentage", percentage: number_to_stats_percentage( budget_stats.find_by(name:"total_participants_#{phase}_gender_#{sex}", budget_id: @budget.id).try(:value).to_f/budget_stats.find_by(name:"total_participants_#{phase}_phase", budget_id: @budget.id).try(:value).to_f*100)),
html_class: "participants #{sex}"
) %>
<%end%>

<%= number_with_info_tags(
stats.send("total_participants_#{phase}_gender")[1],
t("stats.women_percentage", percentage: number_to_stats_percentage(stats.send("total_participants_#{phase}_gender")[1].to_f/stats.send("total_participants_#{phase}_phase").to_f*100)),
html_class: "participants female"
) %>
<%= number_with_info_tags(
stats.send("total_participants_#{phase}_gender")[2],
t("stats.other_percentage", percentage: number_to_stats_percentage(stats.send("total_participants_#{phase}_gender")[2].to_f/stats.send("total_participants_#{phase}_phase").to_f*100)),
) %>

</div>

<% end %>
Expand Down Expand Up @@ -106,42 +97,42 @@
<tr id="<%= heading.name.parameterize %>">
<td id="total_spending_proposals_heading_<%= heading.id %>"
class="text-center border-left border-right">
<%= budget_stats.find_by(name: "total_investments_count", budget_heading_id: heading.id).try(:value).to_i %>
<%= budget_stats.find_by(name: "total_investments_count", budget_id: @budget.id, budget_heading_id: heading.id).try(:value).to_i %>
</td>
<td class="text-center border-left border-right">
<%= budget_stats.find_by(name: "total_investments_male_count", budget_heading_id: heading.id).try(:value).to_i %>
<%= budget_stats.find_by(name: "total_investments_male_count", budget_id: @budget.id, budget_heading_id: heading.id).try(:value).to_i %>
</td>
<td class="text-center border-left border-right">
<%= budget_stats.find_by(name: "total_investments_female_count", budget_heading_id: heading.id).try(:value).to_i %>
<%= budget_stats.find_by(name: "total_investments_female_count", budget_id: @budget.id, budget_heading_id: heading.id).try(:value).to_i %>
</td>
<td class="text-center border-left border-right">
<%= budget_stats.find_by(name: "total_investments_other_count", budget_heading_id: heading.id).try(:value).to_i %>
<%= budget_stats.find_by(name: "total_investments_other_count", budget_id: @budget.id, budget_heading_id: heading.id).try(:value).to_i %>
</td>

<% stats.all_phases.each do |phase| %>
<td id="total_participants_<%= phase %>_phase_heading_<%= heading.id %>"
class="border-left text-center">
<%= budget_stats.find_by(name: "total_participants_#{phase}_phase", budget_heading_id: heading.id).try(:value).to_i %>
<%= budget_stats.find_by(name: "total_participants_#{phase}_phase", budget_id: @budget.id, budget_heading_id: heading.id).try(:value).to_i %>
</td>
<td id="total_participants_<%= phase %>_male_heading_<%= heading.id %>"
class="border-left text-center">
<%= budget_stats.find_by(name: "total_participants_#{phase}_male", budget_heading_id: heading.id).try(:value).to_i%>
<%= budget_stats.find_by(name: "total_participants_#{phase}_male", budget_id: @budget.id, budget_heading_id: heading.id).try(:value).to_i%>
</td>
<td id="total_participants_<%= phase %>_female_heading_<%= heading.id %>"
class="border-left text-center">
<%= budget_stats.find_by(name: "total_participants_#{phase}_female", budget_heading_id: heading.id).try(:value).to_i %>
<%= budget_stats.find_by(name: "total_participants_#{phase}_female", budget_id: @budget.id, budget_heading_id: heading.id).try(:value).to_i %>
</td>
<td id="total_participants_<%= phase %>_other_heading_<%= heading.id %>"
class="border-left text-center">
<%= budget_stats.find_by(name: "total_participants_#{phase}_other", budget_heading_id: heading.id).try(:value).to_i %>
<%= budget_stats.find_by(name: "total_participants_#{phase}_other", budget_id: @budget.id, budget_heading_id: heading.id).try(:value).to_i %>
</td>
<td id="percentage_participants_<%= phase %>_phase_heading_<%= heading.id %>"
class="border-left border-right text-center">
<%= number_to_stats_percentage(budget_stats.find_by(name: "percentage_participants_#{phase}_phase" , budget_heading_id: heading.id).try(:value))%>
<%= number_to_stats_percentage(budget_stats.find_by(name: "percentage_participants_#{phase}_phase", budget_id: @budget.id , budget_heading_id: heading.id).try(:value))%>
</td>
<td id="percentage_district_population_<%= phase %>_phase_heading_<%= heading.id %>"
class="text-center border-right">
<%= number_to_stats_percentage(budget_stats.find_by(name: "percentage_district_population_#{phase}_phase" , budget_heading_id: heading.id).try(:value)) %>
<%= number_to_stats_percentage(budget_stats.find_by(name: "percentage_district_population_#{phase}_phase", budget_id: @budget.id , budget_heading_id: heading.id).try(:value)) %>
</td>
<% end %>
</tr>
Expand Down
4 changes: 2 additions & 2 deletions app/views/budgets/stats/show.html.erb
Expand Up @@ -27,12 +27,12 @@

<div class="row margin">
<div class="small-12 medium-3 column sidebar">
<%= render "shared/stats/links", stats: @stats %>
<%= render "shared/stats/links", stats: @stats, budget_stats: @budget_stats %>
<%= render "advanced_stats_links" if @stats.advanced? %>
</div>

<div class="small-12 medium-9 column stats-content">
<%= render "shared/stats/participation", stats: @stats %>
<%= render "shared/stats/participation", stats: @stats, budget_stats: @budget_stats %>
<%= render "advanced_stats", budget_stats: @budget_stats, stats: @stats if @stats.advanced? %>

<div class="row margin">
Expand Down
66 changes: 35 additions & 31 deletions app/views/shared/stats/_age.html.erb
@@ -1,34 +1,38 @@
<div id="participants_by_age" class="stats-group">
<h4><%= t("stats.by_age") %></h4>
<% if !budget_stats.blank?%>
<%else%>
<div id="participants_by_age" class="stats-group">
<h4><%= t("stats.by_age") %></h4>

<table>
<thead>
<tr>
<th class="small-4"><%= t("stats.age") %></th>
<th><%= t("stats.male") %></th>
<th><%= t("stats.female") %></th>
<th><%= t("stats.other") %></th>
<th><%= t("stats.total") %></th>
</tr>
</thead>

<tbody>
<% stats.participants_by_age.values.each do |group| %>
<table>
<thead>
<tr>
<td><%= group[:range] %></td>
<td><%= group[:male] %></td>
<td><%= group[:female] %></td>
<td><%= group[:other] %></td>
<td>
<strong>
<%= "#{group[:count]} (#{number_to_stats_percentage(group[:percentage])})" %>
</strong>
<div class="progress" tabindex="0">
<span class="progress-meter" style="width: <%= group[:percentage] %>%"></span>
</div>
</td>
<th class="small-4"><%= t("stats.age") %></th>
<th><%= t("stats.male") %></th>
<th><%= t("stats.female") %></th>
<th><%= t("stats.other") %></th>
<th><%= t("stats.total") %></th>
</tr>
<% end %>
</tbody>
</table>
</div>
</thead>

<tbody>
<% stats.participants_by_age.values.each do |group| %>
<tr>
<td><%= group[:range] %></td>
<td><%= group[:male] %></td>
<td><%= group[:female] %></td>
<td><%= group[:other] %></td>
<td>
<strong>
<%= "#{group[:count]} (#{number_to_stats_percentage(group[:percentage])})" %>
</strong>
<div class="progress" tabindex="0">
<span class="progress-meter" style="width: <%= group[:percentage] %>%"></span>
</div>
</td>
</tr>
<% end %>
</tbody>
</table>
</div>
<%end%>
56 changes: 39 additions & 17 deletions app/views/shared/stats/_gender.html.erb
@@ -1,19 +1,41 @@
<div id="participants_by_gender" class="stats-group">
<h4><%= t("stats.by_gender") %></h4>
<% if !budget_stats.blank?%>
<div id="participants_by_gender" class="stats-group">
<h4><%= t("stats.by_gender") %></h4>

<%= number_with_info_tags(
stats.participants.where("upper(gender) = upper('male')").count,
t("stats.men_percentage", percentage: number_to_stats_percentage(stats.participants.where("upper(gender) = upper('male')").count.to_f/stats.participants.count.to_f*100 )),
html_class: "participants male"
) %>
<%= number_with_info_tags(
budget_stats.find_by(name: 'total_male_participants', budget: @budget.id).try(:value).to_i,
t("stats.male_percentage", percentage: number_to_stats_percentage(budget_stats.find_by(name: 'total_male_participants', budget: @budget.id).try(:value).to_f/budget_stats.find_by(name: 'total_participants', budget: @budget.id).try(:value).to_f*100 )),
html_class: "participants male"
) %>
<%= number_with_info_tags(
stats.participants.where("upper(gender) = upper('female')").count,
t("stats.women_percentage", percentage: number_to_stats_percentage(stats.participants.where("upper(gender) = upper('female')").count.to_f/stats.participants.count.to_f*100)),
html_class: "participants female"
) %>
<%= number_with_info_tags(
stats.participants.where("gender is null or gender = ''").count,
t("stats.other_percentage", percentage: number_to_stats_percentage(stats.participants.where("gender is null or gender = ''").count.to_f/stats.participants.count.to_f*100))
) %>
</div>
<%= number_with_info_tags(
budget_stats.find_by(name: 'total_female_participants', budget: @budget.id).try(:value).to_i,
t("stats.female_percentage", percentage: number_to_stats_percentage(budget_stats.find_by(name: 'total_female_participants', budget: @budget.id).try(:value).to_f/budget_stats.find_by(name: 'total_participants', budget: @budget.id).try(:value).to_f*100)),
html_class: "participants female"
) %>
<%= number_with_info_tags(
budget_stats.find_by(name: 'total_other_participants', budget: @budget.id).try(:value).to_i,
t("stats.other_percentage", percentage: number_to_stats_percentage(budget_stats.find_by(name: 'total_other_participants', budget: @budget.id).try(:value).to_f/budget_stats.find_by(name: 'total_participants', budget: @budget.id).try(:value).to_f*100))
) %>
</div>
<%else%>
<div id="participants_by_gender" class="stats-group">
<h4><%= t("stats.by_gender") %></h4>

<%= number_with_info_tags(
stats.participants.where("upper(gender) = upper('male')").count,
t("stats.men_percentage", percentage: number_to_stats_percentage(stats.participants.where("upper(gender) = upper('male')").count.to_f/stats.participants.count.to_f*100 )),
html_class: "participants male"
) %>
<%= number_with_info_tags(
stats.participants.where("upper(gender) = upper('female')").count,
t("stats.women_percentage", percentage: number_to_stats_percentage(stats.participants.where("upper(gender) = upper('female')").count.to_f/stats.participants.count.to_f*100)),
html_class: "participants female"
) %>
<%= number_with_info_tags(
stats.participants.where("gender is null or gender = ''").count,
t("stats.other_percentage", percentage: number_to_stats_percentage(stats.participants.where("gender is null or gender = ''").count.to_f/stats.participants.count.to_f*100))
) %>
</div>
<%end%>

0 comments on commit 7ae50da

Please sign in to comment.