diff --git a/app/models/data_point.rb b/app/models/data_point.rb index 3dfc32d37..9debe8526 100644 --- a/app/models/data_point.rb +++ b/app/models/data_point.rb @@ -1,6 +1,12 @@ class DataPoint < ActiveRecord::Base - attr_accessor :descriptor - attr_accessor :value - belongs_to :statistic + + def self.users_with_posts_today(number) + sql = ActiveRecord::Base.connection() + value = sql.execute( + "SELECT COUNT(*) FROM (SELECT `people`.guid, COUNT(*) AS posts_sum FROM `people` LEFT JOIN `posts` ON `people`.id = `posts`.person_id AND `posts`.created_at > '#{(Time.now - 1.days).to_date}' GROUP BY `people`.guid) AS t1 WHERE t1.posts_sum = #{number};" + ).first[0] + + self.new(:key => number, :value => value) + end end diff --git a/app/models/statistc.rb b/app/models/statistc.rb index 65ea33dad..f8a133bb8 100644 --- a/app/models/statistc.rb +++ b/app/models/statistc.rb @@ -1,5 +1,3 @@ class Statistc < ActiveRecord::Base - attr_accessor :average - has_many :data_points, :class_name => 'DataPoint' end diff --git a/db/migrate/20110120182100_create_data_points.rb b/db/migrate/20110120182100_create_data_points.rb index 5d1c248b2..d12351bed 100644 --- a/db/migrate/20110120182100_create_data_points.rb +++ b/db/migrate/20110120182100_create_data_points.rb @@ -1,7 +1,7 @@ class CreateDataPoints < ActiveRecord::Migration def self.up create_table :data_points do |t| - t.string :descriptor + t.string :key t.integer :value t.integer :statistic_id diff --git a/db/schema.rb b/db/schema.rb index 0a07ab14b..6bde3fdee 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -68,7 +68,7 @@ ActiveRecord::Schema.define(:version => 20110120182100) do add_index "contacts", ["user_id", "person_id"], :name => "index_contacts_on_user_id_and_person_id", :unique => true create_table "data_points", :force => true do |t| - t.string "descriptor" + t.string "key" t.integer "value" t.integer "statistic_id" t.datetime "created_at" diff --git a/lib/tasks/statistics.rake b/lib/tasks/statistics.rake index 3ed0e0dce..d59a14f83 100644 --- a/lib/tasks/statistics.rake +++ b/lib/tasks/statistics.rake @@ -22,13 +22,19 @@ namespace :statistics do def users_with_x_posts(count) @sql.execute( - "SELECT COUNT(*) FROM (SELECT `people`.guid, COUNT(*) AS posts_sum FROM `people` LEFT JOIN `posts` ON `people`.id = `posts`.person_id GROUP BY `people`.guid) AS t1 WHERE t1.posts_sum > #{count};" + "SELECT COUNT(*) FROM (SELECT `people`.guid, COUNT(*) AS posts_sum FROM `people` LEFT JOIN `posts` ON `people`.id = `posts`.person_id GROUP BY `people`.guid) AS t1 WHERE t1.posts_sum = #{count};" + ).first[0] + end + + def users_with_x_posts_today(count) + @sql.execute( + "SELECT COUNT(*) FROM (SELECT `people`.guid, COUNT(*) AS posts_sum FROM `people` LEFT JOIN `posts` ON `people`.id = `posts`.person_id AND `post`.created_at > '#{(Time.now - 1.days).to_date}' GROUP BY `people`.guid) AS t1 WHERE t1.posts_sum = #{count};" ).first[0] end def users_with_x_contacts(count) @sql.execute( - "SELECT COUNT(*) FROM (SELECT `users`.id, COUNT(*) AS contact_sum FROM `users` LEFT JOIN `contacts` ON `users`.id = `contacts`.person_id AND `contacts`.pending = 0 GROUP BY `users`.id) AS t1 WHERE t1.contact_sum > #{count};" + "SELECT COUNT(*) FROM (SELECT `users`.id, COUNT(*) AS contact_sum FROM `users` LEFT JOIN `contacts` ON `users`.id = `contacts`.person_id AND `contacts`.pending = 0 GROUP BY `users`.id) AS t1 WHERE t1.contact_sum = #{count};" ).first[0] end @@ -49,6 +55,15 @@ namespace :statistics do puts "Users with 10 or more contacts: %i" % users_with_x_contacts(9) end + task :model => :environment do + stat = Statistic.new(:type => "posts_per_day") + [0..15].each do |n| + stat.data_points << DataPoint.posts_per_day(n) + end + stat.compute_avg + stat.save! + end + task :splunk => :environment do puts "event=statistic, type=users, count=#{users}, "+ "incomplete=#{incomplete}, " + diff --git a/spec/models/data_point_spec.rb b/spec/models/data_point_spec.rb index c5a630bd2..9f927cedd 100644 --- a/spec/models/data_point_spec.rb +++ b/spec/models/data_point_spec.rb @@ -1,5 +1,39 @@ require 'spec_helper' describe DataPoint do - pending "add some examples to (or delete) #{__FILE__}" + context '.posts_per_day_last_week' do + before do + 1.times do |n| + alice.post(:status_message, :message => 'hi', :to => alice.aspects.first) + end + + 5.times do |n| + bob.post(:status_message, :message => 'hi', :to => bob.aspects.first) + end + + 10.times do |n| + eve.post(:status_message, :message => 'hi', :to => eve.aspects.first) + end + end + + it 'returns a DataPoint object' do + DataPoint.users_with_posts_today(1).class.should == DataPoint + end + + it 'returns a DataPoint with non-zero value' do + point = DataPoint.users_with_posts_today(1) + point.value.should == 1 + end + + it 'returns a DataPoint with zero value' do + point = DataPoint.users_with_posts_today(15) + point.value.should == 0 + end + + it 'returns the correct descriptor' do + point = DataPoint.users_with_posts_today(15) + point.key.should == 15 + end + end + end