Class: FnordMetric::NumbersWidget

Inherits:
Widget
  • Object
show all
Defined in:
lib/fnordmetric/widgets/numbers_widget.rb

Instance Attribute Summary

Attributes inherited from Widget

#gauges, #tick

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Widget

#add_gauges, #default_range, #ensure_has_tick!, #error!, #include_current?, #initialize, #range, #render, #ticks, #title, #token

Constructor Details

This class inherits a constructor from FnordMetric::Widget

Class Method Details

.execute(namespace, event) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
# File 'lib/fnordmetric/widgets/numbers_widget.rb', line 3

def self.execute(namespace, event)
  resp = if event["cmd"] == "values_for"
    execute_values_for(namespace.gauges[event["gauge"].to_sym], event) 
  end

  return false unless resp

  resp.merge(
    :type => "widget_response",
    :widget_key => event["widget_key"]
  )
end

.execute_values_for(gauge, event) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/fnordmetric/widgets/numbers_widget.rb', line 16

def self.execute_values_for(gauge, event)
  unless gauge
    return { "error" => "gauge not found..." }
  end

  _t = Time.now.to_i

  if at = event["at"]
    value = if at =~ /sum\((.+)\)/
      vals = gauge.values_in(FnordMetric::Util.parse_time($1).._t+gauge.tick)
      vals.values.compact.map(&:to_f).inject(&:+)
    elsif at =~ /avg\((.+)\)/
      vals = gauge.values_in(FnordMetric::Util.parse_time($1).._t+gauge.tick)
      (vals.values.compact.map(&:to_f).inject(&:+) || 0) / vals.size.to_f
    else
      gauge.value_at(FnordMetric::Util.parse_time(at)).to_i
    end

    return({
      "cmd" => "values_for",
      "at" => event["at"],
      "gauge" => gauge.name,
      "value" => value })
  end

  values = {}.tap do |out|
    event["offsets"].each do |off|
      if off.to_s.starts_with?("s")
        offset = 0
        span = (gauge.tick * off.to_s[1..-1].to_i)
        values = gauge.values_in((_t-span).._t+gauge.tick)
        value = values.values.compact.map(&:to_i).sum
      else
        offset = off.to_i * gauge.tick
        span = gauge.tick
        value = gauge.value_at(_t-offset)
      end

      out["#{gauge.name}-#{offset}-#{span}"]  = { 
        :value => value,
        :desc  => "$formatOffset(#{offset}, #{span})"
      }
    end
  end

  { "cmd" => "values_for",
    "gauge" => gauge.name,
    "values" => values }
end

Instance Method Details

#dataObject



66
67
68
69
70
71
72
73
74
# File 'lib/fnordmetric/widgets/numbers_widget.rb', line 66

def data
  super.merge(
    :series => gauges.map(&:name),
    :series_titles => gauges.map(&:title),
    :series_units => Hash[gauges.map{ |g| [g.name, g.unit] }],
    :offsets => (@opts[:offsets] || [0, 1, "s30"]),
    :autoupdate => (@opts[:autoupdate] || 60)
  )
end

#has_tick?Boolean

Returns:

  • (Boolean)


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

def has_tick?
  false
end