This commit is contained in:
Stefanescu Mihai
2026-03-19 19:47:06 +02:00
parent e8d2728d57
commit eb735a9a98
19 changed files with 860 additions and 9 deletions
+35 -3
View File
@@ -12,6 +12,7 @@ class EventProcessor
public function __construct(
private Fingerprinter $fingerprinter,
private PayloadNormalizer $normalizer,
private NotificationDispatcher $notificationDispatcher,
) {}
/**
@@ -24,7 +25,9 @@ class EventProcessor
$fingerprint = $this->fingerprinter->compute($rawPayload);
$now = Carbon::now();
$issue = $this->findOrCreateIssue($project, $data, $fingerprint, $now);
$isNew = false;
$isRegression = false;
$issue = $this->findOrCreateIssue($project, $data, $fingerprint, $now, $isNew, $isRegression);
$event = Event::create([
'issue_id' => $issue->id,
@@ -48,6 +51,8 @@ class EventProcessor
'raw_payload' => $rawPayload,
]);
$this->dispatchNotifications($project, $issue, $isNew, $isRegression);
return $event;
}
@@ -56,17 +61,23 @@ class EventProcessor
array $data,
string $fingerprint,
Carbon $now,
bool &$isNew,
bool &$isRegression,
): Issue {
$issue = Issue::where('project_id', $project->id)
->where('fingerprint', $fingerprint)
->first();
if ($issue) {
$this->updateExistingIssue($issue, $data, $now);
$isRegression = $this->updateExistingIssue($issue, $data, $now);
$isNew = false;
return $issue;
}
$isNew = true;
$isRegression = false;
return $this->createNewIssue($project, $data, $fingerprint, $now);
}
@@ -101,7 +112,7 @@ class EventProcessor
]);
}
private function updateExistingIssue(Issue $issue, array $data, Carbon $now): void
private function updateExistingIssue(Issue $issue, array $data, Carbon $now): bool
{
$wasResolved = $issue->status === 'resolved';
@@ -110,7 +121,11 @@ class EventProcessor
if ($wasResolved) {
$issue->markRegression();
return true;
}
return false;
}
private function buildTitle(?array $exception, ?string $message): string
@@ -147,4 +162,21 @@ class EventProcessor
return $function ? "{$file} in {$function}" : $file;
}
private function dispatchNotifications(
Project $project,
Issue $issue,
bool $isNew,
bool $isRegression,
): void {
if ($isNew) {
$this->notificationDispatcher->onNewIssue($project, $issue);
}
if ($isRegression) {
$this->notificationDispatcher->onRegression($project, $issue);
}
$this->notificationDispatcher->checkSpike($project, $issue);
}
}
+97
View File
@@ -0,0 +1,97 @@
<?php
namespace App\Services;
use App\Models\AlertRule;
use App\Models\Event;
use App\Models\Issue;
use App\Models\Project;
use App\Notifications\EventSpikeNotification;
use App\Notifications\IssueRegressionNotification;
use App\Notifications\NewIssueNotification;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Notification;
class NotificationDispatcher
{
public function onNewIssue(Project $project, Issue $issue): void
{
if (! $this->ruleEnabled($project, 'new_issue')) {
return;
}
$recipients = $this->getRecipients($project);
Notification::send($recipients, new NewIssueNotification($project, $issue));
}
public function onRegression(Project $project, Issue $issue): void
{
if (! $this->ruleEnabled($project, 'regression')) {
return;
}
$recipients = $this->getRecipients($project);
Notification::send($recipients, new IssueRegressionNotification($project, $issue));
}
public function checkSpike(Project $project, Issue $issue): void
{
$rule = $project->alertRules()
->where('type', 'spike')
->where('enabled', true)
->first();
if (! $rule) {
return;
}
$threshold = $rule->getSpikeThreshold();
$windowMinutes = $rule->getSpikeWindowMinutes();
$recentCount = Event::where('issue_id', $issue->id)
->where('created_at', '>=', Carbon::now()->subMinutes($windowMinutes))
->count();
if ($recentCount < $threshold) {
return;
}
// Only send once per spike window — check if we already notified
$alreadyNotified = $project->users()
->first()
?->notifications()
->where('type', EventSpikeNotification::class)
->where('created_at', '>=', Carbon::now()->subMinutes($windowMinutes))
->whereJsonContains('data->issue_id', $issue->id)
->exists();
if ($alreadyNotified) {
return;
}
$recipients = $this->getRecipients($project);
Notification::send(
$recipients,
new EventSpikeNotification($project, $issue, $recentCount, $windowMinutes),
);
}
private function ruleEnabled(Project $project, string $type): bool
{
$rule = $project->alertRules()->where('type', $type)->first();
// Default: enabled if no rule exists (opt-out model)
return $rule === null || $rule->enabled;
}
private function getRecipients(Project $project)
{
return $project->users()
->get()
->filter(function ($user) use ($project) {
$pref = $user->notificationPreferenceFor($project);
return ! $pref->muted;
});
}
}