Add dashboard statistics module and refactor views for metrics handling
All checks were successful
CI / ci (push) Successful in 14s
All checks were successful
CI / ci (push) Successful in 14s
- Introduced a new `stats.py` module to encapsulate dashboard statistics building and cache key constants. - Refactored `views.py` to utilize the new `build_stats` function for constructing metrics context, improving code organization and readability. - Updated Prometheus query handling to streamline metrics fetching with a new `fetch_dashboard_metrics` function. - Enhanced test cases to reflect changes in metrics fetching and context building, ensuring accurate functionality. - Added new HTML templates for displaying detailed resource allocation and flavor statistics on the dashboard.
This commit is contained in:
@@ -11,444 +11,10 @@
|
||||
{% block content %}
|
||||
<!-- MAIN DASHBOARD -->
|
||||
<div id="dashboard-content" class="p-4 space-y-8" {% if skeleton %}data-dashboard="skeleton"{% endif %}>
|
||||
<!-- QUICK STATS ROW -->
|
||||
<section class="grid grid-cols-1 lg:grid-cols-3 gap-4" aria-label="Quick stats">
|
||||
<!-- CPU Utilization -->
|
||||
<div class="card bg-base-100 shadow-sm hover:shadow transition-shadow" id="statsPcpuCard">
|
||||
<div class="card-body p-5">
|
||||
{% if skeleton %}
|
||||
<div class="flex items-center justify-between mb-3">
|
||||
<div>
|
||||
<h3 class="text-sm font-medium text-base-content/70">CPU Utilization</h3>
|
||||
<div class="text-xs text-base-content/60 mt-0.5 animate-pulse"><span data-stats="pcpu.usage">—</span> / <span data-stats="pcpu.total">—</span> CPU</div>
|
||||
</div>
|
||||
<div class="text-xl font-bold text-primary animate-pulse" data-stats="pcpu.used_percentage">—%</div>
|
||||
</div>
|
||||
<div class="space-y-2">
|
||||
<div class="flex justify-between text-xs">
|
||||
<span class="text-base-content/60">Used</span>
|
||||
<span class="font-medium animate-pulse" data-stats="pcpu.usage_val">—</span>
|
||||
</div>
|
||||
<progress class="progress progress-primary w-full animate-pulse" data-stats="pcpu.progress" value="0" max="100"></progress>
|
||||
<div class="flex justify-between text-xs">
|
||||
<span class="text-base-content/60">Free</span>
|
||||
<span class="font-medium animate-pulse" data-stats="pcpu.free">—</span>
|
||||
</div>
|
||||
</div>
|
||||
{% else %}
|
||||
<div class="flex items-center justify-between mb-3">
|
||||
<div>
|
||||
<h3 class="text-sm font-medium text-base-content/70">CPU Utilization</h3>
|
||||
<div class="text-xs text-base-content/60 mt-0.5">{{ pcpu.usage|floatformat:1 }} / {{ pcpu.total }} CPU</div>
|
||||
</div>
|
||||
<div class="text-xl font-bold text-primary">{{ pcpu.used_percentage|floatformat:1 }}%</div>
|
||||
</div>
|
||||
<div class="space-y-2">
|
||||
<div class="flex justify-between text-xs">
|
||||
<span class="text-base-content/60">Used</span>
|
||||
<span class="font-medium">{{ pcpu.usage|floatformat:1 }} CPU</span>
|
||||
</div>
|
||||
<progress class="progress progress-primary w-full" value="{{ pcpu.used_percentage }}" max="100"></progress>
|
||||
<div class="flex justify-between text-xs">
|
||||
<span class="text-base-content/60">Free</span>
|
||||
<span class="font-medium">{{ pcpu.free }} CPU</span>
|
||||
</div>
|
||||
</div>
|
||||
{% endif %}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- RAM Utilization -->
|
||||
<div class="card bg-base-100 shadow-sm hover:shadow transition-shadow" id="statsPramCard">
|
||||
<div class="card-body p-5">
|
||||
{% if skeleton %}
|
||||
<div class="flex items-center justify-between mb-3">
|
||||
<div>
|
||||
<h3 class="text-sm font-medium text-base-content/70">RAM Utilization</h3>
|
||||
<div class="text-xs text-base-content/60 mt-0.5 animate-pulse"><span data-stats="pram.usage_gb">—</span> / <span data-stats="pram.total_gb">—</span> GB</div>
|
||||
</div>
|
||||
<div class="text-xl font-bold text-secondary animate-pulse" data-stats="pram.used_percentage">—%</div>
|
||||
</div>
|
||||
<div class="space-y-2">
|
||||
<div class="flex justify-between text-xs">
|
||||
<span class="text-base-content/60">Used</span>
|
||||
<span class="font-medium animate-pulse" data-stats="pram.usage_gb_val">—</span>
|
||||
</div>
|
||||
<progress class="progress progress-secondary w-full animate-pulse" data-stats="pram.progress" value="0" max="100"></progress>
|
||||
<div class="flex justify-between text-xs">
|
||||
<span class="text-base-content/60">Free</span>
|
||||
<span class="font-medium animate-pulse" data-stats="pram.free_gb">—</span>
|
||||
</div>
|
||||
</div>
|
||||
{% else %}
|
||||
<div class="flex items-center justify-between mb-3">
|
||||
<div>
|
||||
<h3 class="text-sm font-medium text-base-content/70">RAM Utilization</h3>
|
||||
<div class="text-xs text-base-content/60 mt-0.5">{{ pram.usage|convert_bytes }} / {{ pram.total|convert_bytes }} GB</div>
|
||||
</div>
|
||||
<div class="text-xl font-bold text-secondary">{{ pram.used_percentage|floatformat:1 }}%</div>
|
||||
</div>
|
||||
<div class="space-y-2">
|
||||
<div class="flex justify-between text-xs">
|
||||
<span class="text-base-content/60">Used</span>
|
||||
<span class="font-medium">{{ pram.usage|convert_bytes }} GB</span>
|
||||
</div>
|
||||
<progress class="progress progress-secondary w-full" value="{{ pram.used_percentage }}" max="100"></progress>
|
||||
<div class="flex justify-between text-xs">
|
||||
<span class="text-base-content/60">Free</span>
|
||||
<span class="font-medium">{{ pram.free|convert_bytes }} GB</span>
|
||||
</div>
|
||||
</div>
|
||||
{% endif %}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Instance Summary -->
|
||||
<div class="card bg-base-100 shadow-sm hover:shadow transition-shadow" id="statsVmCard">
|
||||
<div class="card-body p-5">
|
||||
{% if skeleton %}
|
||||
<div class="flex items-center justify-between mb-3">
|
||||
<div>
|
||||
<h3 class="text-sm font-medium text-base-content/70">Instances</h3>
|
||||
<div class="text-xs text-base-content/60 mt-0.5 animate-pulse"><span data-stats="vm.active">—</span> active / <span data-stats="vm.stopped">—</span> stopped</div>
|
||||
</div>
|
||||
<div class="text-xl font-bold text-accent animate-pulse" data-stats="vm.count">—</div>
|
||||
</div>
|
||||
<div class="space-y-3">
|
||||
<div class="flex justify-between items-center text-xs">
|
||||
<div class="flex items-center gap-2">
|
||||
<div class="w-2 h-2 rounded-full bg-success"></div>
|
||||
<span>Most Used Flavor</span>
|
||||
</div>
|
||||
<span class="font-medium animate-pulse" data-stats="flavors.first_name">—</span>
|
||||
</div>
|
||||
<div class="flex justify-between items-center text-xs">
|
||||
<div class="flex items-center gap-2">
|
||||
<div class="w-2 h-2 rounded-full bg-info"></div>
|
||||
<span>Avg. vCPU/VM</span>
|
||||
</div>
|
||||
<span class="font-medium animate-pulse" data-stats="vm.avg_cpu">—</span>
|
||||
</div>
|
||||
<div class="flex justify-between items-center text-xs">
|
||||
<div class="flex items-center gap-2">
|
||||
<div class="w-2 h-2 rounded-full bg-warning"></div>
|
||||
<span>Density</span>
|
||||
</div>
|
||||
<span class="font-medium animate-pulse" data-stats="vm.density">—</span>
|
||||
</div>
|
||||
</div>
|
||||
{% else %}
|
||||
<div class="flex items-center justify-between mb-3">
|
||||
<div>
|
||||
<h3 class="text-sm font-medium text-base-content/70">Instances</h3>
|
||||
<div class="text-xs text-base-content/60 mt-0.5">{{ vm.active }} active / {{ vm.stopped }} stopped</div>
|
||||
</div>
|
||||
<div class="text-xl font-bold text-accent">{{ vm.count }}</div>
|
||||
</div>
|
||||
<div class="space-y-3">
|
||||
<div class="flex justify-between items-center text-xs">
|
||||
<div class="flex items-center gap-2">
|
||||
<div class="w-2 h-2 rounded-full bg-success"></div>
|
||||
<span>Most Used Flavor</span>
|
||||
</div>
|
||||
<span class="font-medium">{{ flavors.first_common_flavor.name }}</span>
|
||||
</div>
|
||||
<div class="flex justify-between items-center text-xs">
|
||||
<div class="flex items-center gap-2">
|
||||
<div class="w-2 h-2 rounded-full bg-info"></div>
|
||||
<span>Avg. vCPU/VM</span>
|
||||
</div>
|
||||
<span class="font-medium">{{ vm.avg_cpu|floatformat:1 }}</span>
|
||||
</div>
|
||||
<div class="flex justify-between items-center text-xs">
|
||||
<div class="flex items-center gap-2">
|
||||
<div class="w-2 h-2 rounded-full bg-warning"></div>
|
||||
<span>Density</span>
|
||||
</div>
|
||||
<span class="font-medium">{{ vm.density|floatformat:1 }}/host</span>
|
||||
</div>
|
||||
</div>
|
||||
{% endif %}
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
<!-- DETAILED OVERVIEW -->
|
||||
<section class="grid grid-cols-1 lg:grid-cols-2 gap-4" aria-label="Resource allocation and flavors">
|
||||
<!-- Resource Allocation -->
|
||||
<div class="card bg-base-100 shadow-sm border-t-gradient-vtb" id="statsAllocationCard">
|
||||
<div class="card-body p-5">
|
||||
<h2 class="text-lg font-semibold mb-4 flex items-center gap-2">
|
||||
<svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
|
||||
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 19v-6a2 2 0 00-2-2H5a2 2 0 00-2 2v6a2 2 0 002 2h2a2 2 0 002-2zm0 0V9a2 2 0 012-2h2a2 2 0 012 2v10m-6 0a2 2 0 002 2h2a2 2 0 002-2m0 0V5a2 2 0 012-2h2a2 2 0 012 2v14a2 2 0 01-2 2h-2a2 2 0 01-2-2z"/>
|
||||
</svg>
|
||||
Resource Allocation
|
||||
</h2>
|
||||
{% if skeleton %}
|
||||
<div class="mb-4">
|
||||
<div class="flex justify-between text-xs mb-1">
|
||||
<span class="text-base-content/70">CPU Allocation</span>
|
||||
<span class="font-medium animate-pulse" data-stats="vcpu.allocated_total">— / — vCPU</span>
|
||||
</div>
|
||||
<div class="flex items-center gap-2">
|
||||
<progress class="progress progress-primary flex-1 animate-pulse" data-stats="vcpu.progress" value="0" max="100"></progress>
|
||||
<span class="text-xs font-medium w-12 text-right animate-pulse" data-stats="vcpu.allocated_percentage">—%</span>
|
||||
</div>
|
||||
<div class="flex justify-between text-xs mt-1 animate-pulse" data-stats="vcpu.overcommit">—</div>
|
||||
</div>
|
||||
<div>
|
||||
<div class="flex justify-between text-xs mb-1">
|
||||
<span class="text-base-content/70">RAM Allocation</span>
|
||||
<span class="font-medium animate-pulse" data-stats="vram.allocated_total">— / — GB</span>
|
||||
</div>
|
||||
<div class="flex items-center gap-2">
|
||||
<progress class="progress progress-secondary flex-1 animate-pulse" data-stats="vram.progress" value="0" max="100"></progress>
|
||||
<span class="text-xs font-medium w-12 text-right animate-pulse" data-stats="vram.allocated_percentage">—%</span>
|
||||
</div>
|
||||
<div class="flex justify-between text-xs mt-1 animate-pulse" data-stats="vram.overcommit">—</div>
|
||||
</div>
|
||||
{% else %}
|
||||
<!-- CPU Allocation -->
|
||||
<div class="mb-4">
|
||||
<div class="flex justify-between text-xs mb-1">
|
||||
<span class="text-base-content/70">CPU Allocation</span>
|
||||
<span class="font-medium">{{ vcpu.allocated }} / {{ vcpu.total }} vCPU</span>
|
||||
</div>
|
||||
<div class="flex items-center gap-2">
|
||||
<progress class="progress progress-primary flex-1" value="{{ vcpu.allocated_percentage }}" max="100"></progress>
|
||||
<span class="text-xs font-medium w-12 text-right">{{ vcpu.allocated_percentage|floatformat:1 }}%</span>
|
||||
</div>
|
||||
<div class="flex justify-between text-xs mt-1">
|
||||
<span class="text-base-content/60">overcommit: {{ vcpu.overcommit_ratio|floatformat:1 }} / {{ vcpu.overcommit_max|floatformat:1 }}</span>
|
||||
<span class="text-base-content/60">{{ vcpu.allocated_percentage|floatformat:1 }}% allocated</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- RAM Allocation -->
|
||||
<div>
|
||||
<div class="flex justify-between text-xs mb-1">
|
||||
<span class="text-base-content/70">RAM Allocation</span>
|
||||
<span class="font-medium">{{ vram.allocated|convert_bytes }} / {{ vram.total|convert_bytes }} GB</span>
|
||||
</div>
|
||||
<div class="flex items-center gap-2">
|
||||
<progress class="progress progress-secondary flex-1" value="{{ vram.allocated_percentage }}" max="100"></progress>
|
||||
<span class="text-xs font-medium w-12 text-right">{{ vram.allocated_percentage|floatformat:1 }}%</span>
|
||||
</div>
|
||||
<div class="flex justify-between text-xs mt-1">
|
||||
<span class="text-base-content/60">overcommit: {{ vram.overcommit_ratio|floatformat:1 }} / {{ vram.overcommit_max|floatformat:1 }}</span>
|
||||
<span class="text-base-content/60">{{ vram.allocated_percentage|floatformat:1 }}% allocated</span>
|
||||
</div>
|
||||
</div>
|
||||
{% endif %}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Flavor Distribution -->
|
||||
<div class="card bg-base-100 shadow-sm border-t-gradient-vtb" id="statsFlavorsCard">
|
||||
<div class="card-body p-5">
|
||||
<h2 class="text-lg font-semibold mb-4 flex items-center gap-2">
|
||||
<svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
|
||||
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M11 3.055A9.001 9.001 0 1020.945 13H11V3.055z"/>
|
||||
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M20.488 9H15V3.512A9.025 9.025 0 0120.488 9z"/>
|
||||
</svg>
|
||||
Top Flavors
|
||||
</h2>
|
||||
{% if skeleton %}
|
||||
<div class="space-y-3">
|
||||
<div class="bg-base-200/50 rounded-lg p-3">
|
||||
<div class="flex justify-between items-center mb-1">
|
||||
<span class="text-sm font-medium animate-pulse" data-stats="flavors.first_name">—</span>
|
||||
<span class="text-xs badge badge-primary animate-pulse" data-stats="flavors.first_count">— instances</span>
|
||||
</div>
|
||||
<div class="flex justify-between text-xs">
|
||||
<span class="text-base-content/60">Share</span>
|
||||
<span class="font-medium animate-pulse" data-stats="flavors.first_share">—%</span>
|
||||
</div>
|
||||
</div>
|
||||
<div class="space-y-2">
|
||||
<div class="flex justify-between items-center text-sm">
|
||||
<div class="flex items-center gap-2">
|
||||
<div class="w-1.5 h-1.5 rounded-full bg-base-content/30"></div>
|
||||
<span class="animate-pulse" data-stats="flavors.second_name">—</span>
|
||||
</div>
|
||||
<span class="text-xs badge badge-outline animate-pulse" data-stats="flavors.second_count">—</span>
|
||||
</div>
|
||||
<div class="flex justify-between items-center text-sm">
|
||||
<div class="flex items-center gap-2">
|
||||
<div class="w-1.5 h-1.5 rounded-full bg-base-content/30"></div>
|
||||
<span class="animate-pulse" data-stats="flavors.third_name">—</span>
|
||||
</div>
|
||||
<span class="text-xs badge badge-outline animate-pulse" data-stats="flavors.third_count">—</span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
{% else %}
|
||||
<div class="space-y-3">
|
||||
<!-- Most Common -->
|
||||
<div class="bg-base-200/50 rounded-lg p-3">
|
||||
<div class="flex justify-between items-center mb-1">
|
||||
<span class="text-sm font-medium">{{ flavors.first_common_flavor.name }}</span>
|
||||
<span class="text-xs badge badge-primary">{{ flavors.first_common_flavor.count }} instances</span>
|
||||
</div>
|
||||
<div class="flex justify-between text-xs">
|
||||
<span class="text-base-content/60">Share</span>
|
||||
<span class="font-medium">{{ flavors.first_common_flavor.count|div:vm.count|mul:100|floatformat:0 }}%</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Other Flavors -->
|
||||
<div class="space-y-2">
|
||||
{% if flavors.second_common_flavor %}
|
||||
<div class="flex justify-between items-center text-sm">
|
||||
<div class="flex items-center gap-2">
|
||||
<div class="w-1.5 h-1.5 rounded-full bg-base-content/30"></div>
|
||||
<span>{{ flavors.second_common_flavor.name }}</span>
|
||||
</div>
|
||||
<span class="text-xs badge badge-outline">{{ flavors.second_common_flavor.count }}</span>
|
||||
</div>
|
||||
{% endif %}
|
||||
|
||||
{% if flavors.third_common_flavor %}
|
||||
<div class="flex justify-between items-center text-sm">
|
||||
<div class="flex items-center gap-2">
|
||||
<div class="w-1.5 h-1.5 rounded-full bg-base-content/30"></div>
|
||||
<span>{{ flavors.third_common_flavor.name }}</span>
|
||||
</div>
|
||||
<span class="text-xs badge badge-outline">{{ flavors.third_common_flavor.count }}</span>
|
||||
</div>
|
||||
{% endif %}
|
||||
</div>
|
||||
</div>
|
||||
{% endif %}
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
<!-- AUDIT CONTROL -->
|
||||
<section aria-label="Audit analysis">
|
||||
<div class="card bg-base-100 shadow-sm" id="auditSection">
|
||||
<div class="card-body p-5">
|
||||
<div class="flex flex-col sm:flex-row sm:items-center justify-between gap-3 mb-4">
|
||||
<div>
|
||||
<h2 class="text-lg font-semibold">Audit Analysis</h2>
|
||||
<div class="text-base text-base-content/60 mt-0.5">Select an audit to analyze resource distribution</div>
|
||||
</div>
|
||||
<div class="flex items-center gap-2">
|
||||
<span class="text-xs text-base-content/60" id="auditsCount">{% if skeleton %}Loading…{% else %}{{ audits|length }} available{% endif %}</span>
|
||||
<div class="dropdown dropdown-end no-print">
|
||||
<label tabindex="0" class="btn btn-xs btn-ghost">
|
||||
<svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
|
||||
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M13 16h-1v-4h-1m1-4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z"/>
|
||||
</svg>
|
||||
Info
|
||||
</label>
|
||||
<div tabindex="0" class="dropdown-content z-[1] card card-compact w-64 p-2 shadow bg-base-100">
|
||||
<div class="card-body">
|
||||
<div class="text-xs space-y-1">
|
||||
<div class="flex justify-between">
|
||||
<span class="text-base-content/60">Strategy:</span>
|
||||
<span id="previewStrategy">Balanced</span>
|
||||
</div>
|
||||
<div class="flex justify-between">
|
||||
<span class="text-base-content/60">Scope:</span>
|
||||
<span id="previewScope">Full Cluster</span>
|
||||
</div>
|
||||
<div class="flex justify-between">
|
||||
<span class="text-base-content/60">CPU Weight:</span>
|
||||
<span id="previewCpu">1.0</span>
|
||||
</div>
|
||||
<div class="flex justify-between">
|
||||
<span class="text-base-content/60">RAM Weight:</span>
|
||||
<span id="previewRam">1.0</span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="flex flex-col md:flex-row gap-3">
|
||||
<select id="auditSelector" class="select select-bordered select-sm flex-1" {% if skeleton %}disabled{% endif %}>
|
||||
{% if skeleton %}
|
||||
<option value="">Loading…</option>
|
||||
{% else %}
|
||||
{% for audit in audits %}
|
||||
<option value="{{ audit.id }}"
|
||||
data-cpu="{{ audit.cpu_weight }}"
|
||||
data-ram="{{ audit.ram_weight }}"
|
||||
data-scope="{{ audit.scope }}"
|
||||
data-strategy="{{ audit.strategy }}"
|
||||
data-goal="{{ audit.goal }}">
|
||||
{{ audit.name }} ({{ audit.created_at|date:"M d" }})
|
||||
</option>
|
||||
{% endfor %}
|
||||
{% endif %}
|
||||
</select>
|
||||
<button type="button" onclick="loadSelectedAudit()" class="btn btn-primary btn-sm gap-2 no-print">
|
||||
<svg class="w-4 h-4 shrink-0" fill="none" stroke="currentColor" viewBox="0 0 24 24" aria-hidden="true">
|
||||
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M13 7l5 5m0 0l-5 5m5-5H6"/>
|
||||
</svg>
|
||||
Load Analysis
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
<!-- ANALYSIS VISUALIZATION -->
|
||||
<section aria-label="CPU distribution chart">
|
||||
<div class="card bg-base-100 shadow-sm">
|
||||
<div class="card-body p-5">
|
||||
<h3 class="text-lg font-semibold mb-4">CPU Distribution (Current vs Projected)</h3>
|
||||
<div class="h-48">
|
||||
<canvas id="cpuDistributionChart"></canvas>
|
||||
</div>
|
||||
<div class="flex items-center justify-center gap-3 mt-3">
|
||||
<div class="flex items-center gap-1 text-xs">
|
||||
<div class="w-3 h-0.5 bg-success"></div>
|
||||
<span class="text-success">Mean: <span id="currentCpuMean">0</span>%</span>
|
||||
</div>
|
||||
<div class="flex items-center gap-1 text-xs">
|
||||
<div class="w-3 h-0.5 bg-error/60"></div>
|
||||
<span class="text-error/60">±0.5σ: <span id="currentCpuStd">0</span>%</span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
<!-- MIGRATION ACTIONS -->
|
||||
<section aria-label="Migration actions">
|
||||
<div class="card bg-base-100 shadow-sm">
|
||||
<div class="card-body p-6">
|
||||
<div class="flex items-center justify-between mb-4">
|
||||
<h3 class="text-lg font-semibold">Migration Actions</h3>
|
||||
<div class="badge badge-neutral badge-sm" id="migrationCount">Select audit</div>
|
||||
</div>
|
||||
|
||||
<div class="overflow-x-auto">
|
||||
<table class="table table-zebra table-sm">
|
||||
<thead>
|
||||
<tr class="bg-base-200">
|
||||
<th class="text-xs font-medium">Instance</th>
|
||||
<th class="text-xs font-medium">Source → Destination</th>
|
||||
<th class="text-xs font-medium">Flavor</th>
|
||||
<th class="text-xs font-medium">Impact</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody id="migrationTableBody" class="text-sm">
|
||||
<tr>
|
||||
<td colspan="4" class="text-center py-6 text-base-content/60">
|
||||
No audit selected. Load an audit to view migration recommendations.
|
||||
</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
{% include "dashboard/_stats_cards.html" %}
|
||||
{% include "dashboard/_allocation_flavors.html" %}
|
||||
{% include "dashboard/_audit_section.html" %}
|
||||
{% include "dashboard/_chart_migrations.html" %}
|
||||
</div>
|
||||
{% endblock %}
|
||||
|
||||
@@ -457,7 +23,7 @@
|
||||
const SKELETON_MODE = {{ skeleton|yesno:"true,false" }};
|
||||
const CURRENT_CLUSTER = {% if current_cluster %}{ "host_labels": {{ current_cluster.host_labels|safe }}, "cpu_current": {{ current_cluster.cpu_current|safe }} }{% else %}null{% endif %};
|
||||
|
||||
let auditData = {
|
||||
window.auditData = {
|
||||
{% if not skeleton %}
|
||||
{% for audit in audits %}
|
||||
"{{ audit.id }}": {
|
||||
@@ -472,430 +38,9 @@
|
||||
{% endfor %}
|
||||
{% endif %}
|
||||
};
|
||||
|
||||
document.getElementById('auditSelector').addEventListener('change', function(e) {
|
||||
const option = this.options[this.selectedIndex];
|
||||
if (!option) return;
|
||||
document.getElementById('previewCpu').textContent = option.dataset.cpu || '1.0';
|
||||
document.getElementById('previewRam').textContent = option.dataset.ram || '1.0';
|
||||
document.getElementById('previewScope').textContent = option.dataset.scope || 'Full Cluster';
|
||||
document.getElementById('previewStrategy').textContent = option.dataset.strategy || 'Balanced';
|
||||
});
|
||||
|
||||
let cpuDistributionChart = null;
|
||||
|
||||
function setStat(key, text) {
|
||||
document.querySelectorAll('[data-stats="' + key + '"]').forEach(function(el) {
|
||||
el.textContent = text;
|
||||
el.classList.remove('animate-pulse');
|
||||
});
|
||||
}
|
||||
function setProgress(key, value) {
|
||||
document.querySelectorAll('[data-stats="' + key + '"]').forEach(function(el) {
|
||||
if (el.tagName === 'PROGRESS') {
|
||||
el.value = value;
|
||||
el.classList.remove('animate-pulse');
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function renderStats(data) {
|
||||
if (!data) return;
|
||||
var el = function(k) { return document.querySelector('[data-stats="' + k + '"]'); };
|
||||
var regionBadge = document.getElementById('regionBadge');
|
||||
if (regionBadge) regionBadge.textContent = data.region && data.region.name ? data.region.name : '—';
|
||||
setStat('pcpu.usage', Number((data.pcpu && data.pcpu.usage) || 0).toFixed(1));
|
||||
setStat('pcpu.total', String((data.pcpu && data.pcpu.total) || 0));
|
||||
setStat('pcpu.used_percentage', Number((data.pcpu && data.pcpu.used_percentage) || 0).toFixed(1) + '%');
|
||||
setStat('pcpu.usage_val', Number((data.pcpu && data.pcpu.usage) || 0).toFixed(1) + ' CPU');
|
||||
setProgress('pcpu.progress', (data.pcpu && data.pcpu.used_percentage) || 0);
|
||||
setStat('pcpu.free', String((data.pcpu && data.pcpu.free) || 0));
|
||||
var pramUsageGb = formatBytes(data.pram && data.pram.usage, 'GB');
|
||||
var pramTotalGb = formatBytes(data.pram && data.pram.total, 'GB');
|
||||
var pramFreeGb = formatBytes(data.pram && data.pram.free, 'GB');
|
||||
setStat('pram.usage_gb', pramUsageGb);
|
||||
setStat('pram.total_gb', pramTotalGb);
|
||||
setStat('pram.used_percentage', Number((data.pram && data.pram.used_percentage) || 0).toFixed(1) + '%');
|
||||
setStat('pram.usage_gb_val', pramUsageGb + ' GB');
|
||||
setProgress('pram.progress', (data.pram && data.pram.used_percentage) || 0);
|
||||
setStat('pram.free_gb', pramFreeGb + ' GB');
|
||||
setStat('vm.active', String(data.vm && data.vm.active));
|
||||
setStat('vm.stopped', String(data.vm && data.vm.stopped));
|
||||
setStat('vm.count', String(data.vm && data.vm.count));
|
||||
setStat('flavors.first_name', data.flavors && data.flavors.first_common_flavor ? data.flavors.first_common_flavor.name : '—');
|
||||
setStat('vm.avg_cpu', Number((data.vm && data.vm.avg_cpu) || 0).toFixed(1));
|
||||
setStat('vm.density', Number((data.vm && data.vm.density) || 0).toFixed(1) + '/host');
|
||||
setStat('vcpu.allocated_total', ((data.vcpu && data.vcpu.allocated) || 0) + ' / ' + ((data.vcpu && data.vcpu.total) || 0) + ' vCPU');
|
||||
setProgress('vcpu.progress', (data.vcpu && data.vcpu.allocated_percentage) || 0);
|
||||
setStat('vcpu.allocated_percentage', Number((data.vcpu && data.vcpu.allocated_percentage) || 0).toFixed(1) + '%');
|
||||
var vcpuOver = el('vcpu.overcommit');
|
||||
if (vcpuOver) {
|
||||
vcpuOver.textContent = 'overcommit: ' + Number((data.vcpu && data.vcpu.overcommit_ratio) || 0).toFixed(1) + ' / ' + Number((data.vcpu && data.vcpu.overcommit_max) || 0).toFixed(1) + ' — ' + Number((data.vcpu && data.vcpu.allocated_percentage) || 0).toFixed(1) + '% allocated';
|
||||
vcpuOver.classList.remove('animate-pulse');
|
||||
}
|
||||
var vramAllocGb = formatBytes(data.vram && data.vram.allocated, 'GB');
|
||||
var vramTotalGb = formatBytes(data.vram && data.vram.total, 'GB');
|
||||
setStat('vram.allocated_total', vramAllocGb + ' / ' + vramTotalGb + ' GB');
|
||||
setProgress('vram.progress', (data.vram && data.vram.allocated_percentage) || 0);
|
||||
setStat('vram.allocated_percentage', Number((data.vram && data.vram.allocated_percentage) || 0).toFixed(1) + '%');
|
||||
var vramOver = el('vram.overcommit');
|
||||
if (vramOver) {
|
||||
vramOver.textContent = 'overcommit: ' + Number((data.vram && data.vram.overcommit_ratio) || 0).toFixed(1) + ' / ' + Number((data.vram && data.vram.overcommit_max) || 0).toFixed(1) + ' — ' + Number((data.vram && data.vram.allocated_percentage) || 0).toFixed(1) + '% allocated';
|
||||
vramOver.classList.remove('animate-pulse');
|
||||
}
|
||||
setStat('flavors.first_count', (data.flavors && data.flavors.first_common_flavor ? data.flavors.first_common_flavor.count : 0) + ' instances');
|
||||
var vmCount = data.vm && data.vm.count ? data.vm.count : 0;
|
||||
var firstCount = data.flavors && data.flavors.first_common_flavor ? data.flavors.first_common_flavor.count : 0;
|
||||
setStat('flavors.first_share', (vmCount ? Math.round(firstCount / vmCount * 100) : 0) + '%');
|
||||
setStat('flavors.second_name', data.flavors && data.flavors.second_common_flavor ? data.flavors.second_common_flavor.name : '—');
|
||||
setStat('flavors.second_count', data.flavors && data.flavors.second_common_flavor ? String(data.flavors.second_common_flavor.count) : '—');
|
||||
setStat('flavors.third_name', data.flavors && data.flavors.third_common_flavor ? data.flavors.third_common_flavor.name : '—');
|
||||
setStat('flavors.third_count', data.flavors && data.flavors.third_common_flavor ? String(data.flavors.third_common_flavor.count) : '—');
|
||||
document.querySelectorAll('[data-stats]').forEach(function(n) { n.classList.remove('animate-pulse'); });
|
||||
}
|
||||
|
||||
function renderAudits(auditsList) {
|
||||
if (!auditsList || !auditsList.length) {
|
||||
var countEl = document.getElementById('auditsCount');
|
||||
if (countEl) countEl.textContent = '0 available';
|
||||
var sel = document.getElementById('auditSelector');
|
||||
if (sel) { sel.disabled = false; sel.innerHTML = '<option value="">No audits</option>'; }
|
||||
return;
|
||||
}
|
||||
auditData = {};
|
||||
auditsList.forEach(function(a) {
|
||||
auditData[a.id] = {
|
||||
name: a.name,
|
||||
migrations: typeof a.migrations === 'string' ? JSON.parse(a.migrations) : a.migrations,
|
||||
hostData: {
|
||||
labels: typeof a.host_labels === 'string' ? JSON.parse(a.host_labels) : a.host_labels,
|
||||
current: typeof a.cpu_current === 'string' ? JSON.parse(a.cpu_current) : a.cpu_current,
|
||||
projected: typeof a.cpu_projected === 'string' ? JSON.parse(a.cpu_projected) : a.cpu_projected
|
||||
}
|
||||
};
|
||||
});
|
||||
var sel = document.getElementById('auditSelector');
|
||||
if (sel) {
|
||||
sel.disabled = false;
|
||||
sel.innerHTML = '';
|
||||
auditsList.forEach(function(audit) {
|
||||
var opt = document.createElement('option');
|
||||
opt.value = audit.id;
|
||||
opt.setAttribute('data-cpu', audit.cpu_weight || '1.0');
|
||||
opt.setAttribute('data-ram', audit.ram_weight || '1.0');
|
||||
opt.setAttribute('data-scope', audit.scope || 'Full Cluster');
|
||||
opt.setAttribute('data-strategy', audit.strategy || 'Balanced');
|
||||
opt.setAttribute('data-goal', audit.goal || '');
|
||||
var dateStr = audit.created_at ? new Date(audit.created_at).toLocaleDateString('en-US', { month: 'short', day: 'numeric' }) : '';
|
||||
opt.textContent = audit.name + ' (' + dateStr + ')';
|
||||
sel.appendChild(opt);
|
||||
});
|
||||
}
|
||||
var countEl = document.getElementById('auditsCount');
|
||||
if (countEl) countEl.textContent = auditsList.length + ' available';
|
||||
if (auditsList.length > 0) {
|
||||
document.getElementById('auditSelector').dispatchEvent(new Event('change'));
|
||||
loadSelectedAudit();
|
||||
}
|
||||
}
|
||||
|
||||
// Load selected audit
|
||||
function loadSelectedAudit() {
|
||||
const auditId = document.getElementById('auditSelector').value;
|
||||
updateMigrationTable(auditId);
|
||||
updateCPUCharts(auditId);
|
||||
}
|
||||
|
||||
// Update migration table
|
||||
function updateMigrationTable(auditId) {
|
||||
const tbody = document.getElementById('migrationTableBody');
|
||||
const migrationCount = document.getElementById('migrationCount');
|
||||
const data = auditData[auditId];
|
||||
|
||||
if (!data || !data.migrations || data.migrations.length === 0) {
|
||||
tbody.innerHTML = `
|
||||
<tr>
|
||||
<td colspan="4" class="text-center py-6 text-base-content/60">
|
||||
No migration actions recommended
|
||||
</td>
|
||||
</tr>
|
||||
`;
|
||||
migrationCount.textContent = '0 actions';
|
||||
return;
|
||||
}
|
||||
|
||||
let html = '';
|
||||
data.migrations.forEach(migration => {
|
||||
const impact = migration.impact || 'Low';
|
||||
const impactClass = {
|
||||
'Low': 'badge-success',
|
||||
'Medium': 'badge-warning',
|
||||
'High': 'badge-error'
|
||||
}[impact] || 'badge-neutral';
|
||||
|
||||
html += `
|
||||
<tr>
|
||||
<td class="font-medium">
|
||||
<div>${migration.instanceName}</div>
|
||||
</td>
|
||||
<td>
|
||||
<div class="flex items-center gap-2">
|
||||
<span class="badge badge-outline badge-xs">${migration.source}</span>
|
||||
<svg class="w-3 h-3 text-base-content/30" fill="none" stroke="currentColor" viewBox="0 0 24 24">
|
||||
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M13 7l5 5m0 0l-5 5m5-5H6"/>
|
||||
</svg>
|
||||
<span class="badge badge-primary badge-outline badge-xs">${migration.destination}</span>
|
||||
</div>
|
||||
</td>
|
||||
<td>
|
||||
<code class="text-xs bg-base-200 px-2 py-1 rounded">${migration.flavor}</code>
|
||||
</td>
|
||||
<td>
|
||||
<span class="badge ${impactClass} badge-xs">${impact}</span>
|
||||
</td>
|
||||
</tr>
|
||||
`;
|
||||
});
|
||||
|
||||
tbody.innerHTML = html;
|
||||
migrationCount.textContent = `${data.migrations.length} action${data.migrations.length !== 1 ? 's' : ''}`;
|
||||
}
|
||||
|
||||
// Update CPU chart (combined current vs projected)
|
||||
function updateCPUCharts(auditId) {
|
||||
const data = auditData[auditId];
|
||||
if (!data || !data.hostData) return;
|
||||
|
||||
const ctx = document.getElementById('cpuDistributionChart').getContext('2d');
|
||||
|
||||
const currentStats = calculateStats(data.hostData.current);
|
||||
|
||||
document.getElementById('currentCpuMean').textContent = currentStats.mean.toFixed(1);
|
||||
document.getElementById('currentCpuStd').textContent = (currentStats.std * 0.5).toFixed(1);
|
||||
|
||||
if (cpuDistributionChart) cpuDistributionChart.destroy();
|
||||
|
||||
const colors = {
|
||||
primary: getCSSVar('--color-primary'),
|
||||
secondary: getCSSVar('--color-secondary'),
|
||||
accent: getCSSVar('--color-accent'),
|
||||
neutral: getCSSVar('--color-neutral'),
|
||||
info: getCSSVar('--color-info'),
|
||||
success: getCSSVar('--color-success'),
|
||||
warning: getCSSVar('--color-warning'),
|
||||
error: getCSSVar('--color-error')
|
||||
};
|
||||
const textColor = getCSSVar('--color-base-content');
|
||||
const gridColor = getCSSVar('--chart-grid-color') || textColor;
|
||||
|
||||
cpuDistributionChart = new Chart(ctx, {
|
||||
type: 'bar',
|
||||
data: {
|
||||
labels: data.hostData.labels,
|
||||
datasets: [
|
||||
{
|
||||
label: 'Current',
|
||||
data: data.hostData.current.slice(),
|
||||
backgroundColor: colors.info + '40',
|
||||
borderColor: colors.info,
|
||||
borderWidth: 1,
|
||||
borderRadius: 3
|
||||
},
|
||||
{
|
||||
label: 'Projected',
|
||||
data: data.hostData.projected.slice(),
|
||||
backgroundColor: colors.warning + '40',
|
||||
borderColor: colors.warning,
|
||||
borderWidth: 1,
|
||||
borderRadius: 3
|
||||
}
|
||||
]
|
||||
},
|
||||
options: {
|
||||
responsive: true,
|
||||
maintainAspectRatio: false,
|
||||
animation: {
|
||||
onComplete: function() {
|
||||
var chart = this;
|
||||
if (typeof chart.getDatasetMeta !== 'function') chart = chart.chart;
|
||||
if (!chart || chart._hidingDataset === undefined) return;
|
||||
var i = chart._hidingDataset;
|
||||
chart.getDatasetMeta(i).hidden = true;
|
||||
chart.data.datasets[i].data = chart._cpuOriginalData[i].slice();
|
||||
delete chart._hidingDataset;
|
||||
chart.update('none');
|
||||
}
|
||||
},
|
||||
plugins: {
|
||||
legend: {
|
||||
display: true,
|
||||
position: 'top',
|
||||
align: 'center',
|
||||
onClick: function(e, legendItem, legend) {
|
||||
const i = legendItem.datasetIndex;
|
||||
const chart = legend.chart;
|
||||
const len = chart.data.labels.length;
|
||||
if (chart.isDatasetVisible(i)) {
|
||||
chart._hidingDataset = i;
|
||||
chart.data.datasets[i].data = Array(len).fill(0);
|
||||
chart.update();
|
||||
} else {
|
||||
chart.data.datasets[i].data = Array(len).fill(0);
|
||||
chart.show(i);
|
||||
chart.update('none');
|
||||
chart.data.datasets[i].data = chart._cpuOriginalData[i].slice();
|
||||
chart.update();
|
||||
}
|
||||
},
|
||||
labels: {
|
||||
usePointStyle: true,
|
||||
pointStyle: 'rect',
|
||||
boxWidth: 14,
|
||||
boxHeight: 14,
|
||||
padding: 12,
|
||||
color: textColor,
|
||||
generateLabels: function(chart) {
|
||||
const datasets = chart.data.datasets;
|
||||
const labelColor = getCSSVar('--color-base-content');
|
||||
return datasets.map(function(ds, i) {
|
||||
return {
|
||||
text: ds.label,
|
||||
fillStyle: ds.borderColor,
|
||||
strokeStyle: ds.borderColor,
|
||||
lineWidth: 1,
|
||||
fontColor: labelColor,
|
||||
color: labelColor,
|
||||
hidden: !chart.isDatasetVisible(i),
|
||||
datasetIndex: i
|
||||
};
|
||||
});
|
||||
}
|
||||
}
|
||||
},
|
||||
tooltip: {
|
||||
callbacks: {
|
||||
label: (ctx) => `${ctx.dataset.label}: ${Number(ctx.parsed.y).toFixed(2)}% CPU`
|
||||
}
|
||||
},
|
||||
annotation: {
|
||||
annotations: {
|
||||
MeanLine: {
|
||||
type: 'line',
|
||||
yMin: currentStats.mean.toFixed(1),
|
||||
yMax: currentStats.mean.toFixed(1),
|
||||
borderColor: colors.success,
|
||||
borderWidth: 2,
|
||||
borderDash: []
|
||||
},
|
||||
upperStdLine: {
|
||||
type: 'line',
|
||||
yMin: (currentStats.mean + currentStats.std * 0.5).toFixed(1),
|
||||
yMax: (currentStats.mean + currentStats.std * 0.5).toFixed(1),
|
||||
borderColor: colors.error,
|
||||
borderWidth: 1,
|
||||
borderDash: [5, 5]
|
||||
},
|
||||
lowerStdLine: {
|
||||
type: 'line',
|
||||
yMin: currentStats.mean > currentStats.std * 0.5 ? (currentStats.mean - currentStats.std * 0.5).toFixed(1) : 0,
|
||||
yMax: currentStats.mean > currentStats.std * 0.5 ? (currentStats.mean - currentStats.std * 0.5).toFixed(1) : 0,
|
||||
borderColor: colors.error,
|
||||
borderWidth: 1,
|
||||
borderDash: [5, 5]
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
scales: {
|
||||
y: {
|
||||
beginAtZero: true,
|
||||
max: 100,
|
||||
grid: { drawBorder: false, color: gridColor },
|
||||
ticks: {
|
||||
color: textColor,
|
||||
callback: value => value + '%'
|
||||
}
|
||||
},
|
||||
x: {
|
||||
grid: { display: false },
|
||||
ticks: {
|
||||
display: false
|
||||
},
|
||||
barPercentage: 1,
|
||||
categoryPercentage: 0.85
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
cpuDistributionChart._cpuOriginalData = [
|
||||
data.hostData.current.slice(),
|
||||
data.hostData.projected.slice()
|
||||
];
|
||||
}
|
||||
|
||||
// Utility functions
|
||||
function calculateStats(data) {
|
||||
const mean = data.reduce((a, b) => a + b, 0) / data.length;
|
||||
const variance = data.reduce((a, b) => a + Math.pow(b - mean, 2), 0) / data.length;
|
||||
const std = Math.sqrt(variance);
|
||||
return { mean, std };
|
||||
}
|
||||
|
||||
document.addEventListener('DOMContentLoaded', function() {
|
||||
if (SKELETON_MODE) {
|
||||
Promise.all([
|
||||
fetch('/api/stats/').then(function(r) { return r.ok ? r.json() : Promise.reject(r); }),
|
||||
fetch('/api/audits/').then(function(r) { return r.ok ? r.json() : Promise.reject(r); })
|
||||
]).then(function(results) {
|
||||
renderStats(results[0]);
|
||||
renderAudits(results[1].audits);
|
||||
if (!results[1].audits || results[1].audits.length === 0) {
|
||||
var cc = results[1].current_cluster;
|
||||
if (cc && cc.host_labels && cc.cpu_current && cc.host_labels.length) {
|
||||
auditData["current"] = {
|
||||
hostData: {
|
||||
labels: cc.host_labels,
|
||||
current: cc.cpu_current,
|
||||
projected: cc.cpu_current
|
||||
}
|
||||
};
|
||||
updateCPUCharts('current');
|
||||
}
|
||||
}
|
||||
}).catch(function(err) {
|
||||
var msg = err.status ? 'Failed to load data (' + err.status + ')' : 'Failed to load data';
|
||||
var countEl = document.getElementById('auditsCount');
|
||||
if (countEl) countEl.textContent = msg;
|
||||
fetch('/api/stats/').then(function(r) { return r.ok ? r.json() : null; }).then(function(d) { if (d) renderStats(d); });
|
||||
fetch('/api/audits/').then(function(r) { return r.ok ? r.json() : null; }).then(function(d) { if (d && d.audits) renderAudits(d.audits); });
|
||||
});
|
||||
} else {
|
||||
var initialAudit = "{% if audits %}{{ audits.0.id }}{% endif %}";
|
||||
if (initialAudit && auditData[initialAudit]) {
|
||||
document.getElementById('auditSelector').dispatchEvent(new Event('change'));
|
||||
loadSelectedAudit();
|
||||
} else if (!initialAudit && CURRENT_CLUSTER && CURRENT_CLUSTER.host_labels && CURRENT_CLUSTER.host_labels.length) {
|
||||
auditData["current"] = {
|
||||
hostData: {
|
||||
labels: CURRENT_CLUSTER.host_labels,
|
||||
current: CURRENT_CLUSTER.cpu_current,
|
||||
projected: CURRENT_CLUSTER.cpu_current
|
||||
}
|
||||
};
|
||||
updateCPUCharts('current');
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
document.addEventListener('themechange', function() {
|
||||
if (cpuDistributionChart) {
|
||||
const auditId = document.getElementById('auditSelector').value;
|
||||
cpuDistributionChart.destroy();
|
||||
cpuDistributionChart = null;
|
||||
if (auditId) updateCPUCharts(auditId);
|
||||
}
|
||||
});
|
||||
var INITIAL_AUDIT_ID = "{% if audits %}{{ audits.0.id }}{% endif %}";
|
||||
</script>
|
||||
<script src="{% static 'js/dashboard.js' %}"></script>
|
||||
{% endblock %}
|
||||
|
||||
{% block css %}
|
||||
@@ -910,4 +55,4 @@
|
||||
@apply px-1.5 py-0.5 text-xs;
|
||||
}
|
||||
</style>
|
||||
{% endblock %}
|
||||
{% endblock %}
|
||||
|
||||
Reference in New Issue
Block a user