Sha256: f58ae2bc5f976156ae8559ea4923eb01db5ed4208ec18fe488894df136d10013

Contents?: true

Size: 1.12 KB

Versions: 1

Compression:

Stored size: 1.12 KB

Contents

module GovukHealthcheck
  STATUSES = [
    OK = "ok".freeze,
    WARNING = "warning".freeze,
    CRITICAL = "critical".freeze,
  ].freeze

  class Checkup
    # @param checks [Array] Array of objects/classes that respond to `run`
    def initialize(checks)
      @checks = checks
    end

    def run
      {
        status: worst_status,
        checks: component_statuses,
      }
    end

  private

    def component_statuses
      @component_statuses ||= @checks.each_with_object({}) do |check, hash|
        hash[check.name] = build_component_status(check)
      end
    end

    def worst_status
      if status?(CRITICAL)
        CRITICAL
      elsif status?(WARNING)
        WARNING
      else
        OK
      end
    end

    def status?(status)
      component_statuses.values.any? {|s| s[:status] == status }
    end

    def build_component_status(check)
      component_status = details(check).merge(status: check.status)
      component_status[:message] = check.message if check.respond_to?(:message)
      component_status
    end

    def details(check)
      check.respond_to?(:details) ? check.details : {}
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
govuk_app_config-1.5.0 lib/govuk_app_config/govuk_healthcheck/checkup.rb