Class: ServerStatus::Report

Inherits:
Object
  • Object
show all
Defined in:
lib/server-status/report.rb

Constant Summary collapse

COLUMN_SPACING =
5

Instance Method Summary collapse

Constructor Details

#initialize(config, options, host_set) ⇒ Report

Returns a new instance of Report.



6
7
8
9
10
# File 'lib/server-status/report.rb', line 6

def initialize(config, options, host_set)
  @config   = config
  @options  = options
  @host_set = host_set
end

Instance Method Details

#generate_tableObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/server-status/report.rb', line 12

def generate_table
  str = "\n"

  # Gather failures
  failed_columns = if (failed_hosts = @host_set.hosts.reject(&:feteched_status?)).any?
    failed_hosts.map do |host|
      { name: host.name, error: host.status[:error].strip }
    end
  end


  columns = [ :host ] + @host_set.hosts.detect(&:feteched_status?).status.keys

  # Process status values
  statuses = @host_set.hosts.select(&:feteched_status?).map do |host|
    host.status.each_with_object({ host: host.name }) do |(k, v), hash|
      hash[k] = send("format_#{k}", v)
    end
  end

  # Determine column widths
  column_widths = columns.each_with_object({}) do |col, hash|
    sizes  = statuses.map { |s| s[col].uncolorize.size }
    sizes << col.to_s.size

    hash[col] = sizes.max
  end


  # Render headings
  column_widths.each do |col, col_width|
    str << col.to_s.upcase.colorize(:blue)
    str << ' ' * (COLUMN_SPACING + col_width - col.size)
  end

  str << "\n"


  # Render table
  statuses.sort { |x,y| x[:host] <=> y[:host] }.each do |status|
    column_widths.each do |col, col_width|
      str << status[col]
      str << ' ' * (COLUMN_SPACING + col_width - status[col].uncolorize.size)
    end

    str << "\n"
  end

  # Display failures at the end
  if failed_columns
    str << "\nFAILED HOSTS".colorize(:blue)
    str << "\n"
    failed_columns.each do |host|
      str << host[:name]
      str << ' ' * (COLUMN_SPACING + column_widths[:host] - host[:name].uncolorize.size)
      str << host[:error].colorize(:red)
      str << "\n"
    end
  end

  str << "\n"
  str
end