Merge pull request #2163 from Pistos/postgresql-statistics

Postgresql statistics
This commit is contained in:
Ilya Zhitomirskiy 2011-10-11 15:33:54 -07:00
commit 496dae1607
2 changed files with 32 additions and 16 deletions

View file

@ -87,7 +87,7 @@ SQL
x_array = []
y_array = []
self.result_hash(first_metric).keys.each do |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
@ -109,7 +109,7 @@ SQL
def correlation(x_array, y_array)
sum = 0.0
x_array.each_index do |i|
sum = sum + x_array[i]*y_array[i]
sum = sum + x_array[i].to_f * y_array[i].to_f
end
x_y_mean = sum/x_array.length.to_f
x_mean = mean(x_array)
@ -123,7 +123,7 @@ SQL
def mean(array)
sum = array.inject(0.0) do |sum, val|
sum += val
sum += val.to_f
end
sum / array.length
end
@ -132,7 +132,7 @@ SQL
variance = lambda do
m = mean(array)
sum = 0.0
array.each{ |v| sum += (v-m)**2 }
array.each{ |v| sum += (v.to_f-m)**2 }
sum/array.length.to_f
end.call
@ -146,7 +146,13 @@ SQL
protected
def where_clause_sql
"where users.created_at > FROM_UNIXTIME(#{(Time.now - 1.month).to_i})"
if postgres?
"WHERE users.created_at > NOW() - '1 month'::INTERVAL"
elsif sqlite?
raise "#where_clause_sql not yet written for SQLite"
else
"where users.created_at > FROM_UNIXTIME(#{(Time.now - 1.month).to_i})"
end
end
def week_created(n)

View file

@ -3,6 +3,16 @@ require 'lib/statistics'
describe Statistics do
def result_should_equal( actual )
actual.count.should == @result.count
@result.each do |expected_hash|
actual.find { |actual_hash|
actual_hash['id'].to_i == expected_hash['id'].to_i &&
actual_hash['count'].to_i == expected_hash['count'].to_i
}.should_not be_nil
end
end
before do
@time = Time.now
@stats = Statistics.new#(@time, @time - 1.week)
@ -16,7 +26,7 @@ describe Statistics do
describe '#posts_count_sql' do
it "pulls back an array of post counts and ids" do
Factory.create(:status_message, :author => bob.person)
User.connection.select_all(@stats.posts_count_sql).should =~ @result
result_should_equal User.connection.select_all(@stats.posts_count_sql)
end
end
@ -24,7 +34,7 @@ describe Statistics do
it "pulls back an array of post counts and ids" do
sm = Factory.create(:status_message, :author => alice.person)
bob.comment("sup", :post => sm)
User.connection.select_all(@stats.comments_count_sql).should =~ @result
result_should_equal User.connection.select_all(@stats.comments_count_sql)
end
end
@ -32,14 +42,14 @@ describe Statistics do
describe '#invites_sent_count_sql' do
it "pulls back an array of invite counts and ids" do
Invitation.batch_invite(["a@a.com"], :sender => bob, :aspect => bob.aspects.first, :service => 'email')
User.connection.select_all(@stats.invites_sent_count_sql).should =~ @result
result_should_equal User.connection.select_all(@stats.invites_sent_count_sql)
end
end
describe '#tags_followed_count_sql' do
it "pulls back an array of tag following counts and ids" do
TagFollowing.create!(:user => bob, :tag_id => 1)
User.connection.select_all(@stats.tags_followed_count_sql).should =~ @result
result_should_equal User.connection.select_all(@stats.tags_followed_count_sql)
end
end
@ -47,7 +57,7 @@ describe Statistics do
it "pulls back an array of mentions following counts and ids" do
post = Factory.create(:status_message, :author => bob.person)
Mention.create(:post => post, :person => bob.person)
User.connection.select_all(@stats.mentions_count_sql).should =~ @result
result_should_equal User.connection.select_all(@stats.mentions_count_sql)
end
end
@ -61,7 +71,7 @@ describe Statistics do
{"id" => local_luke.id , "count" => 2 },
{"id" => local_leia.id , "count" => 2 }]
User.connection.select_all(@stats.contacts_sharing_with_count_sql).should =~ @result
result_should_equal User.connection.select_all(@stats.contacts_sharing_with_count_sql)
end
end
@ -69,19 +79,19 @@ describe Statistics do
it "pulls back an array of sign_in_counts and ids" do
bob.sign_in_count = 1
bob.save!
User.connection.select_all(@stats.sign_in_count_sql).should =~ @result
result_should_equal User.connection.select_all(@stats.sign_in_count_sql)
end
end
["posts_count", "comments_count", "invites_sent_count", "tags_followed_count",
["posts_count", "comments_count", "invites_sent_count", "tags_followed_count",
"mentions_count", "sign_in_count", "contacts_sharing_with_count" ].each do |method|
it "#{method}_sql calls where_sql" do
@stats.should_receive(:where_clause_sql)
@stats.send("#{method}_sql".to_sym)
end
if !["sign_in_count", "tags_followed_count"].include?(method)
it "#generate_correlations calls correlate with #{method} and sign_in_count" do
@stats.stub(:correlate).and_return(0.5)
@ -114,7 +124,7 @@ describe Statistics do
@stats.generate_correlations[:posts_count].to_s.should == "1.0"
end
end
describe "#correlate" do
it 'calls correlation with post' do
User.connection.should_receive(:select_all).and_return([{"id"=> 1, "count" => 7},