From bb7a2b4b123df2c99bab7a091b4c57913c8979bc Mon Sep 17 00:00:00 2001 From: Mika Date: Wed, 4 Mar 2026 15:16:37 +0000 Subject: [PATCH] Add artifact.1/src/artifact_1/cli.py --- artifact.1/src/artifact_1/cli.py | 63 ++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 artifact.1/src/artifact_1/cli.py diff --git a/artifact.1/src/artifact_1/cli.py b/artifact.1/src/artifact_1/cli.py new file mode 100644 index 0000000..2e10336 --- /dev/null +++ b/artifact.1/src/artifact_1/cli.py @@ -0,0 +1,63 @@ +import argparse +import json +from pathlib import Path +from typing import Any, Dict, List +from statistics import mean + +from artifact_1 import core + + +def _validate_test_result(entry: Dict[str, Any]) -> bool: + required_fields = { + "group": str, + "pinned_status": str, + "warn_rate": (int, float), + "unknown_rate": (int, float), + "delta_t_rate": (int, float), + } + for field, expected_type in required_fields.items(): + if field not in entry: + raise ValueError(f"Missing required field: {field}") + if not isinstance(entry[field], expected_type): + raise TypeError(f"Field '{field}' has invalid type: {type(entry[field])}, expected {expected_type}") + if entry["group"] not in {"A", "B"}: + raise ValueError(f"Invalid group value: {entry['group']}") + if entry["pinned_status"] not in {"pinned", "unpinned"}: + raise ValueError(f"Invalid pinned_status value: {entry['pinned_status']}") + return True + + +def main() -> None: + parser = argparse.ArgumentParser( + description="Analyse von A/B-Testdaten basierend auf Eingabedateien." + ) + parser.add_argument("--input", required=True, help="Pfad zur JSON-Datei mit A/B-Testdaten.") + parser.add_argument( + "--output", required=True, help="Pfad zur Ausgabedatei mit aggregierten Ergebnissen." + ) + args = parser.parse_args() + + input_path = Path(args.input) + output_path = Path(args.output) + + if not input_path.exists(): + raise FileNotFoundError(f"Input file not found: {input_path}") + + with input_path.open("r", encoding="utf-8") as f: + raw_data = json.load(f) + + if not isinstance(raw_data, list): + raise ValueError("Input JSON must be a list of TestResult dictionaries.") + + for entry in raw_data: + _validate_test_result(entry) + + result = core.analyze_ab_data(raw_data) + + output_path.parent.mkdir(parents=True, exist_ok=True) + with output_path.open("w", encoding="utf-8") as f: + json.dump(result, f, indent=2, ensure_ascii=False) + + +if __name__ == "__main__": + main() \ No newline at end of file