added users_with_posts_today to DataPoint

This commit is contained in:
zhitomirskiyi 2011-01-20 11:45:11 -08:00
parent ecb059b661
commit 55bfbfd5b0
6 changed files with 63 additions and 10 deletions

View file

@ -1,6 +1,12 @@
class DataPoint < ActiveRecord::Base class DataPoint < ActiveRecord::Base
attr_accessor :descriptor
attr_accessor :value
belongs_to :statistic 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 end

View file

@ -1,5 +1,3 @@
class Statistc < ActiveRecord::Base class Statistc < ActiveRecord::Base
attr_accessor :average
has_many :data_points, :class_name => 'DataPoint' has_many :data_points, :class_name => 'DataPoint'
end end

View file

@ -1,7 +1,7 @@
class CreateDataPoints < ActiveRecord::Migration class CreateDataPoints < ActiveRecord::Migration
def self.up def self.up
create_table :data_points do |t| create_table :data_points do |t|
t.string :descriptor t.string :key
t.integer :value t.integer :value
t.integer :statistic_id t.integer :statistic_id

View file

@ -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 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| create_table "data_points", :force => true do |t|
t.string "descriptor" t.string "key"
t.integer "value" t.integer "value"
t.integer "statistic_id" t.integer "statistic_id"
t.datetime "created_at" t.datetime "created_at"

View file

@ -22,13 +22,19 @@ namespace :statistics do
def users_with_x_posts(count) def users_with_x_posts(count)
@sql.execute( @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] ).first[0]
end end
def users_with_x_contacts(count) def users_with_x_contacts(count)
@sql.execute( @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] ).first[0]
end end
@ -49,6 +55,15 @@ namespace :statistics do
puts "Users with 10 or more contacts: %i" % users_with_x_contacts(9) puts "Users with 10 or more contacts: %i" % users_with_x_contacts(9)
end 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 task :splunk => :environment do
puts "event=statistic, type=users, count=#{users}, "+ puts "event=statistic, type=users, count=#{users}, "+
"incomplete=#{incomplete}, " + "incomplete=#{incomplete}, " +

View file

@ -1,5 +1,39 @@
require 'spec_helper' require 'spec_helper'
describe DataPoint do 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 end