Sha256: 06587ca824fc14f30d057ecff3c7d9eae6a25141fc815185048d85f24d3c0ef3
Contents?: true
Size: 1.04 KB
Versions: 23
Compression:
Stored size: 1.04 KB
Contents
class FnordMetric::MetricAPI def initialize(params) @params = params.to_options @metric = FnordMetric.metrics.to_options[params[:name].to_sym] end def render return {:error => "metric not found"}.to_json unless @metric data = if @params[:at] && @params[:at] =~ /^[0-9]+$/ { :value => @metric.at(Time.at(@params[:at].to_i)) } elsif @params[:at] && @params[:at] =~ /^([0-9]+)-([0-9]+)$/ && @params[:tick] { :values => tick_data } elsif @params[:at] && @params[:at] =~ /^([0-9]+)-([0-9]+)$/ { :value => range_data } else { :value => @metric.at(Time.now) } end data.to_json end private def range_data start_ts, end_ts = @params[:at].split("-").map(&:to_i) @metric.at(start_ts..end_ts) end def tick_data(tick_length=@params[:tick].to_i) start_ts, end_ts = @params[:at].split("-").map(&:to_i) ticks = (start_ts..end_ts).step(tick_length) ticks.map do |tick| [tick, @metric.at(@params[:delta] ? (tick..tick+tick_length) : tick)] end end end
Version data entries
23 entries across 23 versions & 1 rubygems