class FnordMetric::Widget

Attributes

gauges[RW]
tick[RW]

Public Class Methods

new(opts={}) click to toggle source
# File lib/fnordmetric/widget.rb, line 5
def initialize(opts={})
  @opts = opts

  unless opts.has_key?(:title)
    error! "widget can't be initialized without a title"
  end

  add_gauges(opts.delete(:gauges))
end

Public Instance Methods

add_gauges(gauges) click to toggle source
# File lib/fnordmetric/widget.rb, line 25
def add_gauges(gauges)
  if gauges.blank? && has_tick?
    error! "initializing a widget without gauges is void"
  else
    @gauges = gauges
  end
  
  if (ticks = gauges.map{ |g| g.tick }).uniq.length == 1
    @tick = ticks.first
  elsif !!self.try(:has_tick?)
    error! "you can't add gauges with different ticks to the same widget"
  end
end
data() click to toggle source
# File lib/fnordmetric/widget.rb, line 66
def data
  { 
    :title => @opts[:title], 
    :width => @opts[:width] || 100,
    :klass => self.class.name.split("::").last 
  }
end
default_range(now=Time.now) click to toggle source
# File lib/fnordmetric/widget.rb, line 54
def default_range(now=Time.now)
  ensure_has_tick!
  te = gauges.first.tick_at(now.to_i)
  te -= @tick unless include_current?
  rs = (@opts[:ticks] || (@tick == 1.hour.to_i ? 24 : 30)).to_i
  (te-(@tick*rs)..te)
end
ensure_has_tick!() click to toggle source
# File lib/fnordmetric/widget.rb, line 78
def ensure_has_tick!
  error! "widget does not have_tick" unless has_tick?
end
error!(msg) click to toggle source
# File lib/fnordmetric/widget.rb, line 39
def error!(msg)
  FnordMetric.error!(msg)
end
include_current?() click to toggle source
# File lib/fnordmetric/widget.rb, line 62
def include_current?
  !(@opts[:include_current] == false)
end
range() click to toggle source
# File lib/fnordmetric/widget.rb, line 43
def range
  ensure_has_tick!
  #@opts[:range] || default_range # FIXME: allow custom ranges, but assure that the range-start is 'on a tick'
  default_range
end
render() click to toggle source
# File lib/fnordmetric/widget.rb, line 74
def render
  data
end
ticks() click to toggle source
# File lib/fnordmetric/widget.rb, line 49
def ticks
  ensure_has_tick!
  range.step(@tick)
end
title() click to toggle source
# File lib/fnordmetric/widget.rb, line 15
def title
  @opts[:title]
end
token() click to toggle source
# File lib/fnordmetric/widget.rb, line 19
def token
  token = title.to_s.gsub(/[\W]/, '').downcase
  token = Digest::SHA1.hexdigest(title.to_s) if token.empty?
  token
end