Class: Puppet::Util::Metric

Inherits:
Object show all
Includes:
Network::FormatSupport, PsychSupport
Defined in:
lib/puppet/util/metric.rb

Overview

A class for handling metrics. This is currently ridiculously hackish.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Network::FormatSupport

included, #mime, #render, #support_format?, #to_json, #to_msgpack, #to_pson

Methods included from PsychSupport

#encode_with, #init_with

Constructor Details

#initialize(name, label = nil) ⇒ Metric


43
44
45
46
47
48
49
# File 'lib/puppet/util/metric.rb', line 43

def initialize(name,label = nil)
  @name = name.to_s

  @label = label || self.class.labelize(name)

  @values = []
end

Instance Attribute Details

#labelObject

Returns the value of attribute label


10
11
12
# File 'lib/puppet/util/metric.rb', line 10

def label
  @label
end

#nameObject

Returns the value of attribute name


10
11
12
# File 'lib/puppet/util/metric.rb', line 10

def name
  @name
end

#typeObject

Returns the value of attribute type


10
11
12
# File 'lib/puppet/util/metric.rb', line 10

def type
  @type
end

#valueObject

Returns the value of attribute value


10
11
12
# File 'lib/puppet/util/metric.rb', line 10

def value
  @value
end

#valuesObject


57
58
59
# File 'lib/puppet/util/metric.rb', line 57

def values
  @values.sort_by { |a| a[1] }
end

Class Method Details

.from_data_hash(data) ⇒ Object


13
14
15
16
17
# File 'lib/puppet/util/metric.rb', line 13

def self.from_data_hash(data)
  metric = allocate
  metric.initialize_from_hash(data)
  metric
end

.labelize(name) ⇒ Object

Convert a name into a label.


62
63
64
# File 'lib/puppet/util/metric.rb', line 62

def self.labelize(name)
  name.to_s.capitalize.tr("_", " ")
end

Instance Method Details

#[](name) ⇒ Object

Return a specific value


34
35
36
37
38
39
40
41
# File 'lib/puppet/util/metric.rb', line 34

def [](name)
  value = @values.find { |v| v[0] == name }
  if value
    return value[2]
  else
    return 0
  end
end

#initialize_from_hash(data) ⇒ Object


19
20
21
22
23
# File 'lib/puppet/util/metric.rb', line 19

def initialize_from_hash(data)
  @name = data['name']
  @label = data['label'] || self.class.labelize(@name)
  @values = data['values']
end

#newvalue(name, value, label = nil) ⇒ Object

Raises:

  • (ArgumentError)

51
52
53
54
55
# File 'lib/puppet/util/metric.rb', line 51

def newvalue(name,value,label = nil)
  raise ArgumentError.new("metric name #{name.inspect} is not a string") unless name.is_a? String
  label ||= self.class.labelize(name)
  @values.push [name,label,value]
end

#to_data_hashObject


25
26
27
28
29
30
31
# File 'lib/puppet/util/metric.rb', line 25

def to_data_hash
  {
    'name' => @name,
    'label' => @label,
    'values' => @values
  }
end