Class: MonitoringProtocols::JSON::Builder

Inherits:
Builder
  • Object
show all
Defined in:
lib/monitoring_protocols/json/builder.rb

Instance Method Summary collapse

Methods inherited from Builder

#initialize

Constructor Details

This class inherits a constructor from MonitoringProtocols::Builder

Instance Method Details

#build_packetObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/monitoring_protocols/json/builder.rb', line 9

def build_packet
  # first we need to find common properties
  json = {type: 'datapoints'}
  
  COMPRESSABLE_KEYS.each do |attr_name|
    v = @points[0].send(attr_name)
    if @points.all?{|p| p.send(attr_name) == v }
      json[attr_name] = v
    end
  end
  
  points_left = @points
  
  
  # find the root key
  keys_left = KEYS.select{|key| !json.has_key?(key) }
  
  raise "unsupported" unless keys_left.size == 3
  
  # now fill the rest
  until points_left.empty?
    p = points_left.pop()
    
    json[p.res_name] ||= {}
    json[p.res_name][p.metric_name] = p.value
  end
  
  Oj.dump(json, symbol_keys: false, mode: :compat)
end