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.
137 lines
4.5 KiB
Python
137 lines
4.5 KiB
Python
"""Mock context for dashboard when USE_MOCK_DATA is enabled (no OpenStack/Prometheus)."""
|
|
|
|
from dashboard.serializers import (
|
|
serialize_audit_for_response,
|
|
serialize_current_cluster_for_template,
|
|
)
|
|
|
|
|
|
def get_mock_context():
|
|
"""Return a context dict with the same structure as collect_context(), render-ready."""
|
|
hosts_total = 6
|
|
pcpu_total = 48
|
|
pcpu_usage = 12.5
|
|
vcpu_allocated = 96
|
|
vcpu_overcommit_max = 2.0
|
|
pram_total = 256 * 1024**3 # 256 GB in bytes
|
|
pram_usage = 120 * 1024**3
|
|
vram_allocated = 192 * 1024**3
|
|
vram_overcommit_max = 1.5
|
|
vm_count = 24
|
|
vm_active = 22
|
|
|
|
vcpu_total = pcpu_total * vcpu_overcommit_max
|
|
vram_total = pram_total * vram_overcommit_max
|
|
|
|
# Two sample audits with serialized fields for JS
|
|
host_labels = [
|
|
"compute-0",
|
|
"compute-1",
|
|
"compute-2",
|
|
"compute-3",
|
|
"compute-4",
|
|
"compute-5",
|
|
"compute-6",
|
|
"compute-7",
|
|
"compute-8",
|
|
"compute-9",
|
|
"compute-10",
|
|
"compute-11",
|
|
]
|
|
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_raw = [
|
|
{
|
|
"id": "mock-audit-uuid-1",
|
|
"name": "Mock audit (balanced)",
|
|
"created_at": "2025-02-01T10:00:00",
|
|
"strategy": "Balanced",
|
|
"goal": "BALANCED",
|
|
"type": "ONESHOT",
|
|
"scope": "Full Cluster",
|
|
"cpu_weight": "1.0",
|
|
"ram_weight": "1.0",
|
|
"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",
|
|
"name": "Mock audit (workload consolidation)",
|
|
"created_at": "2025-02-02T14:30:00",
|
|
"strategy": "Workload consolidation",
|
|
"goal": "WORKLOAD_CONSOLIDATION",
|
|
"type": "ONESHOT",
|
|
"scope": "Full Cluster",
|
|
"cpu_weight": "1.0",
|
|
"ram_weight": "1.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": {
|
|
"name": "mock-region",
|
|
"hosts_total": hosts_total,
|
|
},
|
|
"pcpu": {
|
|
"total": pcpu_total,
|
|
"usage": pcpu_usage,
|
|
"free": pcpu_total - pcpu_usage,
|
|
"used_percentage": pcpu_usage / pcpu_total * 100,
|
|
},
|
|
"vcpu": {
|
|
"total": int(vcpu_total),
|
|
"allocated": vcpu_allocated,
|
|
"free": int(vcpu_total) - vcpu_allocated,
|
|
"allocated_percentage": vcpu_allocated / vcpu_total * 100,
|
|
"overcommit_ratio": vcpu_allocated / pcpu_total,
|
|
"overcommit_max": vcpu_overcommit_max,
|
|
},
|
|
"pram": {
|
|
"total": pram_total,
|
|
"usage": pram_usage,
|
|
"free": pram_total - pram_usage,
|
|
"used_percentage": pram_usage / pram_total * 100,
|
|
},
|
|
"vram": {
|
|
"total": vram_total,
|
|
"allocated": vram_allocated,
|
|
"free": vram_total - vram_allocated,
|
|
"allocated_percentage": vram_allocated / vram_total * 100,
|
|
"overcommit_ratio": vram_allocated / pram_total,
|
|
"overcommit_max": vram_overcommit_max,
|
|
},
|
|
"vm": {
|
|
"count": vm_count,
|
|
"active": vm_active,
|
|
"stopped": vm_count - vm_active,
|
|
"avg_cpu": vcpu_allocated / vm_count if vm_count else 0,
|
|
"avg_ram": vram_allocated / vm_count if vm_count else 0,
|
|
"density": vm_count / hosts_total,
|
|
},
|
|
"flavors": {
|
|
"first_common_flavor": {"name": "m1.small", "count": 12},
|
|
"second_common_flavor": {"name": "m1.medium", "count": 8},
|
|
"third_common_flavor": {"name": "m1.large", "count": 4},
|
|
},
|
|
"audits": audits,
|
|
"current_cluster": serialize_current_cluster_for_template(
|
|
{"host_labels": host_labels, "cpu_current": cpu_current}
|
|
),
|
|
}
|