Add generate_report/src/generate_report/cli.py
This commit is contained in:
parent
09eb81c055
commit
64042606c5
1 changed files with 58 additions and 0 deletions
58
generate_report/src/generate_report/cli.py
Normal file
58
generate_report/src/generate_report/cli.py
Normal file
|
|
@ -0,0 +1,58 @@
|
||||||
|
import argparse
|
||||||
|
import json
|
||||||
|
from pathlib import Path
|
||||||
|
from datetime import datetime
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
|
from generate_report.core import create_report
|
||||||
|
|
||||||
|
|
||||||
|
def _load_json(path: str) -> Any:
|
||||||
|
"""Lädt eine JSON-Datei und gibt ihr Objekt zurück, inklusive Validierung."""
|
||||||
|
p = Path(path)
|
||||||
|
if not p.exists():
|
||||||
|
raise FileNotFoundError(f"Datei nicht gefunden: {path}")
|
||||||
|
with p.open('r', encoding='utf-8') as f:
|
||||||
|
try:
|
||||||
|
return json.load(f)
|
||||||
|
except json.JSONDecodeError as e:
|
||||||
|
raise ValueError(f"Ungültige JSON-Struktur in {path}: {e}") from e
|
||||||
|
|
||||||
|
|
||||||
|
def main() -> None:
|
||||||
|
"""Kommandozeileninterface zur Erstellung des Berichts aus den angegebenen JSON-Dateien."""
|
||||||
|
parser = argparse.ArgumentParser(
|
||||||
|
description="Erstellt einen Analysebericht aus Latenz-Messergebnissen und Analyse-Zusammenfassung."
|
||||||
|
)
|
||||||
|
parser.add_argument("--latency", required=True, help="Pfad zur Datei mit Latenz-Messergebnissen.")
|
||||||
|
parser.add_argument("--summary", required=True, help="Pfad zur Datei mit Analyse-Zusammenfassung.")
|
||||||
|
parser.add_argument("--output", required=True, help="Pfad, unter dem der generierte Bericht gespeichert wird.")
|
||||||
|
|
||||||
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
latency_results = _load_json(args.latency)
|
||||||
|
analysis_summary = _load_json(args.summary)
|
||||||
|
|
||||||
|
if not isinstance(latency_results, list):
|
||||||
|
raise TypeError("latency_results muss eine Liste von Objekten sein.")
|
||||||
|
if not isinstance(analysis_summary, dict):
|
||||||
|
raise TypeError("analysis_summary muss ein Dictionary sein.")
|
||||||
|
|
||||||
|
report = create_report(latency_results, analysis_summary)
|
||||||
|
|
||||||
|
if not isinstance(report, dict):
|
||||||
|
raise TypeError("create_report muss ein Dictionary zurückgeben.")
|
||||||
|
|
||||||
|
output_path = Path(args.output)
|
||||||
|
output_path.parent.mkdir(parents=True, exist_ok=True)
|
||||||
|
|
||||||
|
report['generated_at'] = datetime.utcnow().isoformat()
|
||||||
|
|
||||||
|
with output_path.open('w', encoding='utf-8') as f:
|
||||||
|
json.dump(report, f, indent=2, ensure_ascii=False)
|
||||||
|
|
||||||
|
print(f"Bericht erfolgreich erstellt: {output_path}")
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
Loading…
Reference in a new issue