Rewrited the processing, sorting and hierarchy building of the groups.
Fixes ticket #3084. (cherry picked from commit 1770adb04d1f886eb9d5080cab76c860defa9c86)
This commit is contained in:
parent
7e830b363f
commit
af19df20cf
|
@ -1123,28 +1123,66 @@ class Tree {
|
||||||
return strcmp($a["name"], $b["name"]);
|
return strcmp($a["name"], $b["name"]);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function getGroupsChildren(&$groups, &$groups_tmp, $parent_id, $server = false, $remove_empty = false) {
|
protected function getProcessedGroups ($items, $remove_empty = false) {
|
||||||
$children = array();
|
$processed_groups = array();
|
||||||
foreach ($groups as $key => $group) {
|
// Index and process the groups
|
||||||
if (isset($group['parent']) && $group['parent'] == $parent_id) {
|
$groups = array();
|
||||||
unset($groups[$key]);
|
foreach ($items as $item) {
|
||||||
|
$groups[$item['id']] = $this->getProcessedItem($item);
|
||||||
|
}
|
||||||
|
// Build the group hierarchy
|
||||||
|
foreach ($groups as $id => $group) {
|
||||||
|
if (!isset($groups[$id]['parent']))
|
||||||
|
continue;
|
||||||
|
$parent = $groups[$id]['parent'];
|
||||||
|
// Parent exists
|
||||||
|
if (isset($groups[$parent])) {
|
||||||
|
if (!isset($groups[$parent]['children']))
|
||||||
|
$groups[$parent]['children'] = array();
|
||||||
|
// Store a reference to the group into the parent
|
||||||
|
$groups[$parent]['children'][] = &$groups[$id];
|
||||||
|
|
||||||
$children_aux = $this->getProcessedItem($group, $server, $groups, $groups_tmp, $remove_empty);
|
// Add the child counters to the parent
|
||||||
if (!empty($children_aux))
|
if (isset($groups[$id]['counters']) && !empty($groups[$id]['counters'])) {
|
||||||
$children[] = $children_aux;
|
foreach ($groups[$id]['counters'] as $type => $value) {
|
||||||
|
if (isset($groups[$parent]['counters'][$type]))
|
||||||
|
$groups[$parent]['counters'][$type] += $value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
foreach ($groups_tmp as $key_tmp => $group_tmp) {
|
|
||||||
if (isset($group_tmp['parent']) && $group_tmp['parent'] == $parent_id) {
|
|
||||||
unset($groups_tmp[$key_tmp]);
|
|
||||||
|
|
||||||
$children[] = $group_tmp;
|
// This group was introduced into a parent
|
||||||
|
$groups[$id]['have_parent'] = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
usort($children, array("Tree", "cmpSortNames"));
|
if ($remove_empty) {
|
||||||
$children = array_filter($children);
|
// Filter empty groups
|
||||||
|
$groups = array_filter($groups, function ($group) {
|
||||||
|
return (isset($group['counters']) &&
|
||||||
|
isset($group['counters']['total']) &&
|
||||||
|
!empty($group['counters']['total']));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
// Sort the children groups
|
||||||
|
foreach ($groups as $id => $group) {
|
||||||
|
if (isset($groups[$id]['children'])) {
|
||||||
|
if ($remove_empty) {
|
||||||
|
// Remove empty childs
|
||||||
|
$groups[$id]['children'] = array_filter($groups[$id]['children'], function ($g) use ($groups) {
|
||||||
|
return (!empty($g) && isset($g['id']) && isset($groups[$g['id']]));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
usort($groups[$id]['children'], array("Tree", "cmpSortNames"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Extract the root groups
|
||||||
|
foreach ($groups as $group) {
|
||||||
|
if (!$group['have_parent'])
|
||||||
|
$processed_groups[] = $group;
|
||||||
|
}
|
||||||
|
// Sort the root groups
|
||||||
|
usort($processed_groups, array("Tree", "cmpSortNames"));
|
||||||
|
|
||||||
return $children;
|
return $processed_groups;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function getProcessedItem ($item, $server = false, &$items = array(), &$items_tmp = array(), $remove_empty = false) {
|
protected function getProcessedItem ($item, $server = false, &$items = array(), &$items_tmp = array(), $remove_empty = false) {
|
||||||
|
@ -1291,33 +1329,9 @@ class Tree {
|
||||||
if (isset($item['total_fired_count']))
|
if (isset($item['total_fired_count']))
|
||||||
$counters['alerts'] = $item['total_fired_count'];
|
$counters['alerts'] = $item['total_fired_count'];
|
||||||
|
|
||||||
// Get the children of the group (special case)
|
|
||||||
if ($processed_item['type'] == 'group') {
|
|
||||||
$children = $this->getGroupsChildren($items, $items_tmp, $item['id'], $server, $remove_empty);
|
|
||||||
|
|
||||||
if (!empty($children)) {
|
|
||||||
$processed_item['children'] = $children;
|
|
||||||
|
|
||||||
foreach ($children as $key => $child) {
|
|
||||||
if (isset($child['counters'])) {
|
|
||||||
foreach ($child['counters'] as $type => $value) {
|
|
||||||
if (isset($counters[$type]))
|
|
||||||
$counters[$type] += $value;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!empty($counters))
|
if (!empty($counters))
|
||||||
$processed_item['counters'] = $counters;
|
$processed_item['counters'] = $counters;
|
||||||
|
|
||||||
if ($remove_empty && $processed_item['type'] == 'group'
|
|
||||||
&& (!isset($processed_item['counters']['total'])
|
|
||||||
|| empty($processed_item['counters']['total']))) {
|
|
||||||
$processed_item = array();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!empty($processed_item))
|
if (!empty($processed_item))
|
||||||
$processed_item['is_processed'] = true;
|
$processed_item['is_processed'] = true;
|
||||||
|
|
||||||
|
@ -1987,25 +2001,32 @@ class Tree {
|
||||||
$items = group_get_data($config['id_user'], $this->strictACL, $this->acltags, false, 'tree', $agent_filter, $module_filter);
|
$items = group_get_data($config['id_user'], $this->strictACL, $this->acltags, false, 'tree', $agent_filter, $module_filter);
|
||||||
|
|
||||||
// Build the group and tag hierarchy
|
// Build the group and tag hierarchy
|
||||||
$processed_items = array();
|
$processed_groups = array();
|
||||||
$processed_items_tmp = array();
|
$processed_tags = array();
|
||||||
foreach ($items as $key => $item) {
|
|
||||||
unset($items[$key]);
|
|
||||||
|
|
||||||
$processed_item = $this->getProcessedItem($item, false, $items, $processed_items_tmp, true);
|
foreach ($items as $key => $item) {
|
||||||
if (!empty($processed_item)
|
$processed_item = $this->getProcessedItem($item);
|
||||||
&& isset($processed_item['counters'])
|
if ($processed_item['type'] == 'tag') {
|
||||||
&& isset($processed_item['counters']['total'])
|
if (!empty($processed_item) &&
|
||||||
&& !empty($processed_item['counters']['total'])) {
|
isset($processed_item['counters']) &&
|
||||||
$processed_items_tmp[] = $processed_item;
|
isset($processed_item['counters']['total']) &&
|
||||||
|
!empty($processed_item['counters']['total'])) {
|
||||||
|
$processed_tags[] = $processed_item;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!empty($processed_items_tmp)) {
|
else {
|
||||||
usort($processed_items_tmp, array("Tree", "cmpSortNames"));
|
$processed_groups[] = $processed_item;
|
||||||
// array_filter clean the empty elements
|
|
||||||
$processed_items = array_filter($processed_items_tmp);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Build the groups hierarchy
|
||||||
|
$processed_groups = $this->getProcessedGroups($processed_groups, true);
|
||||||
|
// Sort tags
|
||||||
|
usort($processed_tags, array("Tree", "cmpSortNames"));
|
||||||
|
|
||||||
|
// Join tags and groups
|
||||||
|
$processed_items = array_merge($processed_groups, $processed_tags);
|
||||||
|
}
|
||||||
else {
|
else {
|
||||||
$unmerged_items = array();
|
$unmerged_items = array();
|
||||||
|
|
||||||
|
@ -2018,18 +2039,25 @@ class Tree {
|
||||||
$items = group_get_data($config['id_user'], $this->strictACL, $this->acltags, false, 'tree', $agent_filter, $module_filter);
|
$items = group_get_data($config['id_user'], $this->strictACL, $this->acltags, false, 'tree', $agent_filter, $module_filter);
|
||||||
|
|
||||||
// Build the group and tag hierarchy
|
// Build the group and tag hierarchy
|
||||||
$processed_items = array();
|
$processed_groups = array();
|
||||||
$processed_items_tmp = array();
|
$processed_tags = array();
|
||||||
|
|
||||||
foreach ($items as $key => $item) {
|
foreach ($items as $key => $item) {
|
||||||
unset($items[$key]);
|
$processed_item = $this->getProcessedItem($item);
|
||||||
$processed_items_tmp[] = $this->getProcessedItem($item, $server, $items, $processed_items_tmp);
|
if ($processed_item['type'] == 'tag')
|
||||||
}
|
$processed_tags[] = $processed_item;
|
||||||
if (!empty($processed_items_tmp)) {
|
else
|
||||||
usort($processed_items_tmp, array("Tree", "cmpSortNames"));
|
$processed_groups[] = $processed_item;
|
||||||
// array_filter clean the empty elements
|
|
||||||
$processed_items = array_filter($processed_items_tmp);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Build the groups hierarchy
|
||||||
|
$processed_groups = $this->getProcessedGroups($processed_groups);
|
||||||
|
// Sort tags
|
||||||
|
usort($processed_tags, array("Tree", "cmpSortNames"));
|
||||||
|
|
||||||
|
// Join tags and groups
|
||||||
|
$processed_items = array_merge($processed_groups, $processed_tags);
|
||||||
|
|
||||||
$unmerged_items += $processed_items;
|
$unmerged_items += $processed_items;
|
||||||
|
|
||||||
metaconsole_restore_db();
|
metaconsole_restore_db();
|
||||||
|
@ -2105,16 +2133,7 @@ class Tree {
|
||||||
|
|
||||||
$items = $this->getItems();
|
$items = $this->getItems();
|
||||||
|
|
||||||
// Build the group hierarchy
|
$processed_items = $this->getProcessedGroups($items, true);
|
||||||
while (($item = array_shift($items)) !== null) {
|
|
||||||
$processed_item = $this->getProcessedItem($item, false, $items, $processed_items, true);
|
|
||||||
|
|
||||||
if (!empty($processed_item)
|
|
||||||
&& isset($processed_item['counters'])
|
|
||||||
&& isset($processed_item['counters']['total'])
|
|
||||||
&& !empty($processed_item['counters']['total']))
|
|
||||||
$processed_items[] = $processed_item;
|
|
||||||
}
|
|
||||||
|
|
||||||
// groupID filter. To access the view from tactical views f.e.
|
// groupID filter. To access the view from tactical views f.e.
|
||||||
if (!empty($processed_items) && !empty($this->filter['groupID'])) {
|
if (!empty($processed_items) && !empty($this->filter['groupID'])) {
|
||||||
|
|
Loading…
Reference in New Issue