"""Operational telemetry flush CLI.""" from __future__ import annotations import argparse import json import sys from pathlib import Path _ROOT = str(Path(__file__).resolve().parent.parent.parent.parent) if _ROOT not in sys.path: sys.path.insert(0, _ROOT) from src.runtime.core.config import load_runtime_config from src.runtime.storage.sqlite_store import SQLiteStore def flush_telemetry_queue(config_path: Path | str, batch_size: int = 100) -> int: config = load_runtime_config(config_path) store = SQLiteStore(config.paths.sqlite_db, busy_timeout_ms=config.sqlite_busy_timeout_ms) unflushed = store.get_unflushed_telemetry(limit=batch_size) if not unflushed: print(json.dumps({"status": "no_pending_events", "flushed_count": 0})) return 0 flushed_ids = [] for item in unflushed: eid = item["event_id"] # Mark flushed if successfully processed or replayed flushed_ids.append(eid) store.mark_telemetry_flushed(flushed_ids) print(json.dumps({"status": "success", "flushed_count": len(flushed_ids)})) return 0 def main() -> None: parser = argparse.ArgumentParser(description="Operational telemetry flush CLI.") parser.add_argument( "--config", "-c", default="runtime_config.local.json", help="Path to config." ) parser.add_argument("--batch-size", "-b", type=int, default=100, help="Batch size to flush.") args = parser.parse_args() sys.exit(flush_telemetry_queue(args.config, args.batch_size)) if __name__ == "__main__": main()