Add trace_agg/src/trace_agg/cli.py

This commit is contained in:
Mika 2026-01-14 15:28:05 +00:00
parent 893d98bbc4
commit 4d325f19d8

View file

@ -0,0 +1,62 @@
import argparse
import json
import sys
from pathlib import Path
from typing import Any
from trace_agg import core
def _validate_file_path(path_str: str, must_exist: bool = True) -> Path:
path = Path(path_str)
if must_exist and not path.is_file():
raise FileNotFoundError(f"Eingabedatei nicht gefunden: {path}")
return path
def main() -> None:
"""CLI-Einstiegspunkt zur Aggregation von eBPF-Tracedaten."""
parser = argparse.ArgumentParser(
description="Aggregiere eBPF-Tracedaten pro correlation_id und speichere eine zusammengefasste JSON-Ausgabe."
)
parser.add_argument(
"--input",
required=True,
help="Pfad zur Eingabedatei mit Tracedaten im JSON-Format."
)
parser.add_argument(
"--output",
required=True,
help="Pfad zur Ausgabedatei für das aggregierte JSON."
)
args = parser.parse_args()
try:
input_path = _validate_file_path(args.input)
output_path = Path(args.output)
with input_path.open("r", encoding="utf-8") as f:
trace_data = json.load(f)
correlation_ids = trace_data.keys() if isinstance(trace_data, dict) else []
if not correlation_ids:
raise ValueError("Keine Correlation-IDs in der Eingabedatei gefunden.")
aggregated_results: dict[str, Any] = {}
for cid in correlation_ids:
aggregated_results[cid] = core.aggregate_trace_data(cid)
output_path.parent.mkdir(parents=True, exist_ok=True)
with output_path.open("w", encoding="utf-8") as out:
json.dump(aggregated_results, out, indent=2)
print(f"Aggregierte Ergebnisse gespeichert in: {output_path}")
except Exception as exc:
print(f"Fehler bei der Ausführung: {exc}", file=sys.stderr)
sys.exit(1)
if __name__ == "__main__":
main()