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

Returns a new instance of Metric.



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

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.



12
13
14
# File 'lib/puppet/util/metric.rb', line 12

def label
  @label
end

#nameObject

Returns the value of attribute name.



12
13
14
# File 'lib/puppet/util/metric.rb', line 12

def name
  @name
end

#typeObject

Returns the value of attribute type.



12
13
14
# File 'lib/puppet/util/metric.rb', line 12

def type
  @type
end

#valueObject

Returns the value of attribute value.



12
13
14
# File 'lib/puppet/util/metric.rb', line 12

def value
  @value
end

#valuesObject



60
61
62
# File 'lib/puppet/util/metric.rb', line 60

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

Class Method Details

.from_data_hash(data) ⇒ Object



15
16
17
18
19
# File 'lib/puppet/util/metric.rb', line 15

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

.labelize(name) ⇒ Object

Convert a name into a label.



65
66
67
# File 'lib/puppet/util/metric.rb', line 65

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

Instance Method Details

#[](name) ⇒ Object

Return a specific value



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

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

#initialize_from_hash(data) ⇒ Object



21
22
23
24
25
# File 'lib/puppet/util/metric.rb', line 21

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)


53
54
55
56
57
58
# File 'lib/puppet/util/metric.rb', line 53

def newvalue(name, value, label = nil)
  raise ArgumentError, "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



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

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