Class: Stoplight::Admin::LightsRepository::Light
- Inherits:
-
Object
- Object
- Stoplight::Admin::LightsRepository::Light
- Defined in:
- lib/stoplight/admin/lights_repository/light.rb
Constant Summary collapse
- COLORS =
[ GREEN = Stoplight::Color::GREEN, YELLOW = Stoplight::Color::YELLOW, RED = Stoplight::Color::RED ].freeze
Instance Attribute Summary collapse
-
#color ⇒ Object
Returns the value of attribute color.
-
#failure_count ⇒ Object
Returns the value of attribute failure_count.
-
#failures ⇒ Object
Returns the value of attribute failures.
-
#id ⇒ Object
Returns the value of attribute id.
-
#name ⇒ Object
Returns the value of attribute name.
-
#state ⇒ Object
Returns the value of attribute state.
Instance Method Summary collapse
- #as_json ⇒ Hash
- #default_sort_key ⇒ Array
- #description_comment ⇒ String
- #description_message ⇒ String
- #description_title ⇒ String
-
#initialize(name:, color:, state:, failures:, failure_count: nil) ⇒ Light
constructor
A new instance of Light.
-
#last_check ⇒ Object
TODO: take into account positive checks as well.
- #last_check_in_words ⇒ String?
- #latest_failure ⇒ Object
- #locked? ⇒ Boolean
- #unlocked? ⇒ Boolean
Constructor Details
#initialize(name:, color:, state:, failures:, failure_count: nil) ⇒ Light
Returns a new instance of Light.
44 45 46 47 48 49 50 51 |
# File 'lib/stoplight/admin/lights_repository/light.rb', line 44 def initialize(name:, color:, state:, failures:, failure_count: nil) @id = SecureRandom.uuid @name = name @color = color @state = state @failures = failures @failure_count = failure_count end |
Instance Attribute Details
#color ⇒ Object
Returns the value of attribute color.
25 26 27 |
# File 'lib/stoplight/admin/lights_repository/light.rb', line 25 def color @color end |
#failure_count ⇒ Object
Returns the value of attribute failure_count.
37 38 39 |
# File 'lib/stoplight/admin/lights_repository/light.rb', line 37 def failure_count @failure_count end |
#failures ⇒ Object
Returns the value of attribute failures.
33 34 35 |
# File 'lib/stoplight/admin/lights_repository/light.rb', line 33 def failures @failures end |
#id ⇒ Object
Returns the value of attribute id.
17 18 19 |
# File 'lib/stoplight/admin/lights_repository/light.rb', line 17 def id @id end |
#name ⇒ Object
Returns the value of attribute name.
21 22 23 |
# File 'lib/stoplight/admin/lights_repository/light.rb', line 21 def name @name end |
#state ⇒ Object
Returns the value of attribute state.
29 30 31 |
# File 'lib/stoplight/admin/lights_repository/light.rb', line 29 def state @state end |
Instance Method Details
#as_json ⇒ Hash
68 69 70 71 72 73 74 75 |
# File 'lib/stoplight/admin/lights_repository/light.rb', line 68 def as_json { name: name, color: color, failures: failures, locked: locked? } end |
#default_sort_key ⇒ Array
78 79 80 |
# File 'lib/stoplight/admin/lights_repository/light.rb', line 78 def default_sort_key [-COLORS.index(color), name] end |
#description_comment ⇒ String
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/stoplight/admin/lights_repository/light.rb', line 150 def description_comment case color when RED if locked? "Override active - all requests blocked" else "Will attempt recovery after cooling period" end when YELLOW "Allowing limited test traffic (0 of 1 requests)" when GREEN if locked? "Override active - all requests processed" else "Operating normally" end end end |
#description_message ⇒ String
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/stoplight/admin/lights_repository/light.rb', line 124 def case color when RED if latest_failure "#{latest_failure.error_class}: #{latest_failure.}" elsif locked? "Circuit manually locked open" else "Not available" end when Stoplight::Color::YELLOW if latest_failure "#{latest_failure.error_class}: #{latest_failure.}" else "Not available" end when GREEN if locked? "Circuit manually locked closed" else "No recent errors" end end end |
#description_title ⇒ String
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/stoplight/admin/lights_repository/light.rb', line 104 def description_title case color when RED if locked? && failures.empty? "Locked Open" else "Last Error" end when Stoplight::Color::YELLOW "Testing Recovery" when GREEN if locked? "Forced Healthy" else "Healthy" end end end |
#last_check ⇒ Object
TODO: take into account positive checks as well
82 |
# File 'lib/stoplight/admin/lights_repository/light.rb', line 82 def last_check = latest_failure&.time # TODO: take into account positive checks as well |
#last_check_in_words ⇒ String?
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/stoplight/admin/lights_repository/light.rb', line 85 def last_check_in_words last_error_time = latest_failure&.time return unless last_error_time time_difference = Time.now.utc - last_error_time if time_difference < 1 "just now" elsif time_difference < 60 "#{time_difference.to_i}s ago" elsif time_difference < 3600 "#{(time_difference / 60).to_i}m ago" elsif time_difference < 86400 "#{(time_difference / 3600).to_i}h ago" else "#{(time_difference / 86400).to_i}d ago" end end |
#latest_failure ⇒ Object
53 54 55 |
# File 'lib/stoplight/admin/lights_repository/light.rb', line 53 def latest_failure failures.first end |
#locked? ⇒ Boolean
58 59 60 |
# File 'lib/stoplight/admin/lights_repository/light.rb', line 58 def locked? !unlocked? end |