|
| 1 | +import dash |
| 2 | +from dash import html, dcc |
| 3 | +import dash_bootstrap_components as dbc |
| 4 | +from dash.dependencies import Input, Output |
| 5 | +import laue_portal.components.navbar as navbar |
| 6 | +from laue_portal.processing import redis_utils |
| 7 | +import config |
| 8 | +import os |
| 9 | + |
| 10 | +dash.register_page(__name__, path='/') |
| 11 | + |
| 12 | +# Build the layout |
| 13 | +layout = html.Div([ |
| 14 | + navbar.navbar, |
| 15 | + dbc.Container([ |
| 16 | + # Auto-refresh interval (every 5 seconds) |
| 17 | + dcc.Interval( |
| 18 | + id='status-refresh-interval', |
| 19 | + interval=5*1000, # in milliseconds |
| 20 | + n_intervals=0 |
| 21 | + ), |
| 22 | + |
| 23 | + # Row 1: Welcome and Connection Status |
| 24 | + dbc.Row([ |
| 25 | + # Welcome Card |
| 26 | + dbc.Col([ |
| 27 | + dbc.Card([ |
| 28 | + dbc.CardHeader(html.H4("Welcome to the 3DMN Laue Portal", className="mb-0")), |
| 29 | + dbc.CardBody([ |
| 30 | + html.P([ |
| 31 | + "The 3DMN Laue Portal is a data processing platform for ", |
| 32 | + "Laue X-ray diffraction experiments at beamline 34-ID-E." |
| 33 | + ], className="mb-3"), |
| 34 | + html.H6("Capabilities:", className="mb-2"), |
| 35 | + html.Ul([ |
| 36 | + html.Li("Scan management and tracking"), |
| 37 | + html.Li("Wire-based depth reconstruction"), |
| 38 | + html.Li("Coded aperture reconstruction"), |
| 39 | + html.Li("Automated peak indexing"), |
| 40 | + html.Li("Distributed job queue"), |
| 41 | + ], className="mb-0"), |
| 42 | + ]) |
| 43 | + ], className="h-100") |
| 44 | + ], md=6, className="mb-4"), |
| 45 | + |
| 46 | + # Connection Status Card |
| 47 | + dbc.Col([ |
| 48 | + dbc.Card([ |
| 49 | + dbc.CardHeader(html.H4("Connection Status", className="mb-0")), |
| 50 | + dbc.CardBody([ |
| 51 | + html.Div(id='connection-status-content') |
| 52 | + ]) |
| 53 | + ], className="h-100") |
| 54 | + ], md=6, className="mb-4"), |
| 55 | + ]), |
| 56 | + |
| 57 | + # Row 2: System Resources and Quick Actions |
| 58 | + dbc.Row([ |
| 59 | + # System Resources Card |
| 60 | + dbc.Col([ |
| 61 | + dbc.Card([ |
| 62 | + dbc.CardHeader(html.H4("System Resources", className="mb-0")), |
| 63 | + dbc.CardBody([ |
| 64 | + html.Div(id='system-resources-content') |
| 65 | + ]) |
| 66 | + ], className="h-100") |
| 67 | + ], md=6, className="mb-4"), |
| 68 | + |
| 69 | + # Quick Actions Card |
| 70 | + dbc.Col([ |
| 71 | + dbc.Card([ |
| 72 | + dbc.CardHeader(html.H4("Quick Actions", className="mb-0")), |
| 73 | + dbc.CardBody([ |
| 74 | + dbc.ListGroup([ |
| 75 | + dbc.ListGroupItem([ |
| 76 | + html.I(className="bi bi-list-ul me-2"), |
| 77 | + "View All Scans" |
| 78 | + ], href="/scans", action=True, className="d-flex align-items-center"), |
| 79 | + dbc.ListGroupItem([ |
| 80 | + html.I(className="bi bi-gear me-2"), |
| 81 | + "Create Wire Reconstruction" |
| 82 | + ], href="/create-wire-reconstruction", action=True, className="d-flex align-items-center"), |
| 83 | + dbc.ListGroupItem([ |
| 84 | + html.I(className="bi bi-grid-3x3 me-2"), |
| 85 | + "Create Coded Aperture Reconstruction" |
| 86 | + ], href="/create-reconstruction", action=True, className="d-flex align-items-center"), |
| 87 | + dbc.ListGroupItem([ |
| 88 | + html.I(className="bi bi-search me-2"), |
| 89 | + "Create Peak Indexing" |
| 90 | + ], href="/create-peakindexing", action=True, className="d-flex align-items-center"), |
| 91 | + dbc.ListGroupItem([ |
| 92 | + html.I(className="bi bi-activity me-2"), |
| 93 | + "Monitor Job Queue" |
| 94 | + ], href="/run-monitor", action=True, className="d-flex align-items-center"), |
| 95 | + ], flush=True) |
| 96 | + ]) |
| 97 | + ], className="h-100") |
| 98 | + ], md=6, className="mb-4"), |
| 99 | + ]), |
| 100 | + |
| 101 | + ], fluid=True, className="mt-4") |
| 102 | +]) |
| 103 | + |
| 104 | + |
| 105 | +# Callback to update connection status |
| 106 | +@dash.callback( |
| 107 | + Output('connection-status-content', 'children'), |
| 108 | + Input('status-refresh-interval', 'n_intervals') |
| 109 | +) |
| 110 | +def update_connection_status(n): |
| 111 | + """Update the connection status card with current system information.""" |
| 112 | + |
| 113 | + # Check Redis connection |
| 114 | + redis_connected = redis_utils.check_redis_connection() |
| 115 | + redis_startup_status = redis_utils.REDIS_CONNECTED_AT_STARTUP |
| 116 | + |
| 117 | + # Database path |
| 118 | + db_path = config.db_file |
| 119 | + db_exists = os.path.exists(db_path) |
| 120 | + |
| 121 | + # Server info |
| 122 | + dash_url = f"http://{config.DASH_CONFIG['host']}:{config.DASH_CONFIG['port']}" |
| 123 | + redis_url = f"{config.REDIS_CONFIG['host']}:{config.REDIS_CONFIG['port']}" |
| 124 | + |
| 125 | + return [ |
| 126 | + # Database Status |
| 127 | + html.Div([ |
| 128 | + html.Strong("Database: "), |
| 129 | + dbc.Badge( |
| 130 | + "Connected" if db_exists else "Not Found", |
| 131 | + color="success" if db_exists else "danger", |
| 132 | + className="me-2" |
| 133 | + ), |
| 134 | + html.Br(), |
| 135 | + html.Small(db_path, className="text-muted"), |
| 136 | + ], className="mb-3"), |
| 137 | + |
| 138 | + # Dash Server Status |
| 139 | + html.Div([ |
| 140 | + html.Strong("Dash Server: "), |
| 141 | + dbc.Badge("Running", color="success", className="me-2"), |
| 142 | + html.Br(), |
| 143 | + html.Small(dash_url, className="text-muted"), |
| 144 | + ], className="mb-3"), |
| 145 | + |
| 146 | + # Redis Status |
| 147 | + html.Div([ |
| 148 | + html.Strong("Redis Queue: "), |
| 149 | + dbc.Badge( |
| 150 | + "Connected" if redis_connected else "Disconnected", |
| 151 | + color="success" if redis_connected else "danger", |
| 152 | + className="me-2" |
| 153 | + ), |
| 154 | + html.Br(), |
| 155 | + html.Small(redis_url, className="text-muted"), |
| 156 | + html.Br(), |
| 157 | + html.Small( |
| 158 | + f"Startup status: {'Connected' if redis_startup_status else 'Disconnected'}" |
| 159 | + if redis_startup_status is not None else "Startup status: Unknown", |
| 160 | + className="text-muted fst-italic" |
| 161 | + ), |
| 162 | + ], className="mb-0"), |
| 163 | + ] |
| 164 | + |
| 165 | + |
| 166 | +# Callback to update system resources |
| 167 | +@dash.callback( |
| 168 | + Output('system-resources-content', 'children'), |
| 169 | + Input('status-refresh-interval', 'n_intervals') |
| 170 | +) |
| 171 | +def update_system_resources(n): |
| 172 | + """Update the system resources card with queue and worker information.""" |
| 173 | + |
| 174 | + try: |
| 175 | + # Get queue statistics |
| 176 | + queue_stats = redis_utils.get_queue_stats() |
| 177 | + |
| 178 | + # Get worker information |
| 179 | + workers_info = redis_utils.get_workers_info() |
| 180 | + |
| 181 | + return [ |
| 182 | + # Queue Statistics |
| 183 | + html.H6("Job Queue:", className="mb-2"), |
| 184 | + dbc.Row([ |
| 185 | + dbc.Col([ |
| 186 | + html.Div([ |
| 187 | + html.H3(queue_stats.get('queued', 0), className="mb-0 text-primary"), |
| 188 | + html.Small("Queued", className="text-muted") |
| 189 | + ], className="text-center") |
| 190 | + ], width=6), |
| 191 | + dbc.Col([ |
| 192 | + html.Div([ |
| 193 | + html.H3(queue_stats.get('started', 0), className="mb-0 text-info"), |
| 194 | + html.Small("Running", className="text-muted") |
| 195 | + ], className="text-center") |
| 196 | + ], width=6), |
| 197 | + ], className="mb-3"), |
| 198 | + |
| 199 | + html.Hr(), |
| 200 | + |
| 201 | + # Worker Information |
| 202 | + html.H6("Workers:", className="mb-2"), |
| 203 | + html.Div([ |
| 204 | + dbc.Badge( |
| 205 | + f"{len(workers_info)} Active" if workers_info else "No Workers", |
| 206 | + color="success" if workers_info else "warning", |
| 207 | + className="me-2" |
| 208 | + ), |
| 209 | + html.Br() if workers_info else None, |
| 210 | + html.Div([ |
| 211 | + html.Div([ |
| 212 | + html.Small([ |
| 213 | + html.Strong(f"{worker['name']}: "), |
| 214 | + f"{worker['state']}" |
| 215 | + ]) |
| 216 | + ], className="mb-1") for worker in workers_info |
| 217 | + ] if workers_info else [], className="mt-2") |
| 218 | + ], className="mb-0"), |
| 219 | + ] |
| 220 | + |
| 221 | + except Exception as e: |
| 222 | + return [ |
| 223 | + dbc.Alert([ |
| 224 | + html.I(className="bi bi-exclamation-triangle me-2"), |
| 225 | + "No system data. Redis is not connected." |
| 226 | + ], color="warning", className="mb-0") |
| 227 | + ] |
0 commit comments