Module: FFWD::Plugin::GoogleCloud::Utils

Defined in:
lib/ffwd/plugin/google_cloud/utils.rb

Class Method Summary collapse

Class Method Details

.extract_labels(source) ⇒ Object



85
86
87
# File 'lib/ffwd/plugin/google_cloud/utils.rb', line 85

def self.extract_labels source
  source.map{|k, v| {:key => k, :description => ""}}
end

.make_common_labels(buffer) ⇒ Object



21
22
23
24
# File 'lib/ffwd/plugin/google_cloud/utils.rb', line 21

def self.make_common_labels buffer
  return nil if buffer.empty?
  make_labels buffer.first.fixed_attr
end

.make_desc(m) ⇒ Object



55
56
57
# File 'lib/ffwd/plugin/google_cloud/utils.rb', line 55

def self.make_desc m
  {:metric => make_key(m), :labels => make_labels(m.external_attr)}
end

.make_key(m) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/ffwd/plugin/google_cloud/utils.rb', line 59

def self.make_key m
  attributes = Hash[m.external_attr]

  entries = []

  what ||= attributes.delete(:what)
  what ||= attributes.delete("what")

  entries << what unless what.nil?
  entries = entries.join('.')

  hash = attributes.keys.sort.hash.abs.to_s(32)

  unless entries.empty?
    "#{CUSTOM_PREFIX}/#{m.key}/#{entries}-#{hash}"
  else
    "#{CUSTOM_PREFIX}/#{m.key}-#{other_keys}"
  end
end

.make_labels(attr) ⇒ Object



79
80
81
82
83
# File 'lib/ffwd/plugin/google_cloud/utils.rb', line 79

def self.make_labels attr
  Hash[attr.select{|k, v| k.to_s != "what"}.map{|k, v|
    ["#{CUSTOM_PREFIX}/#{k}", v]
  }]
end

.make_point(m) ⇒ Object



50
51
52
53
# File 'lib/ffwd/plugin/google_cloud/utils.rb', line 50

def self.make_point m
  time = m.time.utc.strftime('%FT%TZ')
  {:start => time, :end => time, :doubleValue => m.value}
end

.make_timeseries(buffer) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/ffwd/plugin/google_cloud/utils.rb', line 26

def self.make_timeseries buffer
  # we are not allowed to send duplicate data.
  seen = Set.new

  result = []
  dropped = 0

  buffer.each do |m|
    d = make_desc(m)

    seen_key = [d[:metric], d[:labels].map{|k, v| [k, v]}.sort].hash

    if seen.member?(seen_key)
      dropped += 1
      next
    end

    seen.add(seen_key)
    result << {:timeseriesDesc => d, :point => make_point(m)}
  end

  [dropped, result]
end