Class: Fractor::ResultAggregator

Inherits:
Object
  • Object
show all
Defined in:
lib/fractor/result_aggregator.rb

Overview

Aggregates results and errors from worker Ractors.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeResultAggregator

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

#errorsObject (readonly)

Returns the value of attribute errors.



6
7
8
# File 'lib/fractor/result_aggregator.rb', line 6

def errors
  @errors
end

#resultsObject (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_summaryHash

Get a summary of errors

Returns:

  • (Hash)

    Error summary with counts, categories, and other stats



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)
  unique_messages = @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: unique_messages,
  }
end

#inspectObject



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_sObject



32
33
34
# File 'lib/fractor/result_aggregator.rb', line 32

def to_s
  "Results: #{@results.size}, Errors: #{@errors.size}"
end