Add HubDistributor service. #543

This commit is contained in:
Korina Cordero 2021-03-15 10:09:11 +00:00
parent 0f245e9038
commit 1abaad1727
5 changed files with 76 additions and 8 deletions

View file

@ -57,6 +57,7 @@ COUNTRY_CODE=+insert_country_code_here
# redis hash
LATEST_ACTIVE_JO=latest_active_jo
HUB_JO_KEY=hub_jo_count
# dashboard
DASHBOARD_ENABLE=set_to_true_or_false

View file

@ -271,3 +271,10 @@ services:
arguments:
$em: "@doctrine.orm.entity_manager"
$im: "@App\\Service\\InventoryManager"
$hub_distributor: "@App\\Service\\HubDistributor"
# hub distributor
App\Service\HubDistributor:
arguments:
$redis: "@App\\Service\\RedisClientProvider"
$hub_jo_key: "%env(HUB_JO_KEY)%"

View file

@ -2546,6 +2546,7 @@ class APIController extends Controller implements LoggedController
$nearest_hubs = $hub_select->find($hub_criteria);
}
// TODO: this might need changes after the new hub selector
if (!empty($nearest_hubs))
{
//error_log('found nearest hub ' . $nearest_hub->getID());

View file

@ -0,0 +1,54 @@
<?php
namespace App\Service;
use Doctrine\ORM\EntityManagerInterface;
use App\Service\RedisClientProvider;
use App\Entity\Hub;
class HubDistributor
{
protected $redis;
protected $hub_jo_key;
public function __construct(RedisClientProvider $redis,
$hub_jo_key)
{
$this->redis = $redis->getRedisClient();
$this->hub_jo_key = $hub_jo_key;
}
public function addJoCountForHub(Hub $hub)
{
$key = $hub->gtID();
// get current count
$result = $this->redis->hget($this->hub_jo_key, $key);
if ($result == true)
{
// hub exist in hash
// add to count
}
else
{
// hub not in hash
// add hub to hash
}
}
public function arrangeHubs($hubs)
{
$arranged_hubs = [];
foreach ($hubs as $hub_data)
{
$hub = $hub_data['hub'];
// get hub in hash
}
return $arranged_hubs;
}
}

View file

@ -8,7 +8,7 @@ use CrEOF\Spatial\PHP\Types\Geometry\Point;
use App\Entity\Hub;
use App\Service\RedisClientProvider;
use App\Service\HubDistributor;
use App\Service\InventoryManager;
use App\Ramcar\HubCriteria;
@ -18,14 +18,14 @@ class HubSelector
{
protected $em;
protected $im;
protected $redis;
protected $hub_distributor;
public function __construct(EntityManagerInterface $em, InventoryManager $im,
RedisClientProvider $redis)
HubDistributor $hub_distributor)
{
$this->em = $em;
$this->im = $im;
$this->redis = $redis;
$this->hub_distributor = $hub_distributor;
}
public function find(HubCriteria $criteria)
@ -57,8 +57,8 @@ class HubSelector
$filtered_hubs = $hubs_inventory;
// round robin filter
//$hubs_round_robin = $this->filterHubsByRoundRobin($filtered_hubs);
//$filtered_hubs = $hubs_round_robin;
$hubs_round_robin = $this->filterHubsByRoundRobin($filtered_hubs);
$filtered_hubs = $hubs_round_robin;
// max results filter
$hubs_max_result = $this->filterHubsByMaxResults($filtered_hubs, $limit_results);
@ -74,8 +74,13 @@ class HubSelector
if (empty($hubs))
return $hubs;
// for now only
return $hubs;
$results = [];
// call hub distributor service
$arranged_hubs = $this->hub_distributor->arrangeHubs($hubs);
$results = $arranged_hubs;
return $results;
}
protected function filterHubsByMaxResults($hubs, $limit_result)