Class: LitmusPaper::Health

Inherits:
Object
  • Object
show all
Defined in:
lib/litmus_paper/health.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(forced = :none, forced_reason = "") ⇒ Health

Returns a new instance of Health.



6
7
8
9
10
11
12
# File 'lib/litmus_paper/health.rb', line 6

def initialize(forced = :none, forced_reason = "")
  @value = 0
  @dependencies_available = true
  @summary = ""
  @forced = forced
  @forced_reason = forced_reason
end

Instance Attribute Details

#forced_reasonObject (readonly)

Returns the value of attribute forced_reason.



4
5
6
# File 'lib/litmus_paper/health.rb', line 4

def forced_reason
  @forced_reason
end

#summaryObject (readonly)

Returns the value of attribute summary.



4
5
6
# File 'lib/litmus_paper/health.rb', line 4

def summary
  @summary
end

Instance Method Details

#directionObject



22
23
24
# File 'lib/litmus_paper/health.rb', line 22

def direction
  @forced
end

#ensure(dependency) ⇒ Object



58
59
60
61
62
63
# File 'lib/litmus_paper/health.rb', line 58

def ensure(dependency)
  available = dependency.available?

  @dependencies_available &&= available
  @summary << "#{dependency}: #{available ? 'OK' : 'FAIL'}\n"
end

#forced?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/litmus_paper/health.rb', line 18

def forced?
  @forced != :none
end

#measured_healthObject



46
47
48
49
# File 'lib/litmus_paper/health.rb', line 46

def measured_health
  return 0 unless @dependencies_available
  @value
end

#ok?Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/litmus_paper/health.rb', line 14

def ok?
  value > 0
end

#perform(metric) ⇒ Object



51
52
53
54
55
56
# File 'lib/litmus_paper/health.rb', line 51

def perform(metric)
  health = metric.current_health.ceil

  @value += health
  @summary << "#{metric}: #{health}\n"
end

#valueObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/litmus_paper/health.rb', line 26

def value
  if forced?
    return case @forced
    when :up
      100
    when :down
      0
    when :health
      forced_health = @forced_reason.split("\n").last.to_i

      # This could potentially be argued differently, but I feel like forcing
      # a health value != forcing up - if the measured health is less than the
      # forced health, we should return the measured health.
      measured_health < forced_health ? measured_health : forced_health
    end
  end

  measured_health
end