98 lines
3.1 KiB
PHP
98 lines
3.1 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 HealthCheckAlert extends Notification implements ShouldQueue
|
|
{
|
|
use Queueable;
|
|
|
|
/**
|
|
* Create a new notification instance.
|
|
*/
|
|
public function __construct(
|
|
public HealthCheck $healthCheck,
|
|
public string $status,
|
|
public ?int $statusCode = null,
|
|
public ?int $responseTime = 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 = $this->status === 'unhealthy'
|
|
? "Health Check Failed: {$this->healthCheck->endpoint}"
|
|
: "Health Check Restored: {$this->healthCheck->endpoint}";
|
|
|
|
$message = (new MailMessage)
|
|
->subject($subject)
|
|
->line("Health check alert for {$this->healthCheck->site->name}");
|
|
|
|
if ($this->status === 'unhealthy') {
|
|
$message->error()
|
|
->line("The health endpoint {$this->healthCheck->endpoint} is reporting as unhealthy.")
|
|
->line("This could indicate an application issue or service degradation.");
|
|
|
|
if ($this->statusCode) {
|
|
$message->line("HTTP Status Code: {$this->statusCode}");
|
|
}
|
|
} else {
|
|
$message->success()
|
|
->line("The health endpoint {$this->healthCheck->endpoint} is now healthy.")
|
|
->line("The service has been restored.");
|
|
|
|
if ($this->statusCode) {
|
|
$message->line("HTTP Status Code: {$this->statusCode}");
|
|
}
|
|
|
|
if ($this->responseTime) {
|
|
$message->line("Response Time: {$this->responseTime}ms");
|
|
}
|
|
}
|
|
|
|
return $message
|
|
->line("Endpoint: {$this->healthCheck->full_url}")
|
|
->line("Checked at: {$this->healthCheck->last_checked_at->format('Y-m-d H:i:s')}")
|
|
->action('View Details', url("/sites/{$this->healthCheck->site_id}"));
|
|
}
|
|
|
|
/**
|
|
* Get the array representation of the notification.
|
|
*
|
|
* @return array<string, mixed>
|
|
*/
|
|
public function toArray(object $notifiable): array
|
|
{
|
|
return [
|
|
'type' => 'health_check',
|
|
'health_check_id' => $this->healthCheck->id,
|
|
'site_id' => $this->healthCheck->site_id,
|
|
'site_name' => $this->healthCheck->site->name,
|
|
'endpoint' => $this->healthCheck->endpoint,
|
|
'full_url' => $this->healthCheck->full_url,
|
|
'status' => $this->status,
|
|
'is_healthy' => $this->healthCheck->is_healthy,
|
|
'status_code' => $this->statusCode,
|
|
'response_time_ms' => $this->responseTime,
|
|
'checked_at' => $this->healthCheck->last_checked_at,
|
|
];
|
|
}
|
|
}
|