some fixes to the alerts

This commit is contained in:
Doar Mihai
2026-03-28 13:26:13 +02:00
parent d5f7793337
commit 02d7f07b29
+37 -8
View File
@@ -24,6 +24,11 @@ class CheckAlerts extends Command
$triggered = 0; $triggered = 0;
foreach ($alerts as $alert) { foreach ($alerts as $alert) {
// Skip if triggered recently (within the period to avoid spam)
if ($this->wasRecentlyTriggered($alert)) {
continue;
}
$currentValue = $this->getCurrentValue($alert); $currentValue = $this->getCurrentValue($alert);
$shouldTrigger = $this->evaluate($alert, $currentValue); $shouldTrigger = $this->evaluate($alert, $currentValue);
@@ -40,10 +45,10 @@ class CheckAlerts extends Command
private function getCurrentValue(Alert $alert): float private function getCurrentValue(Alert $alert): float
{ {
$to = Carbon::today(); $to = Carbon::now();
$from = $alert->period === 'hourly' $from = $alert->period === 'hourly'
? Carbon::now()->subHour() ? Carbon::now()->subHour()
: Carbon::yesterday(); : Carbon::today()->startOfDay();
$service = AnalyticsQueryService::for($alert->website, $from, $to); $service = AnalyticsQueryService::for($alert->website, $from, $to);
$summary = $service->summary(); $summary = $service->summary();
@@ -70,13 +75,25 @@ class CheckAlerts extends Command
private function percentageChange(Alert $alert): float private function percentageChange(Alert $alert): float
{ {
$to = Carbon::today(); if ($alert->period === 'hourly') {
$from = Carbon::yesterday(); // Compare current hour with previous hour
$prevFrom = Carbon::yesterday()->subDay(); $currentTo = Carbon::now();
$prevTo = Carbon::yesterday(); $currentFrom = Carbon::now()->subHour();
$prevTo = Carbon::now()->subHour();
$prevFrom = Carbon::now()->subHours(2);
$current = $this->getValueForPeriod($alert, $from, $to); $current = $this->getValueForPeriod($alert, $currentFrom, $currentTo);
$previous = $this->getValueForPeriod($alert, $prevFrom, $prevTo); $previous = $this->getValueForPeriod($alert, $prevFrom, $prevTo);
} else {
// Compare today so far with yesterday same time
$currentTo = Carbon::now();
$currentFrom = Carbon::today()->startOfDay();
$prevTo = Carbon::yesterday()->setTime($currentTo->hour, $currentTo->minute, $currentTo->second);
$prevFrom = Carbon::yesterday()->startOfDay();
$current = $this->getValueForPeriod($alert, $currentFrom, $currentTo);
$previous = $this->getValueForPeriod($alert, $prevFrom, $prevTo);
}
if ($previous == 0) { if ($previous == 0) {
return $current > 0 ? 100 : 0; return $current > 0 ? 100 : 0;
@@ -93,6 +110,18 @@ class CheckAlerts extends Command
return $summary[$alert->metric] ?? 0; return $summary[$alert->metric] ?? 0;
} }
private function wasRecentlyTriggered(Alert $alert): bool
{
if (!$alert->last_triggered_at) {
return false;
}
$cooldown = $alert->period === 'hourly' ? 1 : 24; // hours
$threshold = Carbon::now()->subHours($cooldown);
return $alert->last_triggered_at->isAfter($threshold);
}
private function trigger(Alert $alert, float $currentValue): void private function trigger(Alert $alert, float $currentValue): void
{ {
$conditionLabel = Alert::$conditions[$alert->condition] ?? $alert->condition; $conditionLabel = Alert::$conditions[$alert->condition] ?? $alert->condition;