Move supported area query to hub selector #810

This commit is contained in:
Ramon Gutierrez 2024-12-08 21:37:58 +08:00
parent 3c79d1fe28
commit aa042a435b
2 changed files with 52 additions and 30 deletions

View file

@ -72,33 +72,6 @@ class BaseHubFilter
@file_put_contents(__DIR__ . $filename, $entry, FILE_APPEND);
}
public function isExemptedByArea(): bool
{
$point = $this->crit->getPoint();
$long = $point->getLongitude();
$lat = $point->getLatitude();
// get supported area given a set of coordinates
$query = $this->em->createQuery('SELECT s from App\Entity\SupportedArea s where st_contains(s.coverage_area, point(:long, :lat)) = true');
$area = $query->setParameter('long', $long)
->setParameter('lat', $lat)
->setMaxResults(1)
->getOneOrNullResult();
if ($area !== null) {
// get all exceptions
$exceptions = $area->getHubFilterExceptions();
if (isset($exceptions[$this->id])) {
// disable this filter for this area
return true;
}
}
// filter is in place
return false;
}
protected function createRejectionEntry($hub, $reason, $remarks = ""): JORejection
{
$jo_id = $this->crit->getJobOrderId();

View file

@ -16,6 +16,7 @@ use App\Service\HubFilterLogger;
use App\Service\RisingTideGateway;
use App\Ramcar\HubCriteria;
use App\Service\HubFilter\HubFilterInterface;
class HubSelector
{
@ -50,7 +51,7 @@ class HubSelector
return $enabled_filters;
}
public function find(HubCriteria $criteria)
public function find(HubCriteria $criteria): array
{
// get all the hubs
$hubs = $this->getHubList($criteria->getPoint());
@ -58,6 +59,9 @@ class HubSelector
// get customer record
$cust = $this->em->getRepository(Customer::class)->find($criteria->getCustomerId());
// get all areas that cover the JO location
$areas = $this->getAreaCoverage($criteria);
// loop through all enabled filters
foreach ($this->getActiveFilters() as $hub_filter) {
// no hubs left to filter
@ -65,15 +69,16 @@ class HubSelector
break;
}
// initialize the filter
$f = $this->container->get($hub_filter);
$f->initialize($criteria, $cust);
// check if supported area is exempted from this filter
if ($f->isExemptedByArea()) {
if ($this->isExemptedByArea($areas, $f)) {
continue;
}
// filter hub list
// run the filter
$hubs = $f->filter($hubs, $criteria);
}
@ -82,6 +87,50 @@ class HubSelector
return $hubs;
}
protected function isExemptedByArea(array $areas, HubFilterInterface $filter): bool
{
$is_exempted = false;
if (!empty($areas)) {
// check if at least one area has this filter enabled
$has_support = false;
foreach ($areas as $area) {
// get all exceptions
$exceptions = $area->getHubFilterExceptions();
// if any area has this filter enabled, consider it enabled and move on
if (!isset($exceptions[$filter->getID()])) {
$has_support = true;
break;
}
}
// none of the areas have this filter enabled, consider it exempted
if (!$has_support) {
error_log("skipping filter " . $filter->getID() . " due to exempted area");
$is_exempted = true;
}
}
// filter is in place
return $is_exempted;
}
protected function getAreaCoverage(HubCriteria $criteria): array
{
$point = $criteria->getPoint();
$long = $point->getLongitude();
$lat = $point->getLatitude();
// get supported area given a set of coordinates
$query = $this->em->createQuery('SELECT s from App\Entity\SupportedArea s where st_contains(s.coverage_area, point(:long, :lat)) = true');
return $query->setParameter('long', $long)
->setParameter('lat', $lat)
->getResult();
}
protected function getHubList(Point $point): array
{
// get closest hubs based on st_distance function from db