-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #41 from richard-muvirimi/development
Send daily system status messages
- Loading branch information
Showing
8 changed files
with
172 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
|
||
namespace App\Console\Commands; | ||
|
||
use App\Mail\StatusMail; | ||
use App\Traits\QueriesFaultyRates; | ||
use Illuminate\Console\Command; | ||
use Illuminate\Support\Facades\Mail; | ||
|
||
class Status extends Command | ||
{ | ||
use QueriesFaultyRates; | ||
|
||
/** | ||
* The name and signature of the console command. | ||
* | ||
* @var string | ||
*/ | ||
protected $signature = 'app:status'; | ||
|
||
/** | ||
* The console command description. | ||
* | ||
* @var string | ||
*/ | ||
protected $description = 'Send Server StatusMail Report'; | ||
|
||
/** | ||
* Execute the console command. | ||
*/ | ||
public function handle(): void | ||
{ | ||
$this->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(); | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
|
||
namespace App\Mail; | ||
|
||
use Illuminate\Bus\Queueable; | ||
use Illuminate\Database\Eloquent\Collection; | ||
use Illuminate\Mail\Mailable; | ||
use Illuminate\Mail\Mailables\Address; | ||
use Illuminate\Mail\Mailables\Attachment; | ||
use Illuminate\Mail\Mailables\Content; | ||
use Illuminate\Mail\Mailables\Envelope; | ||
use Illuminate\Queue\SerializesModels; | ||
|
||
class StatusMail extends Mailable | ||
{ | ||
use Queueable, SerializesModels; | ||
|
||
/** | ||
* Create a new message instance. | ||
*/ | ||
public function __construct(protected Collection $rates) | ||
{ | ||
// | ||
} | ||
|
||
/** | ||
* Get the message envelope. | ||
*/ | ||
public function envelope(): Envelope | ||
{ | ||
return new Envelope( | ||
to : new Address(env('MAIL_TO_ADDRESS'), env('MAIL_TO_ADDRESS')), | ||
subject: sprintf('%s server status email! %d issues!', env('APP_NAME'), $this->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<int, Attachment> | ||
*/ | ||
public function attachments(): array | ||
{ | ||
return []; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
namespace App\Traits; | ||
|
||
use App\Models\Rate; | ||
use Carbon\Carbon; | ||
use Carbon\CarbonInterface; | ||
|
||
trait QueriesFaultyRates | ||
{ | ||
public function getFaultyRates() | ||
{ | ||
return Rate::query() | ||
->enabled() | ||
->whereDate('updated_at', '<', Carbon::now('UTC')->subHours(6)->format(CarbonInterface::DEFAULT_TO_STRING_FORMAT)) | ||
->where('status_message', '<>', '') | ||
->where('status', '=', true) | ||
->get(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
{{--@formatter:off--}} | ||
<x-mail::message> | ||
# {{ config('app.name') }} Site Scraping Status! | ||
|
||
@if( $rates->count() > 0 ) | ||
### {{ $rates->count() }} rates where not successfully scraped in the last 24 hrs. | ||
|
||
<x-mail::panel> | ||
@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 }} | ||
<br> | ||
@endforeach | ||
</x-mail::panel> | ||
@else | ||
### All rates where successfully scraped in the period leading upto the last 6 hrs. | ||
@endif | ||
|
||
Thanks,<br> | ||
|
||
{{ config('app.name') }} | ||
|
||
<x-mail::button :url="url('status')" color="primary"> | ||
View In Browser | ||
</x-mail::button> | ||
|
||
</x-mail::message> | ||
{{--@formatter:on--}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters