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.
Constant Summary collapse
- FORMAT_NAMES =
Display name lookup for document formats.
{ text: 'Plain Text', markdown: 'Markdown', asciidoc: 'AsciiDoc', code: 'Code' }.freeze
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.
38 39 40 41 42 |
# File 'lib/kotoshu/cli/batch_reporter.rb', line 38 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.
23 24 25 |
# File 'lib/kotoshu/cli/batch_reporter.rb', line 23 def document @document end |
#formatter ⇒ Object (readonly)
Returns the value of attribute formatter.
23 24 25 |
# File 'lib/kotoshu/cli/batch_reporter.rb', line 23 def formatter @formatter end |
#navigation ⇒ Object (readonly)
Returns the value of attribute navigation.
23 24 25 |
# File 'lib/kotoshu/cli/batch_reporter.rb', line 23 def @navigation end |
Instance Method Details
#exit_code(max_errors: 0) ⇒ Integer
Get exit code based on error severity.
Useful for CI/CD pipelines.
239 240 241 242 243 |
# File 'lib/kotoshu/cli/batch_reporter.rb', line 239 def exit_code(max_errors: 0) return 0 if @navigation.errors.size <= max_errors 1 end |
#print(format: :text) ⇒ Object
Print report to stdout.
262 263 264 265 266 267 268 269 270 271 272 273 |
# File 'lib/kotoshu/cli/batch_reporter.rb', line 262 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.
248 249 250 251 252 253 254 255 256 257 |
# File 'lib/kotoshu/cli/batch_reporter.rb', line 248 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.
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/kotoshu/cli/batch_reporter.rb', line 83 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.
49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/kotoshu/cli/batch_reporter.rb', line 49 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.
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 226 227 228 229 230 231 |
# File 'lib/kotoshu/cli/batch_reporter.rb', line 178 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.
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 164 165 166 167 168 169 |
# File 'lib/kotoshu/cli/batch_reporter.rb', line 113 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: #{FORMAT_NAMES[@document.format] || @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.
65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/kotoshu/cli/batch_reporter.rb', line 65 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 |