Model-config web apps: describe the app in configuration instead of writing code. Built on Polars and DuckDB.
integrate_presentation_layer.py · 16.6 KB · 555 lines
| 1 | #!/usr/bin/env python3 |
| 2 | """ |
| 3 | Integrate Presentation Layer into all DBBasic services |
| 4 | This script updates all service endpoints to use the new presentation layer |
| 5 | """ |
| 6 | |
| 7 | import os |
| 8 | import re |
| 9 | from pathlib import Path |
| 10 | from typing import Dict, List, Tuple |
| 11 | |
| 12 | class ServiceIntegrator: |
| 13 | """Integrate presentation layer into existing services""" |
| 14 | |
| 15 | def __init__(self): |
| 16 | self.services_updated = 0 |
| 17 | self.endpoints_converted = 0 |
| 18 | self.files_modified = [] |
| 19 | |
| 20 | def update_service_file(self, file_path: str, service_name: str) -> bool: |
| 21 | """Update a service file to use presentation layer""" |
| 22 | try: |
| 23 | print(f"Updating {file_path}...") |
| 24 | |
| 25 | with open(file_path, 'r') as f: |
| 26 | content = f.read() |
| 27 | |
| 28 | # Check if already using presentation layer |
| 29 | if 'from presentation_layer import' in content: |
| 30 | print(f" ✅ Already using presentation layer") |
| 31 | return True |
| 32 | |
| 33 | # Add imports at the top |
| 34 | import_statement = """from presentation_layer import PresentationLayer |
| 35 | from bootstrap_components import ExtendedBootstrapRenderer |
| 36 | from dbbasic_unified_ui import get_master_layout, SERVICES |
| 37 | |
| 38 | # Initialize presentation layer |
| 39 | PresentationLayer.add_renderer('bootstrap', ExtendedBootstrapRenderer()) |
| 40 | """ |
| 41 | |
| 42 | # Find the right place to insert imports (after initial imports) |
| 43 | import_pattern = r'((?:from .+ import .+\n|import .+\n)+)' |
| 44 | match = re.search(import_pattern, content) |
| 45 | |
| 46 | if match: |
| 47 | insert_pos = match.end() |
| 48 | content = content[:insert_pos] + "\n" + import_statement + "\n" + content[insert_pos:] |
| 49 | |
| 50 | # Update HTMLResponse endpoints |
| 51 | content = self.convert_endpoints(content, service_name) |
| 52 | |
| 53 | # Write back |
| 54 | with open(file_path, 'w') as f: |
| 55 | f.write(content) |
| 56 | |
| 57 | self.services_updated += 1 |
| 58 | self.files_modified.append(file_path) |
| 59 | print(f" ✅ Updated successfully") |
| 60 | return True |
| 61 | |
| 62 | except Exception as e: |
| 63 | print(f" ❌ Error updating {file_path}: {e}") |
| 64 | return False |
| 65 | |
| 66 | def convert_endpoints(self, content: str, service_name: str) -> str: |
| 67 | """Convert HTML endpoints to use presentation layer""" |
| 68 | |
| 69 | # Pattern to find HTMLResponse with inline HTML |
| 70 | pattern = r'@app\.get\("([^"]+)"\)[^{]*{[^}]*return HTMLResponse\(content="""([^"]*)"""' |
| 71 | |
| 72 | def replacement(match): |
| 73 | route = match.group(1) |
| 74 | self.endpoints_converted += 1 |
| 75 | |
| 76 | # Generate appropriate UI based on route |
| 77 | if route == "/" or route == "": |
| 78 | ui_function = f"get_{service_name}_dashboard()" |
| 79 | elif "template" in route: |
| 80 | ui_function = "get_template_marketplace()" |
| 81 | elif "model" in route: |
| 82 | ui_function = "get_model_editor()" |
| 83 | else: |
| 84 | ui_function = f"get_{service_name}_ui()" |
| 85 | |
| 86 | return f'''@app.get("{route}") |
| 87 | async def {route.strip('/').replace('/', '_') or 'root'}(): |
| 88 | """Serve {service_name} interface using presentation layer""" |
| 89 | ui_data = {ui_function} |
| 90 | return HTMLResponse(content=PresentationLayer.render(ui_data, 'bootstrap'))''' |
| 91 | |
| 92 | # Apply conversions |
| 93 | content = re.sub(pattern, replacement, content) |
| 94 | |
| 95 | return content |
| 96 | |
| 97 | def create_service_ui_modules(self): |
| 98 | """Create UI modules for each service""" |
| 99 | |
| 100 | services = [ |
| 101 | ('realtime_monitor', 'monitor'), |
| 102 | ('dbbasic_ai_service_builder', 'ai_services'), |
| 103 | ('dbbasic_event_store', 'event_store'), |
| 104 | ('dbbasic_crud_engine', 'data') |
| 105 | ] |
| 106 | |
| 107 | for file_name, service_key in services: |
| 108 | module_name = f"{file_name}_ui.py" |
| 109 | |
| 110 | if Path(module_name).exists(): |
| 111 | print(f"✓ UI module {module_name} already exists") |
| 112 | continue |
| 113 | |
| 114 | print(f"Creating UI module: {module_name}") |
| 115 | |
| 116 | content = f'''#!/usr/bin/env python3 |
| 117 | """ |
| 118 | {file_name.replace('_', ' ').title()} UI Module |
| 119 | Provides UI data structures for the service |
| 120 | """ |
| 121 | |
| 122 | from dbbasic_unified_ui import get_master_layout |
| 123 | |
| 124 | def get_{service_key}_dashboard(): |
| 125 | """Get dashboard UI for {service_key}""" |
| 126 | return get_master_layout( |
| 127 | title='{service_key.replace("_", " ").title()}', |
| 128 | service_name='{service_key}', |
| 129 | content=[ |
| 130 | {{ |
| 131 | 'type': 'hero', |
| 132 | 'title': '{service_key.replace("_", " ").title()}', |
| 133 | 'subtitle': 'Service dashboard' |
| 134 | }}, |
| 135 | {{ |
| 136 | 'type': 'div', |
| 137 | 'id': 'content', |
| 138 | 'children': [] |
| 139 | }} |
| 140 | ] |
| 141 | ) |
| 142 | |
| 143 | def get_{service_key}_ui(): |
| 144 | """Get default UI for {service_key}""" |
| 145 | return get_{service_key}_dashboard() |
| 146 | ''' |
| 147 | |
| 148 | with open(module_name, 'w') as f: |
| 149 | f.write(content) |
| 150 | |
| 151 | print(f" ✅ Created {module_name}") |
| 152 | |
| 153 | def update_all_services(self): |
| 154 | """Update all DBBasic services""" |
| 155 | |
| 156 | print("=" * 60) |
| 157 | print("🔧 DBBasic Service Integration") |
| 158 | print("=" * 60) |
| 159 | |
| 160 | # Create UI modules first |
| 161 | self.create_service_ui_modules() |
| 162 | |
| 163 | # Update service files |
| 164 | service_files = [ |
| 165 | ('realtime_monitor.py', 'monitor'), |
| 166 | ('dbbasic_ai_service_builder.py', 'ai_services'), |
| 167 | ('dbbasic_event_store.py', 'event_store'), |
| 168 | ('dbbasic_crud_engine.py', 'data') |
| 169 | ] |
| 170 | |
| 171 | print("\nUpdating service files...") |
| 172 | for file_path, service_name in service_files: |
| 173 | if Path(file_path).exists(): |
| 174 | self.update_service_file(file_path, service_name) |
| 175 | |
| 176 | self.print_summary() |
| 177 | |
| 178 | def print_summary(self): |
| 179 | """Print integration summary""" |
| 180 | print("\n" + "=" * 60) |
| 181 | print("INTEGRATION SUMMARY") |
| 182 | print("=" * 60) |
| 183 | |
| 184 | print(f"Services updated: {self.services_updated}") |
| 185 | print(f"Endpoints converted: {self.endpoints_converted}") |
| 186 | print(f"Files modified: {len(self.files_modified)}") |
| 187 | |
| 188 | if self.files_modified: |
| 189 | print("\nModified files:") |
| 190 | for file in self.files_modified: |
| 191 | print(f" • {file}") |
| 192 | |
| 193 | print("\n✅ Integration complete!") |
| 194 | print("\nNext steps:") |
| 195 | print("1. Test each service endpoint") |
| 196 | print("2. Verify UI rendering") |
| 197 | print("3. Check WebSocket connections") |
| 198 | print("4. Deploy to production") |
| 199 | |
| 200 | |
| 201 | class ServiceLauncher: |
| 202 | """Unified service launcher for DBBasic""" |
| 203 | |
| 204 | def __init__(self): |
| 205 | self.services = [] |
| 206 | self.processes = [] |
| 207 | |
| 208 | def create_launcher_script(self): |
| 209 | """Create a unified launcher for all services""" |
| 210 | |
| 211 | launcher_content = '''#!/usr/bin/env python3 |
| 212 | """ |
| 213 | DBBasic Unified Service Launcher |
| 214 | Start all DBBasic services with presentation layer |
| 215 | """ |
| 216 | |
| 217 | import subprocess |
| 218 | import time |
| 219 | import signal |
| 220 | import sys |
| 221 | from pathlib import Path |
| 222 | |
| 223 | class DBBasicLauncher: |
| 224 | def __init__(self): |
| 225 | self.processes = [] |
| 226 | |
| 227 | # Service configurations |
| 228 | self.services = [ |
| 229 | { |
| 230 | 'name': 'Real-time Monitor', |
| 231 | 'file': 'realtime_monitor.py', |
| 232 | 'port': 8004, |
| 233 | 'color': '\\033[92m' # Green |
| 234 | }, |
| 235 | { |
| 236 | 'name': 'Data Service', |
| 237 | 'file': 'dbbasic_crud_engine.py', |
| 238 | 'port': 8005, |
| 239 | 'color': '\\033[94m' # Blue |
| 240 | }, |
| 241 | { |
| 242 | 'name': 'AI Service Builder', |
| 243 | 'file': 'dbbasic_ai_service_builder.py', |
| 244 | 'port': 8003, |
| 245 | 'color': '\\033[95m' # Purple |
| 246 | }, |
| 247 | { |
| 248 | 'name': 'Event Store', |
| 249 | 'file': 'dbbasic_event_store.py', |
| 250 | 'port': 8006, |
| 251 | 'color': '\\033[93m' # Yellow |
| 252 | } |
| 253 | ] |
| 254 | |
| 255 | def start_service(self, service): |
| 256 | """Start a single service""" |
| 257 | if not Path(service['file']).exists(): |
| 258 | print(f"❌ {service['name']}: File not found - {service['file']}") |
| 259 | return None |
| 260 | |
| 261 | try: |
| 262 | process = subprocess.Popen( |
| 263 | ['python', service['file']], |
| 264 | stdout=subprocess.PIPE, |
| 265 | stderr=subprocess.STDOUT, |
| 266 | universal_newlines=True, |
| 267 | bufsize=1 |
| 268 | ) |
| 269 | |
| 270 | print(f"{service['color']}✅ {service['name']} started on port {service['port']}\\033[0m") |
| 271 | return process |
| 272 | except Exception as e: |
| 273 | print(f"❌ Failed to start {service['name']}: {e}") |
| 274 | return None |
| 275 | |
| 276 | def start_all(self): |
| 277 | """Start all services""" |
| 278 | print("=" * 60) |
| 279 | print("🚀 DBBasic Platform Launcher") |
| 280 | print("=" * 60) |
| 281 | print("\\nStarting all services...\\n") |
| 282 | |
| 283 | for service in self.services: |
| 284 | process = self.start_service(service) |
| 285 | if process: |
| 286 | self.processes.append(process) |
| 287 | time.sleep(2) # Give each service time to start |
| 288 | |
| 289 | print("\\n" + "=" * 60) |
| 290 | print("✅ All services started!") |
| 291 | print("=" * 60) |
| 292 | print("\\n📌 Service URLs:") |
| 293 | print(" • Dashboard: http://localhost:8004") |
| 294 | print(" • Data Service: http://localhost:8005") |
| 295 | print(" • AI Services: http://localhost:8003") |
| 296 | print(" • Event Store: http://localhost:8006") |
| 297 | print(" • API Docs: http://localhost:8005/docs") |
| 298 | print("\\nPress Ctrl+C to stop all services\\n") |
| 299 | |
| 300 | # Monitor processes |
| 301 | try: |
| 302 | while True: |
| 303 | for i, process in enumerate(self.processes): |
| 304 | if process.poll() is not None: |
| 305 | print(f"⚠️ Service {self.services[i]['name']} stopped") |
| 306 | # Restart the service |
| 307 | self.processes[i] = self.start_service(self.services[i]) |
| 308 | time.sleep(5) |
| 309 | except KeyboardInterrupt: |
| 310 | self.stop_all() |
| 311 | |
| 312 | def stop_all(self): |
| 313 | """Stop all services""" |
| 314 | print("\\n\\nStopping all services...") |
| 315 | |
| 316 | for process in self.processes: |
| 317 | if process: |
| 318 | process.terminate() |
| 319 | |
| 320 | # Wait for processes to terminate |
| 321 | for process in self.processes: |
| 322 | if process: |
| 323 | process.wait() |
| 324 | |
| 325 | print("✅ All services stopped") |
| 326 | sys.exit(0) |
| 327 | |
| 328 | def signal_handler(self, sig, frame): |
| 329 | """Handle Ctrl+C""" |
| 330 | self.stop_all() |
| 331 | |
| 332 | if __name__ == "__main__": |
| 333 | launcher = DBBasicLauncher() |
| 334 | |
| 335 | # Register signal handler |
| 336 | signal.signal(signal.SIGINT, launcher.signal_handler) |
| 337 | |
| 338 | # Start all services |
| 339 | launcher.start_all() |
| 340 | ''' |
| 341 | |
| 342 | with open('launch_dbbasic.py', 'w') as f: |
| 343 | f.write(launcher_content) |
| 344 | |
| 345 | # Make it executable |
| 346 | os.chmod('launch_dbbasic.py', 0o755) |
| 347 | |
| 348 | print("✅ Created launch_dbbasic.py") |
| 349 | print(" Run with: python launch_dbbasic.py") |
| 350 | |
| 351 | |
| 352 | class WebSocketIntegrator: |
| 353 | """Add WebSocket support for live UI updates""" |
| 354 | |
| 355 | def create_websocket_handler(self): |
| 356 | """Create unified WebSocket handler""" |
| 357 | |
| 358 | ws_content = '''#!/usr/bin/env python3 |
| 359 | """ |
| 360 | DBBasic WebSocket Handler |
| 361 | Unified WebSocket support for live UI updates |
| 362 | """ |
| 363 | |
| 364 | from fastapi import WebSocket, WebSocketDisconnect |
| 365 | from typing import List, Dict |
| 366 | import json |
| 367 | import asyncio |
| 368 | |
| 369 | class ConnectionManager: |
| 370 | """Manage WebSocket connections""" |
| 371 | |
| 372 | def __init__(self): |
| 373 | self.active_connections: List[WebSocket] = [] |
| 374 | self.service_connections: Dict[str, List[WebSocket]] = {} |
| 375 | |
| 376 | async def connect(self, websocket: WebSocket, service: str = None): |
| 377 | """Accept new connection""" |
| 378 | await websocket.accept() |
| 379 | self.active_connections.append(websocket) |
| 380 | |
| 381 | if service: |
| 382 | if service not in self.service_connections: |
| 383 | self.service_connections[service] = [] |
| 384 | self.service_connections[service].append(websocket) |
| 385 | |
| 386 | def disconnect(self, websocket: WebSocket, service: str = None): |
| 387 | """Remove connection""" |
| 388 | self.active_connections.remove(websocket) |
| 389 | |
| 390 | if service and service in self.service_connections: |
| 391 | self.service_connections[service].remove(websocket) |
| 392 | |
| 393 | async def send_personal_message(self, message: str, websocket: WebSocket): |
| 394 | """Send message to specific connection""" |
| 395 | await websocket.send_text(message) |
| 396 | |
| 397 | async def broadcast(self, message: str, service: str = None): |
| 398 | """Broadcast to all or service-specific connections""" |
| 399 | connections = self.service_connections.get(service, self.active_connections) if service else self.active_connections |
| 400 | |
| 401 | for connection in connections: |
| 402 | try: |
| 403 | await connection.send_text(message) |
| 404 | except: |
| 405 | # Connection closed, remove it |
| 406 | await self.disconnect(connection, service) |
| 407 | |
| 408 | # Global connection manager |
| 409 | manager = ConnectionManager() |
| 410 | |
| 411 | async def websocket_endpoint(websocket: WebSocket, service: str = None): |
| 412 | """WebSocket endpoint for real-time updates""" |
| 413 | await manager.connect(websocket, service) |
| 414 | |
| 415 | try: |
| 416 | while True: |
| 417 | # Wait for messages from client |
| 418 | data = await websocket.receive_text() |
| 419 | |
| 420 | # Process message |
| 421 | message = json.loads(data) |
| 422 | |
| 423 | if message.get('type') == 'subscribe': |
| 424 | # Subscribe to specific events |
| 425 | pass |
| 426 | elif message.get('type') == 'update': |
| 427 | # Broadcast UI update |
| 428 | await manager.broadcast(json.dumps({ |
| 429 | 'type': 'ui_update', |
| 430 | 'target': message.get('target'), |
| 431 | 'data': message.get('data') |
| 432 | }), service) |
| 433 | |
| 434 | except WebSocketDisconnect: |
| 435 | manager.disconnect(websocket, service) |
| 436 | |
| 437 | async def send_ui_update(target_id: str, new_value: any, service: str = None): |
| 438 | """Send UI update to all connected clients""" |
| 439 | await manager.broadcast(json.dumps({ |
| 440 | 'type': 'ui_update', |
| 441 | 'target': target_id, |
| 442 | 'value': new_value |
| 443 | }), service) |
| 444 | |
| 445 | async def send_metric_update(metrics: Dict, service: str = None): |
| 446 | """Send metric updates""" |
| 447 | await manager.broadcast(json.dumps({ |
| 448 | 'type': 'metrics', |
| 449 | 'data': metrics |
| 450 | }), service) |
| 451 | ''' |
| 452 | |
| 453 | with open('dbbasic_websocket.py', 'w') as f: |
| 454 | f.write(ws_content) |
| 455 | |
| 456 | print("✅ Created dbbasic_websocket.py") |
| 457 | |
| 458 | |
| 459 | def main(): |
| 460 | """Run all integration tasks""" |
| 461 | |
| 462 | print("🔧 Starting DBBasic Presentation Layer Integration") |
| 463 | print("=" * 60) |
| 464 | |
| 465 | # Step 1: Integrate with services |
| 466 | integrator = ServiceIntegrator() |
| 467 | integrator.update_all_services() |
| 468 | |
| 469 | # Step 2: Create launcher |
| 470 | launcher = ServiceLauncher() |
| 471 | launcher.create_launcher_script() |
| 472 | |
| 473 | # Step 3: Add WebSocket support |
| 474 | ws_integrator = WebSocketIntegrator() |
| 475 | ws_integrator.create_websocket_handler() |
| 476 | |
| 477 | # Step 4: Create deployment script |
| 478 | create_deployment_script() |
| 479 | |
| 480 | print("\n" + "=" * 60) |
| 481 | print("🎉 INTEGRATION COMPLETE!") |
| 482 | print("=" * 60) |
| 483 | |
| 484 | print("\n📋 Created files:") |
| 485 | print(" • UI modules for each service") |
| 486 | print(" • launch_dbbasic.py - Unified launcher") |
| 487 | print(" • dbbasic_websocket.py - WebSocket handler") |
| 488 | print(" • deploy_dbbasic.sh - Deployment script") |
| 489 | |
| 490 | print("\n🚀 To start DBBasic:") |
| 491 | print(" python launch_dbbasic.py") |
| 492 | |
| 493 | print("\n📦 To deploy:") |
| 494 | print(" ./deploy_dbbasic.sh") |
| 495 | |
| 496 | |
| 497 | def create_deployment_script(): |
| 498 | """Create deployment script""" |
| 499 | |
| 500 | deploy_content = '''#!/bin/bash |
| 501 | |
| 502 | # DBBasic Deployment Script |
| 503 | # Deploy DBBasic with Presentation Layer |
| 504 | |
| 505 | echo "======================================" |
| 506 | echo "🚀 DBBasic Deployment" |
| 507 | echo "======================================" |
| 508 | |
| 509 | # Check Python version |
| 510 | python_version=$(python3 --version 2>&1 | grep -Po '(?<=Python )\\d+\\.\\d+') |
| 511 | echo "✓ Python version: $python_version" |
| 512 | |
| 513 | # Install dependencies |
| 514 | echo "Installing dependencies..." |
| 515 | pip install -r requirements.txt |
| 516 | |
| 517 | # Generate UI files |
| 518 | echo "Generating UI with Presentation Layer..." |
| 519 | python generate_all_ui.py |
| 520 | |
| 521 | # Run tests |
| 522 | echo "Running tests..." |
| 523 | python -m pytest tests/ -v |
| 524 | |
| 525 | # Start services |
| 526 | echo "Starting DBBasic services..." |
| 527 | python launch_dbbasic.py & |
| 528 | |
| 529 | echo "" |
| 530 | echo "======================================" |
| 531 | echo "✅ Deployment Complete!" |
| 532 | echo "======================================" |
| 533 | echo "" |
| 534 | echo "Services running at:" |
| 535 | echo " • Dashboard: http://localhost:8004" |
| 536 | echo " • Data Service: http://localhost:8005" |
| 537 | echo " • AI Services: http://localhost:8003" |
| 538 | echo " • Event Store: http://localhost:8006" |
| 539 | echo "" |
| 540 | echo "Press Ctrl+C to stop all services" |
| 541 | |
| 542 | # Wait for interrupt |
| 543 | wait |
| 544 | ''' |
| 545 | |
| 546 | with open('deploy_dbbasic.sh', 'w') as f: |
| 547 | f.write(deploy_content) |
| 548 | |
| 549 | os.chmod('deploy_dbbasic.sh', 0o755) |
| 550 | |
| 551 | print("✅ Created deploy_dbbasic.sh") |
| 552 | |
| 553 | |
| 554 | if __name__ == "__main__": |
| 555 | main() |