Class: Cloudcost::VolumeList

Inherits:
Object
  • Object
show all
Includes:
CsvOutput, VolumeInfluxdbOutput
Defined in:
lib/cloudcost/commands/volume/volume_list.rb

Overview

volumeList represents a list of volumes and integrates several output methods

Instance Method Summary collapse

Methods included from VolumeInfluxdbOutput

#totals_influx_line_protocol, #volumes_attached_state

Methods included from CsvOutput

#groups_to_csv, #to_csv

Constructor Details

#initialize(volumes, options = {}) ⇒ VolumeList

Returns a new instance of VolumeList.



11
12
13
14
# File 'lib/cloudcost/commands/volume/volume_list.rb', line 11

def initialize(volumes, options = {})
  @volumes = volumes
  @options = options
end

Instance Method Details

#calculate_totals(volumes = @volumes) ⇒ Object



16
17
18
19
20
21
22
23
24
# File 'lib/cloudcost/commands/volume/volume_list.rb', line 16

def calculate_totals(volumes = @volumes)
  total = { size: 0, size_ssd: 0, size_bulk: 0, cost: 0.0 }
  volumes.each do |volume|
    total[:size] += volume.size_gb
    total["size_#{volume.type}".to_sym] += volume.size_gb if %w[ssd bulk].include? volume.type
    total[:cost] += volume.costs_per_day
  end
  total
end

#cost_tableObject



70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/cloudcost/commands/volume/volume_list.rb', line 70

def cost_table
  table = Terminal::Table.new do |t|
    t.title = "cloudscale.ch volume costs"
    t.title += " (#{@options[:profile]})" if @options[:profile]
    t.headings = headings
    t.rows = rows unless @options[:summary]
  end

  table.add_separator unless @options[:summary]
  table.add_row totals
  first_number_row = @options[:summary] ? 1 : 2
  (first_number_row..table.columns.size).each { |column| table.align_column(column, :right) }
  table
end

#headingsObject



44
45
46
47
48
49
50
51
# File 'lib/cloudcost/commands/volume/volume_list.rb', line 44

def headings
  headings = if @options[:summary]
               ["", "SSD [GB]", "Bulk [GB]", "Total [GB]"]
             else
               ["Name", "UUID", "Type", "Servers", "Tags", "Size [GB]"]
             end
  headings.concat ["CHF/day", "CHF/30-days"]
end

#rowsObject



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/cloudcost/commands/volume/volume_list.rb', line 53

def rows
  rows = []
  @volumes.sort_by(&:name).map do |volume|
    rows << [
      volume.name,
      volume.uuid,
      volume.type,
      volume.server_name,
      volume.tags_to_s,
      volume.size_gb,
      format("%.2f", volume.costs_per_day.round(2)),
      format("%.2f", (volume.costs_per_day * 30).round(2))
    ]
  end
  rows
end

#totals(volumes = @volumes) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/cloudcost/commands/volume/volume_list.rb', line 26

def totals(volumes = @volumes)
  total = calculate_totals(volumes)
  total_row = @options[:summary] ? %w[Total] : ["Total", "", "", "", ""]
  if @options[:summary]
    total_row.concat [
      total[:size_ssd],
      total[:size_bulk],
      total[:size]
    ]
  else
    total_row.concat [total[:size]]
  end
  total_row.concat [
    format("%.2f", total[:cost].round(2)),
    format("%.2f", (total[:cost] * 30).round(2))
  ]
end