mirror of
https://github.com/TracksApp/tracks.git
synced 2026-03-04 20:10:16 +01:00
Encapsulate action stats
This gets rid of a bunch of instance variables in the view/controller layer.
This commit is contained in:
parent
6c3f447d8e
commit
0ebb98d49f
4 changed files with 130 additions and 65 deletions
|
|
@ -13,8 +13,8 @@ class StatsController < ApplicationController
|
||||||
@tags_count = tag_ids.size
|
@tags_count = tag_ids.size
|
||||||
@unique_tags_count = tag_ids.uniq.size
|
@unique_tags_count = tag_ids.uniq.size
|
||||||
@hidden_contexts = current_user.contexts.hidden
|
@hidden_contexts = current_user.contexts.hidden
|
||||||
|
@actions = Stats::Actions.new(current_user)
|
||||||
|
|
||||||
get_stats_actions
|
|
||||||
get_stats_contexts
|
get_stats_contexts
|
||||||
get_stats_projects
|
get_stats_projects
|
||||||
get_stats_tags
|
get_stats_tags
|
||||||
|
|
@ -387,60 +387,6 @@ class StatsController < ApplicationController
|
||||||
@cut_off_3months = 3.months.ago.beginning_of_day
|
@cut_off_3months = 3.months.ago.beginning_of_day
|
||||||
end
|
end
|
||||||
|
|
||||||
def get_stats_actions
|
|
||||||
# time to complete
|
|
||||||
@completed_actions = current_user.todos.completed.select("completed_at, created_at")
|
|
||||||
|
|
||||||
actions_sum, actions_max = 0,0
|
|
||||||
actions_min = @completed_actions.first ? @completed_actions.first.completed_at - @completed_actions.first.created_at : 0
|
|
||||||
|
|
||||||
@completed_actions.each do |r|
|
|
||||||
actions_sum += (r.completed_at - r.created_at)
|
|
||||||
actions_max = [(r.completed_at - r.created_at), actions_max].max
|
|
||||||
actions_min = [(r.completed_at - r.created_at), actions_min].min
|
|
||||||
end
|
|
||||||
|
|
||||||
sum_actions = @completed_actions.size
|
|
||||||
sum_actions = 1 if sum_actions==0 # to prevent dividing by zero
|
|
||||||
|
|
||||||
@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
|
|
||||||
|
|
||||||
min_ttc_sec = Time.utc(2000,1,1,0,0)+actions_min # convert to a datetime
|
|
||||||
@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
|
|
||||||
|
|
||||||
# get count of actions created and actions done in the past 30 days.
|
|
||||||
@sum_actions_done_last30days = current_user.todos.completed.completed_after(@cut_off_month).count
|
|
||||||
@sum_actions_created_last30days = current_user.todos.created_after(@cut_off_month).count
|
|
||||||
|
|
||||||
# get count of actions done in the past 12 months.
|
|
||||||
@sum_actions_done_last12months = current_user.todos.completed.completed_after(@cut_off_year).count
|
|
||||||
@sum_actions_created_last12months = current_user.todos.created_after(@cut_off_year).count
|
|
||||||
|
|
||||||
@completion_charts = %w{
|
|
||||||
actions_done_last30days_data
|
|
||||||
actions_done_last12months_data
|
|
||||||
actions_completion_time_data
|
|
||||||
}.map do |action|
|
|
||||||
Stats::Chart.new(action)
|
|
||||||
end
|
|
||||||
|
|
||||||
@timing_charts = %w{
|
|
||||||
actions_visible_running_time_data
|
|
||||||
actions_running_time_data
|
|
||||||
actions_open_per_week_data
|
|
||||||
actions_day_of_week_all_data
|
|
||||||
actions_day_of_week_30days_data
|
|
||||||
actions_time_of_day_all_data
|
|
||||||
actions_time_of_day_30days_data
|
|
||||||
}.map do |action|
|
|
||||||
Stats::Chart.new(action)
|
|
||||||
end
|
|
||||||
|
|
||||||
end
|
|
||||||
|
|
||||||
def get_stats_contexts
|
def get_stats_contexts
|
||||||
@actions_per_context = Stats::TopContextsQuery.new(current_user, :limit => 5).result
|
@actions_per_context = Stats::TopContextsQuery.new(current_user, :limit => 5).result
|
||||||
@running_actions_per_context = Stats::TopContextsQuery.new(current_user, :limit => 5, :running => true).result
|
@running_actions_per_context = Stats::TopContextsQuery.new(current_user, :limit => 5, :running => true).result
|
||||||
|
|
|
||||||
119
app/models/stats/actions.rb
Normal file
119
app/models/stats/actions.rb
Normal file
|
|
@ -0,0 +1,119 @@
|
||||||
|
module Stats
|
||||||
|
class Actions
|
||||||
|
|
||||||
|
SECONDS_PER_DAY = 86400;
|
||||||
|
|
||||||
|
attr_reader :user
|
||||||
|
def initialize(user)
|
||||||
|
@user = user
|
||||||
|
end
|
||||||
|
|
||||||
|
def avg_ttc
|
||||||
|
@avg_ttc ||= (sum/count)/SECONDS_PER_DAY
|
||||||
|
end
|
||||||
|
|
||||||
|
def max_ttc
|
||||||
|
@max_ttc ||= max/SECONDS_PER_DAY
|
||||||
|
end
|
||||||
|
|
||||||
|
def min_ttc
|
||||||
|
@min_ttc ||= min/SECONDS_PER_DAY
|
||||||
|
end
|
||||||
|
|
||||||
|
def min_ttc_sec
|
||||||
|
min_ttc_sec = arbitrary_day + min # convert to a datetime
|
||||||
|
@actions_min_ttc_sec = (min_ttc_sec).strftime("%H:%M:%S")
|
||||||
|
@actions_min_ttc_sec = (min / SECONDS_PER_DAY).round.to_s + " days " + @actions_min_ttc_sec if min > SECONDS_PER_DAY
|
||||||
|
@actions_min_ttc_sec
|
||||||
|
end
|
||||||
|
|
||||||
|
def done_last30days
|
||||||
|
@done_last30days ||= done_since(one_month)
|
||||||
|
end
|
||||||
|
|
||||||
|
def done_last12months
|
||||||
|
@done_last12months ||= done_since(one_year)
|
||||||
|
end
|
||||||
|
|
||||||
|
def created_last30days
|
||||||
|
@sum_actions_created_last30days ||= new_since(one_month)
|
||||||
|
end
|
||||||
|
|
||||||
|
def created_last12months
|
||||||
|
@sum_actions_created_last12months ||= new_since(one_year)
|
||||||
|
end
|
||||||
|
|
||||||
|
def completion_charts
|
||||||
|
@completion_charts ||= %w{
|
||||||
|
actions_done_last30days_data
|
||||||
|
actions_done_last12months_data
|
||||||
|
actions_completion_time_data
|
||||||
|
}.map do |action|
|
||||||
|
Stats::Chart.new(action)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def timing_charts
|
||||||
|
@timing_charts ||= %w{
|
||||||
|
actions_visible_running_time_data
|
||||||
|
actions_running_time_data
|
||||||
|
actions_open_per_week_data
|
||||||
|
actions_day_of_week_all_data
|
||||||
|
actions_day_of_week_30days_data
|
||||||
|
actions_time_of_day_all_data
|
||||||
|
actions_time_of_day_30days_data
|
||||||
|
}.map do |action|
|
||||||
|
Stats::Chart.new(action)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def arbitrary_day
|
||||||
|
@arbitrary_day ||= Time.utc(2000,1,1,0,0)
|
||||||
|
end
|
||||||
|
|
||||||
|
def one_year
|
||||||
|
@one_year ||= 12.months.ago.beginning_of_day
|
||||||
|
end
|
||||||
|
|
||||||
|
def one_month
|
||||||
|
@one_month ||= 1.month.ago.beginning_of_day
|
||||||
|
end
|
||||||
|
|
||||||
|
def completed
|
||||||
|
@completed ||= user.todos.completed.select("completed_at, created_at")
|
||||||
|
end
|
||||||
|
|
||||||
|
def durations
|
||||||
|
@durations ||= completed.map do |r|
|
||||||
|
(r.completed_at - r.created_at)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def sum
|
||||||
|
@sum ||= durations.inject(0) {|sum, d| sum + d}
|
||||||
|
end
|
||||||
|
|
||||||
|
def min
|
||||||
|
@min ||= durations.min || 0
|
||||||
|
end
|
||||||
|
|
||||||
|
def max
|
||||||
|
@max ||= durations.max || 0
|
||||||
|
end
|
||||||
|
|
||||||
|
def count
|
||||||
|
completed.empty? ? 1 : completed.size
|
||||||
|
end
|
||||||
|
|
||||||
|
def new_since(cutoff)
|
||||||
|
user.todos.created_after(cutoff).count
|
||||||
|
end
|
||||||
|
|
||||||
|
def done_since(cutoff)
|
||||||
|
user.todos.completed.completed_after(cutoff).count
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
@ -1,19 +1,19 @@
|
||||||
<p><%= t('stats.actions_avg_completion_time', :count => (@actions_avg_ttc*10).round/10.0) %>
|
<p><%= t('stats.actions_avg_completion_time', :count => (actions.avg_ttc*10).round/10.0) %>
|
||||||
<%= t('stats.actions_min_max_completion_days', :max => (@actions_max_ttc*10).round/10.0, :min => (@actions_min_ttc*10).round/10.0) %>
|
<%= t('stats.actions_min_max_completion_days', :max => (actions.max_ttc*10).round/10.0, :min => (actions.min_ttc*10).round/10.0) %>
|
||||||
<%= t('stats.actions_min_completion_time', :time => @actions_min_ttc_sec) %></p>
|
<%= t('stats.actions_min_completion_time', :time => actions.min_ttc_sec) %></p>
|
||||||
|
|
||||||
<p><%= t('stats.actions_actions_avg_created_30days', :count => (@sum_actions_created_last30days*10.0/30.0).round/10.0 )%>
|
<p><%= t('stats.actions_actions_avg_created_30days', :count => (actions.created_last30days*10.0/30.0).round/10.0 )%>
|
||||||
<%= t('stats.actions_avg_completed_30days', :count => (@sum_actions_done_last30days*10.0/30.0).round/10.0 )%>
|
<%= t('stats.actions_avg_completed_30days', :count => (actions.done_last30days*10.0/30.0).round/10.0 )%>
|
||||||
<%= t('stats.actions_avg_created', :count => (@sum_actions_created_last12months*10.0/12.0).round/10.0 )%>
|
<%= t('stats.actions_avg_created', :count => (actions.created_last12months*10.0/12.0).round/10.0 )%>
|
||||||
<%= t('stats.actions_avg_completed', :count => (@sum_actions_done_last12months*10.0/12.0).round/10.0 )%></p>
|
<%= t('stats.actions_avg_completed', :count => (actions.done_last12months*10.0/12.0).round/10.0 )%></p>
|
||||||
|
|
||||||
<% @completion_charts.each do |chart| %><%=
|
<% actions.completion_charts.each do |chart| %><%=
|
||||||
render :partial => 'chart', :locals => {:chart => chart}
|
render :partial => 'chart', :locals => {:chart => chart}
|
||||||
-%><% end %>
|
-%><% end %>
|
||||||
|
|
||||||
<br style="clear:both">
|
<br style="clear:both">
|
||||||
|
|
||||||
<% @timing_charts.each do |chart| %><%=
|
<% actions.timing_charts.each do |chart| %><%=
|
||||||
render :partial => 'chart', :locals => {:chart => chart}
|
render :partial => 'chart', :locals => {:chart => chart}
|
||||||
-%><% end %>
|
-%><% end %>
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
<% unless current_user.todos.empty? -%>
|
<% unless current_user.todos.empty? -%>
|
||||||
|
|
||||||
<h2><%= t('stats.actions') %></h2>
|
<h2><%= t('stats.actions') %></h2>
|
||||||
<%= render :partial => 'actions' -%>
|
<%= render :partial => 'actions', :locals => {:actions => @actions} -%>
|
||||||
|
|
||||||
<h2><%= t('stats.contexts') %></h2>
|
<h2><%= t('stats.contexts') %></h2>
|
||||||
<%= render :partial => 'contexts' -%>
|
<%= render :partial => 'contexts' -%>
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue