Make statistics work under PostgreSQL.
This commit is contained in:
parent
76ab5b2efb
commit
ed9328450a
2 changed files with 27 additions and 11 deletions
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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,7 +79,7 @@ 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
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue