Model-config web apps: describe the app in configuration instead of writing code. Built on Polars and DuckDB.
update_services.py · 6.5 KB · 198 lines
| 1 | #!/usr/bin/env python3 |
| 2 | """ |
| 3 | Update all DBBasic services to use the presentation layer |
| 4 | """ |
| 5 | |
| 6 | import os |
| 7 | import re |
| 8 | from pathlib import Path |
| 9 | |
| 10 | def update_ai_service_builder(): |
| 11 | """Update AI Service Builder to use presentation layer""" |
| 12 | |
| 13 | file_path = "dbbasic_ai_service_builder.py" |
| 14 | |
| 15 | # Read the current file |
| 16 | with open(file_path, 'r') as f: |
| 17 | content = f.read() |
| 18 | |
| 19 | # Check if already updated |
| 20 | if 'get_ai_service_main_ui' in content: |
| 21 | print("✅ AI Service Builder already uses presentation layer") |
| 22 | return |
| 23 | |
| 24 | # Replace the root endpoint |
| 25 | new_root = '''@app.get("/") |
| 26 | async def root(): |
| 27 | """Serve the AI Service Builder using presentation layer""" |
| 28 | from dbbasic_ai_service_builder_presentation import get_ai_service_main_ui |
| 29 | |
| 30 | ui_data = get_ai_service_main_ui() |
| 31 | html_content = PresentationLayer.render(ui_data, 'bootstrap') |
| 32 | |
| 33 | return HTMLResponse(content=html_content)''' |
| 34 | |
| 35 | # Find and replace the entire root function (from @app.get("/") to the end of the HTML) |
| 36 | pattern = r'@app\.get\("/"\).*?</html>"""?\)' |
| 37 | content = re.sub(pattern, new_root, content, flags=re.DOTALL) |
| 38 | |
| 39 | # Write back |
| 40 | with open(file_path, 'w') as f: |
| 41 | f.write(content) |
| 42 | |
| 43 | print("✅ Updated AI Service Builder root endpoint") |
| 44 | |
| 45 | def update_crud_engine(): |
| 46 | """Update CRUD Engine to use presentation layer""" |
| 47 | |
| 48 | file_path = "dbbasic_crud_engine.py" |
| 49 | |
| 50 | # Check if file exists |
| 51 | if not Path(file_path).exists(): |
| 52 | print("❌ dbbasic_crud_engine.py not found") |
| 53 | return |
| 54 | |
| 55 | with open(file_path, 'r') as f: |
| 56 | content = f.read() |
| 57 | |
| 58 | # Check if already has presentation layer |
| 59 | if 'get_crud_dashboard' not in content: |
| 60 | # Add import if not present |
| 61 | if 'from dbbasic_crud_engine_presentation import' not in content: |
| 62 | # Add import after other imports |
| 63 | import_line = "\nfrom dbbasic_crud_engine_presentation import get_crud_dashboard, get_template_marketplace\n" |
| 64 | content = content.replace('from fastapi import', import_line + 'from fastapi import', 1) |
| 65 | |
| 66 | # Update the root endpoint |
| 67 | new_root = '''@app.get("/") |
| 68 | async def dashboard(): |
| 69 | """Serve CRUD Engine dashboard using presentation layer""" |
| 70 | ui_data = get_crud_dashboard() |
| 71 | html_content = PresentationLayer.render(ui_data, 'bootstrap') |
| 72 | return HTMLResponse(content=html_content)''' |
| 73 | |
| 74 | # Find and replace dashboard endpoint |
| 75 | pattern = r'@app\.get\("/"\).*?return HTMLResponse\([^)]+\)' |
| 76 | if re.search(pattern, content, re.DOTALL): |
| 77 | content = re.sub(pattern, new_root, content, flags=re.DOTALL) |
| 78 | |
| 79 | # Update templates endpoint |
| 80 | new_templates = '''@app.get("/templates") |
| 81 | async def templates(): |
| 82 | """Serve template marketplace using presentation layer""" |
| 83 | ui_data = get_template_marketplace() |
| 84 | html_content = PresentationLayer.render(ui_data, 'bootstrap') |
| 85 | return HTMLResponse(content=html_content)''' |
| 86 | |
| 87 | pattern = r'@app\.get\("/templates"\).*?return HTMLResponse\([^)]+\)' |
| 88 | if re.search(pattern, content, re.DOTALL): |
| 89 | content = re.sub(pattern, new_templates, content, flags=re.DOTALL) |
| 90 | |
| 91 | # Write back |
| 92 | with open(file_path, 'w') as f: |
| 93 | f.write(content) |
| 94 | |
| 95 | print("✅ Updated CRUD Engine endpoints") |
| 96 | |
| 97 | def update_realtime_monitor(): |
| 98 | """Update Realtime Monitor to use presentation layer""" |
| 99 | |
| 100 | file_path = "realtime_monitor.py" |
| 101 | |
| 102 | if not Path(file_path).exists(): |
| 103 | print("❌ realtime_monitor.py not found") |
| 104 | return |
| 105 | |
| 106 | with open(file_path, 'r') as f: |
| 107 | content = f.read() |
| 108 | |
| 109 | # Check if already has presentation layer |
| 110 | if 'get_realtime_monitor_ui' not in content: |
| 111 | # Add import |
| 112 | if 'from realtime_monitor_presentation import' not in content: |
| 113 | import_line = "\nfrom realtime_monitor_presentation import get_realtime_monitor_ui\n" |
| 114 | content = content.replace('from fastapi import', import_line + 'from fastapi import', 1) |
| 115 | |
| 116 | # Update the root endpoint |
| 117 | new_root = '''@app.get("/") |
| 118 | async def dashboard(): |
| 119 | """Serve real-time monitor dashboard using presentation layer""" |
| 120 | from realtime_monitor_presentation import get_realtime_monitor_ui |
| 121 | |
| 122 | ui_data = get_realtime_monitor_ui() |
| 123 | html_content = PresentationLayer.render(ui_data, 'bootstrap') |
| 124 | return HTMLResponse(content=html_content)''' |
| 125 | |
| 126 | # Find and replace |
| 127 | pattern = r'@app\.get\("/"\).*?return HTMLResponse\([^)]+\)' |
| 128 | if re.search(pattern, content, re.DOTALL): |
| 129 | content = re.sub(pattern, new_root, content, flags=re.DOTALL) |
| 130 | |
| 131 | # Write back |
| 132 | with open(file_path, 'w') as f: |
| 133 | f.write(content) |
| 134 | |
| 135 | print("✅ Updated Realtime Monitor") |
| 136 | |
| 137 | def update_event_store(): |
| 138 | """Update Event Store to use presentation layer""" |
| 139 | |
| 140 | file_path = "dbbasic_event_store.py" |
| 141 | |
| 142 | if not Path(file_path).exists(): |
| 143 | print("❌ dbbasic_event_store.py not found") |
| 144 | return |
| 145 | |
| 146 | with open(file_path, 'r') as f: |
| 147 | content = f.read() |
| 148 | |
| 149 | # Check if already has presentation layer |
| 150 | if 'get_event_store_dashboard' not in content: |
| 151 | # Add import |
| 152 | if 'from dbbasic_event_store_presentation import' not in content: |
| 153 | import_line = "\nfrom dbbasic_event_store_presentation import get_event_store_dashboard\n" |
| 154 | content = content.replace('from fastapi import', import_line + 'from fastapi import', 1) |
| 155 | |
| 156 | # Update the root endpoint |
| 157 | new_root = '''@app.get("/") |
| 158 | async def dashboard(): |
| 159 | """Serve event store dashboard using presentation layer""" |
| 160 | from dbbasic_event_store_presentation import get_event_store_dashboard |
| 161 | |
| 162 | ui_data = get_event_store_dashboard() |
| 163 | html_content = PresentationLayer.render(ui_data, 'bootstrap') |
| 164 | return HTMLResponse(content=html_content)''' |
| 165 | |
| 166 | # Find and replace |
| 167 | pattern = r'@app\.get\("/"\).*?return HTMLResponse\([^)]+\)' |
| 168 | if re.search(pattern, content, re.DOTALL): |
| 169 | content = re.sub(pattern, new_root, content, flags=re.DOTALL) |
| 170 | |
| 171 | # Write back |
| 172 | with open(file_path, 'w') as f: |
| 173 | f.write(content) |
| 174 | |
| 175 | print("✅ Updated Event Store") |
| 176 | |
| 177 | def main(): |
| 178 | print("=" * 60) |
| 179 | print("🔧 Updating all services to use presentation layer") |
| 180 | print("=" * 60) |
| 181 | |
| 182 | # Update all services |
| 183 | update_ai_service_builder() |
| 184 | update_crud_engine() |
| 185 | update_realtime_monitor() |
| 186 | update_event_store() |
| 187 | |
| 188 | print("\n" + "=" * 60) |
| 189 | print("✅ All services updated!") |
| 190 | print("=" * 60) |
| 191 | print("\n📌 Services will now use the presentation layer") |
| 192 | print(" delivering 90% token reduction!") |
| 193 | print("\n🔄 Restart services to apply changes:") |
| 194 | print(" 1. Stop current services (Ctrl+C)") |
| 195 | print(" 2. Run: python launch_dbbasic.py") |
| 196 | |
| 197 | if __name__ == "__main__": |
| 198 | main() |