Class: Uniword::Batch::BatchResult
- Inherits:
-
Object
- Object
- Uniword::Batch::BatchResult
- Defined in:
- lib/uniword/batch/batch_result.rb
Overview
Aggregates and reports results from batch document processing.
Responsibility: Track processing results, statistics, and failures. Single Responsibility - only manages result aggregation and reporting.
Instance Attribute Summary collapse
-
#end_time ⇒ Object
readonly
Returns the value of attribute end_time.
-
#failures ⇒ Object
readonly
Returns the value of attribute failures.
-
#start_time ⇒ Object
readonly
Returns the value of attribute start_time.
-
#successes ⇒ Object
readonly
Returns the value of attribute successes.
Instance Method Summary collapse
-
#add_failure(file:, error:, stage: nil, metadata: {}) ⇒ self
Add a failed processing result.
-
#add_success(file:, duration: 0.0, stages: [], metadata: {}) ⇒ self
Add a successful processing result.
-
#average_duration ⇒ Float
Get average processing time per successful document.
-
#complete! ⇒ self
Mark processing as completed.
-
#elapsed_time ⇒ Float
Get elapsed processing time in seconds.
-
#export_csv(path) ⇒ self
Export results to CSV file.
-
#export_json(path) ⇒ self
Export results to JSON file.
-
#export_yaml(path) ⇒ self
Export results to YAML file.
-
#failure_count ⇒ Integer
Get number of failed files.
-
#initialize ⇒ BatchResult
constructor
Initialize batch result tracker.
-
#success? ⇒ Boolean
Check if batch processing was successful (no failures).
-
#success_count ⇒ Integer
Get number of successful files.
-
#success_rate ⇒ Float
Get success rate as percentage.
-
#summary ⇒ Hash
Get summary statistics.
-
#summary_text ⇒ String
Get formatted summary text.
-
#total_count ⇒ Integer
Get total number of files processed.
Constructor Details
#initialize ⇒ BatchResult
Initialize batch result tracker
27 28 29 30 31 32 |
# File 'lib/uniword/batch/batch_result.rb', line 27 def initialize @successes = [] @failures = [] @start_time = Time.now @end_time = nil end |
Instance Attribute Details
#end_time ⇒ Object (readonly)
Returns the value of attribute end_time.
24 25 26 |
# File 'lib/uniword/batch/batch_result.rb', line 24 def end_time @end_time end |
#failures ⇒ Object (readonly)
Returns the value of attribute failures.
24 25 26 |
# File 'lib/uniword/batch/batch_result.rb', line 24 def failures @failures end |
#start_time ⇒ Object (readonly)
Returns the value of attribute start_time.
24 25 26 |
# File 'lib/uniword/batch/batch_result.rb', line 24 def start_time @start_time end |
#successes ⇒ Object (readonly)
Returns the value of attribute successes.
24 25 26 |
# File 'lib/uniword/batch/batch_result.rb', line 24 def successes @successes end |
Instance Method Details
#add_failure(file:, error:, stage: nil, metadata: {}) ⇒ self
Add a failed processing result
67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/uniword/batch/batch_result.rb', line 67 def add_failure(file:, error:, stage: nil, metadata: {}) = error.is_a?(Exception) ? error. : error.to_s error_class = error.is_a?(Exception) ? error.class.name : "Error" @failures << { file: file, error: , error_class: error_class, stage: stage, metadata: , timestamp: Time.now, } self end |
#add_success(file:, duration: 0.0, stages: [], metadata: {}) ⇒ self
Add a successful processing result
49 50 51 52 53 54 55 56 57 58 |
# File 'lib/uniword/batch/batch_result.rb', line 49 def add_success(file:, duration: 0.0, stages: [], metadata: {}) @successes << { file: file, duration: duration, stages: stages, metadata: , timestamp: Time.now, } self end |
#average_duration ⇒ Float
Get average processing time per successful document
123 124 125 126 127 128 |
# File 'lib/uniword/batch/batch_result.rb', line 123 def average_duration return 0.0 if successes.empty? total = successes.sum { |s| s[:duration] } (total / successes.size).round(2) end |
#complete! ⇒ self
Mark processing as completed
37 38 39 40 |
# File 'lib/uniword/batch/batch_result.rb', line 37 def complete! @end_time = Time.now self end |
#elapsed_time ⇒ Float
Get elapsed processing time in seconds
115 116 117 118 |
# File 'lib/uniword/batch/batch_result.rb', line 115 def elapsed_time end_time = @end_time || Time.now (end_time - @start_time).round(2) end |
#export_csv(path) ⇒ self
Export results to CSV file
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 |
# File 'lib/uniword/batch/batch_result.rb', line 201 def export_csv(path) CSV.open(path, "wb") do |csv| # Header row csv << %w[File Status Duration Error Stage] # Success rows successes.each do |success| csv << [ success[:file], "SUCCESS", success[:duration], "", success[:stages].join(", "), ] end # Failure rows failures.each do |failure| csv << [ failure[:file], "FAILURE", "", failure[:error], failure[:stage] || "", ] end end self end |
#export_json(path) ⇒ self
Export results to JSON file
171 172 173 174 175 176 177 178 179 180 |
# File 'lib/uniword/batch/batch_result.rb', line 171 def export_json(path) data = { summary: summary, successes: successes, failures: failures, } File.write(path, JSON.pretty_generate(data)) self end |
#export_yaml(path) ⇒ self
Export results to YAML file
186 187 188 189 190 191 192 193 194 195 |
# File 'lib/uniword/batch/batch_result.rb', line 186 def export_yaml(path) data = { "summary" => summary, "successes" => successes.map { |s| stringify_keys(s) }, "failures" => failures.map { |f| stringify_keys(f) }, } File.write(path, YAML.dump(data)) self end |
#failure_count ⇒ Integer
Get number of failed files
99 100 101 |
# File 'lib/uniword/batch/batch_result.rb', line 99 def failure_count failures.size end |
#success? ⇒ Boolean
Check if batch processing was successful (no failures)
133 134 135 |
# File 'lib/uniword/batch/batch_result.rb', line 133 def success? failures.empty? end |
#success_count ⇒ Integer
Get number of successful files
92 93 94 |
# File 'lib/uniword/batch/batch_result.rb', line 92 def success_count successes.size end |
#success_rate ⇒ Float
Get success rate as percentage
106 107 108 109 110 |
# File 'lib/uniword/batch/batch_result.rb', line 106 def success_rate return 0.0 if total_count.zero? (success_count.to_f / total_count * 100).round(2) end |
#summary ⇒ Hash
Get summary statistics
140 141 142 143 144 145 146 147 148 149 |
# File 'lib/uniword/batch/batch_result.rb', line 140 def summary { total: total_count, successes: success_count, failures: failure_count, success_rate: success_rate, elapsed_time: elapsed_time, average_duration: average_duration, } end |
#summary_text ⇒ String
Get formatted summary text
154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/uniword/batch/batch_result.rb', line 154 def summary_text lines = [] lines << "Batch Processing Results" lines << ("=" * 40) lines << "Total files: #{total_count}" lines << "Successful: #{success_count}" lines << "Failed: #{failure_count}" lines << "Success rate: #{success_rate}%" lines << "Elapsed time: #{elapsed_time}s" lines << "Average duration: #{average_duration}s" if success_count.positive? lines.join("\n") end |
#total_count ⇒ Integer
Get total number of files processed
85 86 87 |
# File 'lib/uniword/batch/batch_result.rb', line 85 def total_count successes.size + failures.size end |