Skip to content

Commit 93dd021

Browse files
authored
Merge pull request #601 from coopdevs/develop
v3.8.0
2 parents ad40974 + d2c3ba5 commit 93dd021

19 files changed

+229
-224
lines changed

.rubocop.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ Style/LineEndConcatenation:
114114
Enabled: false
115115

116116
Layout/LineLength:
117-
Max: 80
117+
Max: 100
118118

119119
Metrics/MethodLength:
120120
Enabled: false

app/admin/dashboard.rb

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,20 @@
1616
column do
1717
panel "Recent Users" do
1818
ul do
19-
User.last(5).map do |u|
20-
li link_to(u, admin_user_path(u))
19+
User.last(5).map do |user|
20+
li link_to(user, admin_user_path(user))
2121
end
2222
end
2323
end
2424
end
2525

2626
column do
27-
panel "Info" do
28-
para "Welcome to ActiveAdmin."
27+
panel "Recent Posts" do
28+
ul do
29+
Post.last(5).map do |post|
30+
li link_to(post, admin_post_path(post))
31+
end
32+
end
2933
end
3034
end
3135
end

app/controllers/statistics_controller.rb

Lines changed: 83 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -1,92 +1,75 @@
11
class StatisticsController < ApplicationController
2-
AGE_GROUP_LABELS = {
3-
0..17 => " -17",
4-
18..24 => "18-24",
5-
25..34 => "25-34",
6-
35..44 => "35-44",
7-
45..54 => "45-54",
8-
55..64 => "55-64",
9-
65..100 => "65+",
10-
}
11-
122
before_action :authenticate_user!
133

14-
def statistics_global_activity
15-
@members = current_organization.members
16-
@active_members = @members.active
4+
def global_activity
5+
members = current_organization.members
6+
@active_members = members.active
7+
178
@total_hours = num_movements = 0
18-
@members.each do |m|
9+
members.each do |m|
1910
num_movements += m.account.movements.count
2011
@total_hours += m.account.movements.map do
2112
|a| (a.amount > 0) ? a.amount : 0
2213
end.inject(0, :+)
2314
end
24-
# cada intercambio implica dos movimientos
25-
@num_swaps = (num_movements +
26-
current_organization.account.movements.count) / 2
27-
# intercambios con el banco
2815
@total_hours += current_organization.account.movements.
2916
map { |a| (a.amount > 0) ? a.amount : 0 }.inject(0, :+)
30-
# periodo a mostrar actividades globales, por defecto 6 meses
31-
ini = params[:ini].presence.try(:to_date) || DateTime.now.to_date - 5.month
32-
fin = params[:fin].presence.try(:to_date) || DateTime.now.to_date
33-
if ini.present?
34-
# calculo numero de meses
35-
num_months = (fin.year * 12 + fin.month) - (ini.year * 12 + ini.month) + 1
36-
date = ini
37-
# vector para los meses de la gráfica ["Enero", "Febrero",...]
38-
@months_names = []
39-
# y vectores con los datos para la gráfica
40-
@user_reg_months = []
41-
@num_swaps_months = []
42-
@hours_swaps_months = []
43-
# valores por cada mes
44-
num_months.times do
45-
@months_names.push(l(date, format: "%B %Y"))
46-
@user_reg_months.push(@members.by_month(date).count)
47-
# movimientos de los miembros en dicho mes
48-
swaps_members = @members.map { |a| a.account.movements.by_month(date) }
49-
# movimimentos del banco
50-
swaps_organization = current_organization.account.
51-
movements.by_month(date)
52-
# numero de movimientos totales
53-
sum_swaps = (swaps_members.flatten.count + swaps_organization.count) / 2
54-
@num_swaps_months.push(sum_swaps)
55-
# horas intercambiadas
56-
sum_hours = 0
57-
swaps_members.flatten.each do |s|
58-
sum_hours += (s.amount > 0) ? s.amount : 0
59-
end
60-
sum_hours += swaps_organization.map do
61-
|a| (a.amount > 0) ? a.amount : 0
62-
end.inject(0, :+)
63-
sum_hours = sum_hours / 3600.0 if sum_hours > 0
64-
@hours_swaps_months.push(sum_hours)
65-
date = date.next_month
17+
18+
@num_swaps = (num_movements + current_organization.account.movements.count) / 2
19+
20+
from = params[:from].presence.try(:to_date) || DateTime.now.to_date - 5.month
21+
to = params[:to].presence.try(:to_date) || DateTime.now.to_date
22+
num_months = (to.year * 12 + to.month) - (from.year * 12 + from.month) + 1
23+
date = from
24+
25+
@months_names = []
26+
@user_reg_months = []
27+
@num_swaps_months = []
28+
@hours_swaps_months = []
29+
30+
num_months.times do
31+
@months_names << l(date, format: "%B %Y")
32+
@user_reg_months << members.by_month(date).count
33+
34+
swaps_members = members.map { |a| a.account.movements.by_month(date) }
35+
swaps_organization = current_organization.account.movements.by_month(date)
36+
sum_swaps = (swaps_members.flatten.count + swaps_organization.count) / 2
37+
@num_swaps_months << sum_swaps
38+
39+
sum_hours = 0
40+
swaps_members.flatten.each do |s|
41+
sum_hours += (s.amount > 0) ? s.amount : 0
6642
end
43+
sum_hours += swaps_organization.map do
44+
|a| (a.amount > 0) ? a.amount : 0
45+
end.inject(0, :+)
46+
sum_hours = sum_hours / 3600.0 if sum_hours > 0
47+
@hours_swaps_months << sum_hours
48+
49+
date = date.next_month
6750
end
6851
end
6952

70-
def statistics_inactive_users
53+
def inactive_users
7154
@members = current_organization.members.active
7255
end
7356

74-
def statistics_demographics
75-
@members = current_organization.members
76-
@age_counts = age_counts
77-
@gender_counts = gender_counts
57+
def demographics
58+
members = current_organization.members
59+
@age_counts = age_counts(members)
60+
@gender_counts = gender_counts(members)
7861
end
7962

80-
def statistics_last_login
63+
def last_login
8164
@members = current_organization.members.active.joins(:user).
8265
order("users.current_sign_in_at ASC NULLS FIRST")
8366
end
8467

85-
def statistics_without_offers
68+
def without_offers
8669
@members = current_organization.members.active
8770
end
8871

89-
def statistics_type_swaps
72+
def type_swaps
9073
offers = current_organization.posts.
9174
where(type: "Offer").joins(:transfers, transfers: :movements).
9275
select("posts.tags, posts.category_id, SUM(movements.amount) as
@@ -99,7 +82,7 @@ def statistics_type_swaps
9982
sort_by(&:last).reverse
10083
end
10184

102-
def statistics_all_transfers
85+
def all_transfers
10386
@transfers = current_organization.all_transfers.
10487
includes(movements: {account: :accountable}).
10588
order("transfers.created_at DESC").
@@ -110,73 +93,79 @@ def statistics_all_transfers
11093

11194
protected
11295

113-
def age(date_of_birth)
114-
return unless date_of_birth
115-
age_in_days = Date.today - date_of_birth
116-
(age_in_days / 365.26).to_i
117-
end
118-
119-
# returns a hash of
120-
# {
121-
# [ category_label, tag_label ] => [ sum, count, ratio ],
122-
# ...
123-
# }
12496
def count_offers_by_label(offers)
125-
# Cannot use Hash.new([0, 0]) because then
126-
# counters[key][0] += n
97+
# Cannot use Hash.new([0, 0]) because then counters[key][0] += n
12798
# will modify directly the "global default" instead of
12899
# first assigning a new array with the zeroed counters.
129100
counters = Hash.new { |h, k| h[k] = [0, 0] }
101+
130102
offers.each do |offer|
131103
labels_for_offer(offer).each do |labels|
132-
# labels = [ category_label, tag_label ]
133104
counters[labels][0] += offer.sum_of_transfers
134105
counters[labels][1] += offer.count_of_transfers
135106
end
136107
end
137-
add_ratios!(counters)
108+
add_ratios(counters)
109+
138110
counters
139111
end
140112

141-
def add_ratios!(counters)
142-
# add the ratio at the end of each value
113+
def add_ratios(counters)
143114
total_count = counters.values.map { |_, counts| counts }.sum
115+
144116
counters.each do |_, v|
145117
v << v[1].to_f / total_count
146118
end
147119
end
148120

149-
# returns an array of
150-
# [category_name, tag_name]
151-
# one item per each tag. If the category or the tags are missing, they are
152-
# replaced with a fallback "Unknown" label.
153121
def labels_for_offer(offer)
154122
tag_labels = offer.tags.presence ||
155-
[t("statistics.statistics_type_swaps.without_tags")]
123+
[t("statistics.type_swaps.without_tags")]
156124

157125
category_label = offer.category.try(:name) ||
158-
t("statistics.statistics_type_swaps.without_category")
126+
t("statistics.type_swaps.without_category")
159127

160128
[category_label].product(tag_labels)
161129
end
162130

163-
def age_counts
164-
@members.each_with_object(Hash.new(0)) do |member, counts|
165-
age = age(member.user_date_of_birth)
166-
age_label = AGE_GROUP_LABELS.detect do |range, _|
131+
def age_counts(members)
132+
members.each_with_object(Hash.new(0)) do |member, counts|
133+
age = compute_age(member.user_date_of_birth)
134+
135+
age_label = age_group_labels.detect do |range, _|
167136
range.include? age
168-
end.try(:last) || t("statistics.statistics_demographics.unknown")
137+
end.try(:last) || t("statistics.demographics.unknown")
138+
169139
counts[age_label] += 1
170140
end
171141
end
172142

173-
def gender_counts
174-
@members.each_with_object(Hash.new(0)) do |member, counts|
143+
def compute_age(date_of_birth)
144+
return unless date_of_birth
145+
146+
age_in_days = Date.today - date_of_birth
147+
(age_in_days / 365.26).to_i
148+
end
149+
150+
def age_group_labels
151+
{
152+
0..17 => "-17",
153+
18..24 => "18-24",
154+
25..34 => "25-34",
155+
35..44 => "35-44",
156+
45..54 => "45-54",
157+
55..64 => "55-64",
158+
65..100 => "65+",
159+
}
160+
end
161+
162+
def gender_counts(members)
163+
members.each_with_object(Hash.new(0)) do |member, counts|
175164
gender = member.user_gender
176165
gender_label = if gender.present?
177166
t("simple_form.options.user.gender.#{gender}")
178167
else
179-
t("statistics.statistics_demographics.unknown")
168+
t("statistics.demographics.unknown")
180169
end
181170
counts[gender_label] += 1
182171
end

app/views/application/menus/_organization_statistics_menu.html.erb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,43 +6,43 @@
66
</a>
77
<ul class="dropdown-menu" role="menu">
88
<li>
9-
<%= link_to statistics_global_activity_statistics_path do %>
9+
<%= link_to global_activity_statistics_path do %>
1010
<%= glyph :stats %>
1111
<%= t "application.navbar.global_activity" %>
1212
<% end %>
1313
</li>
1414
<li>
15-
<%= link_to statistics_inactive_users_statistics_path do %>
15+
<%= link_to inactive_users_statistics_path do %>
1616
<%= glyph :stats %>
1717
<%= t "application.navbar.inactive_users" %>
1818
<% end %>
1919
</li>
2020
<li>
21-
<%= link_to statistics_demographics_statistics_path do %>
21+
<%= link_to demographics_statistics_path do %>
2222
<%= glyph :stats %>
2323
<%= t "application.navbar.demographics" %>
2424
<% end %>
2525
</li>
2626
<li>
27-
<%= link_to statistics_last_login_statistics_path do %>
27+
<%= link_to last_login_statistics_path do %>
2828
<%= glyph :stats %>
2929
<%= t "application.navbar.last_login" %>
3030
<% end %>
3131
</li>
3232
<li>
33-
<%= link_to statistics_without_offers_statistics_path do %>
33+
<%= link_to without_offers_statistics_path do %>
3434
<%= glyph :stats %>
3535
<%= t "application.navbar.without_offers" %>
3636
<% end %>
3737
</li>
3838
<li>
39-
<%= link_to statistics_type_swaps_statistics_path do %>
39+
<%= link_to type_swaps_statistics_path do %>
4040
<%= glyph :stats %>
4141
<%= t "application.navbar.type_of_swaps" %>
4242
<% end %>
4343
</li>
4444
<li>
45-
<%= link_to statistics_all_transfers_statistics_path do %>
45+
<%= link_to all_transfers_statistics_path do %>
4646
<%= glyph :stats %>
4747
<%= t "application.navbar.statistics_all_transfers" %>
4848
<% end %>

0 commit comments

Comments
 (0)