Class: StatsController

Inherits:
ApplicationController show all
Defined in:
app/controllers/stats_controller.rb

Constant Summary collapse

SECONDS_PER_DAY =
86_400

Instance Method Summary collapse

Methods inherited from ApplicationController

#admin_login_required, #admin_or_self_login_required, #all_done_todos_for, #boolean_param, cas_enabled?, #cas_enabled?, #count_deferred_todos, #count_undone_todos, #count_undone_todos_phrase, #done_todos_for, #enable_mobile_content_negotiation, #for_autocomplete, #format_date, #format_dependencies_as_json_for_auto_complete, #handle_unverified_request, #init_data_for_sidebar, #init_hidden_todo_counts, #init_not_done_counts, #mobile?, #notify, #openid_enabled?, openid_enabled?, #parse_date_per_user_prefs, prefered_auth?, #prefered_auth?, #redirect_back_or_home, #render_failure, #sanitize, #set_group_view_by, #set_locale, #set_session_expiration, #set_time_zone, #set_zindex_counter, #todo_xml_params

Methods included from Common

like_operator, #set_theme

Methods included from LoginSystem

#access_denied, #authorize?, #basic_auth_denied, #current_user, #get_basic_auth_data, #get_current_user, #logged_in?, #login_from_cookie, #login_optional, #login_or_feed_token_required, #login_required, #logout_user, #prefs, #protect?, #redirect_back_or_default, #redirect_to_login, #set_current_user, #store_location

Instance Method Details

#actions_done_last_yearsObject



13
14
15
16
# File 'app/controllers/stats_controller.rb', line 13

def actions_done_last_years
  @page_title = t('stats.index_title')
  @chart = Stats::Chart.new('actions_done_lastyears_data', :height => 400, :width => 900)
end

#actions_done_lastyears_dataObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'app/controllers/stats_controller.rb', line 18

def actions_done_lastyears_data
  actions_last_months = current_user.todos.select("completed_at,created_at")

  month_count = difference_in_months(@today, actions_last_months.minimum(:created_at))
  # because this action is not scoped by date, the minimum created_at should always be
  # less than the minimum completed_at, so no reason to check minimum completed_at

  # convert to array and fill in non-existing months
  @actions_done_last_months_array = put_events_into_month_buckets(actions_last_months, month_count + 1, :completed_at)
  @actions_created_last_months_array = put_events_into_month_buckets(actions_last_months, month_count + 1, :created_at)

  # find max for graph in both hashes
  @max = (@actions_done_last_months_array + @actions_created_last_months_array).max

  # set running avg
  @actions_done_avg_last_months_array = compute_running_avg_array(@actions_done_last_months_array, month_count + 1)
  @actions_created_avg_last_months_array = compute_running_avg_array(@actions_created_last_months_array, month_count + 1)

  # interpolate avg for this month.
  @interpolated_actions_created_this_month = interpolate_avg_for_current_month(@actions_created_last_months_array)
  @interpolated_actions_done_this_month = interpolate_avg_for_current_month(@actions_done_last_months_array)

  @created_count_array = Array.new(month_count + 1, actions_last_months.select { |x| x.created_at }.size / month_count)
  @done_count_array    = Array.new(month_count + 1, actions_last_months.select { |x| x.completed_at }.size / month_count)

  render :layout => false
end

#convert_to_array(records, upper_bound) ⇒ Object (private)

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



155
156
157
158
159
# File 'app/controllers/stats_controller.rb', line 155

def convert_to_array(records, upper_bound)
  a = Array.new(upper_bound, 0)
  records.each { |r| (yield r).each { |i| a[i] += 1 if a[i] } }
  a
end

#difference_in_days(date1, date2) ⇒ Object (private)

assumes date1 > date2



166
167
168
# File 'app/controllers/stats_controller.rb', line 166

def difference_in_days(date1, date2)
  return ((date1.utc.at_midnight - date2.utc.at_midnight) / SECONDS_PER_DAY).to_i
end

#difference_in_weeks(date1, date2) ⇒ Object (private)

assumes date1 > date2



171
172
173
# File 'app/controllers/stats_controller.rb', line 171

def difference_in_weeks(date1, date2)
  return difference_in_days(date1, date2) / 7
end

#doneObject



111
112
113
114
115
116
117
118
119
120
121
# File 'app/controllers/stats_controller.rb', line 111

def done
  @source_view = 'done'

  init_not_done_counts

  @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)
  @last_completed_contexts = []
  @last_completed_recurring_todos = current_user.recurring_todos.completed.limit(10).reorder('completed_at DESC').includes(:tags, :taggings)
  # TODO: @last_completed_contexts = current_user.contexts.completed.all(:limit => 10, :order => 'completed_at DESC')
end

#get_ids_from(actions, week_from, week_to, at_end) ⇒ Object (private)



138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'app/controllers/stats_controller.rb', line 138

def get_ids_from(actions, week_from, week_to, at_end)
  selected_todo_ids = []

  actions.each do |r|
    weeks = difference_in_weeks(@today, r.created_at)
    if at_end
      selected_todo_ids << r.id.to_s if weeks >= week_from
    else
      selected_todo_ids << r.id.to_s if weeks.between?(week_from, week_to - 1)
    end
  end

  return selected_todo_ids
end

#indexObject



7
8
9
10
11
# File 'app/controllers/stats_controller.rb', line 7

def index
  @page_title = t('stats.index_title')
  @hidden_contexts = current_user.contexts.hidden
  @stats = Stats::UserStats.new(current_user)
end

#initObject (private)



125
126
127
128
129
130
131
132
133
134
135
136
# File 'app/controllers/stats_controller.rb', line 125

def init
  @me = self # for meta programming

  # get the current date wih time set to 0:0
  @today = Time.zone.now.utc.beginning_of_day

  # define cut_off date and discard the time for a month, 3 months and a year
  @cut_off_year = 12.months.ago.beginning_of_day
  @cut_off_year_plus3 = 15.months.ago.beginning_of_day
  @cut_off_month = 1.month.ago.beginning_of_day
  @cut_off_30days = 30.days.ago.beginning_of_day
end

#put_events_into_month_buckets(records, array_size, date_method_on_todo) ⇒ Object (private)



161
162
163
# File 'app/controllers/stats_controller.rb', line 161

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

#show_selected_actions_from_chartObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'app/controllers/stats_controller.rb', line 46

def show_selected_actions_from_chart
  @page_title = t('stats.action_selection_title')
  @count = 99

  @source_view = 'stats'

  case params['id']
  when 'avrt', 'avrt_end' # actions_visible_running_time
    # 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

    week_from = params['index'].to_i
    week_to = week_from + 1

    @chart = Stats::Chart.new('actions_visible_running_time_data')
    @page_title = t('stats.actions_selected_from_week')
    @further = false
    if params['id'] == 'avrt_end'
      @page_title += week_from.to_s + t('stats.actions_further')
      @further = true
    else
      @page_title += week_from.to_s + " - " + week_to.to_s + ""
    end

    # get all running actions that are visible
    @actions_running_time = current_user.todos.not_completed.not_hidden.not_deferred_or_blocked
      .select("todos.id, todos.created_at")
      .reorder("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.where("id in (" + selected_todo_ids.join(",") + ")")
    @count = @selected_actions.size

    render :action => "show_selection_from_chart"

  when 'art', 'art_end'
    week_from = params['index'].to_i
    week_to = week_from + 1

    @chart = Stats::Chart.new('actions_running_time_data')
    @page_title = "Actions selected from week "
    @further = false
    if params['id'] == 'art_end'
      @page_title += week_from.to_s + " and further"
      @further = true
    else
      @page_title += week_from.to_s + " - " + week_to.to_s + ""
    end

    # get all running actions
    @actions_running_time = current_user.todos.not_completed.select("id, created_at")

    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.where("id in (#{selected_todo_ids.join(",")})")
    @count = @selected_actions.size

    render :action => "show_selection_from_chart"
  else
    # render error
    render_failure "404 NOT FOUND. Unknown query selected"
  end
end