Skip to content
Extraits de code Groupes Projets
Valider f89c4427 rédigé par zhitomirskiyi's avatar zhitomirskiyi
Parcourir les fichiers

statistics class does distributions and averages on its DataPoints

parent 55bfbfd5
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
class Statistc < ActiveRecord::Base
has_many :data_points, :class_name => 'DataPoint'
end
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
class CreateStatistcs < ActiveRecord::Migration class CreateStatistics < ActiveRecord::Migration
def self.up def self.up
create_table :statistcs do |t| create_table :statistics do |t|
t.integer :average t.integer :average
t.string :type t.string :type
......
...@@ -417,7 +417,7 @@ ActiveRecord::Schema.define(:version => 20110120182100) do ...@@ -417,7 +417,7 @@ ActiveRecord::Schema.define(:version => 20110120182100) do
add_index "services", ["mongo_id"], :name => "index_services_on_mongo_id" add_index "services", ["mongo_id"], :name => "index_services_on_mongo_id"
add_index "services", ["user_id"], :name => "index_services_on_user_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.integer "average"
t.string "type" t.string "type"
t.datetime "created_at" t.datetime "created_at"
......
...@@ -60,8 +60,8 @@ namespace :statistics do ...@@ -60,8 +60,8 @@ namespace :statistics do
[0..15].each do |n| [0..15].each do |n|
stat.data_points << DataPoint.posts_per_day(n) stat.data_points << DataPoint.posts_per_day(n)
end end
stat.compute_avg stat.compute_average
stat.save! stat.save
end end
task :splunk => :environment do task :splunk => :environment do
......
require 'spec_helper'
describe Statistc do
pending "add some examples to (or delete) #{__FILE__}"
end
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
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter