From 4d325f19d851ca7aaf6b3396376ca9ef14195e2f Mon Sep 17 00:00:00 2001 From: Mika Date: Wed, 14 Jan 2026 15:28:05 +0000 Subject: [PATCH] Add trace_agg/src/trace_agg/cli.py --- trace_agg/src/trace_agg/cli.py | 62 ++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 trace_agg/src/trace_agg/cli.py diff --git a/trace_agg/src/trace_agg/cli.py b/trace_agg/src/trace_agg/cli.py new file mode 100644 index 0000000..108cb7d --- /dev/null +++ b/trace_agg/src/trace_agg/cli.py @@ -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()