Class: Multisync::Summary

Inherits:
Object
  • Object
show all
Includes:
Colors
Defined in:
lib/multisync/summary.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Colors

#as_fail, #as_main, #as_note, #as_skipped, #as_success

Constructor Details

#initialize(tasks) ⇒ Summary

Returns a new instance of Summary.



7
8
9
# File 'lib/multisync/summary.rb', line 7

def initialize tasks
  @tasks = tasks
end

Instance Attribute Details

#tasksObject (readonly)

All tasks to include in the summary



5
6
7
# File 'lib/multisync/summary.rb', line 5

def tasks
  @tasks
end

Instance Method Details

#as_message(message) ⇒ Object



83
84
85
# File 'lib/multisync/summary.rb', line 83

def as_message message
  {value: message, colspan: 6}
end

#dataObject



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
# File 'lib/multisync/summary.rb', line 30

def data
  tasks.map do |task|
    result = task.result
    desc = [task.source_description, task.destination_description]

    case result[:action]
    when :run
      if result[:status]&.success?
        # successfull run
        stats = Multisync::RsyncStat.new(result[:stdout])
        [*desc, *stats.formatted_values.map { {value: as_success(_1), alignment: :right} }]
      else
        # failed or interrupted run
        [*desc, as_message(as_fail("Failed, for more information see details below"))]
        # [*desc, as_message(as_fail(result[:stderr] || "n/a").strip)]
      end

    when :skip
      # skiped sync
      [*desc, as_message(as_skipped(result[:skip_message]))]

    else
      # not executed
      [*desc, as_message(as_note("not executed"))]
    end
  end.push(
    [
      as_note("Total"),
      "",
      *Multisync::RsyncStat
        .formatted_totals
        .map { {value: as_note(_1), alignment: :right} }
    ]
  )
end

#failuresObject



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/multisync/summary.rb', line 66

def failures
  tasks
    .select { _1.result[:action] == :run && !_1.result[:status]&.success? }
    .flat_map do |task|
      [
        as_fail([task.source_description, task.destination_description].join(" --> ")),
        message_or_nil(task.result[:stdout]),
        message_or_nil(task.result[:stderr]),
        ""
      ].compact
    end
    # Add title if any failures
    .tap { _1.unshift "\n#{as_main("Failures")}" if _1.any? }
    # Return failures as string or nil
    .then { _1.any? ? _1.join("\n") : nil }
end

#headingsObject



23
24
25
26
27
28
# File 'lib/multisync/summary.rb', line 23

def headings
  %w[SOURCE DESTINATION FILES + - → ∑ ↑]
    .map(&method(:as_note))
    .zip(i[left left right right right right right right])
    .map { |v, a| {value: v, alignment: a} }
end

#message_or_nil(message) ⇒ Object



87
88
89
# File 'lib/multisync/summary.rb', line 87

def message_or_nil message
  (message.nil? || message.empty?) ? nil : message
end

#tableObject



19
20
21
# File 'lib/multisync/summary.rb', line 19

def table
  Terminal::Table.new(headings: headings, rows: data, style: table_style)
end

#table_styleObject



91
92
93
# File 'lib/multisync/summary.rb', line 91

def table_style
  {border_x: as_note("─"), border_y: "", border_i: "", border_top: false, border_bottom: false, padding_left: 0, padding_right: 3}
end

#to_sObject



11
12
13
14
15
16
17
# File 'lib/multisync/summary.rb', line 11

def to_s
  ["", as_main("Summary"), table.to_s, failures]
    .compact
    # HACK: does this fix the sporadic encoding errors?
    .map { _1.force_encoding("UTF-8") }
    .join("\n")
end