This commit is contained in:
Ilya Zhitomirskiy 2011-10-10 16:30:00 -07:00
parent 818167c565
commit 4c9b45b9c8
6 changed files with 68 additions and 22 deletions

View file

@ -66,18 +66,7 @@ class AdminsController < ApplicationController
end end
def correlations def correlations
@post_count_correlation = Statistics.new.post_count_correlation
@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
end end
private private

View file

@ -5,6 +5,7 @@
%li= link_to 'User Search', user_search_path %li= link_to 'User Search', user_search_path
%li= link_to 'Weekly User Stats', weekly_user_stats_path %li= link_to 'Weekly User Stats', weekly_user_stats_path
%li= link_to 'Pod Stats', pod_stats_path %li= link_to 'Pod Stats', pod_stats_path
%li= link_to 'Correlations', correlations_path
- if AppConfig[:mount_resque_web] - if AppConfig[:mount_resque_web]
%li= link_to 'Resque Overview', resque_web_path %li= link_to 'Resque Overview', resque_web_path

View file

@ -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

View file

@ -97,6 +97,7 @@ Diaspora::Application.routes.draw do
match :user_search match :user_search
get :admin_inviter get :admin_inviter
get :weekly_user_stats get :weekly_user_stats
get :correlations
get :stats, :as => 'pod_stats' get :stats, :as => 'pod_stats'
end end

View file

@ -55,15 +55,21 @@ SQL
SQL SQL
end end
def posts_count_correlation def post_count_correlation
# [{"id" => 1 , "count" => 123}] # [{"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 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) User.where("username IS NOT NULL").where("created_at > ? and created_at < ?", Time.now - (n+1).weeks, Time.now - n.weeks)
end 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 end

View file

@ -4,7 +4,8 @@ require 'lib/statistics'
describe Statistics do describe Statistics do
before 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 }, @result = [{"id" => alice.id , "count" => 0 },
{"id" => bob.id , "count" => 1 }, {"id" => bob.id , "count" => 1 },
{"id" => eve.id , "count" => 0 }, {"id" => eve.id , "count" => 0 },
@ -59,11 +60,9 @@ describe Statistics do
describe "#correlation_hash" do describe "#correlation_hash" do
it 'it returns a hash of including start and end time' do it 'it returns a hash of including start and end time' do
time = Time.now
hash = @stats.correlation_hash hash = @stats.correlation_hash
hash[:starrt_time].should == time hash[:starrt_time].should == @time
hash[:end_time].should == time - 1.week hash[:end_time].should == @time - 1.week
end end
it 'returns the post count (and sign_in_count) correlation' do 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 @stats.generate_correlations[:posts_count].should == 0.5
end end
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 context 'todos' do
before do before do