Permalink
Browse files

Version 0.1.4

Support for counter_4_days_ago syntax
  • Loading branch information...
1 parent ee192d5 commit 3cb64504c1be82d8ed3cc8ef4886e9af0e6ec78d @winton committed Jun 22, 2010
Showing with 73 additions and 21 deletions.
  1. +3 −2 README.markdown
  2. +38 −14 lib/periodic_counter.rb
  3. +1 −1 require.rb
  4. +2 −0 spec/db/migrate/001_counters.rb
  5. +29 −4 spec/periodic_counter_spec.rb
View
@@ -21,9 +21,10 @@ Add your periodic counters (all integers):
* <code>X\_last\_week</code>
* <code>X\_last\_6\_hours</code>
-* <code>X\_last_sunday</code>
+* <code>X\_last\_sunday</code>
+* <code>X\_2\_days\_ago</code>
-Currently only days of the week and [ActiveSupport's time extensions](http://api.rubyonrails.org/classes/ActiveSupport/CoreExtensions/Numeric/Time.html) are supported for text after <code>X_last</code>. If no digit is present, "1" is assumed.
+Currently only days of the week and [ActiveSupport's time extensions](http://api.rubyonrails.org/classes/ActiveSupport/CoreExtensions/Numeric/Time.html) are supported for time periods. If no digit is present, "1" is assumed.
Create configuration
--------------------
@@ -31,7 +31,7 @@ def initialize(environment, root)
# Find period columns
period = columns.select do |col|
col =~ /^#{column}/ &&
- col =~ /_last_/
+ (col =~ /_last_/ || col =~ /_ago$/)
end
# Grab all records
select_columns = [ 'id', column, "#{column}_data" ]
@@ -54,25 +54,41 @@ def initialize(environment, root)
data["#{col}_before_today"] = count
end
else
- computed_at = data["#{col}_at"] || Time.now.utc
- duration = column_to_period_integer(col)
- time_since_compute = Time.now.utc - computed_at
- last_day =
- if col.include?('day')
+ computed_at = data["#{col}_at"]
+ last_time =
+ if col.include?('minute')
+ self.class.this_minute
+ elsif col.include?('hour')
+ self.class.this_hour
+ elsif col.include?('day')
self.class.today
elsif col.include?('week')
self.class.last_monday
elsif col.include?('month')
self.class.first_of_the_month
end
- if (time_since_compute - duration) >= 0
- data[col] = count
- data["#{col}_at"] = last_day
+ if col =~ /_ago$/
+ duration = column_to_period_integer(col, -3, -2)
+ if !computed_at || last_time == computed_at
+ data[col] = count
+ data["#{col}_at"] = last_time
+ end
+ if computed_at && (Time.now.utc - computed_at - duration) >= 0
+ record[col] = data[col]
+ data[col] = count
+ data["#{col}_at"] = last_time
+ end
else
- data[col] ||= count
- data["#{col}_at"] ||= last_day
+ duration = column_to_period_integer(col, -2, -1)
+ if !computed_at || (Time.now.utc - computed_at - duration) >= 0
+ data[col] = count
+ data["#{col}_at"] = last_time
+ else
+ data[col] ||= count
+ data["#{col}_at"] ||= last_time
+ end
+ record[col] = count - data[col].to_i
end
- record[col] = count - data[col].to_i
end
end
# Update record
@@ -90,8 +106,8 @@ def initialize(environment, root)
end
end
- def column_to_period_integer(column)
- column = column.split('_')[-2..-1]
+ def column_to_period_integer(column, from, to)
+ column = column.split('_')[from..to]
column[0] = column[0].to_i
if column[0] == 0
column[0] = 1
@@ -114,6 +130,14 @@ def last_monday(now=Time.now.utc.to_date)
end
Date.new(now.year, now.month, now.day + diff).to_time(:utc)
end
+
+ def this_hour(now=Time.now.utc)
+ now - (now.min * 60) - now.sec
+ end
+
+ def this_minute(now=Time.now.utc)
+ now - now.sec
+ end
def today(now=Time.now.utc.to_date)
Date.new(now.year, now.month, now.day).to_time(:utc)
View
@@ -18,7 +18,7 @@
name 'periodic_counter'
homepage "http://github.com/winton/#{name}"
summary "Maintains period fields on any counter column in your database"
- version '0.1.3'
+ version '0.1.4'
end
bin { require 'lib/periodic_counter' }
@@ -2,6 +2,8 @@ class Counters < ActiveRecord::Migration
def self.up
create_table :counters do |t|
t.integer :counter
+ t.integer :counter_1_day_ago
+ t.integer :counter_2_days_ago
t.integer :counter_last_day
t.integer :counter_last_2_days
t.integer :counter_last_monday
@@ -6,17 +6,21 @@
$db.migrate(1)
$db.migrate(0)
$db.migrate(1)
- stub_time(PeriodicCounter.last_monday)
create_counter
end
it "should set up data and increment last_monday (today)" do
+ stub_time(PeriodicCounter.last_monday)
start
- attributes = Counter.last.attributes
+ attributes = Counter.last.reload.attributes
data = attributes.delete('counter_data')
+ data.delete('counter_1_day_ago_at').to_s.should == PeriodicCounter.today.to_s
+ data.delete('counter_2_days_ago_at').to_s.should == PeriodicCounter.today.to_s
data.delete('counter_last_day_at').to_s.should == PeriodicCounter.today.to_s
data.delete('counter_last_2_days_at').to_s.should == PeriodicCounter.today.to_s
data.should == {
+ "counter_1_day_ago"=>1,
+ "counter_2_days_ago"=>1,
"counter_last_day"=>1,
"counter_last_2_days"=>1,
"counter_last_monday_before_today"=>0,
@@ -26,6 +30,8 @@
attributes.should == {
"id"=>1,
"counter"=>1,
+ "counter_1_day_ago"=>0,
+ "counter_2_days_ago"=>0,
"counter_last_day"=>0,
"counter_last_2_days"=>0,
"counter_last_monday"=>1,
@@ -35,13 +41,18 @@
end
it "should add to last_day, last_2_days, and last_monday counters on increment" do
+ stub_time(PeriodicCounter.last_monday)
Counter.last.update_attribute :counter, 2
start
attributes = Counter.last.attributes
data = attributes.delete('counter_data')
+ data.delete('counter_1_day_ago_at').to_s.should == PeriodicCounter.today.to_s
+ data.delete('counter_2_days_ago_at').to_s.should == PeriodicCounter.today.to_s
data.delete('counter_last_day_at').to_s.should == PeriodicCounter.today.to_s
data.delete('counter_last_2_days_at').to_s.should == PeriodicCounter.today.to_s
data.should == {
+ "counter_1_day_ago"=>2,
+ "counter_2_days_ago"=>2,
"counter_last_day"=>1,
"counter_last_2_days"=>1,
"counter_last_monday_before_today"=>0,
@@ -51,6 +62,8 @@
attributes.should == {
"id"=>1,
"counter"=>2,
+ "counter_1_day_ago"=>0,
+ "counter_2_days_ago"=>0,
"counter_last_day"=>1,
"counter_last_2_days"=>1,
"counter_last_monday"=>2,
@@ -61,13 +74,17 @@
it "should reset counter_last_day and increment last_tuesday" do
Counter.last.update_attribute :counter, 3
- stub_time(Time.now + 1.day) # Tuesday
+ stub_time(PeriodicCounter.last_monday + 1.day) # Tuesday
start
attributes = Counter.last.attributes
data = attributes.delete('counter_data')
+ data.delete('counter_1_day_ago_at').to_s.should == PeriodicCounter.today.to_s
+ data.delete('counter_2_days_ago_at').to_s.should == (PeriodicCounter.today - 1.day).to_s
data.delete('counter_last_day_at').to_s.should == PeriodicCounter.today.to_s
data.delete('counter_last_2_days_at').to_s.should == (PeriodicCounter.today - 1.day).to_s
data.should == {
+ "counter_1_day_ago"=>3,
+ "counter_2_days_ago"=>2,
"counter_last_day"=>3,
"counter_last_2_days"=>1,
"counter_last_monday_before_today"=>3,
@@ -77,6 +94,8 @@
attributes.should == {
"id"=>1,
"counter"=>3,
+ "counter_1_day_ago"=>2,
+ "counter_2_days_ago"=>0,
"counter_last_day"=>0,
"counter_last_2_days"=>2,
"counter_last_monday"=>2,
@@ -87,13 +106,17 @@
it "should reset last_2_days and not touch last_wednesday" do
Counter.last.update_attribute :counter, 4
- stub_time(Time.now + 2.days) # Thursday
+ stub_time(PeriodicCounter.last_monday + 3.days) # Thursday
start
attributes = Counter.last.attributes
data = attributes.delete('counter_data')
+ data.delete('counter_1_day_ago_at').to_s.should == PeriodicCounter.today.to_s
+ data.delete('counter_2_days_ago_at').to_s.should == PeriodicCounter.today.to_s
data.delete('counter_last_day_at').to_s.should == PeriodicCounter.today.to_s
data.delete('counter_last_2_days_at').to_s.should == PeriodicCounter.today.to_s
data.should == {
+ "counter_1_day_ago"=>4,
+ "counter_2_days_ago"=>4,
"counter_last_day"=>4,
"counter_last_2_days"=>4,
"counter_last_monday_before_today"=>4,
@@ -103,6 +126,8 @@
attributes.should == {
"id"=>1,
"counter"=>4,
+ "counter_1_day_ago"=>3,
+ "counter_2_days_ago"=>2,
"counter_last_day"=>0,
"counter_last_2_days"=>0,
"counter_last_monday"=>2,

0 comments on commit 3cb6450

Please sign in to comment.