Class: Fractor::ErrorReportGenerator

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

Overview

Generates error reports in multiple formats (text, JSON, Prometheus). Extracted from ErrorReporter for better separation of concerns.

Class Method Summary collapse

Class Method Details

.text_report(report_data) ⇒ String

Generate a human-readable text report

Parameters:

  • report_data (Hash)

    The report data from ErrorReporter

Returns:

  • (String)

    Formatted text report



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/fractor/error_report_generator.rb', line 13

def self.text_report(report_data)
  lines = []
  lines << "=" * 80
  lines << "ERROR REPORT"
  lines << "=" * 80
  lines << ""

  # Summary
  lines << "SUMMARY"
  lines << "-" * 80
  summary = report_data[:summary]
  lines << "Uptime:          #{summary[:uptime]}s"
  lines << "Total Errors:    #{summary[:total_errors]}"
  lines << "Total Successes: #{summary[:total_successes]}"
  lines << "Error Rate:      #{summary[:error_rate]}%"
  lines << ""

  # Errors by Severity
  lines << "Errors by Severity:"
  summary[:errors_by_severity].each do |severity, count|
    lines << "  #{severity.to_s.ljust(10)}: #{count}"
  end
  lines << ""

  # Top Categories
  lines << "TOP ERROR CATEGORIES"
  lines << "-" * 80
  report_data[:top_categories].each do |category, count|
    lines << "#{category.to_s.ljust(20)}: #{count} errors"
  end
  lines << ""

  # Top Jobs
  unless report_data[:top_jobs].empty?
    lines << "TOP ERROR JOBS"
    lines << "-" * 80
    report_data[:top_jobs].each do |job, count|
      lines << "#{job.to_s.ljust(20)}: #{count} errors"
    end
    lines << ""
  end

  # Critical Errors
  unless report_data[:critical_errors].empty?
    lines << "CRITICAL ERRORS"
    lines << "-" * 80
    report_data[:critical_errors].each do |error_info|
      lines << "Category: #{error_info[:category]}"
      lines << "Count:    #{error_info[:count]}"
      lines << "Recent errors:"
      error_info[:recent].each do |err|
        lines << "  - [#{err[:timestamp]}] #{err[:error_class]}: #{err[:error_message]}"
      end
      lines << ""
    end
  end

  # Trending Errors
  unless report_data[:trending_errors].empty?
    lines << "TRENDING ERRORS (Increasing)"
    lines << "-" * 80
    report_data[:trending_errors].each do |trend|
      stats = trend[:stats]
      lines << "Category:    #{stats[:category]}"
      lines << "Total Count: #{stats[:total_count]}"
      lines << "Error Rate:  #{stats[:error_rate]}/s"
      lines << "Trend:       #{stats[:trending]}"
      lines << ""
    end
  end

  lines << "=" * 80
  lines.join("\n")
end

.to_json(report_data, *args) ⇒ String

Export errors to JSON format

Parameters:

  • report_data (Hash)

    The report data from ErrorReporter

  • args (Array)

    Additional arguments for JSON generation

Returns:

  • (String)

    JSON representation



148
149
150
# File 'lib/fractor/error_report_generator.rb', line 148

def self.to_json(report_data, *args)
  report_data.to_json(*args)
end

.to_prometheus(reporter) ⇒ String

Export errors to Prometheus format

Parameters:

Returns:

  • (String)

    Prometheus metrics



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/fractor/error_report_generator.rb', line 92

def self.to_prometheus(reporter)
  lines = []

  # Total errors
  lines << "# HELP fractor_errors_total Total number of errors"
  lines << "# TYPE fractor_errors_total counter"
  lines << "fractor_errors_total #{reporter.total_errors}"
  lines << ""

  # Total successes
  lines << "# HELP fractor_successes_total Total number of successes"
  lines << "# TYPE fractor_successes_total counter"
  lines << "fractor_successes_total #{reporter.total_successes}"
  lines << ""

  # Error rate
  lines << "# HELP fractor_error_rate Error rate percentage"
  lines << "# TYPE fractor_error_rate gauge"
  lines << "fractor_error_rate #{reporter.overall_error_rate}"
  lines << ""

  # Errors by severity
  lines << "# HELP fractor_errors_by_severity Errors by severity level"
  lines << "# TYPE fractor_errors_by_severity gauge"
  reporter.errors_by_severity.each do |severity, count|
    lines << "fractor_errors_by_severity{severity=\"#{severity}\"} #{count}"
  end
  lines << ""

  # Errors by category
  lines << "# HELP fractor_errors_by_category Errors by category"
  lines << "# TYPE fractor_errors_by_category gauge"
  reporter.instance_variable_get(:@by_category)&.each do |category, stats|
    lines << "fractor_errors_by_category{category=\"#{category}\"} #{stats.total_count}"
  end
  lines << ""

  # Errors by job
  by_job = reporter.instance_variable_get(:@by_job)
  unless by_job&.empty?
    lines << "# HELP fractor_errors_by_job Errors by job name"
    lines << "# TYPE fractor_errors_by_job gauge"
    by_job.each do |job, stats|
      lines << "fractor_errors_by_job{job=\"#{job}\"} #{stats.total_count}"
    end
    lines << ""
  end

  lines.join("\n")
end