Add artifact.retry_analysis/src/artifact_retry_analysis/cli.py
This commit is contained in:
parent
535ba49562
commit
49252400d7
1 changed files with 55 additions and 0 deletions
55
artifact.retry_analysis/src/artifact_retry_analysis/cli.py
Normal file
55
artifact.retry_analysis/src/artifact_retry_analysis/cli.py
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
import argparse
|
||||
import json
|
||||
import sys
|
||||
from pathlib import Path
|
||||
from typing import Optional
|
||||
|
||||
from artifact_retry_analysis.core import analyze_retry_data
|
||||
|
||||
|
||||
def main(argv: Optional[list[str]] = None) -> None:
|
||||
"""Command-line interface to analyze retry logs and produce a JSON report."""
|
||||
parser = argparse.ArgumentParser(
|
||||
description="Analyze retry log data and produce a retry analysis report in JSON format."
|
||||
)
|
||||
parser.add_argument(
|
||||
"--input",
|
||||
required=True,
|
||||
help="Path to the input log file in JSON format."
|
||||
)
|
||||
parser.add_argument(
|
||||
"--output",
|
||||
required=False,
|
||||
help="Path to the output file where the analysis report will be saved."
|
||||
)
|
||||
|
||||
args = parser.parse_args(argv)
|
||||
|
||||
input_path = Path(args.input)
|
||||
if not input_path.exists() or not input_path.is_file():
|
||||
print(f"Error: Input file not found: {input_path}", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
|
||||
try:
|
||||
report = analyze_retry_data(str(input_path))
|
||||
except Exception as exc:
|
||||
print(f"Error analyzing retry data: {exc}", file=sys.stderr)
|
||||
sys.exit(2)
|
||||
|
||||
report_json = report.to_json()
|
||||
|
||||
if args.output:
|
||||
output_path = Path(args.output)
|
||||
try:
|
||||
output_path.parent.mkdir(parents=True, exist_ok=True)
|
||||
with output_path.open('w', encoding='utf-8') as f:
|
||||
f.write(report_json)
|
||||
except Exception as exc:
|
||||
print(f"Error writing output file: {exc}", file=sys.stderr)
|
||||
sys.exit(3)
|
||||
else:
|
||||
print(report_json)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Loading…
Reference in a new issue