mirror of
https://github.com/TracksApp/tracks.git
synced 2026-03-02 02:50:16 +01:00
Extract pie chart data logic into model layer
Move most of the tests for this logic into the unit test layer.
This commit is contained in:
parent
9f3470f9dc
commit
961227da0c
5 changed files with 142 additions and 164 deletions
37
app/models/stats/pie_chart_data.rb
Normal file
37
app/models/stats/pie_chart_data.rb
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
module Stats
|
||||
class PieChartData
|
||||
|
||||
attr_reader :all_actions_per_context, :values, :labels, :ids, :alpha, :title
|
||||
def initialize(all_actions_per_context, title, alpha)
|
||||
@all_actions_per_context = all_actions_per_context
|
||||
@title = title
|
||||
@alpha = alpha
|
||||
end
|
||||
|
||||
def calculate
|
||||
sum = all_actions_per_context.inject(0){|sum, apc| sum + apc['total']}
|
||||
|
||||
pie_cutoff=10
|
||||
size = [all_actions_per_context.size, pie_cutoff].min
|
||||
|
||||
# explicitly copy contents of hash to avoid ending up with two arrays pointing to same hashes
|
||||
actions_per_context = Array.new(size){|i| {
|
||||
'name' => all_actions_per_context[i]['name'],
|
||||
'total' => all_actions_per_context[i]['total'],
|
||||
'id' => all_actions_per_context[i]['id']
|
||||
} }
|
||||
|
||||
if all_actions_per_context.size > pie_cutoff
|
||||
actions_per_context[-1]['name']=I18n.t('stats.other_actions_label')
|
||||
actions_per_context[-1]['id']=-1
|
||||
size.upto(all_actions_per_context.size-1){ |i| actions_per_context[-1]['total']+=(all_actions_per_context[i]['total']) }
|
||||
end
|
||||
|
||||
@values = Array.new(size){|i| actions_per_context[i]['total']*100/sum }
|
||||
@labels = Array.new(size){|i| actions_per_context[i]['name'].truncate(15, :omission => '...') }
|
||||
@ids = Array.new(size){|i| actions_per_context[i]['id']}
|
||||
end
|
||||
|
||||
|
||||
end
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue