Class: Makit::Humanize

Inherits:
Object
  • Object
show all
Defined in:
lib/makit/humanize.rb

Overview

Provides utilities for converting data into human-readable formats.

This class handles formatting of file sizes, timestamps, durations, and build result summaries for display to users.

Examples:

Basic usage

Makit::Humanize.get_humanized_size(1024)  # => "1.00 KB"
Makit::Humanize.get_humanized_duration(3661)  # => "1 hour, 1 minute, 1 second"

Class Method Summary collapse

Class Method Details

.get_command_details(command) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/makit/humanize.rb', line 69

def self.get_command_details(command)
  summary = "#{get_command_summary(command)}\n"
  summary += "  Name: #{command.name}\n"
  summary += "  Arguments: #{command.arguments.join(" ")}\n"
  summary += "  Directory: #{command.directory}\n"
  summary += "  Exit Code: #{command.exit_code}\n"
  if command.output.length.positive?
    summary += "  Output:\n"
    summary += indent_string(command.output, 4)
    summary += "\n"
  end
  if command.error.length.positive?
    summary += "  Error:\n"
    summary += indent_string(command.error, 4)
    summary += "\n"
  end
  summary
end

.get_command_summary(command) ⇒ Object



62
63
64
65
66
67
# File 'lib/makit/humanize.rb', line 62

def self.get_command_summary(command)
  symbol = Makit::Symbols.warning
  symbol = Makit::Symbols.checkmark if !command.exit_code.nil? && command.exit_code.zero?
  symbol = Makit::Symbols.error if command.exit_code != 0
  "#{symbol} #{command.name} #{command.arguments.join(" ")}"
end

.get_commands(commands) ⇒ Object



54
55
56
57
58
59
60
# File 'lib/makit/humanize.rb', line 54

def self.get_commands(commands)
  message = ""
  commands.each do |command|
    message += Makit::Humanize.get_command_details(command)
  end
  message
end

.get_humanized_duration(seconds_value) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/makit/humanize.rb', line 104

def self.get_humanized_duration(seconds_value)
  minutes = (seconds_value / 60).to_i
  seconds = (seconds_value % 60).to_i
  hours = (minutes / 60).to_i
  minutes %= 60
  days = (hours / 24).to_i
  hours %= 24
  milliseconds = (seconds_value % 1 * 1000).to_i

  parts = []
  parts << "#{days} days" if days.positive?
  parts << "#{hours} hours" if hours.positive?
  if minutes.positive?
    parts << if minutes == 1
      "1 minute"
    else
      "#{minutes} minutes"
    end
  end
  if seconds.positive?
    parts << if seconds == 1
      "1 second"
    else
      "#{seconds} seconds"
    end
  end
  # parts << "#{seconds} seconds" if seconds > 0

  parts << "#{milliseconds} milliseconds" if milliseconds.positive? && seconds < 1

  parts << "0 seconds" if parts.empty?
  parts.join(", ")
end

.get_humanized_size(bytes, precision = 2) ⇒ Object



15
16
17
18
19
20
21
22
23
24
# File 'lib/makit/humanize.rb', line 15

def self.get_humanized_size(bytes, precision = 2)
  units = %w[B KB MB GB TB PB]
  return "0 B" if bytes.zero?

  exp = (Math.log(bytes) / Math.log(1024)).to_i
  exp = units.size - 1 if exp >= units.size

  size = bytes.to_f / (1024 ** exp)
  format("%.#{precision}f %s", size, units[exp])
end

.get_humanized_timestamp(timestamp) ⇒ Object



26
27
28
29
30
# File 'lib/makit/humanize.rb', line 26

def self.get_humanized_timestamp(timestamp)
  return timestamp.strftime("%Y-%m-%d %I:%M:%S %p") if timestamp.respond_to?(:strftime)

  timestamp.strftime("%Y-%m-%d %H:%M:%S")
end

.get_job_execution_details(job_result) ⇒ Object



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/makit/humanize.rb', line 180

def self.get_job_execution_details(job_result)
  details = "Job: #{job_result.job_name}\n"
  details += "  Status: #{job_result.status}\n"
  details += "  Started: #{job_result.started_at}\n"
  details += "  Finished: #{job_result.finished_at}\n" if job_result.finished_at
  details += "  Exit Code: #{job_result.exit_code}\n" if job_result.exit_code
  
  if job_result.errors && job_result.errors.any?
    details += "  Errors:\n"
    job_result.errors.each do |error|
      details += "    - #{error}\n"
    end
  end
  
  if job_result.logs && job_result.logs.any?
    details += "  Logs:\n"
    job_result.logs.each do |log|
      details += "    #{log}\n"
    end
  end
  
  if job_result.respond_to?(:stdout) && job_result.stdout && !job_result.stdout.empty?
    details += "  Stdout:\n"
    job_result.stdout.split("\n").each do |line|
      details += "    #{line}\n"
    end
  end
  
  if job_result.respond_to?(:stderr) && job_result.stderr && !job_result.stderr.empty?
    details += "  Stderr:\n"
    job_result.stderr.split("\n").each do |line|
      details += "    #{line}\n"
    end
  end
  
  details
end

.get_make_result_summary(make_result) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/makit/humanize.rb', line 32

def self.get_make_result_summary(make_result)
  summary = "Make Result\n"
  summary += "  Repository: #{make_result.repository}\n"
  summary += "  Commit: #{make_result.commit}\n"
  summary += "  Branch: #{make_result.branch}\n"
  summary += "  Tag: #{make_result.tag}\n"
  summary += "  Device: #{make_result.device}\n"
  summary += "  Runtime Identifier: #{make_result.runtime_identifier}\n"
  summary += "  Initial Size: #{get_humanized_size(make_result.initial_size)}\n"
  summary += "  Final Size: #{get_humanized_size(make_result.final_size)}\n"
  summary += "  Delta Size: #{get_humanized_size(make_result.final_size - make_result.initial_size)}\n"
  summary += "  Commands: (#{make_result.commands.length})\n"
  make_result.commands.each do |command|
    details = get_command_details(command)
    summary += "\n"
    summary += indent_string(details, 4)
    summary += "\n"
  end

  summary
end

.get_pipeline_execution_summary(execution_result) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/makit/humanize.rb', line 137

def self.get_pipeline_execution_summary(execution_result)
  summary = "Pipeline Execution Summary\n"
  summary += "  Execution ID: #{execution_result.execution_id}\n"
  summary += "  Status: #{execution_result.status}\n"
  summary += "  Started: #{execution_result.started_at}\n"
  summary += "  Finished: #{execution_result.finished_at}\n" if execution_result.finished_at
  
  if execution_result.respond_to?(:total_duration_seconds) && execution_result.total_duration_seconds
    summary += "  Duration: #{get_humanized_duration(execution_result.total_duration_seconds)}\n"
  end
  
  if execution_result.respond_to?(:podman_version) && execution_result.podman_version
    summary += "  Podman Version: #{execution_result.podman_version}\n"
  end
  
  if execution_result.respond_to?(:execution_host) && execution_result.execution_host
    summary += "  Host: #{execution_result.execution_host}\n"
  end
  
  summary += "  Jobs: (#{execution_result.job_results.length})\n"
  execution_result.job_results.each do |job_result|
    summary += "\n"
    summary += indent_string(get_job_execution_details(job_result), 4)
    summary += "\n"
  end
  
  if execution_result.errors && execution_result.errors.any?
    summary += "\n  Errors:\n"
    execution_result.errors.each do |error|
      summary += "    - #{error}\n"
    end
  end
  
  if execution_result.warnings && execution_result.warnings.any?
    summary += "\n  Warnings:\n"
    execution_result.warnings.each do |warning|
      summary += "    - #{warning}\n"
    end
  end
  
  summary
end

.get_protobuf_duration(duration) ⇒ Object



96
97
98
99
100
101
102
# File 'lib/makit/humanize.rb', line 96

def self.get_protobuf_duration(duration)
  total_seconds = duration.seconds + (duration.nanos / 1_000_000_000.0)
  hours = (total_seconds / 3600).to_i
  minutes = ((total_seconds % 3600) / 60).to_i
  seconds = (total_seconds % 60).round(2)
  "#{hours}h #{minutes}m #{seconds}s"
end

.get_protobuf_timestamp(timestamp) ⇒ Object



92
93
94
# File 'lib/makit/humanize.rb', line 92

def self.get_protobuf_timestamp(timestamp)
  Time.at(timestamp.seconds, timestamp.nanos / 1000.0).strftime("%Y-%m-%d %H:%M:%S")
end

.indent_string(string, spaces) ⇒ Object



88
89
90
# File 'lib/makit/humanize.rb', line 88

def self.indent_string(string, spaces)
  string.split("\n").map { |line| (" " * spaces) + line }.join("\n")
end