Class: Pvectl::Presenters::SnapshotOperationResult

Inherits:
Base
  • Object
show all
Includes:
Formatters::ColorSupport
Defined in:
lib/pvectl/presenters/snapshot_operation_result.rb,
sig/pvectl/presenters/snapshot_operation_result.rbs

Overview

Presenter for snapshot operation results.

Formats OperationResult models from snapshot operations for table/JSON/YAML output. Unlike OperationResult presenter, this handles resource hash instead of VM model.

Examples:

Using with formatter

presenter = SnapshotOperationResult.new
formatter = Formatters::Table.new
output = formatter.format(results, presenter)

Constant Summary

Constants included from Formatters::ColorSupport

Formatters::ColorSupport::STATUS_COLORS

Instance Method Summary collapse

Methods included from Formatters::ColorSupport

colorize_status, enabled?, pastel

Methods inherited from Base

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

Instance Method Details

#colorize(text, color) ⇒ String

Colorizes text using Pastel.

Parameters:

  • text (String)

    text to colorize

  • color (Symbol)

    color name (:green, :red, :yellow)

Returns:

  • (String)

    colored text



119
120
121
122
# File 'lib/pvectl/presenters/snapshot_operation_result.rb', line 119

def colorize(text, color)
  pastel = Formatters::ColorSupport.pastel(explicit_flag: nil)
  pastel.public_send(color, text)
end

#columnsArray<String>

Returns column headers for standard table output.

Returns:

  • (Array<String>)

    column headers



21
22
23
# File 'lib/pvectl/presenters/snapshot_operation_result.rb', line 21

def columns
  %w[VMID NAME TYPE NODE STATUS MESSAGE]
end

#duration_display(model) ⇒ String

Returns formatted duration.

Parameters:

Returns:

  • (String)

    duration or "-"



107
108
109
110
111
112
# File 'lib/pvectl/presenters/snapshot_operation_result.rb', line 107

def duration_display(model)
  duration = model.task&.duration
  return "-" unless duration

  "#{duration.to_f.round(1)}s"
end

#extra_columnsArray<String>

Returns additional columns for wide output.

Returns:

  • (Array<String>)

    extra column headers



28
29
30
# File 'lib/pvectl/presenters/snapshot_operation_result.rb', line 28

def extra_columns
  %w[TASK DURATION]
end

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

Returns additional values for wide output.

Parameters:

Returns:

  • (Array<String>)

    extra values



54
55
56
57
58
59
# File 'lib/pvectl/presenters/snapshot_operation_result.rb', line 54

def extra_values(model, **_context)
  [
    task_upid(model),
    duration_display(model)
  ]
end

#status_display(model) ⇒ String

Returns colored status display.

Parameters:

Returns:

  • (String)

    colored status



85
86
87
88
89
90
91
92
93
# File 'lib/pvectl/presenters/snapshot_operation_result.rb', line 85

def status_display(model)
  case model.status_text
  when "Success" then colorize(model.status_text, :green)
  when "Failed" then colorize(model.status_text, :red)
  when "Pending" then colorize(model.status_text, :yellow)
  when "Partial" then colorize(model.status_text, :bright_yellow)
  else model.status_text
  end
end

#task_upid(model) ⇒ String

Returns task UPID or dash.

Parameters:

Returns:

  • (String)

    UPID or "-"



99
100
101
# File 'lib/pvectl/presenters/snapshot_operation_result.rb', line 99

def task_upid(model)
  model.task_upid || model.task&.upid || "-"
end

#to_hash(model) ⇒ Hash

Converts result to hash for JSON/YAML output.

Parameters:

Returns:

  • (Hash)

    hash representation



65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/pvectl/presenters/snapshot_operation_result.rb', line 65

def to_hash(model)
  resource = model.resource || {}
  {
    "vmid" => resource[:vmid],
    "name" => resource[:name],
    "type" => resource[:type]&.to_s,
    "node" => resource[:node],
    "operation" => model.operation&.to_s,
    "status" => model.status_text,
    "message" => model.message,
    "task_upid" => model.task_upid || model.task&.upid
  }
end

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

Converts result to table row values.

Parameters:

Returns:

  • (Array<String>)

    row values



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/pvectl/presenters/snapshot_operation_result.rb', line 37

def to_row(model, **_context)
  resource = model.resource || {}
  [
    resource[:vmid].to_s,
    resource[:name] || "-",
    resource[:type]&.to_s || "-",
    resource[:node] || "-",
    status_display(model),
    model.message
  ]
end