From e496a16da86c137a992fe8af80e2bc469b93d6d6 Mon Sep 17 00:00:00 2001 From: Andy Ford Date: Sat, 27 Apr 2024 20:29:03 +0100 Subject: [PATCH] fix: dont assign arrival stands if aircraft not seen in last minute --- app/Models/Vatsim/NetworkAircraft.php | 5 +++++ app/Services/Stand/ArrivalAllocationService.php | 8 +++++++- tests/app/Services/Stand/ArrivalAllocationServiceTest.php | 5 ++--- 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/app/Models/Vatsim/NetworkAircraft.php b/app/Models/Vatsim/NetworkAircraft.php index 373cf1ff4..3e4407eb7 100644 --- a/app/Models/Vatsim/NetworkAircraft.php +++ b/app/Models/Vatsim/NetworkAircraft.php @@ -145,6 +145,11 @@ public function scopeNotTimedOut(Builder $builder): Builder return $builder->where('network_aircraft.updated_at', '>', Carbon::now()->subMinutes(self::TIME_OUT_MINUTES)); } + public function scopeSeenSince(Builder $builder, Carbon $time): Builder + { + return $builder->where('network_aircraft.updated_at', '>', $time); + } + public function destinationAirfield(): BelongsTo { return $this->belongsTo( diff --git a/app/Services/Stand/ArrivalAllocationService.php b/app/Services/Stand/ArrivalAllocationService.php index 5ecd4d063..06b92d66d 100644 --- a/app/Services/Stand/ArrivalAllocationService.php +++ b/app/Services/Stand/ArrivalAllocationService.php @@ -9,6 +9,7 @@ use App\Models\Stand\StandAssignment; use App\Models\Vatsim\NetworkAircraft; use App\Services\LocationService; +use Carbon\Carbon; use Illuminate\Support\Collection; use Location\Distance\Haversine; @@ -18,6 +19,11 @@ class ArrivalAllocationService * How many minutes before arrival the stand should be assigned */ private const ASSIGN_STAND_MINUTES_BEFORE = 15.0; + + /** + * How recently an aircraft should have been seen to be considered for stand assignment. + */ + private const ASSIGN_STAND_IF_SEEN_WITHIN_MINUTES = 1; /** * How many minutes before we remove the stand assignment for an aircraft that has disconnected. @@ -113,7 +119,7 @@ private function getAircraftThatCanHaveArrivalStandsAllocated(): Collection ->where('aircraft.allocate_stands', '<>', 0) ->where('network_aircraft.groundspeed', '>', 0) ->whereNull('stand_assignments.callsign') - ->notTimedOut() + ->seenSince(Carbon::now()->subMinutes(self::ASSIGN_STAND_IF_SEEN_WITHIN_MINUTES)) ->select('network_aircraft.*') ->get(); } diff --git a/tests/app/Services/Stand/ArrivalAllocationServiceTest.php b/tests/app/Services/Stand/ArrivalAllocationServiceTest.php index 3a40621df..853212c80 100644 --- a/tests/app/Services/Stand/ArrivalAllocationServiceTest.php +++ b/tests/app/Services/Stand/ArrivalAllocationServiceTest.php @@ -34,7 +34,6 @@ use App\Models\Vatsim\NetworkAircraft; use App\Services\NetworkAircraftService; use Carbon\Carbon; -use Exception; use Illuminate\Support\Collection; use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Event; @@ -247,7 +246,7 @@ public function testItAllocatesAStandFromAllocator() Event::assertDispatched(StandAssignedEvent::class); } - public function testItDoesntAllocateStandIfTimedOut() + public function testItDoesntAllocateStandIfNotSeenWithinTheLastMinute() { $aircraft = NetworkAircraftService::createOrUpdateNetworkAircraft( 'BMI221', @@ -265,7 +264,7 @@ public function testItDoesntAllocateStandIfTimedOut() 'aircraft_id' => 1, ] ); - $aircraft->updated_at = Carbon::now()->subMinutes(30); + $aircraft->updated_at = Carbon::now()->subMinutes(1)->subSecond(); $aircraft->save(); $this->service->allocateStandsAtArrivalAirfields();