#13636 vc performance recursive loops

This commit is contained in:
Jonathan 2024-05-07 10:52:56 +02:00
parent 3444e41dd4
commit 1718e8b5c5
2 changed files with 39 additions and 17 deletions

View File

@ -3886,10 +3886,27 @@ function visual_map_translate_module_status($module_status)
* *
* @return integer The status of the given layout. * @return integer The status of the given layout.
*/ */
function visual_map_get_layout_status($layout_id, $status_data=[], $depth=0) function visual_map_get_layout_status(
{ $layout_id,
$status_data=[],
$depth=0,
$exclude_recursive=[],
&$num_elements_by_status=[
VISUAL_MAP_STATUS_CRITICAL_BAD => 0,
VISUAL_MAP_STATUS_CRITICAL_ALERT => 0,
VISUAL_MAP_STATUS_NORMAL => 0,
VISUAL_MAP_STATUS_WARNING => 0,
VISUAL_MAP_STATUS_UNKNOWN => 0,
VISUAL_MAP_STATUS_WARNING_ALERT => 0,
]
) {
global $config; global $config;
if (in_array($layout_id, $exclude_recursive) === true) {
return VISUAL_MAP_STATUS_UNKNOWN;
}
$exclude_recursive[] = $layout_id;
// TODO: Implement this limit into the setup. // TODO: Implement this limit into the setup.
if ($depth > 10) { if ($depth > 10) {
return VISUAL_MAP_STATUS_UNKNOWN; return VISUAL_MAP_STATUS_UNKNOWN;
@ -3951,15 +3968,6 @@ function visual_map_get_layout_status($layout_id, $status_data=[], $depth=0)
sort_by_column($valid_layout_items, 'id_metaconsole'); sort_by_column($valid_layout_items, 'id_metaconsole');
} }
$num_elements_by_status = [
VISUAL_MAP_STATUS_CRITICAL_BAD => 0,
VISUAL_MAP_STATUS_CRITICAL_ALERT => 0,
VISUAL_MAP_STATUS_NORMAL => 0,
VISUAL_MAP_STATUS_WARNING => 0,
VISUAL_MAP_STATUS_UNKNOWN => 0,
VISUAL_MAP_STATUS_WARNING_ALERT => 0,
];
$meta_connected_to = null; $meta_connected_to = null;
foreach ($valid_layout_items as $layout_item_data) { foreach ($valid_layout_items as $layout_item_data) {
@ -4018,7 +4026,9 @@ function visual_map_get_layout_status($layout_id, $status_data=[], $depth=0)
$status = visual_map_get_layout_status( $status = visual_map_get_layout_status(
$layout_item_data['id_layout_linked'], $layout_item_data['id_layout_linked'],
$layout_item_data, $layout_item_data,
($depth + 1) ($depth + 1),
$exclude_recursive,
$num_elements_by_status
); );
} else if (!empty($layout_item_data['id_agente_modulo'])) { } else if (!empty($layout_item_data['id_agente_modulo'])) {
// Module. // Module.

View File

@ -2654,6 +2654,18 @@ class Item extends CachedModel
*/ */
public static function checkLayoutAlertsRecursive(array $item, array $visitedLayouts=[]) public static function checkLayoutAlertsRecursive(array $item, array $visitedLayouts=[])
{ {
static $cache = [];
if (isset($cache[$item['id_layout_linked']]) === true && empty($item['id_layout_linked']) === false) {
return $cache[$item['id_layout_linked']];
}
if (in_array($item['id_layout'], $visitedLayouts) === true) {
// Item has no linked layout or it has already been visited (avoid infinite loop caused by circular references).
$cache[$item['id_layout']] = false;
return false;
}
if (isset($item['type']) === true) { if (isset($item['type']) === true) {
$excludedItemTypes = [ $excludedItemTypes = [
22, 22,
@ -2668,6 +2680,7 @@ class Item extends CachedModel
]; ];
if (in_array($item['type'], $excludedItemTypes) === true) { if (in_array($item['type'], $excludedItemTypes) === true) {
$cache[$item['id_layout']] = false;
return false; return false;
} }
} }
@ -2712,15 +2725,11 @@ class Item extends CachedModel
// Item has a triggered alert. // Item has a triggered alert.
if ($firedAlert !== false) { if ($firedAlert !== false) {
$cache[$item['id_layout']] = true;
return true; return true;
} }
} }
if ($linkedLayoutID === 0 || in_array($linkedLayoutID, $visitedLayouts) === true) {
// Item has no linked layout or it has already been visited (avoid infinite loop caused by circular references).
return false;
}
$filter = ['id_layout' => $linkedLayoutID]; $filter = ['id_layout' => $linkedLayoutID];
$linkedLayoutItems = \db_get_all_rows_filter( $linkedLayoutItems = \db_get_all_rows_filter(
@ -2737,15 +2746,18 @@ class Item extends CachedModel
if ($linkedLayoutItems === false) { if ($linkedLayoutItems === false) {
// There are no items in the linked visual console. Nothing to check. // There are no items in the linked visual console. Nothing to check.
$cache[$item['id_layout']] = false;
return false; return false;
} }
foreach ($linkedLayoutItems as $linkedLayoutItem) { foreach ($linkedLayoutItems as $linkedLayoutItem) {
if (self::checkLayoutAlertsRecursive($linkedLayoutItem, $visitedLayouts)) { if (self::checkLayoutAlertsRecursive($linkedLayoutItem, $visitedLayouts)) {
$cache[$item['id_layout']] = true;
return true; return true;
} }
} }
$cache[$item['id_layout']] = false;
return false; return false;
} }