Merge pull request #195 from doncruse/master

Removing some intermediate values and shifting HTML formatting to a helper
This commit is contained in:
Matt Rogers 2013-07-22 19:44:18 -07:00
commit bcbfe42899
5 changed files with 69 additions and 63 deletions

View file

@ -13,39 +13,38 @@ 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).select("completed_at" )
actions_created_last12months = current_user.todos.created_after(@cut_off_year).select("created_at")
# - outermost set of entries needed for these calculations
actions_last12months = current_user.todos.created_or_completed_after(@cut_off_year_plus3).select("completed_at,created_at")
# convert to array and fill in non-existing months
@actions_done_last12months_array = convert_to_months_from_today_array(actions_done_last12months, 13, :completed_at)
@actions_created_last12months_array = convert_to_months_from_today_array(actions_created_last12months, 13, :created_at)
@actions_done_last12months_array = put_events_into_month_buckets(actions_last12months, 13, :completed_at)
@actions_created_last12months_array = put_events_into_month_buckets(actions_last12months, 13, :created_at)
# find max for graph in both arrays
@max = [@actions_done_last12months_array.max, @actions_created_last12months_array.max].max
@max = (@actions_done_last12months_array + @actions_created_last12months_array).max
# find running avg
actions_done_last12monthsPlus3 = current_user.todos.completed_after(@cut_off_year_plus3).select("completed_at" )
actions_created_last12monthsPlus3 = current_user.todos.created_after(@cut_off_year_plus3).select("created_at")
actions_done_last12monthsPlus3_array = convert_to_months_from_today_array(actions_done_last12monthsPlus3, 16, :completed_at)
actions_created_last12monthsPlus3_array = convert_to_months_from_today_array(actions_created_last12monthsPlus3, 16, :created_at)
done_in_last_15_months = put_events_into_month_buckets(actions_last12months, 16, :completed_at)
created_in_last_15_months = put_events_into_month_buckets(actions_last12months, 16, :created_at)
@actions_done_avg_last12months_array, @actions_created_avg_last12months_array =
find_running_avg_array(actions_done_last12monthsPlus3_array, actions_created_last12monthsPlus3_array, 13)
@actions_done_avg_last12months_array = compute_running_avg_array(done_in_last_15_months, 13)
@actions_created_avg_last12months_array = compute_running_avg_array(created_in_last_15_months, 13)
# interpolate avg for current month.
interpolate_avg_for_current_month(@actions_created_last12months_array, @actions_done_last12months_array)
@interpolated_actions_created_this_month = interpolate_avg_for_current_month(@actions_created_last12months_array)
@interpolated_actions_done_this_month = interpolate_avg_for_current_month(@actions_done_last12months_array)
@created_count_array = Array.new(13, actions_created_last12months.size/12.0)
@done_count_array = Array.new(13, actions_done_last12months.size/12.0)
@month_names = Array.new(13){ |i| t('date.month_names')[ (Time.now.mon - i -1 ) % 12 + 1 ]}
@created_count_array = Array.new(13, actions_last12months.created_after(@cut_off_year).count/12.0)
@done_count_array = Array.new(13, actions_last12months.completed_after(@cut_off_year).count/12.0)
render :layout => false
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)
def interpolate_avg_for_current_month(set)
(set[0]*(1/percent_of_month) + set[1] + set[2]) / 3.0
end
def percent_of_month
Time.zone.now.day / Time.zone.now.end_of_month.day.to_f
end
def actions_done_last_years
@ -54,34 +53,29 @@ class StatsController < ApplicationController
end
def actions_done_lastyears_data
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" )
# 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
actions_last_months = current_user.todos.select("completed_at,created_at")
month_count = difference_in_months(@today, actions_last_months.minimum(:created_at))
# because this action is not scoped by date, the minimum created_at should always be
# less than the minimum completed_at, so no reason to check minimum completed_at
# 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_created_last_months_array = convert_to_months_from_today_array(actions_created_last_months, month_count+1, :created_at)
@actions_done_last_months_array = put_events_into_month_buckets(actions_last_months, month_count+1, :completed_at)
@actions_created_last_months_array = put_events_into_month_buckets(actions_last_months, month_count+1, :created_at)
# 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 + @actions_created_last_months_array).max
# 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+1)
# 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_created_avg_last_months_array, month_count)
# set running avg
@actions_done_avg_last_months_array = compute_running_avg_array(@actions_done_last_months_array,month_count+1)
@actions_created_avg_last_months_array = compute_running_avg_array(@actions_created_last_months_array,month_count+1)
# interpolate avg for this month.
interpolate_avg_for_current_month(@actions_created_last_months_array, @actions_done_last_months_array)
@interpolated_actions_created_this_month = interpolate_avg_for_current_month(@actions_created_last_months_array)
@interpolated_actions_done_this_month = interpolate_avg_for_current_month(@actions_done_last_months_array)
@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}
@created_count_array = Array.new(month_count+1, actions_last_months.select { |x| x.created_at }.size/month_count)
@done_count_array = Array.new(month_count+1, actions_last_months.select { |x| x.completed_at }.size/month_count)
render :layout => false
end
@ -382,16 +376,15 @@ class StatsController < ApplicationController
# uses the supplied block to determine array of indexes in hash
# the block should return an array of indexes each is added to the hash and summed
def convert_to_array(records, upper_bound)
# use 0 to initialise action count to zero
a = Array.new(upper_bound){|i| 0 }
a = Array.new(upper_bound, 0)
records.each { |r| (yield r).each { |i| a[i] += 1 } }
return a
a
end
def convert_to_months_from_today_array(records, array_size, date_method_on_todo)
return convert_to_array(records, array_size){ |r| [difference_in_months(@today, r.send(date_method_on_todo))]}
def put_events_into_month_buckets(records, array_size, date_method_on_todo)
convert_to_array(records.select { |x| x.send(date_method_on_todo) }, array_size) { |r| [difference_in_months(@today, r.send(date_method_on_todo))]}
end
def convert_to_days_from_today_array(records, array_size, date_method_on_todo)
return convert_to_array(records, array_size){ |r| [difference_in_days(@today, r.send(date_method_on_todo))]}
end
@ -455,24 +448,20 @@ class StatsController < ApplicationController
end
def three_month_avg(set, i)
return ( (set[i]||0) + (set[i+1]||0) + (set[i+2]||0) ) / 3.0
(set.fetch(i,0) + set.fetch(i+1,0) + set.fetch(i+2,0)) / 3.0
end
def interpolate_avg(set, percent)
(set[0]*(1/percent) + set[1] + set[2]) / 3.0
def set_three_month_avg(set,upper_bound)
(0..upper_bound-1).map { |i| three_month_avg(set, i) }
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_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
# sets "null" on first column and - if necessary - cleans up last two columns, which may have insufficient data
def compute_running_avg_array(set, upper_bound)
result = set_three_month_avg(set, upper_bound)
result[upper_bound-1] = result[upper_bound-1] * 3 if upper_bound == set.length
result[upper_bound-2] = result[upper_bound-2] * 3 / 2 if upper_bound > 1 and upper_bound == set.length
result[0] = "null"
result
end # unsolved, not triggered, edge case for set.length == upper_bound + 1
end

View file

@ -4,4 +4,20 @@ module StatsHelper
9 + 2 * cloud.relative_size(tag)
end
def month_and_year_label(i)
t('date.month_names')[ (Time.now.mon - i -1 ) % 12 + 1 ]+ " " + (Time.now - i.months).year.to_s
end
def array_of_month_and_year_labels(count)
Array.new(count) { |i| month_and_year_label(i) }
end
def month_label(i)
t('date.month_names')[ (Time.now.mon - i -1 ) % 12 + 1 ]
end
def array_of_month_labels(count)
Array.new(count) { |i| month_label(i) }
end
end

View file

@ -48,6 +48,7 @@ class Todo < ActiveRecord::Base
scope :completed_before, lambda { |date| where("todos.completed_at < ?", date) }
scope :created_after, lambda { |date| where("todos.created_at > ?", date) }
scope :created_before, lambda { |date| where("todos.created_at < ?", date) }
scope :created_or_completed_after, lambda { |date| where("todos.created_at > ? or todos.completed_at > ?", date, date) }
def self.due_after(date)
where('todos.due > ?', date)

View file

@ -23,7 +23,7 @@
&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(",")%>&
&x_labels=<%= array_of_month_labels(@done_count_array.size).join(",")%>&
&y_min=0&
<% # add one to @max for people who have no actions completed yet.
# OpenFlashChart cannot handle y_max=0 -%>

View file

@ -18,7 +18,7 @@
&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(",")%>&
&x_labels=<%= array_of_month_and_year_labels(@done_count_array.size).join(",")%>&
&y_min=0&
<% # add one to @max for people who have no actions completed yet.
# OpenFlashChart cannot handle y_max=0 -%>