|
1 | 1 | import sys
|
2 | 2 | import time
|
| 3 | +import json |
3 | 4 | import logging
|
4 | 5 | from itertools import islice
|
5 | 6 |
|
|
50 | 51 | @click.option("--max-age", default=None, help="Considers only rows younger than specified. See --min-age.")
|
51 | 52 | @click.option("-s", "--stats", is_flag=True, help="Print stats instead of a detailed diff")
|
52 | 53 | @click.option("-d", "--debug", is_flag=True, help="Print debug info")
|
| 54 | +@click.option("--json", 'json_output', is_flag=True, help="Print JSONL output for machine readability") |
53 | 55 | @click.option("-v", "--verbose", is_flag=True, help="Print extra info")
|
54 | 56 | @click.option("-i", "--interactive", is_flag=True, help="Confirm queries, implies --debug")
|
55 | 57 | @click.option("--keep-column-case", is_flag=True, help="Don't use the schema to fix the case of given column names.")
|
@@ -80,6 +82,7 @@ def main(
|
80 | 82 | interactive,
|
81 | 83 | threads,
|
82 | 84 | keep_column_case,
|
| 85 | + json_output, |
83 | 86 | ):
|
84 | 87 | if limit and stats:
|
85 | 88 | print("Error: cannot specify a limit when using the -s/--stats switch")
|
@@ -144,17 +147,35 @@ def main(
|
144 | 147 | if stats:
|
145 | 148 | diff = list(diff_iter)
|
146 | 149 | unique_diff_count = len({i[0] for _, i in diff})
|
147 |
| - table1_count = differ.stats.get("table1_count") |
148 |
| - percent = 100 * unique_diff_count / (table1_count or 1) |
149 |
| - print(f"Diff-Total: {len(diff)} changed rows out of {table1_count}") |
150 |
| - print(f"Diff-Percent: {percent:.4f}%") |
| 150 | + max_table_count = max(differ.stats["table1_count"], differ.stats["table2_count"]) |
| 151 | + percent = 100 * unique_diff_count / (max_table_count or 1) |
151 | 152 | plus = len([1 for op, _ in diff if op == "+"])
|
152 | 153 | minus = len([1 for op, _ in diff if op == "-"])
|
153 |
| - print(f"Diff-Split: +{plus} -{minus}") |
| 154 | + |
| 155 | + if json_output: |
| 156 | + json_output = { |
| 157 | + "different_rows": len(diff), |
| 158 | + "different_percent": percent, |
| 159 | + "different_+": plus, |
| 160 | + "different_-": minus, |
| 161 | + "total": max_table_count, |
| 162 | + } |
| 163 | + print(json.dumps(json_output)) |
| 164 | + else: |
| 165 | + print(f"Diff-Total: {len(diff)} changed rows out of {max_table_count}") |
| 166 | + print(f"Diff-Percent: {percent:.14f}%") |
| 167 | + print(f"Diff-Split: +{plus} -{minus}") |
154 | 168 | else:
|
155 |
| - for op, key in diff_iter: |
| 169 | + for op, columns in diff_iter: |
156 | 170 | color = COLOR_SCHEME[op]
|
157 |
| - rich.print(f"[{color}]{op} {key!r}[/{color}]") |
| 171 | + |
| 172 | + if json_output: |
| 173 | + jsonl = json.dumps([op, list(columns)]) |
| 174 | + rich.print(f"[{color}]{jsonl}[/{color}]") |
| 175 | + else: |
| 176 | + text = f"{op} {', '.join(columns)}" |
| 177 | + rich.print(f"[{color}]{text}[/{color}]") |
| 178 | + |
158 | 179 | sys.stdout.flush()
|
159 | 180 |
|
160 | 181 | end = time.time()
|
|
0 commit comments