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.
-
#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:) ⇒ Light
constructor
A new instance of Light.
- #last_check_in_words ⇒ String?
- #latest_failure ⇒ Object
- #locked? ⇒ Boolean
- #unlocked? ⇒ Boolean
Constructor Details
#initialize(name:, color:, state:, failures:) ⇒ Light
Returns a new instance of Light.
44 45 46 47 48 49 50 |
# File 'lib/stoplight/admin/lights_repository/light.rb', line 44 def initialize(name:, color:, state:, failures:) @id = SecureRandom.uuid @name = name @color = color @state = state @failures = failures end |
Instance Attribute Details
#color ⇒ Object
Returns the value of attribute color.
28 29 30 |
# File 'lib/stoplight/admin/lights_repository/light.rb', line 28 def color @color end |
#failures ⇒ Object
Returns the value of attribute failures.
38 39 40 |
# File 'lib/stoplight/admin/lights_repository/light.rb', line 38 def failures @failures end |
#id ⇒ Object
Returns the value of attribute id.
18 19 20 |
# File 'lib/stoplight/admin/lights_repository/light.rb', line 18 def id @id end |
#name ⇒ Object
Returns the value of attribute name.
23 24 25 |
# File 'lib/stoplight/admin/lights_repository/light.rb', line 23 def name @name end |
#state ⇒ Object
Returns the value of attribute state.
33 34 35 |
# File 'lib/stoplight/admin/lights_repository/light.rb', line 33 def state @state end |
Instance Method Details
#as_json ⇒ Hash
67 68 69 70 71 72 73 74 |
# File 'lib/stoplight/admin/lights_repository/light.rb', line 67 def as_json { name: name, color: color, failures: failures, locked: locked? } end |
#default_sort_key ⇒ Array
77 78 79 |
# File 'lib/stoplight/admin/lights_repository/light.rb', line 77 def default_sort_key [-COLORS.index(color), name] end |
#description_comment ⇒ String
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/stoplight/admin/lights_repository/light.rb', line 139 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
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/stoplight/admin/lights_repository/light.rb', line 119 def case color when RED if locked? && failures.empty? "Circuit manually locked open" else "#{latest_failure.error_class}: #{latest_failure.}" end when Stoplight::Color::YELLOW "#{latest_failure.error_class}: #{latest_failure.}" when GREEN if locked? "Circuit manually locked closed" else "No recent errors" end end end |
#description_title ⇒ String
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/stoplight/admin/lights_repository/light.rb', line 99 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_in_words ⇒ String?
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/stoplight/admin/lights_repository/light.rb', line 82 def last_check_in_words last_error_time = latest_failure&.time return unless last_error_time time_difference = Time.now - 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" else "#{(time_difference / 3600).to_i}h ago" end end |
#latest_failure ⇒ Object
52 53 54 |
# File 'lib/stoplight/admin/lights_repository/light.rb', line 52 def latest_failure failures.first end |
#locked? ⇒ Boolean
57 58 59 |
# File 'lib/stoplight/admin/lights_repository/light.rb', line 57 def locked? !unlocked? end |