mirror of
https://github.com/TracksApp/tracks.git
synced 2026-03-12 07:32:37 +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
|
|
@ -178,20 +178,18 @@ class StatsController < ApplicationController
|
|||
end
|
||||
|
||||
def context_total_actions_data
|
||||
all_actions_per_context = Stats::TopContextsQuery.new(current_user).result
|
||||
@title = t('stats.spread_of_actions_for_all_context')
|
||||
@alpha = 70
|
||||
prep_context_data_for_view(all_actions_per_context)
|
||||
actions = Stats::TopContextsQuery.new(current_user).result
|
||||
|
||||
@data = Stats::PieChartData.new(actions, t('stats.spread_of_actions_for_all_context'), 70)
|
||||
@data.calculate
|
||||
|
||||
render :pie_chart_data, :layout => false
|
||||
end
|
||||
|
||||
def context_running_actions_data
|
||||
all_actions_per_context = Stats::TopContextsQuery.new(current_user, :running => true).result
|
||||
|
||||
@title = t('stats.spread_of_running_actions_for_visible_contexts')
|
||||
@alpha = 60
|
||||
prep_context_data_for_view(all_actions_per_context)
|
||||
actions = Stats::TopContextsQuery.new(current_user, :running => true).result
|
||||
@data = Stats::PieChartData.new(actions, t('stats.spread_of_running_actions_for_visible_contexts'), 60)
|
||||
@data.calculate
|
||||
|
||||
render :pie_chart_data, :layout => false
|
||||
end
|
||||
|
|
@ -345,31 +343,6 @@ class StatsController < ApplicationController
|
|||
|
||||
private
|
||||
|
||||
def prep_context_data_for_view(all_actions_per_context)
|
||||
|
||||
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']=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
|
||||
|
||||
@pie_slices = Array.new(size){|i| actions_per_context[i]['total']*100/sum }
|
||||
@pie_labels = Array.new(size){|i| actions_per_context[i]['name'].truncate(15, :omission => '...') }
|
||||
@pie_links = Array.new(size){|i| context_path(actions_per_context[i]['id'])}
|
||||
end
|
||||
|
||||
def init
|
||||
@me = self # for meta programming
|
||||
|
||||
|
|
|
|||
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
|
||||
|
|
@ -1,9 +1,9 @@
|
|||
&title=<%= @title %>,{font-size:16}&
|
||||
&pie=<%= @alpha %>,#505050,{font-size: 12px; color: #404040;}&
|
||||
&title=<%= @data.title %>,{font-size:16}&
|
||||
&pie=<%= @data.alpha %>,#505050,{font-size: 12px; color: #404040;}&
|
||||
&x_axis_steps=1& &y_ticks=5,10,5& &line=3,#87421F& &y_min=0& &y_max=20&
|
||||
&values=<%= @pie_slices.join(",") %>&
|
||||
&pie_labels=<%= @pie_labels.join(",") %>&
|
||||
&links=<%= @pie_links.join(",") %>&
|
||||
&values=<%= @data.values.join(",") %>&
|
||||
&pie_labels=<%= @data.labels.join(",") %>&
|
||||
&links=<%= @data.ids.map{|id| context_path(id)}.join(",") %>&
|
||||
&colours=#d01f3c,#356aa0,#C79810,#c61fd0,#1fc6d0,#1fd076,#72d01f,#c6d01f,#d0941f,#40941f&
|
||||
&tool_tip=#x_label#: #val#%25&
|
||||
&x_label_style=9,,2,1&
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue