lib/tabs/metrics/task/token.rb in tabs-0.6.3 vs lib/tabs/metrics/task/token.rb in tabs-0.7.0

- old
+ new

@@ -2,42 +2,27 @@ module Metrics class Task class Token < String include Storage - def self.keys_for_range(key, range, resolution, type) - range.map do |date| - formatted_time = Tabs::Resolution.serialize(resolution, date) - "stat:task:#{key}:#{type}:#{formatted_time}" - end - end - attr_reader :key def initialize(token, key) @key = key super(token) end - def start_key - "stat:started:#{key}:#{self}" - end - - def end_key - "stat:completed:#{key}:#{self}" - end - - def start - self.start_time = Time.now.utc + def start(timestamp=Time.now) + self.start_time = timestamp.utc sadd("stat:task:#{key}:tokens", self) Tabs::RESOLUTIONS.each { |res| record_start(res, start_time) } end - def complete + def complete(timestamp=Time.now) + self.complete_time = timestamp.utc unless sismember("stat:task:#{key}:tokens", self) raise UnstartedTaskMetricError.new("No task for metric '#{key}' was started with token '#{self}'") end - self.complete_time = Time.now.utc Tabs::RESOLUTIONS.each { |res| record_complete(res, complete_time) } end def time_elapsed(resolution) Tabs::Resolution.from_seconds(resolution, complete_time - start_time)