Fix query and display metrics

- Remove LIMIT 200000 from SQL - GROUP BY handles aggregation
- Pass displayMetrics to template
- Replace hardcoded filters with visibleMetrics array
- If displayMetrics is set in server settings, show only selected metrics
- Otherwise show default metrics (backward compatibility)
This commit is contained in:
mirivlad 2026-04-26 14:13:36 +08:00
parent 393edebee6
commit c4f3a1388d
2 changed files with 16 additions and 15 deletions

View File

@ -146,24 +146,20 @@ class ServerDetailController extends Model
// Запрос с агрегацией если нужно
if ($groupBy) {
// Оптимизированный запрос с подзапросом и LIMIT для больших периодов
// GROUP BY автоматически агрегирует до нужного количества точек (500 для любого периода)
$sql = "
SELECT
AVG(sm.value) as value,
mn.name,
mn.unit,
DATE_FORMAT(sm.created_at, '{$bucketFormat}') as time_bucket
FROM (
SELECT sm_inner.value, sm_inner.metric_name_id, sm_inner.created_at
FROM server_metrics sm_inner
FORCE INDEX (idx_server_metric_time)
WHERE sm_inner.server_id = :id
AND sm_inner.created_at >= :start_date
AND sm_inner.created_at <= :end_date
LIMIT 200000
) sm
FROM server_metrics sm
FORCE INDEX (idx_server_metric_time)
INNER JOIN metric_names mn ON mn.id = sm.metric_name_id
WHERE 1=1 {$metricsFilter}
WHERE sm.server_id = :id
AND sm.created_at >= :start_date
AND sm.created_at <= :end_date
AND 1=1 {$metricsFilter}
{$groupBy}
ORDER BY time_bucket ASC
";
@ -299,6 +295,7 @@ class ServerDetailController extends Model
'title' => 'Сервер: ' . $server['name'],
'server' => $server,
'metrics' => $groupedMetrics,
'displayMetrics' => $displayMetrics,
'allMetricTypes' => $allMetricTypes,
'existingThresholds' => $existingThresholds,
'allServices' => $allServices,

View File

@ -153,8 +153,9 @@
<div class="row">
{% set visibleMetrics = displayMetrics ?: ['cpu_load', 'ram_used', 'disk_used_root', 'disk_used_home', 'disk_used_boot', 'temp_cpu', 'temp_disk_sda', 'temp_disk_sdb', 'temp_disk_sdc'] %}
{% for metricName, metricData in metrics %}
{% if metricName!="top_cpu_proc" and metricName!="top_ram_proc" and metricName!="disk_used" and not (metricName starts with "disk_used_") and not (metricName starts with "disk_total_gb_") and metricName!="ram_total_gb" and not (metricName starts with "net_in_") and not (metricName starts with "net_out_") and metricName!="network_rx" and metricName!="network_tx" and not (metricName starts with "temp_") %}
{% if metricName in visibleMetrics %}
<div class="col-12 mb-4">
<div class="card">
<div class="card-header">
@ -666,8 +667,9 @@ var diskTotalGB = {
};
// Графики метрик
{% set visibleMetrics = displayMetrics ?: ['cpu_load', 'ram_used', 'disk_used_root', 'disk_used_home', 'disk_used_boot', 'temp_cpu', 'temp_disk_sda', 'temp_disk_sdb', 'temp_disk_sdc'] %}
{% for metricName, metricData in metrics %}
{% if metricName!="top_cpu_proc" and metricName!="top_ram_proc" and metricName!="disk_used" and not (metricName starts with "disk_used_") and not (metricName starts with "disk_total_gb_") and metricName!="ram_total_gb" and not (metricName starts with "net_in_") and not (metricName starts with "net_out_") and metricName!="network_rx" and metricName!="network_tx" and not (metricName starts with "temp_") and metricName!="uptime" %}
{% if metricName in visibleMetrics and metricName != 'uptime' %}
const ctx{{ metricName|replace({'-': '_', '.': '_'}) }} = document.getElementById('chart-{{ metricName }}').getContext('2d');
// Подготовка данных для графика
@ -893,8 +895,9 @@ chart{{ metricName|replace({'-': '_', '.': '_'}) }}.canvas.addEventListener('mou
// Глобальный обработчик mousemove для скрытия тултипов при уходе курсора за пределы canvas
// Глобальный обработчик для скрытия тултипов при уходе курсора за пределы canvas
document.addEventListener('mousemove', function(e) {
{% set visibleMetrics = displayMetrics ?: ['cpu_load', 'ram_used', 'disk_used_root', 'disk_used_home', 'disk_used_boot', 'temp_cpu', 'temp_disk_sda', 'temp_disk_sdb', 'temp_disk_sdc'] %}
{% for metricName, metricData in metrics %}
{% if metricName!="top_cpu_proc" and metricName!="top_ram_proc" and metricName!="disk_used" and not (metricName starts with "disk_used_") and not (metricName starts with "disk_total_gb_") and metricName!="ram_total_gb" and not (metricName starts with "net_in_") and not (metricName starts with "net_out_") and metricName!="network_rx" and metricName!="network_tx" and not (metricName starts with "temp_") and metricName!="uptime" %}
{% if metricName in visibleMetrics and metricName != 'uptime' %}
(function() {
var canvas = chart{{ metricName|replace({'-': '_', '.': '_'}) }}.canvas;
var rect = canvas.getBoundingClientRect();
@ -1114,8 +1117,9 @@ document.addEventListener('mousemove', function(e) {
// Сбросить зум на всех графиках
function resetAllZoom() {
{% set visibleMetrics = displayMetrics ?: ['cpu_load', 'ram_used', 'disk_used_root', 'disk_used_home', 'disk_used_boot', 'temp_cpu', 'temp_disk_sda', 'temp_disk_sdb', 'temp_disk_sdc'] %}
{% for metricName, metricData in metrics %}
{% if metricName!="top_cpu_proc" and metricName!="top_ram_proc" and metricName!="disk_used" and not (metricName starts with "disk_used_") and not (metricName starts with "disk_total_gb_") and metricName!="ram_total_gb" and not (metricName starts with "net_in_") and not (metricName starts with "net_out_") and metricName!="network_rx" and metricName!="network_tx" and not (metricName starts with "temp_") %}
{% if metricName in visibleMetrics and metricName != 'uptime' %}
if (typeof chart{{ metricName|replace({'-': '_', '.': '_'}) }} !== 'undefined') {
chart{{ metricName|replace({'-': '_', '.': '_'}) }}.resetZoom();
}