Class: Philiprehberger::Batch::Result
- Inherits:
-
Object
- Object
- Philiprehberger::Batch::Result
- Includes:
- Enumerable
- Defined in:
- lib/philiprehberger/batch/result.rb
Overview
Holds the outcome of a batch processing run.
Instance Attribute Summary collapse
-
#chunks ⇒ Integer
readonly
Number of chunks processed.
-
#elapsed ⇒ Float
readonly
Elapsed time in seconds.
-
#errors ⇒ Array<Hash>
readonly
Errors with :item and :error keys.
-
#processed ⇒ Integer
readonly
Number of items processed successfully.
-
#results ⇒ Array
readonly
Results collected from each processed item.
-
#total ⇒ Integer
readonly
Total number of items.
Instance Method Summary collapse
-
#counts ⇒ Hash
Count occurrences of each result value.
-
#each {|item_result| ... } ⇒ Enumerator
Iterate over all collected item results.
-
#flat_map {|item_result| ... } ⇒ Array
Map over all collected item results and flatten one level.
-
#group_by {|item_result| ... } ⇒ Hash
Group results by the return value of the block.
-
#halted? ⇒ Boolean
Check if processing was halted early by an error handler returning :halt.
-
#initialize(processed:, errors:, total:, chunks:, elapsed:, halted: false, results: [], chunk_times: []) ⇒ Result
constructor
A new instance of Result.
-
#success? ⇒ Boolean
Check if all items were processed without errors.
-
#success_rate ⇒ Float
Ratio of successfully processed items to total items.
-
#timing ⇒ Hash
Timing statistics for the batch run.
Constructor Details
#initialize(processed:, errors:, total:, chunks:, elapsed:, halted: false, results: [], chunk_times: []) ⇒ Result
Returns a new instance of Result.
35 36 37 38 39 40 41 42 43 44 |
# File 'lib/philiprehberger/batch/result.rb', line 35 def initialize(processed:, errors:, total:, chunks:, elapsed:, halted: false, results: [], chunk_times: []) @processed = processed @errors = errors @total = total @chunks = chunks @elapsed = elapsed @halted = halted @results = results @chunk_times = chunk_times end |
Instance Attribute Details
#chunks ⇒ Integer (readonly)
Returns number of chunks processed.
19 20 21 |
# File 'lib/philiprehberger/batch/result.rb', line 19 def chunks @chunks end |
#elapsed ⇒ Float (readonly)
Returns elapsed time in seconds.
22 23 24 |
# File 'lib/philiprehberger/batch/result.rb', line 22 def elapsed @elapsed end |
#errors ⇒ Array<Hash> (readonly)
Returns errors with :item and :error keys.
13 14 15 |
# File 'lib/philiprehberger/batch/result.rb', line 13 def errors @errors end |
#processed ⇒ Integer (readonly)
Returns number of items processed successfully.
10 11 12 |
# File 'lib/philiprehberger/batch/result.rb', line 10 def processed @processed end |
#results ⇒ Array (readonly)
Returns results collected from each processed item.
25 26 27 |
# File 'lib/philiprehberger/batch/result.rb', line 25 def results @results end |
#total ⇒ Integer (readonly)
Returns total number of items.
16 17 18 |
# File 'lib/philiprehberger/batch/result.rb', line 16 def total @total end |
Instance Method Details
#counts ⇒ Hash
Count occurrences of each result value.
79 80 81 |
# File 'lib/philiprehberger/batch/result.rb', line 79 def counts @results.tally end |
#each {|item_result| ... } ⇒ Enumerator
Iterate over all collected item results.
64 65 66 |
# File 'lib/philiprehberger/batch/result.rb', line 64 def each(&) @results.each(&) end |
#flat_map {|item_result| ... } ⇒ Array
Map over all collected item results and flatten one level.
72 73 74 |
# File 'lib/philiprehberger/batch/result.rb', line 72 def flat_map(&) @results.flat_map(&) end |
#group_by {|item_result| ... } ⇒ Hash
Group results by the return value of the block.
87 88 89 |
# File 'lib/philiprehberger/batch/result.rb', line 87 def group_by(&) @results.group_by(&) end |
#halted? ⇒ Boolean
Check if processing was halted early by an error handler returning :halt.
56 57 58 |
# File 'lib/philiprehberger/batch/result.rb', line 56 def halted? @halted end |
#success? ⇒ Boolean
Check if all items were processed without errors.
49 50 51 |
# File 'lib/philiprehberger/batch/result.rb', line 49 def success? @errors.empty? end |
#success_rate ⇒ Float
Ratio of successfully processed items to total items.
Returns 1.0 for empty batches (no items to fail).
96 97 98 99 100 |
# File 'lib/philiprehberger/batch/result.rb', line 96 def success_rate return 1.0 if @total.zero? @processed.to_f / @total end |
#timing ⇒ Hash
Timing statistics for the batch run.
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/philiprehberger/batch/result.rb', line 105 def timing if @chunk_times.empty? return { total: @elapsed, per_chunk: 0.0, per_item: 0.0, fastest_chunk: 0.0, slowest_chunk: 0.0 } end { total: @elapsed, per_chunk: @elapsed / @chunk_times.size, per_item: @total.zero? ? 0.0 : @elapsed / @total, fastest_chunk: @chunk_times.min, slowest_chunk: @chunk_times.max } end |