This commit is contained in:
@@ -4,6 +4,7 @@ from datetime import timedelta
|
||||
from typing import Any
|
||||
|
||||
from django.conf import settings
|
||||
from django.core.cache import cache
|
||||
from django.db.models import Count, Q, Sum
|
||||
from django.utils import timezone
|
||||
|
||||
@@ -14,6 +15,32 @@ from apps.profiles.models import SearchProfile
|
||||
from apps.sources.models import Source, SourceRun
|
||||
|
||||
from ..health import readiness
|
||||
from ..tasks import RUNTIME_HEARTBEAT_KEY
|
||||
|
||||
|
||||
def _runtime_heartbeat_state() -> dict[str, Any]:
|
||||
if settings.CELERY_TASK_ALWAYS_EAGER:
|
||||
return {"label": "Eager/lokaal", "healthy": True, "observed_at": None}
|
||||
|
||||
raw_timestamp = cache.get(RUNTIME_HEARTBEAT_KEY)
|
||||
if not raw_timestamp:
|
||||
return {"label": "Geen recente heartbeat", "healthy": False, "observed_at": None}
|
||||
|
||||
try:
|
||||
observed_at = timezone.datetime.fromisoformat(str(raw_timestamp))
|
||||
if timezone.is_naive(observed_at):
|
||||
observed_at = timezone.make_aware(observed_at)
|
||||
except (TypeError, ValueError):
|
||||
return {"label": "Ongeldige heartbeat", "healthy": False, "observed_at": None}
|
||||
|
||||
age_seconds = max(0, int((timezone.now() - observed_at).total_seconds()))
|
||||
return {
|
||||
"label": f"Actief · {age_seconds}s geleden"
|
||||
if age_seconds <= 150
|
||||
else "Heartbeat verouderd",
|
||||
"healthy": age_seconds <= 150,
|
||||
"observed_at": observed_at,
|
||||
}
|
||||
|
||||
|
||||
def build_automation_cockpit() -> dict[str, Any]:
|
||||
@@ -84,6 +111,7 @@ def build_automation_cockpit() -> dict[str, Any]:
|
||||
"unit": "verzonden / 24u",
|
||||
},
|
||||
]
|
||||
runtime_heartbeat = _runtime_heartbeat_state()
|
||||
return {
|
||||
"health": readiness(),
|
||||
"source_total": source_total,
|
||||
@@ -103,7 +131,8 @@ def build_automation_cockpit() -> dict[str, Any]:
|
||||
"source_rows": Source.objects.annotate(run_count=Count("runs")).order_by("name")[:12],
|
||||
"queue_state": "Eager/lokaal"
|
||||
if settings.CELERY_TASK_ALWAYS_EAGER
|
||||
else "Geconfigureerd, worker niet gemeten",
|
||||
else "Diepte niet beschikbaar",
|
||||
"runtime_heartbeat": runtime_heartbeat,
|
||||
"ai_state": "Geconfigureerd"
|
||||
if settings.OLLAMA_ENABLED and settings.OLLAMA_MODEL
|
||||
else "Niet geconfigureerd",
|
||||
|
||||
Reference in New Issue
Block a user