Sha256: 8e53090e51a5c04b0fa0596fbe7ed0c2fab926a83807eb6df9f03bdd5794114f

Contents?: true

Size: 1.84 KB

Versions: 2

Compression:

Stored size: 1.84 KB

Contents

module ThreeScaleToolbox
  module Commands
    module MetricsCommand
      module List
        class ListSubcommand < Cri::CommandRunner
          include ThreeScaleToolbox::Command

          FIELDS_TO_SHOW = %w[id friendly_name system_name unit description].freeze

          def self.command
            Cri::Command.define do
              name        'list'
              usage       'list [opts] <remote> <service>'
              summary     'list metrics'
              description 'List metrics'

              param       :remote
              param       :service_ref

              runner ListSubcommand
            end
          end

          def run
            print_header
            print_data
          end

          private

          def print_header
            puts FIELDS_TO_SHOW.map(&:upcase).join("\t")
          end

          def print_data
            metrics.each do |metric|
              puts FIELDS_TO_SHOW.map { |field| metric.fetch(field, '(empty)') }.join("\t")
            end
          end

          def metrics
            hits_id = service.hits['id']
            ThreeScaleToolbox::Helper.array_difference(service.metrics, service.methods(hits_id)) do |metric, method|
              ThreeScaleToolbox::Helper.compare_hashes(metric, method, %w[id])
            end
          end

          def service
            @service ||= find_service
          end

          def find_service
            Entities::Service.find(remote: remote,
                                   ref: service_ref).tap do |svc|
              raise ThreeScaleToolbox::Error, "Service #{service_ref} does not exist" if svc.nil?
            end
          end

          def remote
            @remote ||= threescale_client(arguments[:remote])
          end

          def service_ref
            arguments[:service_ref]
          end
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
3scale_toolbox-0.11.0 lib/3scale_toolbox/commands/metrics_command/list_command.rb
3scale_toolbox-0.10.0 lib/3scale_toolbox/commands/metrics_command/list_command.rb