91 lines
2.8 KiB
PHP
91 lines
2.8 KiB
PHP
<?php
|
|||
|
|
|
||
|
|
namespace App\Notifications;
|
||
|
|
|
||
|
|
use App\Models\HealthCheck;
|
||
|
|
use Illuminate\Bus\Queueable;
|
||
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
||
|
|
use Illuminate\Notifications\Messages\MailMessage;
|
||
|
|
use Illuminate\Notifications\Notification;
|
||
|
|
|
||
|
|
class PerformanceAlert extends Notification implements ShouldQueue
|
||
|
|
{
|
||
|
|
use Queueable;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Create a new notification instance.
|
||
|
|
*/
|
||
|
|
public function __construct(
|
||
|
|
public string $resourceType,
|
||
|
|
public string $resourceName,
|
||
|
|
public int $responseTime,
|
||
|
|
public int $threshold,
|
||
|
|
public ?string $url = null,
|
||
|
|
public ?int $resourceId = null
|
||
|
|
) {}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Get the notification's delivery channels.
|
||
|
|
*
|
||
|
|
* @return array<int, string>
|
||
|
|
*/
|
||
|
|
public function via(object $notifiable): array
|
||
|
|
{
|
||
|
|
return ['mail', 'database'];
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Get the mail representation of the notification.
|
||
|
|
*/
|
||
|
|
public function toMail(object $notifiable): MailMessage
|
||
|
|
{
|
||
|
|
$subject = "Performance Alert: {$this->resourceName}";
|
||
|
|
$percentageOver = round((($this->responseTime - $this->threshold) / $this->threshold) * 100);
|
||
|
|
|
||
|
|
$message = (new MailMessage)
|
||
|
|
->subject($subject)
|
||
|
|
->warning()
|
||
|
|
->line("Performance threshold exceeded for {$this->resourceType}: {$this->resourceName}")
|
||
|
|
->line("Response time ({$this->responseTime}ms) exceeded the threshold of {$this->threshold}ms by {$percentageOver}%.")
|
||
|
|
->line("This could indicate a performance degradation or resource constraint.");
|
||
|
|
|
||
|
|
$message->line("Response Time: {$this->responseTime}ms")
|
||
|
|
->line("Threshold: {$this->threshold}ms")
|
||
|
|
->line("Difference: " . ($this->responseTime - $this->threshold) . "ms");
|
||
|
|
|
||
|
|
if ($this->url) {
|
||
|
|
$message->line("URL: {$this->url}");
|
||
|
|
}
|
||
|
|
|
||
|
|
if ($this->resourceId) {
|
||
|
|
$actionUrl = match($this->resourceType) {
|
||
|
|
'Health Check' => url("/sites/{$this->resourceId}"),
|
||
|
|
default => url('/dashboard'),
|
||
|
|
};
|
||
|
|
$message->action('View Details', $actionUrl);
|
||
|
|
}
|
||
|
|
|
||
|
|
return $message;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Get the array representation of the notification.
|
||
|
|
*
|
||
|
|
* @return array<string, mixed>
|
||
|
|
*/
|
||
|
|
public function toArray(object $notifiable): array
|
||
|
|
{
|
||
|
|
return [
|
||
|
|
'type' => 'performance_alert',
|
||
|
|
'resource_type' => $this->resourceType,
|
||
|
|
'resource_name' => $this->resourceName,
|
||
|
|
'resource_id' => $this->resourceId,
|
||
|
|
'response_time_ms' => $this->responseTime,
|
||
|
|
'threshold_ms' => $this->threshold,
|
||
|
|
'difference_ms' => $this->responseTime - $this->threshold,
|
||
|
|
'percentage_over' => round((($this->responseTime - $this->threshold) / $this->threshold) * 100, 2),
|
||
|
|
'url' => $this->url,
|
||
|
|
];
|
||
|
|
}
|
||
|
|
}
|