Class: Kotoshu::Cli::BatchReporter
- Inherits:
-
Object
- Object
- Kotoshu::Cli::BatchReporter
- Defined in:
- lib/kotoshu/cli/batch_reporter.rb
Overview
Batch reporter for non-interactive error reporting.
Outputs error reports in various formats (JSON, YAML, CSV, text). Used for automated checking and CI/CD integration.
Instance Attribute Summary collapse
-
#document ⇒ Object
readonly
Returns the value of attribute document.
-
#formatter ⇒ Object
readonly
Returns the value of attribute formatter.
-
#navigation ⇒ Object
readonly
Returns the value of attribute navigation.
Instance Method Summary collapse
-
#exit_code(max_errors: 0) ⇒ Integer
Get exit code based on error severity.
-
#initialize(document, navigation, formatter: nil) ⇒ BatchReporter
constructor
Create a new batch reporter.
-
#print(format: :text) ⇒ Object
Print report to stdout.
-
#summary ⇒ Hash
Get report summary as hash.
-
#to_csv(filepath: nil) ⇒ String?
Generate CSV report.
-
#to_json(filepath: nil, pretty: true) ⇒ String?
Generate JSON report.
-
#to_sarif(filepath: nil) ⇒ String?
Generate SARIF report (Static Analysis Results Interchange Format).
-
#to_text ⇒ String
Generate text summary.
-
#to_yaml(filepath: nil) ⇒ String?
Generate YAML report.
Constructor Details
#initialize(document, navigation, formatter: nil) ⇒ BatchReporter
Create a new batch reporter.
32 33 34 35 36 |
# File 'lib/kotoshu/cli/batch_reporter.rb', line 32 def initialize(document, , formatter: nil) @document = document @navigation = @formatter = formatter || DisplayFormatter.new end |
Instance Attribute Details
#document ⇒ Object (readonly)
Returns the value of attribute document.
25 26 27 |
# File 'lib/kotoshu/cli/batch_reporter.rb', line 25 def document @document end |
#formatter ⇒ Object (readonly)
Returns the value of attribute formatter.
25 26 27 |
# File 'lib/kotoshu/cli/batch_reporter.rb', line 25 def formatter @formatter end |
#navigation ⇒ Object (readonly)
Returns the value of attribute navigation.
25 26 27 |
# File 'lib/kotoshu/cli/batch_reporter.rb', line 25 def @navigation end |
Instance Method Details
#exit_code(max_errors: 0) ⇒ Integer
Get exit code based on error severity.
Useful for CI/CD pipelines.
233 234 235 236 237 |
# File 'lib/kotoshu/cli/batch_reporter.rb', line 233 def exit_code(max_errors: 0) return 0 if @navigation.errors.size <= max_errors 1 end |
#print(format: :text) ⇒ Object
Print report to stdout.
256 257 258 259 260 261 262 263 264 265 266 267 |
# File 'lib/kotoshu/cli/batch_reporter.rb', line 256 def print(format: :text) case format when :text puts to_text when :json puts to_json when :yaml puts to_yaml else raise ArgumentError, "Unknown format: #{format}" end end |
#summary ⇒ Hash
Get report summary as hash.
242 243 244 245 246 247 248 249 250 251 |
# File 'lib/kotoshu/cli/batch_reporter.rb', line 242 def summary @navigation.statistics.merge( document: { name: @document.name, format: @document.format, language: @document.language_code }, has_errors: @navigation.errors.any? ) end |
#to_csv(filepath: nil) ⇒ String?
Generate CSV report.
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/kotoshu/cli/batch_reporter.rb', line 77 def to_csv(filepath: nil) csv_string = CSV.generate do |csv| # Header csv << ['ID', 'Line', 'Original', 'Suggestion', 'Confidence', 'Error Type'] # Data rows @navigation.errors.each do |error| suggestion = error.recommended_suggestion csv << [ error.id, error.location.line, error.original, suggestion&.word || '', "#{(error.confidence * 100).round(1)}%", error.error_type.to_s.capitalize ] end end if filepath File.write(filepath, csv_string) nil else csv_string end end |
#to_json(filepath: nil, pretty: true) ⇒ String?
Generate JSON report.
43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/kotoshu/cli/batch_reporter.rb', line 43 def to_json(filepath: nil, pretty: true) data = generate_report_data json = pretty ? JSON.pretty_generate(data) : JSON.generate(data) if filepath File.write(filepath, json) nil else json end end |
#to_sarif(filepath: nil) ⇒ String?
Generate SARIF report (Static Analysis Results Interchange Format).
SARIF is a standard format for static analysis tools. Useful for CI/CD integration and IDE integration.
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 |
# File 'lib/kotoshu/cli/batch_reporter.rb', line 172 def to_sarif(filepath: nil) sarif = { version: "2.1.0", "$schema": "https://json.schemastore.org/sarif-2.1.0.json", runs: [ { tool: { driver: { name: "Kotoshu", version: Kotoshu::VERSION, informationUri: "https://github.com/kotoshu/kotoshu", rules: [] } }, results: @navigation.errors.map do |error| { ruleId: error.error_type.to_s, level: error.high_confidence? ? "error" : "warning", message: { text: "Potential #{error.error_type} error: '#{error.original}'" }, locations: [ { physicalLocation: { artifactLocation: { uri: @document.name }, region: { startLine: error.location.line || 1, startColumn: error.location.column || 0 } } } ], suggestions: error.suggestions&.map do |sugg| { text: sugg.word } end } end } ] } json = JSON.pretty_generate(sarif) if filepath File.write(filepath, json) nil else json end end |
#to_text ⇒ String
Generate text summary.
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 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/kotoshu/cli/batch_reporter.rb', line 107 def to_text lines = [] lines << "" lines << @formatter.colorize("╔═══════════════════════════════════════════════════════════════╗", :bold) lines << @formatter.colorize("║ Batch Error Report ║", :bold) lines << @formatter.colorize("╚═══════════════════════════════════════════════════════════════╝", :bold) lines << "" lines << "Document: #{@document.name}" lines << "Format: #{Documents::Document::FORMATS[@document.format]}" lines << "Language: #{@document.language_code}" lines << "" lines << @formatter.colorize("Summary", :bold) lines << "─" * 70 stats = @navigation.statistics lines << "Total errors: #{stats[:total]}" lines << " • High confidence (>0.8): #{stats[:by_confidence][:high]}" lines << " • Medium confidence (0.5-0.8): #{stats[:by_confidence][:medium]}" lines << " • Low confidence (≤0.5): #{stats[:by_confidence][:low]}" lines << "" # Breakdown by type if stats[:by_type]&.any? lines << @formatter.colorize("By Type", :bold) stats[:by_type].each do |type, count| label = Models::SemanticError::ERROR_TYPES[type] || type.to_s.capitalize lines << " • #{label}: #{count}" end lines << "" end # Top errors if @navigation.errors.any? lines << @formatter.colorize("Top Errors", :bold) lines << "─" * 70 @navigation.errors.first(10).each_with_index do |error, idx| lines << "#{idx + 1}. [#{error.location}] #{error.original}" lines << " Type: #{error.error_type}" lines << " Confidence: #{(error.confidence * 100).round(1)}%" if error.suggestions&.any? top_suggestion = error.suggestions.first lines << " Suggestion: #{top_suggestion.word} (#{(top_suggestion.confidence * 100).round(0)}%)" end lines << "" end if @navigation.errors.size > 10 lines << "... and #{@navigation.errors.size - 10} more" lines << "" end end lines.join("\n") end |
#to_yaml(filepath: nil) ⇒ String?
Generate YAML report.
59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/kotoshu/cli/batch_reporter.rb', line 59 def to_yaml(filepath: nil) require 'yaml' data = generate_report_data yaml = data.to_yaml if filepath File.write(filepath, yaml) nil else yaml end end |