correlations on the admins page

This commit is contained in:
Ilya Zhitomirskiy 2011-10-10 18:29:57 -07:00
parent 4c9b45b9c8
commit 288d20b6e8
4 changed files with 37 additions and 38 deletions

View file

@ -66,7 +66,7 @@ class AdminsController < ApplicationController
end
def correlations
@post_count_correlation = Statistics.new.post_count_correlation
@correlations_hash = Statistics.new.generate_correlations
end
private

View file

@ -6,6 +6,8 @@
.span-24.last
%h1
= "Correlation between Post Count and Sign In Count"
= "Correlations between with Sign In Count"
%ul
= @post_count_correlation
- @correlations_hash.keys.each do |k|
%li
= "#{k.to_s}, #{@correlations_hash[k]}"

View file

@ -55,16 +55,16 @@ SQL
SQL
end
def post_count_correlation
def correlate(first_metric, second_metric)
# [{"id" => 1 , "count" => 123}]
x_array = []
y_array = []
post_count_hash.keys.each do |k|
if val = sign_in_count_hash[k]
x_array << post_count_hash[k]
self.result_hash(first_metric).keys.each do |k|
if val = self.result_hash(second_metric)[k]
x_array << self.result_hash(first_metric)[k]
y_array << val
end
end
@ -72,15 +72,15 @@ SQL
correlation(x_array, y_array)
end
def generate_correlations
result = {}
[:posts_count, :invites_sent_count, :tags_followed_count,
:mentions_count].each do |metric|
result[metric] = self.correlate(metric,:sign_in_count)
end
result
end
###\
#def correlate(thing)
# sql = self.send("#{thing}_count_sql".to_sym)
# self.correlation(User.connection.select_all(sql),
#end
###
def correlation(x_array, y_array)
x = x_array.to_scale
@ -99,23 +99,17 @@ 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)
#@param [Symbol] input type
#@returns [Hash] of resulting query
def result_hash(type)
instance_hash = self.instance_variable_get("@#{type.to_s}_hash".to_sym)
unless instance_hash
post_count_array = User.connection.select_all(self.send("#{type.to_s}_sql".to_sym))
@post_count_hash = {}
post_count_array.each{ |h| @post_count_hash[h['id']] = h["count"]}
instance_hash = {}
post_count_array.each{ |h| instance_hash[h['id']] = h["count"]}
self.instance_variable_set("@#{type.to_s}_hash".to_sym, instance_hash)
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
instance_hash
end
end

View file

@ -57,22 +57,25 @@ describe Statistics do
@stats.correlation([1,2,1,2],[1,1,2,2]).to_s.should == 0.0.to_s
end
end
describe "#correlation_hash" do
describe "#generate_correlations" do
it 'it returns a hash of including start and end time' do
pending
hash = @stats.correlation_hash
hash[:starrt_time].should == @time
hash[:start_time].should == @time
hash[:end_time].should == @time - 1.week
end
it 'returns the post count (and sign_in_count) correlation' do
@stats.stub(:posts_count_correlation).and_return(0.5)
bob.sign_in_count = 1
bob.post(:status_message, :text => "here is a message")
bob.save!
@stats.generate_correlations[:posts_count].should == 0.5
@stats.generate_correlations[:posts_count].to_s.should == "1.0"
end
end
describe "#post_count_correlation" do
describe "#correlate" do
it 'calls correlation with post' do
User.connection.should_receive(:select_all).and_return([{"id"=> 1, "count" => 7},
{"id" => 2, "count" => 8},
@ -82,7 +85,7 @@ describe Statistics do
)
@stats.should_receive(:correlation).with([7,9],[17,19]).and_return(0.5)
@stats.posts_count_correlation.should == 0.5
@stats.correlate(:posts_count,:sign_in_count).should == 0.5
end
end