1
1
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
-
12
2
before_action :authenticate_user!
13
3
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
+
17
8
@total_hours = num_movements = 0
18
- @ members. each do |m |
9
+ members . each do |m |
19
10
num_movements += m . account . movements . count
20
11
@total_hours += m . account . movements . map do
21
12
|a | ( a . amount > 0 ) ? a . amount : 0
22
13
end . inject ( 0 , :+ )
23
14
end
24
- # cada intercambio implica dos movimientos
25
- @num_swaps = ( num_movements +
26
- current_organization . account . movements . count ) / 2
27
- # intercambios con el banco
28
15
@total_hours += current_organization . account . movements .
29
16
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
66
42
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
67
50
end
68
51
end
69
52
70
- def statistics_inactive_users
53
+ def inactive_users
71
54
@members = current_organization . members . active
72
55
end
73
56
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 )
78
61
end
79
62
80
- def statistics_last_login
63
+ def last_login
81
64
@members = current_organization . members . active . joins ( :user ) .
82
65
order ( "users.current_sign_in_at ASC NULLS FIRST" )
83
66
end
84
67
85
- def statistics_without_offers
68
+ def without_offers
86
69
@members = current_organization . members . active
87
70
end
88
71
89
- def statistics_type_swaps
72
+ def type_swaps
90
73
offers = current_organization . posts .
91
74
where ( type : "Offer" ) . joins ( :transfers , transfers : :movements ) .
92
75
select ( "posts.tags, posts.category_id, SUM(movements.amount) as
@@ -99,7 +82,7 @@ def statistics_type_swaps
99
82
sort_by ( &:last ) . reverse
100
83
end
101
84
102
- def statistics_all_transfers
85
+ def all_transfers
103
86
@transfers = current_organization . all_transfers .
104
87
includes ( movements : { account : :accountable } ) .
105
88
order ( "transfers.created_at DESC" ) .
@@ -110,73 +93,79 @@ def statistics_all_transfers
110
93
111
94
protected
112
95
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
- # }
124
96
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
127
98
# will modify directly the "global default" instead of
128
99
# first assigning a new array with the zeroed counters.
129
100
counters = Hash . new { |h , k | h [ k ] = [ 0 , 0 ] }
101
+
130
102
offers . each do |offer |
131
103
labels_for_offer ( offer ) . each do |labels |
132
- # labels = [ category_label, tag_label ]
133
104
counters [ labels ] [ 0 ] += offer . sum_of_transfers
134
105
counters [ labels ] [ 1 ] += offer . count_of_transfers
135
106
end
136
107
end
137
- add_ratios! ( counters )
108
+ add_ratios ( counters )
109
+
138
110
counters
139
111
end
140
112
141
- def add_ratios! ( counters )
142
- # add the ratio at the end of each value
113
+ def add_ratios ( counters )
143
114
total_count = counters . values . map { |_ , counts | counts } . sum
115
+
144
116
counters . each do |_ , v |
145
117
v << v [ 1 ] . to_f / total_count
146
118
end
147
119
end
148
120
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.
153
121
def labels_for_offer ( offer )
154
122
tag_labels = offer . tags . presence ||
155
- [ t ( "statistics.statistics_type_swaps .without_tags" ) ]
123
+ [ t ( "statistics.type_swaps .without_tags" ) ]
156
124
157
125
category_label = offer . category . try ( :name ) ||
158
- t ( "statistics.statistics_type_swaps .without_category" )
126
+ t ( "statistics.type_swaps .without_category" )
159
127
160
128
[ category_label ] . product ( tag_labels )
161
129
end
162
130
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 , _ |
167
136
range . include? age
168
- end . try ( :last ) || t ( "statistics.statistics_demographics.unknown" )
137
+ end . try ( :last ) || t ( "statistics.demographics.unknown" )
138
+
169
139
counts [ age_label ] += 1
170
140
end
171
141
end
172
142
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 |
175
164
gender = member . user_gender
176
165
gender_label = if gender . present?
177
166
t ( "simple_form.options.user.gender.#{ gender } " )
178
167
else
179
- t ( "statistics.statistics_demographics .unknown" )
168
+ t ( "statistics.demographics .unknown" )
180
169
end
181
170
counts [ gender_label ] += 1
182
171
end
0 commit comments