Class: Naplug::PerformanceData

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/naplug/performancedata.rb

Constant Summary collapse

FIELDS =
[:label, :value, :uom, :warn, :crit, :min, :max]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(plugin) ⇒ PerformanceData

Returns a new instance of PerformanceData.



13
14
15
16
17
# File 'lib/naplug/performancedata.rb', line 13

def initialize(plugin)
  @tag = plugin.tag
  @data = Hash.new
  @meta = OpenStruct.new :plugin => plugin, :ancestors => traverse_to_root(plugin)
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



9
10
11
# File 'lib/naplug/performancedata.rb', line 9

def data
  @data
end

#metaObject (readonly)

Returns the value of attribute meta.



9
10
11
# File 'lib/naplug/performancedata.rb', line 9

def meta
  @meta
end

#tagObject (readonly)

Returns the value of attribute tag.



9
10
11
# File 'lib/naplug/performancedata.rb', line 9

def tag
  @tag
end

Instance Method Details

#[](label) ⇒ Object



47
48
49
# File 'lib/naplug/performancedata.rb', line 47

def [](label)
  @data[curate_label(label)]
end

#[]=(label, valuefields) ⇒ Object

Raises:

  • Naplug::Error if the label contains invalid characters, the value is not a number, or specify invalid fields



34
35
36
37
38
39
40
41
# File 'lib/naplug/performancedata.rb', line 34

def []=(label,valuefields)
  value, fields = valuefields
  if validate_label label and validate_value value and validate_fields fields
    @data[curate_label(label)] = { :label => curate_label(label), :value => value }.merge fields
  else
    raise Naplug::Error, "invalid performance data label (#{label}), value (#{value}), field representation (#{fields.class}) or field (#{fields.keys.join(',')})"
  end
end

#ancestors(options = { :mode => :tags, :separator => :/ }) ⇒ Array<Plugin>

Returns of parent plugins.

Returns:

  • (Array<Plugin>)

    of parent plugins



75
76
77
78
# File 'lib/naplug/performancedata.rb', line 75

def ancestors(options = { :mode => :tags, :separator => :/ })
  options[:separator] = :/ if options[:separator].nil?
  options[:mode].nil? ? @meta.ancestors : @meta.ancestors.map { |a| a.tag }.join(options[:separator].to_s)
end

#delete(label) ⇒ Object



51
52
53
# File 'lib/naplug/performancedata.rb', line 51

def delete(label)
  @data.delete(curate_label(label))
end

#each(&block) ⇒ Object



43
44
45
# File 'lib/naplug/performancedata.rb', line 43

def each(&block)
  @data.values.each(&block)
end

#fieldsObject



65
66
67
# File 'lib/naplug/performancedata.rb', line 65

def fields
  FIELDS
end

#include?(label) ⇒ Boolean Also known as: has_label?

Returns:

  • (Boolean)


55
56
57
# File 'lib/naplug/performancedata.rb', line 55

def include?(label)
  @data.has_key? curate_label(label)
end

#keysObject Also known as: labels



60
61
62
# File 'lib/naplug/performancedata.rb', line 60

def keys
  @data.keys
end

#pluginPlugin

Returns plugin performance data belongs to.

Returns:

  • (Plugin)

    plugin performance data belongs to



70
71
72
# File 'lib/naplug/performancedata.rb', line 70

def plugin
  @meta.plugin
end

#to_aArray<Hash<label,field_data>>

List of performance data label entries

Returns:

  • (Array<Hash<label,field_data>>)

    an array of hashes keyed by field



29
30
31
# File 'lib/naplug/performancedata.rb', line 29

def to_a
  @data.values
end

#to_str(label = nil) ⇒ Object

performance data format: ‘label=value;[warn];;[min];



20
21
22
23
24
25
# File 'lib/naplug/performancedata.rb', line 20

def to_str(label = nil)
  label_ary = label.nil? ? labels : [curate_label(label)]
  label_ary.map do |l|
    '%s=%s%s;%s;%s;%s;%s' % FIELDS.map { |k| @data[l][k] }
  end.join(' ').strip
end