Class: FnordMetric::ToplistWidget

Inherits:
Widget
  • Object
show all
Defined in:
lib/fnordmetric/widgets/toplist_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
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/fnordmetric/widgets/toplist_widget.rb', line 3

def self.execute(namespace, event)
  t = Time.now.to_i

  return false unless event["gauge"]

  resp = if event["cmd"] == "values_for"
    vals = execute_values_for(namespace.gauges[event["gauge"].to_sym], t)

    {
      :cmd => :values_for,
      :gauge => event["gauge"],
      :values => vals,
      :count  => vals.inject(0){ |m, (k,c)| m + c }
    }
  end

  return false unless resp

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

.execute_values_for(gauge, time) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/fnordmetric/widgets/toplist_widget.rb', line 27

def self.execute_values_for(gauge, time)
  gauge.field_values_at(time).sort do |a,b|
    a.first.to_i <=> b.first.to_i
  end.map do |a|
    [a.first, a.second.to_i]
  end
end

Instance Method Details

#dataObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/fnordmetric/widgets/toplist_widget.rb', line 35

def data
  super.merge(
    :gauges => data_gauges,
    :autoupdate => (@opts[:autoupdate] || 0),
    :render_target => @opts[:render_target],
    :ticks => @opts[:ticks],
    :click_callback => @opts[:click_callback],
    :async_chart => true,
    :tick => tick,
    :limit => @opts[:limit],
    :no_min_height => @opts[:no_min_height]
  ).tap do |dat|
    dat.merge!(
      :gauges => @opts[:_gauges]
    ) if dat[:ticks]
  end
end

#data_gaugesObject



53
54
55
56
57
58
59
60
61
62
# File 'lib/fnordmetric/widgets/toplist_widget.rb', line 53

def data_gauges
  Hash.new.tap do |hash|
    gauges.each do |g|
      hash[g.name] = {
        :tick => g.tick,
        :title => g.title
      }
    end
  end
end

#has_tick?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/fnordmetric/widgets/toplist_widget.rb', line 64

def has_tick?
  false
end