2012-01-23 Vanessa Gil <vanessa.gil@artica.es>

* operation/agentes/estado_agente.php
	  include/functions_agents.php: Simplify code estado_agente
	and fix bug in search filter.


git-svn-id: https://svn.code.sf.net/p/pandora/code/trunk@5404 c3f86ba8-e40f-0410-aaad-9ba5e7f4b01f
This commit is contained in:
vgilc 2012-01-23 16:28:22 +00:00
parent 9e60e3f19e
commit 7d6ab5a944
3 changed files with 137 additions and 119 deletions

View File

@ -1,3 +1,9 @@
2012-01-23 Vanessa Gil <vanessa.gil@artica.es>
* operation/agentes/estado_agente.php
include/functions_agents.php: Simplify code estado_agente
and fix bug in search filter.
2012-01-23 Ramon Novoa <rnovoa@artica.es> 2012-01-23 Ramon Novoa <rnovoa@artica.es>
* pandoradb_data.sql, pandoradb.sql, * pandoradb_data.sql, pandoradb.sql,

View File

@ -316,6 +316,10 @@ function agents_get_agents ($filter = false, $fields = false, $access = 'AR', $o
$filter = array (); $filter = array ();
} }
if(isset($filter['search'])) {
$search = $filter['search'];
unset($filter['search']);
}
if(isset($filter['offset'])) { if(isset($filter['offset'])) {
$offset = $filter['offset']; $offset = $filter['offset'];
@ -327,6 +331,80 @@ function agents_get_agents ($filter = false, $fields = false, $access = 'AR', $o
unset($filter['limit']); unset($filter['limit']);
} }
$status_sql = ' 1 = 1';
if(isset($filter['status'])) {
$normal_modules = 'SELECT tagente.id_agente FROM tagente_estado, tagente, tagente_modulo
WHERE tagente.disabled = 0 AND tagente_estado.id_agente = tagente.id_agente
AND tagente_estado.id_agente_modulo = tagente_modulo.id_agente_modulo
AND tagente_modulo.disabled = 0 AND estado = 0
AND (utimestamp != 0 OR tagente_modulo.id_tipo_modulo IN (21,22,23))
AND (utimestamp >= ( UNIX_TIMESTAMP() - (current_interval * 2))
OR tagente_modulo.id_tipo_modulo IN (21,22,23,100))';
$warning_modules = 'SELECT tagente.id_agente FROM tagente_estado, tagente, tagente_modulo
WHERE tagente.disabled = 0 AND tagente_estado.id_agente = tagente.id_agente
AND tagente_estado.id_agente_modulo = tagente_modulo.id_agente_modulo
AND tagente_modulo.disabled = 0 AND estado = 2
AND (utimestamp >= ( UNIX_TIMESTAMP() - (current_interval * 2))
OR tagente_modulo.id_tipo_modulo IN (21,22,23,100))';
$critical_modules = 'SELECT tagente.id_agente FROM tagente_estado, tagente, tagente_modulo
WHERE tagente.disabled = 0 AND tagente_estado.id_agente = tagente.id_agente
AND tagente_estado.id_agente_modulo = tagente_modulo.id_agente_modulo
AND tagente_modulo.disabled = 0 AND estado = 1
AND (utimestamp >= ( UNIX_TIMESTAMP() - (current_interval * 2))
OR tagente_modulo.id_tipo_modulo IN (21,22,23,100))';
$unknown_modules = 'SELECT tagente.id_agente FROM tagente_estado, tagente, tagente_modulo
WHERE tagente.disabled = 0 AND tagente.id_agente = tagente_estado.id_agente
AND tagente_estado.id_agente_modulo = tagente_modulo.id_agente_modulo
AND tagente_modulo.disabled = 0 AND tagente_modulo.id_tipo_modulo NOT IN (21,22,23,100)
AND utimestamp < ( UNIX_TIMESTAMP() - (current_interval * 2)) AND utimestamp != 0';
$notinit_modules = 'SELECT tagente_estado.id_agente FROM tagente_estado, tagente, tagente_modulo
WHERE tagente.disabled = 0 AND tagente.id_agente = tagente_estado.id_agente
AND tagente_estado.id_agente_modulo = tagente_modulo.id_agente_modulo
AND tagente_modulo.disabled = 0
AND tagente_modulo.id_tipo_modulo NOT IN (21,22,23)
AND utimestamp = 0';
switch ($filter['status']) {
// Normal
case 0:
$status_sql = "id_agente IN ($normal_modules) && id_agente NOT IN ($warning_modules) &&
id_agente NOT IN ($critical_modules) && id_agente NOT IN ($unknown_modules)"; //&& id_agente NOT IN ($notinit_modules)";
break;
// Warning
case 1:
$status_sql = "id_agente IN ($warning_modules) &&
id_agente NOT IN ($critical_modules)"; //&& id_agente NOT IN ($notinit_modules)";
break;
// Critical
case 2:
$status_sql = "id_agente IN ($critical_modules)";
break;
// Unknown
case 3:
$status_sql = "id_agente IN ($unknown_modules) &&
id_agente NOT IN ($critical_modules) && id_agente NOT IN ($warning_modules)";
break;
// Not normal
case 4:
//$status_sql = "id_agente NOT IN ($normal_modules)";
$status_sql = "id_agente NOT IN ($normal_modules) || id_agente IN ($warning_modules) ||
id_agente IN ($critical_modules) || id_agente IN ($unknown_modules)";
break;
// Not init
case 5:
$status_sql = "id_agente NOT IN ($warning_modules) &&
id_agente NOT IN ($critical_modules) && id_agente NOT IN ($unknown_modules)";
break;
}
unset($filter['status']);
}
unset($filter['order']); unset($filter['order']);
$filter_nogroup = $filter; $filter_nogroup = $filter;
@ -336,9 +414,11 @@ function agents_get_agents ($filter = false, $fields = false, $access = 'AR', $o
//If no group specified, get all user groups //If no group specified, get all user groups
if (empty ($filter['id_grupo'])) { if (empty ($filter['id_grupo'])) {
$all_groups = true;
$filter['id_grupo'] = $groups; $filter['id_grupo'] = $groups;
} }
elseif (! is_array ($filter['id_grupo'])) { elseif (! is_array ($filter['id_grupo'])) {
$all_groups = false;
//If group is specified but not allowed, return false //If group is specified but not allowed, return false
if (! in_array ($filter['id_grupo'], $groups)) { if (! in_array ($filter['id_grupo'], $groups)) {
return false; return false;
@ -346,6 +426,7 @@ function agents_get_agents ($filter = false, $fields = false, $access = 'AR', $o
$filter['id_grupo'] = (array) $filter['id_grupo']; //Make an array $filter['id_grupo'] = (array) $filter['id_grupo']; //Make an array
} }
else { else {
$all_groups = true;
//Check each group specified to the user groups, remove unwanted groups //Check each group specified to the user groups, remove unwanted groups
foreach ($filter['id_grupo'] as $key => $id_group) { foreach ($filter['id_grupo'] as $key => $id_group) {
if (! in_array ($id_group, $groups)) { if (! in_array ($id_group, $groups)) {
@ -379,26 +460,30 @@ function agents_get_agents ($filter = false, $fields = false, $access = 'AR', $o
$where_nogroup = db_format_array_where_clause_sql ($filter_nogroup, 'AND', ''); $where_nogroup = db_format_array_where_clause_sql ($filter_nogroup, 'AND', '');
$sql_extra = enterprise_hook('policies_get_agents_sql_condition'); if ($where_nogroup == '') {
$where_nogroup = '1 = 1';
}
$extra = false; $extra = false;
if($sql_extra != ENTERPRISE_NOT_HOOK) {
if (!empty($sql_extra)) { $sql_extra = '';
$extra = true; if ($all_groups){
$where_nogroup = '1 = 1';
$sql_extra = enterprise_hook('policies_get_agents_sql_condition');
if($sql_extra != ENTERPRISE_NOT_HOOK) {
if (!empty($sql_extra)) {
$extra = true;
}
} }
} }
if($extra) { if($extra) {
if (empty($where_nogroup)) $where = sprintf('(%s OR (%s)) AND (%s) AND (%s) %s', $sql_extra, $where, $where_nogroup, $status_sql, $search);
$where = sprintf('%s AND (%s)', $where, $sql_extra); } else {
else $where = sprintf('%s AND %s AND (%s) %s', $where, $where_nogroup, $status_sql, $search);
$where = sprintf('%s AND (%s OR %s)', $where, $where_nogroup, $sql_extra);
} }
else {
if (!empty($where_nogroup))
$where = sprintf('%s AND %s', $where, $where_nogroup);
}
$sql = sprintf('SELECT %s FROM tagente WHERE %s %s', implode(',',$fields), $where, $order); $sql = sprintf('SELECT %s FROM tagente WHERE %s %s', implode(',',$fields), $where, $order);
switch ($config["dbtype"]) { switch ($config["dbtype"]) {
@ -424,7 +509,6 @@ function agents_get_agents ($filter = false, $fields = false, $access = 'AR', $o
} }
return $agents; return $agents;
return db_get_all_rows_sql($sql);
} }
/** /**

View File

@ -281,128 +281,56 @@ switch ($sortField) {
$search_sql = ''; $search_sql = '';
if ($search != ""){ if ($search != ""){
$search_sql = " AND ( nombre COLLATE utf8_general_ci LIKE '%$search%' OR direccion LIKE '%$search%') "; $search_sql = " AND ( nombre COLLATE utf8_general_ci LIKE '%$search%' OR direccion LIKE '%$search%' OR comentarios LIKE '%$search%') ";
} }
// Show only selected groups // Show only selected groups
if ($group_id > 0) { if ($group_id > 0) {
$groups = $group_id; $groups = array($group_id);
$agent_names = agents_get_group_agents ($group_id, $filter, "upper", false, $recursion);
if ($recursion) { if ($recursion) {
$groups = groups_get_id_recursive($group_id, true); $groups = groups_get_id_recursive($group_id, true);
} }
} }
else { else {
$groups = array(); $groups = array();
$user_groups = users_get_groups($config["id_user"], "AR");
$user_group = users_get_groups($config["id_user"], "PM"); $groups = array_keys($user_groups);
$groups = array_keys($user_group);
$user_group = users_get_groups($config["id_user"], "AR");
$groups = array_unique(array_merge($groups, array_keys($user_group)));
$agent_names = agents_get_group_agents($groups, $filter, "upper");
} }
$total_agents = 0; $total_agents = 0;
$agents = false; $agents = false;
if (! empty ($agent_names)) { $total_agents = agents_get_agents(array (
$total_agents = agents_get_agents(array ('id_agente' => array_keys ($agent_names), 'order' => 'nombre ASC',
'order' => 'nombre ASC', 'disabled' => 0,
'disabled' => 0, 'id_grupo' => $groups,
'id_grupo' => $groups), 'search' => $search_sql,
array ('COUNT(*) as total')); 'status' => $status),
$total_agents = isset ($total_agents[0]['total']) ? $total_agents[0]['total'] : 0; array ('COUNT(*) as total'));
$total_agents = isset ($total_agents[0]['total']) ? $total_agents[0]['total'] : 0;
$agents = agents_get_agents(array ('id_agente' => array_keys ($agent_names), $agents = agents_get_agents(array (
'order' => 'nombre ASC', 'order' => 'nombre ASC',
'id_grupo' => $groups, 'id_grupo' => $groups,
/* 'offset' => (int) get_parameter ('offset'), 'disabled' => 0,
'limit' => (int) $config['block_size'] */ ), 'status' => $status,
array ('id_agente', 'search' => $search_sql,
'id_grupo', 'offset' => (int) get_parameter ('offset'),
'id_os', 'limit' => (int) $config['block_size'] ),
'ultimo_contacto',
'intervalo', array ('id_agente',
'comentarios description'), 'id_grupo',
'AR', 'id_os',
$order); 'ultimo_contacto',
} 'intervalo',
'comentarios description'),
'AR',
$order);
if (empty ($agents)) { if (empty ($agents)) {
$agents = array (); $agents = array ();
} }
$result_agents = array();
// This will be use above to simulate pagination
$i = 0;
$limit_agents = $offset + $config['block_size'];
// Filter by agents status
if ($status != -1){
$result_agents_tmp = array();
foreach ($agents as $agent_filter){
$filter_by_status = false;
$agent_stat = reporting_get_agent_module_info ($agent_filter["id_agente"]);
switch ($status){
// Normal
case 0:
if ($agent_stat['status'] != STATUS_AGENT_OK)
$filter_by_status = true;
break;
// Warning
case 1:
if ($agent_stat['status'] != STATUS_AGENT_WARNING)
$filter_by_status = true;
break;
// Critical
case 2:
if ($agent_stat['status'] != STATUS_AGENT_CRITICAL)
$filter_by_status = true;
break;
// Unknown
case 3:
if ($agent_stat['status'] != STATUS_AGENT_DOWN)
$filter_by_status = true;
break;
// Not normal
case 4:
if ($agent_stat['status'] == STATUS_AGENT_OK)
$filter_by_status = true;
break;
// Not init
case 5:
if ($agent_stat['status'] != STATUS_AGENT_NO_DATA)
$filter_by_status = true;
break;
}
// If status is different from filter, don't show agent
if (! $filter_by_status)
$result_agents_tmp[] = $agent_filter;
}
// Recalculate total agents
if (! empty ($result_agents_tmp))
$total_agents = count($result_agents_tmp);
else
$total_agents = 0;
// Simulate pagination
foreach ($result_agents_tmp as $agent_filter_off){
if ($i >= $offset and $i < $limit_agents)
$result_agents[] = $agent_filter_off;
$i++;
}
}
else {
// Simulate pagination
foreach ($agents as $agent_filter){
if ($i >= $offset and $i < $limit_agents)
$result_agents[] = $agent_filter;
$i++;
}
}
// Prepare pagination // Prepare pagination
ui_pagination ($total_agents, ui_get_url_refresh (array ('group_id' => $group_id, 'recursion' => $recursion, 'search' => $search, 'sort_field' => $sortField, 'sort' => $sort, 'status' => $status))); ui_pagination ($total_agents, ui_get_url_refresh (array ('group_id' => $group_id, 'recursion' => $recursion, 'search' => $search, 'sort_field' => $sortField, 'sort' => $sort, 'status' => $status)));
@ -457,7 +385,7 @@ $table->data = array ();
$rowPair = true; $rowPair = true;
$iterator = 0; $iterator = 0;
foreach ($result_agents as $agent) { foreach ($agents as $agent) {
if ($rowPair) if ($rowPair)
$table->rowclass[$iterator] = 'rowPair'; $table->rowclass[$iterator] = 'rowPair';
else else