askrobots / dbbasic-object-primitive
Public
Python functions as REST objects, with clustering, load balancing and state replication; no containers or orchestration.
[id].py · 29.5 KB · 735 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('state')">State</div> |
| 192 | <div class="tab" onclick="switchTab('files')">Files</div> |
| 193 | <div class="tab" onclick="switchTab('metrics')">Metrics</div> |
| 194 | <div class="tab" onclick="switchTab('versions')">Versions</div> |
| 195 | </div> |
| 196 | |
| 197 | <div class="content"> |
| 198 | <div id="overview" class="tab-content active"> |
| 199 | <h2 style="margin-bottom: 20px;">Object Overview</h2> |
| 200 | <div class="metric-grid"> |
| 201 | <div class="metric-card"> |
| 202 | <div class="metric-value" id="version-count">-</div> |
| 203 | <div class="metric-label">Total Versions</div> |
| 204 | </div> |
| 205 | <div class="metric-card"> |
| 206 | <div class="metric-value" id="log-count">-</div> |
| 207 | <div class="metric-label">Total Logs</div> |
| 208 | </div> |
| 209 | <div class="metric-card"> |
| 210 | <div class="metric-value" id="current-version">-</div> |
| 211 | <div class="metric-label">Current Version</div> |
| 212 | </div> |
| 213 | <div class="metric-card"> |
| 214 | <div class="metric-value" id="last-modified">-</div> |
| 215 | <div class="metric-label">Last Modified</div> |
| 216 | </div> |
| 217 | </div> |
| 218 | <div style="margin-top: 30px;"> |
| 219 | <h3 style="margin-bottom: 15px;">Quick Actions</h3> |
| 220 | <button class="btn btn-primary" onclick="executeObject()">Execute (GET)</button> |
| 221 | <button class="btn btn-primary" onclick="viewRawJSON()">View Raw JSON</button> |
| 222 | </div> |
| 223 | </div> |
| 224 | |
| 225 | <div id="source" class="tab-content"> |
| 226 | <h2 style="margin-bottom: 20px;">Source Code</h2> |
| 227 | <pre id="source-code"><div class="loading">Loading source...</div></pre> |
| 228 | </div> |
| 229 | |
| 230 | <div id="logs" class="tab-content"> |
| 231 | <h2 style="margin-bottom: 20px;">Recent Logs</h2> |
| 232 | <div id="logs-container"> |
| 233 | <div class="loading">Loading logs...</div> |
| 234 | </div> |
| 235 | </div> |
| 236 | |
| 237 | <div id="state" class="tab-content"> |
| 238 | <h2 style="margin-bottom: 20px;">Object State</h2> |
| 239 | <p style="color: #999; margin-bottom: 20px;"> |
| 240 | Current in-memory state of this object |
| 241 | </p> |
| 242 | <div id="state-container" style="background: #1a1a1a; padding: 20px; border-radius: 8px;"> |
| 243 | <div class="loading">Loading state...</div> |
| 244 | </div> |
| 245 | </div> |
| 246 | |
| 247 | <div id="files" class="tab-content"> |
| 248 | <h2 style="margin-bottom: 20px;">Uploaded Files</h2> |
| 249 | <p style="color: #999; margin-bottom: 20px;"> |
| 250 | Files uploaded to this object (images, documents, etc.) |
| 251 | </p> |
| 252 | <div id="files-container" style="display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); gap: 15px;"> |
| 253 | <div class="loading">Loading files...</div> |
| 254 | </div> |
| 255 | </div> |
| 256 | |
| 257 | <div id="metrics" class="tab-content"> |
| 258 | <h2 style="margin-bottom: 20px;">Performance Metrics</h2> |
| 259 | <p style="color: #999; margin-bottom: 20px;"> |
| 260 | Extracted from object logs - useful for AI scheduling and debugging |
| 261 | </p> |
| 262 | <div class="metric-grid"> |
| 263 | <div class="metric-card"> |
| 264 | <div class="metric-value" id="total-executions">-</div> |
| 265 | <div class="metric-label">Total Executions</div> |
| 266 | </div> |
| 267 | <div class="metric-card"> |
| 268 | <div class="metric-value" id="error-rate">-</div> |
| 269 | <div class="metric-label">Error Rate</div> |
| 270 | </div> |
| 271 | <div class="metric-card"> |
| 272 | <div class="metric-value" id="avg-exec-time">-</div> |
| 273 | <div class="metric-label">Avg Execution (ms)</div> |
| 274 | </div> |
| 275 | <div class="metric-card"> |
| 276 | <div class="metric-value" id="last-execution">-</div> |
| 277 | <div class="metric-label">Last Execution</div> |
| 278 | </div> |
| 279 | </div> |
| 280 | <div style="margin-top: 30px;"> |
| 281 | <h3 style="margin-bottom: 15px;">Recent Performance</h3> |
| 282 | <div id="recent-performance" style="background: #1a1a1a; padding: 20px; border-radius: 8px;"> |
| 283 | <div class="loading">Loading performance data...</div> |
| 284 | </div> |
| 285 | </div> |
| 286 | </div> |
| 287 | |
| 288 | <div id="versions" class="tab-content"> |
| 289 | <h2 style="margin-bottom: 20px;">Version History</h2> |
| 290 | <ul id="versions-list" class="version-list"> |
| 291 | <li class="loading">Loading versions...</li> |
| 292 | </ul> |
| 293 | </div> |
| 294 | </div> |
| 295 | |
| 296 | <script> |
| 297 | const objectId = '{object_id}'; |
| 298 | |
| 299 | function switchTab(tabName) {{ |
| 300 | // Update tab buttons |
| 301 | document.querySelectorAll('.tab').forEach(tab => {{ |
| 302 | tab.classList.remove('active'); |
| 303 | }}); |
| 304 | event.target.classList.add('active'); |
| 305 | |
| 306 | // Update tab content |
| 307 | document.querySelectorAll('.tab-content').forEach(content => {{ |
| 308 | content.classList.remove('active'); |
| 309 | }}); |
| 310 | document.getElementById(tabName).classList.add('active'); |
| 311 | |
| 312 | // Load content if needed |
| 313 | if (tabName === 'source' && !window.sourceLoaded) {{ |
| 314 | loadSource(); |
| 315 | window.sourceLoaded = true; |
| 316 | }} |
| 317 | if (tabName === 'logs' && !window.logsLoaded) {{ |
| 318 | loadLogs(); |
| 319 | window.logsLoaded = true; |
| 320 | }} |
| 321 | if (tabName === 'state' && !window.stateLoaded) {{ |
| 322 | loadState(); |
| 323 | window.stateLoaded = true; |
| 324 | }} |
| 325 | if (tabName === 'files' && !window.filesLoaded) {{ |
| 326 | loadFiles(); |
| 327 | window.filesLoaded = true; |
| 328 | }} |
| 329 | if (tabName === 'metrics' && !window.metricsLoaded) {{ |
| 330 | loadMetrics(); |
| 331 | window.metricsLoaded = true; |
| 332 | }} |
| 333 | if (tabName === 'versions' && !window.versionsLoaded) {{ |
| 334 | loadVersions(); |
| 335 | window.versionsLoaded = true; |
| 336 | }} |
| 337 | }} |
| 338 | |
| 339 | async function loadMetadata() {{ |
| 340 | try {{ |
| 341 | const response = await fetch(`/objects/${{objectId}}?metadata=true`); |
| 342 | const data = await response.json(); |
| 343 | |
| 344 | document.getElementById('version-count').textContent = data.version_count || 0; |
| 345 | document.getElementById('log-count').textContent = data.log_count || 0; |
| 346 | document.getElementById('current-version').textContent = data.current_version || 1; |
| 347 | document.getElementById('last-modified').textContent = |
| 348 | data.last_modified ? new Date(data.last_modified * 1000).toLocaleString() : 'Unknown'; |
| 349 | }} catch (e) {{ |
| 350 | console.error('Error loading metadata:', e); |
| 351 | }} |
| 352 | }} |
| 353 | |
| 354 | async function loadSource() {{ |
| 355 | try {{ |
| 356 | const response = await fetch(`/objects/${{objectId}}?source=true`); |
| 357 | const data = await response.json(); |
| 358 | const source = data.source || 'No source found'; |
| 359 | document.getElementById('source-code').textContent = source; |
| 360 | }} catch (e) {{ |
| 361 | document.getElementById('source-code').textContent = 'Error loading source: ' + e.message; |
| 362 | }} |
| 363 | }} |
| 364 | |
| 365 | async function loadLogs() {{ |
| 366 | try {{ |
| 367 | const response = await fetch(`/objects/${{objectId}}?logs=true&limit=50`); |
| 368 | const data = await response.json(); |
| 369 | |
| 370 | const container = document.getElementById('logs-container'); |
| 371 | container.innerHTML = ''; |
| 372 | |
| 373 | if (!data.logs || data.logs.length === 0) {{ |
| 374 | container.innerHTML = '<p style="color: #999;">No logs found</p>'; |
| 375 | return; |
| 376 | }} |
| 377 | |
| 378 | data.logs.forEach(log => {{ |
| 379 | const entry = document.createElement('div'); |
| 380 | entry.className = 'log-entry'; |
| 381 | if (log.level === 'ERROR') entry.classList.add('error'); |
| 382 | if (log.level === 'WARN') entry.classList.add('warn'); |
| 383 | |
| 384 | const timestamp = new Date(log.timestamp * 1000).toLocaleString(); |
| 385 | entry.innerHTML = ` |
| 386 | <strong>${{log.level || 'INFO'}}</strong> ${{timestamp}}<br> |
| 387 | ${{log.message || JSON.stringify(log)}} |
| 388 | `; |
| 389 | container.appendChild(entry); |
| 390 | }}); |
| 391 | }} catch (e) {{ |
| 392 | document.getElementById('logs-container').innerHTML = |
| 393 | '<p style="color: #ef4444;">Error loading logs: ' + e.message + '</p>'; |
| 394 | }} |
| 395 | }} |
| 396 | |
| 397 | async function loadState() {{ |
| 398 | try {{ |
| 399 | const container = document.getElementById('state-container'); |
| 400 | container.innerHTML = '<p style="color: #999;">Loading state from all stations...</p>'; |
| 401 | |
| 402 | // Get list of active stations |
| 403 | const stationsResp = await fetch('/cluster/stations'); |
| 404 | const stationsData = await stationsResp.json(); |
| 405 | |
| 406 | if (!stationsData.stations || stationsData.stations.length === 0) {{ |
| 407 | container.innerHTML = '<p style="color: #999;">No active stations found</p>'; |
| 408 | return; |
| 409 | }} |
| 410 | |
| 411 | // Query each active station for state |
| 412 | const statePromises = stationsData.stations |
| 413 | .filter(s => s.is_active) |
| 414 | .map(async station => {{ |
| 415 | try {{ |
| 416 | const resp = await fetch(`/objects/${{objectId}}@${{station.station_id}}?state=true`); |
| 417 | const data = await resp.json(); |
| 418 | return {{ |
| 419 | station_id: station.station_id, |
| 420 | state: data.state || {{}}, |
| 421 | error: null |
| 422 | }}; |
| 423 | }} catch (e) {{ |
| 424 | return {{ |
| 425 | station_id: station.station_id, |
| 426 | state: null, |
| 427 | error: e.message |
| 428 | }}; |
| 429 | }} |
| 430 | }}); |
| 431 | |
| 432 | const states = await Promise.all(statePromises); |
| 433 | |
| 434 | // Check for consistency |
| 435 | const stateStrings = states |
| 436 | .filter(s => s.state !== null) |
| 437 | .map(s => JSON.stringify(s.state)); |
| 438 | const isConsistent = stateStrings.length > 0 && |
| 439 | stateStrings.every(s => s === stateStrings[0]); |
| 440 | |
| 441 | // Build display |
| 442 | container.innerHTML = ''; |
| 443 | |
| 444 | // Consistency indicator |
| 445 | const statusDiv = document.createElement('div'); |
| 446 | statusDiv.style.marginBottom = '20px'; |
| 447 | statusDiv.style.padding = '10px'; |
| 448 | statusDiv.style.borderRadius = '5px'; |
| 449 | statusDiv.style.background = isConsistent ? '#064e3b' : '#7c2d12'; |
| 450 | statusDiv.innerHTML = isConsistent |
| 451 | ? '✓ State is consistent across all stations' |
| 452 | : '⚠️ State inconsistency detected'; |
| 453 | container.appendChild(statusDiv); |
| 454 | |
| 455 | // Display each station's state |
| 456 | states.forEach(s => {{ |
| 457 | const stationDiv = document.createElement('div'); |
| 458 | stationDiv.style.marginBottom = '15px'; |
| 459 | stationDiv.style.padding = '15px'; |
| 460 | stationDiv.style.background = '#0a0a0a'; |
| 461 | stationDiv.style.borderRadius = '5px'; |
| 462 | stationDiv.style.border = '1px solid #333'; |
| 463 | |
| 464 | const header = document.createElement('div'); |
| 465 | header.style.marginBottom = '10px'; |
| 466 | header.style.fontWeight = 'bold'; |
| 467 | header.style.color = s.error ? '#ef4444' : '#10b981'; |
| 468 | header.textContent = s.station_id; |
| 469 | stationDiv.appendChild(header); |
| 470 | |
| 471 | if (s.error) {{ |
| 472 | const errorP = document.createElement('p'); |
| 473 | errorP.style.color = '#ef4444'; |
| 474 | errorP.textContent = `Error: ${{s.error}}`; |
| 475 | stationDiv.appendChild(errorP); |
| 476 | }} else if (!s.state || Object.keys(s.state).length === 0) {{ |
| 477 | const emptyP = document.createElement('p'); |
| 478 | emptyP.style.color = '#999'; |
| 479 | emptyP.textContent = 'No state'; |
| 480 | stationDiv.appendChild(emptyP); |
| 481 | }} else {{ |
| 482 | const pre = document.createElement('pre'); |
| 483 | pre.style.margin = '0'; |
| 484 | pre.style.whiteSpace = 'pre-wrap'; |
| 485 | pre.style.color = '#d4d4d4'; |
| 486 | pre.textContent = JSON.stringify(s.state, null, 2); |
| 487 | stationDiv.appendChild(pre); |
| 488 | }} |
| 489 | |
| 490 | container.appendChild(stationDiv); |
| 491 | }}); |
| 492 | }} catch (e) {{ |
| 493 | document.getElementById('state-container').innerHTML = |
| 494 | '<p style="color: #ef4444;">Error loading state: ' + e.message + '</p>'; |
| 495 | }} |
| 496 | }} |
| 497 | |
| 498 | async function loadFiles() {{ |
| 499 | try {{ |
| 500 | const response = await fetch(`/objects/${{objectId}}?files=true`); |
| 501 | const data = await response.json(); |
| 502 | |
| 503 | const container = document.getElementById('files-container'); |
| 504 | container.innerHTML = ''; |
| 505 | |
| 506 | if (!data.files || data.files.length === 0) {{ |
| 507 | container.innerHTML = '<p style="color: #999; grid-column: 1/-1;">No files uploaded yet</p>'; |
| 508 | return; |
| 509 | }} |
| 510 | |
| 511 | // Display each file |
| 512 | data.files.forEach(file => {{ |
| 513 | const fileCard = document.createElement('div'); |
| 514 | fileCard.style.cssText = ` |
| 515 | background: #1a1a1a; |
| 516 | border-radius: 8px; |
| 517 | padding: 15px; |
| 518 | border: 1px solid #333; |
| 519 | display: flex; |
| 520 | flex-direction: column; |
| 521 | gap: 10px; |
| 522 | `; |
| 523 | |
| 524 | const filename = file.filename || file; |
| 525 | const isImage = /\.(jpg|jpeg|png|gif|webp|svg)$/i.test(filename); |
| 526 | |
| 527 | // Image preview or file icon |
| 528 | if (isImage) {{ |
| 529 | const img = document.createElement('img'); |
| 530 | img.src = `/objects/${{objectId}}?file=${{encodeURIComponent(filename)}}`; |
| 531 | img.style.cssText = ` |
| 532 | width: 100%; |
| 533 | height: 150px; |
| 534 | object-fit: cover; |
| 535 | border-radius: 6px; |
| 536 | cursor: pointer; |
| 537 | background: #0a0a0a; |
| 538 | `; |
| 539 | img.onclick = () => window.open(img.src, '_blank'); |
| 540 | fileCard.appendChild(img); |
| 541 | }} else {{ |
| 542 | const icon = document.createElement('div'); |
| 543 | icon.style.cssText = ` |
| 544 | width: 100%; |
| 545 | height: 150px; |
| 546 | display: flex; |
| 547 | align-items: center; |
| 548 | justify-content: center; |
| 549 | background: #0a0a0a; |
| 550 | border-radius: 6px; |
| 551 | font-size: 48px; |
| 552 | `; |
| 553 | icon.textContent = '📄'; |
| 554 | fileCard.appendChild(icon); |
| 555 | }} |
| 556 | |
| 557 | // Filename |
| 558 | const nameDiv = document.createElement('div'); |
| 559 | nameDiv.style.cssText = ` |
| 560 | color: #e0e0e0; |
| 561 | font-size: 14px; |
| 562 | word-break: break-word; |
| 563 | `; |
| 564 | nameDiv.textContent = filename; |
| 565 | fileCard.appendChild(nameDiv); |
| 566 | |
| 567 | // Actions |
| 568 | const actionsDiv = document.createElement('div'); |
| 569 | actionsDiv.style.cssText = 'display: flex; gap: 5px;'; |
| 570 | |
| 571 | const viewBtn = document.createElement('button'); |
| 572 | viewBtn.className = 'btn btn-primary'; |
| 573 | viewBtn.style.cssText = 'flex: 1; font-size: 12px; padding: 6px;'; |
| 574 | viewBtn.textContent = isImage ? 'View' : 'Download'; |
| 575 | viewBtn.onclick = () => window.open(`/objects/${{objectId}}?file=${{encodeURIComponent(filename)}}`, '_blank'); |
| 576 | actionsDiv.appendChild(viewBtn); |
| 577 | |
| 578 | fileCard.appendChild(actionsDiv); |
| 579 | container.appendChild(fileCard); |
| 580 | }}); |
| 581 | |
| 582 | }} catch (e) {{ |
| 583 | document.getElementById('files-container').innerHTML = |
| 584 | '<p style="color: #ef4444; grid-column: 1/-1;">Error loading files: ' + e.message + '</p>'; |
| 585 | }} |
| 586 | }} |
| 587 | |
| 588 | async function loadVersions() {{ |
| 589 | try {{ |
| 590 | const response = await fetch(`/objects/${{objectId}}?versions=true`); |
| 591 | const data = await response.json(); |
| 592 | |
| 593 | const list = document.getElementById('versions-list'); |
| 594 | list.innerHTML = ''; |
| 595 | |
| 596 | if (!data.versions || data.versions.length === 0) {{ |
| 597 | list.innerHTML = '<li style="color: #999;">No versions found</li>'; |
| 598 | return; |
| 599 | }} |
| 600 | |
| 601 | data.versions.forEach(version => {{ |
| 602 | const item = document.createElement('li'); |
| 603 | item.className = 'version-item'; |
| 604 | item.innerHTML = ` |
| 605 | <div class="version-info"> |
| 606 | <div class="version-number">Version ${{version.version}}</div> |
| 607 | <div class="version-date">${{new Date(version.timestamp * 1000).toLocaleString()}}</div> |
| 608 | </div> |
| 609 | <button class="btn btn-primary" onclick="rollbackTo(${{version.version}})"> |
| 610 | Rollback |
| 611 | </button> |
| 612 | `; |
| 613 | list.appendChild(item); |
| 614 | }}); |
| 615 | }} catch (e) {{ |
| 616 | document.getElementById('versions-list').innerHTML = |
| 617 | '<li style="color: #ef4444;">Error loading versions: ' + e.message + '</li>'; |
| 618 | }} |
| 619 | }} |
| 620 | |
| 621 | async function loadMetrics() {{ |
| 622 | try {{ |
| 623 | const response = await fetch(`/objects/${{objectId}}?logs=true&limit=1000`); |
| 624 | const data = await response.json(); |
| 625 | |
| 626 | if (!data.logs || data.logs.length === 0) {{ |
| 627 | document.getElementById('total-executions').textContent = '0'; |
| 628 | document.getElementById('error-rate').textContent = '0%'; |
| 629 | document.getElementById('avg-exec-time').textContent = 'N/A'; |
| 630 | document.getElementById('last-execution').textContent = 'Never'; |
| 631 | document.getElementById('recent-performance').innerHTML = |
| 632 | '<p style="color: #999;">No execution data available</p>'; |
| 633 | return; |
| 634 | }} |
| 635 | |
| 636 | const logs = data.logs; |
| 637 | const total = logs.length; |
| 638 | const errors = logs.filter(log => log.level === 'ERROR').length; |
| 639 | const errorRate = total > 0 ? ((errors / total) * 100).toFixed(1) : 0; |
| 640 | |
| 641 | // Extract execution times from log messages (if present) |
| 642 | const executionTimes = []; |
| 643 | logs.forEach(log => {{ |
| 644 | const match = log.message?.match(/(\d+(?:\.\d+)?)\s*ms/); |
| 645 | if (match) {{ |
| 646 | executionTimes.push(parseFloat(match[1])); |
| 647 | }} |
| 648 | }}); |
| 649 | |
| 650 | const avgTime = executionTimes.length > 0 |
| 651 | ? (executionTimes.reduce((a, b) => a + b, 0) / executionTimes.length).toFixed(2) |
| 652 | : 'N/A'; |
| 653 | |
| 654 | // Last execution |
| 655 | const lastLog = logs[0]; // logs are sorted newest first |
| 656 | const lastExecution = lastLog ? new Date(lastLog.timestamp).toLocaleString() : 'Never'; |
| 657 | |
| 658 | // Update metrics display |
| 659 | document.getElementById('total-executions').textContent = total; |
| 660 | document.getElementById('error-rate').textContent = errorRate + '%'; |
| 661 | document.getElementById('avg-exec-time').textContent = avgTime; |
| 662 | document.getElementById('last-execution').textContent = lastExecution; |
| 663 | |
| 664 | // Build performance trend chart (last 10 executions with timing data) |
| 665 | const recentPerf = executionTimes.slice(0, 10).reverse(); |
| 666 | let perfHTML = ''; |
| 667 | |
| 668 | if (recentPerf.length > 0) {{ |
| 669 | const maxTime = Math.max(...recentPerf); |
| 670 | perfHTML = '<div style="display: flex; gap: 10px; align-items: flex-end; height: 150px;">'; |
| 671 | recentPerf.forEach((time, i) => {{ |
| 672 | const height = (time / maxTime) * 100; |
| 673 | const color = time > avgTime * 1.5 ? '#ef4444' : (time > avgTime ? '#fbbf24' : '#4ade80'); |
| 674 | perfHTML += ` |
| 675 | <div style="flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: flex-end;"> |
| 676 | <div style="font-size: 10px; color: #999; margin-bottom: 4px;">${{time.toFixed(1)}}ms</div> |
| 677 | <div style="width: 100%; background: ${{color}}; height: ${{height}}%; min-height: 5px; border-radius: 4px 4px 0 0;"></div> |
| 678 | </div> |
| 679 | `; |
| 680 | }}); |
| 681 | perfHTML += '</div>'; |
| 682 | perfHTML += '<div style="margin-top: 10px; font-size: 12px; color: #999;">Last 10 executions (green=fast, yellow=avg, red=slow)</div>'; |
| 683 | }} else {{ |
| 684 | perfHTML = '<p style="color: #999;">No execution time data found in logs</p>'; |
| 685 | 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>'; |
| 686 | }} |
| 687 | |
| 688 | document.getElementById('recent-performance').innerHTML = perfHTML; |
| 689 | |
| 690 | }} catch (e) {{ |
| 691 | console.error('Error loading metrics:', e); |
| 692 | document.getElementById('recent-performance').innerHTML = |
| 693 | '<p style="color: #ef4444;">Error loading metrics: ' + e.message + '</p>'; |
| 694 | }} |
| 695 | }} |
| 696 | |
| 697 | async function executeObject() {{ |
| 698 | try {{ |
| 699 | const response = await fetch(`/objects/${{objectId}}`); |
| 700 | const result = await response.json(); |
| 701 | alert('Execution result:\\n' + JSON.stringify(result, null, 2)); |
| 702 | }} catch (e) {{ |
| 703 | alert('Error executing object: ' + e.message); |
| 704 | }} |
| 705 | }} |
| 706 | |
| 707 | function viewRawJSON() {{ |
| 708 | window.open(`/objects/${{objectId}}?metadata=true`, '_blank'); |
| 709 | }} |
| 710 | |
| 711 | async function rollbackTo(version) {{ |
| 712 | if (!confirm(`Rollback to version ${{version}}?`)) return; |
| 713 | |
| 714 | try {{ |
| 715 | const response = await fetch(`/objects/${{objectId}}`, {{ |
| 716 | method: 'POST', |
| 717 | headers: {{ 'Content-Type': 'application/json' }}, |
| 718 | body: JSON.stringify({{ action: 'rollback', to: version }}) |
| 719 | }}); |
| 720 | const result = await response.json(); |
| 721 | alert('Rollback successful!'); |
| 722 | location.reload(); |
| 723 | }} catch (e) {{ |
| 724 | alert('Error rolling back: ' + e.message); |
| 725 | }} |
| 726 | }} |
| 727 | |
| 728 | // Initial load |
| 729 | loadMetadata(); |
| 730 | </script> |
| 731 | </body> |
| 732 | </html> |
| 733 | """ |
| 734 | |
| 735 | return html_response(html) |