[Tags performance] Fixed edit and delete massive delete and edit modules in bulk selecting modules first

This commit is contained in:
fermin831 2018-10-10 17:42:33 +02:00
parent 8339eecacc
commit de10de03f7
5 changed files with 121 additions and 110 deletions

View File

@ -101,12 +101,7 @@ function process_manage_delete ($module_name, $id_agents, $module_status = 'all'
}
$count_deleted_modules = count($modules);
if ($config['dbtype'] == "oracle") {
$success = db_process_sql(sprintf("DELETE FROM tagente_modulo WHERE id_agente_modulo IN (%s)", implode(",", $modules)));
}
else {
$success = modules_delete_agent_module ($modules);
}
$success = modules_delete_agent_module ($modules);
if (! $success) {
ui_print_error_message(
@ -123,8 +118,6 @@ function process_manage_delete ($module_name, $id_agents, $module_status = 'all'
}
$module_type = (int) get_parameter ('module_type');
$idGroupMassive = (int) get_parameter('id_group_massive');
$idAgentMassive = (int) get_parameter('id_agent_massive');
$group_select = get_parameter('groups_select');
$delete = (bool) get_parameter_post ('delete');
@ -146,9 +139,7 @@ if ($delete) {
$agents_ = array();
}
foreach ($agents_select as $agent_name) {
$agents_[] = agents_get_agent_id(io_safe_output($agent_name));
}
$agents_ = $agents_select;
$modules_ = $module_name;
break;
case 'agents':
@ -231,35 +222,18 @@ $groups = users_get_groups ();
$agents = agents_get_group_agents (array_keys (users_get_groups ()),
false, "none");
switch ($config["dbtype"]) {
case "mysql":
$module_types = db_get_all_rows_filter ('tagente_modulo,ttipo_modulo',
array ('tagente_modulo.id_tipo_modulo = ttipo_modulo.id_tipo',
'id_agente' => array_keys ($agents),
'disabled' => 0,
'order' => 'ttipo_modulo.nombre'),
array ('DISTINCT(id_tipo)',
'CONCAT(ttipo_modulo.descripcion," (",ttipo_modulo.nombre,")") AS description'));
break;
case "oracle":
$module_types = db_get_all_rows_filter ('tagente_modulo,ttipo_modulo',
array ('tagente_modulo.id_tipo_modulo = ttipo_modulo.id_tipo',
'id_agente' => array_keys ($agents),
'disabled' => 0,
'order' => 'ttipo_modulo.nombre'),
array ('ttipo_modulo.id_tipo',
'ttipo_modulo.descripcion || \' (\' || ttipo_modulo.nombre || \')\' AS description'));
break;
case "postgresql":
$module_types = db_get_all_rows_filter ('tagente_modulo,ttipo_modulo',
array ('tagente_modulo.id_tipo_modulo = ttipo_modulo.id_tipo',
'id_agente' => array_keys ($agents),
'disabled' => 0,
'order' => 'description'),
array ('DISTINCT(id_tipo)',
'ttipo_modulo.descripcion || \' (\' || ttipo_modulo.nombre || \')\' AS description'));
break;
}
$module_types = db_get_all_rows_filter (
'tagente_modulo,ttipo_modulo',
array (
'tagente_modulo.id_tipo_modulo = ttipo_modulo.id_tipo',
'id_agente' => array_keys ($agents),
'disabled' => 0,
'order' => 'ttipo_modulo.nombre'
), array (
'DISTINCT(id_tipo)',
'CONCAT(ttipo_modulo.descripcion," (",ttipo_modulo.nombre,")") AS description'
)
);
if ($module_types === false)
$module_types = array ();
@ -275,16 +249,12 @@ $table->data = array ();
$table->style[0] = 'font-weight: bold';
$table->style[2] = 'font-weight: bold';
$table->data['selection_mode'][0] = __('Selection mode');
$table->data['selection_mode'][1] = '<span style="width:110px;display:inline-block;">'.__('Select modules first ') . '</span>' .
html_print_radio_button_extended ("selection_mode", 'modules', '', $selection_mode, false, '', 'style="margin-right: 40px;"', true).'<br>';
$table->data['selection_mode'][1] .= '<span style="width:110px;display:inline-block;">'.__('Select agents first ') . '</span>' .
html_print_radio_button_extended ("selection_mode", 'agents', '', $selection_mode, false, '', 'style="margin-right: 40px;"', true);
$table->rowclass['form_modules_1'] = 'select_modules_row';
$table->data['form_modules_1'][0] = __('Module type');
$table->data['form_modules_1'][0] .= '<span id="module_loading" class="invisible">';

View File

@ -2552,6 +2552,92 @@ function select_modules_for_agent_group(
return $modules_array;
}
function select_agents_for_module_group(
$module_names, $selection, $filter, $access = "AR"
) {
global $config;
$default_filter = array (
'status' => null
);
$filter = array_merge($default_filter, $filter);
$module_names_condition = "";
$filter_agent_group = "";
$selection_filter = "";
$sql_conditions_tags = "";
$sql_tags_inner = "";
$status_filter = "";
$module_type_filter = "";
$groups = array_keys(users_get_groups(false, $access, false));
// Name
if (!users_can_manage_group_all($access)) {
$group_string = implode(',', $groups);
$filter_agent_group = " AND (
tagente.id_grupo IN ($group_string)
OR tasg.id_group IN ($group_string)
)";
}
// Name filter
if ($module_names) {
$module_names_sql = implode("','", $module_names);
$module_names_condition = " AND tagente_modulo.nombre IN ('$module_names_sql') ";
}
// Common or all modules filter
if (!$selection) {
$number_modules = count($module_names);
$selection_filter = "HAVING COUNT(id_agente) = $number_modules";
}
// Status filter
if ($filter['status'] != null) {
$status_filter = " AND " . modules_get_state_condition(
$filter['status'], "tagente_estado"
);
}
// Tags input and ACL conditions
if (tags_has_user_acl_tags(false) || $filter['tags'] != null){
$sql_conditions_tags = tags_get_acl_tags(
$config['id_user'], $groups, $access,
'module_condition', 'AND', 'tagente_modulo', true, array(),
false);
$sql_tags_inner = "INNER JOIN ttag_module
ON ttag_module.id_agente_modulo = tagente_modulo.id_agente_modulo";
}
$sql = "SELECT * FROM
(
SELECT tagente.id_agente, tagente.alias
FROM tagente
LEFT JOIN tagent_secondary_group tasg
ON tagente.id_agente = tasg.id_agent
INNER JOIN tagente_modulo
ON tagente.id_agente = tagente_modulo.id_agente
$sql_tags_inner
LEFT JOIN tagente_estado
ON tagente_estado.id_agente_modulo = tagente_modulo.id_agente_modulo
WHERE tagente.disabled = 0
AND tagente_modulo.disabled = 0
$module_names_condition
$filter_agent_group
$sql_conditions_tags
$status_filter
$module_type_filter
GROUP BY tagente_modulo.id_agente_modulo
) x
GROUP BY id_agente
$selection_filter";
$modules = db_get_all_rows_sql($sql);
if ($modules === false) return array();
return index_array(db_get_all_rows_sql($sql), 'id_agente', 'alias');
}
/**
* Returns a random name identifier for an agent.
*

View File

@ -2774,33 +2774,33 @@ function modules_get_counter_by_states($state) {
return false;
}
function modules_get_state_condition($state) {
function modules_get_state_condition($state, $prefix = "tae") {
switch ($state) {
case AGENT_MODULE_STATUS_CRITICAL_ALERT:
case AGENT_MODULE_STATUS_CRITICAL_BAD:
return "(
tae.estado = ".AGENT_MODULE_STATUS_CRITICAL_ALERT."
OR tae.estado = ".AGENT_MODULE_STATUS_CRITICAL_BAD."
$prefix.estado = ".AGENT_MODULE_STATUS_CRITICAL_ALERT."
OR $prefix.estado = ".AGENT_MODULE_STATUS_CRITICAL_BAD."
)";
case AGENT_MODULE_STATUS_WARNING_ALERT:
case AGENT_MODULE_STATUS_WARNING:
return "(
tae.estado = ".AGENT_MODULE_STATUS_WARNING_ALERT."
OR tae.estado = ".AGENT_MODULE_STATUS_WARNING."
$prefix.estado = ".AGENT_MODULE_STATUS_WARNING_ALERT."
OR $prefix.estado = ".AGENT_MODULE_STATUS_WARNING."
)";
case AGENT_MODULE_STATUS_UNKNOWN:
return "tae.estado = ".AGENT_MODULE_STATUS_UNKNOWN." ";
return "$prefix.estado = ".AGENT_MODULE_STATUS_UNKNOWN." ";
case AGENT_MODULE_STATUS_NO_DATA:
case AGENT_MODULE_STATUS_NOT_INIT:
return "(
tae.estado = ".AGENT_MODULE_STATUS_NO_DATA."
OR tae.estado = ".AGENT_MODULE_STATUS_NOT_INIT."
$prefix.estado = ".AGENT_MODULE_STATUS_NO_DATA."
OR $prefix.estado = ".AGENT_MODULE_STATUS_NOT_INIT."
)";
case AGENT_MODULE_STATUS_NORMAL_ALERT:
case AGENT_MODULE_STATUS_NORMAL:
return "(
tae.estado = ".AGENT_MODULE_STATUS_NORMAL_ALERT."
OR tae.estado = ".AGENT_MODULE_STATUS_NORMAL."
$prefix.estado = ".AGENT_MODULE_STATUS_NORMAL_ALERT."
OR $prefix.estado = ".AGENT_MODULE_STATUS_NORMAL."
)";
}
// If the state is not an expected state, return no condition

View File

@ -477,7 +477,7 @@ function module_changed_by_multiple_modules (event, id_module, selected) {
jQuery.each (data, function (i, val) {
s = js_html_entity_decode(val);
$('#agents')
.append ($('<option></option>').html (s).attr ("value", val));
.append ($('<option></option>').html (s).attr ("value", i));
$('#agents').fadeIn ('normal');
});

View File

@ -212,63 +212,18 @@ if (is_ajax ()) {
if ($get_agents_json_for_multiple_modules) {
$nameModules = get_parameter('module_name');
$selection_mode = get_parameter('selection_mode','common');
$selection_mode = get_parameter('selection_mode','common') == "all";
$status_modulo = (int) get_parameter ('status_module', -1);
$groups = users_get_groups ($config["id_user"], "AW", false);
$group_id_list = ($groups ? join(",",array_keys($groups)):"0");
$sql = 'SELECT DISTINCT(t1.alias) as name
FROM tagente t1, tagente_modulo t2
WHERE t1.id_agente = t2.id_agente
AND t1.id_grupo IN (' . $group_id_list .')
AND t2.nombre IN (\'' . implode('\',\'', $nameModules) . '\')';
// Status selector
if ($status_modulo == AGENT_MODULE_STATUS_NORMAL) { //Normal
$sql_conditions .= ' estado = 0 AND utimestamp > 0)
OR (t2.id_tipo_modulo IN(21,22,23,100)) ';
}
elseif ($status_modulo == AGENT_MODULE_STATUS_CRITICAL_BAD) { //Critical
$sql_conditions .= ' estado = 1 AND utimestamp > 0 )';
}
elseif ($status_modulo == AGENT_MODULE_STATUS_WARNING) { //Warning
$sql_conditions .= ' estado = 2 AND utimestamp > 0 )';
}
elseif ($status_modulo == AGENT_MODULE_STATUS_NOT_NORMAL) { //Not normal
$sql_conditions .= ' estado <> 0';
}
elseif ($status_modulo == AGENT_MODULE_STATUS_UNKNOWN) { //Unknown
$sql_conditions .= ' estado = 3 AND utimestamp <> 0 )';
}
elseif ($status_modulo == AGENT_MODULE_STATUS_NOT_INIT) { //Not init
$sql_conditions .= ' utimestamp = 0 )
AND t2.id_tipo_modulo NOT IN (21,22,23,100)';
}
if ($status_modulo != -1) {
$sql .= ' AND t2.id_agente_modulo IN (SELECT id_agente_modulo FROM tagente_estado where ' . $sql_conditions;
}
if ($selection_mode == 'common') {
$sql .= 'AND (
SELECT count(t3.nombre)
FROM tagente t3, tagente_modulo t4
WHERE t3.id_agente = t4.id_agente AND t1.nombre = t3.nombre
AND t4.nombre IN (\'' . implode('\',\'', $nameModules) . '\')) = '.count($nameModules);
}
$sql .= ' ORDER BY t1.alias';
$nameAgents = db_get_all_rows_sql($sql);
if ($nameAgents == false)
$nameAgents = array();
foreach ($nameAgents as $nameAgent) {
$names[] = io_safe_output($nameAgent['name']);
}
$names = select_agents_for_module_group(
$nameModules,
$selection_mode,
array (
'status' => $status_modulo
),
"AW"
);
echo json_encode($names);
return;
}