redis = $redis->getRedisClient(); $this->hub_jo_key = $hub_jo_key; } public function incrementJoCountForHub(Hub $hub) { $key = $hub->getID(); // get current count $jo_count = $this->redis->hget($this->hub_jo_key, $key); if ($jo_count == false) { // hub not in hash // add hub to hash // set to 1 since this is first jo for hub $this->redis->hset($this->hub_jo_key, $key, 1); } else { // hub exist in hash // add to count $this->redis->hset($this->hub_jo_key, $key, $jo_count + 1); } } public function decrementJoCountForHub(Hub $hub) { $key = $hub->getID(); // get current count $jo_count = $this->redis->hget($this->hub_jo_key, $key); if ($jo_count) { // hub exist in hash // decrement count $this->redis->hset($this->hub_jo_key, $key, $jo_count - 1); } } public function arrangeHubs($hubs) { if (count($hubs) == 1) return $hubs; $arranged_hubs = []; foreach ($hubs as $hub_data) { $hub = $hub_data['hub']; // need the id of hub $key = $hub->getID(); // get jo count of hub $hub_jo_count = $this->redis->hget($this->hub_jo_key, $key); // check if hub is in hash. if not, hub has no jobs // but should still be added to results if ($hub_jo_count != null) { $arranged_hubs[] = [ 'hub' => $hub, 'db_distance' => $hub_data['db_distance'], 'distance' => $hub_data['distance'], 'duration' => $hub_data['duration'], 'jo_count' => $hub_jo_count, ]; } else { $arranged_hubs[] = [ 'hub' => $hub, 'db_distance' => $hub_data['db_distance'], 'distance' => $hub_data['distance'], 'duration' => $hub_data['duration'], 'jo_count' => 0, ]; } } usort($arranged_hubs, function($a, $b) { if ($a['jo_count'] == $b['jo_count']) return 0; if ($a['jo_count'] < $b['jo_count']) return -1; else return 1; }); //error_log('arranged hubs ' . json_encode($arranged_hubs)); return $arranged_hubs; } }