Class: Cloudcost::ServerList

Inherits:
Object
  • Object
show all
Includes:
CsvOutput, ServerInfluxdbOutput, ServerTabularOutput
Defined in:
lib/cloudcost/commands/server/server_list.rb

Overview

ServerList represents a list of servers and integrates several output methods

Instance Method Summary collapse

Methods included from CsvOutput

#groups_to_csv, #to_csv

Methods included from ServerInfluxdbOutput

#grouped_influx_line_protocol

Methods included from ServerTabularOutput

#cost_table, #grouped_cost_table, #headings, #rows, #tags_table

Constructor Details

#initialize(servers, options = {}) ⇒ ServerList

Returns a new instance of ServerList.



10
11
12
13
# File 'lib/cloudcost/commands/server/server_list.rb', line 10

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

Instance Method Details

#calculate_totals(servers = @servers) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/cloudcost/commands/server/server_list.rb', line 15

def calculate_totals(servers = @servers)
  totals = { vcpu: 0, memory: 0, ssd: 0, bulk: 0, cost: 0.0 }
  servers.each do |server|
    totals[:vcpu] += server.vcpu_count
    totals[:memory] += server.memory_gb
    totals[:ssd] += server.storage_size(:ssd)
    totals[:bulk] += server.storage_size(:bulk)
    totals[:cost] += server.total_costs_per_day
  end
  totals
end

#grouped_costsObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/cloudcost/commands/server/server_list.rb', line 40

def grouped_costs
  no_tag = "<no-tag>"
  group_rows = @servers.group_by { |s| s.tags[@options[:group_by].to_sym] || no_tag }.map do |name, servers|
    server_groups_data(name, servers).values.flatten
  end
  group_rows.sort! { |a, b| a[0] == no_tag ? 1 : a[0] <=> b[0] }
  case @options[:output]
  when "csv"
    groups_to_csv(group_rows)
  when "influx"
    grouped_influx_line_protocol(group_rows)
  else
    grouped_cost_table(group_rows)
  end
end

#server_groups_data(name, servers) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/cloudcost/commands/server/server_list.rb', line 56

def server_groups_data(name, servers)
  data = { name: name, count: 0, vcpu: 0, memory: 0, ssd: 0, bulk: 0, costs_daily: 0 }
  servers.each do |server|
    data[:count] += 1
    data[:vcpu] += server.vcpu_count
    data[:memory] += server.memory_gb
    data[:ssd] += server.storage_size(:ssd)
    data[:bulk] += server.storage_size(:bulk)
    data[:costs_daily] += server.total_costs_per_day
  end
  data[:costs_monthly] = (data[:costs_daily] * 30).round(2)
  data[:costs_daily] = data[:costs_daily].round(2)
  data
end

#totals(servers = @servers) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/cloudcost/commands/server/server_list.rb', line 27

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