snikhilesh commited on
Commit
db6628e
·
verified ·
1 Parent(s): ea096d7

Deploy simple_test_server.py to backend/ directory

Browse files
Files changed (1) hide show
  1. backend/simple_test_server.py +195 -0
backend/simple_test_server.py ADDED
@@ -0,0 +1,195 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Minimal Test Server - No Heavy Dependencies
3
+ Just monitoring infrastructure for load testing
4
+ """
5
+
6
+ from fastapi import FastAPI, Request
7
+ from fastapi.middleware.cors import CORSMiddleware
8
+ from typing import Dict, Any
9
+ from datetime import datetime, timedelta
10
+ import uuid
11
+ import logging
12
+ import time
13
+
14
+ logging.basicConfig(level=logging.INFO)
15
+ logger = logging.getLogger(__name__)
16
+
17
+ # Initialize FastAPI app
18
+ app = FastAPI(title="Medical AI Platform - Test Server", version="2.0.0")
19
+
20
+ app.add_middleware(
21
+ CORSMiddleware,
22
+ allow_origins=["*"],
23
+ allow_credentials=True,
24
+ allow_methods=["*"],
25
+ allow_headers=["*"],
26
+ )
27
+
28
+ # Simple in-memory monitoring
29
+ class SimpleMonitoring:
30
+ def __init__(self):
31
+ self.start_time = datetime.utcnow()
32
+ self.request_count = 0
33
+ self.error_count = 0
34
+ self.latencies = []
35
+ self.cache_hits = 0
36
+ self.cache_misses = 0
37
+ self.cache_entries = 0
38
+
39
+ def track_request(self, latency_ms: float, success: bool):
40
+ self.request_count += 1
41
+ self.latencies.append(latency_ms)
42
+ if not success:
43
+ self.error_count += 1
44
+
45
+ def get_stats(self):
46
+ uptime = (datetime.utcnow() - self.start_time).total_seconds()
47
+ error_rate = self.error_count / max(self.request_count, 1)
48
+ avg_latency = sum(self.latencies) / max(len(self.latencies), 1)
49
+
50
+ return {
51
+ "status": "operational" if error_rate < 0.05 else "degraded",
52
+ "uptime_seconds": uptime,
53
+ "uptime_human": f"{int(uptime//3600)}h {int((uptime%3600)//60)}m",
54
+ "total_requests": self.request_count,
55
+ "error_count": self.error_count,
56
+ "error_rate": error_rate,
57
+ "error_threshold": 0.05,
58
+ "avg_latency_ms": avg_latency,
59
+ "cache": {
60
+ "hits": self.cache_hits,
61
+ "misses": self.cache_misses,
62
+ "hit_rate": self.cache_hits / max(self.cache_hits + self.cache_misses, 1),
63
+ "total_entries": self.cache_entries,
64
+ "memory_usage_mb": self.cache_entries * 0.1
65
+ }
66
+ }
67
+
68
+ monitoring = SimpleMonitoring()
69
+
70
+ @app.middleware("http")
71
+ async def monitoring_middleware(request: Request, call_next):
72
+ start_time = time.time()
73
+ try:
74
+ response = await call_next(request)
75
+ latency_ms = (time.time() - start_time) * 1000
76
+ monitoring.track_request(latency_ms, response.status_code < 400)
77
+ return response
78
+ except Exception as e:
79
+ latency_ms = (time.time() - start_time) * 1000
80
+ monitoring.track_request(latency_ms, False)
81
+ raise
82
+
83
+ @app.get("/health")
84
+ async def health_check():
85
+ stats = monitoring.get_stats()
86
+ return {
87
+ "status": stats["status"],
88
+ "components": {"monitoring": "active"},
89
+ "monitoring": {
90
+ "uptime_seconds": stats["uptime_seconds"],
91
+ "error_rate": stats["error_rate"],
92
+ "active_alerts": 0,
93
+ "critical_alerts": 0
94
+ },
95
+ "timestamp": datetime.utcnow().isoformat()
96
+ }
97
+
98
+ @app.get("/health/dashboard")
99
+ async def get_health_dashboard():
100
+ stats = monitoring.get_stats()
101
+
102
+ # Simulate some cache activity
103
+ if monitoring.request_count % 3 == 0:
104
+ monitoring.cache_hits += 1
105
+ else:
106
+ monitoring.cache_misses += 1
107
+ monitoring.cache_entries += 1
108
+
109
+ return {
110
+ "status": stats["status"],
111
+ "timestamp": datetime.utcnow().isoformat(),
112
+ "system": {
113
+ "uptime_seconds": stats["uptime_seconds"],
114
+ "uptime_human": stats["uptime_human"],
115
+ "error_rate": stats["error_rate"],
116
+ "total_requests": stats["total_requests"],
117
+ "error_threshold": 0.05,
118
+ "status": stats["status"]
119
+ },
120
+ "pipeline": {
121
+ "total_jobs_processed": 0,
122
+ "completed_jobs": 0,
123
+ "failed_jobs": 0,
124
+ "processing_jobs": 0,
125
+ "success_rate": 1.0
126
+ },
127
+ "models": {
128
+ "total_registered": 6,
129
+ "performance": {
130
+ "bio_clinical_bert": {
131
+ "version": "1.0.0",
132
+ "total_inferences": 0,
133
+ "avg_latency_ms": 125.4,
134
+ "error_rate": 0.01,
135
+ "last_used": "2025-10-29T15:00:00Z"
136
+ }
137
+ }
138
+ },
139
+ "synthesis": {
140
+ "total_syntheses": 0,
141
+ "avg_confidence": 0.87,
142
+ "requiring_review": 0,
143
+ "avg_processing_time_ms": 850.5
144
+ },
145
+ "cache": {
146
+ "total_entries": stats["cache"]["total_entries"],
147
+ "hits": stats["cache"]["hits"],
148
+ "misses": stats["cache"]["misses"],
149
+ "hit_rate": stats["cache"]["hit_rate"],
150
+ "evictions": 0,
151
+ "memory_usage_mb": stats["cache"]["memory_usage_mb"],
152
+ "avg_retrieval_time_ms": 0.5,
153
+ "cache_efficiency": stats["cache"]["hit_rate"] * 100
154
+ },
155
+ "alerts": {
156
+ "active_count": 0,
157
+ "critical_count": 0,
158
+ "recent": []
159
+ },
160
+ "compliance": {
161
+ "hipaa_compliant": True,
162
+ "gdpr_compliant": True,
163
+ "audit_logging_active": True,
164
+ "phi_removal_active": True,
165
+ "encryption_enabled": True
166
+ },
167
+ "components": {
168
+ "monitoring_system": "operational",
169
+ "versioning_system": "operational",
170
+ "logging_system": "operational",
171
+ "compliance_reporting": "operational",
172
+ "cache_service": "operational"
173
+ }
174
+ }
175
+
176
+ @app.get("/admin/cache/statistics")
177
+ async def cache_statistics():
178
+ stats = monitoring.get_stats()
179
+ return {
180
+ "statistics": stats["cache"],
181
+ "recommendations": ["Cache performing within normal parameters."],
182
+ "timestamp": datetime.utcnow().isoformat()
183
+ }
184
+
185
+ @app.get("/admin/metrics")
186
+ async def admin_metrics():
187
+ stats = monitoring.get_stats()
188
+ return {
189
+ "system": stats,
190
+ "timestamp": datetime.utcnow().isoformat()
191
+ }
192
+
193
+ if __name__ == "__main__":
194
+ import uvicorn
195
+ uvicorn.run(app, host="0.0.0.0", port=7860, log_level="warning")