From ed59351fa704a3fa330271f713a3428143fd38aa Mon Sep 17 00:00:00 2001 From: edalzell Date: Tue, 18 Apr 2023 15:41:18 -0700 Subject: [PATCH] use new front notification structure --- resources/views/alert_cleared.antlers.html | 158 ++++++++++++++++++++ resources/views/alert_raised.antlers.html | 166 +++++++++++++++++++++ src/Notifications/AbstractAlert.php | 50 +++---- src/Notifications/AlertCleared.php | 26 ++-- src/Notifications/AlertRaised.php | 22 ++- 5 files changed, 378 insertions(+), 44 deletions(-) create mode 100644 resources/views/alert_cleared.antlers.html create mode 100644 resources/views/alert_raised.antlers.html diff --git a/resources/views/alert_cleared.antlers.html b/resources/views/alert_cleared.antlers.html new file mode 100644 index 0000000..c796443 --- /dev/null +++ b/resources/views/alert_cleared.antlers.html @@ -0,0 +1,158 @@ + + + +  + +    Daily Checkout Report +    + +    +    +    +  + + +  + + + + + + + + +
+ + +
+ + +
+ Green Checkmark +
+ +

+ Error Resolved +

+
+ + +
+ +
+ + + + +

+ Hello again. +

+

+ The previously reported error is now resolved. The details for the resolved check are below: +

+ +
    +
  • Name of Check: {{ test }}
  • +
  • Monitor Type: {{ type }}
  • +
  • Alert Output: {{ output }}
  • +
  • Error Duration: {{ duration }}
  • +
+ + + + +
+
+ + +
+ +
+ Transform Studios +
+
+ +
+ +  + diff --git a/resources/views/alert_raised.antlers.html b/resources/views/alert_raised.antlers.html new file mode 100644 index 0000000..ebd8ccb --- /dev/null +++ b/resources/views/alert_raised.antlers.html @@ -0,0 +1,166 @@ + + + +  + +    Daily Checkout Report +    + +    +    +    +  + + +  + + + + + + + + +
+ + +
+ + +
+ Error Detected + +
+ +

+ Error Detected +

+
+ + +
+ +
+ + + + +

+ Hello {{ name or 'there' }}. 👋 +

+

+ Our global network of monitoring servers has reported an error on your web property. Our 1st priority is prompt resolution when any downtime or error is detected by our systems all while providing you full transparency of our progress on resolution.

This message is to confirm we have started to investigate and will keep you updated on the progress until final resolution. +

+ +
    +
  • Name of Check: {{ test }}
  • +
  • Monitor Type: {{ type }}
  • +
  • Location(s): {{ locations }}
  • +
  • Alert Output: {{ output }}
  • +
+ + + + +

+ + Feel free to reply to this email with any questions, but rest assured we are working on resolution. Stay tuned for further updates... + +

+ + +
+
+ + +
+ +
+ Transform Studios +
+
+ +
+ +  + \ No newline at end of file diff --git a/src/Notifications/AbstractAlert.php b/src/Notifications/AbstractAlert.php index 12945bf..70411c6 100644 --- a/src/Notifications/AbstractAlert.php +++ b/src/Notifications/AbstractAlert.php @@ -2,20 +2,21 @@ namespace TransformStudios\Uptime\Notifications; -use Illuminate\Bus\Queueable; -use Illuminate\Contracts\Queue\ShouldQueue; -use Illuminate\Notifications\Notification; +use Illuminate\Support\Collection; use Statamic\Support\Arr; +use TransformStudios\Front\Notifications\BaseNotification; -abstract class AbstractAlert extends Notification implements ShouldQueue +abstract class AbstractAlert extends BaseNotification { - use Queueable; - - protected string $event; - protected string $subject; - - public function __construct(protected $alert, protected $users) + public function __construct(string $subject, array $payload, string $template, Collection $users) { + parent::__construct( + Arr::get($payload, 'service.id'), + $subject, + view("uptime::$template", $this->data($payload))->render(), + $users, + ); + } /** @@ -29,24 +30,17 @@ public function via($notifiable) return ['front']; } - /** - * Get the array representation of the notification. - * - * @param mixed $notifiable - * @return array - */ - public function toArray(): array + protected function data(array $payload, array $additionalData = []): array { - return [ - 'event' => $this->event, - 'date' => Arr::get($this->alert, 'date'), - 'id' => Arr::get($this->alert, 'service.id'), - 'is_up' => Arr::get($this->alert, 'alert.is_up'), - 'output' => Arr::get($this->alert, 'alert.short_output'), - 'subject' => $this->subject, - 'test' => Arr::get($this->alert, 'service.name'), - 'type' => Arr::get($this->alert, 'service.monitoring_service_type'), - 'users' => $this->users, - ]; + return array_merge( + [ + 'date' => Arr::get($payload, 'date'), + 'is_up' => Arr::get($payload, 'alert.is_up'), + 'output' => Arr::get($payload, 'alert.short_output'), + 'test' => Arr::get($payload, 'service.name'), + 'type' => Arr::get($payload, 'service.monitoring_service_type'), + ], + $additionalData, + ); } } diff --git a/src/Notifications/AlertCleared.php b/src/Notifications/AlertCleared.php index f788f2a..c28ad8b 100644 --- a/src/Notifications/AlertCleared.php +++ b/src/Notifications/AlertCleared.php @@ -3,23 +3,29 @@ namespace TransformStudios\Uptime\Notifications; use Illuminate\Support\Carbon; +use Illuminate\Support\Collection; use Statamic\Support\Arr; class AlertCleared extends AbstractAlert { - protected string $event = 'alert_cleared'; - protected string $subject = 'Monitor Alert: Alert Cleared'; + public function __construct(array $payload, Collection $users) + { + parent::__construct( + subject: 'Monitor Alert: Alert Cleared', + payload: $payload, + template: 'alert_cleared', + users: $users + ); + } - public function toArray(): array + protected function data(array $payload, array $additionalData = []): array { - $startedAt = Carbon::parse(Arr::get($this->alert, 'downtime.started_at')); - $endedAt = Carbon::parse(Arr::get($this->alert, 'downtime.ended_at')); + $startedAt = Carbon::parse(Arr::get($payload, 'downtime.started_at')); + $endedAt = Carbon::parse(Arr::get($payload, 'downtime.ended_at')); - return array_merge( - parent::toArray(), - [ - 'duration' => $endedAt->diffForHumans($startedAt, Carbon::DIFF_ABSOLUTE, false, 3), - ] + return parent::data( + $payload, + ['duration' => $endedAt->diffForHumans($startedAt, Carbon::DIFF_ABSOLUTE, false, 3)] ); } } diff --git a/src/Notifications/AlertRaised.php b/src/Notifications/AlertRaised.php index 72f50a7..bf9ade9 100644 --- a/src/Notifications/AlertRaised.php +++ b/src/Notifications/AlertRaised.php @@ -2,20 +2,30 @@ namespace TransformStudios\Uptime\Notifications; +use Illuminate\Support\Collection; use Statamic\Support\Arr; class AlertRaised extends AbstractAlert { protected string $event = 'alert_raised'; + protected string $subject = 'Monitor Alert: Error Detected'; - public function toArray(): array + public function __construct(array $payload, Collection $users) + { + parent::__construct( + subject: 'Monitor Alert: Error Detected', + payload: $payload, + template: 'alert_raised', + users: $users + ); + } + + protected function data(array $payload, array $additionalData = []): array { - return array_merge( - parent::toArray(), - [ - 'locations' => implode(',', Arr::get($this->alert, 'locations', [])), - ] + return parent::data( + $payload, + ['locations' => implode(',', Arr::get($payload, 'locations', []))] ); } }