feat: Apply default_settings thresholds when not set per-server

- Fallback to default_settings when metric_thresholds not configured
- Auto-create default thresholds when new metric type is added
This commit is contained in:
mirivlad 2026-04-17 15:38:02 +08:00
parent 9ed90ca6dd
commit 1b28012a4c
1 changed files with 38 additions and 0 deletions

View File

@ -89,6 +89,30 @@ 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) {
@ -201,6 +225,20 @@ 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) {
$warningThreshold = $thresholds['warning_threshold'];
$criticalThreshold = $thresholds['critical_threshold'];