-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcore.py
More file actions
821 lines (739 loc) · 30.2 KB
/
Copy pathcore.py
File metadata and controls
821 lines (739 loc) · 30.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
import json
import logging
import os
import time
from pathlib import Path
from urllib.parse import urlsplit
import psutil
from flask import Response, jsonify, make_response, redirect, render_template, request, send_from_directory, session, url_for
from flask_wtf.csrf import CSRFError
from config import Config
from error_handlers import APIError, INTERNAL_ERROR_MESSAGE, get_request_id, internal_error_payload
from proxy import PROVIDER_DETAILS
from route_helpers import (
apply_cors_headers,
apply_operational_headers,
check_provider,
copy_upstream_response_headers,
login_required,
request_api_key,
stream_upstream_response,
)
from services.auth_service import AuthService
from services.login_attempt_service import LoginAttemptService
from services.metrics_service import MetricsService
from services.proxy_service import ProxyService
from services.redaction import redact_text
from services.resilience_service import ResilienceService
from services.transport_policy import provider_circuit_mode
logger = logging.getLogger(__name__)
TRUE_JSON_VALUES = {"1", "true", "yes", "on"}
FALSE_JSON_VALUES = {"", "0", "false", "no", "off"}
PRIVATE_CACHE_ENDPOINTS = {
"login",
"logout",
"manage_users",
"delete_user",
"rotate_api_key",
"status_page",
"openrouter_dashboard",
"admin_request_metrics",
"dashboard_openrouter_chat_completions",
"dashboard_openrouter_credits",
"list_admin_models",
"disable_admin_model",
"status_updates",
}
def parse_json_bool(value, field_name: str) -> bool:
"""Parse JSON boolean-ish values without treating arbitrary strings as true."""
if value is None:
return False
if isinstance(value, bool):
return value
if isinstance(value, int) and value in {0, 1}:
return bool(value)
if isinstance(value, str):
normalized = value.strip().lower()
if normalized in TRUE_JSON_VALUES:
return True
if normalized in FALSE_JSON_VALUES:
return False
raise APIError(f"{field_name} must be a boolean", status_code=400)
def is_safe_redirect_target(target: str | None) -> bool:
"""Allow only local, absolute-path redirects after login."""
if not target:
return False
parsed = urlsplit(target)
return (
not parsed.scheme
and not parsed.netloc
and target.startswith("/")
and not target.startswith("//")
and "\\" not in target
)
def build_system_metrics(metrics_service: MetricsService) -> dict:
"""Collect system metrics used by the dashboard and SSE stream."""
return {
"cpu_usage": round(psutil.cpu_percent(interval=None), 1),
"memory_usage": round(psutil.virtual_memory().percent, 1),
"start_time": metrics_service.start_time,
"uptime_start_seconds": int(metrics_service.start_time),
}
def build_dashboard_analytics(
metrics_service: MetricsService,
providers: dict,
stats: dict | None = None,
provider_breakdown: list[dict] | None = None,
) -> dict:
"""Assemble dashboard-focused analytics derived from request and provider data."""
stats = stats or metrics_service.get_stats()
provider_breakdown = (
provider_breakdown
if provider_breakdown is not None
else metrics_service.get_provider_breakdown()
)
recent_failures = metrics_service.get_recent_failures(limit=6)
circuits = [
{
**ResilienceService.snapshot(provider),
"mode": provider_circuit_mode(provider),
}
for provider in sorted(providers)
]
circuit_counts = {
state: sum(
1
for circuit in circuits
if circuit["mode"] != "bypassed"
and circuit["state"] == state
)
for state in ("closed", "degraded", "open", "half_open")
}
configured_providers = sum(
1 for details in providers.values()
if details.get("is_configured", details.get("active", False))
)
active_providers = sum(1 for details in providers.values() if details.get("active"))
traffic_series = stats.get("traffic_series", [])
peak_hour = max(traffic_series, key=lambda item: item.get("requests", 0), default=None)
return {
"provider_breakdown": provider_breakdown,
"recent_failures": recent_failures,
"configured_providers": configured_providers,
"active_providers": active_providers,
"inactive_providers": max(configured_providers - active_providers, 0),
"providers_with_traffic": len(provider_breakdown),
"peak_hour": peak_hour,
"circuits": circuits,
"circuit_counts": circuit_counts,
"cost": metrics_service.get_cost_summary(),
}
def require_admin_dashboard_user() -> dict:
"""Return the current admin user or raise a client-safe API error."""
current_user = AuthService.get_current_user()
if not current_user or not current_user.get("is_admin", False):
raise APIError("Only admin users can perform this action", status_code=403)
return current_user
def build_openrouter_dashboard_headers() -> dict:
"""Build provider headers for dashboard BFF requests without exposing keys to browsers."""
api_key = AuthService.get_api_key("openrouter")
if not api_key:
raise APIError("OpenRouter API key is not configured", status_code=500)
headers = {
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json",
"Accept": "application/json",
}
site_url = os.environ.get("OPENROUTER_SITE_URL")
app_name = os.environ.get("OPENROUTER_APP_NAME")
if site_url:
headers["HTTP-Referer"] = site_url
if app_name:
headers["X-OpenRouter-Title"] = app_name
return headers
def apply_private_cache_headers(response: Response) -> Response:
response.headers["Cache-Control"] = "no-store"
response.headers["Pragma"] = "no-cache"
response.headers["Expires"] = "0"
return response
def register_core_routes(app) -> None:
@app.errorhandler(CSRFError)
def handle_csrf_error(error: CSRFError):
"""
Handle CSRF errors, returning JSON if it's an AJAX/JSON request.
"""
error_msg = "CSRF token missing or invalid."
if request.is_json or "application/json" in request.headers.get("Accept", ""):
return jsonify(
{
"error": "csrf_failed",
"message": error_msg,
"request_id": get_request_id(),
}
), 400
return render_template("error.html", error=error_msg), 400
@app.route("/login", methods=["GET", "POST"])
def login():
"""
Handle user login. On POST, authenticate with username+api_key.
On GET, render the login template.
"""
if request.method == "POST":
username = (request.form.get("username") or "").strip()
api_key = request.form.get("api_key") or ""
decision = LoginAttemptService.check(request.remote_addr, username)
if not decision.allowed:
response = make_response(
render_template(
"login.html",
error="Too many login attempts. Try again later.",
),
429,
)
response.headers["Retry-After"] = str(decision.retry_after or 1)
return response
if username and api_key and AuthService.authenticate_user(username, api_key):
LoginAttemptService.record_success(request.remote_addr, username)
next_page = request.args.get("next")
if is_safe_redirect_target(next_page):
return redirect(next_page)
return redirect(url_for("status_page"))
decision = LoginAttemptService.record_failure(request.remote_addr, username)
status_code = 429 if not decision.allowed else 401
error_message = (
"Too many login attempts. Try again later."
if status_code == 429
else "Invalid username or API key"
)
response = make_response(
render_template("login.html", error=error_message),
status_code,
)
if decision.retry_after:
response.headers["Retry-After"] = str(decision.retry_after)
return response
logger.info("Rendering login template")
return render_template(
"login.html",
error=None,
config={
"server_url": Config.SERVER_BASE_URL,
"providers": list(Config.API_BASE_URLS.keys()),
},
)
@app.route("/logout", methods=["GET", "POST"])
def logout():
"""
Handle user logout without allowing navigation requests to mutate state.
"""
if request.method != "POST":
return Response(status=405, headers={"Allow": "POST"})
AuthService.logout()
return redirect(url_for("login"))
@app.route("/users", methods=["GET", "POST"])
@login_required
def manage_users():
"""
User management page. GET returns list of users (JSON or HTML).
POST creates a new user (admin only).
"""
try:
if request.method == "POST":
current_user = AuthService.get_current_user()
if not current_user or not current_user.get("is_admin", False):
raise APIError("Only admin users can create new users", status_code=403)
payload = request.get_json(silent=True) or {}
username = payload.get("username") or request.form.get("username")
is_admin = (
parse_json_bool(payload.get("is_admin"), "is_admin")
if payload
else request.form.get("is_admin") == "on"
)
if not username:
raise APIError("Username is required", status_code=400)
user = AuthService.create_user(username, is_admin)
return jsonify(
{
"status": "success",
"message": "User created successfully",
"user": user,
}
)
users = AuthService.list_users()
if "application/json" in request.headers.get("Accept", ""):
return jsonify({"status": "success", "users": users})
return render_template(
"users.html",
users=users,
current_user=AuthService.get_current_user(),
)
except APIError as error:
status_code = error.status_code
if "application/json" in request.headers.get("Accept", ""):
return jsonify({"status": "error", "message": error.client_message}), status_code
return render_template("error.html", error=error.client_message), status_code
except Exception as error:
logger.error("Error in user management: %s", redact_text(error))
if "application/json" in request.headers.get("Accept", ""):
return jsonify(internal_error_payload()), 500
return render_template(
"500.html",
error=INTERNAL_ERROR_MESSAGE,
request_id=get_request_id(),
), 500
@app.route("/users/<username>", methods=["DELETE"])
@login_required
def delete_user(username: str):
"""
Delete a user by username.
"""
try:
AuthService.delete_user(username)
return jsonify({"message": f"User {username} deleted successfully"})
except APIError as error:
return jsonify({"error": error.client_message}), error.status_code
@app.route("/users/<username>/rotate-key", methods=["POST"])
@login_required
def rotate_api_key(username: str):
"""
Generate a new API key for a given user.
"""
try:
result = AuthService.rotate_api_key(username)
return jsonify(result)
except APIError as error:
return jsonify({"error": error.client_message}), error.status_code
@app.route("/favicon.ico")
def favicon():
"""
Serve favicon
"""
return send_from_directory(
os.path.join(app.root_path, "static"),
"favicon.ico",
mimetype="image/vnd.microsoft.icon",
)
@app.route("/manifest.webmanifest")
def web_manifest():
"""
Serve the PWA web manifest from the app root.
"""
manifest_path = Path(app.root_path) / "static" / "manifest.webmanifest"
response = Response(manifest_path.read_bytes(), mimetype="application/manifest+json")
response.headers["Cache-Control"] = "public, max-age=300"
return response
@app.route("/service-worker.js")
def service_worker():
"""
Serve the PWA service worker from the app root so it can control the whole app.
"""
service_worker_path = Path(app.root_path) / "static" / "service-worker.js"
response = Response(service_worker_path.read_bytes(), mimetype="application/javascript")
response.headers["Service-Worker-Allowed"] = "/"
response.headers["Cache-Control"] = "no-cache"
return response
@app.route("/apple-touch-icon.png")
def apple_touch_icon():
"""
Serve the iOS home screen icon from the app root.
"""
return send_from_directory(
os.path.join(app.root_path, "static", "icons"),
"apple-touch-icon.png",
mimetype="image/png",
)
@app.route("/static/<path:filename>")
def static_files(filename: str):
"""
Serve static files
"""
response = send_from_directory("static", filename)
response.headers.setdefault("Cache-Control", "public, max-age=3600")
return response
@app.before_request
def handle_redirects():
"""
For every request (except login, static, favicon), enforce authentication.
Also handle direct requests to /<provider> endpoints.
"""
if request.method == "OPTIONS":
return None
if request.headers.get("Authorization") or request_api_key():
return None
if request.endpoint in [
"login",
"static_files",
"favicon",
"health_check",
"web_manifest",
"service_worker",
"apple_touch_icon",
] or request.path.startswith("/static/"):
return None
if not AuthService.is_authenticated():
if request.path == "/":
return redirect(url_for("login"))
if request.is_json:
raise APIError("Authentication required", status_code=401)
return redirect(url_for("login", next=request.url))
sanitized_path = request.path.rstrip("/")
if sanitized_path in [f"/{prov}" for prov in app.config["API_BASE_URLS"]]:
return app.view_functions["proxy"](sanitized_path.strip("/"))
return None
@app.after_request
def add_response_headers(response):
"""
Attach CORS and conservative cache headers.
"""
response = apply_operational_headers(response)
response = apply_cors_headers(response)
if request.endpoint in {"static_files", "favicon", "apple_touch_icon"}:
response.headers["Cache-Control"] = "public, max-age=3600"
return response
if request.endpoint == "web_manifest":
response.headers.setdefault("Cache-Control", "public, max-age=300")
return response
if (
request.endpoint in PRIVATE_CACHE_ENDPOINTS
or request.endpoint in {"health_check"}
or request.headers.get("Authorization")
or request_api_key()
or AuthService.is_authenticated()
):
apply_private_cache_headers(response)
return response
@app.route("/health")
@app.route("/healthz")
def health_check():
"""
Health check endpoint.
"""
try:
response = jsonify(
{
"status": "healthy",
"config": {
"host": os.environ.get("SERVER_HOST", Config.DEFAULT_HOST),
"port": int(os.environ.get("SERVER_PORT", Config.DEFAULT_PORT)),
},
}
)
response.headers["Cache-Control"] = "no-store"
return response, 200
except Exception as error:
logger.error("Health check failed: %s", redact_text(error))
return jsonify(internal_error_payload()), 500
@app.route("/")
@login_required
def status_page():
"""
A status page showing available providers, system metrics, etc.
"""
try:
providers = {}
errors = []
metrics_service = MetricsService.get_instance()
system = build_system_metrics(metrics_service)
stats = metrics_service.get_stats()
provider_breakdown = metrics_service.get_provider_breakdown()
provider_stats = {
item["provider"]: item
for item in provider_breakdown
}
users_info = {
"total": AuthService.count_users(),
"active_sessions": len(session.keys()) if session else 1,
"recent_activity": len(metrics_service.get_recent_activity()),
}
for provider, details in PROVIDER_DETAILS.items():
try:
providers[provider] = check_provider(
provider,
details,
app.config,
provider_stats=provider_stats.get(provider, {}),
)
except Exception as error:
logger.error("Failed to check %s: %s", provider, redact_text(error))
errors.append(f"Failed to check {provider}")
providers[provider] = {
"name": provider.upper(),
"active": False,
"is_configured": False,
"status": "error",
"error": "Provider status unavailable",
"requests_24h": 0,
"success_rate": 0,
"error_rate": 0,
"errors": 0,
"avg_latency": 0,
"p95_latency": 0,
"last_request_at": None,
}
recent_activity = metrics_service.get_recent_activity()
analytics = build_dashboard_analytics(
metrics_service,
providers,
stats,
provider_breakdown,
)
if "application/json" in request.headers.get("Accept", ""):
return jsonify(
{
"status": "running",
"system": system,
"stats": stats,
"analytics": analytics,
"users": users_info,
"providers": providers,
"recent_activity": recent_activity,
"errors": errors if errors else None,
"user": AuthService.get_current_user(),
}
)
return render_template(
"operations.html",
system=system,
stats=stats,
analytics=analytics,
users=users_info,
providers=providers,
recent_activity=recent_activity,
errors=errors if errors else None,
user=AuthService.get_current_user(),
)
except Exception as error:
logger.error("Status page error: %s", redact_text(error))
if "application/json" in request.headers.get("Accept", ""):
return jsonify(internal_error_payload()), 500
return render_template(
"500.html",
error=INTERNAL_ERROR_MESSAGE,
request_id=get_request_id(),
), 500
@app.route("/openrouter")
@login_required
def openrouter_dashboard():
"""
OpenRouter dashboard for testing and interacting with OpenRouter models.
"""
try:
return render_template("openrouter.html", user=AuthService.get_current_user())
except Exception as error:
logger.error("OpenRouter dashboard error: %s", redact_text(error))
if "application/json" in request.headers.get("Accept", ""):
return jsonify(internal_error_payload()), 500
return render_template(
"500.html",
error=INTERNAL_ERROR_MESSAGE,
request_id=get_request_id(),
), 500
@app.route("/admin/metrics/requests")
@login_required
def admin_request_metrics():
require_admin_dashboard_user()
try:
limit = max(1, min(int(request.args.get("limit", 100)), 500))
except ValueError:
limit = 100
return jsonify(
{
"requests": MetricsService.get_instance().get_request_records(limit=limit),
}
)
@app.route("/dashboard/openrouter/chat-completions", methods=["POST"])
@login_required
def dashboard_openrouter_chat_completions():
"""
Server-side dashboard proxy for OpenRouter chat completions.
Browser clients authenticate with the Flask session and never receive provider keys.
"""
require_admin_dashboard_user()
payload = request.get_json(silent=True) or {}
if not payload.get("model"):
raise APIError("Model is required", status_code=400)
if not isinstance(payload.get("messages"), list) or not payload["messages"]:
raise APIError("At least one message is required", status_code=400)
start_time = time.time()
is_streaming = bool(payload.get("stream", False))
headers = build_openrouter_dashboard_headers()
if is_streaming:
headers["Accept"] = "text/event-stream"
try:
upstream_response = ProxyService.make_request(
method="POST",
url="https://openrouter.ai/api/v1/chat/completions",
headers=headers,
params=request.args,
data=json.dumps(payload).encode("utf-8"),
api_provider="openrouter",
use_cache=False,
)
MetricsService.get_instance().track_request(
provider="openrouter",
status_code=upstream_response.status_code,
response_time=(time.time() - start_time) * 1000,
)
if isinstance(upstream_response, Response):
return upstream_response
if is_streaming:
return stream_upstream_response(upstream_response)
return Response(
upstream_response.content,
status=upstream_response.status_code,
headers=copy_upstream_response_headers(upstream_response.headers),
content_type=upstream_response.headers.get("Content-Type", "application/json"),
)
except Exception as error:
status_code = error.status_code if isinstance(error, APIError) else 502
MetricsService.get_instance().track_request(
provider="openrouter",
status_code=status_code,
response_time=(time.time() - start_time) * 1000,
)
raise
@app.route("/dashboard/openrouter/credits", methods=["GET"])
@login_required
def dashboard_openrouter_credits():
"""
Server-side OpenRouter credit lookup for the dashboard.
"""
require_admin_dashboard_user()
start_time = time.time()
try:
upstream_response = ProxyService.make_request(
method="GET",
url="https://openrouter.ai/api/v1/key",
headers=build_openrouter_dashboard_headers(),
params=request.args,
data=None,
api_provider="openrouter",
use_cache=False,
)
MetricsService.get_instance().track_request(
provider="openrouter",
status_code=upstream_response.status_code,
response_time=(time.time() - start_time) * 1000,
)
return Response(
upstream_response.content,
status=upstream_response.status_code,
headers=copy_upstream_response_headers(upstream_response.headers),
content_type=upstream_response.headers.get("Content-Type", "application/json"),
)
except Exception as error:
status_code = error.status_code if isinstance(error, APIError) else 502
MetricsService.get_instance().track_request(
provider="openrouter",
status_code=status_code,
response_time=(time.time() - start_time) * 1000,
)
raise
@app.errorhandler(404)
def not_found_error(error):
"""
Handle 404 errors.
"""
if request.path == "/favicon.ico":
return send_from_directory("static", "favicon.ico")
return render_template("404.html", request_id=get_request_id()), 404
@app.errorhandler(500)
def internal_error(error):
"""
Handle 500 errors.
"""
request_id = get_request_id()
logger.error(
"Internal server error request_id=%s type=%s message=%s",
request_id,
type(error).__name__,
redact_text(error),
)
if request.is_json or "application/json" in request.headers.get("Accept", ""):
return jsonify(internal_error_payload()), 500
return render_template(
"500.html",
error=INTERNAL_ERROR_MESSAGE,
request_id=request_id,
), 500
@app.route("/status/updates")
@login_required
def status_updates():
"""
Server-Sent Events endpoint for real-time status updates.
Streams system, stats, and providers info.
"""
def generate_updates():
metrics_service = MetricsService.get_instance()
while True:
current_time = int(time.time())
try:
if current_time % 5 == 0:
system_data = build_system_metrics(metrics_service)
yield f"event: system\ndata: {json.dumps(system_data)}\n\n"
if current_time % 10 == 0:
stats_data = metrics_service.get_stats()
yield f"event: stats\ndata: {json.dumps(stats_data)}\n\n"
if current_time % 3 == 0:
recent_activity = metrics_service.get_recent_activity()
yield f"event: activity\ndata: {json.dumps(recent_activity)}\n\n"
if current_time % 30 == 0:
provider_breakdown = metrics_service.get_provider_breakdown()
provider_stats = {
item["provider"]: item
for item in provider_breakdown
}
providers_info = {}
for provider, details in PROVIDER_DETAILS.items():
try:
providers_info[provider] = check_provider(
provider,
details,
app.config,
provider_stats=provider_stats.get(provider, {}),
)
except Exception as error:
logger.error(
"Error checking provider %s: %s",
provider,
redact_text(error),
)
providers_info[provider] = {
"active": False,
"is_configured": False,
"status": "error",
"error": "Provider status unavailable",
"requests_24h": 0,
"success_rate": 0,
"error_rate": 0,
"errors": 0,
"avg_latency": 0,
"p95_latency": 0,
"last_request_at": None,
}
yield f"event: providers\ndata: {json.dumps(providers_info)}\n\n"
analytics_data = build_dashboard_analytics(
metrics_service,
providers_info,
stats=stats_data,
provider_breakdown=provider_breakdown,
)
yield f"event: analytics\ndata: {json.dumps(analytics_data)}\n\n"
time.sleep(1)
except GeneratorExit:
break
except Exception as error:
logger.error("Error generating status updates: %s", redact_text(error))
yield (
"event: error\ndata: "
f"{json.dumps({'error': INTERNAL_ERROR_MESSAGE})}\n\n"
)
time.sleep(5)
return Response(
generate_updates(),
mimetype="text/event-stream",
headers={
"Cache-Control": "no-cache, no-transform",
"X-Accel-Buffering": "no",
},
)