Class: FnordMetric::Widget

Inherits:
Object
  • Object
show all
Defined in:
lib/fnordmetric/widget.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Widget

Returns a new instance of Widget.



5
6
7
8
9
10
11
12
13
# 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

Instance Attribute Details

#gaugesObject

Returns the value of attribute gauges.



3
4
5
# File 'lib/fnordmetric/widget.rb', line 3

def gauges
  @gauges
end

#tickObject

Returns the value of attribute tick.



3
4
5
# File 'lib/fnordmetric/widget.rb', line 3

def tick
  @tick
end

Instance Method Details

#add_gauges(gauges) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
# 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

#dataObject



66
67
68
69
70
71
72
# 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) ⇒ Object



54
55
56
57
58
59
60
# 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!Object



78
79
80
# File 'lib/fnordmetric/widget.rb', line 78

def ensure_has_tick!
  error! "widget does not have_tick" unless has_tick?
end

#error!(msg) ⇒ Object



39
40
41
# File 'lib/fnordmetric/widget.rb', line 39

def error!(msg)
  FnordMetric.error!(msg)
end

#include_current?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/fnordmetric/widget.rb', line 62

def include_current?
  !(@opts[:include_current] == false)
end

#rangeObject



43
44
45
46
47
# 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

#renderObject



74
75
76
# File 'lib/fnordmetric/widget.rb', line 74

def render
  data
end

#ticksObject



49
50
51
52
# File 'lib/fnordmetric/widget.rb', line 49

def ticks
  ensure_has_tick!
  range.step(@tick)
end

#titleObject



15
16
17
# File 'lib/fnordmetric/widget.rb', line 15

def title
  @opts[:title]
end

#tokenObject



19
20
21
22
23
# 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