Class: Pvectl::Commands::Top::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/pvectl/commands/top/command.rb

Overview

Dispatcher for the ‘pvectl top <resource_type>` command.

Displays resource usage metrics (CPU, memory, disk, swap) for cluster resources. Supports nodes, VMs, and containers.

Uses Top::ResourceRegistry for handler lookup and Top-specific presenters for metrics-focused display. VMs and containers are filtered to running-only by default (use –all to show all).

Examples:

Basic usage

Commands::Top::Command.execute("nodes", options, global_options)

Constant Summary collapse

SHOW_ALL_RESOURCE_TYPES =

Resource types where running-only filtering does NOT apply.

%w[nodes node].freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resource_type, options, global_options, handler: nil, registry: Top::ResourceRegistry) ⇒ Command

Creates a new Top command instance.

Parameters:

  • resource_type (String, nil)

    type of resource

  • options (Hash)

    command options

  • global_options (Hash)

    global CLI options

  • handler (Object, nil) (defaults to: nil)

    override handler for testing

  • registry (Class) (defaults to: Top::ResourceRegistry)

    resource registry (default: Top::ResourceRegistry)



99
100
101
102
103
104
105
106
# File 'lib/pvectl/commands/top/command.rb', line 99

def initialize(resource_type, options, global_options,
               handler: nil, registry: Top::ResourceRegistry)
  @resource_type = resource_type
  @options = options
  @global_options = global_options
  @handler = handler
  @registry = registry
end

Class Method Details

.execute(resource_type, options, global_options) ⇒ Integer

Executes the top command.

Parameters:

  • resource_type (String, nil)

    type of resource (e.g., “nodes”)

  • options (Hash)

    command-specific options

    • :“sort-by” [String] sort field (cpu, memory, disk)

  • global_options (Hash)

    global CLI options

    • :output [String] output format (table, json, yaml, wide)

    • :color [Boolean, nil] explicit color setting

Returns:

  • (Integer)

    exit code



88
89
90
# File 'lib/pvectl/commands/top/command.rb', line 88

def self.execute(resource_type, options, global_options)
  new(resource_type, options, global_options).execute
end

.register(cli) ⇒ void

This method returns an undefined value.

Registers the top command with the CLI.

Parameters:

  • cli (GLI::App)

    the CLI application object



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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/pvectl/commands/top/command.rb', line 26

def self.register(cli)
  cli.desc "Display resource usage metrics (CPU, memory, disk)"
  cli.long_desc <<~HELP
    Display real-time resource usage metrics for cluster resources.
    Shows CPU, memory, disk, and network utilization in a table format.

    By default, only running VMs and containers are shown. Use --all to
    include stopped resources. Nodes always show all (including offline).

    RESOURCE TYPES
      nodes                     Cluster node metrics
      vms                       Virtual machine metrics (running only by default)
      containers                Container metrics (running only by default)

    EXAMPLES
      Cluster node resource usage:
        $ pvectl top nodes

      VMs sorted by CPU usage:
        $ pvectl top vms --sort-by cpu

      All containers including stopped:
        $ pvectl top containers --all

      Memory usage in JSON format:
        $ pvectl top vms --sort-by memory -o json

    NOTES
      Sort fields: cpu, memory, disk, netin, netout, name, node.

      Stopped VMs/containers show 0% for all metrics. Use --all
      if you need to see them alongside running resources.

    SEE ALSO
      pvectl help get           List resources with status info
      pvectl help describe      Detailed resource information
  HELP
  cli.arg_name "RESOURCE_TYPE"
  cli.command :top do |c|
    c.desc "Sort by field (cpu, memory, disk, netin, netout, name, node)"
    c.flag [:"sort-by"], arg_name: "FIELD"

    c.desc "Show all (including stopped)"
    c.switch [:all], default_value: false

    c.action do |global_options, options, args|
      resource_type = args[0]
      exit_code = execute(resource_type, options, global_options)
      exit exit_code if exit_code != 0
    end
  end
end

Instance Method Details

#executeInteger

Executes the top operation.

Returns:

  • (Integer)

    exit code



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/pvectl/commands/top/command.rb', line 111

def execute
  return missing_resource_type_error if @resource_type.nil?

  handler = @handler || @registry.for(@resource_type)
  return unknown_resource_error unless handler

  models = handler.list(sort: @options[:"sort-by"])
  models = filter_running(models) unless @options[:all]
  output = format_output(models, handler.presenter)
  puts output

  ExitCodes::SUCCESS
rescue Pvectl::Config::ConfigNotFoundError,
       Pvectl::Config::InvalidConfigError,
       Pvectl::Config::ContextNotFoundError,
       Pvectl::Config::ClusterNotFoundError,
       Pvectl::Config::UserNotFoundError => e
  $stderr.puts "Error: #{e.message}"
  ExitCodes::CONFIG_ERROR
rescue Timeout::Error => e
  output_connection_error(e.message)
  ExitCodes::CONNECTION_ERROR
rescue Errno::ECONNREFUSED => e
  output_connection_error(e.message)
  ExitCodes::CONNECTION_ERROR
rescue SocketError => e
  output_connection_error(e.message)
  ExitCodes::CONNECTION_ERROR
end