Skip to content

Commit

Permalink
Made a few changes to the examples in the README and also added a lic…
Browse files Browse the repository at this point in the history
…ense
  • Loading branch information
alex committed May 27, 2009
1 parent 28b513b commit 40b5d66
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 20 deletions.
22 changes: 22 additions & 0 deletions MIT-LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
Copyright (c) 2009 Alexandru Catighera

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
62 changes: 42 additions & 20 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,30 @@
This ActiverRecord plugin allows you to easily define and pull statistics for AR models. This plugin was built with reporting in mind.

## Installation
script/plugin install git://github.com/acatighera/statistics.git
script/plugin install git://github.com/acatighera/statistics.git

## Examples
#### Defining statistics is similar to defining named scopes:

class Account < ActiveRecord::Base
define_statistic "Basic Count", :count => :all
define_statistic "Basic Sum", :sum => :all, :column_name => 'amount'
define_statistic "User Count", :count => :all
define_statistic "Average Age", :average => :all, :column_name => 'age'
define_statistic "Subcriber Count", :count => :all, :conditions => "subscription_opt_in = 1"
end

class Donations < ActiveRecord::Base
define_statistic "Total Donations", :sum => :all, :column_name => "amount"
end

#### Actually pulling the numbers is simple:

#####for all stats

Account.statistics
Account.statistics # returns { 'User Count' => 120, 'Average Age' => 28, 'Subscriber Count' => 74 }

#####for a single stat

Account.get_stat(‘Basic Count’)
Account.get_stat(‘User Count’) # returns 120

### Here are some additional benefits of using this plugin:

Expand All @@ -30,13 +35,14 @@ script/plugin install git://github.com/acatighera/statistics.git
Note: I found filtering to be an important part of reporting (ie. filtering by date). All filters are optional so even if you define them you don’t have to use them when pulling data. Using the `filter_all_stats_on` method and `:joins` options you can make things filterable by the same things which I found to be extremely useful.

class Account < ActiveRecord::Base
define_statistic "Basic Count", :count => :all
define_statistic "Custom Count", :count => :all, :filter_on => { :channel => 'channel = ?', :start_date => 'DATE(created_at) > ?'}
filter_all_stats_on(:user_id, "user_id = ?")
define_statistic "User Count", :count => :all, , :filter_on => { :state => 'state = ?', :created_after => 'DATE(created_at) > ?'}
define_statistic "Subcriber Count", :count => :all, :conditions => "subscription_opt_in = true"
filter_all_stats_on(:account_type, "account_type = ?")
end

Account.statistics(:user_id => 5)
Account.get_stat(‘Custom Count’, :user_id => 5, :start_date => ‘2009-01-01’)
Account.statistics(:account_type => 'non-admin')
Account.get_stat(‘User Count’, :account_type => 'non-admin', :created_after => ‘2009-01-01’, :state => 'NY')

#### Standardized

Expand All @@ -47,28 +53,42 @@ All ActiveRecord classes now respond to `statistics` and `get_stat` methods
all_stats << ar.statistics
end

Account.get_stat(“Basic Count’)

#### Calculated statistics (DRY)

You can define calculated metrics in order to perform mathematical calculations on one or more defined statistics. (These calculated metrics also work with filters!)
You can define calculated metrics in order to perform mathematical calculations on one or more defined statistics.

class Account < ActiveRecord::Base
define_statistic "Basic Count", :count => :all
define_statistic "Basic Sum", :sum => :all, :column_name => 'amount'
define_calculated_statistic "Total Amount" do
defined_stats('Basic Sum') * defined_stats('Basic Count')
has_many :donations
define_statistic "User Count", :count => :all
define_statistic "Total Donations", :sum => :all, :column_name => 'donations.amount', :joins => :donations
define_calculated_statistic "Average Donation per User" do
defined_stats('Total Donations') / defined_stats('User Count')
end
filter_all_stats_on(:account_type, "account_type = ?")
filter_all_stats_on(:state, "state = ?")
filter_all_stats_on(:created_after, "DATE(created_at) > ?")
end


Pulling stats for calculated metrics is the same as for regular statistics. They also work with filters like regular statistics!

Account.get_stat('Average Donation Per User', :account_type => 'non-admin', :state => 'NY')
Account.get_stat('Average Donation Per User', :created_after => '2009-01-01')

#### Reuse scopes you already have defined

You can reuse the code you have written to do reporting.

class Account < ActiveRecord::Base
named_scope :scope1, :conditions => “status = ‘active’”
named_scope :scope2, :joins => :posts
define_statistic "Chained Scope Count", :count => [:scope1, :scope2]
has_many :posts
named_scope :not_admins, :conditions => “account_type = ‘non-admin’”
named_scope :accounts_with_posts, :joins => :posts
define_statistic "Active Users Count", :count => [:not_admins, :accounts_with_posts]
end

#### Accepts all ActiveRecord::Calculations options
Expand All @@ -78,3 +98,5 @@ The `:conditions` and `:joins` options are all particularly useful
class Account < ActiveRecord::Base
define_statistic "Active Accounts With Posts", :count => :all, :joins => :posts, :conditions => "status = 'active'"
end

###### Copyright (c) 2009 Alexandru Catighera, released under MIT license

0 comments on commit 40b5d66

Please sign in to comment.