first refactoring of stats controller

This commit is contained in:
Reinier Balt 2011-11-19 02:41:06 +01:00
parent 4705aca8dc
commit f74370aab5
6 changed files with 260 additions and 315 deletions

View file

@ -260,24 +260,26 @@ class ApplicationController < ActionController::Base
self.class.prefered_auth?
end
# all completed todos [today@00:00, today@now]
def get_done_today(completed_todos, includes = {:include => Todo::DEFAULT_INCLUDES})
start_of_this_day = Time.zone.now.beginning_of_day
completed_todos.completed_after(start_of_this_day).all(includes)
end
# all completed todos [begin_of_week, start_of_today]
def get_done_this_week(completed_todos, includes = {:include => Todo::DEFAULT_INCLUDES})
start_of_this_week = Time.zone.now.beginning_of_week
start_of_this_day = Time.zone.now.beginning_of_day
completed_todos.completed_after(start_of_this_week).completed_before(start_of_this_day).all(includes)
completed_todos.completed_before(start_of_this_day).completed_after(start_of_this_week).all(includes)
end
# all completed todos [begin_of_month, begin_of_week]
def get_done_this_month(completed_todos, includes = {:include => Todo::DEFAULT_INCLUDES})
start_of_this_month = Time.zone.now.beginning_of_month
start_of_this_week = Time.zone.now.beginning_of_week
completed_todos.completed_after(start_of_this_month).completed_before(start_of_this_week).all(includes)
completed_todos.completed_before(start_of_this_week).completed_after(start_of_this_month).all(includes)
end
private
def parse_date_per_user_prefs( s )

View file

@ -5,12 +5,12 @@ class StatsController < ApplicationController
append_before_filter :init, :exclude => []
def index
@page_title = 'TRACKS::Statistics'
@page_title = t('stats.index_title')
@tags_count = get_total_number_of_tags_of_user
@unique_tags_count = get_unique_tags_of_user.size
@hidden_contexts = @contexts.hidden
@first_action = @actions.find(:first, :order => "created_at ASC")
@hidden_contexts = current_user.contexts.hidden
@first_action = current_user.todos.find(:first, :order => "created_at ASC")
get_stats_actions
get_stats_contexts
@ -21,187 +21,82 @@ class StatsController < ApplicationController
end
def actions_done_last12months_data
@actions = @user.todos
# get actions created and completed in the past 12+3 months. +3 for running
# average
@actions_done_last12months = @actions.find(:all, {
:select => "completed_at",
:conditions => ["completed_at > ? AND completed_at IS NOT NULL", @cut_off_year_plus3]
})
@actions_created_last12months = @actions.find(:all, {
:select => "created_at",
:conditions => ["created_at > ?", @cut_off_year_plus3]
})
@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"})
# convert to hash to be able to fill in non-existing days in
# @actions_done_last12months and count the total actions done in the past
# 12 months to be able to calculate percentage
# use 0 to initialise action count to zero
@actions_done_last12months_hash = Hash.new(0)
@actions_done_last12months.each do |r|
months = (@today.year - r.completed_at.year)*12 + (@today.month - r.completed_at.month)
@actions_done_last12months_hash[months] += 1
end
# convert to hash to be able to fill in non-existing days in
# @actions_created_last12months and count the total actions done in the
# past 12 months to be able to calculate percentage
# use 0 to initialise action count to zero
@actions_created_last12months_hash = Hash.new(0)
@actions_created_last12months.each do |r|
months = (@today.year - r.created_at.year)*12 + (@today.month - r.created_at.month)
@actions_created_last12months_hash[months] += 1
end
@sum_actions_done_last12months=0
@sum_actions_created_last12months=0
# convert to hash to be able to fill in non-existing months
@actions_done_last12months_hash = convert_to_hash(@actions_done_last12months, :completed_at)
@actions_created_last12months_hash = convert_to_hash(@actions_created_last12months, :created_at)
# find max for graph in both hashes
@max=0
@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]
@max = @actions_done_last12months_hash[i] if @actions_done_last12months_hash[i] > @max
end
0.upto 13 do |i|
@sum_actions_created_last12months += @actions_created_last12months_hash[i]
@max = @actions_created_last12months_hash[i] if @actions_created_last12months_hash[i] > @max
@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 because you do not have full data for
# it
@actions_done_avg_last12months_hash = Hash.new("null")
1.upto(12) { |i|
@actions_done_avg_last12months_hash[i] = (@actions_done_last12months_hash[i] +
@actions_done_last12months_hash[i+1] +
@actions_done_last12months_hash[i+2])/3.0
}
# 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 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_created_avg_last12months_hash = Hash.new("null")
1.upto(12) { |i|
@actions_created_avg_last12months_hash[i] = (@actions_created_last12months_hash[i] +
@actions_created_last12months_hash[i+1] +
@actions_created_last12months_hash[i+2])/3.0
}
# interpolate avg for this month. Assume 31 days in this month
days_passed_this_month = Time.new.day/1.0
@interpolated_actions_created_this_month = (
@actions_created_last12months_hash[0]/days_passed_this_month*31.0+
@actions_created_last12months_hash[1]+
@actions_created_last12months_hash[2]) / 3.0
@interpolated_actions_done_this_month = (
@actions_done_last12months_hash[0]/days_passed_this_month*31.0 +
@actions_done_last12months_hash[1]+
@actions_done_last12months_hash[2]) / 3.0
# 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)
render :layout => false
end
def actions_done_last_years
@page_title = t('stats.index_title')
@chart_width = 900
@chart_height = 400
end
def actions_done_lastyears_data
@actions = @user.todos
@actions = current_user.todos
# get actions created and completed in the past 12+3 months. +3 for running
# average
@actions_done_last_months = @actions.find(:all, {
:select => "completed_at",
:conditions => ["completed_at IS NOT NULL"]
})
@actions_created_last_months = @actions.find(:all, {
:select => "created_at",
})
@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" })
@month_count = 0
@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
# convert to hash to be able to fill in non-existing days in
# @actions_done_last12months and count the total actions done in the past
# 12 months to be able to calculate percentage
# use 0 to initialise action count to zero
@actions_done_last_months_hash = Hash.new(0)
@actions_done_last_months.each do |r|
months = (@today.year - r.completed_at.year)*12 + (@today.month - r.completed_at.month)
@month_count = months if months > @month_count
@actions_done_last_months_hash[months] += 1
end
# convert to hash to be able to fill in non-existing days in
# @actions_created_last12months and count the total actions done in the
# past 12 months to be able to calculate percentage
# use 0 to initialise action count to zero
@actions_created_last_months_hash = Hash.new(0)
@actions_created_last_months.each do |r|
months = (@today.year - r.created_at.year)*12 + (@today.month - r.created_at.month)
@month_count = months if months > @month_count
@actions_created_last_months_hash[months] += 1
end
@sum_actions_done_last_months=0
@sum_actions_created_last_months=0
# convert to hash to be able to fill in non-existing months
@actions_done_last_months_hash = convert_to_hash(@actions_done_last_months, :completed_at)
@actions_created_last_months_hash = convert_to_hash(@actions_created_last_months, :created_at)
# find max for graph in both hashes
@max=0
@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]
@max = @actions_done_last_months_hash[i] if @actions_done_last_months_hash[i] > @max
end
0.upto @month_count do |i|
@sum_actions_created_last_months += @actions_created_last_months_hash[i]
@max = @actions_created_last_months_hash[i] if @actions_created_last_months_hash[i] > @max
@max = [@actions_done_last_months_hash[i], @actions_created_last_months_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 because you do not have full data for
# it
@actions_done_avg_last_months_hash = Hash.new("null")
1.upto(@month_count) { |i|
@actions_done_avg_last_months_hash[i] = (@actions_done_last_months_hash[i] +
@actions_done_last_months_hash[i+1] +
@actions_done_last_months_hash[i+2])/3.0
}
# 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
# correct last two months
@actions_done_avg_last_months_hash[@month_count] = @actions_done_avg_last_months_hash[@month_count] * 3
@actions_done_avg_last_months_hash[@month_count-1] = @actions_done_avg_last_months_hash[@month_count-1] * 3 / 2 if @month_count > 1
correct_last_two_months(@actions_done_avg_last_months_hash, @month_count)
correct_last_two_months(@actions_created_avg_last_months_hash, @month_count)
# 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_created_avg_last_months_hash = Hash.new("null")
1.upto(@month_count) { |i|
@actions_created_avg_last_months_hash[i] = (@actions_created_last_months_hash[i] +
@actions_created_last_months_hash[i+1] +
@actions_created_last_months_hash[i+2])/3.0
}
# correct last two months
@actions_created_avg_last_months_hash[@month_count] = @actions_created_avg_last_months_hash[@month_count] * 3
@actions_created_avg_last_months_hash[@month_count-1] = @actions_created_avg_last_months_hash[@month_count-1] * 3 / 2 if @month_count > 1
# interpolate avg for this month. Assume 31 days in this month
days_passed_this_month = Time.new.day/1.0
@interpolated_actions_created_this_month = (
@actions_created_last_months_hash[0]/days_passed_this_month*31.0+
@actions_created_last_months_hash[1]+
@actions_created_last_months_hash[2]) / 3.0
@interpolated_actions_done_this_month = (
@actions_done_last_months_hash[0]/days_passed_this_month*31.0 +
@actions_done_last_months_hash[1]+
@actions_done_last_months_hash[2]) / 3.0
# 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)
render :layout => false
end
@ -209,51 +104,22 @@ class StatsController < ApplicationController
def actions_done_last30days_data
# get actions created and completed in the past 30 days.
@actions_done_last30days = @actions.find(:all, {
:select => "completed_at",
:conditions => ["completed_at > ? AND completed_at IS NOT NULL", @cut_off_month]
})
@actions_created_last30days = @actions.find(:all, {
:select => "created_at",
:conditions => ["created_at > ?", @cut_off_month]
})
@actions_done_last30days = current_user.todos.completed_after(@cut_off_month).find(:all, { :select => "completed_at" })
@actions_created_last30days = current_user.todos.created_after(@cut_off_month).find(:all, { :select => "created_at" })
# convert to hash to be able to fill in non-existing days in
# @actions_done_last30days and count the total actions done in the past 30
# days to be able to calculate percentage
@sum_actions_done_last30days=0
# use 0 to initialise action count to zero
@actions_done_last30days_hash = Hash.new(0)
@actions_done_last30days.each do |r|
# only use date part of completed_at
action_date = Time.utc(r.completed_at.year, r.completed_at.month, r.completed_at.day, 0,0)
days = ((@today - action_date) / @seconds_per_day).to_i
@actions_done_last30days_hash[days] += 1
@sum_actions_done_last30days+=1
end
# convert to hash to be able to fill in non-existing days in
# @actions_done_last30days and count the total actions done in the past 30
# days to be able to calculate percentage
@sum_actions_created_last30days=0
# use 0 to initialise action count to zero
@actions_created_last30days_hash = Hash.new(0)
@actions_created_last30days.each do |r|
# only use date part of created_at
action_date = Time.utc(r.created_at.year, r.created_at.month, r.created_at.day, 0,0)
days = ((@today - action_date) / @seconds_per_day).to_i
@actions_created_last30days_hash[days] += 1
@sum_actions_created_last30days += 1
end
@actions_done_last30days_hash = convert_to_hash(@actions_done_last30days, :completed_at, :difference_in_days)
@actions_created_last30days_hash = convert_to_hash(@actions_created_last30days, :created_at, :difference_in_days)
# find max for graph in both hashes
@max=0
0.upto(30) { |i| @max = @actions_done_last30days_hash[i] if @actions_done_last30days_hash[i] > @max }
0.upto(30) { |i| @max = @actions_created_last30days_hash[i] if @actions_created_last30days_hash[i] > @max }
@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
render :layout => false
end
@ -680,9 +546,10 @@ class StatsController < ApplicationController
end
def init
@actions = @user.todos
@projects = @user.projects
@contexts = @user.contexts
@me = self # for meta programming
@actions = current_user.todos
@projects = current_user.projects
@contexts = current_user.contexts
# default chart dimensions
@chart_width=460
@ -691,25 +558,16 @@ class StatsController < ApplicationController
@pie_height=325
# get the current date wih time set to 0:0
now = Time.new
@today = Time.utc(now.year, now.month, now.day, 0,0)
@today = Time.zone.now.beginning_of_day
# define the number of seconds in a day
@seconds_per_day = 60*60*24
# define cut_off date and discard the time for a month, 3 months and a year
cut_off_time = 13.months.ago()
@cut_off_year = Time.utc(cut_off_time.year, cut_off_time.month, cut_off_time.day,0,0)
cut_off_time = 16.months.ago()
@cut_off_year_plus3 = Time.utc(cut_off_time.year, cut_off_time.month, cut_off_time.day,0,0)
cut_off_time = 31.days.ago
@cut_off_month = Time.utc(cut_off_time.year, cut_off_time.month, cut_off_time.day,0,0)
cut_off_time = 91.days.ago
@cut_off_3months = Time.utc(cut_off_time.year, cut_off_time.month, cut_off_time.day,0,0)
@cut_off_year = 13.months.ago.beginning_of_day
@cut_off_year_plus3 = 16.months.ago.beginning_of_day
@cut_off_month = 1.month.ago.beginning_of_day
@cut_off_3months = 3.months.ago.beginning_of_day
end
def get_stats_actions
@ -917,4 +775,34 @@ class StatsController < ApplicationController
return selected_todo_ids, count
end
def convert_to_hash(records, date_method, difference_method=:difference_in_months)
# use 0 to initialise action count to zero
hash = Hash.new(0)
records.each { |t| hash[self.send(difference_method, @today, t.send(date_method))] += 1 }
return hash
end
# assumes date1 > date2
def difference_in_months(date1, date2)
return (date1.year - date2.year)*12 + (date1.month - date2.month)
end
# assumes date1 > date2
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
end
def interpolate_avg(hash, percent)
return (hash[0]*percent + hash[1] + hash[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
end

View file

@ -41,8 +41,10 @@ class Todo < ActiveRecord::Base
named_scope :with_tag, lambda { |tag_id| {:joins => :taggings, :conditions => ["taggings.tag_id = ? ", tag_id] } }
named_scope :with_tags, lambda { |tag_ids| {:conditions => ["EXISTS(SELECT * from taggings t WHERE t.tag_id IN (?) AND t.taggable_id=todos.id AND t.taggable_type='Todo')", tag_ids] } }
named_scope :of_user, lambda { |user_id| {:conditions => ["todos.user_id = ? ", user_id] } }
named_scope :completed_after, lambda { |date| {:conditions => ["todos.completed_at > ? ", date] } }
named_scope :completed_before, lambda { |date| {:conditions => ["todos.completed_at < ? ", date] } }
named_scope :completed_after, lambda { |date| {:conditions => ["todos.completed_at > ?", date] } }
named_scope :completed_before, lambda { |date| {:conditions => ["todos.completed_at < ?", date] } }
named_scope :created_after, lambda { |date| {:conditions => ["todos.created_at > ?", date] } }
named_scope :created_before, lambda { |date| {:conditions => ["todos.created_at < ?", date] } }
STARRED_TAG_NAME = "starred"
DEFAULT_INCLUDES = [ :project, :context, :tags, :taggings, :pending_successors, :uncompleted_predecessors, :recurring_todo ]

View file

@ -233,6 +233,7 @@ en:
one: 1 error prohibited this %{model} from being saved
other: "%{count} errors prohibited this %{model} from being saved"
stats:
index_title: TRACKS::Statistics
tag_cloud_title: Tag cloud for all actions
tag_cloud_description: This tag cloud includes tags of all actions (completed, not completed, visible and/or hidden)
tag_cloud_90days_title: Tag cloud actions in past 90 days

View file

@ -13,11 +13,6 @@ class StatsControllerTest < ActionController::TestCase
@response = ActionController::TestResponse.new
end
# Replace this with your real tests.
def test_truth
assert true
end
def test_get_index_when_not_logged_in
get :index
assert_redirected_to :controller => 'login', :action => 'login'

57
test/unit/todo_test2.rb Normal file
View file

@ -0,0 +1,57 @@
require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
require 'date'
class TodoTest < ActiveSupport::TestCase
fixtures :todos, :recurring_todos, :users, :contexts, :preferences, :tags, :taggings, :projects
def setup
@not_completed1 = Todo.find(1).reload
@not_completed2 = Todo.find(2).reload
@completed = Todo.find(8).reload
end
# test named_scopes
def test_find_completed
# Given 2 completed todos, one completed now and one completed 2 months ago
@not_completed1.toggle_completion!
@completed.completed_at = 2.months.ago
@completed.save!
completed_old = @completed
completed_now = @not_completed1
# When I use the finders
recent_completed_todos = Todo.completed_after(1.month.ago).find(:all)
older_completed_todos = Todo.completed_before(1.month.ago).find(:all)
# Then completed1 should be before and completed2 should be after a month ago
assert older_completed_todos.include?(completed_old)
assert recent_completed_todos.include?(completed_now)
# And completed1 should not be after and completed2 should not be before a month ago
assert !older_completed_todos.include?(completed_now)
assert !recent_completed_todos.include?(completed_old)
end
def test_find_created
# Given 2 created todos, one created now and one created 2 months ago
user = @completed.user
todo_old = user.todos.create!({:description => "created long long ago", :context => @completed.context})
todo_old.created_at = 2.months.ago
todo_old.save!
todo_now = user.todos.create!({:description => "just created", :context => @completed.context})
# When I use the finders
recent_created_todos = Todo.created_after(1.month.ago).find(:all)
older_created_todos = Todo.created_before(1.month.ago).find(:all)
# Then todo1 should be before and todo2 should be after a month ago
assert older_created_todos.include?(todo_old)
assert recent_created_todos.include?(todo_now)
# And todo1 should not be after and todo2 should not be before a month ago
assert !older_created_todos.include?(todo_now)
assert !recent_created_todos.include?(todo_old)
end
end