statistics class does distributions and averages on its DataPoints
This commit is contained in:
parent
55bfbfd5b0
commit
f89c442730
7 changed files with 92 additions and 13 deletions
|
|
@ -1,3 +0,0 @@
|
|||
class Statistc < ActiveRecord::Base
|
||||
has_many :data_points, :class_name => 'DataPoint'
|
||||
end
|
||||
32
app/models/statistic.rb
Normal file
32
app/models/statistic.rb
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
class Statistic < ActiveRecord::Base
|
||||
has_many :data_points, :class_name => 'DataPoint'
|
||||
|
||||
def compute_average
|
||||
users = 0
|
||||
sum = 0
|
||||
self.data_points.each do |d|
|
||||
sum += d.key*d.value
|
||||
users += d.value
|
||||
end
|
||||
self.average = sum.to_f/users
|
||||
end
|
||||
|
||||
def distribution
|
||||
@dist ||= lambda {
|
||||
dist = {}
|
||||
self.data_points.each do |d|
|
||||
dist[d.key] = d.value.to_f/users_in_sample
|
||||
end
|
||||
dist
|
||||
}.call
|
||||
end
|
||||
|
||||
def users_in_sample
|
||||
@users ||= lambda {
|
||||
users = self.data_points.map{|d| d.value}
|
||||
users.inject do |total,curr|
|
||||
total += curr
|
||||
end
|
||||
}.call
|
||||
end
|
||||
end
|
||||
|
|
@ -1,6 +1,6 @@
|
|||
class CreateStatistcs < ActiveRecord::Migration
|
||||
class CreateStatistics < ActiveRecord::Migration
|
||||
def self.up
|
||||
create_table :statistcs do |t|
|
||||
create_table :statistics do |t|
|
||||
t.integer :average
|
||||
t.string :type
|
||||
|
||||
|
|
@ -417,7 +417,7 @@ ActiveRecord::Schema.define(:version => 20110120182100) do
|
|||
add_index "services", ["mongo_id"], :name => "index_services_on_mongo_id"
|
||||
add_index "services", ["user_id"], :name => "index_services_on_user_id"
|
||||
|
||||
create_table "statistcs", :force => true do |t|
|
||||
create_table "statistics", :force => true do |t|
|
||||
t.integer "average"
|
||||
t.string "type"
|
||||
t.datetime "created_at"
|
||||
|
|
|
|||
|
|
@ -60,8 +60,8 @@ namespace :statistics do
|
|||
[0..15].each do |n|
|
||||
stat.data_points << DataPoint.posts_per_day(n)
|
||||
end
|
||||
stat.compute_avg
|
||||
stat.save!
|
||||
stat.compute_average
|
||||
stat.save
|
||||
end
|
||||
|
||||
task :splunk => :environment do
|
||||
|
|
|
|||
|
|
@ -1,5 +0,0 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe Statistc do
|
||||
pending "add some examples to (or delete) #{__FILE__}"
|
||||
end
|
||||
55
spec/models/statistic_spec.rb
Normal file
55
spec/models/statistic_spec.rb
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe Statistic do
|
||||
before(:all) do
|
||||
@stat = Statistic.new
|
||||
|
||||
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
|
||||
|
||||
(0..10).each do |n|
|
||||
@stat.data_points << DataPoint.users_with_posts_today(n)
|
||||
end
|
||||
end
|
||||
|
||||
context '#compute_average' do
|
||||
it 'computes the average of all its DataPoints' do
|
||||
@stat.compute_average.should == 16.to_f/3
|
||||
end
|
||||
end
|
||||
|
||||
context '#distribution' do
|
||||
it 'generates a hash' do
|
||||
@stat.distribution.class.should == Hash
|
||||
end
|
||||
|
||||
it 'correctly sets values' do
|
||||
dist = @stat.distribution
|
||||
[dist[1], dist[5], dist[10]].each do |d|
|
||||
d.should == 1.to_f/3
|
||||
end
|
||||
end
|
||||
|
||||
it 'generates a distribution' do
|
||||
values = @stat.distribution.map{|d| d[1]}
|
||||
values.inject{ |sum, curr|
|
||||
sum += curr
|
||||
}.should == 1
|
||||
end
|
||||
end
|
||||
|
||||
context '#users_in_sample' do
|
||||
it 'returns a count' do
|
||||
@stat.users_in_sample.should == 3
|
||||
end
|
||||
end
|
||||
end
|
||||
Loading…
Reference in a new issue