Class: Riemann::Event
- Inherits:
-
Object
- Object
- Riemann::Event
- Includes:
- Beefcake::Message
- Defined in:
- lib/riemann/event.rb
Constant Summary collapse
- VIRTUAL_FIELDS =
Fields which don’t really exist in protobufs, but which are reserved and can’t be used as attributes.
Set.new([:metric])
- RESERVED_FIELDS =
Fields which are specially encoded in the Event protobuf–that is, they can’t be used as attributes.
fields.map do |_i, field| field.name.to_sym end.reduce(VIRTUAL_FIELDS) do |set, field| # rubocop:disable Style/MultilineBlockChain set << field end
Class Method Summary collapse
-
.average(states, init = Event.new) ⇒ Object
Average a set of states together.
-
.max(states, init = Event.new) ⇒ Object
Finds the maximum of a set of states.
- .mode(array) ⇒ Object
- .now ⇒ Object
-
.partition(states, field) ⇒ Object
Partition a list of states by a field Returns a hash of field_value => state.
-
.sort(states, field) ⇒ Object
Sorts states by a field.
-
.sum(states, init = Event.new) ⇒ Object
Sum a set of states together.
Instance Method Summary collapse
-
#[](key) ⇒ Object
Look up attributes.
-
#[]=(key, value) ⇒ Object
Set attributes.
-
#initialize(hash = nil) ⇒ Event
constructor
A new instance of Event.
- #metric ⇒ Object
- #metric=(value) ⇒ Object
Constructor Details
#initialize(hash = nil) ⇒ Event
Returns a new instance of Event.
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/riemann/event.rb', line 172 def initialize(hash = nil) if hash super hash self.metric = hash[:metric] if hash[:metric] # Add extra attributes to the event as Attribute instances with values # converted to String self.attributes = hash.map do |key, _value| unless RESERVED_FIELDS.include? key.to_sym Attribute.new(key: key.to_s, value: (hash[key] || hash[key.to_sym]).to_s) end end.compact else super() end @time_micros ||= self.class.now unless @time end |
Class Method Details
.average(states, init = Event.new) ⇒ Object
Average a set of states together. Chooses the mean metric, the mode state, mode service, and the mean time. If init is provided, its values override (where present) the computed ones.
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/riemann/event.rb', line 40 def self.average(states, init = Event.new) init = case init when Event init.dup else Event.new init end # Metric init.metric_f ||= states.inject(0.0) do |a, state| a + (state.metric || 0) end / states.size init.metric_f = 0.0 if init.metric_f.nan? # Event init.state ||= mode states.map(&:state) init.service ||= mode states.map(&:service) # Time init.time_micros = begin times = states.map(&:time_micros).compact (times.inject(:+) / times.size).to_i rescue ZeroDivisionError nil end init.time_micros ||= now init end |
.max(states, init = Event.new) ⇒ Object
Finds the maximum of a set of states. Metric is the maximum. Event is the highest, as defined by Dash.config.state_order. Time is the mean.
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/riemann/event.rb', line 105 def self.max(states, init = Event.new) init = case init when Event init.dup else Event.new init end # Metric init.metric_f ||= states.inject(0.0) do |a, state| a + (state.metric || 0) end init.metric = 0.0 if init.metric.nan? # Event init.state ||= states.inject(nil) do |max, state| state.state if Dash.config[:state_order][state.state] > Dash.config[:state_order][max] end # Time init.time_micros = begin times = states.map(&:time_micros).compact (times.inject(:+) / times.size).to_i rescue ZeroDivisionError nil end init.time_micros ||= now init end |
.mode(array) ⇒ Object
136 137 138 139 140 141 142 |
# File 'lib/riemann/event.rb', line 136 def self.mode(array) array.each_with_object(Hash.new(0)) do |e, counts| counts[e] += 1 end.max_by { |_e, count| count }.first # rubocop:disable Style/MultilineBlockChain rescue StandardError nil end |
.now ⇒ Object
33 34 35 |
# File 'lib/riemann/event.rb', line 33 def self.now (Time.now.to_f * 1_000_000).to_i end |
.partition(states, field) ⇒ Object
Partition a list of states by a field Returns a hash of field_value => state
146 147 148 149 150 151 152 153 154 155 |
# File 'lib/riemann/event.rb', line 146 def self.partition(states, field) states.each_with_object({}) do |state, p| k = state.send field if p.include? k p[k] << state else p[k] = [state] end end end |
.sort(states, field) ⇒ Object
Sorts states by a field. nil values first.
158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/riemann/event.rb', line 158 def self.sort(states, field) states.sort do |a, b| a = a.send field b = b.send field if a.nil? -1 elsif b.nil? 1 else a <=> b end end end |
.sum(states, init = Event.new) ⇒ Object
Sum a set of states together. Adds metrics, takes the mode state, mode service and the mean time. If init is provided, its values override (where present) the computed ones.
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/riemann/event.rb', line 73 def self.sum(states, init = Event.new) init = case init when Event init.dup else Event.new init end # Metric init.metric_f ||= states.inject(0.0) do |a, state| a + (state.metric || 0) end init.metric_f = 0.0 if init.metric_f.nan? # Event init.state ||= mode states.map(&:state) init.service ||= mode states.map(&:service) # Time init.time_micros = begin times = states.map(&:time_micros).compact (times.inject(:+) / times.size).to_i rescue ZeroDivisionError nil end init.time_micros ||= now init end |
Instance Method Details
#[](key) ⇒ Object
Look up attributes
209 210 211 212 213 214 215 |
# File 'lib/riemann/event.rb', line 209 def [](key) if RESERVED_FIELDS.include? key.to_sym super else attributes.find { |a| a.key.to_s == key.to_s }.value end end |
#[]=(key, value) ⇒ Object
Set attributes
218 219 220 221 222 223 224 225 226 227 228 229 |
# File 'lib/riemann/event.rb', line 218 def []=(key, value) if RESERVED_FIELDS.include? key.to_sym super else attr = attributes.find { |a| a.key == key.to_s } if attr attr.value = value.to_s else attributes << Attribute.new(key: key.to_s, value: value.to_s) end end end |
#metric ⇒ Object
192 193 194 195 196 |
# File 'lib/riemann/event.rb', line 192 def metric metric_d || metric_sint64 || metric_f end |
#metric=(value) ⇒ Object
198 199 200 201 202 203 204 205 206 |
# File 'lib/riemann/event.rb', line 198 def metric=(value) if value.is_a?(Integer) && (-(2**63)...2**63).include?(value) # Long self.metric_sint64 = value else self.metric_d = value.to_f end self.metric_f = value.to_f end |