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

Version Path
fnordmetric-0.7.5 lib/fnordmetric/metric_api.rb
fnordmetric-0.7.4 lib/fnordmetric/metric_api.rb
fnordmetric-0.7.3 lib/fnordmetric/metric_api.rb
fnordmetric-0.7.1 lib/fnordmetric/metric_api.rb
fnordmetric-0.7.0 lib/fnordmetric/metric_api.rb
fnordmetric-0.6.6 lib/fnordmetric/metric_api.rb
fnordmetric-0.6.5 lib/fnordmetric/metric_api.rb
fnordmetric-0.6.4 lib/fnordmetric/metric_api.rb
fnordmetric-0.6.3 lib/fnordmetric/metric_api.rb
fnordmetric-0.6.2 lib/fnordmetric/metric_api.rb
fnordmetric-0.6.1 lib/fnordmetric/metric_api.rb
fnordmetric-0.6.0 lib/fnordmetric/metric_api.rb
fnordmetric-0.5.9 lib/fnordmetric/metric_api.rb
fnordmetric-0.5.8 lib/fnordmetric/metric_api.rb
fnordmetric-0.5.7 lib/fnordmetric/metric_api.rb
fnordmetric-0.5.6 lib/fnordmetric/metric_api.rb
fnordmetric-0.5.5 lib/fnordmetric/metric_api.rb
fnordmetric-0.5.4 lib/fnordmetric/metric_api.rb
fnordmetric-0.5.3 lib/fnordmetric/metric_api.rb
fnordmetric-0.5.2 lib/fnordmetric/metric_api.rb