lib/ustate/state.rb in ustate-client-0.0.4 vs lib/ustate/state.rb in ustate-client-0.0.5

- old
+ new

@@ -32,15 +32,14 @@ # State init.state ||= mode states.map(&:state) init.service ||= mode states.map(&:service) # Time - init.time ||= begin - (states.inject(0) do |a, state| - a + state.time.to_f - end / states.size).to_i - rescue + init.time = begin + times = states.map(&:time).compact + (times.inject(:+) / times.size).to_i + rescue end init.time ||= Time.now.to_i init end @@ -67,14 +66,13 @@ # State init.state ||= mode states.map(&:state) init.service ||= mode states.map(&:service) # Time - init.time ||= begin - (states.inject(0) do |a, state| - a + state.time.to_f - end / states.size).to_i + init.time = begin + times = states.map(&:time).compact + (times.inject(:+) / times.size).to_i rescue end init.time ||= Time.now.to_i init @@ -102,15 +100,14 @@ 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 ||= begin - (states.inject(0) { |a, state| - a + state.time.to_f - } / states.size).to_i - rescue + init.time = begin + times = states.map(&:time).compact + (times.inject(:+) / times.size).to_i + rescue end init.time ||= Time.now.to_i init end @@ -141,9 +138,15 @@ 1 else a <=> b end end + end + + def initialize(*a) + super *a + + @time ||= Time.now.to_i end def metric @metric || metric_f end