Class: Pvectl::Presenters::TopVm

Inherits:
Vm
  • Object
show all
Includes:
TopPresenter
Defined in:
lib/pvectl/presenters/top_vm.rb

Overview

Presenter for VM resource usage metrics (top command).

Inherits from Presenters::Vm for reuse of formatting methods. Includes TopPresenter for shared metrics display. Focuses on CPU, memory, disk, and network utilization.

Examples:

Using with formatter

presenter = TopVm.new
formatter = Formatters::Table.new
output = formatter.format(vms, presenter)

See Also:

Instance Method Summary collapse

Methods included from TopPresenter

#cpu_cores_value, #cpu_usage_value, #percent_display, #percent_value

Methods inherited from Vm

#cpu_percent, #disk_display, #disk_total_gb, #disk_used_gb, #display_name, #memory_display, #memory_total_gb, #memory_used_gb, #to_description

Methods inherited from Base

#tags_array, #tags_display, #template_display, #to_description, #to_wide_row, #uptime_human, #wide_columns

Instance Method Details

#columnsArray<String>

Returns column headers for standard table output.

Returns:

  • (Array<String>)

    column headers



26
27
28
# File 'lib/pvectl/presenters/top_vm.rb', line 26

def columns
  %w[VMID NAME NODE CPU(cores) CPU% MEMORY MEMORY%]
end

#extra_columnsArray<String>

Returns additional column headers for wide output.

Returns:

  • (Array<String>)

    extra column headers



33
34
35
# File 'lib/pvectl/presenters/top_vm.rb', line 33

def extra_columns
  %w[DISK DISK% NETIN NETOUT]
end

#extra_values(model, **_context) ⇒ Array<String>

Returns additional values for wide output.

Parameters:

  • model (Models::Vm)

    VM model

  • context (Hash)

    optional context

Returns:

  • (Array<String>)

    extra values matching extra_columns order



60
61
62
63
64
65
66
67
68
# File 'lib/pvectl/presenters/top_vm.rb', line 60

def extra_values(model, **_context)
  @vm = model
  [
    disk_display,
    percent_display(vm.disk, vm.maxdisk),
    format_bytes(vm.netin),
    format_bytes(vm.netout)
  ]
end

#to_hash(model) ⇒ Hash

Converts VM model to hash for JSON/YAML output.

Returns metrics-focused hash without operational info (no status, template, tags, ha, uptime).

Parameters:

Returns:

  • (Hash)

    metrics-focused hash representation



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/pvectl/presenters/top_vm.rb', line 77

def to_hash(model)
  @vm = model
  {
    "vmid" => vm.vmid,
    "name" => vm.name,
    "node" => vm.node,
    "cpu" => {
      "usage_percent" => vm.cpu.nil? ? nil : (vm.cpu * 100).round,
      "cores" => vm.maxcpu
    },
    "memory" => {
      "used_bytes" => vm.mem,
      "total_bytes" => vm.maxmem,
      "usage_percent" => percent_value(vm.mem, vm.maxmem)
    },
    "disk" => {
      "used_bytes" => vm.disk,
      "total_bytes" => vm.maxdisk,
      "usage_percent" => percent_value(vm.disk, vm.maxdisk)
    },
    "network" => {
      "in_bytes" => vm.netin,
      "out_bytes" => vm.netout
    }
  }
end

#to_row(model, **_context) ⇒ Array<String>

Converts VM model to table row with metrics values.

Parameters:

  • model (Models::Vm)

    VM model

  • context (Hash)

    optional context

Returns:

  • (Array<String>)

    row values matching columns order



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/pvectl/presenters/top_vm.rb', line 42

def to_row(model, **_context)
  @vm = model
  [
    vm.vmid.to_s,
    display_name,
    vm.node,
    cpu_cores_value(vm),
    cpu_usage_value(vm),
    memory_display,
    percent_display(vm.mem, vm.maxmem)
  ]
end