Model-config web apps: describe the app in configuration instead of writing code. Built on Polars and DuckDB.
dbbasic_event_store_presentation.py · 19.7 KB · 485 lines
| 1 | #!/usr/bin/env python3 |
| 2 | """ |
| 3 | DBBasic Event Store - Presentation Layer Version |
| 4 | Converts Event Store UI to use data structures |
| 5 | """ |
| 6 | |
| 7 | from presentation_layer import PresentationLayer |
| 8 | from bootstrap_components import ExtendedBootstrapRenderer |
| 9 | from dbbasic_unified_ui import get_master_layout, SERVICES |
| 10 | |
| 11 | # Initialize presentation layer |
| 12 | PresentationLayer.add_renderer('bootstrap', ExtendedBootstrapRenderer()) |
| 13 | |
| 14 | def get_event_store_dashboard(): |
| 15 | """Generate Event Store dashboard as data structure""" |
| 16 | return get_master_layout( |
| 17 | title='Event Store', |
| 18 | service_name='event_store', |
| 19 | content=[ |
| 20 | # Hero section |
| 21 | { |
| 22 | 'type': 'hero', |
| 23 | 'title': '📚 Event Store', |
| 24 | 'subtitle': 'Event sourcing, audit trails, and immutable history', |
| 25 | 'variant': 'gradient-blue' |
| 26 | }, |
| 27 | |
| 28 | # Event statistics |
| 29 | { |
| 30 | 'type': 'grid', |
| 31 | 'columns': 4, |
| 32 | 'items': [ |
| 33 | { |
| 34 | 'type': 'card', |
| 35 | 'id': 'total-events-card', |
| 36 | 'body': { |
| 37 | 'type': 'metric', |
| 38 | 'id': 'total-events', |
| 39 | 'label': 'Total Events', |
| 40 | 'value': '0', |
| 41 | 'icon': 'bi-journal-text' |
| 42 | } |
| 43 | }, |
| 44 | { |
| 45 | 'type': 'card', |
| 46 | 'id': 'events-today-card', |
| 47 | 'body': { |
| 48 | 'type': 'metric', |
| 49 | 'id': 'events-today', |
| 50 | 'label': 'Events Today', |
| 51 | 'value': '0', |
| 52 | 'icon': 'bi-calendar-day' |
| 53 | } |
| 54 | }, |
| 55 | { |
| 56 | 'type': 'card', |
| 57 | 'id': 'event-types-card', |
| 58 | 'body': { |
| 59 | 'type': 'metric', |
| 60 | 'id': 'event-types', |
| 61 | 'label': 'Event Types', |
| 62 | 'value': '0', |
| 63 | 'icon': 'bi-tags' |
| 64 | } |
| 65 | }, |
| 66 | { |
| 67 | 'type': 'card', |
| 68 | 'id': 'active-streams-card', |
| 69 | 'body': { |
| 70 | 'type': 'metric', |
| 71 | 'id': 'active-streams', |
| 72 | 'label': 'Active Streams', |
| 73 | 'value': '0', |
| 74 | 'icon': 'bi-broadcast' |
| 75 | } |
| 76 | } |
| 77 | ] |
| 78 | }, |
| 79 | |
| 80 | # Event stream viewer |
| 81 | { |
| 82 | 'type': 'card', |
| 83 | 'title': '🌊 Live Event Stream', |
| 84 | 'id': 'event-stream-card', |
| 85 | 'body': { |
| 86 | 'type': 'div', |
| 87 | 'id': 'event-stream', |
| 88 | 'class': 'event-stream-container', |
| 89 | 'style': 'height: 400px; overflow-y: auto;', |
| 90 | 'children': [] |
| 91 | } |
| 92 | }, |
| 93 | |
| 94 | # Event filters and search |
| 95 | { |
| 96 | 'type': 'card', |
| 97 | 'title': '🔍 Event Explorer', |
| 98 | 'body': { |
| 99 | 'type': 'form', |
| 100 | 'id': 'event-filter-form', |
| 101 | 'class': 'row g-3', |
| 102 | 'fields': [ |
| 103 | { |
| 104 | 'type': 'input', |
| 105 | 'id': 'event-search', |
| 106 | 'name': 'search', |
| 107 | 'label': 'Search Events', |
| 108 | 'placeholder': 'Search by ID, type, or content...', |
| 109 | 'class': 'col-md-4' |
| 110 | }, |
| 111 | { |
| 112 | 'type': 'select', |
| 113 | 'id': 'event-type-filter', |
| 114 | 'name': 'eventType', |
| 115 | 'label': 'Event Type', |
| 116 | 'class': 'col-md-3', |
| 117 | 'options': [ |
| 118 | {'value': '', 'text': 'All Types'}, |
| 119 | {'value': 'created', 'text': 'Created'}, |
| 120 | {'value': 'updated', 'text': 'Updated'}, |
| 121 | {'value': 'deleted', 'text': 'Deleted'}, |
| 122 | {'value': 'custom', 'text': 'Custom'} |
| 123 | ] |
| 124 | }, |
| 125 | { |
| 126 | 'type': 'date', |
| 127 | 'id': 'date-from', |
| 128 | 'name': 'dateFrom', |
| 129 | 'label': 'From Date', |
| 130 | 'class': 'col-md-2' |
| 131 | }, |
| 132 | { |
| 133 | 'type': 'date', |
| 134 | 'id': 'date-to', |
| 135 | 'name': 'dateTo', |
| 136 | 'label': 'To Date', |
| 137 | 'class': 'col-md-2' |
| 138 | }, |
| 139 | { |
| 140 | 'type': 'button', |
| 141 | 'text': 'Search', |
| 142 | 'variant': 'primary', |
| 143 | 'icon': 'bi-search', |
| 144 | 'class': 'col-md-1', |
| 145 | 'onclick': 'searchEvents()' |
| 146 | } |
| 147 | ] |
| 148 | } |
| 149 | }, |
| 150 | |
| 151 | # Event results table |
| 152 | { |
| 153 | 'type': 'card', |
| 154 | 'title': '📋 Event History', |
| 155 | 'body': { |
| 156 | 'type': 'table', |
| 157 | 'id': 'events-table', |
| 158 | 'striped': True, |
| 159 | 'hover': True, |
| 160 | 'headers': ['Event ID', 'Type', 'Entity', 'User', 'Timestamp', 'Actions'], |
| 161 | 'rows': [] # Populated dynamically |
| 162 | } |
| 163 | }, |
| 164 | |
| 165 | # Audit trail viewer |
| 166 | { |
| 167 | 'type': 'card', |
| 168 | 'title': '🔐 Audit Trail', |
| 169 | 'body': { |
| 170 | 'type': 'tabs', |
| 171 | 'tabs': [ |
| 172 | { |
| 173 | 'id': 'entity-audit', |
| 174 | 'title': 'Entity History', |
| 175 | 'content': { |
| 176 | 'type': 'form', |
| 177 | 'fields': [ |
| 178 | { |
| 179 | 'type': 'input', |
| 180 | 'id': 'entity-id', |
| 181 | 'label': 'Entity ID', |
| 182 | 'placeholder': 'Enter entity ID to view history' |
| 183 | }, |
| 184 | { |
| 185 | 'type': 'button', |
| 186 | 'text': 'View History', |
| 187 | 'onclick': 'loadEntityHistory()' |
| 188 | } |
| 189 | ], |
| 190 | 'result': { |
| 191 | 'type': 'timeline', |
| 192 | 'id': 'entity-timeline', |
| 193 | 'items': [] |
| 194 | } |
| 195 | } |
| 196 | }, |
| 197 | { |
| 198 | 'id': 'user-audit', |
| 199 | 'title': 'User Activity', |
| 200 | 'content': { |
| 201 | 'type': 'form', |
| 202 | 'fields': [ |
| 203 | { |
| 204 | 'type': 'input', |
| 205 | 'id': 'user-id', |
| 206 | 'label': 'User ID', |
| 207 | 'placeholder': 'Enter user ID to view activity' |
| 208 | }, |
| 209 | { |
| 210 | 'type': 'button', |
| 211 | 'text': 'View Activity', |
| 212 | 'onclick': 'loadUserActivity()' |
| 213 | } |
| 214 | ], |
| 215 | 'result': { |
| 216 | 'type': 'timeline', |
| 217 | 'id': 'user-timeline', |
| 218 | 'items': [] |
| 219 | } |
| 220 | } |
| 221 | } |
| 222 | ] |
| 223 | } |
| 224 | }, |
| 225 | |
| 226 | # Event replay functionality |
| 227 | { |
| 228 | 'type': 'card', |
| 229 | 'title': '⏮️ Event Replay', |
| 230 | 'body': { |
| 231 | 'type': 'div', |
| 232 | 'children': [ |
| 233 | { |
| 234 | 'type': 'alert', |
| 235 | 'message': 'Replay events to rebuild state at any point in time', |
| 236 | 'variant': 'info' |
| 237 | }, |
| 238 | { |
| 239 | 'type': 'form', |
| 240 | 'id': 'replay-form', |
| 241 | 'fields': [ |
| 242 | { |
| 243 | 'type': 'datetime', |
| 244 | 'id': 'replay-point', |
| 245 | 'label': 'Replay to Point in Time', |
| 246 | 'required': True |
| 247 | }, |
| 248 | { |
| 249 | 'type': 'select', |
| 250 | 'id': 'replay-target', |
| 251 | 'label': 'Target System', |
| 252 | 'options': [ |
| 253 | {'value': 'test', 'text': 'Test Environment'}, |
| 254 | {'value': 'staging', 'text': 'Staging'}, |
| 255 | {'value': 'analytics', 'text': 'Analytics DB'} |
| 256 | ] |
| 257 | }, |
| 258 | { |
| 259 | 'type': 'checkbox', |
| 260 | 'id': 'dry-run', |
| 261 | 'label': 'Dry run (preview only)', |
| 262 | 'checked': True |
| 263 | } |
| 264 | ], |
| 265 | 'submit': { |
| 266 | 'text': 'Start Replay', |
| 267 | 'variant': 'warning', |
| 268 | 'icon': 'bi-arrow-counterclockwise' |
| 269 | } |
| 270 | } |
| 271 | ] |
| 272 | } |
| 273 | }, |
| 274 | |
| 275 | # Event analytics |
| 276 | { |
| 277 | 'type': 'card', |
| 278 | 'title': '📊 Event Analytics', |
| 279 | 'body': { |
| 280 | 'type': 'grid', |
| 281 | 'columns': 2, |
| 282 | 'items': [ |
| 283 | { |
| 284 | 'type': 'chart', |
| 285 | 'id': 'events-by-type-chart', |
| 286 | 'title': 'Events by Type', |
| 287 | 'chartType': 'pie' |
| 288 | }, |
| 289 | { |
| 290 | 'type': 'chart', |
| 291 | 'id': 'events-timeline-chart', |
| 292 | 'title': 'Events Over Time', |
| 293 | 'chartType': 'line' |
| 294 | } |
| 295 | ] |
| 296 | } |
| 297 | } |
| 298 | ], |
| 299 | scripts=[ |
| 300 | { |
| 301 | 'type': 'script', |
| 302 | 'content': ''' |
| 303 | // Event Store functionality |
| 304 | const API_BASE = 'http://localhost:8006'; |
| 305 | let ws = null; |
| 306 | |
| 307 | function connectEventStream() { |
| 308 | ws = new WebSocket('ws://localhost:8006/ws'); |
| 309 | |
| 310 | ws.onopen = () => { |
| 311 | console.log('Connected to event stream'); |
| 312 | updateConnectionStatus('connected'); |
| 313 | }; |
| 314 | |
| 315 | ws.onmessage = (event) => { |
| 316 | const data = JSON.parse(event.data); |
| 317 | handleIncomingEvent(data); |
| 318 | }; |
| 319 | |
| 320 | ws.onclose = () => { |
| 321 | console.log('Disconnected from event stream'); |
| 322 | updateConnectionStatus('disconnected'); |
| 323 | // Reconnect after 3 seconds |
| 324 | setTimeout(connectEventStream, 3000); |
| 325 | }; |
| 326 | } |
| 327 | |
| 328 | function handleIncomingEvent(event) { |
| 329 | // Add to live stream |
| 330 | const stream = document.getElementById('event-stream'); |
| 331 | const eventDiv = document.createElement('div'); |
| 332 | eventDiv.className = 'alert alert-info mb-2'; |
| 333 | eventDiv.innerHTML = ` |
| 334 | <small class="text-muted">${new Date(event.timestamp).toLocaleTimeString()}</small> |
| 335 | <strong>${event.type}</strong>: ${event.entity_type} ${event.entity_id} |
| 336 | `; |
| 337 | stream.insertBefore(eventDiv, stream.firstChild); |
| 338 | |
| 339 | // Keep only last 20 events |
| 340 | while (stream.children.length > 20) { |
| 341 | stream.removeChild(stream.lastChild); |
| 342 | } |
| 343 | |
| 344 | // Update statistics |
| 345 | updateEventStats(); |
| 346 | } |
| 347 | |
| 348 | async function updateEventStats() { |
| 349 | const response = await fetch(`${API_BASE}/api/stats`); |
| 350 | const stats = await response.json(); |
| 351 | |
| 352 | document.getElementById('total-events').innerText = stats.total.toLocaleString(); |
| 353 | document.getElementById('events-today').innerText = stats.today.toLocaleString(); |
| 354 | document.getElementById('event-types').innerText = stats.types; |
| 355 | document.getElementById('active-streams').innerText = stats.streams; |
| 356 | } |
| 357 | |
| 358 | async function searchEvents() { |
| 359 | const form = document.getElementById('event-filter-form'); |
| 360 | const formData = new FormData(form); |
| 361 | |
| 362 | const params = new URLSearchParams(); |
| 363 | for (const [key, value] of formData.entries()) { |
| 364 | if (value) params.append(key, value); |
| 365 | } |
| 366 | |
| 367 | const response = await fetch(`${API_BASE}/api/events?${params}`); |
| 368 | const events = await response.json(); |
| 369 | |
| 370 | updateEventsTable(events); |
| 371 | } |
| 372 | |
| 373 | function updateEventsTable(events) { |
| 374 | const tbody = document.querySelector('#events-table tbody'); |
| 375 | tbody.innerHTML = ''; |
| 376 | |
| 377 | events.forEach(event => { |
| 378 | const row = tbody.insertRow(); |
| 379 | row.innerHTML = ` |
| 380 | <td><code>${event.id}</code></td> |
| 381 | <td><span class="badge bg-primary">${event.type}</span></td> |
| 382 | <td>${event.entity_type} #${event.entity_id}</td> |
| 383 | <td>${event.user || 'System'}</td> |
| 384 | <td>${new Date(event.timestamp).toLocaleString()}</td> |
| 385 | <td> |
| 386 | <button class="btn btn-sm btn-info" onclick="viewEvent('${event.id}')"> |
| 387 | <i class="bi bi-eye"></i> |
| 388 | </button> |
| 389 | </td> |
| 390 | `; |
| 391 | }); |
| 392 | } |
| 393 | |
| 394 | async function loadEntityHistory() { |
| 395 | const entityId = document.getElementById('entity-id').value; |
| 396 | if (!entityId) return; |
| 397 | |
| 398 | const response = await fetch(`${API_BASE}/api/audit/entity/${entityId}`); |
| 399 | const history = await response.json(); |
| 400 | |
| 401 | const timeline = document.getElementById('entity-timeline'); |
| 402 | timeline.innerHTML = history.map(event => ` |
| 403 | <div class="timeline-item"> |
| 404 | <div class="timeline-marker"></div> |
| 405 | <div class="timeline-content"> |
| 406 | <strong>${event.type}</strong> - ${new Date(event.timestamp).toLocaleString()} |
| 407 | <pre>${JSON.stringify(event.data, null, 2)}</pre> |
| 408 | </div> |
| 409 | </div> |
| 410 | `).join(''); |
| 411 | } |
| 412 | |
| 413 | // Initialize on load |
| 414 | document.addEventListener('DOMContentLoaded', () => { |
| 415 | connectEventStream(); |
| 416 | updateEventStats(); |
| 417 | searchEvents(); |
| 418 | }); |
| 419 | ''' |
| 420 | } |
| 421 | ] |
| 422 | ) |
| 423 | |
| 424 | def get_event_viewer(event_id: str, event_data: dict): |
| 425 | """Generate detailed event viewer""" |
| 426 | return get_master_layout( |
| 427 | title=f'Event: {event_id}', |
| 428 | service_name='event_store', |
| 429 | content=[ |
| 430 | { |
| 431 | 'type': 'breadcrumb', |
| 432 | 'items': [ |
| 433 | {'text': 'Event Store', 'url': '/'}, |
| 434 | {'text': 'Events', 'url': '/events'}, |
| 435 | {'text': event_id, 'active': True} |
| 436 | ] |
| 437 | }, |
| 438 | { |
| 439 | 'type': 'card', |
| 440 | 'title': f'Event Details', |
| 441 | 'body': { |
| 442 | 'type': 'description_list', |
| 443 | 'items': [ |
| 444 | {'term': 'Event ID', 'description': event_id}, |
| 445 | {'term': 'Type', 'description': {'type': 'badge', 'text': event_data.get('type'), 'variant': 'primary'}}, |
| 446 | {'term': 'Entity', 'description': f"{event_data.get('entity_type')} #{event_data.get('entity_id')}"}, |
| 447 | {'term': 'Timestamp', 'description': event_data.get('timestamp')}, |
| 448 | {'term': 'User', 'description': event_data.get('user', 'System')}, |
| 449 | {'term': 'Version', 'description': event_data.get('version', '1')} |
| 450 | ] |
| 451 | } |
| 452 | }, |
| 453 | { |
| 454 | 'type': 'card', |
| 455 | 'title': 'Event Payload', |
| 456 | 'body': { |
| 457 | 'type': 'code', |
| 458 | 'language': 'json', |
| 459 | 'content': event_data.get('payload', {}) |
| 460 | } |
| 461 | }, |
| 462 | { |
| 463 | 'type': 'card', |
| 464 | 'title': 'Related Events', |
| 465 | 'body': { |
| 466 | 'type': 'table', |
| 467 | 'id': 'related-events', |
| 468 | 'headers': ['Event ID', 'Type', 'Timestamp'], |
| 469 | 'rows': [] # Populated with related events |
| 470 | } |
| 471 | } |
| 472 | ] |
| 473 | ) |
| 474 | |
| 475 | # Generate interfaces |
| 476 | if __name__ == "__main__": |
| 477 | # Main dashboard |
| 478 | dashboard_html = PresentationLayer.render(get_event_store_dashboard(), 'bootstrap') |
| 479 | with open('event_store_dashboard_new.html', 'w') as f: |
| 480 | f.write(dashboard_html) |
| 481 | |
| 482 | print("✅ Generated Event Store interface using presentation layer") |
| 483 | print("\nBefore: ~600 lines of HTML in Python") |
| 484 | print("After: ~250 lines of data structures") |
| 485 | print("Token reduction: ~58%") |