Add artifact.metrics_analysis/src/artifact_metrics_analysis/cli.py

This commit is contained in:
Mika 2026-04-06 16:12:07 +00:00
parent 15010cd591
commit 8dd26f8a9f

View file

@ -0,0 +1,75 @@
import argparse
import json
from pathlib import Path
import logging
from typing import Any
from artifact_metrics_analysis.core import analyze_metrics
def main() -> None:
"""Command line interface for comparing Evidence Card metrics."""
parser = argparse.ArgumentParser(
description=(
"Analyze and compare metrics between Evidence Card #40 and #42, "
"producing a JSON summary of retry_tail_p99, band_width, and delta_band_width."
)
)
parser.add_argument(
"--card40", required=True, type=str,
help="Path to JSON metrics file for Evidence Card #40"
)
parser.add_argument(
"--card42", required=True, type=str,
help="Path to JSON metrics file for Evidence Card #42"
)
parser.add_argument(
"--output", required=False, type=str,
help="Path for the output JSON file (metrics comparison results)"
)
args = parser.parse_args()
logging.basicConfig(
level=logging.INFO,
format="[%(levelname)s] %(message)s"
)
try:
path_40 = Path(args.card40)
path_42 = Path(args.card42)
if not path_40.exists() or not path_42.exists():
raise FileNotFoundError("One or both input JSON files do not exist.")
with path_40.open('r', encoding='utf-8') as f40:
card_data_40: dict[str, Any] = json.load(f40)
with path_42.open('r', encoding='utf-8') as f42:
card_data_42: dict[str, Any] = json.load(f42)
# Input validation
assert isinstance(card_data_40, dict), "card_data_40 must be a dict"
assert isinstance(card_data_42, dict), "card_data_42 must be a dict"
result = analyze_metrics(card_data_40, card_data_42)
result_dict = result.to_dict() if hasattr(result, 'to_dict') else dict(result)
if args.output:
out_path = Path(args.output)
out_path.parent.mkdir(parents=True, exist_ok=True)
with out_path.open('w', encoding='utf-8') as outf:
json.dump(result_dict, outf, indent=2)
logging.info(f"Metrics comparison results written to {out_path}")
else:
print(json.dumps(result_dict, indent=2))
except AssertionError as ae:
logging.error(f"Input validation failed: {ae}")
except FileNotFoundError as fe:
logging.error(f"File not found: {fe}")
except json.JSONDecodeError as je:
logging.error(f"Invalid JSON input: {je}")
except Exception as exc:
logging.error(f"Unexpected error: {exc}")
if __name__ == "__main__":
main()