mirror of
https://github.com/TracksApp/tracks.git
synced 2025-12-23 10:40:13 +01:00
Refactor last year chart
Pulled some instance variable assignment into the controller. Also extracted a method regarding interpolated values for the present month.
This commit is contained in:
parent
0877bf9ab8
commit
1493304fc1
3 changed files with 26 additions and 28 deletions
|
|
@ -33,45 +33,51 @@ class StatsController < ApplicationController
|
||||||
find_running_avg_array(@actions_done_last12monthsPlus3_array, @actions_created_last12monthsPlus3_array, 13)
|
find_running_avg_array(@actions_done_last12monthsPlus3_array, @actions_created_last12monthsPlus3_array, 13)
|
||||||
|
|
||||||
# interpolate avg for current month.
|
# interpolate avg for current month.
|
||||||
percent_of_month = Time.zone.now.day.to_f / Time.zone.now.end_of_month.day.to_f
|
interpolate_avg_for_current_month(@actions_created_last12months_array, @actions_done_last12months_array)
|
||||||
@interpolated_actions_created_this_month = interpolate_avg(@actions_created_last12months_array, percent_of_month)
|
|
||||||
@interpolated_actions_done_this_month = interpolate_avg(@actions_done_last12months_array, percent_of_month)
|
|
||||||
|
|
||||||
render :layout => false
|
render :layout => false
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def interpolate_avg_for_current_month(created_set, done_set)
|
||||||
|
percent_of_month = Time.zone.now.day.to_f / Time.zone.now.end_of_month.day.to_f
|
||||||
|
@interpolated_actions_created_this_month = interpolate_avg(created_set, percent_of_month)
|
||||||
|
@interpolated_actions_done_this_month = interpolate_avg(done_set, percent_of_month)
|
||||||
|
end
|
||||||
|
|
||||||
def actions_done_last_years
|
def actions_done_last_years
|
||||||
@page_title = t('stats.index_title')
|
@page_title = t('stats.index_title')
|
||||||
@chart = Stats::Chart.new('actions_done_lastyears_data', :height => 400, :width => 900)
|
@chart = Stats::Chart.new('actions_done_lastyears_data', :height => 400, :width => 900)
|
||||||
end
|
end
|
||||||
|
|
||||||
def actions_done_lastyears_data
|
def actions_done_lastyears_data
|
||||||
@actions_done_last_months = current_user.todos.completed.select("completed_at").reorder("completed_at DESC")
|
actions_done_last_months = current_user.todos.completed.select("completed_at").reorder("completed_at DESC")
|
||||||
@actions_created_last_months = current_user.todos.select("created_at").reorder("created_at DESC" )
|
actions_created_last_months = current_user.todos.select("created_at").reorder("created_at DESC" )
|
||||||
|
|
||||||
# query is sorted, so use last todo to calculate number of months
|
# query is sorted, so use last todo to calculate number of months
|
||||||
@month_count = [difference_in_months(@today, @actions_created_last_months.last.created_at),
|
month_count = [difference_in_months(@today, actions_created_last_months.last.created_at),
|
||||||
difference_in_months(@today, @actions_done_last_months.last.completed_at)].max
|
difference_in_months(@today, actions_done_last_months.last.completed_at)].max
|
||||||
|
|
||||||
# convert to array and fill in non-existing months
|
# convert to array and fill in non-existing months
|
||||||
@actions_done_last_months_array = convert_to_months_from_today_array(@actions_done_last_months, @month_count+1, :completed_at)
|
@actions_done_last_months_array = convert_to_months_from_today_array(actions_done_last_months, month_count+1, :completed_at)
|
||||||
@actions_created_last_months_array = convert_to_months_from_today_array(@actions_created_last_months, @month_count+1, :created_at)
|
@actions_created_last_months_array = convert_to_months_from_today_array(actions_created_last_months, month_count+1, :created_at)
|
||||||
|
|
||||||
# find max for graph in both hashes
|
# find max for graph in both hashes
|
||||||
@max = [@actions_done_last_months_array.max, @actions_created_last_months_array.max].max
|
@max = [@actions_done_last_months_array.max, @actions_created_last_months_array.max].max
|
||||||
|
|
||||||
# find running avg
|
# find running avg
|
||||||
@actions_done_avg_last_months_array, @actions_created_avg_last_months_array =
|
@actions_done_avg_last_months_array, @actions_created_avg_last_months_array =
|
||||||
find_running_avg_array(@actions_done_last_months_array, @actions_created_last_months_array, @month_count+1)
|
find_running_avg_array(@actions_done_last_months_array, @actions_created_last_months_array, month_count+1)
|
||||||
|
|
||||||
# correct last two months since the data of last+1 and last+2 are not available for avg
|
# correct last two months since the data of last+1 and last+2 are not available for avg
|
||||||
correct_last_two_months(@actions_done_avg_last_months_array, @month_count)
|
correct_last_two_months(@actions_done_avg_last_months_array, month_count)
|
||||||
correct_last_two_months(@actions_created_avg_last_months_array, @month_count)
|
correct_last_two_months(@actions_created_avg_last_months_array, month_count)
|
||||||
|
|
||||||
# interpolate avg for this month.
|
# interpolate avg for this month.
|
||||||
percent_of_month = Time.zone.now.day.to_f / Time.zone.now.end_of_month.day.to_f
|
interpolate_avg_for_current_month(@actions_created_last_months_array, @actions_done_last_months_array)
|
||||||
@interpolated_actions_created_this_month = interpolate_avg(@actions_created_last_months_array, percent_of_month)
|
|
||||||
@interpolated_actions_done_this_month = interpolate_avg(@actions_done_last_months_array, percent_of_month)
|
@created_count_array = Array.new(month_count+1, actions_created_last_months.size/month_count)
|
||||||
|
@done_count_array = Array.new(month_count+1, actions_done_last_months.size/month_count)
|
||||||
|
@month_names = Array.new(month_count+1){ |i| t('date.month_names')[ (Time.now.mon - i -1 ) % 12 + 1 ]+ " " + (Time.now - i.months).year.to_s}
|
||||||
|
|
||||||
render :layout => false
|
render :layout => false
|
||||||
end
|
end
|
||||||
|
|
@ -449,7 +455,7 @@ class StatsController < ApplicationController
|
||||||
end
|
end
|
||||||
|
|
||||||
def interpolate_avg(set, percent)
|
def interpolate_avg(set, percent)
|
||||||
return (set[0]*(1/percent) + set[1] + set[2]) / 3.0
|
(set[0]*(1/percent) + set[1] + set[2]) / 3.0
|
||||||
end
|
end
|
||||||
|
|
||||||
def correct_last_two_months(month_data, count)
|
def correct_last_two_months(month_data, count)
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,3 @@
|
||||||
<%-
|
|
||||||
created_count_array = Array.new(@month_count+1){ |i| @actions_created_last_months.size/@month_count }
|
|
||||||
done_count_array = Array.new(@month_count+1){ |i| @actions_done_last_months.size/@month_count }
|
|
||||||
month_names = Array.new(@month_count+1){ |i| t('date.month_names')[ (Time.now.mon - i -1 ) % 12 + 1 ]+ " " + (Time.now - i.months).year.to_s}
|
|
||||||
-%>
|
|
||||||
&title=<%= t('stats.actions_last_year') %>,{font-size:16},&
|
&title=<%= t('stats.actions_last_year') %>,{font-size:16},&
|
||||||
&y_legend=<%= t('stats.actions_last_year_legend.number_of_actions') %>,12,0x736AFF&
|
&y_legend=<%= t('stats.actions_last_year_legend.number_of_actions') %>,12,0x736AFF&
|
||||||
&x_legend=<%= t('stats.actions_last_year_legend.months_ago') %>,12,0x736AFF&
|
&x_legend=<%= t('stats.actions_last_year_legend.months_ago') %>,12,0x736AFF&
|
||||||
|
|
@ -17,15 +12,15 @@ month_names = Array.new(@month_count+1){ |i| t('date.month_names')[ (Tim
|
||||||
&line_8=1,0x007700&
|
&line_8=1,0x007700&
|
||||||
&values=<%= @actions_created_last_months_array.join(",")%>&
|
&values=<%= @actions_created_last_months_array.join(",")%>&
|
||||||
&values_2=<%= @actions_done_last_months_array.join(",")%>&
|
&values_2=<%= @actions_done_last_months_array.join(",")%>&
|
||||||
&values_3=<%= created_count_array.join(",")%>&
|
&values_3=<%= @created_count_array.join(",")%>&
|
||||||
&values_4=<%= done_count_array.join(",")%>&
|
&values_4=<%= @done_count_array.join(",")%>&
|
||||||
&values_5=<%= @actions_created_avg_last_months_array.join(",")%>&
|
&values_5=<%= @actions_created_avg_last_months_array.join(",")%>&
|
||||||
&values_6=<%= @actions_done_avg_last_months_array.join(",")%>&
|
&values_6=<%= @actions_done_avg_last_months_array.join(",")%>&
|
||||||
&values_7=<%= @interpolated_actions_created_this_month%>,<%=@actions_done_avg_last_months_array[1]%>&
|
&values_7=<%= @interpolated_actions_created_this_month%>,<%=@actions_done_avg_last_months_array[1]%>&
|
||||||
&values_8=<%= @interpolated_actions_done_this_month%>,<%=@actions_created_avg_last_months_array[1]%>&
|
&values_8=<%= @interpolated_actions_done_this_month%>,<%=@actions_created_avg_last_months_array[1]%>&
|
||||||
&x_labels=<%= month_names.join(",")%>&
|
&x_labels=<%= @month_names.join(",")%>&
|
||||||
&y_min=0&
|
&y_min=0&
|
||||||
<% # add one to @max for people who have no actions completed yet.
|
<% # add one to @max for people who have no actions completed yet.
|
||||||
# OpenFlashChart cannot handle y_max=0 -%>
|
# OpenFlashChart cannot handle y_max=0 -%>
|
||||||
&y_max=<%=@max+@max/10+1-%>&
|
&y_max=<%=@max+@max/10+1-%>&
|
||||||
&x_label_style=9,,2,&
|
&x_label_style=9,,2,&
|
||||||
|
|
|
||||||
|
|
@ -172,9 +172,6 @@ class StatsControllerTest < ActionController::TestCase
|
||||||
|
|
||||||
# only tests difference with actions_done_last_12months_data
|
# only tests difference with actions_done_last_12months_data
|
||||||
|
|
||||||
# Then the count of months should be calculated
|
|
||||||
assert_equal 27, assigns['month_count'], "two years and three months of last todo"
|
|
||||||
|
|
||||||
# And the last two months are corrected
|
# And the last two months are corrected
|
||||||
assert_equal 2/3.0, assigns['actions_done_avg_last_months_array'][23]
|
assert_equal 2/3.0, assigns['actions_done_avg_last_months_array'][23]
|
||||||
assert_equal 2/3.0, assigns['actions_done_avg_last_months_array'][24]
|
assert_equal 2/3.0, assigns['actions_done_avg_last_months_array'][24]
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue