Module: Pvectl::Presenters::TopPresenter

Included in:
TopContainer, TopNode, TopVm
Defined in:
lib/pvectl/presenters/top_presenter.rb

Overview

Shared metrics formatting for top command presenters.

Provides common display methods for CPU cores, CPU usage percentage, and generic percentage calculations used across TopNode, TopVm, and TopContainer presenters.

Examples:

Including in a presenter

class TopVm < Vm
  include TopPresenter
end

Instance Method Summary collapse

Instance Method Details

#cpu_cores_value(resource) ⇒ String

Returns CPU core count for display.

Parameters:

  • resource (Object)

    model with maxcpu attribute

Returns:

  • (String)

    core count or “-” if unavailable



21
22
23
24
25
# File 'lib/pvectl/presenters/top_presenter.rb', line 21

def cpu_cores_value(resource)
  return "-" if resource.maxcpu.nil?

  resource.maxcpu.to_s
end

#cpu_usage_value(resource) ⇒ String

Returns CPU usage as percentage string.

Parameters:

  • resource (Object)

    model with cpu attribute (0.0-1.0 fraction)

Returns:

  • (String)

    CPU percentage (e.g., “23%”) or “-” if unavailable



31
32
33
34
35
# File 'lib/pvectl/presenters/top_presenter.rb', line 31

def cpu_usage_value(resource)
  return "-" if resource.cpu.nil?

  "#{(resource.cpu * 100).round}%"
end

#percent_display(used, total) ⇒ String

Returns percentage display string from used/total values.

Parameters:

  • used (Numeric, nil)

    used amount

  • total (Numeric, nil)

    total amount

Returns:

  • (String)

    percentage (e.g., “45%”) or “-” if unavailable



42
43
44
45
# File 'lib/pvectl/presenters/top_presenter.rb', line 42

def percent_display(used, total)
  pct = percent_value(used, total)
  pct ? "#{pct}%" : "-"
end

#percent_value(used, total) ⇒ Integer?

Calculates percentage as integer from used/total values.

Parameters:

  • used (Numeric, nil)

    used amount

  • total (Numeric, nil)

    total amount

Returns:

  • (Integer, nil)

    percentage or nil if unavailable



52
53
54
55
56
# File 'lib/pvectl/presenters/top_presenter.rb', line 52

def percent_value(used, total)
  return nil if used.nil? || total.nil? || total.zero?

  ((used.to_f / total) * 100).round
end