Merge branch '2611-Cache_de_vista_de_arbol_y_vista_de_grupos' into 'develop'

improve performance on group view

See merge request artica/pandorafms!1735
This commit is contained in:
nramon 2018-09-06 15:46:21 +02:00
commit 6511f67600
3 changed files with 80 additions and 155 deletions

View File

@ -411,111 +411,37 @@ function groupview_monitor_fired_alerts ($group_array, $strict_user = false, $id
AND times_fired > 0"); AND times_fired > 0");
} }
function groupview_get_groups_list($id_user = false, $user_strict = false, $access = 'AR', $force_group_and_tag = true, $returnAllGroup = false) { function groupview_get_groups_list($id_user = false, $access = 'AR') {
global $config;
if ($id_user == false) {
$id_user = $config['id_user'];
}
$acltags = users_get_groups($id_user, $access, true, true);
$result_list = groupview_get_data ($id_user, $user_strict, $acltags,
$returnAllGroup, array(), array(), $access);
return $result_list;
}
function groupview_get_data ($id_user = false, $user_strict = false, $acltags, $returnAllGroup = false, $agent_filter = array(), $module_filter = array(), $access = 'AR') {
global $config; global $config;
if ($id_user == false) { if ($id_user == false) {
$id_user = $config['id_user']; $id_user = $config['id_user'];
} }
$user_groups = array(); $user_groups = users_get_groups($id_user, $access, true, false);
$user_tags = array();
foreach ($acltags as $item) { $groups_with_privileges = implode(',', array_keys($user_groups));
$user_groups[$item["id_grupo"]] = $item["nombre"];
if ($item["tags"] != '') {
$tags_group = explode(',', $item["tags"]);
foreach ($tags_group as $tag) { //change ALL for 0
$user_tags[$tag] = tags_get_name($tag); $user_groups[0] = 0;
}
}
}
$groups_with_privileges = implode(',', array_keys($acltags));
if (!$user_strict) $user_groups_ids = implode(',', array_keys($user_groups));
$acltags[0] = 0;
$user_groups_ids = implode(',', array_keys($acltags));
if (!empty($user_groups_ids)) { if (!empty($user_groups_ids)) {
if (is_metaconsole()) { $table = is_metaconsole() ? 'tmetaconsole_agent' : 'tagente';
switch ($config["dbtype"]) { $table_secondary = is_metaconsole()
case "mysql": ? 'tmetaconsole_agent_secondary_group'
$list_groups = db_get_all_rows_sql(" : 'tagent_secondary_group';
SELECT *
FROM tgrupo $list_groups = db_get_all_rows_sql("
WHERE id_grupo IN (" . $user_groups_ids . ") SELECT *
AND ( FROM tgrupo
id_grupo IN (SELECT id_grupo FROM tmetaconsole_agent WHERE disabled = 0) WHERE id_grupo IN ($user_groups_ids)
OR id_grupo IN (SELECT id_group FROM tmetaconsole_agent_secondary_group WHERE id_group IN (" . $user_groups_ids . ")) AND (
) id_grupo IN (SELECT id_grupo FROM $table WHERE disabled = 0)
ORDER BY nombre COLLATE utf8_general_ci ASC"); OR id_grupo IN (SELECT id_group FROM $table_secondary WHERE id_group IN ($user_groups_ids))
break; )
case "postgresql": ORDER BY nombre COLLATE utf8_general_ci ASC"
$list_groups = db_get_all_rows_sql(" );
SELECT *
FROM tgrupo
WHERE id_grupo IN (" . $user_groups_ids . ")
AND id_grupo IN (SELECT id_grupo FROM tmetaconsole_agent WHERE disabled = 0)
ORDER BY nombre ASC");
break;
case "oracle":
$list_groups = db_get_all_rows_sql("
SELECT *
FROM tgrupo
WHERE id_grupo IN (" . $user_groups_ids . ")
AND id_grupo IN (SELECT id_grupo FROM tmetaconsole_agent WHERE disabled = 0)
ORDER BY nombre ASC");
break;
}
}
else {
switch ($config["dbtype"]) {
case "mysql":
$list_groups = db_get_all_rows_sql("
SELECT *
FROM tgrupo
WHERE id_grupo IN (" . $user_groups_ids . ")
AND (
id_grupo IN (SELECT id_grupo FROM tagente WHERE disabled = 0)
OR id_grupo IN (SELECT id_group FROM tagent_secondary_group WHERE id_group IN (" . $user_groups_ids . "))
)
ORDER BY nombre COLLATE utf8_general_ci ASC");
break;
case "postgresql":
$list_groups = db_get_all_rows_sql("
SELECT *
FROM tgrupo
WHERE id_grupo IN (" . $user_groups_ids . ")
AND id_grupo IN (SELECT id_grupo FROM tagente WHERE disabled = 0)
ORDER BY nombre ASC");
break;
case "oracle":
$list_groups = db_get_all_rows_sql("
SELECT *
FROM tgrupo
WHERE id_grupo IN (" . $user_groups_ids . ")
AND id_grupo IN (SELECT id_grupo FROM tagente WHERE disabled = 0)
ORDER BY nombre ASC");
break;
}
}
} }
//Add the group "All" at first //Add the group "All" at first
@ -527,66 +453,64 @@ function groupview_get_data ($id_user = false, $user_strict = false, $acltags, $
$list_groups = array($group_all); $list_groups = array($group_all);
} }
if (!$user_strict) { //Takes the parents even without agents, first ids
//Takes the parents even without agents, first ids $fathers_id = '';
$fathers_id = ''; $list_father_groups = array();
$list_father_groups = array(); foreach ($list_groups as $group) {
foreach ($list_groups as $group) { if ($group['parent'] != '') {
if ($group['parent'] != '') { $grup = $group['parent'];
$grup = $group['parent']; while ($grup != 0) {
while ($grup != 0) { $recursive_fathers = db_get_row_sql ("SELECT parent FROM tgrupo
$recursive_fathers = db_get_row_sql ("SELECT parent FROM tgrupo WHERE id_grupo = " . $grup);
WHERE id_grupo = " . $grup); $grup = $recursive_fathers['parent'];
$grup = $recursive_fathers['parent']; if (!strpos($fathers_id, $grup)) {
if (!strpos($fathers_id, $grup)) { $fathers_id .= ',' . $grup;
$fathers_id .= ',' . $grup;
}
}
if (!strpos($fathers_id, $group['parent'])) {
$fathers_id .= ',' . $group['parent'];
} }
} }
} if (!strpos($fathers_id, $group['parent'])) {
//Eliminate the first comma $fathers_id .= ',' . $group['parent'];
$fathers_id = substr($fathers_id, 1);
while ($fathers_id{0} == ',') {
$fathers_id = substr($fathers_id, 1);
}
//Takes the parents even without agents, complete groups
if ($fathers_id) {
$list_father_groups = db_get_all_rows_sql("
SELECT *
FROM tgrupo
WHERE id_grupo IN (" . $fathers_id . ")
AND id_grupo IN (" . $groups_with_privileges . ")
ORDER BY nombre COLLATE utf8_general_ci ASC");
if (!empty($list_father_groups)) {
//Merges the arrays and eliminates the duplicates groups
$list_groups = array_merge($list_groups, $list_father_groups);
} }
} }
$list_groups = groupview_array_unique_multidim($list_groups, 'id_grupo');
//Order groups (Father-children)
$ordered_groups = groupview_order_groups_for_parents($list_groups);
$ordered_ids = array();
$ordered_ids = groupview_order_group_ids($ordered_groups, $ordered_ids);
$final_list = array();
array_push($final_list, $group_all);
foreach ($ordered_ids as $key) {
if ($key == 'All') {
continue;
}
$complete_group = db_get_row_sql("
SELECT *
FROM tgrupo
WHERE nombre = '" . $key . "'");
array_push($final_list, $complete_group);
}
$list_groups = $final_list;
} }
//Eliminate the first comma
$fathers_id = substr($fathers_id, 1);
while ($fathers_id{0} == ',') {
$fathers_id = substr($fathers_id, 1);
}
//Takes the parents even without agents, complete groups
if ($fathers_id) {
$list_father_groups = db_get_all_rows_sql("
SELECT *
FROM tgrupo
WHERE id_grupo IN (" . $fathers_id . ")
AND id_grupo IN (" . $groups_with_privileges . ")
ORDER BY nombre COLLATE utf8_general_ci ASC");
if (!empty($list_father_groups)) {
//Merges the arrays and eliminates the duplicates groups
$list_groups = array_merge($list_groups, $list_father_groups);
}
}
$list_groups = groupview_array_unique_multidim($list_groups, 'id_grupo');
//Order groups (Father-children)
$ordered_groups = groupview_order_groups_for_parents($list_groups);
$ordered_ids = array();
$ordered_ids = groupview_order_group_ids($ordered_groups, $ordered_ids);
$final_list = array();
array_push($final_list, $group_all);
foreach ($ordered_ids as $key) {
if ($key == 'All') {
continue;
}
$complete_group = db_get_row_sql("
SELECT *
FROM tgrupo
WHERE nombre = '" . $key . "'");
array_push($final_list, $complete_group);
}
$list_groups = $final_list;
$list = array(); $list = array();
foreach ($list_groups as $group) { foreach ($list_groups as $group) {
$list[$group['id_grupo']]['_name_'] = $group['nombre']; $list[$group['id_grupo']]['_name_'] = $group['nombre'];

View File

@ -187,7 +187,7 @@ class Groups {
$user = User::getInstance(); $user = User::getInstance();
$all_data = groupview_status_modules_agents ($system->getConfig('id_user'), false, 'AR', false); $all_data = groupview_status_modules_agents ($system->getConfig('id_user'), false, 'AR', false);
$result_groups = groupview_get_groups_list($system->getConfig('id_user'), false, 'AR', true, true); $result_groups = groupview_get_groups_list($system->getConfig('id_user'),'AR');
foreach ($all_data as $group_all_data) { foreach ($all_data as $group_all_data) {
$result_groups[0]['_total_agents_'] += $group_all_data["_total_agents_"]; $result_groups[0]['_total_agents_'] += $group_all_data["_total_agents_"];

View File

@ -69,8 +69,6 @@ else {
// Header // Header
ui_print_page_header (__("Group view"), "images/group.png", false, "", false, $updated_time); ui_print_page_header (__("Group view"), "images/group.png", false, "", false, $updated_time);
$strict_user = false;
$total_agentes = 0; $total_agentes = 0;
$monitor_ok = 0; $monitor_ok = 0;
$monitor_warning = 0; $monitor_warning = 0;
@ -83,8 +81,11 @@ $agents_notinit = 0;
$all_alerts_fired = 0; $all_alerts_fired = 0;
//Groups and tags //Groups and tags
$result_groups = groupview_get_groups_list($config['id_user'], $strict_user, $result_groups = groupview_get_groups_list(
($agent_a == true) ? 'AR' : (($agent_w == true) ? 'AW' : 'AR'), true, true); $config['id_user'],
($agent_a == true) ? 'AR' : (($agent_w == true) ? 'AW' : 'AR')
);
$count = count($result_groups); $count = count($result_groups);
if ($result_groups[0]["_id_"] == 0) { if ($result_groups[0]["_id_"] == 0) {