Add outlier_analysis/src/outlier_analysis/cli.py
This commit is contained in:
parent
85fd92bba5
commit
8da7d82862
1 changed files with 51 additions and 0 deletions
51
outlier_analysis/src/outlier_analysis/cli.py
Normal file
51
outlier_analysis/src/outlier_analysis/cli.py
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
import argparse
|
||||
import json
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
from outlier_analysis.core import analyze_outliers
|
||||
|
||||
|
||||
def _validate_path(path_str: str, must_exist: bool = True) -> Path:
|
||||
path = Path(path_str)
|
||||
if must_exist and not path.exists():
|
||||
raise FileNotFoundError(f"Datei nicht gefunden: {path}")
|
||||
return path
|
||||
|
||||
|
||||
def _load_json(path: Path) -> Any:
|
||||
with path.open('r', encoding='utf-8') as f:
|
||||
return json.load(f)
|
||||
|
||||
|
||||
def _save_json(data: Any, path: Path) -> None:
|
||||
with path.open('w', encoding='utf-8') as f:
|
||||
json.dump(data, f, indent=2, ensure_ascii=False)
|
||||
|
||||
|
||||
def main() -> None:
|
||||
"""CLI-Entrypoint zur Analyse von Max-only-Alert Logdaten."""
|
||||
parser = argparse.ArgumentParser(description="Analyse von Max-only-Alert Logdaten und Erstellung von Outlier-Reports.")
|
||||
parser.add_argument("--input", required=True, help="Pfad zu den Logdaten im JSON-Format.")
|
||||
parser.add_argument("--output", required=True, help="Pfad zur Ausgabe des Outlier-Reports als JSON.")
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
input_path = _validate_path(args.input, must_exist=True)
|
||||
output_path = Path(args.output)
|
||||
|
||||
log_entries = _load_json(input_path)
|
||||
if not isinstance(log_entries, list):
|
||||
raise ValueError("Eingabedatei muss eine Liste von Log-Einträgen enthalten.")
|
||||
|
||||
result = analyze_outliers(log_entries)
|
||||
|
||||
assert isinstance(result, dict), "Analyseausgabe muss ein Dictionary sein."
|
||||
|
||||
_save_json(result, output_path)
|
||||
|
||||
print(f"Outlier-Report erfolgreich unter {output_path} gespeichert.")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Loading…
Reference in a new issue