Skip to content

Commit

Permalink
Merge pull request #41 from richard-muvirimi/development
Browse files Browse the repository at this point in the history
Send daily system status messages
  • Loading branch information
richard-muvirimi committed Apr 19, 2024
2 parents 62db4ab + 2d3de61 commit a796af7
Show file tree
Hide file tree
Showing 8 changed files with 172 additions and 1 deletion.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
51 changes: 51 additions & 0 deletions app/Console/Commands/Status.php
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();

}
}
2 changes: 2 additions & 0 deletions app/Console/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}

/**
Expand Down
9 changes: 8 additions & 1 deletion app/Http/Controllers/RatesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -13,7 +15,7 @@

class RatesController extends Controller
{
use ResolvesRates;
use QueriesFaultyRates, ResolvesRates;

public function version0(Request $request): JsonResponse
{
Expand Down Expand Up @@ -57,4 +59,9 @@ public function version1(Request $request): JsonResponse
], Response::HTTP_INTERNAL_SERVER_ERROR);
}
}

public function status(): StatusMail
{
return new StatusMail($this->getFaultyRates());
}
}
57 changes: 57 additions & 0 deletions app/Mail/StatusMail.php
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 [];
}
}
20 changes: 20 additions & 0 deletions app/Traits/QueriesFaultyRates.php
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();
}
}
30 changes: 30 additions & 0 deletions resources/views/mail/status.blade.php
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--}}
3 changes: 3 additions & 0 deletions routes/web.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

use App\Http\Controllers\RatesController;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\Route;
Expand Down Expand Up @@ -32,6 +33,8 @@
Artisan::call('app:scrape');
});

Route::get('/status', [RatesController::class, 'status']);

$appData = json_decode(file_get_contents(base_path('composer.json')), true);

$data = [
Expand Down

0 comments on commit a796af7

Please sign in to comment.