Class: Pvectl::Commands::Top::Command
- Inherits:
-
Object
- Object
- Pvectl::Commands::Top::Command
- Defined in:
- lib/pvectl/commands/top/command.rb,
sig/pvectl/commands/top/command.rbs
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).
Constant Summary collapse
- SHOW_ALL_RESOURCE_TYPES =
Resource types where running-only filtering does NOT apply.
%w[nodes node].freeze
Class Method Summary collapse
-
.execute(resource_type, options, global_options) ⇒ Integer
Executes the top command.
-
.register(cli) ⇒ void
Registers the top command with the CLI.
Instance Method Summary collapse
-
#determine_color_enabled ⇒ Boolean
Determines if color output should be enabled.
-
#execute ⇒ Integer
Executes the top operation.
-
#filter_running(models) ⇒ Array<Object>
Filters models to running-only for VM/CT resource types.
-
#format_output(models, presenter) ⇒ String
Formats models for output using the appropriate formatter.
-
#initialize(resource_type, options, global_options, handler: nil, registry: Top::ResourceRegistry) ⇒ Command
constructor
Creates a new Top command instance.
-
#missing_resource_type_error ⇒ Integer
Outputs error for missing resource type argument.
-
#output_connection_error(message) ⇒ void
Outputs connection error message.
-
#unknown_resource_error ⇒ Integer
Outputs error for unknown resource type.
Constructor Details
#initialize(resource_type, options, global_options, handler: nil, registry: Top::ResourceRegistry) ⇒ Command
Creates a new Top command instance.
99 100 101 102 103 104 105 106 |
# File 'lib/pvectl/commands/top/command.rb', line 99 def initialize(resource_type, , , handler: nil, registry: Top::ResourceRegistry) @resource_type = resource_type = = @handler = handler @registry = registry end |
Class Method Details
.execute(resource_type, options, global_options) ⇒ Integer
Executes the top command.
88 89 90 |
# File 'lib/pvectl/commands/top/command.rb', line 88 def self.execute(resource_type, , ) new(resource_type, , ).execute end |
.register(cli) ⇒ void
This method returns an undefined value.
Registers the top command with the CLI.
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 " Display real-time resource usage metrics for cluster resources.\n Shows CPU, memory, disk, and network utilization in a table format.\n\n By default, only running VMs and containers are shown. Use --all to\n include stopped resources. Nodes always show all (including offline).\n\n RESOURCE TYPES\n nodes Cluster node metrics\n vms Virtual machine metrics (running only by default)\n containers Container metrics (running only by default)\n\n EXAMPLES\n Cluster node resource usage:\n $ pvectl top nodes\n\n VMs sorted by CPU usage:\n $ pvectl top vms --sort-by cpu\n\n All containers including stopped:\n $ pvectl top containers --all\n\n Memory usage in JSON format:\n $ pvectl top vms --sort-by memory -o json\n\n NOTES\n Sort fields: cpu, memory, disk, netin, netout, name, node.\n\n Stopped VMs/containers show 0% for all metrics. Use --all\n if you need to see them alongside running resources.\n\n SEE ALSO\n pvectl help get List resources with status info\n pvectl help describe Detailed resource information\n HELP\n cli.arg_name \"RESOURCE_TYPE\"\n cli.command :top do |c|\n c.desc \"Sort by field (cpu, memory, disk, netin, netout, name, node)\"\n c.flag [:\"sort-by\"], arg_name: \"FIELD\"\n\n c.desc \"Show all (including stopped)\"\n c.switch [:all], default_value: false\n\n c.action do |global_options, options, args|\n resource_type = args[0]\n exit_code = execute(resource_type, options, global_options)\n exit exit_code if exit_code != 0\n end\n end\nend\n" |
Instance Method Details
#determine_color_enabled ⇒ Boolean
Determines if color output should be enabled.
197 198 199 200 201 202 |
# File 'lib/pvectl/commands/top/command.rb', line 197 def determine_color_enabled explicit = [:color] return explicit unless explicit.nil? $stdout.tty? end |
#execute ⇒ Integer
Executes the top operation.
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: [:"sort-by"]) models = filter_running(models) unless [: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.) ExitCodes::CONNECTION_ERROR rescue Errno::ECONNREFUSED => e output_connection_error(e.) ExitCodes::CONNECTION_ERROR rescue SocketError => e output_connection_error(e.) ExitCodes::CONNECTION_ERROR end |
#filter_running(models) ⇒ Array<Object>
Filters models to running-only for VM/CT resource types. Nodes always show all (offline nodes are important info).
167 168 169 170 171 172 |
# File 'lib/pvectl/commands/top/command.rb', line 167 def filter_running(models) return models if SHOW_ALL_RESOURCE_TYPES.include?(@resource_type) return models unless models.first.respond_to?(:running?) models.select(&:running?) end |
#format_output(models, presenter) ⇒ String
Formats models for output using the appropriate formatter.
187 188 189 190 191 192 |
# File 'lib/pvectl/commands/top/command.rb', line 187 def format_output(models, presenter) format = [:output] || "table" color_enabled = determine_color_enabled formatter = Formatters::Registry.for(format) formatter.format(models, presenter, color_enabled: color_enabled) end |
#missing_resource_type_error ⇒ Integer
Outputs error for missing resource type argument.
146 147 148 149 150 151 |
# File 'lib/pvectl/commands/top/command.rb', line 146 def missing_resource_type_error $stderr.puts "Error: resource type is required" $stderr.puts "Usage: pvectl top RESOURCE_TYPE [options]" $stderr.puts "Available resources: nodes, vms, containers" ExitCodes::USAGE_ERROR end |
#output_connection_error(message) ⇒ void
This method returns an undefined value.
Outputs connection error message.
178 179 180 |
# File 'lib/pvectl/commands/top/command.rb', line 178 def output_connection_error() $stderr.puts "Error: #{message}" end |
#unknown_resource_error ⇒ Integer
Outputs error for unknown resource type.
156 157 158 159 160 |
# File 'lib/pvectl/commands/top/command.rb', line 156 def unknown_resource_error $stderr.puts "Unknown resource type: #{@resource_type}" $stderr.puts "Available resources: nodes, vms, containers" ExitCodes::USAGE_ERROR end |