From b7528a6ef87d93513f8147c9577f1755ebe0f98b Mon Sep 17 00:00:00 2001 From: mirivlad Date: Fri, 17 Apr 2026 15:52:13 +0800 Subject: [PATCH] fix: Revert auto-thresholds, apply defaults only when set - Remove auto-creation of thresholds for new metrics - Only apply defaults when threshold record exists but value is NULL - Delete erroneous alerts for GB/Network metrics --- src/Controllers/Api/MetricsController.php | 63 +++++++++-------------- 1 file changed, 23 insertions(+), 40 deletions(-) diff --git a/src/Controllers/Api/MetricsController.php b/src/Controllers/Api/MetricsController.php index 4065ea1..252793b 100755 --- a/src/Controllers/Api/MetricsController.php +++ b/src/Controllers/Api/MetricsController.php @@ -89,30 +89,6 @@ class MetricsController extends Model $stmt = $this->pdo->prepare("SELECT id FROM metric_names WHERE name = :name"); $stmt->execute([':name' => $metricName]); $metricInfo = $stmt->fetch(); - - // Создаём дефолтные пороги для новой метрики - if ($metricInfo) { - $stmtCheck = $this->pdo->prepare("SELECT id FROM metric_thresholds WHERE server_id = :server_id AND metric_name_id = :metric_id"); - $stmtCheck->execute([':server_id' => $serverId, ':metric_id' => $metricInfo['id']]); - if (!$stmtCheck->fetch()) { - $stmtDefaults = $this->pdo->query("SELECT setting_key, setting_value FROM default_settings"); - $defaults = []; - while ($row = $stmtDefaults->fetch()) { - $defaults[$row['setting_key']] = $row['setting_value']; - } - $stmtInsertThreshold = $this->pdo->prepare(" - INSERT INTO metric_thresholds (server_id, metric_name_id, warning_threshold, critical_threshold, duration) - VALUES (:server_id, :metric_name_id, :warning, :critical, :duration) - "); - $stmtInsertThreshold->execute([ - ':server_id' => $serverId, - ':metric_name_id' => $metricInfo['id'], - ':warning' => $defaults['default_warning_threshold'] ?? 70, - ':critical' => $defaults['default_critical_threshold'] ?? 90, - ':duration' => $defaults['default_duration'] ?? 0 - ]); - } - } } if ($metricInfo) { @@ -225,25 +201,32 @@ class MetricsController extends Model ]); $thresholds = $stmt->fetch(); - // Fallback на default_settings если пороги не заданы - if (!$thresholds || ($thresholds['warning_threshold'] === null && $thresholds['critical_threshold'] === null)) { - $stmtDefaults = $this->pdo->query("SELECT setting_key, setting_value FROM default_settings"); - $defaults = []; - while ($row = $stmtDefaults->fetch()) { - $defaults[$row['setting_key']] = $row['setting_value']; - } - $thresholds = [ - 'warning_threshold' => $defaults['default_warning_threshold'] ?? 70, - 'critical_threshold' => $defaults['default_critical_threshold'] ?? 90, - 'duration' => $defaults['default_duration'] ?? 0 - ]; + // Если пороги не заданы - не проверяем + if (!$thresholds) { + return; } - if ($thresholds) { - $warningThreshold = $thresholds['warning_threshold']; - $criticalThreshold = $thresholds['critical_threshold']; - $duration = (int)($thresholds['duration'] ?? 0); + // Подставляем дефолты для NULL значений + $stmtDefaults = $this->pdo->query("SELECT setting_key, setting_value FROM default_settings"); + $defaults = []; + while ($row = $stmtDefaults->fetch()) { + $defaults[$row['setting_key']] = $row['setting_value']; + } + $warningThreshold = $thresholds['warning_threshold']; + $criticalThreshold = $thresholds['critical_threshold']; + $duration = (int)($thresholds['duration'] ?? ($defaults['default_duration'] ?? 0)); + + // Если warning_threshold не задан - берём из defaults + if ($warningThreshold === null) { + $warningThreshold = $defaults['default_warning_threshold'] ?? null; + } + // Если critical_threshold не задан - берём из defaults + if ($criticalThreshold === null) { + $criticalThreshold = $defaults['default_critical_threshold'] ?? null; + } + + if ($warningThreshold !== null || $criticalThreshold !== null) { $severity = null; $threshold = null; if ($criticalThreshold && $value >= $criticalThreshold) {