Class: MetricsCapacitor::Model::Metric

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/metrics-capacitor/model/metric.rb

Instance Method Summary collapse

Constructor Details

#initialize(data = {}) ⇒ Metric

Returns a new instance of Metric.



8
9
10
11
# File 'lib/metrics-capacitor/model/metric.rb', line 8

def initialize(data = {})
  @metric = data if data.class == Hash
  @metric ||= JSON.parse(data, symbolize_names: true)
end

Instance Method Details

#nameObject



29
30
31
# File 'lib/metrics-capacitor/model/metric.rb', line 29

def name
  @metric[:name].to_s
end

#tagsObject



33
34
35
36
# File 'lib/metrics-capacitor/model/metric.rb', line 33

def tags
  return @metric[:tags] if ( @metric[:tags] || @metric[:tags].empty? )
  { capacitor: 'untagged' }
end

#timestamp(scale = :ms) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/metrics-capacitor/model/metric.rb', line 51

def timestamp(scale = :ms)
  m = case scale
      when :ms
        1000.0
      when :us
        1_000_000.0
      when :ns
        1_000_000_000.0
      else
        1.0
      end
  return (Time.now.to_f * m).to_i.to_s unless @metric[:timestamp]
  (Time.at(@metric[:timestamp]).to_f * m).to_i.to_s
end

#to_elasticObject



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/metrics-capacitor/model/metric.rb', line 13

def to_elastic
  { index: {
      data: {
        :@name      => name,
        :@timestamp => timestamp(:ms),
        :@tags      => tags,
        :@values    => values
      }
    }
  }
end

#to_redisObject



25
26
27
# File 'lib/metrics-capacitor/model/metric.rb', line 25

def to_redis
  @metric.to_json
end

#valuesObject



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/metrics-capacitor/model/metric.rb', line 38

def values
  case @metric[:values]
  when Hash
    return @metric[:values]
  when Integer
    return { value: @metric[:values].to_f }
  when Float
    return { value: @metric[:values] }
  else
    return { value: 0.0 }
  end
end