Sha256: cd02d251406e7ba52794618d68826fad4ce71a66c17f132804bdb807a53757bd

Contents?: true

Size: 1.1 KB

Versions: 6

Compression:

Stored size: 1.1 KB

Contents

class Eye::Process::StatesHistory < Eye::Utils::Tail

  def push(state, reason = nil, tm = Time.now)
    super(state: state, at: tm.to_i, reason: reason)
  end

  def states
    self.map{|c| c[:state] }
  end

  def states_for_period(period, from_time = nil)
    tm = Time.now - period
    tm = [tm, from_time].max if from_time
    tm = tm.to_f
    self.select{|s| s[:at] >= tm }.map{|c| c[:state] }
  end

  def last_state
    last[:state]
  end

  def last_reason
    last[:reason]
  end

  def last_state_changed_at
    Time.at(last[:at])
  end

  def seq?(*seq)
    str = states * ','
    substr = seq.flatten * ','
    str.include?(substr)
  end

  def end?(*seq)
    str = states * ','
    substr = seq.flatten * ','
    str.end_with?(substr)
  end

  def any?(*seq)
    states.any? do |st|      
      seq.flatten.include?(st)
    end
  end

  def noone?(*seq)
    !states.all? do |st|      
      seq.flatten.include?(st)
    end
  end

  def all?(*seq)
    states.all? do |st|      
      seq.flatten.include?(st)
    end
  end

  def state_count(state)
    states.count do |st|
      st == state
    end
  end

end

Version data entries

6 entries across 6 versions & 2 rubygems

Version Path
reel-eye-0.3.2 lib/eye/process/states_history.rb
eye-0.3.2 lib/eye/process/states_history.rb
reel-eye-0.3.1 lib/eye/process/states_history.rb
eye-0.3.1 lib/eye/process/states_history.rb
eye-0.3 lib/eye/process/states_history.rb
eye-0.2.4 lib/eye/process/states_history.rb