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:) ⇒ Light

Returns a new instance of Light.

Parameters:

  • name (String)
  • color (String)
  • state (String)
  • failures (<Stoplight::Failure>)


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

#colorObject

Returns the value of attribute color.



28
29
30
# File 'lib/stoplight/admin/lights_repository/light.rb', line 28

def color
  @color
end

#failuresObject

Returns the value of attribute failures.



38
39
40
# File 'lib/stoplight/admin/lights_repository/light.rb', line 38

def failures
  @failures
end

#idObject

Returns the value of attribute id.



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

def id
  @id
end

#nameObject

Returns the value of attribute name.



23
24
25
# File 'lib/stoplight/admin/lights_repository/light.rb', line 23

def name
  @name
end

#stateObject

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_jsonHash

Returns:

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

Returns:

  • (Array)


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

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

#description_commentString

Returns:

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

Returns:

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

#description_titleString

Returns:

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

Returns:

  • (String, nil)


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_failureObject



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

def latest_failure
  failures.first
end

#locked?Boolean

Returns:

  • (Boolean)


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

def locked?
  !unlocked?
end

#unlocked?Boolean

Returns:

  • (Boolean)


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

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