public static function generateInstances(\alojaweb\Filters\Filters $filters, $param_names, $params, $generalize, $db = null) { $filter_options = $filters->getFilterChoices(); $paramAllOptions = $tokens = $instances = array(); // Get info from clusters (Part of header_names!) $cluster_header_names = array('id_cluster' => 'Cluster', 'datanodes' => 'Datanodes', 'vm_OS' => 'VM.OS', 'vm_cores' => 'VM.Cores', 'vm_RAM' => 'VM.RAM', 'provider' => 'Provider', 'vm_size' => 'VM.Size', 'type' => 'Type'); $cluster_descriptor = array(); $query = "select " . implode(",", array_keys($cluster_header_names)) . " from aloja2.clusters;"; $rows = $db->get_rows($query); foreach ($rows as $row) { $cid = $row['id_cluster']; foreach (array_keys($cluster_header_names) as $cname) { $cluster_descriptor[$cid][$cname] = $row[$cname]; } } // If "No Clusters" -> All clusters if (empty($params['id_cluster'])) { $params['id_cluster'] = array(); $paramAllOptions['id_cluster'] = $filter_options['id_cluster']; foreach ($paramAllOptions['id_cluster'] as $par) { $params['id_cluster'][] = $par; } } // For each cluster selected, launch an instance... foreach ($params['id_cluster'] as $cl) { // Reduce the instance to the HW filter override, or even remove instance if no HW coincides $remove_if_no_props = FALSE; foreach (array_keys($cluster_header_names) as $cname) { if (!empty($params[$cname])) { // FIXME - When clusters have more than 1 characteristic, change this // Get only the current_props in params[cname] $current_props = $cluster_descriptor[$cl][$cname]; $current_props = array($current_props); $coincidences = array_intersect($current_props, $params[$cname]); if (empty($coincidences)) { $remove_if_no_props = TRUE; } else { $cluster_descriptor[$cl][$cname] = $coincidences; } } } if ($remove_if_no_props) { continue; } $cl_characteristics = "Cl" . Utils::multi_implode($cluster_descriptor[$cl], ','); $instance = ''; foreach ($param_names as $p) { // Ignore for now. Will be used at each cluster characteristics if (array_key_exists($p, $cluster_header_names) && $p != "id_cluster") { continue; } if ($p != "id_cluster") { if (array_key_exists($p, $filter_options)) { $paramAllOptions[$p] = $filter_options[$p]; } $tokens[$p] = ''; if ($generalize && empty($params[$p])) { $tokens[$p] = '*'; } elseif (!$generalize && empty($params[$p])) { foreach ($paramAllOptions[$p] as $par) { $tokens[$p] = $tokens[$p] . ($tokens[$p] != '' ? '|' : '') . ($p == 'comp' ? 'Cmp' : '') . ($p == 'id_cluster' ? 'Cl' : '') . $par; } } else { if (is_array($params[$p])) { foreach ($params[$p] as $par) { $tokens[$p] = $tokens[$p] . ($tokens[$p] != '' ? '|' : '') . ($p == 'comp' ? 'Cmp' : '') . ($p == 'id_cluster' ? 'Cl' : '') . $par; } } else { $tokens[$p] = $params[$p]; } } $instance = $instance . ($instance == '' ? '' : ',') . $tokens[$p]; } else { $instance = $instance . ($instance == '' ? '' : ',') . $cl_characteristics; } } $instances[] = $instance; } return MLUtils::completeInstances($filters, $instances, $param_names, $params, $db); }
public static function multi_implode($array, $glue) { $ret = ''; if (!is_array($array)) { return $ret; } foreach ($array as $item) { if (is_array($item)) { $ret .= Utils::multi_implode($item, $glue) . $glue; } else { $ret .= $item . $glue; } } $ret = substr($ret, 0, 0 - strlen($glue)); return $ret; }