askrobots / dbbasic-object-primitive
Public
Python functions as REST objects, with clustering, load balancing and state replication; no containers or orchestration.
[id].py · 19.5 KB · 514 lines
| 1 | """ |
| 2 | Object Inspector - Detailed view of a single object |
| 3 | |
| 4 | Shows: |
| 5 | - Source code with syntax highlighting |
| 6 | - Recent logs |
| 7 | - Performance metrics |
| 8 | - Version history |
| 9 | - Actions (execute, rollback) |
| 10 | |
| 11 | Access: http://localhost:8001/dashboard/object/{id} |
| 12 | """ |
| 13 | from dbbasic_web.responses import html as html_response |
| 14 | |
| 15 | |
| 16 | def GET(request, id=None): |
| 17 | """Render object inspector HTML""" |
| 18 | object_id = id or 'unknown' |
| 19 | |
| 20 | html = f""" |
| 21 | <!DOCTYPE html> |
| 22 | <html> |
| 23 | <head> |
| 24 | <title>{object_id} - Object Inspector</title> |
| 25 | <style> |
| 26 | * {{ margin: 0; padding: 0; box-sizing: border-box; }} |
| 27 | body {{ |
| 28 | font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif; |
| 29 | background: #0a0a0a; |
| 30 | color: #e0e0e0; |
| 31 | padding: 20px; |
| 32 | }} |
| 33 | .header {{ |
| 34 | background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); |
| 35 | padding: 20px 30px; |
| 36 | border-radius: 12px; |
| 37 | margin-bottom: 20px; |
| 38 | display: flex; |
| 39 | justify-content: space-between; |
| 40 | align-items: center; |
| 41 | }} |
| 42 | h1 {{ font-size: 28px; }} |
| 43 | .back-btn {{ |
| 44 | padding: 10px 20px; |
| 45 | background: rgba(255,255,255,0.2); |
| 46 | border: none; |
| 47 | border-radius: 6px; |
| 48 | color: white; |
| 49 | cursor: pointer; |
| 50 | text-decoration: none; |
| 51 | }} |
| 52 | .tabs {{ |
| 53 | display: flex; |
| 54 | gap: 10px; |
| 55 | margin-bottom: 20px; |
| 56 | }} |
| 57 | .tab {{ |
| 58 | padding: 12px 24px; |
| 59 | background: #1a1a1a; |
| 60 | border: 1px solid #333; |
| 61 | border-radius: 8px 8px 0 0; |
| 62 | cursor: pointer; |
| 63 | transition: all 0.2s; |
| 64 | }} |
| 65 | .tab.active {{ |
| 66 | background: #2a2a2a; |
| 67 | border-color: #667eea; |
| 68 | border-bottom-color: #2a2a2a; |
| 69 | }} |
| 70 | .content {{ |
| 71 | background: #2a2a2a; |
| 72 | padding: 30px; |
| 73 | border-radius: 0 8px 8px 8px; |
| 74 | border: 1px solid #333; |
| 75 | min-height: 500px; |
| 76 | }} |
| 77 | .tab-content {{ |
| 78 | display: none; |
| 79 | }} |
| 80 | .tab-content.active {{ |
| 81 | display: block; |
| 82 | }} |
| 83 | pre {{ |
| 84 | background: #1a1a1a; |
| 85 | padding: 20px; |
| 86 | border-radius: 8px; |
| 87 | overflow-x: auto; |
| 88 | font-family: 'Monaco', 'Menlo', monospace; |
| 89 | font-size: 14px; |
| 90 | line-height: 1.6; |
| 91 | }} |
| 92 | .log-entry {{ |
| 93 | padding: 12px; |
| 94 | margin-bottom: 8px; |
| 95 | background: #1a1a1a; |
| 96 | border-left: 4px solid #667eea; |
| 97 | border-radius: 4px; |
| 98 | font-family: monospace; |
| 99 | font-size: 13px; |
| 100 | }} |
| 101 | .log-entry.error {{ |
| 102 | border-left-color: #ef4444; |
| 103 | background: #2a1a1a; |
| 104 | }} |
| 105 | .log-entry.warn {{ |
| 106 | border-left-color: #fbbf24; |
| 107 | }} |
| 108 | .metric-grid {{ |
| 109 | display: grid; |
| 110 | grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); |
| 111 | gap: 20px; |
| 112 | margin-bottom: 20px; |
| 113 | }} |
| 114 | .metric-card {{ |
| 115 | background: #1a1a1a; |
| 116 | padding: 20px; |
| 117 | border-radius: 8px; |
| 118 | border: 1px solid #333; |
| 119 | }} |
| 120 | .metric-value {{ font-size: 32px; font-weight: bold; color: #667eea; }} |
| 121 | .metric-label {{ color: #999; margin-top: 5px; font-size: 14px; }} |
| 122 | .version-list {{ |
| 123 | list-style: none; |
| 124 | }} |
| 125 | .version-item {{ |
| 126 | padding: 15px; |
| 127 | background: #1a1a1a; |
| 128 | border-radius: 6px; |
| 129 | margin-bottom: 10px; |
| 130 | display: flex; |
| 131 | justify-content: space-between; |
| 132 | align-items: center; |
| 133 | }} |
| 134 | .version-info {{ |
| 135 | flex: 1; |
| 136 | }} |
| 137 | .version-number {{ |
| 138 | font-weight: bold; |
| 139 | color: #667eea; |
| 140 | }} |
| 141 | .version-date {{ |
| 142 | color: #999; |
| 143 | font-size: 12px; |
| 144 | }} |
| 145 | .btn {{ |
| 146 | padding: 8px 16px; |
| 147 | border-radius: 4px; |
| 148 | border: none; |
| 149 | cursor: pointer; |
| 150 | font-size: 14px; |
| 151 | transition: all 0.2s; |
| 152 | }} |
| 153 | .btn-primary {{ |
| 154 | background: #667eea; |
| 155 | color: white; |
| 156 | }} |
| 157 | .btn-primary:hover {{ |
| 158 | background: #5568d3; |
| 159 | }} |
| 160 | .btn-danger {{ |
| 161 | background: #ef4444; |
| 162 | color: white; |
| 163 | }} |
| 164 | .btn-danger:hover {{ |
| 165 | background: #dc2626; |
| 166 | }} |
| 167 | .loading {{ |
| 168 | text-align: center; |
| 169 | padding: 40px; |
| 170 | color: #999; |
| 171 | }} |
| 172 | .chart-container {{ |
| 173 | height: 300px; |
| 174 | background: #1a1a1a; |
| 175 | border-radius: 8px; |
| 176 | padding: 20px; |
| 177 | margin-top: 20px; |
| 178 | }} |
| 179 | </style> |
| 180 | </head> |
| 181 | <body> |
| 182 | <div class="header"> |
| 183 | <h1>🔍 {object_id}</h1> |
| 184 | <a href="/dashboard" class="back-btn">← Back to Dashboard</a> |
| 185 | </div> |
| 186 | |
| 187 | <div class="tabs"> |
| 188 | <div class="tab active" onclick="switchTab('overview')">Overview</div> |
| 189 | <div class="tab" onclick="switchTab('source')">Source Code</div> |
| 190 | <div class="tab" onclick="switchTab('logs')">Logs</div> |
| 191 | <div class="tab" onclick="switchTab('metrics')">Metrics</div> |
| 192 | <div class="tab" onclick="switchTab('versions')">Versions</div> |
| 193 | </div> |
| 194 | |
| 195 | <div class="content"> |
| 196 | <div id="overview" class="tab-content active"> |
| 197 | <h2 style="margin-bottom: 20px;">Object Overview</h2> |
| 198 | <div class="metric-grid"> |
| 199 | <div class="metric-card"> |
| 200 | <div class="metric-value" id="version-count">-</div> |
| 201 | <div class="metric-label">Total Versions</div> |
| 202 | </div> |
| 203 | <div class="metric-card"> |
| 204 | <div class="metric-value" id="log-count">-</div> |
| 205 | <div class="metric-label">Total Logs</div> |
| 206 | </div> |
| 207 | <div class="metric-card"> |
| 208 | <div class="metric-value" id="current-version">-</div> |
| 209 | <div class="metric-label">Current Version</div> |
| 210 | </div> |
| 211 | <div class="metric-card"> |
| 212 | <div class="metric-value" id="last-modified">-</div> |
| 213 | <div class="metric-label">Last Modified</div> |
| 214 | </div> |
| 215 | </div> |
| 216 | <div style="margin-top: 30px;"> |
| 217 | <h3 style="margin-bottom: 15px;">Quick Actions</h3> |
| 218 | <button class="btn btn-primary" onclick="executeObject()">Execute (GET)</button> |
| 219 | <button class="btn btn-primary" onclick="viewRawJSON()">View Raw JSON</button> |
| 220 | </div> |
| 221 | </div> |
| 222 | |
| 223 | <div id="source" class="tab-content"> |
| 224 | <h2 style="margin-bottom: 20px;">Source Code</h2> |
| 225 | <pre id="source-code"><div class="loading">Loading source...</div></pre> |
| 226 | </div> |
| 227 | |
| 228 | <div id="logs" class="tab-content"> |
| 229 | <h2 style="margin-bottom: 20px;">Recent Logs</h2> |
| 230 | <div id="logs-container"> |
| 231 | <div class="loading">Loading logs...</div> |
| 232 | </div> |
| 233 | </div> |
| 234 | |
| 235 | <div id="metrics" class="tab-content"> |
| 236 | <h2 style="margin-bottom: 20px;">Performance Metrics</h2> |
| 237 | <p style="color: #999; margin-bottom: 20px;"> |
| 238 | Extracted from object logs - useful for AI scheduling and debugging |
| 239 | </p> |
| 240 | <div class="metric-grid"> |
| 241 | <div class="metric-card"> |
| 242 | <div class="metric-value" id="total-executions">-</div> |
| 243 | <div class="metric-label">Total Executions</div> |
| 244 | </div> |
| 245 | <div class="metric-card"> |
| 246 | <div class="metric-value" id="error-rate">-</div> |
| 247 | <div class="metric-label">Error Rate</div> |
| 248 | </div> |
| 249 | <div class="metric-card"> |
| 250 | <div class="metric-value" id="avg-exec-time">-</div> |
| 251 | <div class="metric-label">Avg Execution (ms)</div> |
| 252 | </div> |
| 253 | <div class="metric-card"> |
| 254 | <div class="metric-value" id="last-execution">-</div> |
| 255 | <div class="metric-label">Last Execution</div> |
| 256 | </div> |
| 257 | </div> |
| 258 | <div style="margin-top: 30px;"> |
| 259 | <h3 style="margin-bottom: 15px;">Recent Performance</h3> |
| 260 | <div id="recent-performance" style="background: #1a1a1a; padding: 20px; border-radius: 8px;"> |
| 261 | <div class="loading">Loading performance data...</div> |
| 262 | </div> |
| 263 | </div> |
| 264 | </div> |
| 265 | |
| 266 | <div id="versions" class="tab-content"> |
| 267 | <h2 style="margin-bottom: 20px;">Version History</h2> |
| 268 | <ul id="versions-list" class="version-list"> |
| 269 | <li class="loading">Loading versions...</li> |
| 270 | </ul> |
| 271 | </div> |
| 272 | </div> |
| 273 | |
| 274 | <script> |
| 275 | const objectId = '{object_id}'; |
| 276 | |
| 277 | function switchTab(tabName) {{ |
| 278 | // Update tab buttons |
| 279 | document.querySelectorAll('.tab').forEach(tab => {{ |
| 280 | tab.classList.remove('active'); |
| 281 | }}); |
| 282 | event.target.classList.add('active'); |
| 283 | |
| 284 | // Update tab content |
| 285 | document.querySelectorAll('.tab-content').forEach(content => {{ |
| 286 | content.classList.remove('active'); |
| 287 | }}); |
| 288 | document.getElementById(tabName).classList.add('active'); |
| 289 | |
| 290 | // Load content if needed |
| 291 | if (tabName === 'source' && !window.sourceLoaded) {{ |
| 292 | loadSource(); |
| 293 | window.sourceLoaded = true; |
| 294 | }} |
| 295 | if (tabName === 'logs' && !window.logsLoaded) {{ |
| 296 | loadLogs(); |
| 297 | window.logsLoaded = true; |
| 298 | }} |
| 299 | if (tabName === 'metrics' && !window.metricsLoaded) {{ |
| 300 | loadMetrics(); |
| 301 | window.metricsLoaded = true; |
| 302 | }} |
| 303 | if (tabName === 'versions' && !window.versionsLoaded) {{ |
| 304 | loadVersions(); |
| 305 | window.versionsLoaded = true; |
| 306 | }} |
| 307 | }} |
| 308 | |
| 309 | async function loadMetadata() {{ |
| 310 | try {{ |
| 311 | const response = await fetch(`/objects/${{objectId}}?metadata=true`); |
| 312 | const data = await response.json(); |
| 313 | |
| 314 | document.getElementById('version-count').textContent = data.version_count || 0; |
| 315 | document.getElementById('log-count').textContent = data.log_count || 0; |
| 316 | document.getElementById('current-version').textContent = data.current_version || 1; |
| 317 | document.getElementById('last-modified').textContent = |
| 318 | data.last_modified ? new Date(data.last_modified * 1000).toLocaleString() : 'Unknown'; |
| 319 | }} catch (e) {{ |
| 320 | console.error('Error loading metadata:', e); |
| 321 | }} |
| 322 | }} |
| 323 | |
| 324 | async function loadSource() {{ |
| 325 | try {{ |
| 326 | const response = await fetch(`/objects/${{objectId}}?source=true`); |
| 327 | const data = await response.json(); |
| 328 | const source = data.source || 'No source found'; |
| 329 | document.getElementById('source-code').textContent = source; |
| 330 | }} catch (e) {{ |
| 331 | document.getElementById('source-code').textContent = 'Error loading source: ' + e.message; |
| 332 | }} |
| 333 | }} |
| 334 | |
| 335 | async function loadLogs() {{ |
| 336 | try {{ |
| 337 | const response = await fetch(`/objects/${{objectId}}?logs=true&limit=50`); |
| 338 | const data = await response.json(); |
| 339 | |
| 340 | const container = document.getElementById('logs-container'); |
| 341 | container.innerHTML = ''; |
| 342 | |
| 343 | if (!data.logs || data.logs.length === 0) {{ |
| 344 | container.innerHTML = '<p style="color: #999;">No logs found</p>'; |
| 345 | return; |
| 346 | }} |
| 347 | |
| 348 | data.logs.forEach(log => {{ |
| 349 | const entry = document.createElement('div'); |
| 350 | entry.className = 'log-entry'; |
| 351 | if (log.level === 'ERROR') entry.classList.add('error'); |
| 352 | if (log.level === 'WARN') entry.classList.add('warn'); |
| 353 | |
| 354 | const timestamp = new Date(log.timestamp * 1000).toLocaleString(); |
| 355 | entry.innerHTML = ` |
| 356 | <strong>${{log.level || 'INFO'}}</strong> ${{timestamp}}<br> |
| 357 | ${{log.message || JSON.stringify(log)}} |
| 358 | `; |
| 359 | container.appendChild(entry); |
| 360 | }}); |
| 361 | }} catch (e) {{ |
| 362 | document.getElementById('logs-container').innerHTML = |
| 363 | '<p style="color: #ef4444;">Error loading logs: ' + e.message + '</p>'; |
| 364 | }} |
| 365 | }} |
| 366 | |
| 367 | async function loadVersions() {{ |
| 368 | try {{ |
| 369 | const response = await fetch(`/objects/${{objectId}}?versions=true`); |
| 370 | const data = await response.json(); |
| 371 | |
| 372 | const list = document.getElementById('versions-list'); |
| 373 | list.innerHTML = ''; |
| 374 | |
| 375 | if (!data.versions || data.versions.length === 0) {{ |
| 376 | list.innerHTML = '<li style="color: #999;">No versions found</li>'; |
| 377 | return; |
| 378 | }} |
| 379 | |
| 380 | data.versions.forEach(version => {{ |
| 381 | const item = document.createElement('li'); |
| 382 | item.className = 'version-item'; |
| 383 | item.innerHTML = ` |
| 384 | <div class="version-info"> |
| 385 | <div class="version-number">Version ${{version.version}}</div> |
| 386 | <div class="version-date">${{new Date(version.timestamp * 1000).toLocaleString()}}</div> |
| 387 | </div> |
| 388 | <button class="btn btn-primary" onclick="rollbackTo(${{version.version}})"> |
| 389 | Rollback |
| 390 | </button> |
| 391 | `; |
| 392 | list.appendChild(item); |
| 393 | }}); |
| 394 | }} catch (e) {{ |
| 395 | document.getElementById('versions-list').innerHTML = |
| 396 | '<li style="color: #ef4444;">Error loading versions: ' + e.message + '</li>'; |
| 397 | }} |
| 398 | }} |
| 399 | |
| 400 | async function loadMetrics() {{ |
| 401 | try {{ |
| 402 | const response = await fetch(`/objects/${{objectId}}?logs=true&limit=1000`); |
| 403 | const data = await response.json(); |
| 404 | |
| 405 | if (!data.logs || data.logs.length === 0) {{ |
| 406 | document.getElementById('total-executions').textContent = '0'; |
| 407 | document.getElementById('error-rate').textContent = '0%'; |
| 408 | document.getElementById('avg-exec-time').textContent = 'N/A'; |
| 409 | document.getElementById('last-execution').textContent = 'Never'; |
| 410 | document.getElementById('recent-performance').innerHTML = |
| 411 | '<p style="color: #999;">No execution data available</p>'; |
| 412 | return; |
| 413 | }} |
| 414 | |
| 415 | const logs = data.logs; |
| 416 | const total = logs.length; |
| 417 | const errors = logs.filter(log => log.level === 'ERROR').length; |
| 418 | const errorRate = total > 0 ? ((errors / total) * 100).toFixed(1) : 0; |
| 419 | |
| 420 | // Extract execution times from log messages (if present) |
| 421 | const executionTimes = []; |
| 422 | logs.forEach(log => {{ |
| 423 | const match = log.message?.match(/(\d+(?:\.\d+)?)\s*ms/); |
| 424 | if (match) {{ |
| 425 | executionTimes.push(parseFloat(match[1])); |
| 426 | }} |
| 427 | }}); |
| 428 | |
| 429 | const avgTime = executionTimes.length > 0 |
| 430 | ? (executionTimes.reduce((a, b) => a + b, 0) / executionTimes.length).toFixed(2) |
| 431 | : 'N/A'; |
| 432 | |
| 433 | // Last execution |
| 434 | const lastLog = logs[0]; // logs are sorted newest first |
| 435 | const lastExecution = lastLog ? new Date(lastLog.timestamp * 1000).toLocaleString() : 'Never'; |
| 436 | |
| 437 | // Update metrics display |
| 438 | document.getElementById('total-executions').textContent = total; |
| 439 | document.getElementById('error-rate').textContent = errorRate + '%'; |
| 440 | document.getElementById('avg-exec-time').textContent = avgTime; |
| 441 | document.getElementById('last-execution').textContent = lastExecution; |
| 442 | |
| 443 | // Build performance trend chart (last 10 executions with timing data) |
| 444 | const recentPerf = executionTimes.slice(0, 10).reverse(); |
| 445 | let perfHTML = ''; |
| 446 | |
| 447 | if (recentPerf.length > 0) {{ |
| 448 | const maxTime = Math.max(...recentPerf); |
| 449 | perfHTML = '<div style="display: flex; gap: 10px; align-items: flex-end; height: 150px;">'; |
| 450 | recentPerf.forEach((time, i) => {{ |
| 451 | const height = (time / maxTime) * 100; |
| 452 | const color = time > avgTime * 1.5 ? '#ef4444' : (time > avgTime ? '#fbbf24' : '#4ade80'); |
| 453 | perfHTML += ` |
| 454 | <div style="flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: flex-end;"> |
| 455 | <div style="font-size: 10px; color: #999; margin-bottom: 4px;">${{time.toFixed(1)}}ms</div> |
| 456 | <div style="width: 100%; background: ${{color}}; height: ${{height}}%; min-height: 5px; border-radius: 4px 4px 0 0;"></div> |
| 457 | </div> |
| 458 | `; |
| 459 | }}); |
| 460 | perfHTML += '</div>'; |
| 461 | perfHTML += '<div style="margin-top: 10px; font-size: 12px; color: #999;">Last 10 executions (green=fast, yellow=avg, red=slow)</div>'; |
| 462 | }} else {{ |
| 463 | perfHTML = '<p style="color: #999;">No execution time data found in logs</p>'; |
| 464 | perfHTML += '<p style="color: #666; font-size: 12px; margin-top: 10px;">Tip: Log execution times like "Completed in 42.5ms" to see performance trends</p>'; |
| 465 | }} |
| 466 | |
| 467 | document.getElementById('recent-performance').innerHTML = perfHTML; |
| 468 | |
| 469 | }} catch (e) {{ |
| 470 | console.error('Error loading metrics:', e); |
| 471 | document.getElementById('recent-performance').innerHTML = |
| 472 | '<p style="color: #ef4444;">Error loading metrics: ' + e.message + '</p>'; |
| 473 | }} |
| 474 | }} |
| 475 | |
| 476 | async function executeObject() {{ |
| 477 | try {{ |
| 478 | const response = await fetch(`/objects/${{objectId}}`); |
| 479 | const result = await response.json(); |
| 480 | alert('Execution result:\\n' + JSON.stringify(result, null, 2)); |
| 481 | }} catch (e) {{ |
| 482 | alert('Error executing object: ' + e.message); |
| 483 | }} |
| 484 | }} |
| 485 | |
| 486 | function viewRawJSON() {{ |
| 487 | window.open(`/objects/${{objectId}}?metadata=true`, '_blank'); |
| 488 | }} |
| 489 | |
| 490 | async function rollbackTo(version) {{ |
| 491 | if (!confirm(`Rollback to version ${{version}}?`)) return; |
| 492 | |
| 493 | try {{ |
| 494 | const response = await fetch(`/objects/${{objectId}}`, {{ |
| 495 | method: 'POST', |
| 496 | headers: {{ 'Content-Type': 'application/json' }}, |
| 497 | body: JSON.stringify({{ action: 'rollback', to: version }}) |
| 498 | }}); |
| 499 | const result = await response.json(); |
| 500 | alert('Rollback successful!'); |
| 501 | location.reload(); |
| 502 | }} catch (e) {{ |
| 503 | alert('Error rolling back: ' + e.message); |
| 504 | }} |
| 505 | }} |
| 506 | |
| 507 | // Initial load |
| 508 | loadMetadata(); |
| 509 | </script> |
| 510 | </body> |
| 511 | </html> |
| 512 | """ |
| 513 | |
| 514 | return html_response(html) |