askrobots / dbbasic Public

Model-config web apps: describe the app in configuration instead of writing code. Built on Polars and DuckDB.

0 downloads

dbbasic/realtime_monitor_ondemand.py
Download zip
Clone
Clone with git
git clone https://gitstable.com/git/askrobots/dbbasic.git
HTTPS
https://gitstable.com/git/askrobots/dbbasic.git
SSH
ssh://git@gitstable.com:2222/askrobots/dbbasic.git

Cloning needs no sign-in. Pushing over HTTPS asks for your username and an access token. SSH needs your key under SSH keys.

danq9 Add DBBasic crate engine logo and update README 0d628db 1 year, 2 months ago History
realtime_monitor_ondemand.py · 22.5 KB · 648 lines
1#!/usr/bin/env python3
2"""
3DBBasic Real-time Monitor - On-Demand Mode
4Only processes services when there are actual requests, not continuous simulation
5"""
6
7from fastapi import FastAPI, WebSocket, WebSocketDisconnect, HTTPException
8from fastapi.responses import HTMLResponse, JSONResponse
9import asyncio
10import json
11from datetime import datetime
12from typing import Dict, Any, List, Optional
13import psutil
14import time
15from collections import deque
16import os
17import sys
18
19# Import our services
20sys.path.append(os.path.dirname(os.path.abspath(__file__)))
21from services.calculate_shipping import calculate_shipping
22from services.calculate_discount import calculate_discount
23from services.calculate_order_total import calculate_order_total
24from services.send_notification import send_notification
25from services.detect_fraud import detect_fraud
26from services.segment_customer import segment_customer
27
28app = FastAPI(title="DBBasic Realtime Monitor - On Demand")
29
30# Store for active connections
31active_connections: List[WebSocket] = []
32
33# Metrics storage
34metrics = {
35 "operations_per_second": deque(maxlen=60),
36 "rows_per_second": 402000000, # Our benchmark
37 "active_connections": 0,
38 "total_operations": 0,
39 "service_calls": deque(maxlen=100),
40 "last_activity": None,
41 "mode": "on-demand" # vs "continuous"
42}
43
44# Service registry
45services = {
46 "calculate_shipping": calculate_shipping,
47 "calculate_discount": calculate_discount,
48 "calculate_order_total": calculate_order_total,
49 "send_notification": send_notification,
50 "detect_fraud": detect_fraud,
51 "segment_customer": segment_customer
52}
53
54async def broadcast_message(message: dict):
55 """Broadcast message to all connected clients"""
56 for connection in active_connections:
57 try:
58 await connection.send_json(message)
59 except:
60 pass
61
62@app.post("/api/service/{service_name}")
63async def call_service_endpoint(service_name: str, payload: Dict[str, Any]):
64 """
65 Call a service on-demand via API endpoint.
66 This is the main difference - services only run when explicitly called!
67 """
68 if service_name not in services:
69 raise HTTPException(status_code=404, detail=f"Service '{service_name}' not found")
70
71 try:
72 # Record start time
73 start_time = time.time()
74
75 # Call the actual service
76 service_func = services[service_name]
77 result = await service_func(payload)
78
79 # Calculate execution time
80 execution_time = (time.time() - start_time) * 1000 # ms
81
82 # Create activity event
83 event = {
84 "type": "service_call",
85 "timestamp": datetime.now().isoformat(),
86 "service": service_name,
87 "execution_time_ms": round(execution_time, 3),
88 "success": result.get("success", False),
89 "data": result.get("data", {}),
90 "mode": "on-demand"
91 }
92
93 # Store metrics
94 metrics["service_calls"].append(event)
95 metrics["total_operations"] += 1
96 metrics["last_activity"] = datetime.now().isoformat()
97
98 # Broadcast to connected clients
99 await broadcast_message({
100 **event,
101 "data_preview": str(result.get("data", {}))[:100] + "..." if result.get("data") else "No data"
102 })
103
104 return JSONResponse(content={
105 "success": True,
106 "execution_time_ms": round(execution_time, 3),
107 "result": result
108 })
109
110 except Exception as e:
111 return JSONResponse(
112 status_code=500,
113 content={
114 "success": False,
115 "error": str(e)
116 }
117 )
118
119@app.post("/api/batch")
120async def batch_process(requests: List[Dict[str, Any]]):
121 """
122 Process multiple service requests in batch.
123 Useful for testing or bulk operations.
124 """
125 results = []
126
127 for request in requests:
128 service_name = request.get("service")
129 payload = request.get("payload", {})
130
131 if service_name in services:
132 try:
133 start_time = time.time()
134 result = await services[service_name](payload)
135 execution_time = (time.time() - start_time) * 1000
136
137 results.append({
138 "service": service_name,
139 "success": True,
140 "execution_time_ms": round(execution_time, 3),
141 "result": result
142 })
143
144 # Update metrics
145 metrics["total_operations"] += 1
146
147 except Exception as e:
148 results.append({
149 "service": service_name,
150 "success": False,
151 "error": str(e)
152 })
153
154 metrics["last_activity"] = datetime.now().isoformat()
155
156 # Broadcast summary
157 await broadcast_message({
158 "type": "batch_complete",
159 "timestamp": datetime.now().isoformat(),
160 "total_requests": len(requests),
161 "successful": sum(1 for r in results if r.get("success")),
162 "failed": sum(1 for r in results if not r.get("success"))
163 })
164
165 return JSONResponse(content={
166 "batch_size": len(requests),
167 "results": results
168 })
169
170async def calculate_metrics():
171 """Calculate and broadcast system metrics periodically"""
172 while True:
173 try:
174 # Get system metrics
175 cpu_percent = psutil.cpu_percent(interval=0.1)
176 memory = psutil.virtual_memory()
177
178 # Calculate operations per second (based on last minute)
179 now = datetime.now()
180 recent_ops = [
181 op for op in metrics["service_calls"]
182 if op.get("timestamp") and
183 (now - datetime.fromisoformat(op["timestamp"])).seconds < 60
184 ]
185 ops_per_sec = len(recent_ops) / 60 if recent_ops else 0
186
187 # Prepare metrics message
188 metrics_msg = {
189 "type": "metrics",
190 "timestamp": now.isoformat(),
191 "data": {
192 "operations_per_second": round(ops_per_sec, 2),
193 "rows_per_second": metrics["rows_per_second"],
194 "active_connections": len(active_connections),
195 "total_operations": metrics["total_operations"],
196 "cpu_percent": cpu_percent,
197 "memory_used_gb": round(memory.used / (1024**3), 2),
198 "memory_total_gb": round(memory.total / (1024**3), 2),
199 "mode": metrics["mode"],
200 "last_activity": metrics["last_activity"]
201 }
202 }
203
204 await broadcast_message(metrics_msg)
205
206 except Exception as e:
207 print(f"Error calculating metrics: {e}")
208
209 await asyncio.sleep(1)
210
211@app.on_event("startup")
212async def startup_event():
213 """Start background tasks"""
214 asyncio.create_task(calculate_metrics())
215
216@app.websocket("/ws")
217async def websocket_endpoint(websocket: WebSocket):
218 """WebSocket endpoint for real-time updates"""
219 await websocket.accept()
220 active_connections.append(websocket)
221
222 try:
223 await websocket.send_json({
224 "type": "connection",
225 "status": "connected",
226 "mode": "on-demand",
227 "timestamp": datetime.now().isoformat()
228 })
229
230 while True:
231 data = await websocket.receive_text()
232 # Handle any commands from client if needed
233
234 except WebSocketDisconnect:
235 active_connections.remove(websocket)
236
237@app.get("/")
238async def root():
239 """Serve the real-time monitor interface"""
240 html_content = """
241<!DOCTYPE html>
242<html lang="en">
243<head>
244 <meta charset="UTF-8">
245 <meta name="viewport" content="width=device-width, initial-scale=1.0">
246 <title>DBBasic Monitor - On-Demand Mode</title>
247 <style>
248 * { margin: 0; padding: 0; box-sizing: border-box; }
249
250 body {
251 font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
252 background: #0a0e27;
253 color: #e0e0e0;
254 height: 100vh;
255 display: flex;
256 flex-direction: column;
257 }
258
259 .header {
260 background: rgba(0, 0, 0, 0.5);
261 backdrop-filter: blur(10px);
262 border-bottom: 1px solid rgba(76, 175, 80, 0.3);
263 padding: 1rem 2rem;
264 display: flex;
265 justify-content: space-between;
266 align-items: center;
267 }
268
269 .logo {
270 font-size: 24px;
271 font-weight: bold;
272 color: #4CAF50;
273 text-shadow: 0 0 10px rgba(76, 175, 80, 0.5);
274 }
275
276 .mode-indicator {
277 background: #2196F3;
278 color: white;
279 padding: 0.25rem 1rem;
280 border-radius: 15px;
281 font-size: 12px;
282 font-weight: 600;
283 }
284
285 .main-container {
286 flex: 1;
287 display: grid;
288 grid-template-columns: 250px 1fr 300px;
289 gap: 1rem;
290 padding: 1rem;
291 overflow: hidden;
292 }
293
294 .sidebar {
295 background: rgba(0, 0, 0, 0.3);
296 border: 1px solid rgba(76, 175, 80, 0.2);
297 border-radius: 8px;
298 padding: 1rem;
299 }
300
301 .test-controls {
302 margin-top: 2rem;
303 }
304
305 .btn {
306 width: 100%;
307 padding: 0.75rem;
308 margin-bottom: 0.5rem;
309 background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
310 color: white;
311 border: none;
312 border-radius: 6px;
313 cursor: pointer;
314 font-weight: 600;
315 transition: all 0.2s;
316 }
317
318 .btn:hover {
319 transform: translateY(-2px);
320 box-shadow: 0 4px 12px rgba(102, 126, 234, 0.4);
321 }
322
323 .btn-secondary {
324 background: rgba(76, 175, 80, 0.2);
325 border: 1px solid #4CAF50;
326 }
327
328 .metrics-row {
329 display: grid;
330 grid-template-columns: repeat(4, 1fr);
331 gap: 1rem;
332 margin-bottom: 1rem;
333 }
334
335 .metric-card {
336 background: rgba(0, 0, 0, 0.3);
337 border: 1px solid rgba(76, 175, 80, 0.2);
338 border-radius: 8px;
339 padding: 1.5rem;
340 text-align: center;
341 }
342
343 .metric-value {
344 font-size: 32px;
345 font-weight: bold;
346 color: #4CAF50;
347 text-shadow: 0 0 20px rgba(76, 175, 80, 0.5);
348 margin-bottom: 0.5rem;
349 font-family: 'Courier New', monospace;
350 }
351
352 .metric-label {
353 font-size: 12px;
354 color: #999;
355 text-transform: uppercase;
356 }
357
358 .data-stream {
359 flex: 1;
360 background: rgba(0, 0, 0, 0.3);
361 border: 1px solid rgba(76, 175, 80, 0.2);
362 border-radius: 8px;
363 padding: 1rem;
364 overflow: hidden;
365 display: flex;
366 flex-direction: column;
367 }
368
369 .stream-content {
370 flex: 1;
371 overflow-y: auto;
372 font-family: 'Courier New', monospace;
373 font-size: 12px;
374 }
375
376 .stream-entry {
377 padding: 0.5rem;
378 margin-bottom: 0.25rem;
379 border-left: 3px solid #4CAF50;
380 background: rgba(76, 175, 80, 0.05);
381 animation: slideIn 0.3s;
382 }
383
384 @keyframes slideIn {
385 from { opacity: 0; transform: translateX(-20px); }
386 to { opacity: 1; transform: translateX(0); }
387 }
388
389 .idle-message {
390 text-align: center;
391 color: #666;
392 padding: 2rem;
393 font-style: italic;
394 }
395
396 .timestamp { color: #666; margin-right: 1rem; }
397 .service-name { color: #4CAF50; font-weight: bold; margin-right: 1rem; }
398 .exec-time { color: #2196F3; margin-right: 1rem; }
399 </style>
400</head>
401<body>
402 <div class="header">
403 <div style="display: flex; align-items: center; gap: 1rem;">
404 <div class="logo">DBBasic</div>
405 <span style="color: #666; font-size: 14px;">Real-time Service Monitor</span>
406 </div>
407 <div class="mode-indicator">ON-DEMAND MODE</div>
408 <div>
409 <span id="connection-status">Connecting...</span>
410 <span style="margin-left: 1rem;">Last Activity: <span id="last-activity">Never</span></span>
411 </div>
412 </div>
413
414 <div class="main-container">
415 <div class="sidebar">
416 <h3 style="font-size: 12px; text-transform: uppercase; color: #4CAF50; margin-bottom: 1rem;">Services Available</h3>
417 <div id="services-list"></div>
418
419 <div class="test-controls">
420 <h3 style="font-size: 12px; text-transform: uppercase; color: #4CAF50; margin-bottom: 1rem;">Test Controls</h3>
421 <button class="btn" onclick="testSingleService()">Test Single Service</button>
422 <button class="btn btn-secondary" onclick="testBatch()">Test Batch (10 calls)</button>
423 </div>
424 </div>
425
426 <div style="display: flex; flex-direction: column; gap: 1rem;">
427 <div class="metrics-row">
428 <div class="metric-card">
429 <div class="metric-value" id="ops-per-sec">0</div>
430 <div class="metric-label">Ops/sec (last min)</div>
431 </div>
432 <div class="metric-card">
433 <div class="metric-value">402M</div>
434 <div class="metric-label">Max Rows/sec</div>
435 </div>
436 <div class="metric-card">
437 <div class="metric-value" id="total-ops">0</div>
438 <div class="metric-label">Total Ops</div>
439 </div>
440 <div class="metric-card">
441 <div class="metric-value" id="cpu-usage">0%</div>
442 <div class="metric-label">CPU Usage</div>
443 </div>
444 </div>
445
446 <div class="data-stream">
447 <h3 style="font-size: 14px; color: #4CAF50; margin-bottom: 1rem;">
448 Service Activity (On-Demand Only)
449 </h3>
450 <div class="stream-content" id="stream-content">
451 <div class="idle-message">
452 Waiting for service calls...<br><br>
453 Services only run when explicitly called.<br>
454 Use the test buttons or call the API endpoints directly.
455 </div>
456 </div>
457 </div>
458 </div>
459
460 <div class="sidebar">
461 <h3 style="font-size: 12px; text-transform: uppercase; color: #4CAF50; margin-bottom: 1rem;">API Endpoints</h3>
462 <div style="font-family: monospace; font-size: 11px; color: #999;">
463 <div style="margin-bottom: 1rem;">
464 <div style="color: #4CAF50;">POST /api/service/{name}</div>
465 <div>Call a specific service</div>
466 </div>
467 <div style="margin-bottom: 1rem;">
468 <div style="color: #4CAF50;">POST /api/batch</div>
469 <div>Process multiple requests</div>
470 </div>
471 <div style="margin-bottom: 1rem;">
472 <div style="color: #4CAF50;">GET /api/metrics</div>
473 <div>Get current metrics</div>
474 </div>
475 </div>
476 </div>
477 </div>
478
479 <script>
480 const ws = new WebSocket('ws://localhost:8005/ws');
481 const streamContent = document.getElementById('stream-content');
482 const services = ['calculate_shipping', 'calculate_discount', 'calculate_order_total',
483 'send_notification', 'detect_fraud', 'segment_customer'];
484
485 ws.onopen = () => {
486 document.getElementById('connection-status').textContent = 'Connected';
487 document.getElementById('connection-status').style.color = '#4CAF50';
488 };
489
490 ws.onmessage = (event) => {
491 const data = JSON.parse(event.data);
492
493 if (data.type === 'service_call') {
494 // Clear idle message on first real activity
495 if (streamContent.querySelector('.idle-message')) {
496 streamContent.innerHTML = '';
497 }
498
499 const entry = document.createElement('div');
500 entry.className = 'stream-entry';
501 entry.innerHTML = `
502 <span class="timestamp">${new Date(data.timestamp).toLocaleTimeString()}</span>
503 <span class="service-name">${data.service}</span>
504 <span class="exec-time">${data.execution_time_ms}ms</span>
505 <span>${data.data_preview || 'No data'}</span>
506 `;
507 streamContent.insertBefore(entry, streamContent.firstChild);
508
509 // Keep only last 50 entries
510 while (streamContent.children.length > 50) {
511 streamContent.removeChild(streamContent.lastChild);
512 }
513
514 } else if (data.type === 'metrics') {
515 document.getElementById('ops-per-sec').textContent = data.data.operations_per_second.toFixed(2);
516 document.getElementById('total-ops').textContent = data.data.total_operations.toLocaleString();
517 document.getElementById('cpu-usage').textContent = data.data.cpu_percent + '%';
518
519 if (data.data.last_activity) {
520 const lastTime = new Date(data.data.last_activity);
521 document.getElementById('last-activity').textContent = lastTime.toLocaleTimeString();
522 }
523 }
524 };
525
526 ws.onclose = () => {
527 document.getElementById('connection-status').textContent = 'Disconnected';
528 document.getElementById('connection-status').style.color = '#f44336';
529 };
530
531 // Initialize services list
532 document.getElementById('services-list').innerHTML = services.map(service => `
533 <div style="padding: 0.5rem; margin-bottom: 0.5rem; background: rgba(76, 175, 80, 0.1);
534 border-radius: 4px; font-size: 12px;">
535 ${service.replace(/_/g, ' ')}
536 </div>
537 `).join('');
538
539 async function testSingleService() {
540 const service = services[Math.floor(Math.random() * services.length)];
541 const payload = generateTestPayload(service);
542
543 const response = await fetch(`/api/service/${service}`, {
544 method: 'POST',
545 headers: { 'Content-Type': 'application/json' },
546 body: JSON.stringify(payload)
547 });
548
549 const result = await response.json();
550 console.log('Service result:', result);
551 }
552
553 async function testBatch() {
554 const requests = [];
555 for (let i = 0; i < 10; i++) {
556 const service = services[Math.floor(Math.random() * services.length)];
557 requests.push({
558 service: service,
559 payload: generateTestPayload(service)
560 });
561 }
562
563 const response = await fetch('/api/batch', {
564 method: 'POST',
565 headers: { 'Content-Type': 'application/json' },
566 body: JSON.stringify(requests)
567 });
568
569 const result = await response.json();
570 console.log('Batch result:', result);
571 }
572
573 function generateTestPayload(service) {
574 // Generate appropriate test data for each service
575 const payloads = {
576 calculate_shipping: {
577 weight: Math.random() * 20,
578 shipping_speed: ['standard', 'express', 'overnight'][Math.floor(Math.random() * 3)],
579 is_fragile: Math.random() > 0.5,
580 order_total: Math.random() * 500
581 },
582 calculate_discount: {
583 customer_tier: ['bronze', 'silver', 'gold', 'platinum'][Math.floor(Math.random() * 4)],
584 order_total: Math.random() * 1000,
585 items_count: Math.floor(Math.random() * 10) + 1
586 },
587 calculate_order_total: {
588 items: Array(Math.floor(Math.random() * 5) + 1).fill(0).map(() => ({
589 price: Math.random() * 100,
590 quantity: Math.floor(Math.random() * 5) + 1
591 })),
592 customer_location: {
593 state: ['CA', 'TX', 'NY', 'FL'][Math.floor(Math.random() * 4)],
594 city: ['San Francisco', 'Austin', 'New York', 'Miami'][Math.floor(Math.random() * 4)]
595 }
596 },
597 send_notification: {
598 notification_type: ['email', 'sms', 'push'][Math.floor(Math.random() * 3)],
599 recipient: { email: 'user@example.com' },
600 subject: 'Test Notification',
601 message: 'Test message content'
602 },
603 detect_fraud: {
604 order_data: { total: Math.random() * 5000 },
605 customer_data: {
606 account_age_days: Math.floor(Math.random() * 365),
607 previous_orders: Math.floor(Math.random() * 50)
608 },
609 payment_data: { method: ['card', 'paypal', 'prepaid_card'][Math.floor(Math.random() * 3)] }
610 },
611 segment_customer: {
612 customer_id: 'CUST' + Math.floor(Math.random() * 10000),
613 purchase_history: {
614 total_orders: Math.floor(Math.random() * 100),
615 lifetime_value: Math.random() * 10000,
616 days_since_last_order: Math.floor(Math.random() * 365)
617 },
618 engagement_metrics: {
619 email_open_rate: Math.random() * 0.5,
620 app_usage_days_per_month: Math.floor(Math.random() * 30)
621 }
622 }
623 };
624
625 return payloads[service] || {};
626 }
627 </script>
628</body>
629</html>
630 """
631 return HTMLResponse(content=html_content)
632
633@app.get("/api/metrics")
634async def get_metrics():
635 """Get current metrics as JSON"""
636 return JSONResponse({
637 "mode": "on-demand",
638 "total_operations": metrics["total_operations"],
639 "recent_calls": list(metrics["service_calls"])[-10:],
640 "last_activity": metrics["last_activity"]
641 })
642
643if __name__ == "__main__":
644 import uvicorn
645 print("\n🚀 DBBasic On-Demand Monitor Starting...")
646 print(" Services only run when explicitly called")
647 print(" No continuous simulation - pure on-demand processing\n")
648 uvicorn.run(app, host="0.0.0.0", port=8005)