2007-09-13 03:21:37 +00:00
|
|
|
class StatsController < ApplicationController
|
2007-10-05 19:20:18 +00:00
|
|
|
|
2012-09-08 12:55:06 -05:00
|
|
|
SECONDS_PER_DAY = 86400;
|
|
|
|
|
2011-06-12 05:38:45 +02:00
|
|
|
helper :todos, :projects, :recurring_todos
|
2013-03-02 17:27:38 -07:00
|
|
|
append_before_filter :init, :except => :index
|
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')
|
|
|
|
@hidden_contexts = current_user.contexts.hidden
|
2013-03-05 07:49:44 -07:00
|
|
|
@stats = Stats::UserStats.new(current_user)
|
2011-11-19 02:41:06 +01:00
|
|
|
end
|
2014-08-14 21:05:05 -05: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
|
2013-07-18 22:32:44 -05:00
|
|
|
# - 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")
|
2011-11-28 23:38:57 +01:00
|
|
|
|
|
|
|
# convert to array and fill in non-existing months
|
2013-07-18 22:32:44 -05:00
|
|
|
@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)
|
2014-08-14 21:05:05 -05:00
|
|
|
|
2011-11-28 23:38:57 +01:00
|
|
|
# find max for graph in both arrays
|
2013-07-18 22:32:44 -05:00
|
|
|
@max = (@actions_done_last12months_array + @actions_created_last12months_array).max
|
2011-11-28 23:38:57 +01:00
|
|
|
|
|
|
|
# find running avg
|
2013-07-18 22:32:44 -05:00
|
|
|
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)
|
2013-07-18 21:01:39 -05:00
|
|
|
|
2013-07-18 23:22:44 -05:00
|
|
|
@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)
|
2011-11-19 02:41:06 +01:00
|
|
|
|
|
|
|
# interpolate avg for current month.
|
2013-07-18 22:32:44 -05:00
|
|
|
@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)
|
2011-11-19 02:41:06 +01:00
|
|
|
|
2014-04-11 22:47:36 +02:00
|
|
|
@created_count_array = Array.new(13, actions_last12months.created_after(@cut_off_year).count(:all)/12.0)
|
|
|
|
@done_count_array = Array.new(13, actions_last12months.completed_after(@cut_off_year).count(:all)/12.0)
|
2007-09-13 03:21:37 +00:00
|
|
|
render :layout => false
|
|
|
|
end
|
2011-11-19 02:41:06 +01:00
|
|
|
|
2013-07-18 22:32:44 -05:00
|
|
|
def interpolate_avg_for_current_month(set)
|
2013-07-18 23:37:34 -05:00
|
|
|
(set[0]*(1/percent_of_month) + set[1] + set[2]) / 3.0
|
2013-07-18 16:13:01 -05:00
|
|
|
end
|
|
|
|
|
2013-07-18 23:48:33 -05:00
|
|
|
def percent_of_month
|
|
|
|
Time.zone.now.day / Time.zone.now.end_of_month.day.to_f
|
|
|
|
end
|
|
|
|
|
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')
|
2013-03-01 20:38:24 -05:00
|
|
|
@chart = Stats::Chart.new('actions_done_lastyears_data', :height => 400, :width => 900)
|
2008-07-04 17:56:59 +02:00
|
|
|
end
|
2011-11-19 02:41:06 +01:00
|
|
|
|
2008-07-04 17:56:59 +02:00
|
|
|
def actions_done_lastyears_data
|
2013-07-18 22:32:44 -05:00
|
|
|
actions_last_months = current_user.todos.select("completed_at,created_at")
|
2014-08-14 21:05:05 -05:00
|
|
|
|
2013-07-18 23:05:38 -05:00
|
|
|
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
|
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
|
2013-07-18 22:32:44 -05:00
|
|
|
@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)
|
2008-07-04 17:56:59 +02:00
|
|
|
|
|
|
|
# find max for graph in both hashes
|
2013-07-18 23:05:38 -05:00
|
|
|
@max = (@actions_done_last_months_array + @actions_created_last_months_array).max
|
2008-07-04 17:56:59 +02:00
|
|
|
|
2013-07-18 23:05:38 -05:00
|
|
|
# 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)
|
2011-11-19 02:41:06 +01:00
|
|
|
|
|
|
|
# interpolate avg for this month.
|
2013-07-18 22:32:44 -05:00
|
|
|
@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)
|
2013-07-18 16:13:01 -05:00
|
|
|
|
2013-07-18 22:32:44 -05:00
|
|
|
@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)
|
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.
|
2013-04-22 11:11:53 +02:00
|
|
|
@actions_done_last30days = current_user.todos.completed_after(@cut_off_30days).select("completed_at")
|
|
|
|
@actions_created_last30days = current_user.todos.created_after(@cut_off_30days).select("created_at")
|
2011-11-19 02:41:06 +01:00
|
|
|
|
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
|
2012-04-24 20:47:07 +02:00
|
|
|
@actions_completion_time = current_user.todos.completed.select("completed_at, created_at").reorder("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
|
2013-01-06 16:01:53 +01:00
|
|
|
@max_weeks = @actions_completion_time.last ? difference_in_weeks(@today, @actions_completion_time.last.completed_at) : 1
|
2012-01-20 23:22:21 +01:00
|
|
|
@actions_completed_per_week_array = convert_to_weeks_running_array(@actions_completion_time, @max_weeks+1)
|
2014-08-14 21:05:05 -05: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
|
2014-08-14 21:05:05 -05:00
|
|
|
|
2011-12-09 17:17:42 +01:00
|
|
|
# convert to new array to hold max @cut_off elems + 1 for sum of actions after @cut_off
|
2012-01-21 20:20:24 +01:00
|
|
|
@actions_completion_time_array = cut_off_array_with_sum(@actions_completed_per_week_array, @count)
|
2011-12-09 17:17:42 +01:00
|
|
|
@max_actions = @actions_completion_time_array.max
|
|
|
|
|
2013-02-28 20:34:46 -05:00
|
|
|
# get percentage done cumulative
|
2014-04-11 22:47:36 +02:00
|
|
|
@cum_percent_done = convert_to_cumulative_array(@actions_completion_time_array, @actions_completion_time.count(:all))
|
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
|
2012-04-24 20:47:07 +02:00
|
|
|
@actions_running_time = current_user.todos.not_completed.select("created_at").reorder("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
|
2014-08-14 21:05:05 -05:00
|
|
|
|
2011-12-09 17:17:42 +01:00
|
|
|
# convert to new array to hold max @cut_off elems + 1 for sum of actions after @cut_off
|
2012-01-21 20:20:24 +01:00
|
|
|
@actions_running_time_array = cut_off_array_with_sum(@actions_running_per_week_array, @count)
|
2011-12-09 17:17:42 +01:00
|
|
|
@max_actions = @actions_running_time_array.max
|
2011-11-19 02:41:06 +01:00
|
|
|
|
2013-02-28 20:34:46 -05:00
|
|
|
# get percentage done cumulative
|
|
|
|
@cum_percent_done = convert_to_cumulative_array(@actions_running_time_array, @actions_running_time.count )
|
2014-08-14 21:05:05 -05:00
|
|
|
|
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
|
|
|
|
2012-04-18 14:22:58 +02:00
|
|
|
@actions_running_time = current_user.todos.not_completed.not_hidden.not_deferred_or_blocked.
|
|
|
|
select("todos.created_at").
|
2012-04-24 20:47:07 +02:00
|
|
|
reorder("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
|
2014-08-14 21:05:05 -05:00
|
|
|
|
2011-12-09 17:17:42 +01:00
|
|
|
# convert to new array to hold max @cut_off elems + 1 for sum of actions after @cut_off
|
2012-01-21 20:20:24 +01:00
|
|
|
@actions_running_time_array = cut_off_array_with_sum(@actions_running_per_week_array, @count)
|
2011-12-09 17:17:42 +01:00
|
|
|
@max_actions = @actions_running_time_array.max
|
|
|
|
|
2013-02-28 20:34:46 -05:00
|
|
|
# get percentage done cumulative
|
|
|
|
@cum_percent_done = convert_to_cumulative_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
|
2014-08-14 21:05:05 -05:00
|
|
|
|
2012-01-21 20:20:24 +01:00
|
|
|
def actions_open_per_week_data
|
2012-04-18 14:22:58 +02:00
|
|
|
@actions_started = current_user.todos.created_after(@today-53.weeks).
|
|
|
|
select("todos.created_at, todos.completed_at").
|
2012-04-24 20:47:07 +02:00
|
|
|
reorder("todos.created_at DESC")
|
2014-08-14 21:05:05 -05:00
|
|
|
|
2012-01-21 20:20:24 +01:00
|
|
|
@max_weeks = difference_in_weeks(@today, @actions_started.last.created_at)
|
|
|
|
|
|
|
|
# cut off chart at 52 weeks = one year
|
|
|
|
@count = [52, @max_weeks].min
|
2014-08-14 21:05:05 -05:00
|
|
|
|
2012-04-27 14:22:16 +02:00
|
|
|
@actions_open_per_week_array = convert_to_weeks_running_from_today_array(@actions_started, @max_weeks+1)
|
2012-01-21 20:20:24 +01:00
|
|
|
@actions_open_per_week_array = cut_off_array(@actions_open_per_week_array, @count)
|
2012-09-03 20:32:26 +02:00
|
|
|
@max_actions = (@actions_open_per_week_array.max or 0)
|
2014-08-14 21:05:05 -05:00
|
|
|
|
2012-01-21 20:20:24 +01:00
|
|
|
render :layout => false
|
|
|
|
end
|
2007-09-13 03:21:37 +00:00
|
|
|
|
|
|
|
def context_total_actions_data
|
2013-03-17 20:02:16 -06:00
|
|
|
actions = Stats::TopContextsQuery.new(current_user).result
|
|
|
|
|
|
|
|
@data = Stats::PieChartData.new(actions, t('stats.spread_of_actions_for_all_context'), 70)
|
2011-11-19 02:41:06 +01:00
|
|
|
|
2013-03-17 19:38:57 -06:00
|
|
|
render :pie_chart_data, :layout => false
|
2007-09-13 03:21:37 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def context_running_actions_data
|
2013-03-17 20:02:16 -06:00
|
|
|
actions = Stats::TopContextsQuery.new(current_user, :running => true).result
|
|
|
|
@data = Stats::PieChartData.new(actions, t('stats.spread_of_running_actions_for_visible_contexts'), 60)
|
2011-11-19 02:41:06 +01:00
|
|
|
|
2013-03-17 19:38:57 -06:00
|
|
|
render :pie_chart_data, :layout => false
|
2007-09-13 03:21:37 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def actions_day_of_week_all_data
|
2012-04-18 14:22:58 +02:00
|
|
|
@actions_creation_day = current_user.todos.select("created_at")
|
|
|
|
@actions_completion_day = current_user.todos.completed.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
|
2012-04-18 14:22:58 +02:00
|
|
|
@actions_creation_day = current_user.todos.created_after(@cut_off_month).select("created_at")
|
|
|
|
@actions_completion_day = current_user.todos.completed_after(@cut_off_month).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
|
2012-04-18 14:22:58 +02:00
|
|
|
@actions_creation_hour = current_user.todos.select("created_at")
|
|
|
|
@actions_completion_hour = current_user.todos.completed.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
|
2012-04-18 14:22:58 +02:00
|
|
|
@actions_creation_hour = current_user.todos.created_after(@cut_off_month).select("created_at")
|
|
|
|
@actions_completion_hour = current_user.todos.completed_after(@cut_off_month).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
|
|
|
|
2013-03-01 20:38:24 -05:00
|
|
|
@chart = Stats::Chart.new('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-04-18 14:22:58 +02:00
|
|
|
@actions_running_time = current_user.todos.not_completed.not_hidden.not_deferred_or_blocked.
|
|
|
|
select("todos.id, todos.created_at").
|
2012-04-24 20:47:07 +02:00
|
|
|
reorder("todos.created_at DESC")
|
2012-01-03 22:08:50 +01:00
|
|
|
|
|
|
|
selected_todo_ids = get_ids_from(@actions_running_time, week_from, week_to, params['id']== 'avrt_end')
|
2012-04-18 14:22:58 +02:00
|
|
|
@selected_actions = selected_todo_ids.size == 0 ? [] : current_user.todos.where("id in (" + selected_todo_ids.join(",") + ")")
|
2012-01-03 22:08:50 +01:00
|
|
|
@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
|
|
|
|
2013-03-01 20:38:24 -05:00
|
|
|
@chart = Stats::Chart.new('actions_running_time_data')
|
2008-03-01 17:51:33 +00:00
|
|
|
@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
|
2012-04-18 14:22:58 +02:00
|
|
|
@actions_running_time = current_user.todos.not_completed.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')
|
2012-04-18 14:22:58 +02:00
|
|
|
@selected_actions = selected_todo_ids.size == 0 ? [] : current_user.todos.where("id in (#{selected_todo_ids.join(",")})")
|
2012-01-03 22:08:50 +01:00
|
|
|
@count = @selected_actions.size
|
2014-08-14 21:05:05 -05:00
|
|
|
|
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
|
|
|
|
2012-04-24 20:47:07 +02:00
|
|
|
@done_recently = current_user.todos.completed.limit(10).reorder('completed_at DESC').includes(Todo::DEFAULT_INCLUDES)
|
|
|
|
@last_completed_projects = current_user.projects.completed.limit(10).reorder('completed_at DESC').includes(:todos, :notes)
|
2011-05-03 19:14:30 +02:00
|
|
|
@last_completed_contexts = []
|
2012-04-24 20:47:07 +02:00
|
|
|
@last_completed_recurring_todos = current_user.recurring_todos.completed.limit(10).reorder('completed_at DESC').includes(: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
|
|
|
|
|
|
|
def init
|
2011-11-19 02:41:06 +01:00
|
|
|
@me = self # for meta programming
|
2008-02-29 19:54:32 +00:00
|
|
|
|
2007-10-12 19:44:07 +00:00
|
|
|
# get the current date wih time set to 0:0
|
2012-04-27 14:22:16 +02:00
|
|
|
@today = Time.zone.now.utc.beginning_of_day
|
2007-10-05 19:20:18 +00:00
|
|
|
|
|
|
|
# define cut_off date and discard the time for a month, 3 months and a year
|
2012-05-18 15:33:47 +02:00
|
|
|
@cut_off_year = 12.months.ago.beginning_of_day
|
|
|
|
@cut_off_year_plus3 = 15.months.ago.beginning_of_day
|
2011-11-19 02:41:06 +01:00
|
|
|
@cut_off_month = 1.month.ago.beginning_of_day
|
2013-04-22 11:11:53 +02:00
|
|
|
@cut_off_30days = 30.days.ago.beginning_of_day
|
2007-10-05 19:20:18 +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
|
|
|
|
2012-01-21 20:20:24 +01:00
|
|
|
# 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
|
2012-04-27 14:22:16 +02:00
|
|
|
def convert_to_array(records, upper_bound)
|
2013-07-18 22:32:44 -05:00
|
|
|
a = Array.new(upper_bound, 0)
|
2013-07-28 19:00:51 -05:00
|
|
|
records.each { |r| (yield r).each { |i| a[i] += 1 if a[i] } }
|
2013-07-18 22:32:44 -05:00
|
|
|
a
|
2011-11-19 02:41:06 +01:00
|
|
|
end
|
2014-08-14 21:05:05 -05:00
|
|
|
|
2013-07-18 22:32:44 -05:00
|
|
|
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
|
|
|
|
|
2012-01-20 23:22:21 +01:00
|
|
|
def convert_to_days_from_today_array(records, array_size, date_method_on_todo)
|
2012-04-27 14:22:16 +02:00
|
|
|
return convert_to_array(records, array_size){ |r| [difference_in_days(@today, r.send(date_method_on_todo))]}
|
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)
|
2012-04-27 14:22:16 +02:00
|
|
|
return convert_to_array(records, array_size) { |r| [difference_in_weeks(@today, r.send(date_method_on_todo))]}
|
2012-01-20 23:22:21 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def convert_to_weeks_running_array(records, array_size)
|
2012-04-27 14:22:16 +02:00
|
|
|
return convert_to_array(records, array_size) { |r| [difference_in_weeks(r.completed_at, r.created_at)]}
|
2012-01-21 20:20:24 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def convert_to_weeks_running_from_today_array(records, array_size)
|
2012-04-27 14:22:16 +02:00
|
|
|
return convert_to_array(records, array_size) { |r| week_indexes_of(r) }
|
2012-01-21 20:20:24 +01:00
|
|
|
end
|
2014-08-14 21:05:05 -05:00
|
|
|
|
2012-01-21 20:20:24 +01:00
|
|
|
def week_indexes_of(record)
|
|
|
|
a = []
|
|
|
|
start_week = difference_in_weeks(@today, record.created_at)
|
|
|
|
end_week = record.completed_at ? difference_in_weeks(@today, record.completed_at) : 0
|
|
|
|
end_week.upto(start_week) { |i| a << i };
|
|
|
|
return a
|
2012-01-20 23:22:21 +01:00
|
|
|
end
|
2014-08-14 21:05:05 -05:00
|
|
|
|
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
|
2012-01-21 20:20:24 +01:00
|
|
|
def cut_off_array_with_sum(array, cut_off)
|
2011-12-09 17:17:42 +01:00
|
|
|
# +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
|
2014-08-14 21:05:05 -05:00
|
|
|
|
2012-01-21 20:20:24 +01:00
|
|
|
def cut_off_array(array, cut_off)
|
|
|
|
return Array.new(cut_off){|i| array[i]||0}
|
|
|
|
end
|
2011-12-09 17:17:42 +01:00
|
|
|
|
2013-02-28 20:34:46 -05:00
|
|
|
def convert_to_cumulative_array(array, max)
|
2011-12-09 17:17:42 +01:00
|
|
|
# calculate fractions
|
|
|
|
a = Array.new(array.size){|i| array[i]*100.0/max}
|
2013-02-28 20:34:46 -05:00
|
|
|
# make cumulative
|
2011-12-09 17:17:42 +01:00
|
|
|
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
|
2012-04-27 14:22:16 +02:00
|
|
|
# this results in the number of months before the month of date1, not taking days into account, so diff of 31-dec and 1-jan is 1 month!
|
2011-11-19 02:41:06 +01:00
|
|
|
def difference_in_months(date1, date2)
|
2012-04-27 14:22:16 +02:00
|
|
|
return (date1.utc.year - date2.utc.year)*12 + (date1.utc.month - date2.utc.month)
|
2011-11-19 02:41:06 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
# assumes date1 > date2
|
|
|
|
def difference_in_days(date1, date2)
|
2012-09-08 12:55:06 -05:00
|
|
|
return ((date1.utc.at_midnight-date2.utc.at_midnight)/SECONDS_PER_DAY).to_i
|
2011-11-19 02:41:06 +01:00
|
|
|
end
|
2014-08-14 21:05:05 -05: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)
|
2015-08-19 14:49:52 +02:00
|
|
|
(set.fetch(i){ 0 } + set.fetch(i+1){ 0 } + set.fetch(i+2){ 0 }) / 3.0
|
2011-11-19 02:41:06 +01:00
|
|
|
end
|
|
|
|
|
2013-07-18 23:43:44 -05:00
|
|
|
def set_three_month_avg(set,upper_bound)
|
|
|
|
(0..upper_bound-1).map { |i| three_month_avg(set, i) }
|
|
|
|
end
|
|
|
|
|
2013-07-18 23:22:44 -05:00
|
|
|
# sets "null" on first column and - if necessary - cleans up last two columns, which may have insufficient data
|
2013-07-18 23:05:38 -05:00
|
|
|
def compute_running_avg_array(set, upper_bound)
|
2013-07-18 23:43:44 -05:00
|
|
|
result = set_three_month_avg(set, upper_bound)
|
2014-08-14 21:05:05 -05:00
|
|
|
result[upper_bound-1] = result[upper_bound-1] * 3 if upper_bound == set.length
|
2013-07-18 23:22:44 -05:00
|
|
|
result[upper_bound-2] = result[upper_bound-2] * 3 / 2 if upper_bound > 1 and upper_bound == set.length
|
2013-07-18 23:05:38 -05:00
|
|
|
result[0] = "null"
|
|
|
|
result
|
2013-07-18 23:22:44 -05:00
|
|
|
end # unsolved, not triggered, edge case for set.length == upper_bound + 1
|
2011-11-28 23:38:57 +01:00
|
|
|
|
2012-01-20 23:50:27 +01:00
|
|
|
end
|