Add artifact.1/src/artifact_1/cli.py
This commit is contained in:
parent
43cfda9bfb
commit
bb7a2b4b12
1 changed files with 63 additions and 0 deletions
63
artifact.1/src/artifact_1/cli.py
Normal file
63
artifact.1/src/artifact_1/cli.py
Normal file
|
|
@ -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()
|
||||
Loading…
Reference in a new issue