Class: Pvectl::Presenters::Disk

Inherits:
Base
  • Object
show all
Defined in:
lib/pvectl/presenters/disk.rb,
sig/pvectl/presenters/disk.rbs

Overview

Presenter for physical disks on Proxmox nodes.

Defines column layout and formatting for table output. Standard columns show device, model, size, type, health, and usage. Wide columns add serial, vendor, WWN, GPT, and mount status.

Examples:

Using with formatter

presenter = Disk.new
formatter = Formatters::Table.new
output = formatter.format(disks, presenter)

See Also:

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#format_bytes, #format_firewall, #format_firewall_rules, #format_task_history, #resource, #tags_array, #tags_display, #template_display, #to_wide_row, #uptime_human, #wide_columns

Instance Attribute Details

#diskModels::PhysicalDisk (readonly)



105
106
107
# File 'lib/pvectl/presenters/disk.rb', line 105

def disk
  @disk
end

Instance Method Details

#columnsArray<String>

Returns column headers for standard table output.



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

def columns
  %w[NODE DEVICE MODEL SIZE TYPE HEALTH USED]
end

#device_info_sectionHash{String => String}

Builds the Device Info section hash.



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/pvectl/presenters/disk.rb', line 134

def device_info_section
  info = {
    "Node"    => disk.node || "-",
    "Device"  => disk.devpath || "-",
    "Model"   => disk.model || "-",
    "Serial"  => disk.serial || "-",
    "Vendor"  => disk.vendor || "-",
    "Size"    => format_size,
    "Type"    => disk.type || "-",
    "Health"  => disk.health || "-"
  }
  info["Life Remaining"] = "#{disk.wearout}%" if disk.wearout
  info["Used"]    = disk.used || "-"
  info["GPT"]     = format_boolean(disk.gpt)
  info["WWN"]     = disk.wwn || "-"
  info
end

#extra_columnsArray<String>

Returns additional column headers for wide output.



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

def extra_columns
  %w[SERIAL VENDOR WWN GPT MOUNTED]
end

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

Returns additional values for wide output.



57
58
59
60
61
62
63
64
65
66
# File 'lib/pvectl/presenters/disk.rb', line 57

def extra_values(model, **_context)
  @disk = model
  [
    disk.serial || "-",
    disk.vendor || "-",
    disk.wwn || "-",
    format_boolean(disk.gpt),
    format_boolean(disk.mounted)
  ]
end

#format_ata_attributesArray<Hash{String => String}>

Formats ATA SMART attributes for table display.



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/pvectl/presenters/disk.rb', line 167

def format_ata_attributes
  return [] unless disk.smart_attributes

  disk.smart_attributes.map do |attr|
    {
      "ID"        => attr[:id].to_s,
      "Attribute" => attr[:name].to_s,
      "Value"     => attr[:value].to_s,
      "Worst"     => attr[:worst].to_s,
      "Threshold" => attr[:threshold].to_s,
      "Raw"       => attr[:raw].to_s,
      "Fail"      => attr[:fail].to_s
    }
  end
end

#format_boolean(value) ⇒ String

Formats boolean flag for display.



125
126
127
128
129
# File 'lib/pvectl/presenters/disk.rb', line 125

def format_boolean(value)
  return "-" if value.nil?

  value == 1 ? "yes" : "no"
end

#format_sizeString

Formats disk size with appropriate unit (GB or TB).



110
111
112
113
114
115
116
117
118
119
# File 'lib/pvectl/presenters/disk.rb', line 110

def format_size
  gb = disk.size_gb
  return "-" if gb.nil?

  if gb >= 1024
    "#{(gb / 1024).round(1)} TB"
  else
    "#{gb.round(0).to_i} GB"
  end
end

#smart_attributes_sectionArray<Hash{String => String}>

Builds SMART attributes section based on disk type.



155
156
157
158
159
160
161
162
# File 'lib/pvectl/presenters/disk.rb', line 155

def smart_attributes_section
  case disk.smart_type
  when "ata"
    format_ata_attributes
  else
    Pvectl::Parsers::SmartText.parse(disk.smart_text)
  end
end

#to_description(model) ⇒ Hash{String => Object}

Returns detailed description for describe command output.



95
96
97
98
99
100
# File 'lib/pvectl/presenters/disk.rb', line 95

def to_description(model)
  @disk = model
  result = { "Device Info" => device_info_section }
  result["SMART Attributes"] = smart_attributes_section if disk.smart_type
  result
end

#to_hash(model) ⇒ Hash

Converts PhysicalDisk model to hash for JSON/YAML output.



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

def to_hash(model)
  @disk = model
  {
    "node" => disk.node,
    "device" => disk.devpath,
    "model" => disk.model,
    "size_bytes" => disk.size,
    "size_gb" => disk.size_gb,
    "type" => disk.type,
    "health" => disk.health,
    "used" => disk.used,
    "serial" => disk.serial,
    "vendor" => disk.vendor,
    "wwn" => disk.wwn,
    "gpt" => disk.gpt? || false,
    "mounted" => disk.mounted? || false
  }
end

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

Converts PhysicalDisk model to table row values.



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

def to_row(model, **_context)
  @disk = model
  [
    disk.node || "-",
    disk.devpath || "-",
    disk.model || "-",
    format_size,
    disk.type || "-",
    disk.health || "-",
    disk.used || "-"
  ]
end