Skip to content

Instantly share code, notes, and snippets.

@eric
Created February 24, 2012 01:34
Show Gist options
  • Save eric/1896568 to your computer and use it in GitHub Desktop.
Save eric/1896568 to your computer and use it in GitHub Desktop.
require 'lazy'
require 'system_timer'
class LibratoMetric
COUNT = 10
RESOLUTION = 1
def initialize(metric_name, timeout = 5)
@metric_name = metric_name
@timeout = timeout
# Start loading data
data
end
def data
@data ||= Lazy.future do
with_timeout { Librato::Metrics.fetch(@metric_name, :count => COUNT, :resolution => RESOLUTION, :summarize_time => true) }
end
end
def value
@value ||= Lazy.demand(data).sum { |source, data| data[0]['value'] }
end
def time
measure_time + COUNT.minutes
end
def measure_time
@measure_time ||= Time.at(Lazy.demand(data).collect { |source, data| data[0]['measure_time'] }.min)
end
def with_timeout(&block)
SystemTimer.timeout_after(@timeout, Timeout::Error, &block)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment