Class: Fractor::ErrorReporter

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

Overview

Error reporting and analytics system. Aggregates errors, tracks statistics, and provides actionable insights.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeErrorReporter

Returns a new instance of ErrorReporter.



12
13
14
15
16
17
18
19
20
# File 'lib/fractor/error_reporter.rb', line 12

def initialize
  @start_time = Time.now
  @total_errors = 0
  @total_successes = 0
  @by_category = {}
  @by_job = {}
  @error_handlers = []
  @mutex = Mutex.new
end

Instance Attribute Details

#start_timeObject (readonly)

Returns the value of attribute start_time.



10
11
12
# File 'lib/fractor/error_reporter.rb', line 10

def start_time
  @start_time
end

#total_errorsObject (readonly)

Returns the value of attribute total_errors.



10
11
12
# File 'lib/fractor/error_reporter.rb', line 10

def total_errors
  @total_errors
end

#total_successesObject (readonly)

Returns the value of attribute total_successes.



10
11
12
# File 'lib/fractor/error_reporter.rb', line 10

def total_successes
  @total_successes
end

Instance Method Details

#category_stats(category) ⇒ Hash?

Get statistics for a category

Parameters:

  • category (String, Symbol)

    The error category

Returns:

  • (Hash, nil)

    Statistics for the category



50
51
52
53
54
# File 'lib/fractor/error_reporter.rb', line 50

def category_stats(category)
  @mutex.synchronize do
    @by_category[category]&.to_h
  end
end

#critical_errorsArray<Hash>

Get critical errors

Returns:

  • (Array<Hash>)

    Critical errors with recent occurrences



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/fractor/error_reporter.rb', line 122

def critical_errors
  errors = []
  @mutex.synchronize do
    @by_category.each do |category, stats|
      critical_count = stats.by_severity[WorkResult::SEVERITY_CRITICAL] || 0
      if critical_count.positive?
        errors << {
          category: category,
          count: critical_count,
          recent: stats.recent_errors.select do |e|
            e[:error_severity] == WorkResult::SEVERITY_CRITICAL
          end.last(5),
        }
      end
    end
  end
  errors
end

#errors_by_severityHash

Get errors by severity

Returns:

  • (Hash)

    Error counts grouped by severity



79
80
81
82
83
84
85
86
87
88
89
# File 'lib/fractor/error_reporter.rb', line 79

def errors_by_severity
  result = Hash.new(0)
  @mutex.synchronize do
    @by_category.each_value do |stats|
      stats.by_severity.each do |severity, count|
        result[severity] += count
      end
    end
  end
  result
end

#formatted_reportString

Generate formatted text report

Returns:

  • (String)

    Formatted text report



180
181
182
# File 'lib/fractor/error_reporter.rb', line 180

def formatted_report
  ErrorReportGenerator.text_report(report)
end

#job_stats(job_name) ⇒ Hash?

Get statistics for a job

Parameters:

  • job_name (String)

    The job name

Returns:

  • (Hash, nil)

    Statistics for the job



60
61
62
63
64
# File 'lib/fractor/error_reporter.rb', line 60

def job_stats(job_name)
  @mutex.synchronize do
    @by_job[job_name]&.to_h
  end
end

#on_error {|work_result, job_name| ... } ⇒ void

This method returns an undefined value.

Register an error handler callback

Yields:

  • (work_result, job_name)

    Block to call when error occurs



42
43
44
# File 'lib/fractor/error_reporter.rb', line 42

def on_error(&block)
  @error_handlers << block
end

#overall_error_rateFloat

Get overall error rate

Returns:

  • (Float)

    Error rate percentage



69
70
71
72
73
74
# File 'lib/fractor/error_reporter.rb', line 69

def overall_error_rate
  total = @total_errors + @total_successes
  return 0.0 if total.zero?

  (@total_errors.to_f / total * 100).round(2)
end

#record(work_result, job_name: nil) ⇒ void

This method returns an undefined value.

Record a work result

Parameters:

  • work_result (WorkResult)

    The work result to record

  • job_name (String, nil) (defaults to: nil)

    Optional job name



27
28
29
30
31
32
33
34
35
36
# File 'lib/fractor/error_reporter.rb', line 27

def record(work_result, job_name: nil)
  @mutex.synchronize do
    if work_result.success?
      @total_successes += 1
    else
      @total_errors += 1
      record_error(work_result, job_name)
    end
  end
end

#reportHash

Generate comprehensive report

Returns:

  • (Hash)

    Report data with all statistics



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/fractor/error_reporter.rb', line 160

def report
  {
    summary: {
      uptime: (Time.now - @start_time).round(2),
      total_errors: @total_errors,
      total_successes: @total_successes,
      error_rate: overall_error_rate,
      errors_by_severity: errors_by_severity,
    },
    top_categories: top_categories,
    top_jobs: top_jobs,
    critical_errors: critical_errors,
    trending_errors: trending_errors,
    category_breakdown: category_breakdown,
  }
end

#resetvoid

This method returns an undefined value.

Reset all statistics



202
203
204
205
206
207
208
209
210
# File 'lib/fractor/error_reporter.rb', line 202

def reset
  @mutex.synchronize do
    @start_time = Time.now
    @total_errors = 0
    @total_successes = 0
    @by_category.clear
    @by_job.clear
  end
end

#to_json(*args) ⇒ String

Export to JSON format

Parameters:

  • args (Array)

    Additional arguments for JSON generation

Returns:

  • (String)

    JSON representation



195
196
197
# File 'lib/fractor/error_reporter.rb', line 195

def to_json(*args)
  ErrorReportGenerator.to_json(report, *args)
end

#to_prometheusString

Export to Prometheus format

Returns:

  • (String)

    Prometheus metrics



187
188
189
# File 'lib/fractor/error_reporter.rb', line 187

def to_prometheus
  ErrorReportGenerator.to_prometheus(self)
end

#top_categories(limit: 5) ⇒ Hash

Get top error categories

Parameters:

  • limit (Integer) (defaults to: 5)

    Maximum number of categories to return

Returns:

  • (Hash)

    Top error categories with counts



95
96
97
98
99
100
101
102
103
# File 'lib/fractor/error_reporter.rb', line 95

def top_categories(limit: 5)
  @mutex.synchronize do
    @by_category
      .map { |category, stats| [category, stats.total_count] }
      .sort_by { |_category, count| -count }
      .first(limit)
      .to_h
  end
end

#top_jobs(limit: 5) ⇒ Hash

Get top error jobs

Parameters:

  • limit (Integer) (defaults to: 5)

    Maximum number of jobs to return

Returns:

  • (Hash)

    Top error jobs with counts



109
110
111
112
113
114
115
116
117
# File 'lib/fractor/error_reporter.rb', line 109

def top_jobs(limit: 5)
  @mutex.synchronize do
    @by_job
      .map { |job, stats| [job, stats.total_count] }
      .sort_by { |_job, count| -count }
      .first(limit)
      .to_h
  end
end

Get trending errors (increasing error rates)

Returns:

  • (Array<Hash>)

    Trending error categories



144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/fractor/error_reporter.rb', line 144

def trending_errors
  trends = []
  @mutex.synchronize do
    @by_category.each do |category, stats|
      if stats.increasing?
        trends << { category: category,
                    stats: stats.to_h }
      end
    end
  end
  trends
end