|
| 1 | +from nicegui import app, ui |
| 2 | +import json |
| 3 | +import time |
| 4 | +import os |
| 5 | +import glob |
| 6 | +import requests |
| 7 | +from theme import menu |
| 8 | +from pages.settings import load_config |
| 9 | + |
| 10 | +anomalies = {} |
| 11 | +active_connections = [] |
| 12 | + |
| 13 | +def fetch_anomalies(): |
| 14 | + full_config = load_config() |
| 15 | + cp_config = full_config.get('control_plane', {}) |
| 16 | + anomaly_path = cp_config.get('logPaths', {}).get('anomalyLogs', 'logs/anomalies/') |
| 17 | + abs_anomaly_path = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..', anomaly_path)) |
| 18 | + |
| 19 | + # Read JSON files in this directory |
| 20 | + new_anomalies = {} |
| 21 | + if os.path.exists(abs_anomaly_path): |
| 22 | + for file in glob.glob(os.path.join(abs_anomaly_path, '*.json')): |
| 23 | + try: |
| 24 | + with open(file, 'r') as f: |
| 25 | + data = json.load(f) |
| 26 | + |
| 27 | + # Ensure source exists using filename or data |
| 28 | + source = data.get('source', 'unknown') |
| 29 | + if source == 'unknown': |
| 30 | + # Try to parse from filename: anomaly_rig1_timestamp.json |
| 31 | + basename = os.path.basename(file) |
| 32 | + if '_' in basename: |
| 33 | + source = basename.split('_')[1] |
| 34 | + |
| 35 | + if source not in new_anomalies: |
| 36 | + new_anomalies[source] = [] |
| 37 | + new_anomalies[source].append({ |
| 38 | + "time": time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(os.path.getmtime(file))), |
| 39 | + "data": data, |
| 40 | + "mtime": os.path.getmtime(file) |
| 41 | + }) |
| 42 | + except Exception as e: |
| 43 | + pass |
| 44 | + |
| 45 | + # Sort and keep latest 10 |
| 46 | + global anomalies |
| 47 | + anomalies.clear() |
| 48 | + for source, events in new_anomalies.items(): |
| 49 | + sorted_events = sorted(events, key=lambda x: x['mtime']) |
| 50 | + anomalies[source] = sorted_events[-10:] |
| 51 | + |
| 52 | + |
| 53 | +def fetch_connections(): |
| 54 | + full_config = load_config() |
| 55 | + mc_config = full_config.get('mobile_client', {}) |
| 56 | + edge_port = mc_config.get('port', 8080) |
| 57 | + try: |
| 58 | + resp = requests.get(f'http://127.0.0.1:{edge_port}/api/status', timeout=1) |
| 59 | + if resp.status_code == 200: |
| 60 | + data = resp.json() |
| 61 | + global active_connections |
| 62 | + active_connections = data.get('connections', []) |
| 63 | + except Exception: |
| 64 | + pass |
| 65 | + |
| 66 | + |
| 67 | +def create_page(): |
| 68 | + @ui.page('/') |
| 69 | + def dashboard_page(): |
| 70 | + val1 = load_config() |
| 71 | + cp_config = val1.get('control_plane', {}) |
| 72 | + anomaly_path = cp_config.get('logPaths', {}).get('anomalyLogs', 'logs/anomalies/') |
| 73 | + |
| 74 | + with menu('Dashboard'): |
| 75 | + # Fetch connections layout |
| 76 | + ui.label('Active Mobile Connections').classes('text-xl font-semibold mb-2 mt-4') |
| 77 | + conn_container = ui.list().classes('w-full border rounded p-2 mb-6') |
| 78 | + |
| 79 | + def render_connections(): |
| 80 | + fetch_connections() |
| 81 | + conn_container.clear() |
| 82 | + with conn_container: |
| 83 | + if not active_connections: |
| 84 | + ui.label('No active connections. Wait for mobile clients to pair...').classes('text-gray-500 italic p-4') |
| 85 | + return |
| 86 | + for conn in active_connections: |
| 87 | + ui.label(f'Active Device: {conn.get("clientName", "Unknown")} (ID: {conn.get("id")})').classes('font-bold') |
| 88 | + if conn.get("lastFilename"): |
| 89 | + ui.label(f' Last Upload: {conn["lastFilename"]} ({conn["lastFilesize"]} bytes)').classes('text-sm text-gray-600') |
| 90 | + |
| 91 | + ui.timer(2.0, render_connections) |
| 92 | + |
| 93 | + ui.label('System Anomalies').classes('text-xl font-semibold mb-2') |
| 94 | + container = ui.list().classes('w-full border rounded p-2') |
| 95 | + |
| 96 | + def render_anomalies(): |
| 97 | + fetch_anomalies() |
| 98 | + container.clear() |
| 99 | + with container: |
| 100 | + if not anomalies: |
| 101 | + ui.label(f'No anomalies detected yet. Watching: {anomaly_path}').classes('text-gray-500 italic p-4') |
| 102 | + return |
| 103 | + |
| 104 | + for source, events in anomalies.items(): |
| 105 | + title = f'Rig: {source} ({len(events)} events)' |
| 106 | + with ui.expansion(title, icon='warning').classes('w-full bg-gray-50 mb-2'): |
| 107 | + for ev in reversed(events): |
| 108 | + data_str = json.dumps(ev["data"]) |
| 109 | + ui.label(f'Event: {ev["time"]} - {data_str}').classes('font-mono text-sm mb-1') |
| 110 | + ui.button('Investigate via Agent', on_click=lambda s=source: ui.navigate.to(f'/agent?source={s}')).classes('mt-2 bg-blue-500 text-white') |
| 111 | + |
| 112 | + ui.timer(3.0, render_anomalies) |
| 113 | + |
0 commit comments