forked from consuldemocracy/consuldemocracy
-
Notifications
You must be signed in to change notification settings - Fork 16
/
stats.rb
537 lines (449 loc) · 23.7 KB
/
stats.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
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
def all_phases
return phases unless phases.many?
[*phases, "every"]
end
def support_phase_finished?
budget.valuating_or_later?
end
def accepting_phase_finished?
budget.valuating_or_later?
end
def vote_phase_finished?
budget.finished?
end
def total_participants
participant_ids.distinct.count
end
def total_participants_accepting_phase
authors.joins(:author).select(:author_id).uniq.count
end
def total_participants_accepting_gender
genders = {male: 0, female: 0, other: 0}
User.where("id in (?)",authors.joins(:author).select(:author_id)).select("COALESCE(upper(gender),'') as gender_col, count(*) as total").group("gender_col").each do |g|
case g[:gender_col]
when "MALE"
genders[:male] = g[:total]
when "FEMALE"
genders[:female] = g[:total]
end
end
genders[:other] = total_participants_accepting_phase - genders[:male] - genders[:female]
[ genders[:male], genders[:female], genders[:other]]
end
def total_participants_support_phase
voters.select(:voter_id).count
end
def total_participants_support_gender
genders = {male: 0, female: 0, other: 0}
User.where("id in (?)",voters.select(:voter_id)).select("COALESCE(upper(gender),'') as gender_col, count(*) as total").group("gender_col").each do |g|
case g[:gender_col]
when "MALE"
genders[:male] = g[:total]
when "FEMALE"
genders[:female] = g[:total]
end
end
genders[:other] = total_participants_support_phase - genders[:male] - genders[:female]
[ genders[:male], genders[:female], genders[:other]]
end
def total_participants_vote_phase
balloters.select(:user_id).count + (poll_ballot_voters.blank? ? 0 : poll_ballot_voters.select(:user_id).count)
rescue
0
end
def total_participants_vote_gender
genders = {male: 0, female: 0, other: 0}
participant_ids_vote_phase.select("COALESCE(upper(gender),'') as gender_col, count(*) as total").group("gender_col").each do |g|
case g[:gender_col]
when "MALE"
genders[:male] = g[:total]
when "FEMALE"
genders[:female] = g[:total]
end
end
genders[:other] = total_participants_vote_phase - genders[:male] - genders[:female]
[ genders[:male], genders[:female], genders[:other]]
end
def total_participants_every_phase
User.where("id in (?) or id in (?) or id in (?) or id in (?)",
participant_ids_accepting_phase.select(:author_id),
participant_ids_support_phase.select(:voter_id),
balloters.select(:user_id),
poll_ballot_voters.select(:user_id)
).distinct.count
end
def total_budget_investments
budget.investments.count
end
def total_votes
budget.ballots.select(:ballot_lines_count).inject(0) { |sum, x| sum + x }
end
def total_selected_investments
budget.investments.selected.count
end
def total_unfeasible_investments
budget.investments.unfeasible.count
end
def headings
groups = Hash.new(0)
budget.headings.order("id ASC").each do |heading|
puts "Carga de datos: #{heading.id}"
groups[heading.id] = Hash.new(0).merge(calculate_heading_totals(heading))
end
groups[:total] = Hash.new(0)
groups[:total][:total_investments_count] = groups.collect {|_k, v| v[:total_investments_count]}.sum
groups[:total][:total_investments_male_count] = groups.collect {|_k, v| v[:total_investments_male_count]}.sum
groups[:total][:total_investments_female_count] = groups.collect {|_k, v| v[:total_investments_female_count]}.sum
groups[:total][:total_investments_other_count] = groups.collect {|_k, v| v[:total_investments_other_count]}.sum
groups[:total][:total_participants_accepting_phase] = groups.collect {|_k, v| v[:total_participants_accepting_phase]}.sum
groups[:total][:total_participants_accepting_male] = groups.collect {|_k, v| v[:total_participants_accepting_male]}.sum
groups[:total][:total_participants_accepting_female] = groups.collect {|_k, v| v[:total_participants_accepting_female]}.sum
groups[:total][:total_participants_accepting_other] = groups.collect {|_k, v| v[:total_participants_accepting_other]}.sum
groups[:total][:total_participants_support_phase] = groups.collect {|_k, v| v[:total_participants_support_phase]}.sum
groups[:total][:total_participants_support_male] = groups.collect {|_k, v| v[:total_participants_support_male]}.sum
groups[:total][:total_participants_support_female] = groups.collect {|_k, v| v[:total_participants_support_female]}.sum
groups[:total][:total_participants_support_other] = groups.collect {|_k, v| v[:total_participants_support_other]}.sum
groups[:total][:total_participants_vote_phase] = groups.collect {|_k, v| v[:total_participants_vote_phase]}.sum
groups[:total][:total_participants_vote_male] = groups.collect {|_k, v| v[:total_participants_vote_male]}.sum
groups[:total][:total_participants_vote_female] = groups.collect {|_k, v| v[:total_participants_vote_female]}.sum
groups[:total][:total_participants_vote_other] = groups.collect {|_k, v| v[:total_participants_vote_other]}.sum
groups[:total][:total_participants_every_phase] = groups.collect {|_k, v| v[:total_participants_every_phase]}.sum
groups[:total][:total_participants_every_male] = groups.collect {|_k, v| v[:total_participants_every_male]}.sum
groups[:total][:total_participants_every_female] = groups.collect {|_k, v| v[:total_participants_every_female]}.sum
groups[:total][:total_participants_every_other] = groups.collect {|_k, v| v[:total_participants_every_other]}.sum
budget.headings.each do |heading|
puts "Carga de datos: #{heading.id}"
groups[heading.id].merge!(calculate_heading_stats_with_totals(groups[heading.id], groups[:total], heading.population))
end
groups[:total][:percentage_participants_support_phase] = groups.collect {|_k, v| v[:percentage_participants_support_phase]}.sum
groups[:total][:percentage_participants_vote_phase] = groups.collect {|_k, v| v[:percentage_participants_vote_phase]}.sum
groups[:total][:percentage_participants_every_phase] = groups.collect {|_k, v| v[:percentage_participants_every_phase]}.sum
groups
end
def authors
@authors = budget.investments.where("budget_status_id <> 1 or budget_status_id is null")
end
def participant_ids
User.where("id in (?) or id in (?) or id in (?) or id in (?)",
participant_ids_accepting_phase.select(:author_id),
participant_ids_support_phase.select(:voter_id),
balloters.select(:user_id),
(poll_ballot_voters.blank? ? [] : poll_ballot_voters.select(:user_id).uniq)
)
end
# def participants
# @participants = User.unscoped.where(id: participant_ids)
# end
def total_gender_participant
genders = {male: 0, female: 0, other: 0}
participant_ids.select("COALESCE(upper(users.gender),'') as gender_col, count(*) as total").group("gender_col").each do |g|
case g[:gender_col]
when "MALE"
genders[:male] = g[:total]
when "FEMALE"
genders[:female] = g[:total]
end
end
genders[:other] = participant_ids.count - genders[:male] - genders[:female]
[ genders[:male], genders[:female], genders[:other]]
end
def total_no_demographic_data
budget_stats_exist = Budget::Investment::Stat.find_by(budget_id: budget.id, name: "total_no_demographic_data" )
if budget_stats_exist.blank?
budget_stats = Budget::Investment::Stat.new
budget_stats.value = participant_ids.where("(UPPER(gender) NOT IN ('MALE','FEMALE')) OR date_of_birth IS NULL OR geozone_id IS NULL").count
budget_stats.name = "total_no_demographic_data"
budget_stats.budget = budget
budget_stats.save
end
budget_stats_exist.try(:value).to_i
end
def participants_by_age
genders = {male: 0, female: 0, other: 0}
aux = []
participant_ids.select("case
when extract(year from age(date_of_birth)) between 16 and 19 then '16-19'
when extract(year from age(date_of_birth)) between 20 and 24 then '20-24'
when extract(year from age(date_of_birth)) between 25 and 29 then '25-29'
when extract(year from age(date_of_birth)) between 30 and 34 then '30-34'
when extract(year from age(date_of_birth)) between 35 and 39 then '35-39'
when extract(year from age(date_of_birth)) between 40 and 44 then '40-44'
when extract(year from age(date_of_birth)) between 45 and 49 then '45-49'
when extract(year from age(date_of_birth)) between 50 and 54 then '50-54'
when extract(year from age(date_of_birth)) between 55 and 59 then '55-59'
when extract(year from age(date_of_birth)) between 60 and 64 then '60-64'
when extract(year from age(date_of_birth)) between 65 and 69 then '65-69'
when extract(year from age(date_of_birth)) between 70 and 74 then '70-74'
when extract(year from age(date_of_birth)) between 75 and 79 then '75-79'
when extract(year from age(date_of_birth)) between 80 and 84 then '80-84'
when extract(year from age(date_of_birth)) between 85 and 89 then '85-89'
when extract(year from age(date_of_birth)) >=90 then '90'
else '' end as rango ,
coalesce(upper(gender),'') as gender_col,
count(*) as total").where("extract(year from age(date_of_birth)) >=16").group("rango, gender_col").order("rango asc").each do |g|
case g[:gender_col]
when "MALE"
genders[:male] = g[:total]
when "FEMALE"
genders[:female] = g[:total]
else
genders[:other] = g[:total]
end
rango = g[:rango].split('-')
if aux.select {|x| x[:range] == range_description(rango[0].to_i, rango.count > 1 ? rango[1].to_i : 300)}.blank?
aux.push({
range: range_description(rango[0].to_i, rango.count > 1 ? rango[1].to_i : 300),
count: genders[:male]+genders[:female]+genders[:other],
female: genders[:female],
male: genders[:male],
other: genders[:other],
percentage: calculate_percentage(genders[:male]+genders[:female]+genders[:other], total_participants)
})
else
case g[:gender_col]
when "MALE"
aux.select {|x| x[:range] == range_description(rango[0].to_i, rango.count > 1 ? rango[1].to_i : 300)}[0][:male] = g[:total]
when "FEMALE"
aux.select {|x| x[:range] == range_description(rango[0].to_i, rango.count > 1 ? rango[1].to_i : 300)}[0][:female] = g[:total]
else
aux.select {|x| x[:range] == range_description(rango[0].to_i, rango.count > 1 ? rango[1].to_i : 300)}[0][:other] = g[:total]
end
aux.select {|x| x[:range] == range_description(rango[0].to_i, rango.count > 1 ? rango[1].to_i : 300)}[0][:count] = aux.select {|x| x[:range] == range_description(rango[0].to_i, rango.count > 1 ? rango[1].to_i : 300)}[0][:male] + aux.select {|x| x[:range] == range_description(rango[0].to_i, rango.count > 1 ? rango[1].to_i : 300)}[0][:female] + aux.select {|x| x[:range] == range_description(rango[0].to_i, rango.count > 1 ? rango[1].to_i : 300)}[0][:other]
aux.select {|x| x[:range] == range_description(rango[0].to_i, rango.count > 1 ? rango[1].to_i : 300)}[0][:percentage] = calculate_percentage(aux.select {|x| x[:range] == range_description(rango[0].to_i, rango.count > 1 ? rango[1].to_i : 300)}[0][:count], total_participants)
end
end
aux
end
def participants_by_geozone
genders = {male: 0, female: 0, other: 0}
aux = []
participant_ids.select("coalesce(upper(gender),'') as gender_col,
(select g.name from geozones g where g.id= geozone_id) as geozone ,count(*) as total")
.group("geozone_id, gender_col").order("geozone asc").each do |g|
case g[:gender_col]
when "MALE"
genders[:male] = g[:total]
when "FEMALE"
genders[:female] = g[:total]
else
genders[:other] = g[:total]
end
if aux.select {|x| x[:geozone] == g[:geozone]}.blank?
aux.push({
geozone: g[:geozone],
count: genders[:male]+genders[:female]+genders[:other],
female: genders[:female],
male: genders[:male],
other: genders[:other],
percentage: calculate_percentage(genders[:male]+genders[:female]+genders[:other], total_participants)
})
else
case g[:gender_col]
when "MALE"
aux.select {|x| x[:geozone] == g[:geozone]}[0][:male] = g[:total]
when "FEMALE"
aux.select {|x| x[:geozone] == g[:geozone]}[0][:female] = g[:total]
else
aux.select {|x| x[:geozone] == g[:geozone]}[0][:other] = g[:total]
end
aux.select {|x| x[:geozone] == g[:geozone]}[0][:count] = aux.select {|x| x[:geozone] == g[:geozone]}[0][:male] + aux.select {|x| x[:geozone] == g[:geozone]}[0][:female] + aux.select {|x| x[:geozone] == g[:geozone]}[0][:other]
aux.select {|x| x[:geozone] == g[:geozone]}[0][:percentage] = calculate_percentage(aux.select {|x| x[:geozone] == g[:geozone]}[0][:count], total_participants)
end
end
aux
end
def age_groups
[[16, 19],
[20, 24],
[25, 29],
[30, 34],
[35, 39],
[40, 44],
[45, 49],
[50, 54],
[55, 59],
[60, 64],
[65, 69],
[70, 74],
[75, 79],
[80, 84],
[85, 89],
[90, 300]
]
end
def range_description(start, finish)
if finish > 200
I18n.t("stats.age_more_than", start: start)
else
I18n.t("stats.age_range", start: start, finish: finish)
end
end
def gender?
budget_stats_exist = Budget::Investment::Stat.find_by(budget_id: budget.id, name: "link_gender?" )
if budget_stats_exist.blank?
budget_stats = Budget::Investment::Stat.new
budget_stats.value = participants.where("upper(gender) IN ('MALE','FEMALE')").any? ? 1 : 0
budget_stats.name = "link_gender?"
budget_stats.budget = budget
budget_stats.save
end
budget_stats_exist.try(:value).to_i == 1
end
def age?
budget_stats_exist = Budget::Investment::Stat.find_by(budget_id: budget.id, name: "link_age?" )
if budget_stats_exist.blank?
budget_stats = Budget::Investment::Stat.new
budget_stats.value = participants.between_ages(age_groups.flatten.min, age_groups.flatten.max).any? ? 1 : 0
budget_stats.name = "link_age?"
budget_stats.budget = budget
budget_stats.save
end
budget_stats_exist.try(:value).to_i == 1
end
def geozone?
budget_stats_exist = Budget::Investment::Stat.find_by(budget_id: budget.id, name: "link_geozone?" )
if budget_stats_exist.blank?
budget_stats = Budget::Investment::Stat.new
budget_stats.value = participants.where(geozone: geozones).any? ? 1 : 0
budget_stats.name = "link_geozone?"
budget_stats.budget = budget
budget_stats.save
end
budget_stats_exist.try(:value).to_i == 1
end
private
def phase_methods
phases.map { |phase| self.class.send("#{phase}_phase_methods") }.flatten
end
def participant_ids_accepting_phase
authors.uniq.select(:author_id)
end
def participant_ids_support_phase
voters.uniq
end
def participant_ids_vote_phase
User.where("id in (?) or id in (?)",balloters.select(:user_id),(poll_ballot_voters.blank? ? [] : poll_ballot_voters.select(:user_id).uniq)).uniq
end
def voters
@voters = supports(budget).uniq
end
def balloters
@balloters = budget.ballots.where("ballot_lines_count > ?", 0).uniq
end
def poll_ballot_voters
@poll_ballot_voters = budget.poll.voters
rescue
[]
end
def balloters_by_heading(heading_id)
budget.ballots.joins(:lines)
.where(budget_ballot_lines: { heading_id: heading_id} )
.distinct.select(:user_id)
end
def voters_by_heading(heading)
supports(heading).distinct.select(:voter_id)
end
def accepting_by_heading(heading)
heading.investments.where("budget_status_id <> 1 or budget_status_id is null").distinct.select(:author_id)
end
def poll_voters_by_heading(heading)
if !budget.poll.blank?
budget.poll.voters.where(geozone_id: heading)
.distinct.select(:user_id)
else
[]
end
end
def calculate_heading_totals(heading)
aux_total = heading.investments.select(:author_id)
aux_accepting = accepting_by_heading(heading)
aux_support = voters_by_heading(heading)
aux_ballot = balloters_by_heading(heading.id)
aux_poll = poll_voters_by_heading(heading)
aux_vote = (aux_ballot + aux_poll).uniq
aux_every = (aux_accepting + aux_support + aux_vote).uniq
aux_user_total = User.where("id in (?)", aux_total).select("upper(gender) as aux_gender, count(1) as total").group("aux_gender")
aux_user_accepting = User.where("id in (?)", aux_accepting).select("upper(gender) as aux_gender, count(1) as total").group("aux_gender")
aux_user_support = User.where("id in (?)", aux_support).select("upper(gender) as aux_gender, count(1) as total").group("aux_gender")
if aux_poll.blank?
aux_user_vote = User.where("id in (?)", aux_ballot).select("upper(gender) as aux_gender, count(1) as total").group("aux_gender")
aux_user_every = User.where("id in (?) or id in (?) or id in (?) ", aux_accepting,aux_support,aux_ballot).select("upper(gender) as aux_gender, count(1) as total").group("aux_gender")
else
aux_user_vote = User.where("id in (?) or id in (?)", aux_ballot,aux_poll).select("upper(gender) as aux_gender, count(1) as total").group("aux_gender")
aux_user_every = User.where("id in (?) or id in (?) or id in (?) or id in (?)", aux_accepting,aux_support,aux_ballot,aux_poll).select("upper(gender) as aux_gender, count(1) as total").group("aux_gender")
end
{
total_investments_count: aux_total.count,
total_investments_male_count: aux_user_total.select {|x| x.aux_gender=="MALE"}.blank? ? 0 : aux_user_total.select {|x| x.aux_gender=="MALE"}[0].total,
total_investments_female_count: aux_user_total.select {|x| x.aux_gender=="FEMALE"}.blank? ? 0 : aux_user_total.select {|x| x.aux_gender=="FEMALE"}[0].total,
total_investments_other_count: aux_total.count - (aux_user_total.select {|x| x.aux_gender=="MALE"}.blank? ? 0 : aux_user_total.select {|x| x.aux_gender=="MALE"}[0].total) - (aux_user_total.select {|x| x.aux_gender=="FEMALE"}.blank? ? 0 : aux_user_total.select {|x| x.aux_gender=="FEMALE"}[0].total),
total_participants_accepting_phase: aux_accepting.count,
total_participants_accepting_male: aux_user_accepting.select {|x| x.aux_gender=="MALE"}.blank? ? 0 : aux_user_accepting.select {|x| x.aux_gender=="MALE"}[0].total,
total_participants_accepting_female: aux_user_accepting.select {|x| x.aux_gender=="FEMALE"}.blank? ? 0 : aux_user_accepting.select {|x| x.aux_gender=="FEMALE"}[0].total,
total_participants_accepting_other: aux_accepting.count - (aux_user_accepting.select {|x| x.aux_gender=="MALE"}.blank? ? 0 : aux_user_accepting.select {|x| x.aux_gender=="MALE"}[0].total) - (aux_user_accepting.select {|x| x.aux_gender=="FEMALE"}.blank? ? 0 : aux_user_accepting.select {|x| x.aux_gender=="FEMALE"}[0].total),
total_participants_support_phase: aux_support.count,
total_participants_support_male: aux_user_support.select {|x| x.aux_gender=="MALE"}.blank? ? 0 : aux_user_support.select {|x| x.aux_gender=="MALE"}[0].total,
total_participants_support_female: aux_user_support.select {|x| x.aux_gender=="FEMALE"}.blank? ? 0 : aux_user_support.select {|x| x.aux_gender=="FEMALE"}[0].total,
total_participants_support_other: aux_support.count - (aux_user_support.select {|x| x.aux_gender=="MALE"}.blank? ? 0 : aux_user_support.select {|x| x.aux_gender=="MALE"}[0].total) - (aux_user_support.select {|x| x.aux_gender=="FEMALE"}.blank? ? 0 : aux_user_support.select {|x| x.aux_gender=="FEMALE"}[0].total),
total_participants_vote_phase: aux_vote.count,
total_participants_vote_male: aux_user_vote.select {|x| x.aux_gender=="MALE"}.blank? ? 0 : aux_user_vote.select {|x| x.aux_gender=="MALE"}[0].total,
total_participants_vote_female: aux_user_vote.select {|x| x.aux_gender=="FEMALE"}.blank? ? 0 : aux_user_vote.select {|x| x.aux_gender=="FEMALE"}[0].total,
total_participants_vote_other: aux_vote.count - (aux_user_vote.select {|x| x.aux_gender=="MALE"}.blank? ? 0 : aux_user_vote.select {|x| x.aux_gender=="MALE"}[0].total) - (aux_user_vote.select {|x| x.aux_gender=="FEMALE"}.blank? ? 0 : aux_user_vote.select {|x| x.aux_gender=="FEMALE"}[0].total),
total_participants_every_phase: aux_every.count,
total_participants_every_male: aux_user_every.select {|x| x.aux_gender=="MALE"}.blank? ? 0 : aux_user_every.select {|x| x.aux_gender=="MALE"}[0].total,
total_participants_every_female: aux_user_every.select {|x| x.aux_gender=="FEMALE"}.blank? ? 0 : aux_user_every.select {|x| x.aux_gender=="FEMALE"}[0].total,
total_participants_every_other: aux_every.count - ( aux_user_every.select {|x| x.aux_gender=="MALE"}.blank? ? 0 : aux_user_every.select {|x| x.aux_gender=="MALE"}[0].total) - (aux_user_every.select {|x| x.aux_gender=="FEMALE"}.blank? ? 0 : aux_user_every.select {|x| x.aux_gender=="FEMALE"}[0].total)
}
end
def calculate_heading_stats_with_totals(heading_totals, groups_totals, population)
{
percentage_participants_support_phase: participants_percent(heading_totals, groups_totals, :total_participants_support_phase),
percentage_district_population_support_phase: population_percent(population, heading_totals[:total_participants_support_phase]),
percentage_participants_accepting_phase: participants_percent(heading_totals, groups_totals, :total_participants_accepting_phase),
percentage_district_population_accepting_phase: population_percent(population, heading_totals[:total_participants_accepting_phase]),
percentage_participants_vote_phase: participants_percent(heading_totals, groups_totals, :total_participants_vote_phase),
percentage_district_population_vote_phase: population_percent(population, heading_totals[:total_participants_vote_phase]),
percentage_participants_every_phase: participants_percent(heading_totals, groups_totals, :total_participants_every_phase),
percentage_district_population_every_phase: population_percent(population, heading_totals[:total_participants_every_phase])
}
end
def voters_and_balloters_by_heading(heading)
(accepting_by_heading(heading).distinct.select(:id) + voters_by_heading(heading) + balloters_by_heading(heading.id))
end
def participants_percent(heading_totals, groups_totals, phase)
calculate_percentage(heading_totals[phase], groups_totals[phase])
end
def population_percent(population, participants)
return "N/A" unless population.to_f.positive?
calculate_percentage(participants, population)
end
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