示例#1
0
 /**
  * extractServersFromGroup:
  * extract servers from group accordingly to server group fill_type
  * @param INT  $group_id:    id of the group to extract from
  * @param INT  $server_type: optional server type id to extract. If empty, one server for each type is extracted
  * @return ARRAY
  * 
  */
 public static function extractServersFromGroup($group_id, $server_type = null)
 {
     $arrOutput = array();
     $group_id = isset($group_id) ? intval($group_id) : null;
     $server_type = isset($server_type) ? intval($server_type) : null;
     if (!$group_id) {
         return $arrOutput;
     }
     // Get the fill_type for requested group
     $ServersGroups = ServersGroups::find($group_id);
     if (!$ServersGroups || !isset($ServersGroups->fill_type)) {
         return $arrOutput;
     }
     $fill_type = (int) $ServersGroups->fill_type;
     /*
             	'1' => 'Create services on the least full server',
             	'2' => 'Fill default server until full then switch to next least used', 
             	'3' => 'Fill servers starting from the newest to the older',
             	'4' => 'Fill servers starting from the older to the newest',
             	'5' => 'Fill servers randomly',
             	'6' => 'Fill manually. Only default server will be used.',
             	'7' => 'Fill servers starting from the cheaper to the expensive.',
             	'8' => 'Fill servers starting from the expensive to the cheaper.',
     */
     // Get all servers inside this group
     $serverIds = ServersGroups::getServers($group_id, 's.*');
     // Get servers details
     $ORM = Doctrine_Query::create()->from('Servers s')->leftJoin('s.Servers_Types st')->leftJoin('s.Statuses stat')->leftJoin('s.Panels panel')->leftJoin('s.Servers_Types')->leftJoin('s.CustomAttributesValues')->whereIn('s.server_id', $serverIds);
     if ($server_type) {
         $ORM = $ORM->andWhere('s.type_id = ?', $server_type);
     }
     $ORM = $ORM->execute();
     // Create an array with type_id as index
     $arrServers = array();
     foreach ($ORM as $y) {
         $type_id = $y->type_id;
         $server_id = $y->server_id;
         $services = isset($y->services) ? intval($y->services) : 0;
         $max_services = isset($y->max_services) && $y->max_services > 0 ? intval($y->max_services) : 10000;
         $buy_timestamp = isset($y->buy_date) ? strtotime($y->buy_date) : 0;
         $is_default = isset($y->is_default) ? intval($y->is_default) : 0;
         // Skip full servers
         if ($services >= $max_services) {
             Shineisp_Commons_Utilities::logs(__METHOD__ . " " . $server_type . " is full!", 'servers.log');
             continue;
         }
         $usagePercent = round($services * 100 / $max_services);
         $arrServers[$type_id][$server_id] = $y->toArray();
         $arrServers[$type_id][$server_id]['is_default'] = $is_default;
         $arrServers[$type_id][$server_id]['_usagePercent'] = $usagePercent;
         $arrServers[$type_id][$server_id]['_buyTimestamp'] = $buy_timestamp;
     }
     // Cycle servers by type
     foreach ($arrServers as $type_id => $serverType) {
         $tmp = array();
         // temporary array
         // Extract server
         switch ($fill_type) {
             case 1:
                 // 'Create services on the least full server'
             // 'Create services on the least full server'
             case 2:
                 // 'Fill default server until full then switch to next least used'
                 $method = $fill_type === 1 ? SORT_ASC : SORT_DESC;
                 $tmp = Shineisp_Commons_ArraySorter::multisort($serverType, '_usagePercent', $method);
                 break;
             case 3:
                 // 'Fill servers starting from the newest to the older'
             // 'Fill servers starting from the newest to the older'
             case 4:
                 // 'Fill servers starting from the older to the newest'
                 $method = $fill_type === 3 ? SORT_DESC : SORT_ASC;
                 $tmp = Shineisp_Commons_ArraySorter::multisort($serverType, '_buyTimestamp', $method);
                 break;
             case 5:
                 // 'Fill servers randomly'
                 $randKey = array_rand($serverType);
                 $tmp = array($randKey => $randKey);
                 break;
             case 6:
                 // 'Fill manually. Only default server will be used.'
                 $tmp = Shineisp_Commons_ArraySorter::multisort($serverType, 'is_default', SORT_DESC);
                 break;
             case 7:
                 // 'Fill servers starting from the cheaper to the expensive.'
             // 'Fill servers starting from the cheaper to the expensive.'
             case 8:
                 // 'Fill servers starting from the expensive to the cheaper.'
                 $method = $fill_type === 7 ? SORT_ASC : SORT_DESC;
                 $tmp = Shineisp_Commons_ArraySorter::multisort($serverType, 'cost', $method);
         }
         reset($tmp);
         $selectedServer = key($tmp);
         $arrOutput[$type_id][$selectedServer] = $arrServers[$type_id][$selectedServer];
     }
     if (isset($server_type) && isset($arrOutput[$server_type])) {
         // return just the server array
         $key = key($arrOutput[$server_type]);
         $arrOutput = $arrOutput[$server_type][$key];
     }
     Shineisp_Commons_Utilities::logs(__METHOD__ . '(' . $group_id . ', ' . $server_type . '): ' . serialize($arrOutput), 'servers.log');
     // Return
     return $arrOutput;
 }