Method: UState::State.sum
- Defined in:
- lib/ustate/state.rb
.sum(states, init = State.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.
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/ustate/state.rb', line 50 def self.sum(states, init = State.new) init = case init when State init.dup else State.new init end # Metric init.metric_f ||= states.inject(0.0) { |a, state| a + (state.metric || 0) } if init.metric_f.nan? init.metric_f = 0.0 end # State init.state ||= mode states.map(&:state) init.service ||= mode states.map(&:service) # Time init.time = begin times = states.map(&:time).compact (times.inject(:+) / times.size).to_i rescue end init.time ||= Time.now.to_i init end |