diff --git a/app/controllers/admins_controller.rb b/app/controllers/admins_controller.rb index 9fe6d24beeeb78aa46bddc5673a49766d0a7b41f..4a7cb8f54a5654a481cbf63106b83c581d8a99ae 100644 --- a/app/controllers/admins_controller.rb +++ b/app/controllers/admins_controller.rb @@ -66,18 +66,7 @@ class AdminsController < ApplicationController end def correlations - - - @correlations = Statistics.generate_correlations(params[:number_of_weeks]) - - 5.times.inject({}) do |stats, n| - week_start = (Time.now - n.weeks).beginning_of_week - week_end = week_start - 1.week - stats[week_start] = Statistics.new(week_start, week_end).generate_correlations - end - - - + @post_count_correlation = Statistics.new.post_count_correlation end private diff --git a/app/views/admins/_admin_bar.haml b/app/views/admins/_admin_bar.haml index 5f261d9e0b792f03d1045e4b4805f07ccef55a7f..a366c187cc632e5acd280f35fe7adb0a1bc6efc8 100644 --- a/app/views/admins/_admin_bar.haml +++ b/app/views/admins/_admin_bar.haml @@ -5,6 +5,7 @@ %li= link_to 'User Search', user_search_path %li= link_to 'Weekly User Stats', weekly_user_stats_path %li= link_to 'Pod Stats', pod_stats_path + %li= link_to 'Correlations', correlations_path - if AppConfig[:mount_resque_web] %li= link_to 'Resque Overview', resque_web_path diff --git a/app/views/admins/correlations.haml b/app/views/admins/correlations.haml new file mode 100644 index 0000000000000000000000000000000000000000..ec0f0591740f41f6fce9267a7faf7f862d6be719 --- /dev/null +++ b/app/views/admins/correlations.haml @@ -0,0 +1,11 @@ + +.span-24 + = render :partial => 'admins/admin_bar.haml' +%br +%br + +.span-24.last + %h1 + = "Correlation between Post Count and Sign In Count" + %ul + = @post_count_correlation diff --git a/config/routes.rb b/config/routes.rb index fd90b25c7c42406e643ddc7b8cb43f169131511d..ed28dd963ad31188dac794e5d4c8d34248f4aeb2 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -97,6 +97,7 @@ Diaspora::Application.routes.draw do match :user_search get :admin_inviter get :weekly_user_stats + get :correlations get :stats, :as => 'pod_stats' end diff --git a/lib/statistics.rb b/lib/statistics.rb index 00c80086f6b39ad6907cb12f33c6cd41f23777a7..cd64f402f6c044f85ee642dd9b267c6274ad8299 100644 --- a/lib/statistics.rb +++ b/lib/statistics.rb @@ -55,15 +55,21 @@ SQL SQL end - def posts_count_correlation + def post_count_correlation # [{"id" => 1 , "count" => 123}] - post_count_array = User.connection.select_all(self.posts_count_sql) - - post_count_hash = {} - post_count_array.each{ |h| post_count_hash[h[id]] = h["count"]} + x_array = [] + y_array = [] + post_count_hash.keys.each do |k| + if val = sign_in_count_hash[k] + x_array << post_count_hash[k] + y_array << val + end + end + + correlation(x_array, y_array) end @@ -93,4 +99,23 @@ SQL User.where("username IS NOT NULL").where("created_at > ? and created_at < ?", Time.now - (n+1).weeks, Time.now - n.weeks) end + def post_count_hash + unless @post_count_hash + post_count_array = User.connection.select_all(self.posts_count_sql) + + @post_count_hash = {} + post_count_array.each{ |h| @post_count_hash[h['id']] = h["count"]} + end + @post_count_hash + end + + def sign_in_count_hash + unless @sign_in_count_hash + sign_in_count_array = User.connection.select_all(self.sign_in_count_sql) + + @sign_in_count_hash = {} + sign_in_count_array.each{ |h| @sign_in_count_hash[h['id']] = h["count"]} + end + @sign_in_count_hash + end end diff --git a/spec/lib/statistics_spec.rb b/spec/lib/statistics_spec.rb index 77a62bf9fdb7316605ee703554400bc293003ddf..6ed2f0f752899263c1ac66901fe522fded7fc36d 100644 --- a/spec/lib/statistics_spec.rb +++ b/spec/lib/statistics_spec.rb @@ -4,7 +4,8 @@ require 'lib/statistics' describe Statistics do before do - @stats = Statistics.new(time, time - 1.week) + @time = Time.now + @stats = Statistics.new#(@time, @time - 1.week) @result = [{"id" => alice.id , "count" => 0 }, {"id" => bob.id , "count" => 1 }, {"id" => eve.id , "count" => 0 }, @@ -59,11 +60,9 @@ describe Statistics do describe "#correlation_hash" do it 'it returns a hash of including start and end time' do - time = Time.now - hash = @stats.correlation_hash - hash[:starrt_time].should == time - hash[:end_time].should == time - 1.week + hash[:starrt_time].should == @time + hash[:end_time].should == @time - 1.week end it 'returns the post count (and sign_in_count) correlation' do @@ -72,6 +71,26 @@ describe Statistics do @stats.generate_correlations[:posts_count].should == 0.5 end end + + describe "#post_count_correlation" do + it 'calls correlation with post' do + User.connection.should_receive(:select_all).and_return([{"id"=> 1, "count" => 7}, + {"id" => 2, "count" => 8}, + {"id" => 3, "count" => 9}], + [{"id"=> 1, "count" => 17}, + {"id" => 3, "count" => 19}] + ) + + @stats.should_receive(:correlation).with([7,9],[17,19]).and_return(0.5) + @stats.posts_count_correlation.should == 0.5 + end + end + + + + + + context 'todos' do before do