From 76ab5b2efb7bebb50c8b3bcb916ea909e83eb29a Mon Sep 17 00:00:00 2001 From: Pistos Date: Tue, 11 Oct 2011 17:48:33 -0400 Subject: [PATCH 1/2] Whitespace cleanup. --- lib/statistics.rb | 2 +- spec/lib/statistics_spec.rb | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/statistics.rb b/lib/statistics.rb index 8219a5c8a..fe0f172e7 100644 --- a/lib/statistics.rb +++ b/lib/statistics.rb @@ -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 diff --git a/spec/lib/statistics_spec.rb b/spec/lib/statistics_spec.rb index 9b8b0180d..0f5d4628d 100644 --- a/spec/lib/statistics_spec.rb +++ b/spec/lib/statistics_spec.rb @@ -73,15 +73,15 @@ describe Statistics do 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 +114,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}, From ed9328450ae3e85c7a494e6da0f4e0dff3a068a7 Mon Sep 17 00:00:00 2001 From: Pistos Date: Tue, 11 Oct 2011 17:48:57 -0400 Subject: [PATCH 2/2] Make statistics work under PostgreSQL. --- lib/statistics.rb | 14 ++++++++++---- spec/lib/statistics_spec.rb | 24 +++++++++++++++++------- 2 files changed, 27 insertions(+), 11 deletions(-) diff --git a/lib/statistics.rb b/lib/statistics.rb index fe0f172e7..4ccccc533 100644 --- a/lib/statistics.rb +++ b/lib/statistics.rb @@ -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) diff --git a/spec/lib/statistics_spec.rb b/spec/lib/statistics_spec.rb index 0f5d4628d..3425a97a1 100644 --- a/spec/lib/statistics_spec.rb +++ b/spec/lib/statistics_spec.rb @@ -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