Module: FFWD::Plugin::Protobuf::Serializer::Protocol0

Defined in:
lib/ffwd/plugin/protobuf/serializer/protocol0.rb

Constant Summary collapse

P =
::FFWD::Protocol0
METRIC_FIELDS =
[:key, :value, :host]
EVENT_FIELDS =
[:key, :value, :host, :state, :description, :ttl]

Class Method Summary collapse

Class Method Details

.dump_event(event) ⇒ Object



75
76
77
78
79
80
81
# File 'lib/ffwd/plugin/protobuf/serializer/protocol0.rb', line 75

def self.dump_event event
  e = P::Event.new map_fields(EVENT_FIELDS, event)
  e.time = (event.time.to_f * 1000).to_i if event.time
  e.tags = to_tags event.tags if event.tags
  e.attributes = to_attributes event.attributes if event.attributes
  P::Message.new(:event => e).encode
end

.dump_metric(metric) ⇒ Object



83
84
85
86
87
88
89
# File 'lib/ffwd/plugin/protobuf/serializer/protocol0.rb', line 83

def self.dump_metric metric
  m = P::Metric.new map_fields(METRIC_FIELDS, metric)
  m.time = (metric.time.to_f * 1000).to_i if metric.time
  m.tags = to_tags metric.tags if metric.tags
  m.attributes = to_attributes metric.attributes if metric.attributes
  P::Message.new(:metric => m).encode
end

.from_attributes(source) ⇒ Object



67
68
69
# File 'lib/ffwd/plugin/protobuf/serializer/protocol0.rb', line 67

def self.from_attributes source
  Hash[source.map{|a| [a.key, a.value]}]
end

.from_tags(source) ⇒ Object



71
72
73
# File 'lib/ffwd/plugin/protobuf/serializer/protocol0.rb', line 71

def self.from_tags source
  Array.new source
end

.load(string) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/ffwd/plugin/protobuf/serializer/protocol0.rb', line 34

def self.load string
  message = P::Message.decode(string)

  if message.event
    yield :event, receive_event(message.event)
  end

  if message.metric
    yield :metric, receive_metric(message.metric)
  end
end

.map_fields(fields, s) ⇒ Object



46
47
48
# File 'lib/ffwd/plugin/protobuf/serializer/protocol0.rb', line 46

def self.map_fields fields, s
  Hash[fields.map{|f| [f, s.send(f)]}.reject{|f, v| v.nil?}]
end

.receive_event(event) ⇒ Object



50
51
52
53
54
55
56
# File 'lib/ffwd/plugin/protobuf/serializer/protocol0.rb', line 50

def self.receive_event event
  d = map_fields EVENT_FIELDS, event
  d[:time] = Time.at(event.time.to_f / 1000) if event.time
  d[:tags] = from_tags event.tags if event.tags
  d[:attributes] = from_attributes event.attributes if event.attributes
  return d
end

.receive_metric(metric) ⇒ Object



58
59
60
61
62
63
64
65
# File 'lib/ffwd/plugin/protobuf/serializer/protocol0.rb', line 58

def self.receive_metric metric
  d = map_fields METRIC_FIELDS, metric
  d[:proc] = metric.proc if metric.proc
  d[:time] = Time.at(metric.time.to_f / 1000) if metric.time
  d[:tags] = from_tags metric.tags if metric.tags
  d[:attributes] = from_attributes metric.attributes if metric.attributes
  return d
end