Sha256: 6768fe4606360f2b0e8bd76dbd3e1270e5820bb303f335272354ca4fda2525f1

Contents?: true

Size: 1.48 KB

Versions: 24

Compression:

Stored size: 1.48 KB

Contents

module GovukHealthcheck
  STATUSES = [
    OK = :ok,
    WARNING = :warning,
    CRITICAL = :critical,
  ].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

    attr_reader :checks

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

    def all_components
      checks.map { |check| check.instance_of?(Class) ? check.new : check }
    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)
      if check.respond_to?(:enabled?) && !check.enabled?
        { status: :ok, message: "currently disabled" }
      else
        begin
          component_status = details(check).merge(status: check.status)
          component_status[:message] = check.message if check.respond_to?(:message)
          component_status
        rescue StandardError => e
          { status: :critical, message: e.to_s }
        end
      end
    end

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

Version data entries

24 entries across 24 versions & 1 rubygems

Version Path
govuk_app_config-2.2.0 lib/govuk_app_config/govuk_healthcheck/checkup.rb
govuk_app_config-2.1.2 lib/govuk_app_config/govuk_healthcheck/checkup.rb
govuk_app_config-2.1.1 lib/govuk_app_config/govuk_healthcheck/checkup.rb
govuk_app_config-2.1.0 lib/govuk_app_config/govuk_healthcheck/checkup.rb
govuk_app_config-2.0.3 lib/govuk_app_config/govuk_healthcheck/checkup.rb
govuk_app_config-2.0.2 lib/govuk_app_config/govuk_healthcheck/checkup.rb
govuk_app_config-2.0.1 lib/govuk_app_config/govuk_healthcheck/checkup.rb
govuk_app_config-2.0.0 lib/govuk_app_config/govuk_healthcheck/checkup.rb
govuk_app_config-1.20.2 lib/govuk_app_config/govuk_healthcheck/checkup.rb
govuk_app_config-1.20.1 lib/govuk_app_config/govuk_healthcheck/checkup.rb
govuk_app_config-1.20.0 lib/govuk_app_config/govuk_healthcheck/checkup.rb
govuk_app_config-1.19.0 lib/govuk_app_config/govuk_healthcheck/checkup.rb
govuk_app_config-1.18.1 lib/govuk_app_config/govuk_healthcheck/checkup.rb
govuk_app_config-1.17.0 lib/govuk_app_config/govuk_healthcheck/checkup.rb
govuk_app_config-1.16.3 lib/govuk_app_config/govuk_healthcheck/checkup.rb
govuk_app_config-1.16.1 lib/govuk_app_config/govuk_healthcheck/checkup.rb
govuk_app_config-1.16.0 lib/govuk_app_config/govuk_healthcheck/checkup.rb
govuk_app_config-1.15.1 lib/govuk_app_config/govuk_healthcheck/checkup.rb
govuk_app_config-1.15.0 lib/govuk_app_config/govuk_healthcheck/checkup.rb
govuk_app_config-1.14.0 lib/govuk_app_config/govuk_healthcheck/checkup.rb