From 44f8646881beae085dca02def679a975a1be9379 Mon Sep 17 00:00:00 2001 From: Reinier Balt Date: Fri, 4 Jul 2008 17:56:59 +0200 Subject: [PATCH] adds chart to see all actions in all past months This one is a bit hidden. You need to click on a bar in the chart with the actions from the last 12 months. Need to change this so people can find it easier --- app/controllers/stats_controller.rb | 100 ++++++++++++++++++ .../actions_done_last12months_data.html.erb | 2 + .../stats/actions_done_last_years.html.erb | 2 + .../actions_done_lastyears_data.html.erb | 29 +++++ app/views/stats/index.html.erb | 56 +++++----- 5 files changed, 161 insertions(+), 28 deletions(-) create mode 100644 app/views/stats/actions_done_last_years.html.erb create mode 100644 app/views/stats/actions_done_lastyears_data.html.erb diff --git a/app/controllers/stats_controller.rb b/app/controllers/stats_controller.rb index 7caf0cd9..c74811d4 100755 --- a/app/controllers/stats_controller.rb +++ b/app/controllers/stats_controller.rb @@ -105,7 +105,107 @@ class StatsController < ApplicationController render :layout => false end + + def actions_done_last_years + @chart_width = 900 + @chart_height = 400 + end + + def actions_done_lastyears_data + @actions = @user.todos + + # get actions created and completed in the past 12+3 months. +3 for running + # average + @actions_done_last_months = @actions.find(:all, { + :select => "completed_at", + :conditions => ["completed_at IS NOT NULL"] + }) + @actions_created_last_months = @actions.find(:all, { + :select => "created_at", + }) + + @month_count = 0 + + # convert to hash to be able to fill in non-existing days in + # @actions_done_last12months and count the total actions done in the past + # 12 months to be able to calculate percentage + + # use 0 to initialise action count to zero + @actions_done_last_months_hash = Hash.new(0) + @actions_done_last_months.each do |r| + months = (@today.year - r.completed_at.year)*12 + (@today.month - r.completed_at.month) + @month_count = months if months > @month_count + @actions_done_last_months_hash[months] += 1 + end + + # convert to hash to be able to fill in non-existing days in + # @actions_created_last12months and count the total actions done in the + # past 12 months to be able to calculate percentage + # use 0 to initialise action count to zero + @actions_created_last_months_hash = Hash.new(0) + @actions_created_last_months.each do |r| + months = (@today.year - r.created_at.year)*12 + (@today.month - r.created_at.month) + @month_count = months if months > @month_count + @actions_created_last_months_hash[months] += 1 + end + + @sum_actions_done_last_months=0 + @sum_actions_created_last_months=0 + + # find max for graph in both hashes + @max=0 + 0.upto @month_count do |i| + @sum_actions_done_last_months += @actions_done_last_months_hash[i] + @max = @actions_done_last_months_hash[i] if @actions_done_last_months_hash[i] > @max + end + 0.upto @month_count do |i| + @sum_actions_created_last_months += @actions_created_last_months_hash[i] + @max = @actions_created_last_months_hash[i] if @actions_created_last_months_hash[i] > @max + end + + # find running avg for month i by calculating avg of month i and the two + # after them. Ignore current month because you do not have full data for + # it + @actions_done_avg_last_months_hash = Hash.new("null") + 1.upto(@month_count) { |i| + @actions_done_avg_last_months_hash[i] = (@actions_done_last_months_hash[i] + + @actions_done_last_months_hash[i+1] + + @actions_done_last_months_hash[i+2])/3.0 + } + # correct last two months + @actions_done_avg_last_months_hash[@month_count] = @actions_done_avg_last_months_hash[@month_count] * 3 + @actions_done_avg_last_months_hash[@month_count-1] = @actions_done_avg_last_months_hash[@month_count-1] * 3 / 2 if @month_count > 1 + + # find running avg for month i by calculating avg of month i and the two + # after them. Ignore current month because you do not have full data for + # it + @actions_created_avg_last_months_hash = Hash.new("null") + 1.upto(@month_count) { |i| + @actions_created_avg_last_months_hash[i] = (@actions_created_last_months_hash[i] + + @actions_created_last_months_hash[i+1] + + @actions_created_last_months_hash[i+2])/3.0 + } + # correct last two months + @actions_created_avg_last_months_hash[@month_count] = @actions_created_avg_last_months_hash[@month_count] * 3 + @actions_created_avg_last_months_hash[@month_count-1] = @actions_created_avg_last_months_hash[@month_count-1] * 3 / 2 if @month_count > 1 + + # interpolate avg for this month. Assume 31 days in this month + days_passed_this_month = Time.new.day/1.0 + @interpolated_actions_created_this_month = ( + @actions_created_last_months_hash[0]/days_passed_this_month*31.0+ + @actions_created_last_months_hash[1]+ + @actions_created_last_months_hash[2]) / 3.0 + + @interpolated_actions_done_this_month = ( + @actions_done_last_months_hash[0]/days_passed_this_month*31.0 + + @actions_done_last_months_hash[1]+ + @actions_done_last_months_hash[2]) / 3.0 + + render :layout => false + end + + def actions_done_last30days_data # get actions created and completed in the past 30 days. @actions_done_last30days = @actions.find(:all, { diff --git a/app/views/stats/actions_done_last12months_data.html.erb b/app/views/stats/actions_done_last12months_data.html.erb index df76645a..d038ac48 100755 --- a/app/views/stats/actions_done_last12months_data.html.erb +++ b/app/views/stats/actions_done_last12months_data.html.erb @@ -11,6 +11,8 @@ &line_7=1,0xAA0000& &line_8=1,0x007700& &values=<% 0.upto 11 do |i| -%><%= @actions_created_last12months_hash[i]%>,<% end -%><%= @actions_created_last12months_hash[12]%>& +&links=<% 0.upto 11 do |i| -%><%= url_for :controller => 'stats', :action => 'actions_done_last_years' %>,<% end -%><%= url_for :controller => 'stats', :action => 'actions_done_last_years' %>& +&links_2=<% 0.upto 11 do |i| -%><%= url_for :controller => 'stats', :action => 'actions_done_last_years' %>,<% end -%><%= url_for :controller => 'stats', :action => 'actions_done_last_years' %>& &values_2=<% 0.upto 11 do |i| -%><%= @actions_done_last12months_hash[i]%>,<% end -%><%= @actions_done_last12months_hash[12]%>& &values_3=<%0.upto 11 do |i| -%><%=@sum_actions_created_last12months/12-%>,<%end-%><%=@sum_actions_created_last12months/12-%>& &values_4=<%0.upto 11 do |i| -%><%=@sum_actions_done_last12months/12-%>,<%end-%><%=@sum_actions_done_last12months/12-%>& diff --git a/app/views/stats/actions_done_last_years.html.erb b/app/views/stats/actions_done_last_years.html.erb new file mode 100644 index 00000000..6579d580 --- /dev/null +++ b/app/views/stats/actions_done_last_years.html.erb @@ -0,0 +1,2 @@ +<%= render :partial => 'chart', :locals => {:width => @chart_width, :height => @chart_height, :data => url_for(:action => :actions_done_lastyears_data)} -%> +Click <%=link_to "here", {:controller => "stats", :action => "index"} %> to return to the statistics page. diff --git a/app/views/stats/actions_done_lastyears_data.html.erb b/app/views/stats/actions_done_lastyears_data.html.erb new file mode 100644 index 00000000..2619d56a --- /dev/null +++ b/app/views/stats/actions_done_lastyears_data.html.erb @@ -0,0 +1,29 @@ +&title=Actions in the last years,{font-size:16},& +&y_legend=Number of actions,12,0x736AFF& +&x_legend=Months ago,12,0x736AFF& +&y_ticks=5,10,5& +&filled_bar=50,0x9933CC,0x8010A0,Created,9& +&filled_bar_2=50,0x0066CC,0x0066CC,Completed,9& +&line_3=2,0x00FF00, Avg Created, 9& +&line_4=2,0xFF0000, Avg Completed, 9& +&line_5=2,0x007700, 3 Month Avg Created, 9& +&line_6=2,0xAA0000, 3 Month Avg Completed, 9& +&line_7=1,0xAA0000& +&line_8=1,0x007700& +&values=<% 0.upto @month_count-1 do |i| -%><%= @actions_created_last_months_hash[i]%>,<% end -%><%= @actions_created_last_months_hash[@month_count]%>& +&values_2=<% 0.upto @month_count-1 do |i| -%><%= @actions_done_last_months_hash[i]%>,<% end -%><%= @actions_done_last_months_hash[@month_count]%>& +&values_3=<%0.upto @month_count-1 do |i| -%><%=@sum_actions_created_last_months/@month_count-%>,<%end-%><%=@sum_actions_created_last_months/@month_count-%>& +&values_4=<%0.upto @month_count-1 do |i| -%><%=@sum_actions_done_last_months/@month_count-%>,<%end-%><%=@sum_actions_done_last_months/@month_count-%>& +&values_5=<%0.upto @month_count-1 do |i| -%><%=@actions_created_avg_last_months_hash[i]-%>,<%end-%><%=@actions_created_avg_last_months_hash[@month_count]-%>& +&values_6=<%0.upto @month_count-1 do |i| -%><%=@actions_done_avg_last_months_hash[i]-%>,<%end-%><%=@actions_done_avg_last_months_hash[@month_count]-%>& +&values_7=<%=@interpolated_actions_created_this_month%>,<%=@actions_done_avg_last_months_hash[1]%>& +&values_8=<%=@interpolated_actions_done_this_month%>,<%=@actions_created_avg_last_months_hash[1]%>& +&x_labels=<%0.upto @month_count-1 do |i| -%> +<%= Date::MONTHNAMES[ (Time.now.mon - i -1 ) % 12 + 1 ] + " " + (Time.now - i.months).year.to_s -%>, +<% end -%> +<%= Date::MONTHNAMES[(Time.now.mon - @month_count -1 ) % 12 + 1] + " " + (Time.now - @month_count.months).year.to_s -%>& +&y_min=0& +<% # add one to @max for people who have no actions completed yet. +# OpenFlashChart cannot handle y_max=0 -%> +&y_max=<%=@max+@max/10+1-%>& +&x_label_style=9,,2,& \ No newline at end of file diff --git a/app/views/stats/index.html.erb b/app/views/stats/index.html.erb index ef76c9cf..7e3e9aa8 100755 --- a/app/views/stats/index.html.erb +++ b/app/views/stats/index.html.erb @@ -1,30 +1,30 @@
-

Totals

- -<%= render :partial => 'totals' -%> - -<% unless @actions.empty? -%> - -

Actions

- -<%= render :partial => 'actions' -%> - -

Contexts

- -<%= render :partial => 'contexts' -%> - -

Projects

- -<%= render :partial => 'projects' -%> - -

Tags

- -<%= render :partial => 'tags' -%> - -<% else -%> - -

More statistics will appear here once you have added some actions.

- -<% end -%> - +

Totals

+ + <%= render :partial => 'totals' -%> + + <% unless @actions.empty? -%> + +

Actions

+ + <%= render :partial => 'actions' -%> + +

Contexts

+ + <%= render :partial => 'contexts' -%> + +

Projects

+ + <%= render :partial => 'projects' -%> + +

Tags

+ + <%= render :partial => 'tags' -%> + + <% else -%> + +

More statistics will appear here once you have added some actions.

+ + <% end -%> +
\ No newline at end of file