lib/trackoid/readers.rb in trackoid-0.2.0 vs lib/trackoid/readers.rb in trackoid-0.3.0
- old
+ new
@@ -4,60 +4,72 @@
# Reader methods (previously known as "accessors")
module Readers
# Access methods
def today
- data_for(Date.today)
+ whole_data_for(Time.now)
end
def yesterday
- data_for(Date.today - 1)
+ whole_data_for(Time.now - 1.day)
end
def first_value
data_for(first_date)
end
def last_value
data_for(last_date)
end
-
+
def last_days(how_much = 7)
return [today] unless how_much > 0
- date, values = Date.today, []
- (date - how_much.abs + 1).step(date) {|d| values << data_for(d) }
- values
+ now, hmd = Time.now, (how_much - 1)
+ on( now.ago(hmd.days)..now )
end
def on(date)
- return date.collect {|d| data_for(d)} if date.is_a?(Range)
- data_for(date)
+ return date.collect {|d| whole_data_for(d)} if date.is_a?(Range)
+ whole_data_for(date)
end
def all_values
on(first_date..last_date) if first_date
end
# Utility methods
def first_date
- # We are guaranteed _m and _d to exists unless @data is a malformed
- # hash, so we need to do this nasty "return nil", sorry...
- # TODO: I'm open to change this to a cleaner algorithm :-)
- return nil unless _y = @data.keys.min
- return nil unless _m = @data[_y].keys.min
- return nil unless _d = @data[_y][_m].keys.min
- Date.new(_y.to_i, _m.to_i, _d.to_i)
+ date_cleanup
+ return nil unless _ts = @data.keys.min
+ return nil unless _h = @data[_ts].keys.min
+ Time.from_key(_ts, _h)
end
-
+
def last_date
- # We are guaranteed _m and _d to exists unless @data is a malformed
- # hash, so we need to do this nasty "return nil", sorry...
- # TODO: I'm open to change this to a cleaner algorithm :-)
- return nil unless _y = @data.keys.max
- return nil unless _m = @data[_y].keys.max
- return nil unless _d = @data[_y][_m].keys.max
- Date.new(_y.to_i, _m.to_i, _d.to_i)
+ date_cleanup
+ return nil unless _ts = @data.keys.max
+ return nil unless _h = @data[_ts].keys.max
+ Time.from_key(_ts, _h)
end
+ # We need the cleanup method only for methods who rely on date indexes
+ # to be valid (well formed) like first/last_date. This is because
+ # Mongo update operations cleans up the last key, which in our case
+ # left the array in an inconsistent state.
+ #
+ # Example:
+ # Before update:
+ #
+ # { :visits_data => {"14803" => {"22" => 1} } }
+ #
+ # After updating with: {"$unset"=>{"visits_data.14803.22"=>1}
+ #
+ # { :visits_data => {"14803" => {} } }
+ #
+ # We can NOT retrieve the first date with visits_data.keys.min
+ #
+ def date_cleanup
+ @data.reject! {|k,v| v.count == 0}
+ end
end
end
end