Merge branch 'ent-13421-filtros-de-vista-arbol-en-la-vista-de-grupos-funcionan-mal' into 'develop'

fix filter groups tree pandora_enterprise#13231

See merge request artica/pandorafms!7381
This commit is contained in:
Diego Muñoz-Reja 2024-05-21 10:10:58 +00:00
commit 6f2ca4d161
4 changed files with 114 additions and 284 deletions

View File

@ -771,12 +771,7 @@ if ($tab == 'tree') {
}
$search_group_string = get_parameter('search_group_string', '');
$search_agent_string = get_parameter('search_agent_string', '');
$agent_status = get_parameter('agent_status', '');
$show_not_init_agents = get_parameter('show_not_init_agents', 1);
$show_not_init_modules = get_parameter('show_not_init_modules', 1);
$show_full_hirearchy = get_parameter('show_full_hirearchy', 1);
$show_full_hirearchy = get_parameter('show_full_hirearchy', 0);
$table->data = [];
$table->head = [];
@ -800,42 +795,7 @@ if ($tab == 'tree') {
)
);
$agents_status_list = agents_status_list();
$table->data[0][1] = html_print_label_input_block(
__('Search by agent status').ui_print_help_tip(__('Shows the groups that contain an agent with the status that has been searched'), true),
html_print_select(
$agents_status_list,
'agent_status',
$agent_status,
'',
__('All'),
'',
true,
false,
false,
'w200p',
false,
'width: 100%;'
)
);
$table->data[1][0] = html_print_label_input_block(
__('Search by agent').ui_print_help_tip(__('Shows groups that contain an agent matching the search'), true),
html_print_input_text(
'search_agent_string',
$search_agent_string,
'',
25,
255,
true,
false,
false,
'',
)
);
$table->data[1][1] = html_print_label_input_block(
__('Show full hierarchy'),
html_print_checkbox_switch_extended(
'show_full_hirearchy',
@ -848,9 +808,8 @@ if ($tab == 'tree') {
)
);
$table->data[3][0] = ' ';
$table->data[3][1] = html_print_submit_button(
$table->data[0][2] .= ' ';
$table->data[0][2] .= html_print_submit_button(
__('Filter'),
'filter',
false,
@ -865,8 +824,6 @@ if ($tab == 'tree') {
$form .= html_print_table($table, true);
$form .= '</form>';
ui_toggle(
$form,
'<span class="subsection_header_title">'.__('Filters').'</span>',
@ -1207,8 +1164,7 @@ $tab = 'group_edition';
<script type="text/javascript">
let show_full_hirearchy = "<?php echo (isset($show_full_hirearchy) === true) ? $show_full_hirearchy : ''; ?>";
let show_not_init_agents = "<?php echo (isset($show_not_init_agents) === true) ? $show_not_init_agents : ''; ?>";
let show_not_init_modules = "<?php echo (isset($show_not_init_modules) === true) ? $show_not_init_modules : ''; ?>";
let search_group_string = "<?php echo (isset($search_group_string) === true) ? $search_group_string : ''; ?>";
$('#checkbox-show_full_hirearchy').on("change", function() {
if (show_full_hirearchy == 1) {
@ -1221,17 +1177,17 @@ $tab = 'group_edition';
$('#button-filter').on('click', function(event) {
event.preventDefault();
load_tree(show_full_hirearchy, show_not_init_agents, show_not_init_modules);
load_tree(show_full_hirearchy);
});
window.addEventListener('load', function() {
load_tree(show_full_hirearchy, show_not_init_agents, show_not_init_modules);
load_tree(show_full_hirearchy);
});
function load_tree(show_full_hirearchy, show_not_init_agents, show_not_init_modules) {
function load_tree(show_full_hirearchy) {
var treeController = TreeController.getController();
treeController.meta = <?php echo (is_metaconsole() === true) ? 1 : 0; ?>;
@ -1245,17 +1201,8 @@ $tab = 'group_edition';
parameters['getChildren'] = 1;
parameters['type'] = "<?php echo $tab; ?>";
parameters['filter'] = {};
parameters['filter']['searchHirearchy'] = show_full_hirearchy;
parameters['filter']['searchGroup'] = $('#text-search_group_string').val();
parameters['filter']['searchAgent'] = $('#text-search_agent_string').val();
parameters['filter']['statusAgent'] = $('#agent_status option:selected').val();
parameters['filter']['searchModule'] = '';
parameters['filter']['statusModule'] = '';
parameters['filter']['groupID'] = '';
parameters['filter']['tagID'] = '';
parameters['filter']['searchHirearchy'] = 1;
parameters['filter']['show_full_hirearchy'] = show_full_hirearchy;
parameters['filter']['show_not_init_agents'] = show_not_init_agents;
parameters['filter']['show_not_init_modules'] = show_not_init_modules;
$.ajax({
type: "POST",
@ -1273,44 +1220,7 @@ $tab = 'group_edition';
tree: data.tree,
baseURL: "<?php echo ui_get_full_url(false, false, false, is_metaconsole()); ?>",
ajaxURL: "<?php echo ui_get_full_url('ajax.php', false, false, false); ?>",
filter: parameters['filter'],
counterTitles: {
total: {
agents: "<?php echo __('Total agents'); ?>",
modules: "<?php echo __('Total modules'); ?>",
none: "<?php echo __('Total'); ?>"
},
alerts: {
agents: "<?php echo __('Fired alerts'); ?>",
modules: "<?php echo __('Fired alerts'); ?>",
none: "<?php echo __('Fired alerts'); ?>"
},
critical: {
agents: "<?php echo __('Critical agents'); ?>",
modules: "<?php echo __('Critical modules'); ?>",
none: "<?php echo __('Critical'); ?>"
},
warning: {
agents: "<?php echo __('Warning agents'); ?>",
modules: "<?php echo __('Warning modules'); ?>",
none: "<?php echo __('Warning'); ?>"
},
unknown: {
agents: "<?php echo __('Unknown agents'); ?>",
modules: "<?php echo __('Unknown modules'); ?>",
none: "<?php echo __('Unknown'); ?>"
},
not_init: {
agents: "<?php echo __('Not init agents'); ?>",
modules: "<?php echo __('Not init modules'); ?>",
none: "<?php echo __('Not init'); ?>"
},
ok: {
agents: "<?php echo __('Normal agents'); ?>",
modules: "<?php echo __('Normal modules'); ?>",
none: "<?php echo __('Normal'); ?>"
}
}
filter: parameters['filter']
});
}
},

View File

@ -160,37 +160,21 @@ class TreeGroup extends Tree
$processed_items = $this->getProcessedGroups();
if (empty($processed_items) === false) {
// Filter by group name. This should be done after rerieving
// the items cause we need the possible items descendants.
if (empty($this->filter['searchGroup']) === false) {
// Save the groups which intersect with the user groups.
$groups = db_get_all_rows_filter(
'tgrupo',
['nombre' => '%'.$this->filter['searchGroup'].'%']
);
if ($groups === false) {
$groups = [];
$parentGroup = [
'name' => '',
'children' => $processed_items,
];
if (isset($this->filter['searchHirearchy']) === true
&& (bool) $this->filter['searchHirearchy'] === true
) {
self::getFullHierarchyGroups($parentGroup, $this->filter['searchGroup']);
$result = $parentGroup['children'];
} else {
$result = self::pruneHierarchyGroups($parentGroup, $this->filter['searchGroup']);
}
$userGroupsACL = $this->userGroupsACL;
$ids_hash = array_reduce(
$groups,
function ($userGroups, $group) use ($userGroupsACL) {
$group_id = $group['id_grupo'];
if (isset($userGroupsACL[$group_id]) === true) {
$userGroups[$group_id] = $userGroupsACL[$group_id];
}
return $userGroups;
},
[]
);
$result = self::extractGroupsWithIDs(
$processed_items,
$ids_hash
);
$processed_items = ($result === false) ? [] : $result;
}
@ -200,7 +184,7 @@ class TreeGroup extends Tree
$processed_items,
$this->filter['groupID'],
'group',
$this->strictACL
false
);
$processed_items = ($result === false) ? [] : [$result];
@ -564,23 +548,61 @@ class TreeGroup extends Tree
}
private static function extractGroupsWithIDs($groups, $ids_hash)
/**
* Get all hierarchy groups.
*
* @param array $group Groups.
* @param string $search Serach.
*
* @return boolean
*/
protected static function getFullHierarchyGroups(&$group, $search): bool
{
$result_groups = [];
foreach ($groups as $group) {
if (isset($ids_hash[$group['id']])) {
$result_groups[] = $group;
} else if (!empty($group['children'])) {
$result = self::extractGroupsWithIDs($group['children'], $ids_hash);
// Item found on children
if (!empty($result)) {
$result_groups = array_merge($result_groups, $result);
if (stripos($group['name'], $search) !== false) {
return true;
}
if (empty($group['children'] ?? []) === true) {
return false;
}
$has_occurrences = false;
$count_children = count($group['children']);
for ($i = 0; $i < $count_children; $i++) {
if (self::getFullHierarchyGroups($group['children'][$i], $search) === false) {
unset($group['children'][$i]);
} else {
$has_occurrences = true;
}
}
return $result_groups;
return $has_occurrences;
}
/**
* Get prune groups.
*
* @param array $group Groups.
* @param string $search Serach.
*
* @return array
*/
protected static function pruneHierarchyGroups($group, $search): array
{
$matches = [];
foreach (($group['children'] ?? []) as $child) {
if (stripos($child['name'], $search) !== false) {
$matches[] = $child;
} else {
$matches = [
...$matches,
...self::pruneHierarchyGroups($child, $search),
];
}
}
return $matches;
}
@ -622,7 +644,7 @@ class TreeGroup extends Tree
protected function getDisplayHierarchy()
{
if (isset($this->filter['searchHirearchy']) === false) {
$this->filter['searchHirearchy'] = '';
$this->filter['searchHirearchy'] = false;
}
return $this->filter['searchHirearchy'] ||

View File

@ -91,24 +91,8 @@ class TreeGroupEdition extends TreeGroup
*/
protected function getProcessedGroups()
{
$processed_groups = [];
// Index and process the groups.
$groups = $this->getGroupCounters();
// If user have not permissions in parent, set parent node to 0 (all)
// Avoid to do foreach for admins.
if (users_can_manage_group_all('AR') === false) {
foreach ($groups as $id => $group) {
if (isset($this->userGroups[$groups[$id]['parent']]) === false) {
$groups[$id]['parent'] = 0;
}
}
}
// Build the group hierarchy.
if (isset($this->filter['show_full_hirearchy']) === false
|| (isset($this->filter['show_full_hirearchy']) === true && (bool) $this->filter['show_full_hirearchy'] === true)
) {
$groups = $this->getGroups();
foreach ($groups as $id => $group) {
if (isset($groups[$id]['parent']) === true
&& ($groups[$id]['parent'] != 0)
@ -125,7 +109,6 @@ class TreeGroupEdition extends TreeGroup
$groups[$id]['have_parent'] = true;
}
}
}
// Sort the children groups.
foreach ($groups as $id => $group) {
@ -162,101 +145,9 @@ class TreeGroupEdition extends TreeGroup
*
* @return mixed
*/
protected function getGroupCounters()
protected function getGroups()
{
$messages = [
'confirm' => __('Confirm'),
'cancel' => __('Cancel'),
'messg' => __('Are you sure?'),
];
$group_acl = '';
$search_agent = '';
$status_agent = '';
$inner_agent = '';
if ((bool) is_metaconsole() === true) {
if (users_can_manage_group_all('AR') === false) {
$user_groups_str = implode(',', $this->userGroupsArray);
$group_acl = sprintf(
' AND tgrupo.id_grupo IN (%s) ',
$user_groups_str
);
}
if (isset($this->filter['searchAgent']) === true && empty($this->filter['searchAgent']) === false
|| isset($this->filter['statusAgent']) === true && strlen($this->filter['statusAgent']) > 0
) {
$inner_agent = 'INNER JOIN tmetaconsole_agent ON tgrupo.id_grupo = tmetaconsole_agent.id_grupo';
}
if (isset($this->filter['searchAgent']) === true && empty($this->filter['searchAgent']) === false) {
$search_agent = ' AND tmetaconsole_agent.alias LIKE "%'.$this->filter['searchAgent'].'%" ';
}
if (isset($this->filter['statusAgent']) === true && strlen($this->filter['statusAgent']) > 0) {
switch ($this->filter['statusAgent']) {
case AGENT_STATUS_NORMAL:
$status_agent = ' AND (
tmetaconsole_agent.critical_count = 0
AND tmetaconsole_agent.warning_count = 0
AND tmetaconsole_agent.unknown_count = 0
AND tmetaconsole_agent.normal_count > 0)';
break;
case AGENT_STATUS_WARNING:
$status_agent = ' AND (
tmetaconsole_agent.critical_count = 0
AND tmetaconsole_agent.warning_count > 0
AND tmetaconsole_agent.total_count > 0)';
break;
case AGENT_STATUS_CRITICAL:
$status_agent = ' AND tmetaconsole_agent.critical_count > 0';
break;
case AGENT_STATUS_UNKNOWN:
$status_agent = ' AND (
tmetaconsole_agent.critical_count = 0
AND tmetaconsole_agent.warning_count = 0
AND tmetaconsole_agent.unknown_count > 0)';
break;
case AGENT_STATUS_NOT_NORMAL:
$status_agent = ' AND (
tmetaconsole_agent.normal_count <> total_count
OR tmetaconsole_agent.total_count = notinit_count)';
break;
case AGENT_STATUS_NOT_INIT:
$status_agent = ' AND (
tmetaconsole_agent.total_count = 0
OR tmetaconsole_agent.total_count = notinit_count)';
break;
default:
// Nothing to do.
break;
}
}
$sql = sprintf(
'SELECT tgrupo.id_grupo AS gid,
tgrupo.nombre as name,
tgrupo.parent,
tgrupo.icon
FROM tgrupo
%s
WHERE 1=1
%s
%s
%s ',
$inner_agent,
$search_agent,
$status_agent,
$group_acl
);
} else {
if (users_can_manage_group_all('AR') === false) {
$user_groups_str = implode(',', $this->userGroupsArray);
$group_acl = sprintf(
@ -275,7 +166,6 @@ class TreeGroupEdition extends TreeGroup
%s',
$group_acl
);
}
$stats = db_get_all_rows_sql($sql);
$group_stats = [];
@ -290,6 +180,12 @@ class TreeGroupEdition extends TreeGroup
$group_stats[$group['gid']]
);
if (is_management_allowed() === true) {
$messages = [
'confirm' => __('Confirm'),
'cancel' => __('Cancel'),
'messg' => __('Are you sure?'),
];
$group_stats[$group['gid']]['delete']['messages'] = $messages;
$group_stats[$group['gid']]['edit'] = 1;
}

View File

@ -197,7 +197,7 @@ function users_get_groups_for_select(
*
* @return array
*/
function get_group_ancestors($group_id, $groups)
function get_group_ancestors($group_id, $groups, $forceNotPropagate=false)
{
if ($group_id == 0) {
return 0;
@ -209,9 +209,11 @@ function get_group_ancestors($group_id, $groups)
$parent = $groups[$group_id]['parent'];
if ($forceNotPropagate === false) {
if ($groups[$group_id]['propagate'] == 0) {
return $group_id;
}
}
$r = get_group_ancestors($parent, $groups);