Class: FnordMetric::Widget

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

Direct Known Subclasses

FunnelWidget, NumbersWidget, TimelineWidget

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ 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



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/fnordmetric/widget.rb', line 23

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
  else
    error! "you can't add gauges with different ticks to the same widget"
  end
end

#dataObject



64
65
66
67
68
69
70
# File 'lib/fnordmetric/widget.rb', line 64

def data
  { 
    :title => @opts[:title], 
    :width => @opts[:width] || 100,
    :klass => self.class.name.split("::").last 
  }
end

#default_range(now = Time.now) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/fnordmetric/widget.rb', line 52

def default_range(now=Time.now)
  ensure_has_tick!
  te = gauges.first.tick_at(now.to_i)
  te += @tick if include_current?
  rs = @tick == 1.hour.to_i ? 24 : 30
  (te-(@tick*rs)..te)
end

#ensure_has_tick!Object



76
77
78
# File 'lib/fnordmetric/widget.rb', line 76

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

#error!(msg) ⇒ Object



37
38
39
# File 'lib/fnordmetric/widget.rb', line 37

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

#include_current?Boolean



60
61
62
# File 'lib/fnordmetric/widget.rb', line 60

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

#rangeObject



41
42
43
44
45
# File 'lib/fnordmetric/widget.rb', line 41

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



72
73
74
# File 'lib/fnordmetric/widget.rb', line 72

def render
  data
end

#ticksObject



47
48
49
50
# File 'lib/fnordmetric/widget.rb', line 47

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
# File 'lib/fnordmetric/widget.rb', line 19

def token
  title.to_s.gsub(/[\W]/, '').downcase
end