Class: Pvectl::Presenters::Storage

Inherits:
Base
  • Object
show all
Defined in:
lib/pvectl/presenters/storage.rb

Overview

Presenter for Proxmox cluster storage pools.

Defines column layout and formatting for table output. Standard columns show essential storage info. Wide columns add content types and shared status.

Examples:

Using with formatter

presenter = Storage.new
formatter = Formatters::Table.new
output = formatter.format(storage_pools, presenter)

See Also:

Instance Method Summary collapse

Methods inherited from Base

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

Instance Method Details

#avail_displayString

Returns available storage formatted with appropriate unit (GB or TB).

Returns:

  • (String)

    formatted available storage (e.g., “55 GB” or “2.0 TB”) or “-”



184
185
186
187
188
# File 'lib/pvectl/presenters/storage.rb', line 184

def avail_display
  return "-" unless storage.active? && avail_gb

  format_size(avail_gb)
end

#avail_gbFloat?

Returns available bytes in GB.

Returns:

  • (Float, nil)

    available bytes in GB, or nil if unavailable



175
176
177
178
179
# File 'lib/pvectl/presenters/storage.rb', line 175

def avail_gb
  return nil if storage.avail.nil?

  (storage.avail.to_f / 1024 / 1024 / 1024).round(1)
end

#columnsArray<String>

Returns column headers for standard table output.

Returns:

  • (Array<String>)

    column headers



23
24
25
# File 'lib/pvectl/presenters/storage.rb', line 23

def columns
  %w[NAME TYPE STATUS USED TOTAL %USED NODE]
end

#content_displayString

Returns content types for display.

Returns:

  • (String)

    comma-separated content types or “-”



220
221
222
# File 'lib/pvectl/presenters/storage.rb', line 220

def content_display
  storage.content.nil? || storage.content.empty? ? "-" : storage.content
end

#disk_total_gbFloat?

Returns total disk in GB.

Returns:

  • (Float, nil)

    total disk in GB, or nil if unavailable



157
158
159
160
161
# File 'lib/pvectl/presenters/storage.rb', line 157

def disk_total_gb
  return nil if storage.maxdisk.nil?

  (storage.maxdisk.to_f / 1024 / 1024 / 1024).round(1)
end

#disk_used_gbFloat?

Returns disk used in GB.

Returns:

  • (Float, nil)

    disk used in GB, or nil if unavailable



148
149
150
151
152
# File 'lib/pvectl/presenters/storage.rb', line 148

def disk_used_gb
  return nil if storage.disk.nil?

  (storage.disk.to_f / 1024 / 1024 / 1024).round(1)
end

#extra_columnsArray<String>

Returns additional column headers for wide output.

Returns:

  • (Array<String>)

    extra column headers



30
31
32
# File 'lib/pvectl/presenters/storage.rb', line 30

def extra_columns
  %w[CONTENT SHARED]
end

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

Returns additional values for wide output.

Parameters:

  • model (Models::Storage)

    Storage model

  • context (Hash)

    optional context

Returns:

  • (Array<String>)

    extra values matching extra_columns order



57
58
59
60
61
62
63
# File 'lib/pvectl/presenters/storage.rb', line 57

def extra_values(model, **_context)
  @storage = model
  [
    content_display,
    shared_display
  ]
end

#node_displayString

Returns node display. Shows “-” for shared storage (available on multiple nodes).

Returns:

  • (String)

    node name or “-” for shared storage



141
142
143
# File 'lib/pvectl/presenters/storage.rb', line 141

def node_display
  storage.shared? ? "-" : (storage.node || "-")
end

#shared_displayString

Returns shared status for display.

Returns:

  • (String)

    “yes” or “no”



227
228
229
# File 'lib/pvectl/presenters/storage.rb', line 227

def shared_display
  storage.shared? ? "yes" : "no"
end

#status_displayString

Returns status for display. Maps “available” to “active” for consistency with kubectl style.

Returns:

  • (String)

    status (active, inactive)



133
134
135
# File 'lib/pvectl/presenters/storage.rb', line 133

def status_display
  storage.active? ? "active" : "inactive"
end

#to_description(model) ⇒ Hash

Converts Storage model to description format for describe command.

Returns a structured Hash with sections for kubectl-style vertical output. Nested Hashes create indented subsections. Arrays of Hashes render as inline tables.

Parameters:

Returns:

  • (Hash)

    structured hash for describe formatter



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/pvectl/presenters/storage.rb', line 98

def to_description(model)
  @storage = model

  {
    "Name" => storage.name,
    "Type" => type_display,
    "Status" => status_display,
    "Shared" => shared_display,
    "Nodes" => nodes_display,

    "Capacity" => build_capacity_section,

    "Configuration" => build_configuration_section,

    "Content Summary" => format_content_summary,

    "Backup Retention" => format_backup_retention
  }
end

#to_hash(model) ⇒ Hash

Converts Storage model to hash for JSON/YAML output.

Returns a structured hash with nested objects for complex data.

Parameters:

Returns:

  • (Hash)

    hash representation with string keys



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/pvectl/presenters/storage.rb', line 71

def to_hash(model)
  @storage = model
  {
    "name" => storage.name,
    "type" => storage.plugintype,
    "status" => storage.status,
    "node" => storage.node,
    "shared" => storage.shared?,
    "content" => storage.content,
    "disk" => {
      "used_bytes" => storage.disk,
      "total_bytes" => storage.maxdisk,
      "used_gb" => disk_used_gb,
      "total_gb" => disk_total_gb,
      "usage_percent" => usage_percent
    }
  }
end

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

Converts Storage model to table row values.

Parameters:

  • model (Models::Storage)

    Storage model

  • context (Hash)

    optional context

Returns:

  • (Array<String>)

    row values matching columns order



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/pvectl/presenters/storage.rb', line 39

def to_row(model, **_context)
  @storage = model
  [
    storage.name,
    type_display,
    status_display,
    used_display,
    total_display,
    usage_display,
    node_display
  ]
end

#total_displayString

Returns total storage formatted with appropriate unit (GB or TB).

Returns:

  • (String)

    formatted total storage (e.g., “100 GB” or “2.0 TB”) or “-”



202
203
204
205
206
# File 'lib/pvectl/presenters/storage.rb', line 202

def total_display
  return "-" if disk_total_gb.nil?

  format_size(disk_total_gb)
end

#type_displayString

Returns storage type for display.

Returns:

  • (String)

    storage plugin type or “-”



125
126
127
# File 'lib/pvectl/presenters/storage.rb', line 125

def type_display
  storage.plugintype || "-"
end

#usage_displayString

Returns usage percentage for display.

Returns:

  • (String)

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



211
212
213
214
215
# File 'lib/pvectl/presenters/storage.rb', line 211

def usage_display
  return "-" unless storage.active? && usage_percent

  "#{usage_percent}%"
end

#usage_percentInteger?

Returns storage usage percentage.

Returns:

  • (Integer, nil)

    usage percentage (0-100), or nil if unavailable



166
167
168
169
170
# File 'lib/pvectl/presenters/storage.rb', line 166

def usage_percent
  return nil if storage.maxdisk.nil? || storage.maxdisk.zero? || storage.disk.nil?

  ((storage.disk.to_f / storage.maxdisk) * 100).round
end

#used_displayString

Returns used storage formatted with appropriate unit (GB or TB).

Returns:

  • (String)

    formatted used storage (e.g., “45 GB” or “1.2 TB”) or “-”



193
194
195
196
197
# File 'lib/pvectl/presenters/storage.rb', line 193

def used_display
  return "-" unless storage.active? && disk_used_gb

  format_size(disk_used_gb)
end