phase 4
This commit is contained in:
@@ -0,0 +1,77 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\AlertRule;
|
||||
use App\Models\Project;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\View\View;
|
||||
|
||||
class AlertRuleController extends Controller
|
||||
{
|
||||
public function edit(Request $request, Project $project): View
|
||||
{
|
||||
$this->authorizeProject($request, $project);
|
||||
|
||||
$rules = collect(['new_issue', 'regression', 'spike'])->map(function ($type) use ($project) {
|
||||
return $project->alertRules()->firstOrCreate(
|
||||
['type' => $type],
|
||||
['enabled' => true, 'config' => $type === 'spike' ? ['threshold' => 10, 'window_minutes' => 5] : null],
|
||||
);
|
||||
});
|
||||
|
||||
$preference = $request->user()->notificationPreferenceFor($project);
|
||||
|
||||
return view('projects.alerts', [
|
||||
'project' => $project,
|
||||
'rules' => $rules->keyBy('type'),
|
||||
'preference' => $preference,
|
||||
]);
|
||||
}
|
||||
|
||||
public function update(Request $request, Project $project): RedirectResponse
|
||||
{
|
||||
$this->authorizeProject($request, $project);
|
||||
|
||||
foreach (['new_issue', 'regression', 'spike'] as $type) {
|
||||
$rule = $project->alertRules()->where('type', $type)->first();
|
||||
if ($rule) {
|
||||
$rule->update(['enabled' => $request->boolean("rules.{$type}.enabled")]);
|
||||
|
||||
if ($type === 'spike') {
|
||||
$rule->update([
|
||||
'config' => [
|
||||
'threshold' => max(1, (int) $request->input('rules.spike.threshold', 10)),
|
||||
'window_minutes' => max(1, (int) $request->input('rules.spike.window_minutes', 5)),
|
||||
],
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return back()->with('status', 'Alert rules updated.');
|
||||
}
|
||||
|
||||
public function updatePreference(Request $request, Project $project): RedirectResponse
|
||||
{
|
||||
$this->authorizeProject($request, $project);
|
||||
|
||||
$pref = $request->user()->notificationPreferenceFor($project);
|
||||
$pref->update([
|
||||
'email_enabled' => $request->boolean('email_enabled'),
|
||||
'database_enabled' => $request->boolean('database_enabled'),
|
||||
'muted' => $request->boolean('muted'),
|
||||
]);
|
||||
|
||||
return back()->with('status', 'Notification preferences updated.');
|
||||
}
|
||||
|
||||
private function authorizeProject(Request $request, Project $project): void
|
||||
{
|
||||
abort_unless(
|
||||
$request->user()->projects()->where('projects.id', $project->id)->exists(),
|
||||
403,
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\View\View;
|
||||
|
||||
class NotificationController extends Controller
|
||||
{
|
||||
public function index(Request $request): View
|
||||
{
|
||||
$notifications = $request->user()
|
||||
->notifications()
|
||||
->latest()
|
||||
->cursorPaginate(20);
|
||||
|
||||
return view('notifications.index', compact('notifications'));
|
||||
}
|
||||
|
||||
public function markAsRead(Request $request, string $id): RedirectResponse
|
||||
{
|
||||
$notification = $request->user()->notifications()->findOrFail($id);
|
||||
$notification->markAsRead();
|
||||
|
||||
$data = $notification->data;
|
||||
if (! empty($data['project_id']) && ! empty($data['issue_id'])) {
|
||||
return redirect()->route('projects.issues.show', [$data['project_id'], $data['issue_id']]);
|
||||
}
|
||||
|
||||
return back();
|
||||
}
|
||||
|
||||
public function markAllAsRead(Request $request): RedirectResponse
|
||||
{
|
||||
$request->user()->unreadNotifications->markAsRead();
|
||||
|
||||
return back()->with('status', 'All notifications marked as read.');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Concerns\HasUlids;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
class AlertRule extends Model
|
||||
{
|
||||
use HasUlids;
|
||||
|
||||
protected $fillable = [
|
||||
'project_id',
|
||||
'type',
|
||||
'enabled',
|
||||
'config',
|
||||
];
|
||||
|
||||
protected function casts(): array
|
||||
{
|
||||
return [
|
||||
'enabled' => 'boolean',
|
||||
'config' => 'array',
|
||||
];
|
||||
}
|
||||
|
||||
public function project(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Project::class);
|
||||
}
|
||||
|
||||
public function getSpikeThreshold(): int
|
||||
{
|
||||
return $this->config['threshold'] ?? 10;
|
||||
}
|
||||
|
||||
public function getSpikeWindowMinutes(): int
|
||||
{
|
||||
return $this->config['window_minutes'] ?? 5;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
class NotificationPreference extends Model
|
||||
{
|
||||
protected $fillable = [
|
||||
'user_id',
|
||||
'project_id',
|
||||
'email_enabled',
|
||||
'database_enabled',
|
||||
'muted',
|
||||
];
|
||||
|
||||
protected function casts(): array
|
||||
{
|
||||
return [
|
||||
'email_enabled' => 'boolean',
|
||||
'database_enabled' => 'boolean',
|
||||
'muted' => 'boolean',
|
||||
];
|
||||
}
|
||||
|
||||
public function user(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(User::class);
|
||||
}
|
||||
|
||||
public function project(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Project::class);
|
||||
}
|
||||
}
|
||||
@@ -50,6 +50,11 @@ class Project extends Model
|
||||
return $this->hasMany(Event::class);
|
||||
}
|
||||
|
||||
public function alertRules(): HasMany
|
||||
{
|
||||
return $this->hasMany(AlertRule::class);
|
||||
}
|
||||
|
||||
public function regenerateKeys(): void
|
||||
{
|
||||
$this->update([
|
||||
|
||||
@@ -8,6 +8,7 @@ use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||
use Illuminate\Database\Eloquent\Attributes\Hidden;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Illuminate\Foundation\Auth\User as Authenticatable;
|
||||
use Illuminate\Notifications\Notifiable;
|
||||
|
||||
@@ -37,4 +38,18 @@ class User extends Authenticatable
|
||||
{
|
||||
return $this->projects()->wherePivot('role', 'owner');
|
||||
}
|
||||
|
||||
public function notificationPreferences(): HasMany
|
||||
{
|
||||
return $this->hasMany(NotificationPreference::class);
|
||||
}
|
||||
|
||||
public function notificationPreferenceFor(Project $project): NotificationPreference
|
||||
{
|
||||
return $this->notificationPreferences()
|
||||
->firstOrCreate(
|
||||
['project_id' => $project->id],
|
||||
['email_enabled' => true, 'database_enabled' => true, 'muted' => false],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,63 @@
|
||||
<?php
|
||||
|
||||
namespace App\Notifications;
|
||||
|
||||
use App\Models\Issue;
|
||||
use App\Models\Project;
|
||||
use Illuminate\Bus\Queueable;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use Illuminate\Notifications\Messages\MailMessage;
|
||||
use Illuminate\Notifications\Notification;
|
||||
|
||||
class EventSpikeNotification extends Notification implements ShouldQueue
|
||||
{
|
||||
use Queueable;
|
||||
|
||||
public function __construct(
|
||||
public Project $project,
|
||||
public Issue $issue,
|
||||
public int $eventCount,
|
||||
public int $windowMinutes,
|
||||
) {}
|
||||
|
||||
public function via(object $notifiable): array
|
||||
{
|
||||
$pref = $notifiable->notificationPreferenceFor($this->project);
|
||||
$channels = [];
|
||||
|
||||
if ($pref->database_enabled) {
|
||||
$channels[] = 'database';
|
||||
}
|
||||
|
||||
if ($pref->email_enabled) {
|
||||
$channels[] = 'mail';
|
||||
}
|
||||
|
||||
return $channels;
|
||||
}
|
||||
|
||||
public function toMail(object $notifiable): MailMessage
|
||||
{
|
||||
$url = route('projects.issues.show', [$this->project, $this->issue]);
|
||||
|
||||
return (new MailMessage)
|
||||
->subject("[{$this->project->name}] Spike: {$this->eventCount} events in {$this->windowMinutes}m")
|
||||
->greeting("Event spike in {$this->project->name}")
|
||||
->line("{$this->eventCount} events received in the last {$this->windowMinutes} minutes for:")
|
||||
->line($this->issue->title)
|
||||
->action('View Issue', $url);
|
||||
}
|
||||
|
||||
public function toArray(object $notifiable): array
|
||||
{
|
||||
return [
|
||||
'type' => 'spike',
|
||||
'project_id' => $this->project->id,
|
||||
'project_name' => $this->project->name,
|
||||
'issue_id' => $this->issue->id,
|
||||
'issue_title' => $this->issue->title,
|
||||
'event_count' => $this->eventCount,
|
||||
'window_minutes' => $this->windowMinutes,
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
<?php
|
||||
|
||||
namespace App\Notifications;
|
||||
|
||||
use App\Models\Issue;
|
||||
use App\Models\Project;
|
||||
use Illuminate\Bus\Queueable;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use Illuminate\Notifications\Messages\MailMessage;
|
||||
use Illuminate\Notifications\Notification;
|
||||
|
||||
class IssueRegressionNotification extends Notification implements ShouldQueue
|
||||
{
|
||||
use Queueable;
|
||||
|
||||
public function __construct(
|
||||
public Project $project,
|
||||
public Issue $issue,
|
||||
) {}
|
||||
|
||||
public function via(object $notifiable): array
|
||||
{
|
||||
$pref = $notifiable->notificationPreferenceFor($this->project);
|
||||
$channels = [];
|
||||
|
||||
if ($pref->database_enabled) {
|
||||
$channels[] = 'database';
|
||||
}
|
||||
|
||||
if ($pref->email_enabled) {
|
||||
$channels[] = 'mail';
|
||||
}
|
||||
|
||||
return $channels;
|
||||
}
|
||||
|
||||
public function toMail(object $notifiable): MailMessage
|
||||
{
|
||||
$url = route('projects.issues.show', [$this->project, $this->issue]);
|
||||
|
||||
return (new MailMessage)
|
||||
->subject("[{$this->project->name}] Regression: {$this->issue->title}")
|
||||
->greeting("Issue regressed in {$this->project->name}")
|
||||
->line("A previously resolved issue has reappeared:")
|
||||
->line($this->issue->title)
|
||||
->action('View Issue', $url)
|
||||
->line("Total occurrences: {$this->issue->times_seen}");
|
||||
}
|
||||
|
||||
public function toArray(object $notifiable): array
|
||||
{
|
||||
return [
|
||||
'type' => 'regression',
|
||||
'project_id' => $this->project->id,
|
||||
'project_name' => $this->project->name,
|
||||
'issue_id' => $this->issue->id,
|
||||
'issue_title' => $this->issue->title,
|
||||
'level' => $this->issue->level,
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
<?php
|
||||
|
||||
namespace App\Notifications;
|
||||
|
||||
use App\Models\Issue;
|
||||
use App\Models\Project;
|
||||
use Illuminate\Bus\Queueable;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use Illuminate\Notifications\Messages\MailMessage;
|
||||
use Illuminate\Notifications\Notification;
|
||||
|
||||
class NewIssueNotification extends Notification implements ShouldQueue
|
||||
{
|
||||
use Queueable;
|
||||
|
||||
public function __construct(
|
||||
public Project $project,
|
||||
public Issue $issue,
|
||||
) {}
|
||||
|
||||
public function via(object $notifiable): array
|
||||
{
|
||||
$pref = $notifiable->notificationPreferenceFor($this->project);
|
||||
$channels = [];
|
||||
|
||||
if ($pref->database_enabled) {
|
||||
$channels[] = 'database';
|
||||
}
|
||||
|
||||
if ($pref->email_enabled) {
|
||||
$channels[] = 'mail';
|
||||
}
|
||||
|
||||
return $channels;
|
||||
}
|
||||
|
||||
public function toMail(object $notifiable): MailMessage
|
||||
{
|
||||
$url = route('projects.issues.show', [$this->project, $this->issue]);
|
||||
|
||||
return (new MailMessage)
|
||||
->subject("[{$this->project->name}] New issue: {$this->issue->title}")
|
||||
->greeting("New issue in {$this->project->name}")
|
||||
->line($this->issue->title)
|
||||
->when($this->issue->culprit, fn ($mail) => $mail->line("in {$this->issue->culprit}"))
|
||||
->action('View Issue', $url)
|
||||
->line("Level: {$this->issue->level}");
|
||||
}
|
||||
|
||||
public function toArray(object $notifiable): array
|
||||
{
|
||||
return [
|
||||
'type' => 'new_issue',
|
||||
'project_id' => $this->project->id,
|
||||
'project_name' => $this->project->name,
|
||||
'issue_id' => $this->issue->id,
|
||||
'issue_title' => $this->issue->title,
|
||||
'level' => $this->issue->level,
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user