Class: SystemdMon::StateChange
- Inherits:
-
Object
- Object
- SystemdMon::StateChange
- Includes:
- Enumerable
- Defined in:
- lib/systemd_mon/state_change.rb
Instance Attribute Summary collapse
-
#states ⇒ Object
readonly
Returns the value of attribute states.
Instance Method Summary collapse
- #<<(state) ⇒ Object
- #auto_restart? ⇒ Boolean
- #changes ⇒ Object
- #diff ⇒ Object
- #each ⇒ Object
- #fail? ⇒ Boolean
- #important? ⇒ Boolean
-
#initialize(original_state = nil) ⇒ StateChange
constructor
A new instance of StateChange.
- #last ⇒ Object
- #length ⇒ Object
- #ok? ⇒ Boolean
- #recovery? ⇒ Boolean
- #reload? ⇒ Boolean
- #restart? ⇒ Boolean
- #status_text ⇒ Object
- #still_fail? ⇒ Boolean
- #to_s ⇒ Object
- #zipped ⇒ Object
Constructor Details
#initialize(original_state = nil) ⇒ StateChange
Returns a new instance of StateChange.
7 8 9 10 |
# File 'lib/systemd_mon/state_change.rb', line 7 def initialize(original_state = nil) self.states = [] states << original_state if original_state end |
Instance Attribute Details
#states ⇒ Object
Returns the value of attribute states.
5 6 7 |
# File 'lib/systemd_mon/state_change.rb', line 5 def states @states end |
Instance Method Details
#<<(state) ⇒ Object
20 21 22 23 |
# File 'lib/systemd_mon/state_change.rb', line 20 def <<(state) self.states << state @diff = nil end |
#auto_restart? ⇒ Boolean
51 52 53 |
# File 'lib/systemd_mon/state_change.rb', line 51 def auto_restart? first.ok? && last.ok? && changes.any? { |s| s.sub == "auto-restart" } end |
#changes ⇒ Object
31 32 33 |
# File 'lib/systemd_mon/state_change.rb', line 31 def changes states[1..-1] end |
#diff ⇒ Object
89 90 91 92 93 94 |
# File 'lib/systemd_mon/state_change.rb', line 89 def diff @diff ||= zipped.reject { |states| match = states.first.value states.all? { |s| s.value == match } } end |
#each ⇒ Object
25 26 27 28 29 |
# File 'lib/systemd_mon/state_change.rb', line 25 def each states.each do |state| yield state end end |
#fail? ⇒ Boolean
43 44 45 |
# File 'lib/systemd_mon/state_change.rb', line 43 def fail? last.fail? end |
#important? ⇒ Boolean
81 82 83 84 85 86 87 |
# File 'lib/systemd_mon/state_change.rb', line 81 def important? if length == 1 first.fail? else diff.map(&:last).any?(&:important?) end end |
#last ⇒ Object
12 13 14 |
# File 'lib/systemd_mon/state_change.rb', line 12 def last states.last end |
#length ⇒ Object
16 17 18 |
# File 'lib/systemd_mon/state_change.rb', line 16 def length states.length end |
#ok? ⇒ Boolean
39 40 41 |
# File 'lib/systemd_mon/state_change.rb', line 39 def ok? last.ok? end |
#recovery? ⇒ Boolean
35 36 37 |
# File 'lib/systemd_mon/state_change.rb', line 35 def recovery? first.fail? && last.ok? end |
#reload? ⇒ Boolean
55 56 57 |
# File 'lib/systemd_mon/state_change.rb', line 55 def reload? first.ok? && last.ok? && changes.any? { |s| s.active == "reloading" } end |
#restart? ⇒ Boolean
47 48 49 |
# File 'lib/systemd_mon/state_change.rb', line 47 def restart? first.ok? && last.ok? && changes.any? { |s| s.active == "deactivating" } end |
#status_text ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/systemd_mon/state_change.rb', line 63 def status_text if recovery? "recovered" elsif auto_restart? "automatically restarted" elsif restart? "restarted" elsif reload? "reloaded" elsif still_fail? "still failed" elsif fail? "failed" else "started" end end |
#still_fail? ⇒ Boolean
59 60 61 |
# File 'lib/systemd_mon/state_change.rb', line 59 def still_fail? length > 1 && first.fail? && last.fail? end |
#to_s ⇒ Object
104 105 106 107 108 109 110 111 112 |
# File 'lib/systemd_mon/state_change.rb', line 104 def to_s diff.inject("") { |s, (*states)| first = states.shift s << "#{first.name} state changed from #{first.value} to " s << states.map(&:value).join(" then ") s << "\n" s } end |
#zipped ⇒ Object
96 97 98 99 100 101 102 |
# File 'lib/systemd_mon/state_change.rb', line 96 def zipped if length == 1 first.all_states else first.all_states.zip(*changes.map(&:all_states)) end end |