Class: Wellness::DetailedReport

Inherits:
Object
  • Object
show all
Defined in:
lib/wellness/detailed_report.rb

Constant Summary collapse

STATUSES =
{
  'HEALTHY' => 200,
  'DEGRADED' => 500,
  'UNHEALTHY' => 500
}

Instance Method Summary collapse

Constructor Details

#initialize(system) ⇒ DetailedReport

Returns a new instance of DetailedReport.



11
12
13
# File 'lib/wellness/detailed_report.rb', line 11

def initialize(system)
  @system = system
end

Instance Method Details

#callObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/wellness/detailed_report.rb', line 15

def call
  non_criticals = []
  criticals = []

  services = {}
  @system.services.each do |name, service|
    services[name] = service.call

    if service.critical?
      criticals << services[name]['status']
    else
      non_criticals << services[name]['status']
    end
  end

  details = {}
  @system.details.each do |name, detail|
    details[name] = detail.call
  end

  healthy = criticals.all? { |s| s == 'HEALTHY' }
  degraded = non_criticals.any? { |s| s == 'UNHEALTHY' }

  if healthy
    if degraded
      status = 'DEGRADED'
    else
      status = 'HEALTHY'
    end
  else
    status = 'UNHEALTHY'
  end

  results = {
    'status'   => status,
    'services' => services,
    'details'  => details
  }

  render({json: results, status: STATUSES[status]})
end

#render(options = {}) ⇒ Object



57
58
59
60
61
62
# File 'lib/wellness/detailed_report.rb', line 57

def render(options={})
  status   = options[:status]  || 200
  response = JSON.dump(options[:json])

  [status, { 'Content-Type' => 'application/json' }, [response]]
end