Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fixing time logic

  • Loading branch information...
commit 7beb15f8d6783dfdebfe396d95930ad14ab4282d 1 parent dba9321
@winton authored
Showing with 10 additions and 6 deletions.
  1. +4 −3 lib/periodic_counter.rb
  2. +6 −3 spec/periodic_counter_spec.rb
View
7 lib/periodic_counter.rb
@@ -42,22 +42,23 @@ def initialize(environment, root)
records.each do |record|
id = record.delete('id')
data = YAML::load(record["#{column}_data"] || '') || {}
- computed_at = data['computed_at'] || Time.now.utc
count = record.delete(column).to_i
- time_since_compute = Time.now.utc - computed_at
# Set period counters
period.each do |col|
+ computed_at = data["#{col}_at"] || Time.now.utc
duration = column_to_period_integer(col)
+ time_since_compute = Time.now.utc - computed_at
starting_value = data[col].to_i
if (time_since_compute - duration) >= 0
record[col] = count - starting_value
data[col] = count
+ data["#{col}_at"] = Time.now.utc
else
data[col] ||= count
+ data["#{col}_at"] ||= Time.now.utc
end
end
# Update record
- data['computed_at'] = Time.now.utc
record["#{column}_data"] = "'#{YAML::dump(data)}'"
set = record.collect { |col, value| "#{col} = #{value || 0}" }
ActiveRecord::Base.connection.update <<-SQL
View
9 spec/periodic_counter_spec.rb
@@ -13,7 +13,8 @@
start
attributes = Counter.last.attributes
data = attributes.delete('counter_data')
- data.delete('computed_at').to_s.should == Time.now.utc.to_s
+ data.delete('counter_last_day_at').to_s.should == Time.now.utc.to_s
+ data.delete('counter_last_2_days_at').to_s.should == Time.now.utc.to_s
data.should == {
"counter_last_day"=>1,
"counter_last_2_days"=>1
@@ -32,7 +33,8 @@
start
attributes = Counter.last.attributes
data = attributes.delete('counter_data')
- data.delete('computed_at').to_s.should == Time.now.utc.to_s
+ data.delete('counter_last_day_at').to_s.should == Time.now.utc.to_s
+ data.delete('counter_last_2_days_at').to_s.should == (Time.now - 1.day).utc.to_s
data.should == {
"counter_last_day"=>2,
"counter_last_2_days"=>1
@@ -51,7 +53,8 @@
start
attributes = Counter.last.attributes
data = attributes.delete('counter_data')
- data.delete('computed_at').to_s.should == Time.now.utc.to_s
+ data.delete('counter_last_day_at').to_s.should == Time.now.utc.to_s
+ data.delete('counter_last_2_days_at').to_s.should == Time.now.utc.to_s
data.should == {
"counter_last_day"=>3,
"counter_last_2_days"=>3
Please sign in to comment.
Something went wrong with that request. Please try again.