Skip to content

Commit

Permalink
Merge pull request #1566 from AndyTWF/stand-assignments-if-active
Browse files Browse the repository at this point in the history
fix: dont assign arrival stands if aircraft not seen in last minute
  • Loading branch information
AndyTWF authored Apr 27, 2024
2 parents bcc3070 + e496a16 commit cf699bb
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
5 changes: 5 additions & 0 deletions app/Models/Vatsim/NetworkAircraft.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
8 changes: 7 additions & 1 deletion app/Services/Stand/ArrivalAllocationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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.
Expand Down Expand Up @@ -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();
}
Expand Down
5 changes: 2 additions & 3 deletions tests/app/Services/Stand/ArrivalAllocationServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -247,7 +246,7 @@ public function testItAllocatesAStandFromAllocator()
Event::assertDispatched(StandAssignedEvent::class);
}

public function testItDoesntAllocateStandIfTimedOut()
public function testItDoesntAllocateStandIfNotSeenWithinTheLastMinute()
{
$aircraft = NetworkAircraftService::createOrUpdateNetworkAircraft(
'BMI221',
Expand All @@ -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();
Expand Down

0 comments on commit cf699bb

Please sign in to comment.