further refactorings and tests

one test is failing, will fix that next time
This commit is contained in:
Reinier Balt 2011-11-28 23:38:57 +01:00
parent 80d8d2b67a
commit 2349bee535
8 changed files with 289 additions and 182 deletions

View file

@ -23,33 +23,28 @@ class StatsController < ApplicationController
def actions_done_last12months_data
# get actions created and completed in the past 12+3 months. +3 for running
# average
@actions_done_last12months = current_user.todos.completed_after(@cut_off_year_plus3).find(:all, { :select => "completed_at" })
@actions_created_last12months = current_user.todos.created_after(@cut_off_year_plus3).find(:all, { :select => "created_at"})
@actions_done_last12months = current_user.todos.completed_after(@cut_off_year).find(:all, { :select => "completed_at" })
@actions_created_last12months = current_user.todos.created_after(@cut_off_year).find(:all, { :select => "created_at"})
@actions_done_last12monthsPlus3 = current_user.todos.completed_after(@cut_off_year_plus3).find(:all, { :select => "completed_at" })
@actions_created_last12monthsPlus3 = current_user.todos.created_after(@cut_off_year_plus3).find(:all, { :select => "created_at"})
# convert to hash to be able to fill in non-existing months
@actions_done_last12months_hash = convert_to_hash(@actions_done_last12months, :difference_in_months, :completed_at)
@actions_created_last12months_hash = convert_to_hash(@actions_created_last12months, :difference_in_months, :created_at)
# convert to array and fill in non-existing months
@actions_done_last12months_array = convert_to_array(convert_to_hash(@actions_done_last12months, :difference_in_months, :completed_at),13)
@actions_created_last12months_array = convert_to_array(convert_to_hash(@actions_created_last12months, :difference_in_months, :created_at),13)
@actions_done_last12monthsPlus3_array = convert_to_array(convert_to_hash(@actions_done_last12monthsPlus3, :difference_in_months, :completed_at),15)
@actions_created_last12monthsPlus3_array = convert_to_array(convert_to_hash(@actions_created_last12monthsPlus3, :difference_in_months, :created_at),15)
# find max for graph in both arrays
@max = [@actions_done_last12months_array.max, @actions_created_last12months_array.max].max
# find max for graph in both hashes
@sum_actions_done_last12months, @sum_actions_created_last12months, @max = 0, 0, 0
0.upto 13 do |i|
@sum_actions_done_last12months += @actions_done_last12months_hash[i]
@sum_actions_created_last12months += @actions_created_last12months_hash[i]
@max = [@actions_done_last12months_hash[i], @actions_created_last12months_hash[i], @max].max
end
# find running avg for month i by calculating avg of month i and the two
# after them. Ignore current month [0] because you do not have full data for it
@actions_done_avg_last12months_hash, @actions_created_avg_last12months_hash = Hash.new("null"), Hash.new("null")
1.upto(12) do |i|
@actions_done_avg_last12months_hash[i] = three_month_avg(@actions_done_last12months_hash, i)
@actions_created_avg_last12months_hash[i] = three_month_avg(@actions_created_last12months_hash, i)
end
# find running avg
@actions_done_avg_last12months_array, @actions_created_avg_last12months_array =
find_running_avg_array(@actions_done_last12monthsPlus3_array, @actions_created_last12monthsPlus3_array, 13)
# interpolate avg for current month.
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(@actions_created_last12months_hash, percent_of_month)
@interpolated_actions_done_this_month = interpolate_avg(@actions_done_last12months_hash, percent_of_month)
@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
end
@ -61,47 +56,36 @@ class StatsController < ApplicationController
end
def actions_done_lastyears_data
@actions = current_user.todos
@actions_done_last_months = current_user.todos.completed.find(:all, { :select => "completed_at", :order => "completed_at DESC" })
@actions_created_last_months = current_user.todos.find(:all, { :select => "created_at", :order => "created_at DESC" })
# 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),
difference_in_months(@today, @actions_done_last_months.last.completed_at)].max
difference_in_months(@today, @actions_done_last_months.last.completed_at)].max + 1
# convert to hash to be able to fill in non-existing months
@actions_done_last_months_hash = convert_to_hash(@actions_done_last_months, :difference_in_months, :completed_at)
@actions_created_last_months_hash = convert_to_hash(@actions_created_last_months, :difference_in_months, :created_at)
# convert to array and fill in non-existing months
@actions_done_last_months_array = convert_to_array(convert_to_hash(@actions_done_last_months, :difference_in_months, :completed_at), @month_count)
@actions_created_last_months_array = convert_to_array(convert_to_hash(@actions_created_last_months, :difference_in_months, :created_at), @month_count)
# find max for graph in both hashes
@sum_actions_done_last_months, @sum_actions_created_last_months, @max = 0, 0, 0
0.upto @month_count do |i|
@sum_actions_done_last_months += @actions_done_last_months_hash[i]
@sum_actions_created_last_months += @actions_created_last_months_hash[i]
@max = [@actions_done_last_months_hash[i], @actions_created_last_months_hash[i], @max].max
end
@max = [@actions_done_last_months_array.max, @actions_created_last_months_array.max].max
# find running avg for month i by calculating avg of month i and the two
# after them. Ignore current month because you do not have full data for it
@actions_done_avg_last_months_hash, @actions_created_avg_last_months_hash = Hash.new("null"), Hash.new("null")
1.upto(@month_count) do |i|
@actions_done_avg_last_months_hash[i] = three_month_avg(@actions_done_last_months_hash, i)
@actions_created_avg_last_months_hash[i] = three_month_avg(@actions_created_last_months_hash, i)
end
# find running avg
@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)
# correct last two months
correct_last_two_months(@actions_done_avg_last_months_hash, @month_count)
correct_last_two_months(@actions_created_avg_last_months_hash, @month_count)
# 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-1)
correct_last_two_months(@actions_created_avg_last_months_array, @month_count-1)
# interpolate avg for this month.
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(@actions_created_last_months_hash, percent_of_month)
@interpolated_actions_done_this_month = interpolate_avg(@actions_done_last_months_hash, percent_of_month)
@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)
render :layout => false
end
def actions_done_last30days_data
# get actions created and completed in the past 30 days.
@actions_done_last30days = current_user.todos.completed_after(@cut_off_month).find(:all, { :select => "completed_at" })
@ -114,33 +98,21 @@ class StatsController < ApplicationController
@actions_created_last30days_hash = convert_to_hash(@actions_created_last30days, :difference_in_days, :created_at)
# find max for graph in both hashes
@sum_actions_done_last30days, @sum_actions_created_last30days, @max = 0, 0, 0
0.upto(30) do |i|
@sum_actions_done_last30days += @actions_done_last30days_hash[i]
@sum_actions_created_last30days += @actions_created_last30days_hash[i]
@max = [ @actions_done_last30days_hash[i], @actions_created_last30days_hash[i], @max].max
end
@max = [find_max_in_hash(@actions_done_last30days_hash, 30), find_max_in_hash(@actions_created_last30days_hash, 30)].max
render :layout => false
end
def actions_completion_time_data
@actions_completion_time = current_user.todos.completed.find(:all, { :select => "completed_at, created_at" })
@actions_completion_time = current_user.todos.completed.find(:all, { :select => "completed_at, created_at", :order => "completed_at DESC" })
# convert to hash to be able to fill in non-existing days in
# @actions_completion_time also convert days to weeks (/7)
@actions_completion_time_hash,@max_days, @max_actions, @sum_actions=Hash.new(0), 0,0,0
@actions_completion_time.each do |r|
days = (r.completed_at - r.created_at) / @seconds_per_day
weeks = (days/7).to_i
@actions_completion_time_hash[weeks] += 1
@max_days= [days, @max_days].max
@max_actions = [@actions_completion_time_hash[weeks], @max_actions].max
@sum_actions += 1
end
@actions_completion_time_hash = convert_to_week_hash(@actions_completion_time)
@max_weeks = difference_in_weeks(@today, @actions_completion_time.last.completed_at)
@max_actions = find_max_in_hash(@actions_completion_time_hash, @max_weeks)
# stop the chart after 10 weeks
@cut_off = 10
@ -148,23 +120,11 @@ class StatsController < ApplicationController
end
def actions_running_time_data
@actions_running_time = current_user.todos.not_completed.find(:all, { :select => "created_at" })
@actions_running_time = current_user.todos.not_completed.find(:all, { :select => "created_at", :order => "created_at DESC" })
# convert to hash to be able to fill in non-existing days in
# @actions_running_time also convert days to weeks (/7)
@max_days, @max_actions, @sum_actions=0,0,0
@actions_running_time_hash = Hash.new(0)
@actions_running_time.each do |r|
days = (@today - r.created_at) / @seconds_per_day
weeks = (days/7).to_i
@actions_running_time_hash[weeks] += 1
@max_days=[days,@max_days].max
@max_actions = [@actions_running_time_hash[weeks], @max_actions].max
@sum_actions += 1
end
@actions_running_time_hash = convert_to_week_hash_today(@actions_running_time)
@max_weeks = difference_in_weeks(@today, @actions_running_time.last.created_at)
@max_actions = find_max_in_hash(@actions_running_time_hash, @max_weeks)
# cut off chart at 52 weeks = one year
@cut_off=52
@ -181,22 +141,12 @@ class StatsController < ApplicationController
# - actions not deferred (show_from must be null)
# - actions not pending/blocked
@actions_running_time = current_user.todos.not_completed.not_hidden.not_deferred_or_blocked.find(:all, :select => "todos.created_at")
@actions_running_time = current_user.todos.not_completed.not_hidden.not_deferred_or_blocked.find(
:all, :select => "todos.created_at", :order => "todos.created_at DESC")
# convert to hash to be able to fill in non-existing days in
# @actions_running_time also convert days to weeks (/7)
@max_days, @max_actions, @sum_actions=0,0,0
@actions_running_time_hash = Hash.new(0)
@actions_running_time.each do |r|
days = (@today - r.created_at) / @seconds_per_day
weeks = (days/7).to_i
@actions_running_time_hash[weeks] += 1
@max_days=[days, @max_days].max
@max_actions = [@actions_running_time_hash[weeks], @max_actions].max
@sum_actions += 1
end
@actions_running_time_hash = convert_to_week_hash_today(@actions_running_time)
@max_weeks = difference_in_weeks(@today, @actions_running_time.last.created_at)
@max_actions = find_max_in_hash(@actions_running_time_hash, @max_weeks)
# cut off chart at 52 weeks = one year
@cut_off=52
@ -204,7 +154,6 @@ class StatsController < ApplicationController
render :layout => false
end
def context_total_actions_data
# get total action count per context Went from GROUP BY c.id to c.name for
# compatibility with postgresql. Since the name is forced to be unique, this
@ -674,6 +623,10 @@ class StatsController < ApplicationController
return selected_todo_ids, count
end
def convert_to_array(hash, upper_bound)
return Array.new(upper_bound){ |i| hash[i] }
end
def convert_to_hash(records, difference_method, date_method_on_todo)
# use 0 to initialise action count to zero
hash = Hash.new(0)
@ -681,6 +634,24 @@ class StatsController < ApplicationController
return hash
end
def convert_to_week_hash(records)
hash = Hash.new(0)
records.each do |r|
index = difference_in_weeks(r.completed_at, r.created_at)
hash[index] += 1
end
return hash
end
def convert_to_week_hash_today(records)
hash = Hash.new(0)
records.each do |r|
index = difference_in_weeks(@today, r.created_at)
hash[index] += 1
end
return hash
end
# assumes date1 > date2
def difference_in_months(date1, date2)
return (date1.year - date2.year)*12 + (date1.month - date2.month)
@ -690,18 +661,50 @@ class StatsController < ApplicationController
def difference_in_days(date1, date2)
return ((date1.at_midnight-date2.at_midnight)/@seconds_per_day).to_i
end
def three_month_avg(hash, i)
return (hash[i] + hash[i+1] + hash[i+2])/3.0
def difference_in_weeks(date1, date2)
return difference_in_days(date1, date2) / 7
end
def interpolate_avg(hash, percent)
return (hash[0]*percent + hash[1] + hash[2]) / 3.0
def three_month_avg(set, i)
return ( (set[i]||0) + (set[i+1]||0) + (set[i+2]||0) )/3.0
end
def interpolate_avg(set, percent)
return (set[0]*percent + set[1] + set[2]) / 3.0
end
def correct_last_two_months(month_data, count)
month_data[count] = month_data[count] * 3
month_data[count-1] = month_data[count-1] * 3 / 2 if count > 1
end
def find_max_in_hash(hash, upper_bound)
max = hash[0]
1.upto(upper_bound){ |i| max = [hash[i], max].max }
return max
end
def find_running_avg(done_hash, created_hash, upper_bound)
avg_done, avg_created = Hash.new("null"), Hash.new("null")
# find running avg for month i by calculating avg of month i and the two
# after them. Ignore current month [0] because you do not have full data for it
1.upto(upper_bound) do |i|
avg_done[i] = three_month_avg(done_hash, i)
avg_created[i] = three_month_avg(created_hash, i)
end
return avg_done, avg_created
end
def find_running_avg_array(done_array, created_array, upper_bound)
avg_done = Array.new(upper_bound){ |i| three_month_avg(done_array,i) }
avg_created = Array.new(upper_bound){ |i| three_month_avg(created_array,i) }
avg_done[0] = avg_created[0] = "null"
return avg_done, avg_created
end
end

View file

@ -5,33 +5,32 @@
&y_ticks=5,10,5&
&filled_bar=50,0x9933CC,0x8010A0&
&values=
<% @count = @max_days > @cut_off*7 ? @cut_off : @max_days/7
@count = @count.to_i
0.upto @count-1 do |i| -%>
<% @count = @max_weeks > @cut_off ? @cut_off : @max_weeks
0.upto @count.to_i-1 do |i| -%>
<%= @actions_completion_time_hash[i] -%>,
<% end -%>
<%
<%
@sum=0
@count.upto @max_days/7 do |i|
@count.upto @max_weeks do |i|
@sum += @actions_completion_time_hash[i]
end -%>
<%=@sum%>&
&line_2=2,0xFF0000&
&values_2=
<% total=0
@count = @max_days > @cut_off*7 ? @cut_off : @max_days/7
@count = @max_weeks > @cut_off ? @cut_off : @max_weeks
0.upto @count-1 do |i|
total += @actions_completion_time_hash[i]*100.0/@sum_actions -%>
total += @actions_completion_time_hash[i]*100.0/@actions_completion_time.count -%>
<%= total -%>,
<% end -%>
<%= total+@sum*100.0/@sum_actions%>&
<%= total+@sum*100.0/@actions_completion_time.count%>&
&x_labels=within 1,
<% 1.upto @count-1 do |i| -%>
<%= i %>-<%= i+1 %>,
<% end -%>
> <%= @count %>&
&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 -%>
&y_max=<%=1+@max_actions+@max_actions/10-%>&
&show_y2=true&

View file

@ -1,3 +1,9 @@
<%-
url_array = Array.new(13){ |i| url_for :controller => 'stats', :action => 'actions_done_last_years'}
created_count_array = Array.new(13){ |i| @actions_created_last12months.count/12.0 }
done_count_array = Array.new(13){ |i| @actions_done_last12months.count/12.0 }
month_names = Array.new(13){ |i| Date::MONTHNAMES[ (Time.now.mon - i -1 ) % 12 + 1 ]}
-%>
&title=<%= t('stats.actions_lastyear_title') %>,{font-size:16},&
&y_legend=<%= t('stats.legend.number_of_actions') %>,12,0x736AFF&
&x_legend=<%= t('stats.legend.months_ago') %>,12,0x736AFF&
@ -10,22 +16,19 @@
&line_6=2,0xAA0000, <%= t('stats.labels.month_avg_completed', :months => 3) %>, 9&
&line_7=1,0xAA0000&
&line_8=1,0x007700&
&values=<% 0.upto 11 do |i| -%><%= @actions_created_last12months_hash[i]%>,<% end -%><%= @actions_created_last12months_hash[12]%>&
&links=<% 0.upto 11 do |i| -%><%= url_for :controller => 'stats', :action => 'actions_done_last_years' %>,<% end -%><%= url_for :controller => 'stats', :action => 'actions_done_last_years' %>&
&links_2=<% 0.upto 11 do |i| -%><%= url_for :controller => 'stats', :action => 'actions_done_last_years' %>,<% end -%><%= url_for :controller => 'stats', :action => 'actions_done_last_years' %>&
&values_2=<% 0.upto 11 do |i| -%><%= @actions_done_last12months_hash[i]%>,<% end -%><%= @actions_done_last12months_hash[12]%>&
&values_3=<%0.upto 11 do |i| -%><%=@sum_actions_created_last12months/12.0-%>,<%end-%><%=@sum_actions_created_last12months/12.0-%>&
&values_4=<%0.upto 11 do |i| -%><%=@sum_actions_done_last12months/12.0-%>,<%end-%><%=@sum_actions_done_last12months/12.0-%>&
&values_5=<%0.upto 11 do |i| -%><%=@actions_created_avg_last12months_hash[i]-%>,<%end-%><%=@actions_created_avg_last12months_hash[12]-%>&
&values_6=<%0.upto 11 do |i| -%><%=@actions_done_avg_last12months_hash[i]-%>,<%end-%><%=@actions_done_avg_last12months_hash[12]-%>&
&values_7=<%=@interpolated_actions_created_this_month%>,<%=@actions_done_avg_last12months_hash[1]%>&
&values_8=<%=@interpolated_actions_done_this_month%>,<%=@actions_created_avg_last12months_hash[1]%>&
&x_labels=<%0.upto 11 do |i| -%>
<%= Date::MONTHNAMES[ (Time.now.mon - i -1 ) % 12 + 1 ] -%>,
<% end -%>
<%= Date::MONTHNAMES[(Time.now.mon - 12 -1 ) % 12 + 1] -%>&
&values=<%= @actions_created_last12months_array.join(",")%>&
&links=<%= url_array.join(",")%>&
&links_2=<%= url_array.join(",")%>&
&values_2=<%= @actions_done_last12months_array.join(",")%>&
&values_3=<%= created_count_array.join(",")%>&
&values_4=<%= done_count_array.join(",")%>&
&values_5=<%= @actions_created_avg_last12months_array.join(",")%>&
&values_6=<%= @actions_done_avg_last12months_array.join(",")%>&
&values_7=<%= @interpolated_actions_created_this_month%>,<%=@actions_done_avg_last12months_array[1]%>&
&values_8=<%= @interpolated_actions_done_this_month%>,<%=@actions_created_avg_last12months_array[1]%>&
&x_labels=<%= month_names.join(",")%>&
&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 -%>
&y_max=<%=@max+@max/10+1-%>&
&x_label_style=9,,2,&

View file

@ -16,18 +16,18 @@
<% end -%><%= @actions_done_last30days_hash[30]%>&
&values_3=
<%0.upto 29 do |i| -%>
<%=@sum_actions_created_last30days/30.0-%>,
<%=@actions_created_last30days.count/30.0-%>,
<%end-%>
<%=@sum_actions_created_last30days/30.0-%>&
<%=@actions_created_last30days.count/30.0-%>&
&values_4=
<%0.upto 29 do |i| -%>
<%=@sum_actions_done_last30days/30.0-%>,
<%=@actions_done_last30days.count/30.0-%>,
<%end-%>
<%=@sum_actions_done_last30days/30.0-%>&
<%=@actions_done_last30days.count/30.0-%>&
&x_labels=
<%0.upto 29 do |i|
<%0.upto 29 do |i|
seconds = i * 24 * 60 * 60
delta = Time.now-seconds
delta = Time.now-seconds
-%>
<%= delta.strftime("%a %d-%m") -%>,
<% end
@ -36,7 +36,7 @@
<%= delta.strftime("%a %d-%m") -%>&
&y_min=0&
<% # max + 10% for some extra space at the top
# 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 -%>
&y_max=<%=@max+@max/10+1 -%>&
&x_label_style=9,,2,3&

View file

@ -1,3 +1,8 @@
<%-
created_count_array = Array.new(@month_count){ |i| @actions_created_last_months.count/@month_count }
done_count_array = Array.new(@month_count){ |i| @actions_done_last_months.count/@month_count }
month_names = Array.new(@month_count){ |i| Date::MONTHNAMES[ (Time.now.mon - i -1 ) % 12 + 1 ]+ " " + (Time.now - i.months).year.to_s}
-%>
&title=<%= t('stats.actions_last_year') %>,{font-size:16},&
&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&
@ -10,20 +15,17 @@
&line_6=2,0xAA0000, <%= t('stats.labels.month_avg_completed', :months => 3) %>, 9&
&line_7=1,0xAA0000&
&line_8=1,0x007700&
&values=<% 0.upto @month_count-1 do |i| -%><%= @actions_created_last_months_hash[i]%>,<% end -%><%= @actions_created_last_months_hash[@month_count]%>&
&values_2=<% 0.upto @month_count-1 do |i| -%><%= @actions_done_last_months_hash[i]%>,<% end -%><%= @actions_done_last_months_hash[@month_count]%>&
&values_3=<%0.upto @month_count-1 do |i| -%><%=@sum_actions_created_last_months/@month_count-%>,<%end-%><%=@sum_actions_created_last_months/@month_count-%>&
&values_4=<%0.upto @month_count-1 do |i| -%><%=@sum_actions_done_last_months/@month_count-%>,<%end-%><%=@sum_actions_done_last_months/@month_count-%>&
&values_5=<%0.upto @month_count-1 do |i| -%><%=@actions_created_avg_last_months_hash[i]-%>,<%end-%><%=@actions_created_avg_last_months_hash[@month_count]-%>&
&values_6=<%0.upto @month_count-1 do |i| -%><%=@actions_done_avg_last_months_hash[i]-%>,<%end-%><%=@actions_done_avg_last_months_hash[@month_count]-%>&
&values_7=<%=@interpolated_actions_created_this_month%>,<%=@actions_done_avg_last_months_hash[1]%>&
&values_8=<%=@interpolated_actions_done_this_month%>,<%=@actions_created_avg_last_months_hash[1]%>&
&x_labels=<%0.upto @month_count-1 do |i| -%>
<%= Date::MONTHNAMES[ (Time.now.mon - i -1 ) % 12 + 1 ] + " " + (Time.now - i.months).year.to_s -%>,
<% end -%>
<%= Date::MONTHNAMES[(Time.now.mon - @month_count -1 ) % 12 + 1] + " " + (Time.now - @month_count.months).year.to_s -%>&
&values=<%= @actions_created_last_months_array.join(",")%>&
&values_2=<%= @actions_done_last_months_array.join(",")%>&
&values_3=<%= created_count_array.join(",")%>&
&values_4=<%= done_count_array.join(",")%>&
&values_5=<%= @actions_created_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_8=<%= @interpolated_actions_done_this_month%>,<%=@actions_created_avg_last_months_array[1]%>&
&x_labels=<%= month_names.join(",")%>&
&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 -%>
&y_max=<%=@max+@max/10+1-%>&
&x_label_style=9,,2,&

View file

@ -5,13 +5,13 @@
&y_ticks=5,10,5&
&filled_bar=50,0x9933CC,0x8010A0&
&values=
<% @count = @max_days > @cut_off*7 ? @cut_off : (@max_days/7).to_i
0.upto @count-1 do |i| -%>
<% @count = @max_weeks > @cut_off ? @cut_off : @max_weeks
0.upto @count.to_i-1 do |i| -%>
<%= @actions_running_time_hash[i] -%>,
<% end -%>
<%
<%
@sum=0
@count.upto((@max_days/7).to_i) {|i| @sum += @actions_running_time_hash[i]} -%>
@count.upto(@max_weeks.to_i) {|i| @sum += @actions_running_time_hash[i]} -%>
<%=@sum%>&
&links=<%
0.upto(@count-1) { |i| %><%= url_for :controller => 'stats', :action => 'show_selected_actions_from_chart', :index => i, :id=> "art" %>, <% }
@ -19,20 +19,20 @@
&line_2=2,0xFF0000&
&values_2=
<% total=0
@count = @max_days > @cut_off*7 ? @cut_off : (@max_days/7).to_i
0.upto @count-1 do |i|
@count = @max_weeks > @cut_off ? @cut_off : @max_weeks
0.upto @count.to_i-1 do |i|
total += @actions_running_time_hash[i] -%>
<%= total*100.0/@sum_actions -%>,
<%= total*100.0/@actions_running_time.count -%>,
<% end -%>
<%= (total+@sum)*100.0/@sum_actions%>&
<%= (total+@sum)*100.0/@actions_running_time.count%>&
&x_labels=< 1,
<% 1.upto @count-1 do |i| -%>
<%= i %>-<%= i+1 %>,
<% end -%>
><%=@count-%>&
&y_min=0&
<% @max_actions = @sum > @max_actions ? @sum : @max_actions -%>
<% # add one to @max for people who have no actions completed yet.
<% @max_actions = [@sum,@max_actions].max -%>
<% # add one to @max for people who have no actions completed yet.
# OpenFlashChart cannot handle y_max=0 -%>
&y_max=<%=1+@max_actions+@max_actions/10-%>&
&x_label_style=9,,2,2&

View file

@ -5,11 +5,11 @@
&y_ticks=5,10,5&
&filled_bar=50,0x9933CC,0x8010A0&
&values=
<% @count = @max_days > @cut_off*7 ? @cut_off : (@max_days/7).to_i
<% @count = @max_weeks > @cut_off ? @cut_off : @max_weeks
0.upto(@count-1) { |i| -%><%= @actions_running_time_hash[i] -%>,<% } %>
<%
<%
@sum=0
@count.upto((@max_days/7).to_i) { |i| @sum += @actions_running_time_hash[i] } -%>
@count.upto(@max_weeks.to_i) { |i| @sum += @actions_running_time_hash[i] } -%>
<%=@sum%>&
&links=<%
0.upto(@count-1) { |i| %><%= url_for :controller => 'stats', :action => 'show_selected_actions_from_chart', :index => i, :id=> "avrt" %>, <% }
@ -17,12 +17,12 @@
&line_2=2,0xFF0000&
&values_2=
<% total=0
@count = @max_days > @cut_off*7 ? @cut_off : (@max_days/7).to_i
0.upto @count-1 do |i|
@count = @max_weeks > @cut_off ? @cut_off : @max_weeks
0.upto @count-1 do |i|
total += @actions_running_time_hash[i] -%>
<%= total*100.0/@sum_actions -%>,
<%= total*100.0/@actions_running_time.count -%>,
<% end -%>
<%= (total+@sum)*100.0/@sum_actions%>&
<%= (total+@sum)*100.0/@actions_running_time.count%>&
&x_labels=< 1,
<% 1.upto @count-1 do |i| -%>
<%= i %>-<%= i+1 %>,
@ -30,7 +30,7 @@
><%=@count-%>&
&y_min=0&
<% @max_actions = @sum > @max_actions ? @sum : @max_actions -%>
<% # 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 -%>
&y_max=<%=1+@max_actions+@max_actions/10-%>&
&x_label_style=9,,2,2&

View file

@ -100,17 +100,7 @@ class StatsControllerTest < ActionController::TestCase
@current_user = User.find(users(:admin_user).id)
@current_user.todos.delete_all
# Given two todos created today
todo_today1 = @current_user.todos.create!(:description => "created today1", :context => contexts(:office))
todo_today2 = @current_user.todos.create!(:description => "created today2", :context => contexts(:office))
# And a todo created a month ago
todo_month1 = create_todo_in_past(1.month+1.day)
# And a todo created two months ago
todo_month2 = create_completed_todo_in_past(2.months+1.day, 2.months+2.days)
# And a todo created three months ago
todo_month3 = create_todo_in_past(3.months+1.day)
# And a todo created over a year ago
todo_year = create_todo_in_past(2.years+1.day)
given_todos_for_stats
# When I get the chart data
get :actions_done_last12months_data
@ -119,19 +109,129 @@ class StatsControllerTest < ActionController::TestCase
# Then the todos for the chart should be retrieved
assert_not_nil assigns['actions_done_last12months']
assert_not_nil assigns['actions_created_last12months']
assert_equal 5, assigns['actions_created_last12months'].count, "very old todo should not be retrieved"
assert_equal 7, assigns['actions_created_last12months'].count, "very old todo should not be retrieved"
# And they should be totalled in a hash
assert_equal 2, assigns['actions_created_last12months_hash'][0], "there should be two todos in current month"
assert_equal 1, assigns['actions_created_last12months_hash'][1], "there should be one todo in previous month"
assert_equal 1, assigns['actions_created_last12months_hash'][2], "there should be one todo in two month ago"
assert_equal 1, assigns['actions_created_last12months_hash'][3], "there should be one todo in three month ago"
assert_equal 2, assigns['actions_created_last12months_array'][0], "there should be two todos in current month"
assert_equal 1, assigns['actions_created_last12months_array'][1], "there should be one todo in previous month"
assert_equal 1, assigns['actions_created_last12months_array'][2], "there should be one todo in two month ago"
assert_equal 1, assigns['actions_created_last12months_array'][3], "there should be one todo in three month ago"
assert_equal 2, assigns['actions_created_last12months_array'][4], "there should be two todos (1 created & 1 done) in four month ago"
assert_equal 1, assigns['actions_done_last12months_hash'][2], "there should be one completed todo in two month ago"
assert_equal 1, assigns['actions_done_last12months_array'][2], "there should be one completed todo in last three months"
assert_equal 1, assigns['actions_done_last12months_array'][4], "there should be one completed todo in last four months"
# And they should be averaged
# And they should be averaged over three months
assert_equal 1/3.0, assigns['actions_done_avg_last12months_array'][1], "fourth month should be excluded"
assert_equal 2/3.0, assigns['actions_done_avg_last12months_array'][2], "fourth month should be included"
assert_equal 1.0, assigns['actions_created_avg_last12months_array'][1], "one every month"
assert_equal 4/3.0, assigns['actions_created_avg_last12months_array'][2], "two in fourth month"
# And the current month should be interpolated
fraction = Time.zone.now.day.to_f / Time.zone.now.end_of_month.day.to_f
assert_equal (2*fraction+2)/3.0, assigns['interpolated_actions_created_this_month'], "two this month and one in the last two months"
assert_equal 1/3.0, assigns['interpolated_actions_done_this_month'], "none this month and one in the last two months"
# And totals should be calculated
assert_equal 2, assigns['max'], "max of created or completed todos"
end
def test_actions_done_lastyears_data
login_as(:admin_user)
@current_user = User.find(users(:admin_user).id)
@current_user.todos.delete_all
given_todos_for_stats
# When I get the chart data
get :actions_done_lastyears_data
assert_response :success
# only tests difference with actions_done_last_12months_data
# Then the count of months should be calculated
assert_equal 24, assigns['month_count']
# And the last two months are corrected
assert_equal 0.5, assigns['actions_done_avg_last_months_hash'][23]
assert_equal 1.0, assigns['actions_done_avg_last_months_hash'][24]
end
def test_actions_completion_time_data
login_as(:admin_user)
@current_user = User.find(users(:admin_user).id)
@current_user.todos.delete_all
given_todos_for_stats
# When I get the chart data
get :actions_completion_time_data
assert_response :success
# do not test stuff already implicitly tested in other tests
assert_equal 104, assigns['max_weeks'], "two years is 104 weeks"
end
def test_actions_running_time_data
login_as(:admin_user)
@current_user = User.find(users(:admin_user).id)
@current_user.todos.delete_all
given_todos_for_stats
# When I get the chart data
get :actions_running_time_data
assert_response :success
# do not test stuff already implicitly tested in other tests
assert_equal 17, assigns['max_weeks'], "there are action in the first 17 weeks of this year"
end
def test_actions_visible_running_time_data
login_as(:admin_user)
@current_user = User.find(users(:admin_user).id)
@current_user.todos.delete_all
given_todos_for_stats
# When I get the chart data
get :actions_visible_running_time_data
login_as(:admin_user)
@current_user = User.find(users(:admin_user).id)
@current_user.todos.delete_all
given_todos_for_stats
# When I get the chart data
get :actions_running_time_data
assert_response :success
# do not test stuff already implicitly tested in other tests
assert_equal 17, assigns['max_weeks'], "there are action in the first 17 weeks of this year"
end
private
def given_todos_for_stats
# Given two todos created today
@todo_today1 = @current_user.todos.create!(:description => "created today1", :context => contexts(:office))
@todo_today2 = @current_user.todos.create!(:description => "created today2", :context => contexts(:office))
# And a todo created a month ago
@todo_month1 = create_todo_in_past(1.month+1.day)
# And a todo created two months ago
@todo_month2 = create_completed_todo_in_past(2.months+1.day, 2.months+2.days)
# And a todo created three months ago
@todo_month3 = create_todo_in_past(3.months+1.day)
# And a todo created four months ago
@todo_month4 = create_todo_in_past(4.months+1.day)
# And a todo created four months ago
@todo_month5 = create_completed_todo_in_past(4.months+1.day, 4.months+2.days)
# And a todo created over a year ago
@todo_year = create_completed_todo_in_past(2.years+1.day, 2.years+2.day)
end
def create_todo_in_past(creation_time_in_past)