0
@@ -105,7 +105,107 @@ class StatsController < ApplicationController
0
render :layout => false
0
+ def actions_done_last_years
0
+ def actions_done_lastyears_data
0
+ @actions = @user.todos
0
+ # get actions created and completed in the past 12+3 months. +3 for running
0
+ @actions_done_last_months = @actions.find(:all, {
0
+ :select => "completed_at",
0
+ :conditions => ["completed_at IS NOT NULL"]
0
+ @actions_created_last_months = @actions.find(:all, {
0
+ :select => "created_at",
0
+ # convert to hash to be able to fill in non-existing days in
0
+ # @actions_done_last12months and count the total actions done in the past
0
+ # 12 months to be able to calculate percentage
0
+ # use 0 to initialise action count to zero
0
+ @actions_done_last_months_hash = Hash.new(0)
0
+ @actions_done_last_months.each do |r|
0
+ months = (@today.year - r.completed_at.year)*12 + (@today.month - r.completed_at.month)
0
+ @month_count = months if months > @month_count
0
+ @actions_done_last_months_hash[months] += 1
0
+ # convert to hash to be able to fill in non-existing days in
0
+ # @actions_created_last12months and count the total actions done in the
0
+ # past 12 months to be able to calculate percentage
0
+ # use 0 to initialise action count to zero
0
+ @actions_created_last_months_hash = Hash.new(0)
0
+ @actions_created_last_months.each do |r|
0
+ months = (@today.year - r.created_at.year)*12 + (@today.month - r.created_at.month)
0
+ @month_count = months if months > @month_count
0
+ @actions_created_last_months_hash[months] += 1
0
+ @sum_actions_done_last_months=0
0
+ @sum_actions_created_last_months=0
0
+ # find max for graph in both hashes
0
+ 0.upto @month_count do |i|
0
+ @sum_actions_done_last_months += @actions_done_last_months_hash[i]
0
+ @max = @actions_done_last_months_hash[i] if @actions_done_last_months_hash[i] > @max
0
+ 0.upto @month_count do |i|
0
+ @sum_actions_created_last_months += @actions_created_last_months_hash[i]
0
+ @max = @actions_created_last_months_hash[i] if @actions_created_last_months_hash[i] > @max
0
+ # find running avg for month i by calculating avg of month i and the two
0
+ # after them. Ignore current month because you do not have full data for
0
+ @actions_done_avg_last_months_hash = Hash.new("null")
0
+ 1.upto(@month_count) { |i|
0
+ @actions_done_avg_last_months_hash[i] = (@actions_done_last_months_hash[i] +
0
+ @actions_done_last_months_hash[i+1] +
0
+ @actions_done_last_months_hash[i+2])/3.0
0
+ # correct last two months
0
+ @actions_done_avg_last_months_hash[@month_count] = @actions_done_avg_last_months_hash[@month_count] * 3
0
+ @actions_done_avg_last_months_hash[@month_count-1] = @actions_done_avg_last_months_hash[@month_count-1] * 3 / 2 if @month_count > 1
0
+ # find running avg for month i by calculating avg of month i and the two
0
+ # after them. Ignore current month because you do not have full data for
0
+ @actions_created_avg_last_months_hash = Hash.new("null")
0
+ 1.upto(@month_count) { |i|
0
+ @actions_created_avg_last_months_hash[i] = (@actions_created_last_months_hash[i] +
0
+ @actions_created_last_months_hash[i+1] +
0
+ @actions_created_last_months_hash[i+2])/3.0
0
+ # correct last two months
0
+ @actions_created_avg_last_months_hash[@month_count] = @actions_created_avg_last_months_hash[@month_count] * 3
0
+ @actions_created_avg_last_months_hash[@month_count-1] = @actions_created_avg_last_months_hash[@month_count-1] * 3 / 2 if @month_count > 1
0
+ # interpolate avg for this month. Assume 31 days in this month
0
+ days_passed_this_month = Time.new.day/1.0
0
+ @interpolated_actions_created_this_month = (
0
+ @actions_created_last_months_hash[0]/days_passed_this_month*31.0+
0
+ @actions_created_last_months_hash[1]+
0
+ @actions_created_last_months_hash[2]) / 3.0
0
+ @interpolated_actions_done_this_month = (
0
+ @actions_done_last_months_hash[0]/days_passed_this_month*31.0 +
0
+ @actions_done_last_months_hash[1]+
0
+ @actions_done_last_months_hash[2]) / 3.0
0
+ render :layout => false
0
def actions_done_last30days_data
0
# get actions created and completed in the past 30 days.
0
@actions_done_last30days = @actions.find(:all, {