Add source status API and enhance dashboard with data source checks
- Introduced a new API endpoint `/api/source-status/` to return the status of Prometheus and OpenStack data sources. - Implemented lightweight health check functions for both Prometheus and OpenStack. - Updated the dashboard template to display the status of data sources dynamically. - Added tests for the new API endpoint to ensure correct functionality and response handling. - Configured a cache timeout for source status checks to improve performance.
This commit is contained in:
@@ -5,9 +5,9 @@ from django.conf import settings
|
||||
from django.core.cache import cache
|
||||
from django.http import JsonResponse
|
||||
from django.shortcuts import render
|
||||
from dashboard.openstack_utils.connect import get_connection
|
||||
from dashboard.openstack_utils.connect import check_openstack, get_connection
|
||||
from dashboard.openstack_utils.flavor import get_flavor_list
|
||||
from dashboard.prometheus_utils.query import query_prometheus
|
||||
from dashboard.prometheus_utils.query import check_prometheus, query_prometheus
|
||||
from dashboard.openstack_utils.audits import get_audits, get_current_cluster_cpu
|
||||
from dashboard.mock_data import get_mock_context
|
||||
|
||||
@@ -264,4 +264,24 @@ def api_audits(request):
|
||||
connection = get_connection()
|
||||
current_cluster = get_current_cluster_cpu(connection)
|
||||
cache.set(cache_key_cluster, current_cluster, timeout=cache_ttl)
|
||||
return JsonResponse({"audits": audits, "current_cluster": current_cluster})
|
||||
return JsonResponse({"audits": audits, "current_cluster": current_cluster})
|
||||
|
||||
|
||||
def api_source_status(request):
|
||||
"""Return status of Prometheus and OpenStack data sources (ok / error / mock)."""
|
||||
if getattr(settings, "USE_MOCK_DATA", False):
|
||||
return JsonResponse({
|
||||
"prometheus": {"status": "mock"},
|
||||
"openstack": {"status": "mock"},
|
||||
})
|
||||
|
||||
cache_key = "dashboard_source_status"
|
||||
cache_ttl = getattr(settings, "SOURCE_STATUS_CACHE_TTL", 30)
|
||||
data = cache.get(cache_key)
|
||||
if data is None:
|
||||
data = {
|
||||
"prometheus": check_prometheus(),
|
||||
"openstack": check_openstack(),
|
||||
}
|
||||
cache.set(cache_key, data, timeout=cache_ttl)
|
||||
return JsonResponse(data)
|
||||
Reference in New Issue
Block a user