Refactor dashboard data serialization and mock context for improved clarity
Some checks failed
CI / ci (push) Has been cancelled

- Introduced `serialize_audit_for_response` and `serialize_current_cluster_for_template` functions to handle JSON serialization of audit and cluster data, enhancing data consistency for API responses and template rendering.
- Updated `get_mock_context` in `mock_data.py` to utilize the new serialization functions, simplifying the mock data structure and improving readability.
- Refactored `collect_context` and `collect_audits` in `views.py` to leverage the new serialization methods, ensuring a cleaner and more maintainable codebase.
- Added unit tests for the new serialization functions to ensure correctness and reliability of data formatting.
This commit is contained in:
2026-02-12 20:10:09 +03:00
parent 76eae52d2a
commit 656a6bfac4
8 changed files with 313 additions and 90 deletions

View File

@@ -1,6 +1,9 @@
"""Mock context for dashboard when USE_MOCK_DATA is enabled (no OpenStack/Prometheus)."""
import json
from dashboard.serializers import (
serialize_audit_for_response,
serialize_current_cluster_for_template,
)
def get_mock_context():
@@ -38,7 +41,7 @@ def get_mock_context():
cpu_current = [45.2, 38.1, 52.0, 41.3, 29.8, 32.1, 36.4, 29.2, 42.2, 41.3, 28.3, 33.3]
cpu_projected = [42.0, 40.0, 48.0, 44.0, 35.0, 46.0, 43.0, 43.0, 44.0, 48.0, 47.0, 49.0]
audits = [
audits_raw = [
{
"id": "mock-audit-uuid-1",
"name": "Mock audit (balanced)",
@@ -49,20 +52,18 @@ def get_mock_context():
"scope": "Full Cluster",
"cpu_weight": "1.0",
"ram_weight": "1.0",
"migrations": json.dumps(
[
{
"instanceName": "instance-1",
"source": "compute-0",
"destination": "compute-3",
"flavor": "m1.small",
"impact": "Low",
}
]
),
"host_labels": json.dumps(host_labels),
"cpu_current": json.dumps(cpu_current),
"cpu_projected": json.dumps(cpu_projected),
"migrations": [
{
"instanceName": "instance-1",
"source": "compute-0",
"destination": "compute-3",
"flavor": "m1.small",
"impact": "Low",
}
],
"host_labels": host_labels,
"cpu_current": cpu_current,
"cpu_projected": cpu_projected,
},
{
"id": "mock-audit-uuid-2",
@@ -74,12 +75,13 @@ def get_mock_context():
"scope": "Full Cluster",
"cpu_weight": "1.0",
"ram_weight": "1.0",
"migrations": json.dumps([]),
"host_labels": json.dumps(host_labels),
"cpu_current": json.dumps(cpu_current),
"cpu_projected": json.dumps([40.0, 42.0, 50.0, 43.0, 36.0, 45.0]),
"migrations": [],
"host_labels": host_labels,
"cpu_current": cpu_current,
"cpu_projected": [40.0, 42.0, 50.0, 43.0, 36.0, 45.0],
},
]
audits = [serialize_audit_for_response(a) for a in audits_raw]
return {
"region": {
@@ -128,8 +130,7 @@ def get_mock_context():
"third_common_flavor": {"name": "m1.large", "count": 4},
},
"audits": audits,
"current_cluster": {
"host_labels": json.dumps(host_labels),
"cpu_current": json.dumps(cpu_current),
},
"current_cluster": serialize_current_cluster_for_template(
{"host_labels": host_labels, "cpu_current": cpu_current}
),
}