Add metrics_reporting/src/metrics_reporting/cli.py
This commit is contained in:
parent
f8134d729e
commit
6797229364
1 changed files with 74 additions and 0 deletions
74
metrics_reporting/src/metrics_reporting/cli.py
Normal file
74
metrics_reporting/src/metrics_reporting/cli.py
Normal file
|
|
@ -0,0 +1,74 @@
|
||||||
|
import argparse
|
||||||
|
import json
|
||||||
|
import sys
|
||||||
|
from pathlib import Path
|
||||||
|
from metrics_reporting import core
|
||||||
|
|
||||||
|
|
||||||
|
def _validate_json_file(path: Path) -> None:
|
||||||
|
if not path.exists():
|
||||||
|
raise FileNotFoundError(f"Input file not found: {path}")
|
||||||
|
if not path.is_file():
|
||||||
|
raise ValueError(f"Input path is not a file: {path}")
|
||||||
|
try:
|
||||||
|
with path.open("r", encoding="utf-8") as f:
|
||||||
|
data = json.load(f)
|
||||||
|
if not isinstance(data, dict):
|
||||||
|
raise ValueError("Input JSON must contain an object with metric fields.")
|
||||||
|
required_fields = {"run_number", "warn_rate", "unknown_rate", "unpin_delta_t"}
|
||||||
|
missing = required_fields - data.keys()
|
||||||
|
if missing:
|
||||||
|
raise ValueError(f"Missing required fields in input JSON: {', '.join(missing)}")
|
||||||
|
except json.JSONDecodeError as e:
|
||||||
|
raise ValueError(f"Invalid JSON structure: {e}") from e
|
||||||
|
|
||||||
|
|
||||||
|
def main() -> None:
|
||||||
|
parser = argparse.ArgumentParser(
|
||||||
|
description="CLI interface for stability metrics reporting."
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
"--input",
|
||||||
|
required=True,
|
||||||
|
type=Path,
|
||||||
|
help="Pfad zur JSON-Datei mit Metriken eines Runs."
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
"--output",
|
||||||
|
required=True,
|
||||||
|
type=Path,
|
||||||
|
help="Pfad zur Ausgabedatei mit berechneten Kennzahlen."
|
||||||
|
)
|
||||||
|
|
||||||
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
try:
|
||||||
|
_validate_json_file(args.input)
|
||||||
|
with args.input.open("r", encoding="utf-8") as infile:
|
||||||
|
metrics_data = json.load(infile)
|
||||||
|
|
||||||
|
core.report_metrics(
|
||||||
|
run_number=int(metrics_data["run_number"]),
|
||||||
|
warn_rate=float(metrics_data["warn_rate"]),
|
||||||
|
unknown_rate=float(metrics_data["unknown_rate"]),
|
||||||
|
unpin_delta_t=float(metrics_data["unpin_delta_t"]),
|
||||||
|
)
|
||||||
|
|
||||||
|
# Simulate writing of output report
|
||||||
|
report = {
|
||||||
|
"run_number": metrics_data["run_number"],
|
||||||
|
"warn_rate": metrics_data["warn_rate"],
|
||||||
|
"unknown_rate": metrics_data["unknown_rate"],
|
||||||
|
"unpin_delta_t": metrics_data["unpin_delta_t"]
|
||||||
|
}
|
||||||
|
args.output.parent.mkdir(parents=True, exist_ok=True)
|
||||||
|
with args.output.open("w", encoding="utf-8") as outfile:
|
||||||
|
json.dump(report, outfile, indent=2)
|
||||||
|
|
||||||
|
except Exception as exc:
|
||||||
|
print(f"Error during metrics reporting: {exc}", file=sys.stderr)
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
Loading…
Reference in a new issue