askrobots / dbbasic-object-primitive
Public
Python functions as REST objects, with clustering, load balancing and state replication; no containers or orchestration.
dashboard.py · 12.2 KB · 356 lines
| 1 | """ |
| 2 | Dashboard - HTML interface for Object Primitive system |
| 3 | |
| 4 | Provides visual interface to: |
| 5 | - See all objects |
| 6 | - View object details (code, logs, metrics) |
| 7 | - Monitor performance |
| 8 | - Trigger actions (execute, rollback) |
| 9 | |
| 10 | Access: http://localhost:8001/dashboard |
| 11 | """ |
| 12 | from dbbasic_web.responses import html as html_response |
| 13 | |
| 14 | |
| 15 | def GET(request): |
| 16 | """Render dashboard HTML""" |
| 17 | |
| 18 | html = """ |
| 19 | <!DOCTYPE html> |
| 20 | <html> |
| 21 | <head> |
| 22 | <title>Object Primitive Dashboard</title> |
| 23 | <style> |
| 24 | * { margin: 0; padding: 0; box-sizing: border-box; } |
| 25 | body { |
| 26 | font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif; |
| 27 | background: #0a0a0a; |
| 28 | color: #e0e0e0; |
| 29 | padding: 20px; |
| 30 | } |
| 31 | .header { |
| 32 | background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); |
| 33 | padding: 30px; |
| 34 | border-radius: 12px; |
| 35 | margin-bottom: 30px; |
| 36 | } |
| 37 | h1 { font-size: 32px; margin-bottom: 10px; } |
| 38 | .subtitle { opacity: 0.9; font-size: 16px; } |
| 39 | .stats { |
| 40 | display: grid; |
| 41 | grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); |
| 42 | gap: 20px; |
| 43 | margin-bottom: 30px; |
| 44 | } |
| 45 | .stat-card { |
| 46 | background: #1a1a1a; |
| 47 | padding: 20px; |
| 48 | border-radius: 8px; |
| 49 | border: 1px solid #333; |
| 50 | } |
| 51 | .stat-value { font-size: 36px; font-weight: bold; color: #667eea; } |
| 52 | .stat-label { color: #999; margin-top: 5px; } |
| 53 | .objects-grid { |
| 54 | display: grid; |
| 55 | grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); |
| 56 | gap: 20px; |
| 57 | } |
| 58 | .object-card { |
| 59 | background: #1a1a1a; |
| 60 | padding: 20px; |
| 61 | border-radius: 8px; |
| 62 | border: 1px solid #333; |
| 63 | transition: all 0.3s; |
| 64 | cursor: pointer; |
| 65 | } |
| 66 | .object-card:hover { |
| 67 | border-color: #667eea; |
| 68 | transform: translateY(-2px); |
| 69 | box-shadow: 0 4px 12px rgba(102, 126, 234, 0.2); |
| 70 | } |
| 71 | .object-name { font-size: 20px; font-weight: bold; margin-bottom: 10px; } |
| 72 | .object-meta { |
| 73 | display: flex; |
| 74 | gap: 15px; |
| 75 | margin: 10px 0; |
| 76 | font-size: 14px; |
| 77 | color: #999; |
| 78 | } |
| 79 | .object-actions { |
| 80 | display: flex; |
| 81 | gap: 10px; |
| 82 | margin-top: 15px; |
| 83 | } |
| 84 | .btn { |
| 85 | padding: 8px 16px; |
| 86 | border-radius: 4px; |
| 87 | border: none; |
| 88 | cursor: pointer; |
| 89 | font-size: 14px; |
| 90 | transition: all 0.2s; |
| 91 | } |
| 92 | .btn-primary { |
| 93 | background: #667eea; |
| 94 | color: white; |
| 95 | } |
| 96 | .btn-primary:hover { |
| 97 | background: #5568d3; |
| 98 | } |
| 99 | .btn-secondary { |
| 100 | background: #333; |
| 101 | color: #e0e0e0; |
| 102 | } |
| 103 | .btn-secondary:hover { |
| 104 | background: #444; |
| 105 | } |
| 106 | .loading { |
| 107 | text-align: center; |
| 108 | padding: 40px; |
| 109 | color: #999; |
| 110 | } |
| 111 | .metric { |
| 112 | display: inline-block; |
| 113 | background: #2a2a2a; |
| 114 | padding: 4px 12px; |
| 115 | border-radius: 4px; |
| 116 | font-size: 12px; |
| 117 | margin-right: 8px; |
| 118 | } |
| 119 | .metric-good { color: #4ade80; } |
| 120 | .metric-warn { color: #fbbf24; } |
| 121 | .metric-error { color: #ef4444; } |
| 122 | </style> |
| 123 | </head> |
| 124 | <body> |
| 125 | <div class="header"> |
| 126 | <h1>🔮 Object Primitive Dashboard</h1> |
| 127 | <div class="subtitle">Multi-system compute. Unix only solved single-system.</div> |
| 128 | </div> |
| 129 | |
| 130 | <div class="stats"> |
| 131 | <div class="stat-card"> |
| 132 | <div class="stat-value" id="object-count">-</div> |
| 133 | <div class="stat-label">Total Objects</div> |
| 134 | </div> |
| 135 | <div class="stat-card"> |
| 136 | <div class="stat-value" id="station-count">-</div> |
| 137 | <div class="stat-label">Active Stations</div> |
| 138 | </div> |
| 139 | <div class="stat-card"> |
| 140 | <div class="stat-value" id="cluster-cpu">-</div> |
| 141 | <div class="stat-label">Cluster CPU</div> |
| 142 | </div> |
| 143 | <div class="stat-card"> |
| 144 | <div class="stat-value" id="cluster-memory">-</div> |
| 145 | <div class="stat-label">Cluster Memory</div> |
| 146 | </div> |
| 147 | </div> |
| 148 | |
| 149 | <div id="cluster-stations" style="margin-bottom: 30px;"> |
| 150 | <h2 style="margin-bottom: 15px; color: #667eea;">Cluster Stations</h2> |
| 151 | <div id="stations-grid" class="objects-grid"> |
| 152 | <div class="loading">Loading cluster status...</div> |
| 153 | </div> |
| 154 | </div> |
| 155 | |
| 156 | <h2 style="margin-bottom: 15px; color: #667eea;">Objects</h2> |
| 157 | <div id="objects-container" class="objects-grid"> |
| 158 | <div class="loading">Loading objects...</div> |
| 159 | </div> |
| 160 | |
| 161 | <script> |
| 162 | // Fetch cluster stations |
| 163 | async function loadClusterStats() { |
| 164 | try { |
| 165 | const response = await fetch('/cluster/stations'); |
| 166 | const data = await response.json(); |
| 167 | |
| 168 | if (!data.stations) { |
| 169 | console.error('No stations found'); |
| 170 | return; |
| 171 | } |
| 172 | |
| 173 | // Update cluster stats |
| 174 | const activeStations = data.stations.filter(s => s.is_active); |
| 175 | document.getElementById('station-count').textContent = |
| 176 | `${activeStations.length}/${data.stations.length}`; |
| 177 | |
| 178 | // Calculate cluster-wide metrics |
| 179 | let totalCpu = 0, totalMemory = 0, stationsWithMetrics = 0; |
| 180 | for (const station of activeStations) { |
| 181 | if (station.metrics && station.metrics.cpu_percent !== undefined) { |
| 182 | totalCpu += station.metrics.cpu_percent; |
| 183 | totalMemory += station.metrics.memory_percent; |
| 184 | stationsWithMetrics++; |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | if (stationsWithMetrics > 0) { |
| 189 | const avgCpu = (totalCpu / stationsWithMetrics).toFixed(1); |
| 190 | const avgMemory = (totalMemory / stationsWithMetrics).toFixed(1); |
| 191 | document.getElementById('cluster-cpu').textContent = `${avgCpu}%`; |
| 192 | document.getElementById('cluster-memory').textContent = `${avgMemory}%`; |
| 193 | } else { |
| 194 | document.getElementById('cluster-cpu').textContent = 'N/A'; |
| 195 | document.getElementById('cluster-memory').textContent = 'N/A'; |
| 196 | } |
| 197 | |
| 198 | // Render stations grid |
| 199 | const container = document.getElementById('stations-grid'); |
| 200 | container.innerHTML = ''; |
| 201 | |
| 202 | for (const station of data.stations) { |
| 203 | const card = createStationCard(station); |
| 204 | container.appendChild(card); |
| 205 | } |
| 206 | |
| 207 | } catch (error) { |
| 208 | console.error('Error loading cluster stats:', error); |
| 209 | document.getElementById('stations-grid').innerHTML = |
| 210 | '<div class="loading">Error loading cluster</div>'; |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | function createStationCard(station) { |
| 215 | const card = document.createElement('div'); |
| 216 | card.className = 'object-card'; |
| 217 | |
| 218 | // Master gets a crown, workers get colored circles |
| 219 | const isMaster = station.station_id === 'station1'; |
| 220 | const statusClass = station.is_active ? 'metric-good' : 'metric-error'; |
| 221 | const statusText = station.is_active |
| 222 | ? (isMaster ? '👑 Master' : '🟢 Active') |
| 223 | : '🔴 Offline'; |
| 224 | |
| 225 | let metricsHtml = ''; |
| 226 | if (station.metrics) { |
| 227 | const m = station.metrics; |
| 228 | metricsHtml = ` |
| 229 | <div style="margin: 10px 0;"> |
| 230 | <span class="metric">CPU: ${m.cpu_percent?.toFixed(1) || 0}%</span> |
| 231 | <span class="metric">RAM: ${m.memory_percent?.toFixed(1) || 0}%</span> |
| 232 | <span class="metric">Objects: ${m.object_count || 0}</span> |
| 233 | </div> |
| 234 | `; |
| 235 | } |
| 236 | |
| 237 | const version = station.version || 'unknown'; |
| 238 | const versionClass = version === 'unknown' ? 'metric-warning' : 'metric'; |
| 239 | |
| 240 | card.innerHTML = ` |
| 241 | <div class="object-name">${station.station_id}</div> |
| 242 | <div class="object-meta"> |
| 243 | <span class="${statusClass}">${statusText}</span> |
| 244 | <span>${station.host}:${station.port}</span> |
| 245 | <span class="${versionClass}">v${version}</span> |
| 246 | </div> |
| 247 | ${metricsHtml} |
| 248 | <div class="object-actions"> |
| 249 | <button class="btn btn-primary" onclick="window.open('${station.url}/dashboard', '_blank')"> |
| 250 | Open Dashboard |
| 251 | </button> |
| 252 | <button class="btn btn-secondary" onclick="window.open('${station.url}/objects', '_blank')"> |
| 253 | View Objects |
| 254 | </button> |
| 255 | </div> |
| 256 | `; |
| 257 | |
| 258 | return card; |
| 259 | } |
| 260 | |
| 261 | // Fetch all objects |
| 262 | async function loadObjects() { |
| 263 | try { |
| 264 | const response = await fetch('/objects'); |
| 265 | const data = await response.json(); |
| 266 | |
| 267 | if (!data.objects) { |
| 268 | console.error('No objects found'); |
| 269 | return; |
| 270 | } |
| 271 | |
| 272 | // Update stats |
| 273 | document.getElementById('object-count').textContent = data.objects.length; |
| 274 | |
| 275 | // Render objects |
| 276 | const container = document.getElementById('objects-container'); |
| 277 | container.innerHTML = ''; |
| 278 | |
| 279 | for (const obj of data.objects) { |
| 280 | const card = await createObjectCard(obj); |
| 281 | container.appendChild(card); |
| 282 | } |
| 283 | |
| 284 | } catch (error) { |
| 285 | console.error('Error loading objects:', error); |
| 286 | document.getElementById('objects-container').innerHTML = |
| 287 | '<div class="loading">Error loading objects</div>'; |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | async function createObjectCard(obj) { |
| 292 | const card = document.createElement('div'); |
| 293 | card.className = 'object-card'; |
| 294 | |
| 295 | // Fetch metadata for this object |
| 296 | let metadata = {}; |
| 297 | try { |
| 298 | const response = await fetch(`/objects/${obj.object_id}?metadata=true`); |
| 299 | metadata = await response.json(); |
| 300 | } catch (e) { |
| 301 | console.error(`Error fetching metadata for ${obj.object_id}:`, e); |
| 302 | } |
| 303 | |
| 304 | card.innerHTML = ` |
| 305 | <div class="object-name">${obj.object_id}</div> |
| 306 | <div class="object-meta"> |
| 307 | <span>📝 ${metadata.version_count || 0} versions</span> |
| 308 | <span>📊 ${metadata.log_count || 0} logs</span> |
| 309 | </div> |
| 310 | <div style="margin: 10px 0;"> |
| 311 | <span class="metric">Python Object</span> |
| 312 | ${obj.path ? '<span class="metric">' + obj.path + '</span>' : ''} |
| 313 | </div> |
| 314 | <div class="object-actions"> |
| 315 | <button class="btn btn-primary" onclick="viewObject('${obj.object_id}')"> |
| 316 | View Details |
| 317 | </button> |
| 318 | <button class="btn btn-secondary" onclick="viewLogs('${obj.object_id}')"> |
| 319 | Logs |
| 320 | </button> |
| 321 | <button class="btn btn-secondary" onclick="viewSource('${obj.object_id}')"> |
| 322 | Source |
| 323 | </button> |
| 324 | </div> |
| 325 | `; |
| 326 | |
| 327 | return card; |
| 328 | } |
| 329 | |
| 330 | function viewObject(id) { |
| 331 | window.location.href = `/dashboard/object/${id}`; |
| 332 | } |
| 333 | |
| 334 | function viewLogs(id) { |
| 335 | window.open(`/objects/${id}?logs=true`, '_blank'); |
| 336 | } |
| 337 | |
| 338 | function viewSource(id) { |
| 339 | window.open(`/objects/${id}?source=true`, '_blank'); |
| 340 | } |
| 341 | |
| 342 | // Auto-refresh every 5 seconds |
| 343 | setInterval(() => { |
| 344 | loadClusterStats(); |
| 345 | loadObjects(); |
| 346 | }, 5000); |
| 347 | |
| 348 | // Initial load |
| 349 | loadClusterStats(); |
| 350 | loadObjects(); |
| 351 | </script> |
| 352 | </body> |
| 353 | </html> |
| 354 | """ |
| 355 | |
| 356 | return html_response(html) |