Class: Stoplight::Admin::LightsRepository::Light

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(name:, color:, state:, failures:, failure_count: nil) ⇒ Light

Returns a new instance of Light.

Parameters:

  • name (String)
  • color (String)
  • state (String)
  • failures (<Stoplight::Failure>)
  • failure_count (Integer, nil) (defaults to: nil)


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

#colorObject

Returns the value of attribute color.



25
26
27
# File 'lib/stoplight/admin/lights_repository/light.rb', line 25

def color
  @color
end

#failure_countObject

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

#failuresObject

Returns the value of attribute failures.



33
34
35
# File 'lib/stoplight/admin/lights_repository/light.rb', line 33

def failures
  @failures
end

#idObject

Returns the value of attribute id.



17
18
19
# File 'lib/stoplight/admin/lights_repository/light.rb', line 17

def id
  @id
end

#nameObject

Returns the value of attribute name.



21
22
23
# File 'lib/stoplight/admin/lights_repository/light.rb', line 21

def name
  @name
end

#stateObject

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_jsonHash

Returns:

  • (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_keyArray

Returns:

  • (Array)


78
79
80
# File 'lib/stoplight/admin/lights_repository/light.rb', line 78

def default_sort_key
  [-COLORS.index(color), name]
end

#description_commentString

Returns:

  • (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_messageString

Returns:

  • (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 description_message
  case color
  when RED
    if latest_failure
      "#{latest_failure.error_class}: #{latest_failure.error_message}"
    elsif locked?
      "Circuit manually locked open"
    else
      "Not available"
    end
  when Stoplight::Color::YELLOW
    if latest_failure
      "#{latest_failure.error_class}: #{latest_failure.error_message}"
    else
      "Not available"
    end
  when GREEN
    if locked?
      "Circuit manually locked closed"
    else
      "No recent errors"
    end
  end
end

#description_titleString

Returns:

  • (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_checkObject

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_wordsString?

Returns:

  • (String, nil)


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_failureObject



53
54
55
# File 'lib/stoplight/admin/lights_repository/light.rb', line 53

def latest_failure
  failures.first
end

#locked?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/stoplight/admin/lights_repository/light.rb', line 58

def locked?
  !unlocked?
end

#unlocked?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/stoplight/admin/lights_repository/light.rb', line 63

def unlocked?
  state == Stoplight::State::UNLOCKED
end