| @@ 12-67 (lines=56) @@ | ||
| 9 | use Spatie\UptimeMonitor\Models\Enums\UptimeStatus; |
|
| 10 | use Spatie\UptimeMonitor\Notifications\BaseNotification; |
|
| 11 | ||
| 12 | class UptimeCheckFailed extends BaseNotification |
|
| 13 | { |
|
| 14 | /** @var \Spatie\UptimeMonitor\Events\UptimeCheckFailed */ |
|
| 15 | public $event; |
|
| 16 | ||
| 17 | /** |
|
| 18 | * Get the mail representation of the notification. |
|
| 19 | * |
|
| 20 | * @param mixed $notifiable |
|
| 21 | * @return \Illuminate\Notifications\Messages\MailMessage |
|
| 22 | */ |
|
| 23 | public function toMail($notifiable) |
|
| 24 | { |
|
| 25 | $mailMessage = (new MailMessage) |
|
| 26 | ->error() |
|
| 27 | ->subject("The uptime check for {$this->event->monitor->url} failed.") |
|
| 28 | ->line("The uptime check for {$this->event->monitor->url} failed."); |
|
| 29 | ||
| 30 | foreach ($this->getMonitorProperties() as $name => $value) { |
|
| 31 | $mailMessage->line($name.': '.$value); |
|
| 32 | } |
|
| 33 | ||
| 34 | return $mailMessage; |
|
| 35 | } |
|
| 36 | ||
| 37 | public function toSlack($notifiable) |
|
| 38 | { |
|
| 39 | return (new SlackMessage) |
|
| 40 | ->error() |
|
| 41 | ->content("The uptime check for {$this->event->monitor->url} failed.") |
|
| 42 | ->attachment(function (SlackAttachment $attachment) { |
|
| 43 | $attachment->fields($this->getMonitorProperties()); |
|
| 44 | }); |
|
| 45 | } |
|
| 46 | ||
| 47 | public function getMonitorProperties($extraProperties = []): array |
|
| 48 | { |
|
| 49 | $extraProperties = [ |
|
| 50 | 'Failing since' => $this->event->monitor->formattedLastUpdatedStatusChangeDate, |
|
| 51 | ]; |
|
| 52 | ||
| 53 | return parent::getMonitorProperties($extraProperties); |
|
| 54 | } |
|
| 55 | ||
| 56 | public function isStillRelevant(): bool |
|
| 57 | { |
|
| 58 | return $this->event->monitor->uptime_status == UptimeStatus::DOWN; |
|
| 59 | } |
|
| 60 | ||
| 61 | public function setEvent(MonitorFailedEvent $event) |
|
| 62 | { |
|
| 63 | $this->event = $event; |
|
| 64 | ||
| 65 | return $this; |
|
| 66 | } |
|
| 67 | } |
|
| 68 | ||
| @@ 12-67 (lines=56) @@ | ||
| 9 | use Spatie\UptimeMonitor\Models\Enums\UptimeStatus; |
|
| 10 | use Spatie\UptimeMonitor\Notifications\BaseNotification; |
|
| 11 | ||
| 12 | class UptimeCheckRecovered extends BaseNotification |
|
| 13 | { |
|
| 14 | /** @var \Spatie\UptimeMonitor\Events\UptimeCheckRecovered */ |
|
| 15 | public $event; |
|
| 16 | ||
| 17 | /** |
|
| 18 | * Get the mail representation of the notification. |
|
| 19 | * |
|
| 20 | * @param mixed $notifiable |
|
| 21 | * @return \Illuminate\Notifications\Messages\MailMessage |
|
| 22 | */ |
|
| 23 | public function toMail($notifiable) |
|
| 24 | { |
|
| 25 | $mailMessage = (new MailMessage) |
|
| 26 | ->success() |
|
| 27 | ->subject("{$this->event->monitor->url} has recovered.") |
|
| 28 | ->line("{$this->event->monitor->url} has recovered."); |
|
| 29 | ||
| 30 | foreach ($this->getMonitorProperties() as $name => $value) { |
|
| 31 | $mailMessage->line($name.': '.$value); |
|
| 32 | } |
|
| 33 | ||
| 34 | return $mailMessage; |
|
| 35 | } |
|
| 36 | ||
| 37 | public function toSlack($notifiable) |
|
| 38 | { |
|
| 39 | return (new SlackMessage) |
|
| 40 | ->success() |
|
| 41 | ->content("{$this->event->monitor->url} has recovered.") |
|
| 42 | ->attachment(function (SlackAttachment $attachment) { |
|
| 43 | $attachment->fields($this->getMonitorProperties()); |
|
| 44 | }); |
|
| 45 | } |
|
| 46 | ||
| 47 | public function getMonitorProperties($extraProperties = []): array |
|
| 48 | { |
|
| 49 | $extraProperties = [ |
|
| 50 | 'Online since' => $this->event->monitor->formattedLastUpdatedStatusChangeDate, |
|
| 51 | ]; |
|
| 52 | ||
| 53 | return parent::getMonitorProperties($extraProperties); |
|
| 54 | } |
|
| 55 | ||
| 56 | public function isStillRelevant(): bool |
|
| 57 | { |
|
| 58 | return $this->event->monitor->uptime_status == UptimeStatus::UP; |
|
| 59 | } |
|
| 60 | ||
| 61 | public function setEvent(MonitorRecoveredEvent $event) |
|
| 62 | { |
|
| 63 | $this->event = $event; |
|
| 64 | ||
| 65 | return $this; |
|
| 66 | } |
|
| 67 | } |
|
| 68 | ||