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.



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

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



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

def values
  @values.sort { |a, b| a[1] <=> b[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.



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

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

Instance Method Details

#[](name) ⇒ Object

Return a specific value



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

def [](name)
  if value = @values.find { |v| v[0] == name }
    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)


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

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