2019-12-18 09:49:57 -06:00
|
|
|
# typed: false
|
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
|
2018-09-22 12:55:27 -05:00
|
|
|
append_before_action :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
|
|
|
|
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
|
|
|
|
|
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
|
2019-05-19 16:40:00 +03:00
|
|
|
|
|
|
|
# assumes date1 > date2
|
|
|
|
def difference_in_days(date1, date2)
|
|
|
|
return ((date1.utc.at_midnight-date2.utc.at_midnight)/SECONDS_PER_DAY).to_i
|
|
|
|
end
|
|
|
|
|
|
|
|
# assumes date1 > date2
|
|
|
|
def difference_in_weeks(date1, date2)
|
|
|
|
return difference_in_days(date1, date2) / 7
|
|
|
|
end
|
2012-01-20 23:50:27 +01:00
|
|
|
end
|