Recalcuate aircraft stats in nightly cron and importer #585 #443 (#587)

This commit is contained in:
Nabeel S
2020-02-24 19:35:28 -05:00
committed by GitHub
parent 30786b7b27
commit 07a75de0bf
5 changed files with 76 additions and 6 deletions

View File

@@ -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();
}
}

View File

@@ -0,0 +1,32 @@
<?php
namespace App\Services;
use App\Contracts\Service;
use App\Models\Aircraft;
use App\Models\Pirep;
use App\Repositories\PirepRepository;
class AircraftService extends Service
{
private $pirepRepo;
public function __construct(PirepRepository $pirepRepo)
{
$this->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();
}
}
}

View File

@@ -24,7 +24,9 @@
</tr>
<tr>
<td colspan="2"><h4>Site Config</h4></td>
<td colspan="2"><h4>Site Config</h4>
<p>Enter the database information for your legacy (v2 or v5) database</p>
</td>
</tr>
<tr>

View File

@@ -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
*/

View File

@@ -1,6 +1,7 @@
<?php
use App\Models\Acars;
use App\Models\Aircraft;
use App\Models\Bid;
use App\Models\Enums\AcarsType;
use App\Models\Enums\PirepState;
@@ -8,6 +9,7 @@ use App\Models\Pirep;
use App\Models\User;
use App\Notifications\Messages\PirepAccepted;
use App\Repositories\SettingRepository;
use App\Services\AircraftService;
use App\Services\BidService;
use App\Services\FlightService;
use App\Services\PirepService;
@@ -299,6 +301,22 @@ class PIREPTest extends TestCase
// Make sure rank went up
$this->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);
}
/**