Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

refactor period to not care about counter keys

  • Loading branch information...
commit 6af463a93964e29b2ee075a30297bb2ecc721c95 1 parent 8fefeee
@blahed authored
Showing with 117 additions and 74 deletions.
  1. +30 −31 lib/von/period.rb
  2. +87 −43 test/period_test.rb
View
61 lib/von/period.rb
@@ -1,7 +1,6 @@
module Von
class Period
- AVAILABLE_PERIODS = [ :minutely, :hourly, :daily, :weekly, :monthly, :yearly ]
- TIME_UNITS = {
+ PERIOD_MAPPING = {
:minutely => :minute,
:hourly => :hour,
:daily => :day,
@@ -9,25 +8,30 @@ class Period
:monthly => :month,
:yearly => :year
}
- TIME_PERIODS = TIME_UNITS.invert
-
+ AVAILABLE_PERIODS = PERIOD_MAPPING.keys
+ AVAILABLE_TIME_UNITS = PERIOD_MAPPING.values
+
attr_reader :period
- attr_reader :counter_key
attr_reader :length
attr_reader :format
# Initialize a Period object
#
- # counter - the field name for the counter
# period - the time period one of AVAILABLE_PERIODS
# length - length of period
- def initialize(counter_key, period, length = nil)
- @counter_key = counter_key
- @period = period.to_sym
- @length = length
- @format = Von.config.send(:"#{@period}_format")
+ def initialize(period, length = nil)
+ period = period.to_sym
+ if AVAILABLE_PERIODS.include?(period)
+ @period = period
+ elsif AVAILABLE_TIME_UNITS.include?(period)
+ @period = PERIOD_MAPPING.invert[period]
+ else
+ raise ArgumentError, "`#{period}' is not a valid period"
+ end
+ @length = length
+ @format = Von.config.send(:"#{@period}_format")
end
-
+
def to_s
@period.to_s
end
@@ -35,7 +39,7 @@ def to_s
# Returns a Symbol representing the time unit
# for the current period.
def time_unit
- @time_unit ||= TIME_UNITS[@period]
+ @time_unit ||= PERIOD_MAPPING[@period]
end
# Returns True or False if the period is hourly
@@ -43,31 +47,26 @@ def hours?
@period == :hourly
end
- def now
- # TODO: this may break minutes
- DateTime.now.beginning_of_hour
- end
-
- def prev
- hours? ? now.ago(3600) : now.send(:"prev_#{_period.time_unit}")
+ # Returns True or False if the period is minutely
+ def minutes?
+ @period == :minutely
end
- # Returns the Redis hash key used for storing counts for this Period
- def hash_key
- @hash ||= "#{Von.config.namespace}:counters:#{@counter_key}:#{@period}"
+ def beginning(time)
+ if minutes?
+ time.change(:seconds => 0)
+ else
+ time.send(:"beginning_of_#{time_unit}")
+ end
end
- # Returns the Redis list key used for storing current "active" counters
- def list_key
- @list ||= "#{Von.config.namespace}:lists:#{@counter_key}:#{@period}"
+ def prev
+ beginning(1.send(time_unit.to_sym).ago).strftime(@format)
end
- # Returns the Redis field representation used for storing the count value
- # TODO: rename this
- def field
- Time.now.strftime(format)
+ def timestamp
+ beginning(Time.now).strftime(format)
end
- alias :timestamp :field
def self.exists?(period)
AVAILABLE_PERIODS.include?(period)
View
130 test/period_test.rb
@@ -1,69 +1,113 @@
+require 'test_helper'
+
describe Von::Period do
Period = Von::Period
before :each do
@config = Von::Config
@config.init!
+ Timecop.freeze(Time.local(2013, 01, 02, 03, 04))
end
- it "intiializes given a counter, period, and length" do
- period = Period.new('foo', :monthly, 6)
- period.counter_key.must_equal 'foo'
- period.length.must_equal 6
+ it "intiializes given a period" do
+ period = Period.new(:monthly)
+ period.to_s.must_equal 'monthly'
+ period.length.must_be_nil
period.format.must_equal '%Y-%m'
end
- it "checks if the period is an hourly period" do
- Period.new('foo', :minutely, 6).wont_be :hours?
- Period.new('foo', :hourly, 6).must_be :hours?
- Period.new('foo', :daily, 6).wont_be :hours?
- Period.new('foo', :weekly, 6).wont_be :hours?
- Period.new('foo', :monthly, 6).wont_be :hours?
- Period.new('foo', :yearly, 6).wont_be :hours?
+ it "intiializes given a time unit" do
+ period = Period.new(:month)
+ period.to_s.must_equal 'monthly'
+ period.length.must_be_nil
+ period.format.must_equal '%Y-%m'
end
- it "knows what time unit it is" do
- Period.new('foo', :minutely, 6).time_unit.must_equal :minute
- Period.new('foo', :hourly, 6).time_unit.must_equal :hour
- Period.new('foo', :daily, 6).time_unit.must_equal :day
- Period.new('foo', :weekly, 6).time_unit.must_equal :week
- Period.new('foo', :monthly, 6).time_unit.must_equal :month
- Period.new('foo', :yearly, 6).time_unit.must_equal :year
+ it "intiializes given a period and length" do
+ period = Period.new(:monthly, 3)
+ period.to_s.must_equal 'monthly'
+ period.length.must_equal 3
+ period.format.must_equal '%Y-%m'
end
- it "pulls a time format from config options" do
- Period.new('foo', :minutely, 6).format.must_equal Von.config.minutely_format
- Period.new('foo', :hourly, 6).format.must_equal Von.config.hourly_format
- Period.new('foo', :daily, 6).format.must_equal Von.config.daily_format
- Period.new('foo', :weekly, 6).format.must_equal Von.config.weekly_format
- Period.new('foo', :monthly, 6).format.must_equal Von.config.monthly_format
- Period.new('foo', :yearly, 6).format.must_equal Von.config.yearly_format
+ it "generates a timestamp for now" do
+ Period.new(:minutely).timestamp.must_equal '2013-01-02 03:04'
+ Period.new(:hourly).timestamp.must_equal '2013-01-02 03:00'
+ Period.new(:daily).timestamp.must_equal '2013-01-02'
+ Period.new(:weekly).timestamp.must_equal '2012-12-31'
+ Period.new(:monthly).timestamp.must_equal '2013-01'
+ Period.new(:yearly).timestamp.must_equal '2013'
end
- it "builds a redis hash key string" do
- field = 'foo'
- period = :hourly
- period_obj = Period.new(field, period, 6)
+ it "knows the prev time period" do
+ Period.new(:minutely).prev.must_equal '2013-01-02 03:03'
+ Period.new(:hourly).prev.must_equal '2013-01-02 02:00'
+ Period.new(:daily).prev.must_equal '2013-01-01'
+ Period.new(:weekly).prev.must_equal '2012-12-24'
+ Period.new(:monthly).prev.must_equal '2012-12'
+ Period.new(:yearly).prev.must_equal '2012'
+ end
- period_obj.hash_key.must_equal "#{@config.namespace}:counters:#{field}:#{period}"
+ it "checks if the period is an hourly period" do
+ Period.new(:minutely).wont_be :hours?
+ Period.new(:hourly).must_be :hours?
+ Period.new(:daily).wont_be :hours?
+ Period.new(:weekly).wont_be :hours?
+ Period.new(:monthly).wont_be :hours?
+ Period.new(:yearly).wont_be :hours?
end
- it "builds a redis list key string" do
- field = 'foo'
- period = :hourly
- period_obj = Period.new(field, period, 6)
+ it "checks if the period is an hourly period" do
+ Period.new(:minutely).must_be :minutes?
+ Period.new(:hourly).wont_be :minutes?
+ Period.new(:daily).wont_be :minutes?
+ Period.new(:weekly).wont_be :minutes?
+ Period.new(:monthly).wont_be :minutes?
+ Period.new(:yearly).wont_be :minutes?
+ end
- period_obj.list_key.must_equal "#{@config.namespace}:lists:#{field}:#{period}"
+ it "knows what time unit it is" do
+ Period.new(:minutely).time_unit.must_equal :minute
+ Period.new(:hourly).time_unit.must_equal :hour
+ Period.new(:daily).time_unit.must_equal :day
+ Period.new(:weekly).time_unit.must_equal :week
+ Period.new(:monthly).time_unit.must_equal :month
+ Period.new(:yearly).time_unit.must_equal :year
end
- it "builds a redis field for the given period and current time" do
- Timecop.freeze(Time.local(2013, 02, 01, 05, 15))
- Period.new('foo', :minutely, 6).field.must_equal '2013-02-01 05:15'
- Period.new('foo', :hourly, 6).field.must_equal '2013-02-01 05:00'
- Period.new('foo', :daily, 6).field.must_equal '2013-02-01'
- Period.new('foo', :weekly, 6).field.must_equal '2013-02-01'
- Period.new('foo', :monthly, 6).field.must_equal '2013-02'
- Period.new('foo', :yearly, 6).field.must_equal '2013'
+ it "gets a time format from config" do
+ Period.new(:minutely).format.must_equal Von.config.minutely_format
+ Period.new(:hourly).format.must_equal Von.config.hourly_format
+ Period.new(:daily).format.must_equal Von.config.daily_format
+ Period.new(:weekly).format.must_equal Von.config.weekly_format
+ Period.new(:monthly).format.must_equal Von.config.monthly_format
+ Period.new(:yearly).format.must_equal Von.config.yearly_format
end
+ # it "builds a redis hash key string" do
+ # field = 'foo'
+ # period = :hourly
+ # period_obj = Period.new(field, period, 6)
+ #
+ # period_obj.hash_key.must_equal "#{@config.namespace}:counters:#{field}:#{period}"
+ # end
+ #
+ # it "builds a redis list key string" do
+ # field = 'foo'
+ # period = :hourly
+ # period_obj = Period.new(field, period, 6)
+ #
+ # period_obj.list_key.must_equal "#{@config.namespace}:lists:#{field}:#{period}"
+ # end
+ #
+ # it "builds a redis field for the given period and current time" do
+ # Timecop.freeze(Time.local(2013, 02, 01, 05, 15))
+ # Period.new('foo', :minutely, 6).field.must_equal '2013-02-01 05:15'
+ # Period.new('foo', :hourly, 6).field.must_equal '2013-02-01 05:00'
+ # Period.new('foo', :daily, 6).field.must_equal '2013-02-01'
+ # Period.new('foo', :weekly, 6).field.must_equal '2013-01-28'
+ # Period.new('foo', :monthly, 6).field.must_equal '2013-02'
+ # Period.new('foo', :yearly, 6).field.must_equal '2013'
+ # end
+
end
Please sign in to comment.
Something went wrong with that request. Please try again.