def wg_events(file, filter_client, filter_type, limit, collapse='1', since='', filter_event='', endpoint_cache_file=''): """ Format WireGuard events with dedup, counts, gap and endpoint resolution. Output per line: ts|client|endpoint|event|count|gap_seconds """ from datetime import datetime from collections import defaultdict do_collapse = str(collapse) != '0' limit = int(limit) if limit else 50 since_dt = parse_since(since) if since else None # Load endpoint cache once endpoint_cache = {} if endpoint_cache_file and os.path.exists(endpoint_cache_file): try: with open(endpoint_cache_file) as f: endpoint_cache = json.load(f) except Exception: pass events = [] try: with open(file) as f: for line in f: try: e = json.loads(line.strip()) client = e.get('client', '') if not client: continue if filter_client and client != filter_client: continue if filter_type and not client.startswith(filter_type + '-'): continue if filter_event and e.get('event', '') != filter_event: continue if since_dt: ts_str = e.get('timestamp', '') try: from datetime import timezone