Module: StructuredDataToSql::DiagnosticsReport

Defined in:
lib/structured_data_to_sql/diagnostics_report.rb

Overview

Writes the Markdown diagnostics report that sits beside a dump: what the run processed, every diagnostic with its complete lists and suggested action, the exporter runs that produced the source (when a manifest was found), and the per-file ledger. Written atomically and always — a clean run replaces a stale report with a "no warnings" one. Never includes member values, credentials, or per-row error payloads.

Class Method Summary collapse

Class Method Details

.default_path(output) ⇒ Object



20
21
22
23
24
25
# File 'lib/structured_data_to_sql/diagnostics_report.rb', line 20

def default_path(output)
  return nil unless output.is_a?(String) || output.is_a?(Pathname)
  return nil if output.to_s == "-"

  "#{output}.diagnostics.md"
end

.render(context) ⇒ Object



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/structured_data_to_sql/diagnostics_report.rb', line 46

def render(context)
  stats = context[:stats] || {}
  diagnostics = Array(context[:diagnostics])
  lines = []
  lines << "# Conversion diagnostics"
  lines << ""
  lines << "| | |"
  lines << "|---|---|"
  lines << "| Tool | structured_data_to_sql #{VERSION} (#{context[:format]}) |"
  lines << "| Source | `#{context[:source]}` |"
  lines << "| Output | `#{context[:output]}` |"
  lines << "| Profile | #{context[:profile] || "none"} |"
  lines << "| Started | #{context[:started_at]} |" if context[:started_at]
  if context[:finished_at]
    lines << "| Finished | #{context[:finished_at]} |"
  end
  if context[:elapsed]
    lines << "| Elapsed | #{Format.format_duration(context[:elapsed])} |"
  end
  lines << ""
  lines.concat(
    render_summary(stats, diagnostics.length, context[:input_bytes])
  )
  lines.concat(render_diagnostics(diagnostics))
  lines.concat(render_manifest(context[:manifest], context[:source_paths]))
  lines.concat(render_ledger(context[:ledger]))
  "#{lines.join("\n")}\n"
end

.render_diagnostics(diagnostics) ⇒ Object



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
# File 'lib/structured_data_to_sql/diagnostics_report.rb', line 93

def render_diagnostics(diagnostics)
  lines = ["## Diagnostics", ""]
  if diagnostics.empty?
    lines << "No warnings were raised by this conversion."
    lines << ""
    return lines
  end

  diagnostics.each_with_index do |diagnostic, index|
    heading = "### #{index + 1}. #{diagnostic.title}"
    heading +=
      " (#{Format.format_count(diagnostic.count)})" if diagnostic.count
    lines << heading
    lines << ""
    lines << "`#{diagnostic.code}`"
    lines << ""
    if diagnostic.summary
      lines << diagnostic.summary
      lines << ""
    end
    diagnostic.details.each { |detail| lines << "- #{detail}" }
    lines << "" if diagnostic.details.any?
    if diagnostic.items.any?
      lines << "**#{diagnostic.items_label || "Items"}** (#{Format.format_count(diagnostic.items.length)}):"
      lines << ""
      diagnostic.items.each { |item| lines << "- `#{item}`" }
      lines << ""
    end
    if diagnostic.action
      lines << "**Action:** #{diagnostic.action}"
      lines << ""
    end
    if diagnostic.sources.any?
      lines << "**Sources:** #{diagnostic.sources.map { |source| "`#{source}`" }.join(", ")}"
      lines << ""
    end
    lines << "<details><summary>Warning line</summary>"
    lines << ""
    lines << "```"
    lines << diagnostic.message
    lines << "```"
    lines << ""
    lines << "</details>"
    lines << ""
  end
  lines
end

.render_ledger(ledger) ⇒ Object



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/structured_data_to_sql/diagnostics_report.rb', line 171

def render_ledger(ledger)
  ledger = Array(ledger)
  return [] if ledger.empty?

  lines = ["## Files", ""]
  lines << "| File | Size | Rows | Child rows | Time |"
  lines << "|---|---:|---:|---:|---:|"
  ledger.each do |entry|
    lines << "| #{entry[:name]} | #{Format.format_size(entry[:size])} | " \
      "#{Format.format_count(entry[:rows])} | #{Format.format_count(entry[:child_rows])} | " \
      "#{Format.format_duration(entry[:elapsed])} |"
  end
  lines << ""
  lines
end

.render_manifest(manifest, source_paths) ⇒ Object



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/structured_data_to_sql/diagnostics_report.rb', line 141

def render_manifest(manifest, source_paths)
  return [] if manifest.nil?

  lines = ["## Exporter runs", ""]
  lines << "Manifest: `#{manifest.path}`"
  lines << ""
  if manifest.problem
    lines << "Not usable for run scoping: #{manifest.problem}."
    lines << ""
    return lines
  end

  lines << "Quality signals are scoped to the latest run whose manifest entry is `completed`, " \
    "by attributing the last _N_ rows of the append-only errors/gaps files to it (_N_ = that run's declared count). " \
    "Rows beyond the sum of declared counts belong to runs that never finalized their entry."
  lines << ""
  lines << "| Run | Status | Started | Finished | Mode | Credential | Emails | Errors | Gaps |"
  lines << "|---:|---|---|---|---|---|---|---:|---:|"
  manifest.runs.each do |run|
    lines << "| #{run.index} | #{run.status} | #{run.started_at} | #{run.finished_at} | #{run.mode} | " \
      "#{run.credential} | #{run.user_emails} | #{Format.format_count(run.errors)} | #{Format.format_count(run.gaps)} |"
  end
  lines << ""
  Array(source_paths).each do |table, path|
    lines << "- Raw `#{table}` rows: `#{path}`" if path
  end
  lines << "" if source_paths&.any?
  lines
end

.render_summary(stats, warning_count, input_bytes = nil) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/structured_data_to_sql/diagnostics_report.rb', line 75

def render_summary(stats, warning_count, input_bytes = nil)
  lines = ["## Run summary", ""]
  lines << "- Files processed: #{Format.format_count(stats[:files_processed])}" \
    "#{stats[:files_skipped].to_i.positive? ? " (#{Format.format_count(stats[:files_skipped])} skipped)" : ""}"
  lines << "- Rows: #{Format.format_count(stats[:rows_processed])}" \
    "#{stats[:child_rows_processed].to_i.positive? ? " (+#{Format.format_count(stats[:child_rows_processed])} child rows)" : ""}"
  lines << "- Tables: #{Format.format_count(stats[:tables_processed])}" \
    "#{stats[:tables_skipped].to_i.positive? ? " (#{Format.format_count(stats[:tables_skipped])} skipped)" : ""}"
  if input_bytes
    lines << "- Input: #{Format.format_size(input_bytes)} · Read: #{Format.format_size(stats[:bytes_read])} (all passes) · Written: #{Format.format_size(stats[:bytes_written])}"
  elsif stats[:bytes_read]
    lines << "- Read: #{Format.format_size(stats[:bytes_read])} · Written: #{Format.format_size(stats[:bytes_written])}"
  end
  lines << "- Warnings: #{warning_count}"
  lines << ""
  lines
end

.write(path, context) ⇒ Object

context keys: format, source, output, profile, started_at, finished_at, elapsed, stats (Hash), diagnostics (Array), ledger (Array<Hash name,size,rows,child_rows,elapsed>), manifest (Json::ExporterManifest or nil), source_paths (Hash table => path).



31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/structured_data_to_sql/diagnostics_report.rb', line 31

def write(path, context)
  pathname = Pathname(path)
  temporary = IOSupport.unique_adjacent_path(pathname)
  begin
    File.write(temporary, render(context))
    File.rename(temporary, pathname)
  ensure
    FileUtils.rm_f(temporary) if temporary.exist?
  end
  pathname.to_s
rescue SystemCallError, IOError => e
  raise OutputError,
        "Could not write diagnostics report #{path}: #{e.message}"
end