From 07a75de0bf8a82d6b73977d8a97fe47d06c9de2a Mon Sep 17 00:00:00 2001 From: Nabeel S Date: Mon, 24 Feb 2020 19:35:28 -0500 Subject: [PATCH] Recalcuate aircraft stats in nightly cron and importer #585 #443 (#587) --- app/Cron/Nightly/RecalculateStats.php | 13 +++++--- app/Services/AircraftService.php | 32 +++++++++++++++++++ .../Resources/views/step1-configure.blade.php | 4 ++- .../Services/Importers/FinalizeImporter.php | 15 +++++++++ tests/PIREPTest.php | 18 +++++++++++ 5 files changed, 76 insertions(+), 6 deletions(-) create mode 100644 app/Services/AircraftService.php diff --git a/app/Cron/Nightly/RecalculateStats.php b/app/Cron/Nightly/RecalculateStats.php index cb761372..e4eb2320 100644 --- a/app/Cron/Nightly/RecalculateStats.php +++ b/app/Cron/Nightly/RecalculateStats.php @@ -4,6 +4,7 @@ namespace App\Cron\Nightly; use App\Contracts\Listener; use App\Events\CronNightly; +use App\Services\AircraftService; use App\Services\UserService; use Illuminate\Support\Facades\Log; @@ -12,11 +13,13 @@ use Illuminate\Support\Facades\Log; */ class RecalculateStats extends Listener { + private $aircraftSvc; private $userSvc; - public function __construct(UserService $userService) + public function __construct(AircraftService $aircraftSvc, UserService $userSvc) { - $this->userSvc = $userService; + $this->aircraftSvc = $aircraftSvc; + $this->userSvc = $userSvc; } /** @@ -29,10 +32,10 @@ class RecalculateStats extends Listener */ public function handle(CronNightly $event): void { - Log::info('Recalculating balances'); - + Log::info('Recalculating user stats'); $this->userSvc->recalculateAllUserStats(); - Log::info('Done recalculating stats'); + Log::info('Recalcuating aircraft status'); + $this->aircraftSvc->recalculateStats(); } } diff --git a/app/Services/AircraftService.php b/app/Services/AircraftService.php new file mode 100644 index 00000000..047c7757 --- /dev/null +++ b/app/Services/AircraftService.php @@ -0,0 +1,32 @@ +pirepRepo = $pirepRepo; + } + + /** + * Recalculate all aircraft stats and hours + */ + public function recalculateStats() + { + $allAircraft = Aircraft::all(); // TODO: Soft delete + foreach ($allAircraft as $aircraft) { + $pirep_time_total = Pirep::where('aircraft_id', $aircraft->id) + ->sum('flight_time'); + $aircraft->flight_time = $pirep_time_total; + $aircraft->save(); + } + } +} diff --git a/modules/Importer/Resources/views/step1-configure.blade.php b/modules/Importer/Resources/views/step1-configure.blade.php index 41f01ce3..226fda1d 100644 --- a/modules/Importer/Resources/views/step1-configure.blade.php +++ b/modules/Importer/Resources/views/step1-configure.blade.php @@ -24,7 +24,9 @@ -

Site Config

+

Site Config

+

Enter the database information for your legacy (v2 or v5) database

+ diff --git a/modules/Importer/Services/Importers/FinalizeImporter.php b/modules/Importer/Services/Importers/FinalizeImporter.php index c711664b..7d340de0 100644 --- a/modules/Importer/Services/Importers/FinalizeImporter.php +++ b/modules/Importer/Services/Importers/FinalizeImporter.php @@ -3,6 +3,7 @@ namespace Modules\Importer\Services\Importers; use App\Models\User; +use App\Services\AircraftService; use App\Services\UserService; use Modules\Importer\Services\BaseImporter; @@ -51,6 +52,8 @@ class FinalizeImporter extends BaseImporter protected function recalculateUserStats() { $this->comment('--- RECALCULATING USER STATS ---'); + + /** @var UserService $userSvc */ $userSvc = app(UserService::class); User::all()->each(function ($user) use ($userSvc) { @@ -58,6 +61,18 @@ class FinalizeImporter extends BaseImporter }); } + /** + * Update the aircraft stats with the newest/latest PIREPs + */ + protected function recalculateAircraftStats() + { + $this->comment('--- RECALCULATING AIRCRAFT STATS ---'); + + /** @var AircraftService $aircraftSvc */ + $aircraftSvc = app(AircraftService::class); + $aircraftSvc->recalculateStats(); + } + /** * Clear the value store of any old value mappings */ diff --git a/tests/PIREPTest.php b/tests/PIREPTest.php index 48e6dc22..ecc6cf82 100644 --- a/tests/PIREPTest.php +++ b/tests/PIREPTest.php @@ -1,6 +1,7 @@ assertGreaterThan($user->rank_id, $pilot->rank_id); + + // Check the aircraft + $aircraft = Aircraft::where('id', 1)->first(); + $this->assertEquals(120, $aircraft->flight_time); + + // Reset the aircraft flight time + $aircraft->flight_time = 10; + $aircraft->save(); + + // Recalculate the status + /** @var AircraftService $aircraftSvc */ + $aircraftSvc = app(AircraftService::class); + $aircraftSvc->recalculateStats(); + + $aircraft = Aircraft::where('id', 1)->first(); + $this->assertEquals(120, $aircraft->flight_time); } /**