2023-11-16 10:53:37 +00:00
< ? php
namespace App\Notifications\Server ;
use App\Models\Server ;
use App\Notifications\Channels\DiscordChannel ;
use App\Notifications\Channels\EmailChannel ;
use App\Notifications\Channels\TelegramChannel ;
2024-10-01 19:38:12 +00:00
use App\Notifications\Dto\DiscordMessage ;
2024-06-10 20:43:34 +00:00
use Illuminate\Bus\Queueable ;
2023-11-16 10:53:37 +00:00
use Illuminate\Contracts\Queue\ShouldQueue ;
use Illuminate\Notifications\Messages\MailMessage ;
use Illuminate\Notifications\Notification ;
class HighDiskUsage extends Notification implements ShouldQueue
{
use Queueable ;
public $tries = 1 ;
2024-06-10 20:43:34 +00:00
2024-10-22 12:01:36 +00:00
public function __construct ( public Server $server , public int $disk_usage , public int $server_disk_usage_notification_threshold ) {}
2023-11-16 10:53:37 +00:00
public function via ( object $notifiable ) : array
{
$channels = [];
$isEmailEnabled = isEmailEnabled ( $notifiable );
$isDiscordEnabled = data_get ( $notifiable , 'discord_enabled' );
$isTelegramEnabled = data_get ( $notifiable , 'telegram_enabled' );
if ( $isDiscordEnabled ) {
$channels [] = DiscordChannel :: class ;
}
if ( $isEmailEnabled ) {
$channels [] = EmailChannel :: class ;
}
if ( $isTelegramEnabled ) {
$channels [] = TelegramChannel :: class ;
}
2024-06-10 20:43:34 +00:00
2023-11-16 10:53:37 +00:00
return $channels ;
}
public function toMail () : MailMessage
{
2024-07-24 19:11:12 +00:00
$mail = new MailMessage ;
2023-11-16 10:53:37 +00:00
$mail -> subject ( " Coolify: Server ( { $this -> server -> name } ) high disk usage detected! " );
$mail -> view ( 'emails.high-disk-usage' , [
'name' => $this -> server -> name ,
'disk_usage' => $this -> disk_usage ,
2024-10-22 12:01:36 +00:00
'threshold' => $this -> server_disk_usage_notification_threshold ,
2023-11-16 10:53:37 +00:00
]);
2024-06-10 20:43:34 +00:00
2023-11-16 10:53:37 +00:00
return $mail ;
}
2024-10-01 19:38:12 +00:00
public function toDiscord () : DiscordMessage
2023-11-16 10:53:37 +00:00
{
2024-10-01 19:38:12 +00:00
$message = new DiscordMessage (
2024-10-21 20:40:43 +00:00
title : ':cross_mark: High disk usage detected' ,
description : " Server ' { $this -> server -> name } ' high disk usage detected! " ,
2024-10-01 19:38:12 +00:00
color : DiscordMessage :: errorColor (),
);
$message -> addField ( 'Disk usage' , " { $this -> disk_usage } % " );
2024-10-22 12:01:36 +00:00
$message -> addField ( 'Threshold' , " { $this -> server_disk_usage_notification_threshold } % " );
2024-10-21 20:40:43 +00:00
$message -> addField ( 'Tips' , '[Link](https://coolify.io/docs/knowledge-base/server/automated-cleanup)' );
2024-06-10 20:43:34 +00:00
2023-11-16 10:53:37 +00:00
return $message ;
}
2024-06-10 20:43:34 +00:00
2023-11-16 10:53:37 +00:00
public function toTelegram () : array
{
return [
2024-10-22 12:01:36 +00:00
'message' => " Coolify: Server ' { $this -> server -> name } ' high disk usage detected! \n Disk usage: { $this -> disk_usage } %. Threshold: { $this -> server_disk_usage_notification_threshold } %. \n Please cleanup your disk to prevent data-loss. \n Here are some tips: https://coolify.io/docs/knowledge-base/server/automated-cleanup. " ,
2023-11-16 10:53:37 +00:00
];
}
}