Module: Cloudcost::ServerTabularOutput

Included in:
ServerList
Defined in:
lib/cloudcost/commands/server/server_tabular_output.rb

Overview

Tabular output methods for the ServerList class

Instance Method Summary collapse

Instance Method Details

#cost_tableObject



66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/cloudcost/commands/server/server_tabular_output.rb', line 66

def cost_table
  table = Terminal::Table.new do |t|
    t.title = "cloudscale.ch server 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

#grouped_cost_table(group_rows) ⇒ Object



81
82
83
84
85
86
87
88
89
90
# File 'lib/cloudcost/commands/server/server_tabular_output.rb', line 81

def grouped_cost_table(group_rows)
  table = Terminal::Table.new do |t|
    t.title = "cloudscale.ch server costs grouped by tag \"#{@options[:group_by]}\""
    t.title += " (#{@options[:profile]})" if @options[:profile]
    t.headings = headings
  end
  table.rows = group_rows
  (1..table.columns.size).each { |column| table.align_column(column, :right) }
  table
end

#headingsObject



8
9
10
11
12
13
14
15
16
17
# File 'lib/cloudcost/commands/server/server_tabular_output.rb', line 8

def headings
  headings = if @options[:summary]
               [""]
             elsif @options[:group_by]
               %w[Group Servers]
             else
               %w[Name UUID Flavor Tags]
             end
  headings.concat ["vCPU's", "Memory [GB]", "SSD [GB]", "Bulk [GB]", "CHF/day", "CHF/30-days"]
end

#rowsObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/cloudcost/commands/server/server_tabular_output.rb', line 19

def rows
  rows = []
  @servers.sort_by(&:name).map do |server|
    rows << [
      server.name,
      server.uuid,
      server.flavor,
      server.tags_to_s,
      server.vcpu_count,
      server.memory_gb,
      server.storage_size(:ssd),
      server.storage_size(:bulk),
      format("%.2f", server.total_costs_per_day.round(2)),
      format("%.2f", (server.total_costs_per_day * 30).round(2))
    ]
  end
  rows
end

#tags_tableObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/cloudcost/commands/server/server_tabular_output.rb', line 51

def tags_table
  Terminal::Table.new do |t|
    t.title = "cloudscale.ch server tags"
    t.title += " (#{@options[:profile]})" if @options[:profile]
    t.headings = %w[Name UUID Tags]
    t.rows = @servers.sort_by(&:name).map do |server|
      [
        server.name,
        server.uuid,
        server.tags_to_s
      ]
    end
  end
end

#totals(servers = @servers) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/cloudcost/commands/server/server_tabular_output.rb', line 38

def totals(servers = @servers)
  totals = calculate_totals(servers)
  total_row = @options[:summary] ? %w[Total] : ["Total", "", "", ""]
  total_row.concat [
    totals[:vcpu],
    totals[:memory],
    totals[:ssd],
    totals[:bulk],
    format("%.2f", totals[:cost].round(2)),
    format("%.2f", (totals[:cost] * 30).round(2))
  ]
end