Permalink
Browse files

Improved documentation.

  Also renamed Sme::Config#granularity to Sme::Config#rollup_window.
  • Loading branch information...
1 parent 8fc77b4 commit 7f691c363f611c67a51213bdd63c86e79df629c3 @ss committed Dec 6, 2011
Showing with 38 additions and 26 deletions.
  1. +3 −3 README
  2. +8 −8 app/models/sme/rollup.rb
  3. +2 −1 lib/sme.rb
  4. +21 −10 lib/sme/config.rb
  5. +4 −4 test/unit/sme/rollup_test.rb
View
6 README
@@ -21,9 +21,9 @@ in config/initializers/simple_metrics_engine.rb
# only allow admins to access metrics
config.permission_check { redirect_to login_path and return false unless current_user.admin? }
-
+
# (Optional) Metrics will be rolled up into 15-minute periods starting every hour on the hour.
- config.granularity = 15.minutes
+ config.rollup_window = 15.minutes
end
Modify your User controller to log a visits and conversions.
@@ -78,6 +78,6 @@ in app/models/user.rb:
Create a cron job that runs the +sme:rollup_logs+ rake task:
HOME=<rails root>
-1 * * * * rake sme:rollup_logs >> $HOME/log/sme_rollup_logs.log 2>&1
+0/15 * * * * rake sme:rollup_logs >> $HOME/log/sme_rollup_logs.log 2>&1
View
@@ -41,7 +41,7 @@ def self.update_metrics(opts={})
end
end
- def self.each_period(from, to, inc=granularity)
+ def self.each_period(from, to, inc=rollup_window)
from, to = to, from if to < from
while from < to do
@@ -75,7 +75,7 @@ def self.merge_sum(range, to, sum)
end
def self.default_range
- period_for(Time.zone.now - granularity)
+ period_for(Time.zone.now - rollup_window)
end
def self.period_for(time)
@@ -85,26 +85,26 @@ def self.period_for(time)
(round_down(time.first) .. round_up(time.last))
else
time = round_down(time)
- (time .. (time + granularity))
+ (time .. (time + rollup_window))
end
end
def self.round_down(time)
time = to_time(time).to_i # also handily gets rid of usec
- Time.at(on_boundary?(time) ? time : time - (time - granularity) % granularity)
+ Time.at(on_boundary?(time) ? time : time - (time - rollup_window) % rollup_window)
end
def self.round_up(time)
time = to_time(time).to_i # also handily gets rid of usec
- Time.at(on_boundary?(time) ? time : time - time % granularity + granularity)
+ Time.at(on_boundary?(time) ? time : time - time % rollup_window + rollup_window)
end
def self.on_boundary?(time)
- (0 == (time.to_i % granularity))
+ (0 == (time.to_i % rollup_window))
end
- def self.granularity
- Sme.configuration.granularity
+ def self.rollup_window
+ Sme.configuration.rollup_window
end
def self.to_time(time)
View
@@ -6,13 +6,14 @@ module Sme
#
# Sme.configure do |config|
#
- # config.granularity = 15.minutes
+ # config.rollup_window = 15.minutes
#
# end
def self.configure
yield configuration
end
+ # Returns the current configuration
def self.configuration
@configuration ||= Sme::Config.new
end
View
@@ -1,20 +1,31 @@
module Sme
class Config
- attr_accessor :event_separator, :granularity, :report_intervals
+ # The seperator used to delimit parts of an event (default = '|').
+ #
+ # call-seq:
+ # config.event_separator = '/'
+ #
+ attr_accessor :event_separator
+
+ # Rollup window (default = 1.hour).
+ #
+ # call-seq:
+ # config.rollup_window = 15.minutes
+ #
+ attr_accessor :rollup_window
def initialize
- @event_separator = '|'
- @granularity = 1.hour
- @report_intervals = [
- {'1 hour' => 1.hour},
- {'1 day' => 1.days},
- {'1 week' => 1.week},
- {'28 days' => 28.days},
- ]
+ @event_separator = '|'
+ @rollup_window = 1.hour
end
- def permission_check(*args, &blk)
+ # Specify the permission check block.
+ #
+ # call-seq:
+ # config.permission_check { redirect_to login_path and return false unless current_user.admin? }
+ #
+ def permission_check(&blk)
if block_given?
Sme::MetricsController.prepend_before_filter(:permission_check)
Sme::MetricsController.send(:define_method, :permission_check, &blk)
@@ -57,12 +57,12 @@ class RollupTest < ActiveRecord::TestCase
expected = Time.parse('2010-05-21 12:00:00 UTC') .. Time.parse('2010-05-21 13:00:00 UTC')
assert_equal expected, Rollup.period_for('2010-05-21 12:50:00 UTC'), 'period_for should handle String parameter'
assert_equal expected, Rollup.period_for(Time.parse('2010-05-21 12:50:00 UTC')), 'period_for should handle Time parameter'
- assert_equal expected, Rollup.period_for('2010-05-21 12:30:00 UTC' .. '2010-05-21 12:50:00 UTC'), 'period_for should handle String Range less than granularity'
- assert_equal expected, Rollup.period_for(Time.parse('2010-05-21 12:30:00 UTC') .. Time.parse('2010-05-21 12:50:00 UTC')), 'period_for should handle Time Range less than granularity'
+ assert_equal expected, Rollup.period_for('2010-05-21 12:30:00 UTC' .. '2010-05-21 12:50:00 UTC'), 'period_for should handle String Range less than rollup_window'
+ assert_equal expected, Rollup.period_for(Time.parse('2010-05-21 12:30:00 UTC') .. Time.parse('2010-05-21 12:50:00 UTC')), 'period_for should handle Time Range less than rollup_window'
expected = Time.parse('2010-05-21 11:00:00 UTC') .. Time.parse('2010-05-21 13:00:00 UTC')
- assert_equal expected, Rollup.period_for('2010-05-21 11:30:00 UTC' .. '2010-05-21 12:50:00 UTC'), 'period_for should handle String Range greater than granularity'
- assert_equal expected, Rollup.period_for(Time.parse('2010-05-21 11:30:00 UTC') .. Time.parse('2010-05-21 12:50:00 UTC')), 'period_for should handle Time Range greater than granularity'
+ assert_equal expected, Rollup.period_for('2010-05-21 11:30:00 UTC' .. '2010-05-21 12:50:00 UTC'), 'period_for should handle String Range greater than rollup_window'
+ assert_equal expected, Rollup.period_for(Time.parse('2010-05-21 11:30:00 UTC') .. Time.parse('2010-05-21 12:50:00 UTC')), 'period_for should handle Time Range greater than rollup_window'
end
test 'round_down' do

0 comments on commit 7f691c3

Please sign in to comment.