From 2d3de61fe3a2797623177cfee9b8eb0fb7cb092f Mon Sep 17 00:00:00 2001 From: Richard Muvirimi Date: Fri, 19 Apr 2024 20:03:37 +0200 Subject: [PATCH] Send daily system status messages --- .env.example | 1 + app/Console/Commands/Status.php | 51 +++++++++++++++++++++ app/Console/Kernel.php | 2 + app/Http/Controllers/RatesController.php | 9 +++- app/Mail/StatusMail.php | 57 ++++++++++++++++++++++++ app/Traits/QueriesFaultyRates.php | 20 +++++++++ resources/views/mail/status.blade.php | 30 +++++++++++++ routes/web.php | 3 ++ 8 files changed, 172 insertions(+), 1 deletion(-) create mode 100644 app/Console/Commands/Status.php create mode 100644 app/Mail/StatusMail.php create mode 100644 app/Traits/QueriesFaultyRates.php create mode 100644 resources/views/mail/status.blade.php diff --git a/.env.example b/.env.example index f79100c..17ba7b2 100644 --- a/.env.example +++ b/.env.example @@ -37,6 +37,7 @@ MAIL_PASSWORD=null MAIL_ENCRYPTION=null MAIL_FROM_ADDRESS="hello@example.com" MAIL_FROM_NAME="${APP_NAME}" +MAIL_TO_ADDRESS= AWS_ACCESS_KEY_ID= AWS_SECRET_ACCESS_KEY= diff --git a/app/Console/Commands/Status.php b/app/Console/Commands/Status.php new file mode 100644 index 0000000..c592468 --- /dev/null +++ b/app/Console/Commands/Status.php @@ -0,0 +1,51 @@ +info('Retrieving rates for status...'); + $this->newLine(); + + $rates = $this->getFaultyRates(); + + if ($rates->count() === 0) { + $this->info('No rates have issues, all good!'); + } else { + $this->info('Sending status mail...'); + + Mail::send(new StatusMail($rates)); + + $this->info('Status mail sent!'); + } + + $this->newLine(); + + } +} diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index 73eb91a..9b313c5 100644 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -15,6 +15,8 @@ protected function schedule(Schedule $schedule): void // $schedule->command('inspire')->hourly(); $schedule->command('sanctum:prune-expired --hours=24')->hourly(); $schedule->command('app:scrape')->hourly()->between('8:00', '20:00'); + + $schedule->command('app:status')->daily()->at('06:00'); } /** diff --git a/app/Http/Controllers/RatesController.php b/app/Http/Controllers/RatesController.php index 8af9146..8cd135d 100644 --- a/app/Http/Controllers/RatesController.php +++ b/app/Http/Controllers/RatesController.php @@ -3,7 +3,9 @@ namespace App\Http\Controllers; use App\GraphQL\Queries\InfoQuery; +use App\Mail\StatusMail; use App\Rules\IsBoolean; +use App\Traits\QueriesFaultyRates; use App\Traits\ResolvesRates; use Exception; use Illuminate\Http\JsonResponse; @@ -13,7 +15,7 @@ class RatesController extends Controller { - use ResolvesRates; + use QueriesFaultyRates, ResolvesRates; public function version0(Request $request): JsonResponse { @@ -57,4 +59,9 @@ public function version1(Request $request): JsonResponse ], Response::HTTP_INTERNAL_SERVER_ERROR); } } + + public function status(): StatusMail + { + return new StatusMail($this->getFaultyRates()); + } } diff --git a/app/Mail/StatusMail.php b/app/Mail/StatusMail.php new file mode 100644 index 0000000..672e718 --- /dev/null +++ b/app/Mail/StatusMail.php @@ -0,0 +1,57 @@ +rates->count()), + ); + } + + /** + * Get the message content definition. + */ + public function content(): Content + { + return new Content( + markdown: 'mail.status', + with: ['rates' => $this->rates], + ); + } + + /** + * Get the attachments for the message. + * + * @return array + */ + public function attachments(): array + { + return []; + } +} diff --git a/app/Traits/QueriesFaultyRates.php b/app/Traits/QueriesFaultyRates.php new file mode 100644 index 0000000..d0ab356 --- /dev/null +++ b/app/Traits/QueriesFaultyRates.php @@ -0,0 +1,20 @@ +enabled() + ->whereDate('updated_at', '<', Carbon::now('UTC')->subHours(6)->format(CarbonInterface::DEFAULT_TO_STRING_FORMAT)) + ->where('status_message', '<>', '') + ->where('status', '=', true) + ->get(); + } +} diff --git a/resources/views/mail/status.blade.php b/resources/views/mail/status.blade.php new file mode 100644 index 0000000..ccaa486 --- /dev/null +++ b/resources/views/mail/status.blade.php @@ -0,0 +1,30 @@ +{{--@formatter:off--}} + +# {{ config('app.name') }} Site Scraping Status! + +@if( $rates->count() > 0 ) +### {{ $rates->count() }} rates where not successfully scraped in the last 24 hrs. + + +@foreach ($rates as $index => $rate) +{{ $rate->id }}. [{{ $rate->name }}]({{ $rate->url }}) + - Rate: {{ $rate->rate . $rate->currency_base}} + - Checked: {{ $rate->updated_at->format("M d, H:m") }} ({{ $rate->updated_at->diffForHumans() }}) + - Message: {{ $rate->status_message }} +
+@endforeach +
+@else +### All rates where successfully scraped in the period leading upto the last 6 hrs. +@endif + +Thanks,
+ +{{ config('app.name') }} + + + View In Browser + + +
+{{--@formatter:on--}} diff --git a/routes/web.php b/routes/web.php index ee3977c..7cd0206 100644 --- a/routes/web.php +++ b/routes/web.php @@ -1,5 +1,6 @@