Repository navigation
Expand file tree
/
Copy pathcoverage_report.py
More file actions
740 lines (664 loc) · 24.7 KB
/
Copy pathcoverage_report.py
File metadata and controls
740 lines (664 loc) · 24.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
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
87
88
89
90
91
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
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
170
171
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
#!/usr/bin/env python3
"""
coverage_report.py — coverage target + snapshot tracking and gap reporting.
This module adds two persistent models in SQLite:
- coverage_targets: registry of source-level coverage goals
- source_snapshots: expected decision IDs per source/year snapshot
Use `gap-report` to compute missing IDs and counts against ingested decisions.
"""
from __future__ import annotations
import argparse
import json
import logging
import sqlite3
from datetime import date
from pathlib import Path
from typing import Any
from db_schema import COVERAGE_SCHEMA_SQL
logger = logging.getLogger("coverage_report")
def ensure_coverage_tables(conn: sqlite3.Connection) -> None:
"""Create coverage tables if missing."""
conn.executescript(COVERAGE_SCHEMA_SQL)
def _infer_source_kind(module_name: str) -> str:
if ".cantonal." in module_name:
return "cantonal_court"
if module_name.startswith("scrapers."):
return "federal_or_authority"
return "unknown"
def seed_targets_from_scrapers(conn: sqlite3.Connection, *, only_missing: bool = False) -> tuple[int, int]:
"""Upsert coverage targets from the scraper registry."""
# Local import avoids a module-import cycle when run_scraper imports
# coverage helpers for automatic snapshot recording.
from run_scraper import SCRAPERS
inserted = 0
updated = 0
for source_key, (module_name, class_name) in sorted(SCRAPERS.items()):
row = conn.execute(
"SELECT source_key FROM coverage_targets WHERE source_key = ?",
(source_key,),
).fetchone()
source_name = source_key.replace("_", " ")
source_kind = _infer_source_kind(module_name)
notes = f"{module_name}:{class_name}"
if row:
if only_missing:
continue
conn.execute(
"""
UPDATE coverage_targets
SET source_name = ?, source_kind = ?, active = 1, notes = ?, updated_at = datetime('now')
WHERE source_key = ?
""",
(source_name, source_kind, notes, source_key),
)
updated += 1
continue
conn.execute(
"""
INSERT INTO coverage_targets (
source_key, source_name, source_kind, active, notes, created_at, updated_at
) VALUES (?, ?, ?, 1, ?, datetime('now'), datetime('now'))
""",
(source_key, source_name, source_kind, notes),
)
inserted += 1
conn.commit()
return inserted, updated
def _dedupe_preserve_order(items: list[str]) -> list[str]:
seen: set[str] = set()
out: list[str] = []
for item in items:
value = item.strip()
if not value or value in seen:
continue
seen.add(value)
out.append(value)
return out
def _load_ids_from_file(path: Path) -> list[str]:
"""Load decision IDs from JSON/JSONL/text files."""
text = path.read_text(encoding="utf-8")
stripped = text.strip()
if not stripped:
return []
# JSON list or object wrapper
if stripped[0] in "[{":
try:
parsed = json.loads(stripped)
if isinstance(parsed, list):
if parsed and isinstance(parsed[0], dict):
return _dedupe_preserve_order(
[str(row.get("decision_id", "")).strip() for row in parsed]
)
return _dedupe_preserve_order([str(x).strip() for x in parsed])
if isinstance(parsed, dict):
if isinstance(parsed.get("decision_ids"), list):
return _dedupe_preserve_order(
[str(x).strip() for x in parsed["decision_ids"]]
)
except json.JSONDecodeError:
pass
# JSONL or plain text one-ID-per-line
ids: list[str] = []
for line in text.splitlines():
line = line.strip()
if not line:
continue
if line.startswith("{"):
try:
row = json.loads(line)
decision_id = str(row.get("decision_id", "")).strip()
if decision_id:
ids.append(decision_id)
continue
except json.JSONDecodeError:
pass
ids.append(line)
return _dedupe_preserve_order(ids)
def _ensure_target_row(conn: sqlite3.Connection, source_key: str) -> None:
conn.execute(
"""
INSERT OR IGNORE INTO coverage_targets (
source_key, source_name, source_kind, active, created_at, updated_at
) VALUES (?, ?, 'unknown', 1, datetime('now'), datetime('now'))
""",
(source_key, source_key.replace("_", " "),),
)
def record_snapshot(
conn: sqlite3.Connection,
*,
source_key: str,
snapshot_year: int,
snapshot_date: str,
decision_ids: list[str],
notes: str | None = None,
) -> tuple[int, int]:
"""Insert or update one source/year snapshot."""
raw_count = len(decision_ids)
ids = _dedupe_preserve_order(decision_ids)
expected_count = len(ids)
_ensure_target_row(conn, source_key)
conn.execute(
"""
INSERT INTO source_snapshots (
source_key, snapshot_year, snapshot_date, expected_count, expected_ids_json, notes
) VALUES (?, ?, ?, ?, ?, ?)
ON CONFLICT(source_key, snapshot_year, snapshot_date)
DO UPDATE SET
expected_count = excluded.expected_count,
expected_ids_json = excluded.expected_ids_json,
notes = excluded.notes
""",
(
source_key,
snapshot_year,
snapshot_date,
expected_count,
json.dumps(ids, ensure_ascii=False),
notes,
),
)
conn.execute(
"UPDATE coverage_targets SET updated_at = datetime('now') WHERE source_key = ?",
(source_key,),
)
conn.commit()
return raw_count, expected_count
def _latest_snapshots(
conn: sqlite3.Connection,
*,
sources: list[str] | None = None,
year_from: int | None = None,
year_to: int | None = None,
) -> list[sqlite3.Row]:
outer_where: list[str] = []
inner_where: list[str] = []
inner_params: list[Any] = []
outer_params: list[Any] = []
if sources:
placeholders = ",".join("?" for _ in sources)
inner_where.append(f"ss.source_key IN ({placeholders})")
inner_params.extend(sources)
outer_where.append(f"s.source_key IN ({placeholders})")
outer_params.extend(sources)
if year_from is not None:
inner_where.append("ss.snapshot_year >= ?")
inner_params.append(year_from)
outer_where.append("s.snapshot_year >= ?")
outer_params.append(year_from)
if year_to is not None:
inner_where.append("ss.snapshot_year <= ?")
inner_params.append(year_to)
outer_where.append("s.snapshot_year <= ?")
outer_params.append(year_to)
inner_filter = f"AND {' AND '.join(inner_where)}" if inner_where else ""
outer_filter = f"AND {' AND '.join(outer_where)}" if outer_where else ""
params = inner_params + outer_params
sql = f"""
SELECT s.id, s.source_key, s.snapshot_year, s.snapshot_date,
s.expected_count, s.expected_ids_json, t.source_name
FROM source_snapshots s
LEFT JOIN coverage_targets t ON t.source_key = s.source_key
WHERE s.id = (
SELECT ss.id
FROM source_snapshots ss
WHERE ss.source_key = s.source_key
AND ss.snapshot_year = s.snapshot_year
{inner_filter}
ORDER BY ss.snapshot_date DESC, ss.id DESC
LIMIT 1
)
{outer_filter}
ORDER BY s.source_key, s.snapshot_year
"""
return conn.execute(sql, params).fetchall()
def _fetch_existing_ids(conn: sqlite3.Connection, decision_ids: list[str]) -> set[str]:
if not decision_ids:
return set()
found: set[str] = set()
chunk_size = 800
for i in range(0, len(decision_ids), chunk_size):
chunk = decision_ids[i:i + chunk_size]
rows = conn.execute(
f"SELECT decision_id FROM decisions WHERE decision_id IN ({','.join('?' for _ in chunk)})",
chunk,
).fetchall()
for row in rows:
found.add(row[0])
return found
def _ingested_count_by_year(conn: sqlite3.Connection, source_key: str, snapshot_year: int) -> int:
# Try source_spider first (scraper key), fall back to court for older/minimal schemas.
try:
return int(
conn.execute(
"""
SELECT COUNT(*)
FROM decisions
WHERE (source_spider = ? OR court = ?)
AND SUBSTR(COALESCE(decision_date, ''), 1, 4) = ?
""",
(source_key, source_key, str(snapshot_year)),
).fetchone()[0]
)
except sqlite3.OperationalError:
return int(
conn.execute(
"""
SELECT COUNT(*)
FROM decisions
WHERE court = ?
AND SUBSTR(COALESCE(decision_date, ''), 1, 4) = ?
""",
(source_key, str(snapshot_year)),
).fetchone()[0]
)
def _has_decisions_table(conn: sqlite3.Connection) -> bool:
row = conn.execute(
"""
SELECT 1
FROM sqlite_master
WHERE type = 'table' AND name = 'decisions'
"""
).fetchone()
return row is not None
def mark_gap_failure(
conn: sqlite3.Connection,
*,
source_key: str,
decision_year: int,
decision_id: str,
error_message: str,
retry_delay_days: int = 1,
) -> None:
"""Upsert one gap_queue row as failed/missing."""
conn.execute(
"""
INSERT INTO gap_queue (
source_key, decision_year, decision_id, status,
retry_count, next_retry_at, first_seen_at, last_seen_at,
last_error, resolved_at, resolution
) VALUES (
?, ?, ?, 'retrying', 1, datetime('now', ?), datetime('now'), datetime('now'),
?, NULL, NULL
)
ON CONFLICT(source_key, decision_year, decision_id)
DO UPDATE SET
status = CASE WHEN gap_queue.status = 'ignored' THEN 'ignored' ELSE 'retrying' END,
retry_count = CASE
WHEN gap_queue.status = 'ignored' THEN gap_queue.retry_count
ELSE gap_queue.retry_count + 1
END,
next_retry_at = CASE
WHEN gap_queue.status = 'ignored' THEN gap_queue.next_retry_at
ELSE datetime('now', ?)
END,
last_seen_at = datetime('now'),
last_error = ?,
resolved_at = CASE WHEN gap_queue.status = 'ignored' THEN gap_queue.resolved_at ELSE NULL END,
resolution = CASE WHEN gap_queue.status = 'ignored' THEN gap_queue.resolution ELSE NULL END
""",
(
source_key,
decision_year,
decision_id,
f"+{int(retry_delay_days)} day",
error_message[:500],
f"+{int(retry_delay_days)} day",
error_message[:500],
),
)
def mark_gap_resolved(
conn: sqlite3.Connection,
*,
source_key: str,
decision_year: int,
decision_id: str,
resolution: str = "ingested",
) -> None:
"""Mark one existing gap_queue row resolved."""
conn.execute(
"""
UPDATE gap_queue
SET status = 'resolved',
last_seen_at = datetime('now'),
last_error = NULL,
resolved_at = datetime('now'),
resolution = ?
WHERE source_key = ?
AND decision_year = ?
AND decision_id = ?
AND status IN ('open', 'retrying')
""",
(
resolution,
source_key,
decision_year,
decision_id,
),
)
def generate_gap_report(
conn: sqlite3.Connection,
*,
sources: list[str] | None = None,
year_from: int | None = None,
year_to: int | None = None,
missing_only: bool = False,
include_missing_ids: bool = False,
max_missing_ids: int = 25,
) -> list[dict[str, Any]]:
"""Compute missing coverage per source/year from latest snapshots."""
has_decisions = _has_decisions_table(conn)
rows = _latest_snapshots(
conn,
sources=sources,
year_from=year_from,
year_to=year_to,
)
report: list[dict[str, Any]] = []
for row in rows:
source_key = str(row["source_key"])
expected_count = int(row["expected_count"] or 0)
expected_ids_raw = str(row["expected_ids_json"] or "[]")
try:
expected_ids = _dedupe_preserve_order(
[str(x).strip() for x in json.loads(expected_ids_raw)]
)
except json.JSONDecodeError:
expected_ids = []
if expected_ids:
existing_ids = _fetch_existing_ids(conn, expected_ids) if has_decisions else set()
missing_ids = [did for did in expected_ids if did not in existing_ids]
ingested_count = len(expected_ids) - len(missing_ids)
if expected_count < len(expected_ids):
expected_count = len(expected_ids)
missing_count = max(expected_count - ingested_count, 0)
else:
ingested_count = (
_ingested_count_by_year(conn, source_key, int(row["snapshot_year"]))
if has_decisions else 0
)
missing_count = max(expected_count - ingested_count, 0)
missing_ids = []
if missing_only and missing_count == 0:
continue
item: dict[str, Any] = {
"source_key": source_key,
"source_name": row["source_name"] or source_key,
"snapshot_year": int(row["snapshot_year"]),
"snapshot_date": row["snapshot_date"],
"expected_count": expected_count,
"ingested_count": ingested_count,
"missing_count": missing_count,
"coverage_ratio": round((ingested_count / expected_count), 4) if expected_count else 1.0,
}
if include_missing_ids:
item["missing_ids"] = missing_ids[:max_missing_ids]
item["missing_ids_truncated"] = max(len(missing_ids) - max_missing_ids, 0)
report.append(item)
return report
def sync_gap_queue_from_snapshots(
conn: sqlite3.Connection,
*,
sources: list[str] | None = None,
year_from: int | None = None,
year_to: int | None = None,
) -> dict[str, int]:
"""Reconcile gap_queue against latest snapshots (ID-aware rows only)."""
has_decisions = _has_decisions_table(conn)
rows = _latest_snapshots(
conn,
sources=sources,
year_from=year_from,
year_to=year_to,
)
inserted_or_updated = 0
resolved = 0
for row in rows:
source_key = str(row["source_key"])
snapshot_year = int(row["snapshot_year"])
expected_ids_raw = str(row["expected_ids_json"] or "[]")
try:
expected_ids = _dedupe_preserve_order(
[str(x).strip() for x in json.loads(expected_ids_raw)]
)
except json.JSONDecodeError:
expected_ids = []
# Without explicit expected IDs we cannot queue per-decision gaps.
if not expected_ids:
continue
existing_ids = _fetch_existing_ids(conn, expected_ids) if has_decisions else set()
missing_ids = [did for did in expected_ids if did not in existing_ids]
for did in missing_ids:
conn.execute(
"""
INSERT INTO gap_queue (
source_key, decision_year, decision_id, status,
retry_count, next_retry_at, first_seen_at, last_seen_at,
last_error, resolved_at, resolution
) VALUES (
?, ?, ?, 'open', 0, datetime('now'), datetime('now'), datetime('now'),
'snapshot_missing', NULL, NULL
)
ON CONFLICT(source_key, decision_year, decision_id)
DO UPDATE SET
status = CASE WHEN gap_queue.status = 'ignored' THEN 'ignored' ELSE 'open' END,
last_seen_at = datetime('now'),
next_retry_at = CASE
WHEN gap_queue.status = 'ignored' THEN gap_queue.next_retry_at
ELSE datetime('now')
END,
last_error = CASE
WHEN gap_queue.status = 'ignored' THEN gap_queue.last_error
ELSE 'snapshot_missing'
END,
resolved_at = CASE WHEN gap_queue.status = 'ignored' THEN gap_queue.resolved_at ELSE NULL END,
resolution = CASE WHEN gap_queue.status = 'ignored' THEN gap_queue.resolution ELSE NULL END
""",
(source_key, snapshot_year, did),
)
inserted_or_updated += 1
# Resolve queue rows for this source/year that are no longer missing.
if missing_ids:
placeholders = ",".join("?" for _ in missing_ids)
rowcount = conn.execute(
f"""
UPDATE gap_queue
SET status = 'resolved',
last_seen_at = datetime('now'),
last_error = NULL,
resolved_at = datetime('now'),
resolution = 'snapshot_reconciled'
WHERE source_key = ?
AND decision_year = ?
AND status IN ('open', 'retrying')
AND decision_id NOT IN ({placeholders})
""",
[source_key, snapshot_year, *missing_ids],
).rowcount
else:
rowcount = conn.execute(
"""
UPDATE gap_queue
SET status = 'resolved',
last_seen_at = datetime('now'),
last_error = NULL,
resolved_at = datetime('now'),
resolution = 'snapshot_reconciled'
WHERE source_key = ?
AND decision_year = ?
AND status IN ('open', 'retrying')
""",
(source_key, snapshot_year),
).rowcount
resolved += int(rowcount or 0)
conn.commit()
return {
"upserted_missing": inserted_or_updated,
"resolved": resolved,
"rows": len(rows),
}
def _print_report(rows: list[dict[str, Any]], *, show_ids: bool = False) -> None:
if not rows:
print("No snapshot rows matched filters.")
return
header = (
"source_key".ljust(24)
+ "year".rjust(6)
+ "expected".rjust(10)
+ "ingested".rjust(10)
+ "missing".rjust(9)
+ "coverage".rjust(10)
+ " snapshot"
)
print(header)
print("-" * len(header))
for row in rows:
print(
str(row["source_key"]).ljust(24)
+ str(row["snapshot_year"]).rjust(6)
+ str(row["expected_count"]).rjust(10)
+ str(row["ingested_count"]).rjust(10)
+ str(row["missing_count"]).rjust(9)
+ f"{row['coverage_ratio'] * 100:9.1f}%"
+ f" {row['snapshot_date']}"
)
if show_ids and row.get("missing_ids"):
print(f" missing_ids: {', '.join(row['missing_ids'])}")
truncated = int(row.get("missing_ids_truncated", 0) or 0)
if truncated:
print(f" ... +{truncated} more")
def main() -> None:
parser = argparse.ArgumentParser(description="Coverage target/snapshot management and gap reporting")
parser.add_argument(
"--db",
type=str,
default="state/coverage.db",
help="SQLite database path (default: state/coverage.db — the "
"dedicated coverage DB, isolated from output/decisions.db "
"since 2026-04-20 to avoid read/write contention with MCP)",
)
parser.add_argument("-v", "--verbose", action="store_true")
sub = parser.add_subparsers(dest="command", required=True)
seed = sub.add_parser("seed-targets", help="Seed coverage_targets from run_scraper SCRAPERS registry")
seed.add_argument(
"--only-missing",
action="store_true",
help="Insert missing targets only (do not update existing rows)",
)
record = sub.add_parser("record-snapshot", help="Record expected IDs for one source/year snapshot")
record.add_argument("--source", required=True, help="Source key, e.g. bger or zh_gerichte")
record.add_argument("--year", required=True, type=int, help="Snapshot year")
record.add_argument(
"--snapshot-date",
default=date.today().isoformat(),
help="Snapshot date YYYY-MM-DD (default: today)",
)
record.add_argument(
"--ids-file",
required=True,
type=str,
help="Path to file containing expected decision IDs (JSON list, JSONL, or plain text)",
)
record.add_argument("--notes", type=str, default=None, help="Optional notes")
report = sub.add_parser("gap-report", help="Compute missing IDs/counts per source/year")
report.add_argument(
"--source",
action="append",
default=None,
help="Filter source key (repeatable)",
)
report.add_argument("--year-from", type=int, default=None, help="Filter minimum year")
report.add_argument("--year-to", type=int, default=None, help="Filter maximum year")
report.add_argument("--missing-only", action="store_true", help="Show only rows with missing_count > 0")
report.add_argument(
"--show-missing-ids",
action="store_true",
help="Include missing decision IDs in output",
)
report.add_argument(
"--max-missing-ids",
type=int,
default=25,
help="Maximum missing IDs to print/export per row",
)
report.add_argument(
"--json-output",
type=str,
default=None,
help="Optional JSON output path",
)
sync = sub.add_parser("sync-gap-queue", help="Reconcile gap_queue from latest snapshots")
sync.add_argument(
"--source",
action="append",
default=None,
help="Filter source key (repeatable)",
)
sync.add_argument("--year-from", type=int, default=None, help="Filter minimum year")
sync.add_argument("--year-to", type=int, default=None, help="Filter maximum year")
args = parser.parse_args()
logging.basicConfig(
level=logging.DEBUG if args.verbose else logging.INFO,
format="%(asctime)s %(name)s %(levelname)s %(message)s",
)
db_path = Path(args.db)
db_path.parent.mkdir(parents=True, exist_ok=True)
conn = sqlite3.connect(str(db_path))
conn.row_factory = sqlite3.Row
ensure_coverage_tables(conn)
if args.command == "seed-targets":
inserted, updated = seed_targets_from_scrapers(conn, only_missing=args.only_missing)
print(f"seed-targets: inserted={inserted}, updated={updated}")
conn.close()
return
if args.command == "record-snapshot":
ids = _load_ids_from_file(Path(args.ids_file))
raw_count, expected_count = record_snapshot(
conn,
source_key=args.source,
snapshot_year=args.year,
snapshot_date=args.snapshot_date,
decision_ids=ids,
notes=args.notes,
)
print(
f"record-snapshot: source={args.source} year={args.year} "
f"raw={raw_count} expected_count={expected_count}"
)
conn.close()
return
if args.command == "gap-report":
rows = generate_gap_report(
conn,
sources=args.source,
year_from=args.year_from,
year_to=args.year_to,
missing_only=args.missing_only,
include_missing_ids=args.show_missing_ids,
max_missing_ids=args.max_missing_ids,
)
_print_report(rows, show_ids=args.show_missing_ids)
if args.json_output:
out_path = Path(args.json_output)
out_path.parent.mkdir(parents=True, exist_ok=True)
out_path.write_text(json.dumps(rows, indent=2, ensure_ascii=False), encoding="utf-8")
print(f"wrote {len(rows)} rows to {out_path}")
conn.close()
return
if args.command == "sync-gap-queue":
stats = sync_gap_queue_from_snapshots(
conn,
sources=args.source,
year_from=args.year_from,
year_to=args.year_to,
)
print(
"sync-gap-queue: "
f"rows={stats['rows']} "
f"upserted_missing={stats['upserted_missing']} "
f"resolved={stats['resolved']}"
)
conn.close()
return
conn.close()
if __name__ == "__main__":
main()