Sha256: 365c80182823ed1187265401e2caf85191c11051d02b4d311200bc221ea79097

Contents?: true

Size: 1.26 KB

Versions: 1

Compression:

Stored size: 1.26 KB

Contents

# frozen_string_literal: true

module Protoboard
  module Helpers
    ##
    # This class is responsible to generate information about the +circuits+ added
    class ServicesHealthcheckGenerator

      ##
      # Verifies the list of +circuits+ added and returns a hash with the +circuits names+ and its states.
      #
      #   ==== Examples
      #   'services' => {
      #    'my_service_name' => {
      #       'circuits' => {
      #         'my_service_name#some_method' => 'OK',
      #         'my_custom_name' => 'NOT_OK'
      #       }
      #     }
      #   }
      #  ====
      #
      def call
        circuits_hash = Protoboard::CircuitBreaker.registered_circuits.map do |circuit|
          state = Protoboard.config.adapter.check_state(circuit.name)

          { name: circuit.name, status: state, service: circuit.service }
        end
        services_hash = circuits_hash
                        .group_by { |circuit| circuit[:service] }
                        .map do |service, circuits_hash|
          circuits = circuits_hash.each_with_object({}) { |circuit, memo| memo[circuit[:name]] = circuit[:status] }
          { service => { 'circuits' => circuits } }
        end.reduce(:merge)

        { 'services' => services_hash.to_h }
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
protoboard-0.1.3 lib/protoboard/helpers/services_healthcheck_generator.rb