correlations on the admins page
This commit is contained in:
parent
4c9b45b9c8
commit
288d20b6e8
4 changed files with 37 additions and 38 deletions
|
|
@ -66,7 +66,7 @@ class AdminsController < ApplicationController
|
||||||
end
|
end
|
||||||
|
|
||||||
def correlations
|
def correlations
|
||||||
@post_count_correlation = Statistics.new.post_count_correlation
|
@correlations_hash = Statistics.new.generate_correlations
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,8 @@
|
||||||
|
|
||||||
.span-24.last
|
.span-24.last
|
||||||
%h1
|
%h1
|
||||||
= "Correlation between Post Count and Sign In Count"
|
= "Correlations between with Sign In Count"
|
||||||
%ul
|
%ul
|
||||||
= @post_count_correlation
|
- @correlations_hash.keys.each do |k|
|
||||||
|
%li
|
||||||
|
= "#{k.to_s}, #{@correlations_hash[k]}"
|
||||||
|
|
|
||||||
|
|
@ -55,16 +55,16 @@ SQL
|
||||||
SQL
|
SQL
|
||||||
end
|
end
|
||||||
|
|
||||||
def post_count_correlation
|
def correlate(first_metric, second_metric)
|
||||||
|
|
||||||
# [{"id" => 1 , "count" => 123}]
|
# [{"id" => 1 , "count" => 123}]
|
||||||
|
|
||||||
x_array = []
|
x_array = []
|
||||||
y_array = []
|
y_array = []
|
||||||
|
|
||||||
post_count_hash.keys.each do |k|
|
self.result_hash(first_metric).keys.each do |k|
|
||||||
if val = sign_in_count_hash[k]
|
if val = self.result_hash(second_metric)[k]
|
||||||
x_array << post_count_hash[k]
|
x_array << self.result_hash(first_metric)[k]
|
||||||
y_array << val
|
y_array << val
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
@ -72,15 +72,15 @@ SQL
|
||||||
correlation(x_array, y_array)
|
correlation(x_array, y_array)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def generate_correlations
|
||||||
###\
|
result = {}
|
||||||
#def correlate(thing)
|
[:posts_count, :invites_sent_count, :tags_followed_count,
|
||||||
# sql = self.send("#{thing}_count_sql".to_sym)
|
:mentions_count].each do |metric|
|
||||||
# self.correlation(User.connection.select_all(sql),
|
result[metric] = self.correlate(metric,:sign_in_count)
|
||||||
#end
|
end
|
||||||
|
result
|
||||||
|
end
|
||||||
###
|
|
||||||
|
|
||||||
def correlation(x_array, y_array)
|
def correlation(x_array, y_array)
|
||||||
x = x_array.to_scale
|
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)
|
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
|
#@param [Symbol] input type
|
||||||
unless @post_count_hash
|
#@returns [Hash] of resulting query
|
||||||
post_count_array = User.connection.select_all(self.posts_count_sql)
|
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 = {}
|
instance_hash = {}
|
||||||
post_count_array.each{ |h| @post_count_hash[h['id']] = h["count"]}
|
post_count_array.each{ |h| instance_hash[h['id']] = h["count"]}
|
||||||
|
self.instance_variable_set("@#{type.to_s}_hash".to_sym, instance_hash)
|
||||||
end
|
end
|
||||||
@post_count_hash
|
instance_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
|
end
|
||||||
|
|
|
||||||
|
|
@ -57,22 +57,25 @@ describe Statistics do
|
||||||
@stats.correlation([1,2,1,2],[1,1,2,2]).to_s.should == 0.0.to_s
|
@stats.correlation([1,2,1,2],[1,1,2,2]).to_s.should == 0.0.to_s
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
describe "#correlation_hash" do
|
describe "#generate_correlations" 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
|
||||||
|
pending
|
||||||
hash = @stats.correlation_hash
|
hash = @stats.correlation_hash
|
||||||
hash[:starrt_time].should == @time
|
hash[:start_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
|
||||||
@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
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "#post_count_correlation" do
|
describe "#correlate" do
|
||||||
it 'calls correlation with post' do
|
it 'calls correlation with post' do
|
||||||
User.connection.should_receive(:select_all).and_return([{"id"=> 1, "count" => 7},
|
User.connection.should_receive(:select_all).and_return([{"id"=> 1, "count" => 7},
|
||||||
{"id" => 2, "count" => 8},
|
{"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.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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue