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
This commit is contained in:
mirivlad 2026-04-17 15:52:13 +08:00
parent 1b28012a4c
commit b7528a6ef8
1 changed files with 23 additions and 40 deletions

View File

@ -89,30 +89,6 @@ class MetricsController extends Model
$stmt = $this->pdo->prepare("SELECT id FROM metric_names WHERE name = :name"); $stmt = $this->pdo->prepare("SELECT id FROM metric_names WHERE name = :name");
$stmt->execute([':name' => $metricName]); $stmt->execute([':name' => $metricName]);
$metricInfo = $stmt->fetch(); $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) { if ($metricInfo) {
@ -225,25 +201,32 @@ class MetricsController extends Model
]); ]);
$thresholds = $stmt->fetch(); $thresholds = $stmt->fetch();
// Fallback на default_settings если пороги не заданы // Если пороги не заданы - не проверяем
if (!$thresholds || ($thresholds['warning_threshold'] === null && $thresholds['critical_threshold'] === null)) { if (!$thresholds) {
$stmtDefaults = $this->pdo->query("SELECT setting_key, setting_value FROM default_settings"); return;
$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) { // Подставляем дефолты для NULL значений
$warningThreshold = $thresholds['warning_threshold']; $stmtDefaults = $this->pdo->query("SELECT setting_key, setting_value FROM default_settings");
$criticalThreshold = $thresholds['critical_threshold']; $defaults = [];
$duration = (int)($thresholds['duration'] ?? 0); 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; $severity = null;
$threshold = null; $threshold = null;
if ($criticalThreshold && $value >= $criticalThreshold) { if ($criticalThreshold && $value >= $criticalThreshold) {