Sha256: 93fe8b5da9016933b466bf3a82eea3e256f196eeb8cca7bb93fbe1021d4fb248

Contents?: true

Size: 1.89 KB

Versions: 5

Compression:

Stored size: 1.89 KB

Contents

module Tabs
  module Metrics
    class Task
      class Token < String
        include Storage

        attr_reader :key

        def initialize(token, key)
          @key = key
          super(token)
        end

        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(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
          Tabs::RESOLUTIONS.each { |res| record_complete(res, complete_time) }
        end

        def time_elapsed(resolution)
          Tabs::Resolution.from_seconds(resolution, complete_time - start_time)
        end

        private

        def record_start(resolution, timestamp)
          formatted_time = Tabs::Resolution.serialize(resolution, timestamp)
          sadd("stat:task:#{key}:started:#{formatted_time}", self)
        end

        def record_complete(resolution, timestamp)
          formatted_time = Tabs::Resolution.serialize(resolution, timestamp)
          sadd("stat:task:#{key}:completed:#{formatted_time}", self)
        end

        def start_time=(timestamp)
          set("stat:task:#{key}:#{self}:started_time", timestamp)
          @start_time = timestamp
        end

        def start_time
          @start_time ||= Time.parse(get("stat:task:#{key}:#{self}:started_time"))
        end

        def complete_time=(timestamp)
          set("stat:task:#{key}:#{self}:completed_time", timestamp)
          @complete_time = timestamp
        end

        def complete_time
          @complete_time ||= Time.parse(get("stat:task:#{key}:#{self}:completed_time"))
        end

      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
tabs-0.8.2 lib/tabs/metrics/task/token.rb
tabs-0.8.1 lib/tabs/metrics/task/token.rb
tabs-0.8.0 lib/tabs/metrics/task/token.rb
tabs-0.7.1 lib/tabs/metrics/task/token.rb
tabs-0.7.0 lib/tabs/metrics/task/token.rb