48 lines
1.6 KiB
Python
48 lines
1.6 KiB
Python
from collections import Counter, defaultdict
|
|
from pathlib import Path
|
|
from typing import Iterable
|
|
import csv
|
|
|
|
from serato_doctor.models.reference import ReferenceResult
|
|
|
|
|
|
def write_missing_report(results: Iterable[ReferenceResult], out: Path) -> None:
|
|
rows = [result.as_row() for result in results]
|
|
missing = [r for r in rows if not r["exists_by_filename"]]
|
|
|
|
crate_counts = Counter(r["crate"] for r in missing)
|
|
filename_counts = Counter(r["filename"] for r in missing)
|
|
|
|
with out.open("w", encoding="utf-8") as f:
|
|
f.write("# Serato Doctor Missing Report\n\n")
|
|
f.write(f"Total missing references: {len(missing)}\n\n")
|
|
|
|
f.write("## Missing by crate\n\n")
|
|
for crate, count in crate_counts.most_common():
|
|
f.write(f"{count:5} {crate}\n")
|
|
|
|
f.write("\n## Most common missing filenames\n\n")
|
|
for filename, count in filename_counts.most_common(100):
|
|
f.write(f"{count:5} {filename}\n")
|
|
|
|
f.write("\n## Detail\n\n")
|
|
by_crate = defaultdict(list)
|
|
for r in missing:
|
|
by_crate[r["crate"]].append(r["filename"])
|
|
|
|
for crate, names in sorted(by_crate.items()):
|
|
f.write(f"\n### {crate}\n")
|
|
for name in sorted(set(names)):
|
|
f.write(f"- {name}\n")
|
|
|
|
|
|
def write_csv(results: Iterable[ReferenceResult], out: Path) -> None:
|
|
rows = [result.as_row() for result in results]
|
|
with out.open("w", newline="", encoding="utf-8") as f:
|
|
writer = csv.DictWriter(
|
|
f,
|
|
fieldnames=["crate", "serato_path", "filename", "exists_by_filename"],
|
|
)
|
|
writer.writeheader()
|
|
writer.writerows(rows)
|