Class: Fractor::ResultAggregator
- Inherits:
-
Object
- Object
- Fractor::ResultAggregator
- Defined in:
- lib/fractor/result_aggregator.rb
Overview
Aggregates results and errors from worker Ractors.
Instance Attribute Summary collapse
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
-
#results ⇒ Object
readonly
Returns the value of attribute results.
Instance Method Summary collapse
- #add_result(result) ⇒ Object
-
#errors_summary ⇒ Hash
Get a summary of errors.
-
#initialize ⇒ ResultAggregator
constructor
A new instance of ResultAggregator.
- #inspect ⇒ Object
-
#on_new_result(&callback) ⇒ Object
Register a callback to be called when a new result is added.
- #to_s ⇒ Object
Constructor Details
#initialize ⇒ ResultAggregator
Returns a new instance of ResultAggregator.
8 9 10 11 12 |
# File 'lib/fractor/result_aggregator.rb', line 8 def initialize @results = [] @errors = [] @result_callbacks = [] end |
Instance Attribute Details
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
6 7 8 |
# File 'lib/fractor/result_aggregator.rb', line 6 def errors @errors end |
#results ⇒ Object (readonly)
Returns the value of attribute results.
6 7 8 |
# File 'lib/fractor/result_aggregator.rb', line 6 def results @results end |
Instance Method Details
#add_result(result) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/fractor/result_aggregator.rb', line 14 def add_result(result) if result.success? puts "Work completed successfully: Result: #{result.result}" if ENV["FRACTOR_DEBUG"] @results << result else puts "Error processing work: #{result}" if ENV["FRACTOR_DEBUG"] @errors << result end # Call any registered callbacks with the new result @result_callbacks.each { |callback| callback.call(result) } end |
#errors_summary ⇒ Hash
Get a summary of errors
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 |
# File 'lib/fractor/result_aggregator.rb', line 45 def errors_summary return {} if @errors.empty? # Group errors by category by_category = @errors.group_by do |e| e.error_category || :unknown end.transform_values(&:count) # Group errors by severity by_severity = @errors.group_by do |e| e.error_severity || :unknown end.transform_values(&:count) # Count error types (class names) error_types = @errors.map do |e| e.error&.class&.name || e.error&.class || "String" end.tally # Get unique error messages (first 10) = @errors.map { |e| e.error.to_s }.uniq.first(10) { total_errors: @errors.size, by_category: by_category, by_severity: by_severity, error_types: error_types, sample_messages: , } end |
#inspect ⇒ Object
36 37 38 39 40 41 |
# File 'lib/fractor/result_aggregator.rb', line 36 def inspect { results: @results.map(&:inspect), errors: @errors.map(&:inspect), } end |
#on_new_result(&callback) ⇒ Object
Register a callback to be called when a new result is added
28 29 30 |
# File 'lib/fractor/result_aggregator.rb', line 28 def on_new_result(&callback) @result_callbacks << callback end |
#to_s ⇒ Object
32 33 34 |
# File 'lib/fractor/result_aggregator.rb', line 32 def to_s "Results: #{@results.size}, Errors: #{@errors.size}" end |