2007-09-13 03:21:37 +00:00
|
|
|
class StatsController < ApplicationController
|
2007-10-05 19:20:18 +00:00
|
|
|
|
2011-06-12 05:38:45 +02:00
|
|
|
helper :todos, :projects, :recurring_todos
|
2008-02-29 19:54:32 +00:00
|
|
|
|
2007-10-05 19:20:18 +00:00
|
|
|
append_before_filter :init, :exclude => []
|
2011-11-19 02:41:06 +01:00
|
|
|
|
2007-09-13 03:21:37 +00:00
|
|
|
def index
|
2011-11-19 02:41:06 +01:00
|
|
|
@page_title = t('stats.index_title')
|
2009-01-08 10:18:03 +01:00
|
|
|
|
|
|
|
@tags_count = get_total_number_of_tags_of_user
|
2009-01-08 10:39:14 +01:00
|
|
|
@unique_tags_count = get_unique_tags_of_user.size
|
2011-11-19 02:41:06 +01:00
|
|
|
@hidden_contexts = current_user.contexts.hidden
|
|
|
|
@first_action = current_user.todos.find(:first, :order => "created_at ASC")
|
|
|
|
|
2007-09-13 03:21:37 +00:00
|
|
|
get_stats_actions
|
|
|
|
get_stats_contexts
|
|
|
|
get_stats_projects
|
2011-11-19 02:41:06 +01:00
|
|
|
get_stats_tags
|
|
|
|
|
2007-09-13 03:21:37 +00:00
|
|
|
render :layout => 'standard'
|
2011-11-19 02:41:06 +01:00
|
|
|
end
|
2012-01-20 23:22:21 +01:00
|
|
|
|
2007-09-13 03:21:37 +00:00
|
|
|
def actions_done_last12months_data
|
2007-10-12 19:44:07 +00:00
|
|
|
# get actions created and completed in the past 12+3 months. +3 for running
|
2008-02-29 19:54:32 +00:00
|
|
|
# average
|
2011-11-28 23:38:57 +01:00
|
|
|
@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 array and fill in non-existing months
|
2012-01-20 23:22:21 +01:00
|
|
|
@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_last12monthsPlus3_array = convert_to_months_from_today_array(@actions_done_last12monthsPlus3, 13, :completed_at)
|
|
|
|
@actions_created_last12monthsPlus3_array = convert_to_months_from_today_array(@actions_created_last12monthsPlus3, 15, :created_at)
|
2011-11-28 23:38:57 +01:00
|
|
|
|
|
|
|
# find max for graph in both arrays
|
|
|
|
@max = [@actions_done_last12months_array.max, @actions_created_last12months_array.max].max
|
|
|
|
|
|
|
|
# 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)
|
2011-11-19 02:41:06 +01:00
|
|
|
|
|
|
|
# interpolate avg for current month.
|
|
|
|
percent_of_month = Time.zone.now.day.to_f / Time.zone.now.end_of_month.day.to_f
|
2011-11-28 23:38:57 +01:00
|
|
|
@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)
|
2011-11-19 02:41:06 +01:00
|
|
|
|
2007-09-13 03:21:37 +00:00
|
|
|
render :layout => false
|
|
|
|
end
|
2011-11-19 02:41:06 +01:00
|
|
|
|
2008-07-04 17:56:59 +02:00
|
|
|
def actions_done_last_years
|
2011-11-19 02:41:06 +01:00
|
|
|
@page_title = t('stats.index_title')
|
2008-07-04 17:56:59 +02:00
|
|
|
@chart_width = 900
|
|
|
|
@chart_height = 400
|
|
|
|
end
|
2011-11-19 02:41:06 +01:00
|
|
|
|
2008-07-04 17:56:59 +02:00
|
|
|
def actions_done_lastyears_data
|
2011-11-19 02:41:06 +01:00
|
|
|
@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" })
|
2008-07-04 17:56:59 +02:00
|
|
|
|
2011-11-28 23:38:57 +01:00
|
|
|
# query is sorted, so use last todo to calculate number of months
|
2011-11-19 02:41:06 +01:00
|
|
|
@month_count = [difference_in_months(@today, @actions_created_last_months.last.created_at),
|
2011-12-09 17:17:42 +01:00
|
|
|
difference_in_months(@today, @actions_done_last_months.last.completed_at)].max
|
2011-11-19 02:41:06 +01:00
|
|
|
|
2011-11-28 23:38:57 +01:00
|
|
|
# convert to array and fill in non-existing months
|
2012-01-20 23:22:21 +01:00
|
|
|
@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)
|
2008-07-04 17:56:59 +02:00
|
|
|
|
|
|
|
# find max for graph in both hashes
|
2011-11-28 23:38:57 +01:00
|
|
|
@max = [@actions_done_last_months_array.max, @actions_created_last_months_array.max].max
|
2008-07-04 17:56:59 +02:00
|
|
|
|
2011-11-28 23:38:57 +01:00
|
|
|
# find running avg
|
|
|
|
@actions_done_avg_last_months_array, @actions_created_avg_last_months_array =
|
2011-12-09 17:17:42 +01:00
|
|
|
find_running_avg_array(@actions_done_last_months_array, @actions_created_last_months_array, @month_count+1)
|
2011-11-19 02:41:06 +01:00
|
|
|
|
2011-11-28 23:38:57 +01:00
|
|
|
# correct last two months since the data of last+1 and last+2 are not available for avg
|
2011-12-09 17:17:42 +01:00
|
|
|
correct_last_two_months(@actions_done_avg_last_months_array, @month_count)
|
|
|
|
correct_last_two_months(@actions_created_avg_last_months_array, @month_count)
|
2011-11-19 02:41:06 +01:00
|
|
|
|
|
|
|
# interpolate avg for this month.
|
|
|
|
percent_of_month = Time.zone.now.day.to_f / Time.zone.now.end_of_month.day.to_f
|
2011-11-28 23:38:57 +01:00
|
|
|
@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)
|
2011-11-19 02:41:06 +01:00
|
|
|
|
2008-07-04 17:56:59 +02:00
|
|
|
render :layout => false
|
|
|
|
end
|
|
|
|
|
2007-09-13 03:21:37 +00:00
|
|
|
def actions_done_last30days_data
|
2007-10-05 19:20:18 +00:00
|
|
|
# get actions created and completed in the past 30 days.
|
2011-11-19 02:41:06 +01:00
|
|
|
@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" })
|
|
|
|
|
2011-12-09 17:17:42 +01:00
|
|
|
# convert to array. 30+1 to have 30 complete days and one current day [0]
|
2012-01-20 23:22:21 +01:00
|
|
|
@actions_done_last30days_array = convert_to_days_from_today_array(@actions_done_last30days, 31, :completed_at)
|
|
|
|
@actions_created_last30days_array = convert_to_days_from_today_array(@actions_created_last30days, 31, :created_at)
|
2007-09-18 05:11:58 +00:00
|
|
|
|
2011-11-19 02:41:06 +01:00
|
|
|
# find max for graph in both hashes
|
2011-12-09 17:17:42 +01:00
|
|
|
@max = [@actions_done_last30days_array.max, @actions_created_last30days_array.max].max
|
2007-10-05 19:20:18 +00:00
|
|
|
|
2007-09-13 03:21:37 +00:00
|
|
|
render :layout => false
|
|
|
|
end
|
|
|
|
|
|
|
|
def actions_completion_time_data
|
2011-11-28 23:38:57 +01:00
|
|
|
@actions_completion_time = current_user.todos.completed.find(:all, { :select => "completed_at, created_at", :order => "completed_at DESC" })
|
2011-11-19 02:41:06 +01:00
|
|
|
|
2011-12-09 17:17:42 +01:00
|
|
|
# convert to array and fill in non-existing weeks with 0
|
2011-11-28 23:38:57 +01:00
|
|
|
@max_weeks = difference_in_weeks(@today, @actions_completion_time.last.completed_at)
|
2012-01-20 23:22:21 +01:00
|
|
|
@actions_completed_per_week_array = convert_to_weeks_running_array(@actions_completion_time, @max_weeks+1)
|
2011-11-28 23:38:57 +01:00
|
|
|
|
2007-09-18 05:11:58 +00:00
|
|
|
# stop the chart after 10 weeks
|
2011-12-09 17:17:42 +01:00
|
|
|
@count = [10, @max_weeks].min
|
|
|
|
|
|
|
|
# convert to new array to hold max @cut_off elems + 1 for sum of actions after @cut_off
|
|
|
|
@actions_completion_time_array = cut_off_array(@actions_completed_per_week_array, @count)
|
|
|
|
@max_actions = @actions_completion_time_array.max
|
|
|
|
|
|
|
|
# get percentage done cummulative
|
|
|
|
@cumm_percent_done = convert_to_cummulative_array(@actions_completion_time_array, @actions_completion_time.count)
|
2011-11-19 02:41:06 +01:00
|
|
|
|
2007-09-13 03:21:37 +00:00
|
|
|
render :layout => false
|
|
|
|
end
|
|
|
|
|
|
|
|
def actions_running_time_data
|
2011-11-28 23:38:57 +01:00
|
|
|
@actions_running_time = current_user.todos.not_completed.find(:all, { :select => "created_at", :order => "created_at DESC" })
|
2007-09-13 03:21:37 +00:00
|
|
|
|
2011-12-09 17:17:42 +01:00
|
|
|
# convert to array and fill in non-existing weeks with 0
|
2011-11-28 23:38:57 +01:00
|
|
|
@max_weeks = difference_in_weeks(@today, @actions_running_time.last.created_at)
|
2012-01-20 23:22:21 +01:00
|
|
|
@actions_running_per_week_array = convert_to_weeks_from_today_array(@actions_running_time, @max_weeks+1, :created_at)
|
2011-11-19 02:41:06 +01:00
|
|
|
|
2007-09-13 03:21:37 +00:00
|
|
|
# cut off chart at 52 weeks = one year
|
2011-12-09 17:17:42 +01:00
|
|
|
@count = [52, @max_weeks].min
|
|
|
|
|
|
|
|
# convert to new array to hold max @cut_off elems + 1 for sum of actions after @cut_off
|
|
|
|
@actions_running_time_array = cut_off_array(@actions_running_per_week_array, @count)
|
|
|
|
@max_actions = @actions_running_time_array.max
|
2011-11-19 02:41:06 +01:00
|
|
|
|
2011-12-09 17:17:42 +01:00
|
|
|
# get percentage done cummulative
|
|
|
|
@cumm_percent_done = convert_to_cummulative_array(@actions_running_time_array, @actions_running_time.count )
|
|
|
|
|
2007-09-13 03:21:37 +00:00
|
|
|
render :layout => false
|
|
|
|
end
|
|
|
|
|
|
|
|
def actions_visible_running_time_data
|
2008-02-29 19:54:32 +00:00
|
|
|
# running means
|
2011-11-20 20:23:28 +01:00
|
|
|
# - not completed (completed_at must be null)
|
|
|
|
# visible means
|
2008-02-29 19:54:32 +00:00
|
|
|
# - actions not part of a hidden project
|
|
|
|
# - actions not part of a hidden context
|
|
|
|
# - actions not deferred (show_from must be null)
|
2010-05-01 17:19:28 +02:00
|
|
|
# - actions not pending/blocked
|
2011-11-19 02:41:06 +01:00
|
|
|
|
2011-11-28 23:38:57 +01:00
|
|
|
@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")
|
2011-11-19 02:41:06 +01:00
|
|
|
|
2011-11-28 23:38:57 +01:00
|
|
|
@max_weeks = difference_in_weeks(@today, @actions_running_time.last.created_at)
|
2012-01-20 23:22:21 +01:00
|
|
|
@actions_running_per_week_array = convert_to_weeks_from_today_array(@actions_running_time, @max_weeks+1, :created_at)
|
2011-11-19 02:41:06 +01:00
|
|
|
|
2007-09-13 03:21:37 +00:00
|
|
|
# cut off chart at 52 weeks = one year
|
2011-12-09 17:17:42 +01:00
|
|
|
@count = [52, @max_weeks].min
|
|
|
|
|
|
|
|
# convert to new array to hold max @cut_off elems + 1 for sum of actions after @cut_off
|
|
|
|
@actions_running_time_array = cut_off_array(@actions_running_per_week_array, @count)
|
|
|
|
@max_actions = @actions_running_time_array.max
|
|
|
|
|
|
|
|
# get percentage done cummulative
|
|
|
|
@cumm_percent_done = convert_to_cummulative_array(@actions_running_time_array, @actions_running_time.count )
|
2011-11-19 02:41:06 +01:00
|
|
|
|
2007-09-13 03:21:37 +00:00
|
|
|
render :layout => false
|
|
|
|
end
|
|
|
|
|
|
|
|
def context_total_actions_data
|
2008-02-29 19:54:32 +00:00
|
|
|
# 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
|
|
|
|
# should work.
|
2011-11-20 20:23:28 +01:00
|
|
|
@all_actions_per_context = current_user.contexts.find_by_sql(
|
2007-12-16 21:54:37 +00:00
|
|
|
"SELECT c.name AS name, c.id as id, count(*) AS total "+
|
2007-09-18 05:11:58 +00:00
|
|
|
"FROM contexts c, todos t "+
|
|
|
|
"WHERE t.context_id=c.id "+
|
2011-12-09 17:17:42 +01:00
|
|
|
"AND c.user_id = #{current_user.id} " +
|
2007-12-16 21:54:37 +00:00
|
|
|
"GROUP BY c.name, c.id "+
|
2007-09-18 05:11:58 +00:00
|
|
|
"ORDER BY total DESC"
|
2007-09-13 03:21:37 +00:00
|
|
|
)
|
2011-12-09 17:17:42 +01:00
|
|
|
@sum = @all_actions_per_context.inject(0){|sum, apc| sum += apc['total'].to_i }
|
|
|
|
|
2007-12-02 13:04:33 +00:00
|
|
|
pie_cutoff=10
|
2011-12-09 17:17:42 +01:00
|
|
|
size = [@all_actions_per_context.size, pie_cutoff].min
|
|
|
|
|
|
|
|
# explicitely 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].to_i,
|
|
|
|
'id' => @all_actions_per_context[i][:id]
|
|
|
|
} }
|
2007-12-02 13:04:33 +00:00
|
|
|
|
|
|
|
if size==pie_cutoff
|
2010-10-31 21:27:13 +08:00
|
|
|
@actions_per_context[size-1]['name']=t('stats.other_actions_label')
|
2011-12-09 17:17:42 +01:00
|
|
|
@actions_per_context[size-1]['total']=@actions_per_context[size-1]['total']
|
2007-12-16 21:54:37 +00:00
|
|
|
@actions_per_context[size-1]['id']=-1
|
2011-12-09 17:17:42 +01:00
|
|
|
size.upto(@all_actions_per_context.size-1){ |i| @actions_per_context[size-1]['total']+=(@all_actions_per_context[i]['total'].to_i) }
|
2007-12-02 13:04:33 +00:00
|
|
|
end
|
2011-12-09 17:17:42 +01:00
|
|
|
|
2011-11-19 02:41:06 +01:00
|
|
|
@truncate_chars = 15
|
|
|
|
|
2007-09-13 03:21:37 +00:00
|
|
|
render :layout => false
|
|
|
|
end
|
|
|
|
|
|
|
|
def context_running_actions_data
|
2008-03-12 22:34:48 +00:00
|
|
|
# get incomplete action count per visible context
|
2009-01-08 10:18:03 +01:00
|
|
|
#
|
2007-12-08 21:05:24 +00:00
|
|
|
# Went from GROUP BY c.id to c.name for compatibility with postgresql. Since
|
|
|
|
# the name is forced to be unique, this should work.
|
2011-11-20 20:23:28 +01:00
|
|
|
@all_actions_per_context = current_user.contexts.find_by_sql(
|
2007-12-16 21:54:37 +00:00
|
|
|
"SELECT c.name AS name, c.id as id, count(*) AS total "+
|
2007-09-18 05:11:58 +00:00
|
|
|
"FROM contexts c, todos t "+
|
|
|
|
"WHERE t.context_id=c.id AND t.completed_at IS NULL AND NOT c.hide "+
|
2012-01-03 22:08:50 +01:00
|
|
|
"AND c.user_id = #{current_user.id} " +
|
2007-12-16 21:54:37 +00:00
|
|
|
"GROUP BY c.name, c.id "+
|
2007-09-18 05:11:58 +00:00
|
|
|
"ORDER BY total DESC"
|
2007-09-13 03:21:37 +00:00
|
|
|
)
|
2012-01-03 22:08:50 +01:00
|
|
|
@sum = @all_actions_per_context.inject(0){|sum, apc| sum += apc['total'].to_i }
|
2011-11-19 02:41:06 +01:00
|
|
|
|
2007-12-02 13:04:33 +00:00
|
|
|
pie_cutoff=10
|
2012-01-03 22:08:50 +01:00
|
|
|
size = [@all_actions_per_context.size, pie_cutoff].min
|
|
|
|
|
|
|
|
# explicitely 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].to_i,
|
|
|
|
'id' => @all_actions_per_context[i][:id]
|
|
|
|
} }
|
2007-12-02 13:04:33 +00:00
|
|
|
|
|
|
|
if size==pie_cutoff
|
2010-10-31 21:27:13 +08:00
|
|
|
@actions_per_context[size-1]['name']=t('stats.other_actions_label')
|
2012-01-03 22:08:50 +01:00
|
|
|
@actions_per_context[size-1]['total']=@actions_per_context[size-1]['total']
|
2007-12-16 21:54:37 +00:00
|
|
|
@actions_per_context[size-1]['id']=-1
|
2012-01-03 22:08:50 +01:00
|
|
|
(size).upto(@all_actions_per_context.size()-1){|i| @actions_per_context[size-1]['total']+=@all_actions_per_context[i]['total'].to_i }
|
2007-12-02 13:04:33 +00:00
|
|
|
end
|
2011-11-19 02:41:06 +01:00
|
|
|
|
2007-12-02 13:04:33 +00:00
|
|
|
@truncate_chars = 15
|
2011-11-19 02:41:06 +01:00
|
|
|
|
2007-09-13 03:21:37 +00:00
|
|
|
render :layout => false
|
|
|
|
end
|
|
|
|
|
|
|
|
def actions_day_of_week_all_data
|
2011-11-20 20:23:28 +01:00
|
|
|
@actions_creation_day = current_user.todos.find(:all, { :select => "created_at" })
|
|
|
|
@actions_completion_day = current_user.todos.completed.find(:all, { :select => "completed_at" })
|
2007-09-13 03:21:37 +00:00
|
|
|
|
2011-12-09 17:17:42 +01:00
|
|
|
# convert to array and fill in non-existing days
|
2007-09-13 03:21:37 +00:00
|
|
|
@actions_creation_day_array = Array.new(7) { |i| 0}
|
2011-11-20 20:23:28 +01:00
|
|
|
@actions_creation_day.each { |t| @actions_creation_day_array[ t.created_at.wday ] += 1 }
|
|
|
|
@max = @actions_creation_day_array.max
|
2007-09-13 03:21:37 +00:00
|
|
|
|
2011-12-09 17:17:42 +01:00
|
|
|
# convert to array and fill in non-existing days
|
2007-09-13 03:21:37 +00:00
|
|
|
@actions_completion_day_array = Array.new(7) { |i| 0}
|
2011-11-20 20:23:28 +01:00
|
|
|
@actions_completion_day.each { |t| @actions_completion_day_array[ t.completed_at.wday ] += 1 }
|
|
|
|
@max = @actions_completion_day_array.max
|
2011-11-19 02:41:06 +01:00
|
|
|
|
2007-09-13 03:21:37 +00:00
|
|
|
render :layout => false
|
|
|
|
end
|
|
|
|
|
|
|
|
def actions_day_of_week_30days_data
|
2011-11-20 20:23:28 +01:00
|
|
|
@actions_creation_day = current_user.todos.created_after(@cut_off_month).find(:all, { :select => "created_at" })
|
|
|
|
@actions_completion_day = current_user.todos.completed_after(@cut_off_month).find(:all, { :select => "completed_at" })
|
2007-09-13 03:21:37 +00:00
|
|
|
|
|
|
|
# convert to hash to be able to fill in non-existing days
|
|
|
|
@max=0
|
|
|
|
@actions_creation_day_array = Array.new(7) { |i| 0}
|
2011-11-20 20:23:28 +01:00
|
|
|
@actions_creation_day.each { |r| @actions_creation_day_array[ r.created_at.wday ] += 1 }
|
2007-09-13 03:21:37 +00:00
|
|
|
|
|
|
|
# convert to hash to be able to fill in non-existing days
|
|
|
|
@actions_completion_day_array = Array.new(7) { |i| 0}
|
2011-11-20 20:23:28 +01:00
|
|
|
@actions_completion_day.each { |r| @actions_completion_day_array[r.completed_at.wday] += 1 }
|
|
|
|
|
|
|
|
@max = [@actions_creation_day_array.max, @actions_completion_day_array.max].max
|
2011-11-19 02:41:06 +01:00
|
|
|
|
2007-09-13 03:21:37 +00:00
|
|
|
render :layout => false
|
|
|
|
end
|
|
|
|
|
|
|
|
def actions_time_of_day_all_data
|
2011-11-20 20:23:28 +01:00
|
|
|
@actions_creation_hour = current_user.todos.find(:all, { :select => "created_at" })
|
|
|
|
@actions_completion_hour = current_user.todos.completed.find(:all, { :select => "completed_at" })
|
2007-09-13 03:21:37 +00:00
|
|
|
|
|
|
|
# convert to hash to be able to fill in non-existing days
|
|
|
|
@actions_creation_hour_array = Array.new(24) { |i| 0}
|
2012-01-03 22:08:50 +01:00
|
|
|
@actions_creation_hour.each{|r| @actions_creation_hour_array[r.created_at.hour] += 1 }
|
2007-09-13 03:21:37 +00:00
|
|
|
|
|
|
|
# convert to hash to be able to fill in non-existing days
|
|
|
|
@actions_completion_hour_array = Array.new(24) { |i| 0}
|
2012-01-03 22:08:50 +01:00
|
|
|
@actions_completion_hour.each{|r| @actions_completion_hour_array[r.completed_at.hour] += 1 }
|
2011-11-20 20:23:28 +01:00
|
|
|
|
|
|
|
@max = [@actions_creation_hour_array.max, @actions_completion_hour_array.max].max
|
2011-11-19 02:41:06 +01:00
|
|
|
|
2007-09-13 03:21:37 +00:00
|
|
|
render :layout => false
|
|
|
|
end
|
|
|
|
|
|
|
|
def actions_time_of_day_30days_data
|
2011-11-20 20:23:28 +01:00
|
|
|
@actions_creation_hour = current_user.todos.created_after(@cut_off_month).find(:all, { :select => "created_at" })
|
|
|
|
@actions_completion_hour = current_user.todos.completed_after(@cut_off_month).find(:all, { :select => "completed_at" })
|
2007-09-13 03:21:37 +00:00
|
|
|
|
|
|
|
# convert to hash to be able to fill in non-existing days
|
|
|
|
@actions_creation_hour_array = Array.new(24) { |i| 0}
|
2012-01-03 22:08:50 +01:00
|
|
|
@actions_creation_hour.each{|r| @actions_creation_hour_array[r.created_at.hour] += 1 }
|
2007-09-13 03:21:37 +00:00
|
|
|
|
|
|
|
# convert to hash to be able to fill in non-existing days
|
|
|
|
@actions_completion_hour_array = Array.new(24) { |i| 0}
|
2012-01-03 22:08:50 +01:00
|
|
|
@actions_completion_hour.each{|r| @actions_completion_hour_array[r.completed_at.hour] += 1 }
|
2011-11-20 20:23:28 +01:00
|
|
|
|
|
|
|
@max = [@actions_creation_hour_array.max, @max = @actions_completion_hour_array.max].max
|
2011-11-19 02:41:06 +01:00
|
|
|
|
2007-09-13 03:21:37 +00:00
|
|
|
render :layout => false
|
|
|
|
end
|
2011-11-19 02:41:06 +01:00
|
|
|
|
2008-02-29 19:54:32 +00:00
|
|
|
def show_selected_actions_from_chart
|
2010-10-31 21:27:13 +08:00
|
|
|
@page_title = t('stats.action_selection_title')
|
2008-02-29 19:54:32 +00:00
|
|
|
@count = 99
|
|
|
|
|
|
|
|
@source_view = 'stats'
|
2011-11-19 02:41:06 +01:00
|
|
|
|
2008-02-29 19:54:32 +00:00
|
|
|
case params['id']
|
|
|
|
when 'avrt', 'avrt_end' # actions_visible_running_time
|
2011-11-19 02:41:06 +01:00
|
|
|
|
2008-02-29 19:54:32 +00:00
|
|
|
# HACK: because open flash chart uses & to denote the end of a parameter,
|
|
|
|
# we cannot use URLs with multiple parameters (that would use &). So we
|
|
|
|
# revert to using two id's for the same selection. avtr_end means that the
|
|
|
|
# last bar of the chart is selected. avtr is used for all other bars
|
2011-11-19 02:41:06 +01:00
|
|
|
|
2008-02-29 19:54:32 +00:00
|
|
|
week_from = params['index'].to_i
|
|
|
|
week_to = week_from+1
|
2011-11-19 02:41:06 +01:00
|
|
|
|
2008-02-29 19:54:32 +00:00
|
|
|
@chart_name = "actions_visible_running_time_data"
|
2010-10-31 21:27:13 +08:00
|
|
|
@page_title = t('stats.actions_selected_from_week')
|
2008-06-06 15:22:44 +00:00
|
|
|
@further = false
|
2008-02-29 19:54:32 +00:00
|
|
|
if params['id'] == 'avrt_end'
|
2010-10-31 21:27:13 +08:00
|
|
|
@page_title += week_from.to_s + t('stats.actions_further')
|
2008-06-06 15:22:44 +00:00
|
|
|
@further = true
|
2008-02-29 19:54:32 +00:00
|
|
|
else
|
|
|
|
@page_title += week_from.to_s + " - " + week_to.to_s + ""
|
|
|
|
end
|
|
|
|
|
|
|
|
# get all running actions that are visible
|
2012-01-03 22:08:50 +01:00
|
|
|
@actions_running_time = current_user.todos.not_completed.not_hidden.not_deferred_or_blocked.find(
|
|
|
|
:all, :select => "todos.id, todos.created_at", :order => "todos.created_at DESC")
|
|
|
|
|
|
|
|
selected_todo_ids = get_ids_from(@actions_running_time, week_from, week_to, params['id']== 'avrt_end')
|
|
|
|
@selected_actions = selected_todo_ids.size == 0 ? [] : current_user.todos.find(:all, { :conditions => "id in (" + selected_todo_ids.join(",") + ")" })
|
|
|
|
@count = @selected_actions.size
|
2011-11-19 02:41:06 +01:00
|
|
|
|
2008-03-01 17:51:33 +00:00
|
|
|
render :action => "show_selection_from_chart"
|
2011-11-19 02:41:06 +01:00
|
|
|
|
2008-03-01 17:51:33 +00:00
|
|
|
when 'art', 'art_end'
|
|
|
|
week_from = params['index'].to_i
|
|
|
|
week_to = week_from+1
|
2011-11-19 02:41:06 +01:00
|
|
|
|
2008-03-01 17:51:33 +00:00
|
|
|
@chart_name = "actions_running_time_data"
|
|
|
|
@page_title = "Actions selected from week "
|
2008-06-06 15:22:44 +00:00
|
|
|
@further = false
|
2008-03-01 17:51:33 +00:00
|
|
|
if params['id'] == 'art_end'
|
|
|
|
@page_title += week_from.to_s + " and further"
|
2008-06-06 15:22:44 +00:00
|
|
|
@further = true
|
2008-03-01 17:51:33 +00:00
|
|
|
else
|
|
|
|
@page_title += week_from.to_s + " - " + week_to.to_s + ""
|
|
|
|
end
|
|
|
|
|
|
|
|
# get all running actions
|
2011-11-20 20:23:28 +01:00
|
|
|
@actions_running_time = current_user.todos.not_completed.find(:all, { :select => "id, created_at" })
|
2008-03-01 17:51:33 +00:00
|
|
|
|
2012-01-03 22:08:50 +01:00
|
|
|
selected_todo_ids = get_ids_from(@actions_running_time, week_from, week_to, params['id']=='art_end')
|
|
|
|
@selected_actions = selected_todo_ids.size == 0 ? [] : current_user.todos.find(:all, { :conditions => "id in (" + selected_todo_ids.join(",") + ")" })
|
|
|
|
@count = @selected_actions.size
|
|
|
|
|
2008-02-29 19:54:32 +00:00
|
|
|
render :action => "show_selection_from_chart"
|
|
|
|
else
|
|
|
|
# render error
|
|
|
|
render_failure "404 NOT FOUND. Unknown query selected"
|
|
|
|
end
|
|
|
|
end
|
2007-09-13 03:21:37 +00:00
|
|
|
|
2011-11-19 02:41:06 +01:00
|
|
|
def done
|
2011-05-03 19:14:30 +02:00
|
|
|
@source_view = 'done'
|
2011-11-19 02:41:06 +01:00
|
|
|
|
2011-06-17 20:17:01 +02:00
|
|
|
init_not_done_counts
|
2011-11-19 02:41:06 +01:00
|
|
|
|
2011-06-17 20:17:01 +02:00
|
|
|
@done_recently = current_user.todos.completed.all(:limit => 10, :order => 'completed_at DESC', :include => Todo::DEFAULT_INCLUDES)
|
2011-06-12 05:38:45 +02:00
|
|
|
@last_completed_projects = current_user.projects.completed.all(:limit => 10, :order => 'completed_at DESC', :include => [:todos, :notes])
|
2011-05-03 19:14:30 +02:00
|
|
|
@last_completed_contexts = []
|
2011-06-17 20:17:01 +02:00
|
|
|
@last_completed_recurring_todos = current_user.recurring_todos.completed.all(:limit => 10, :order => 'completed_at DESC', :include => [:tags, :taggings])
|
2011-06-12 05:38:45 +02:00
|
|
|
#TODO: @last_completed_contexts = current_user.contexts.completed.all(:limit => 10, :order => 'completed_at DESC')
|
2011-05-03 19:14:30 +02:00
|
|
|
end
|
|
|
|
|
2007-09-13 03:21:37 +00:00
|
|
|
private
|
2007-10-05 19:20:18 +00:00
|
|
|
|
2009-01-08 10:18:03 +01:00
|
|
|
def get_unique_tags_of_user
|
2011-11-20 20:23:28 +01:00
|
|
|
tag_ids = current_user.todos.find_by_sql([
|
2009-01-08 10:18:03 +01:00
|
|
|
"SELECT DISTINCT tags.id as id "+
|
|
|
|
"FROM tags, taggings, todos "+
|
2011-11-20 20:23:28 +01:00
|
|
|
"WHERE tags.id = taggings.tag_id " +
|
2012-01-03 22:08:50 +01:00
|
|
|
"AND taggings.taggable_id = todos.id "+
|
|
|
|
"AND todos.user_id = #{current_user.id}"])
|
2009-01-08 10:18:03 +01:00
|
|
|
tags_ids_s = tag_ids.map(&:id).sort.join(",")
|
2009-01-17 14:43:06 +01:00
|
|
|
return {} if tags_ids_s.blank? # return empty hash for .size to work
|
2009-01-08 10:18:03 +01:00
|
|
|
return Tag.find(:all, :conditions => "id in (" + tags_ids_s + ")")
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_total_number_of_tags_of_user
|
|
|
|
# same query as get_unique_tags_of_user except for the DISTINCT
|
2011-11-20 20:23:28 +01:00
|
|
|
return current_user.todos.find_by_sql([
|
2009-01-08 10:18:03 +01:00
|
|
|
"SELECT tags.id as id "+
|
|
|
|
"FROM tags, taggings, todos "+
|
2011-11-20 20:23:28 +01:00
|
|
|
"WHERE tags.id = taggings.tag_id " +
|
2012-01-03 22:08:50 +01:00
|
|
|
"AND taggings.taggable_id = todos.id " +
|
|
|
|
"AND todos.user_id = #{current_user.id}"]).size
|
2009-01-08 10:18:03 +01:00
|
|
|
end
|
|
|
|
|
2007-10-05 19:20:18 +00:00
|
|
|
def init
|
2011-11-19 02:41:06 +01:00
|
|
|
@me = self # for meta programming
|
2008-02-29 19:54:32 +00:00
|
|
|
|
|
|
|
# default chart dimensions
|
|
|
|
@chart_width=460
|
|
|
|
@chart_height=250
|
|
|
|
@pie_width=@chart_width
|
|
|
|
@pie_height=325
|
2011-11-19 02:41:06 +01:00
|
|
|
|
2007-10-12 19:44:07 +00:00
|
|
|
# get the current date wih time set to 0:0
|
2011-11-19 02:41:06 +01:00
|
|
|
@today = Time.zone.now.beginning_of_day
|
2007-10-05 19:20:18 +00:00
|
|
|
|
|
|
|
# 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
|
2011-11-19 02:41:06 +01:00
|
|
|
@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
|
2007-10-05 19:20:18 +00:00
|
|
|
end
|
2011-11-19 02:41:06 +01:00
|
|
|
|
2007-09-13 03:21:37 +00:00
|
|
|
def get_stats_actions
|
|
|
|
# time to complete
|
2011-11-20 20:23:28 +01:00
|
|
|
@completed_actions = current_user.todos.completed.find(:all, { :select => "completed_at, created_at" })
|
2011-11-19 02:41:06 +01:00
|
|
|
|
2012-01-03 22:08:50 +01:00
|
|
|
actions_sum, actions_max = 0,0
|
|
|
|
actions_min = @completed_actions.first.completed_at - @completed_actions.first.created_at
|
|
|
|
|
2007-10-05 19:20:18 +00:00
|
|
|
@completed_actions.each do |r|
|
|
|
|
actions_sum += (r.completed_at - r.created_at)
|
2011-11-20 20:23:28 +01:00
|
|
|
actions_max = [(r.completed_at - r.created_at), actions_max].max
|
|
|
|
actions_min = [(r.completed_at - r.created_at), actions_min].min
|
2007-10-05 19:20:18 +00:00
|
|
|
end
|
2011-11-19 02:41:06 +01:00
|
|
|
|
2007-10-05 19:20:18 +00:00
|
|
|
sum_actions = @completed_actions.size
|
2012-01-03 22:08:50 +01:00
|
|
|
sum_actions = 1 if sum_actions==0 # to prevent dividing by zero
|
2011-11-19 02:41:06 +01:00
|
|
|
|
2007-10-05 19:20:18 +00:00
|
|
|
@actions_avg_ttc = (actions_sum/sum_actions)/@seconds_per_day
|
|
|
|
@actions_max_ttc = actions_max/@seconds_per_day
|
|
|
|
@actions_min_ttc = actions_min/@seconds_per_day
|
2011-11-19 02:41:06 +01:00
|
|
|
|
2012-01-03 22:08:50 +01:00
|
|
|
min_ttc_sec = Time.utc(2000,1,1,0,0)+actions_min # convert to a datetime
|
2007-10-05 19:20:18 +00:00
|
|
|
@actions_min_ttc_sec = (min_ttc_sec).strftime("%H:%M:%S")
|
|
|
|
@actions_min_ttc_sec = (actions_min / @seconds_per_day).round.to_s + " days " + @actions_min_ttc_sec if actions_min > @seconds_per_day
|
|
|
|
|
2007-09-18 05:11:58 +00:00
|
|
|
# get count of actions created and actions done in the past 30 days.
|
2011-11-20 20:23:28 +01:00
|
|
|
@sum_actions_done_last30days = current_user.todos.completed.completed_after(@cut_off_month).count(:all)
|
|
|
|
@sum_actions_created_last30days = current_user.todos.created_after(@cut_off_month).count(:all)
|
2011-11-19 02:41:06 +01:00
|
|
|
|
2007-09-18 05:11:58 +00:00
|
|
|
# get count of actions done in the past 12 months.
|
2011-11-20 20:23:28 +01:00
|
|
|
@sum_actions_done_last12months = current_user.todos.completed.completed_after(@cut_off_year).count(:all)
|
|
|
|
@sum_actions_created_last12months = current_user.todos.created_after(@cut_off_year).count(:all)
|
2007-09-13 03:21:37 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def get_stats_contexts
|
|
|
|
# get action count per context for TOP 5
|
2009-01-08 10:18:03 +01:00
|
|
|
#
|
2008-02-29 19:54:32 +00:00
|
|
|
# Went from GROUP BY c.id to c.id, c.name for compatibility with postgresql.
|
|
|
|
# Since the name is forced to be unique, this should work.
|
2011-11-20 20:23:28 +01:00
|
|
|
@actions_per_context = current_user.contexts.find_by_sql(
|
2007-12-16 21:54:37 +00:00
|
|
|
"SELECT c.id AS id, c.name AS name, count(*) AS total "+
|
2007-09-18 05:11:58 +00:00
|
|
|
"FROM contexts c, todos t "+
|
|
|
|
"WHERE t.context_id=c.id "+
|
2012-01-03 22:08:50 +01:00
|
|
|
"AND t.user_id=#{current_user.id} " +
|
2007-12-18 08:04:47 +00:00
|
|
|
"GROUP BY c.id, c.name ORDER BY total DESC " +
|
2007-09-18 05:11:58 +00:00
|
|
|
"LIMIT 5"
|
2007-09-13 03:21:37 +00:00
|
|
|
)
|
|
|
|
|
2008-03-12 22:34:48 +00:00
|
|
|
# get incomplete action count per visible context for TOP 5
|
2009-01-08 10:18:03 +01:00
|
|
|
#
|
2008-02-29 19:54:32 +00:00
|
|
|
# Went from GROUP BY c.id to c.id, c.name for compatibility with postgresql.
|
|
|
|
# Since the name is forced to be unique, this should work.
|
2011-11-20 20:23:28 +01:00
|
|
|
@running_actions_per_context = current_user.contexts.find_by_sql(
|
2007-12-16 21:54:37 +00:00
|
|
|
"SELECT c.id AS id, c.name AS name, count(*) AS total "+
|
2007-09-18 05:11:58 +00:00
|
|
|
"FROM contexts c, todos t "+
|
|
|
|
"WHERE t.context_id=c.id AND t.completed_at IS NULL AND NOT c.hide "+
|
2012-01-03 22:08:50 +01:00
|
|
|
"AND t.user_id=#{current_user.id} " +
|
2007-12-18 08:04:47 +00:00
|
|
|
"GROUP BY c.id, c.name ORDER BY total DESC " +
|
2007-09-18 05:11:58 +00:00
|
|
|
"LIMIT 5"
|
2011-11-19 02:41:06 +01:00
|
|
|
)
|
2007-09-13 03:21:37 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def get_stats_projects
|
|
|
|
# get the first 10 projects and their action count (all actions)
|
2009-01-08 10:18:03 +01:00
|
|
|
#
|
2007-12-08 21:05:24 +00:00
|
|
|
# Went from GROUP BY p.id to p.name for compatibility with postgresql. Since
|
|
|
|
# the name is forced to be unique, this should work.
|
2011-11-20 20:23:28 +01:00
|
|
|
@projects_and_actions = current_user.projects.find_by_sql(
|
2007-12-16 21:54:37 +00:00
|
|
|
"SELECT p.id, p.name, count(*) AS count "+
|
2007-09-18 05:11:58 +00:00
|
|
|
"FROM projects p, todos t "+
|
|
|
|
"WHERE p.id = t.project_id "+
|
2012-01-03 22:08:50 +01:00
|
|
|
"AND t.user_id=#{current_user.id} " +
|
2007-12-18 08:04:47 +00:00
|
|
|
"GROUP BY p.id, p.name "+
|
2007-09-18 05:11:58 +00:00
|
|
|
"ORDER BY count DESC " +
|
|
|
|
"LIMIT 10"
|
2007-09-13 03:21:37 +00:00
|
|
|
)
|
2011-11-19 02:41:06 +01:00
|
|
|
|
2007-09-18 05:11:58 +00:00
|
|
|
# get the first 10 projects with their actions count of actions that have
|
|
|
|
# been created or completed the past 30 days
|
2011-11-19 02:41:06 +01:00
|
|
|
|
2007-12-08 21:05:24 +00:00
|
|
|
# using GROUP BY p.name (was: p.id) for compatibility with Postgresql. Since
|
|
|
|
# you cannot create two contexts with the same name, this will work.
|
2011-11-20 20:23:28 +01:00
|
|
|
@projects_and_actions_last30days = current_user.projects.find_by_sql([
|
2007-12-16 21:54:37 +00:00
|
|
|
"SELECT p.id, p.name, count(*) AS count "+
|
2007-10-05 19:20:18 +00:00
|
|
|
"FROM todos t, projects p "+
|
|
|
|
"WHERE t.project_id = p.id AND "+
|
|
|
|
" (t.created_at > ? OR t.completed_at > ?) "+
|
2012-01-03 22:08:50 +01:00
|
|
|
"AND t.user_id=#{current_user.id} " +
|
2007-12-18 08:04:47 +00:00
|
|
|
"GROUP BY p.id, p.name "+
|
2007-10-05 19:20:18 +00:00
|
|
|
"ORDER BY count DESC " +
|
2011-11-20 20:23:28 +01:00
|
|
|
"LIMIT 10", @cut_off_month, @cut_off_month]
|
2007-09-13 03:21:37 +00:00
|
|
|
)
|
2011-11-19 02:41:06 +01:00
|
|
|
|
2007-09-18 05:11:58 +00:00
|
|
|
# get the first 10 projects and their running time (creation date versus
|
|
|
|
# now())
|
2011-11-20 20:23:28 +01:00
|
|
|
@projects_and_runtime_sql = current_user.projects.find_by_sql(
|
2007-12-16 21:54:37 +00:00
|
|
|
"SELECT id, name, created_at "+
|
2007-10-05 19:20:18 +00:00
|
|
|
"FROM projects "+
|
2007-09-18 05:11:58 +00:00
|
|
|
"WHERE state='active' "+
|
2012-01-03 22:08:50 +01:00
|
|
|
"AND user_id=#{current_user.id} "+
|
2007-10-05 19:20:18 +00:00
|
|
|
"ORDER BY created_at ASC "+
|
2007-09-18 05:11:58 +00:00
|
|
|
"LIMIT 10"
|
2007-09-13 03:21:37 +00:00
|
|
|
)
|
2007-10-05 19:20:18 +00:00
|
|
|
|
|
|
|
i=0
|
2007-12-16 21:54:37 +00:00
|
|
|
@projects_and_runtime = Array.new(10, [-1, "n/a", "n/a"])
|
2007-10-05 19:20:18 +00:00
|
|
|
@projects_and_runtime_sql.each do |r|
|
2012-01-03 22:08:50 +01:00
|
|
|
days = difference_in_days(@today, r.created_at)
|
|
|
|
# add one so that a project that you just created returns 1 day
|
2007-12-16 21:54:37 +00:00
|
|
|
@projects_and_runtime[i]=[r.id, r.name, days.to_i+1]
|
2007-10-05 19:20:18 +00:00
|
|
|
i += 1
|
|
|
|
end
|
2011-11-19 02:41:06 +01:00
|
|
|
|
2007-09-13 03:21:37 +00:00
|
|
|
end
|
2011-11-19 02:41:06 +01:00
|
|
|
|
2007-09-13 03:21:37 +00:00
|
|
|
def get_stats_tags
|
2007-09-18 05:11:58 +00:00
|
|
|
# tag cloud code inspired by this article
|
|
|
|
# http://www.juixe.com/techknow/index.php/2006/07/15/acts-as-taggable-tag-cloud/
|
|
|
|
|
2007-09-26 11:23:06 +00:00
|
|
|
levels=10
|
2007-09-18 05:11:58 +00:00
|
|
|
# TODO: parameterize limit
|
2011-11-19 02:41:06 +01:00
|
|
|
|
2007-09-26 11:23:06 +00:00
|
|
|
# Get the tag cloud for all tags for actions
|
2007-09-18 05:11:58 +00:00
|
|
|
query = "SELECT tags.id, name, count(*) AS count"
|
2009-01-08 10:18:03 +01:00
|
|
|
query << " FROM taggings, tags, todos"
|
2007-09-18 05:11:58 +00:00
|
|
|
query << " WHERE tags.id = tag_id"
|
2009-01-08 10:18:03 +01:00
|
|
|
query << " AND taggings.taggable_id = todos.id"
|
|
|
|
query << " AND todos.user_id="+current_user.id.to_s+" "
|
2007-09-26 11:23:06 +00:00
|
|
|
query << " AND taggings.taggable_type='Todo' "
|
2007-12-14 20:02:09 +00:00
|
|
|
query << " GROUP BY tags.id, tags.name"
|
2007-09-18 05:11:58 +00:00
|
|
|
query << " ORDER BY count DESC, name"
|
|
|
|
query << " LIMIT 100"
|
2007-09-13 03:21:37 +00:00
|
|
|
@tags_for_cloud = Tag.find_by_sql(query).sort_by { |tag| tag.name.downcase }
|
2011-11-19 02:41:06 +01:00
|
|
|
|
2007-09-13 03:21:37 +00:00
|
|
|
max, @tags_min = 0, 0
|
|
|
|
@tags_for_cloud.each { |t|
|
2011-11-20 20:23:28 +01:00
|
|
|
max = [t.count.to_i, max].max
|
|
|
|
@tags_min = [t.count.to_i, @tags_min].min
|
2007-09-13 03:21:37 +00:00
|
|
|
}
|
|
|
|
|
2007-09-26 11:23:06 +00:00
|
|
|
@tags_divisor = ((max - @tags_min) / levels) + 1
|
2007-09-13 03:21:37 +00:00
|
|
|
|
2007-10-05 19:20:18 +00:00
|
|
|
# Get the tag cloud for all tags for actions
|
2007-09-26 11:23:06 +00:00
|
|
|
query = "SELECT tags.id, tags.name AS name, count(*) AS count"
|
|
|
|
query << " FROM taggings, tags, todos"
|
|
|
|
query << " WHERE tags.id = tag_id"
|
2009-01-08 10:18:03 +01:00
|
|
|
query << " AND todos.user_id=? "
|
2007-09-26 11:23:06 +00:00
|
|
|
query << " AND taggings.taggable_type='Todo' "
|
|
|
|
query << " AND taggings.taggable_id=todos.id "
|
2007-10-05 19:20:18 +00:00
|
|
|
query << " AND (todos.created_at > ? OR "
|
|
|
|
query << " todos.completed_at > ?) "
|
2007-12-14 20:02:09 +00:00
|
|
|
query << " GROUP BY tags.id, tags.name"
|
2007-09-26 11:23:06 +00:00
|
|
|
query << " ORDER BY count DESC, name"
|
|
|
|
query << " LIMIT 100"
|
2007-10-05 19:20:18 +00:00
|
|
|
@tags_for_cloud_90days = Tag.find_by_sql(
|
2009-01-08 10:18:03 +01:00
|
|
|
[query, current_user.id, @cut_off_3months, @cut_off_3months]
|
2007-10-05 19:20:18 +00:00
|
|
|
).sort_by { |tag| tag.name.downcase }
|
2007-09-26 11:23:06 +00:00
|
|
|
|
|
|
|
max_90days, @tags_min_90days = 0, 0
|
|
|
|
@tags_for_cloud_90days.each { |t|
|
2011-11-20 20:23:28 +01:00
|
|
|
max_90days = [t.count.to_i, max_90days].max
|
|
|
|
@tags_min_90days = [t.count.to_i, @tags_min_90days].min
|
2007-09-26 11:23:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@tags_divisor_90days = ((max_90days - @tags_min_90days) / levels) + 1
|
2007-09-13 03:21:37 +00:00
|
|
|
end
|
2011-11-19 02:41:06 +01:00
|
|
|
|
2012-01-03 22:08:50 +01:00
|
|
|
def get_ids_from (actions, week_from, week_to, at_end)
|
|
|
|
selected_todo_ids = []
|
2011-11-19 02:41:06 +01:00
|
|
|
|
2012-01-03 22:08:50 +01:00
|
|
|
actions.each do |r|
|
|
|
|
weeks = difference_in_weeks(@today, r.created_at)
|
2008-03-01 17:51:33 +00:00
|
|
|
if at_end
|
2012-01-03 22:08:50 +01:00
|
|
|
selected_todo_ids << r.id.to_s if weeks >= week_from
|
2008-03-01 17:51:33 +00:00
|
|
|
else
|
2012-01-03 22:08:50 +01:00
|
|
|
selected_todo_ids << r.id.to_s if weeks.between?(week_from, week_to-1)
|
2008-03-01 17:51:33 +00:00
|
|
|
end
|
|
|
|
end
|
2011-11-19 02:41:06 +01:00
|
|
|
|
2012-01-03 22:08:50 +01:00
|
|
|
return selected_todo_ids
|
2008-03-01 17:51:33 +00:00
|
|
|
end
|
2011-11-19 02:41:06 +01:00
|
|
|
|
2011-11-28 23:38:57 +01:00
|
|
|
def convert_to_array(hash, upper_bound)
|
|
|
|
return Array.new(upper_bound){ |i| hash[i] }
|
|
|
|
end
|
|
|
|
|
2012-01-20 23:22:21 +01:00
|
|
|
# uses the supplied block to determine index in hash
|
|
|
|
def convert_to_hash(records)
|
2011-11-19 02:41:06 +01:00
|
|
|
# use 0 to initialise action count to zero
|
|
|
|
hash = Hash.new(0)
|
2012-01-20 23:22:21 +01:00
|
|
|
records.each { |r| hash[ yield r ] += 1 }
|
2011-11-19 02:41:06 +01:00
|
|
|
return hash
|
|
|
|
end
|
2012-01-20 23:22:21 +01:00
|
|
|
|
|
|
|
def convert_to_months_from_today_array(records, array_size, date_method_on_todo)
|
|
|
|
return convert_to_array(convert_to_hash(records){ |r| difference_in_months(@today, r.send(date_method_on_todo))}, array_size)
|
2011-11-28 23:38:57 +01:00
|
|
|
end
|
|
|
|
|
2012-01-20 23:22:21 +01:00
|
|
|
def convert_to_days_from_today_array(records, array_size, date_method_on_todo)
|
|
|
|
return convert_to_array(convert_to_hash(records){ |r| difference_in_days(@today, r.send(date_method_on_todo))}, array_size)
|
2011-11-28 23:38:57 +01:00
|
|
|
end
|
|
|
|
|
2012-01-20 23:22:21 +01:00
|
|
|
def convert_to_weeks_from_today_array(records, array_size, date_method_on_todo)
|
|
|
|
return convert_to_array(convert_to_hash(records) { |r| difference_in_weeks(@today, r.send(date_method_on_todo))}, array_size)
|
|
|
|
end
|
|
|
|
|
|
|
|
def convert_to_weeks_running_array(records, array_size)
|
|
|
|
return convert_to_array(convert_to_hash(records) { |r| difference_in_weeks(r.completed_at, r.created_at)}, array_size)
|
|
|
|
end
|
|
|
|
|
2011-12-09 17:17:42 +01:00
|
|
|
# returns a new array containing all elems of array up to cut_off and
|
|
|
|
# adds the sum of the rest of array to the last elem
|
|
|
|
def cut_off_array(array, cut_off)
|
|
|
|
# +1 to hold sum of rest
|
|
|
|
a = Array.new(cut_off+1){|i| array[i]||0}
|
|
|
|
# add rest of array to last elem
|
|
|
|
a[cut_off] += array.inject(:+) - a.inject(:+)
|
|
|
|
return a
|
|
|
|
end
|
|
|
|
|
|
|
|
def convert_to_cummulative_array(array, max)
|
|
|
|
# calculate fractions
|
|
|
|
a = Array.new(array.size){|i| array[i]*100.0/max}
|
|
|
|
# make cummulative
|
|
|
|
1.upto(array.size-1){ |i| a[i] += a[i-1] }
|
|
|
|
return a
|
|
|
|
end
|
|
|
|
|
2011-11-19 02:41:06 +01:00
|
|
|
# assumes date1 > date2
|
2011-12-09 17:17:42 +01:00
|
|
|
# this results in the number of months before the month of date1, not taking days into account, so diff of 31-12 and 1-1 is 1 month!
|
2011-11-19 02:41:06 +01:00
|
|
|
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
|
2011-11-28 23:38:57 +01:00
|
|
|
|
2011-12-09 17:17:42 +01:00
|
|
|
# assumes date1 > date2
|
2011-11-28 23:38:57 +01:00
|
|
|
def difference_in_weeks(date1, date2)
|
|
|
|
return difference_in_days(date1, date2) / 7
|
|
|
|
end
|
2011-11-19 02:41:06 +01:00
|
|
|
|
2011-11-28 23:38:57 +01:00
|
|
|
def three_month_avg(set, i)
|
2011-12-09 17:17:42 +01:00
|
|
|
return ( (set[i]||0) + (set[i+1]||0) + (set[i+2]||0) ) / 3.0
|
2011-11-19 02:41:06 +01:00
|
|
|
end
|
|
|
|
|
2011-11-28 23:38:57 +01:00
|
|
|
def interpolate_avg(set, percent)
|
2012-01-20 23:50:27 +01:00
|
|
|
return (set[0]*(1/percent) + set[1] + set[2]) / 3.0
|
2011-11-19 02:41:06 +01:00
|
|
|
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
|
2011-11-28 23:38:57 +01:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2012-01-20 23:50:27 +01:00
|
|
|
end
|