some fixes to the alerts
This commit is contained in:
@@ -24,6 +24,11 @@ class CheckAlerts extends Command
|
||||
$triggered = 0;
|
||||
|
||||
foreach ($alerts as $alert) {
|
||||
// Skip if triggered recently (within the period to avoid spam)
|
||||
if ($this->wasRecentlyTriggered($alert)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$currentValue = $this->getCurrentValue($alert);
|
||||
$shouldTrigger = $this->evaluate($alert, $currentValue);
|
||||
|
||||
@@ -40,10 +45,10 @@ class CheckAlerts extends Command
|
||||
|
||||
private function getCurrentValue(Alert $alert): float
|
||||
{
|
||||
$to = Carbon::today();
|
||||
$to = Carbon::now();
|
||||
$from = $alert->period === 'hourly'
|
||||
? Carbon::now()->subHour()
|
||||
: Carbon::yesterday();
|
||||
: Carbon::today()->startOfDay();
|
||||
|
||||
$service = AnalyticsQueryService::for($alert->website, $from, $to);
|
||||
$summary = $service->summary();
|
||||
@@ -70,13 +75,25 @@ class CheckAlerts extends Command
|
||||
|
||||
private function percentageChange(Alert $alert): float
|
||||
{
|
||||
$to = Carbon::today();
|
||||
$from = Carbon::yesterday();
|
||||
$prevFrom = Carbon::yesterday()->subDay();
|
||||
$prevTo = Carbon::yesterday();
|
||||
if ($alert->period === 'hourly') {
|
||||
// Compare current hour with previous hour
|
||||
$currentTo = Carbon::now();
|
||||
$currentFrom = Carbon::now()->subHour();
|
||||
$prevTo = Carbon::now()->subHour();
|
||||
$prevFrom = Carbon::now()->subHours(2);
|
||||
|
||||
$current = $this->getValueForPeriod($alert, $from, $to);
|
||||
$previous = $this->getValueForPeriod($alert, $prevFrom, $prevTo);
|
||||
$current = $this->getValueForPeriod($alert, $currentFrom, $currentTo);
|
||||
$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) {
|
||||
return $current > 0 ? 100 : 0;
|
||||
@@ -93,6 +110,18 @@ class CheckAlerts extends Command
|
||||
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
|
||||
{
|
||||
$conditionLabel = Alert::$conditions[$alert->condition] ?? $alert->condition;
|
||||
|
||||
Reference in New Issue
Block a user