diff --git a/pandora_console/include/functions_events.php b/pandora_console/include/functions_events.php index f082f8611a..41a2d76bc4 100644 --- a/pandora_console/include/functions_events.php +++ b/pandora_console/include/functions_events.php @@ -85,6 +85,145 @@ function events_sql_db_filter($filter) } +/** + * Retrieve all events filtered. + * + * @param array $fields Fields to retrieve. + * @param array $filter Filters to be applied. + * @param integer $limit Limit (pagination). + * @param integer $offset Offset (pagination). + * + * @return array Events. + * @throws Exception On error. + */ +function events_get_all( + $fields, + array $filter, + $offset=null, + $limit=null, + $order=null, + $sort_field=null +) { + global $config; + + if (!is_array($filter)) { + throw new Exception('[events_get_all] Filter must be an array.'); + } + + $count = false; + if (!is_array($fields) && $fields == 'count') { + $fields = ['te.*']; + $count = true; + } else if (!is_array($fields)) { + throw new Exception('[events_get_all] Fields must be an array or "count".'); + } + + $hour_filter = ''; + if (isset($filter['event_view_hr'])) { + $hour_filter = sprintf( + ' AND utimestamp > UNIX_TIMESTAMP(now() - INTERVAL %d HOUR) ', + $filter['event_view_hr'] + ); + } + + $agent_id_filter = ''; + if (isset($filter['id_agent']) && $filter['id_agent'] > 0) { + $agent_id_filter = sprintf( + ' AND id_agente = %d ', + $filter['id_agent'] + ); + } + + $table = events_get_events_table($meta, $history); + + $tevento = sprintf( + '(SELECT * + FROM %s + WHERE 1=1 %s %s) te', + $table, + $hour_filter, + $agent_id_filter + ); + + $agent_name_filter = ''; + if (!empty($filter['agent_alias'])) { + $agent_name_filter = sprintf( + ' AND ta.alias = "%s" ', + $filter['agent_alias'] + ); + } + + $order_by = ''; + if (isset($order, $sort_field)) { + $order_by = events_get_sql_order($sort_field, $order); + } + + $pagination = ''; + if (isset($limit, $offset)) { + $pagination = sprintf(' LIMIT %d OFFSET %d', $limit, $offset); + } + + $extra = ''; + if (is_metaconsole()) { + $extra = ', server_id'; + } + + $group_by = 'GROUP BY '; + $tagente_join = 'LEFT'; + switch ($filter['group_rep']) { + case '0': + default: + // All events. + $group_by = ''; + break; + + case '1': + // Group by events. + $group_by .= 'estado, evento, id_agente, id_agentmodule'; + $group_by .= $extra; + break; + + case '2': + // Group by agents. + $tagente_join = 'INNER'; + $group_by .= 'te.id_agente, te.event_type'; + $group_by .= $extra; + break; + } + + // Secondary groups. + db_process_sql('SET group_concat_max_len = 9999999'); + $event_lj = events_get_secondary_groups_left_join($table); + + $sql = sprintf( + 'SELECT %s + FROM %s + %s JOIN tagente ta + ON ta.id_agente = te.id_agente + %s + %s + WHERE 1=1 + %s + %s + %s + ', + join(',', $fields), + $tevento, + $tagente_join, + $event_lj, + $filter_extra_agents, + $group_by, + $order_by, + $pagination + ); + if ($count) { + $sql = 'SELECT count(*) as nitems FROM ('.$sql.') tt'; + } + + return db_get_all_rows_sql($sql); +} + + /** * Get all rows of events from the database, that * pass the filter, and can get only some fields. @@ -244,49 +383,30 @@ function events_get_events_grouped( db_process_sql('SET group_concat_max_len = 9999999'); $event_lj = events_get_secondary_groups_left_join($table); if ($total) { - $sql = sprintf( - 'SELECT COUNT(*) FROM (SELECT id_evento - FROM %s te %s - WHERE 1=1 %s - GROUP BY estado, evento, id_agente, id_agentmodule %s) AS t ', - $table, - $event_lj, - $sql_post, - $groupby_extra - ); + $sql = "SELECT COUNT(*) FROM (SELECT id_evento + FROM $table te $event_lj + WHERE 1=1 ".$sql_post.' + GROUP BY estado, evento, id_agente, id_agentmodule'.$groupby_extra.') AS t'; } else { - $sql = sprintf( - 'SELECT *, MAX(id_evento) AS id_evento, - GROUP_CONCAT(DISTINCT user_comment SEPARATOR "
") AS user_comment, - GROUP_CONCAT(DISTINCT id_evento SEPARATOR ",") AS similar_ids, + $sql = "SELECT *, MAX(id_evento) AS id_evento, + GROUP_CONCAT(DISTINCT user_comment SEPARATOR '
') AS user_comment, + GROUP_CONCAT(DISTINCT id_evento SEPARATOR ',') AS similar_ids, COUNT(id_evento) AS event_rep, MAX(utimestamp) AS timestamp_rep, MIN(utimestamp) AS timestamp_rep_min, - (SELECT owner_user FROM %s WHERE id_evento = MAX(te.id_evento)) owner_user, - (SELECT id_usuario FROM %s WHERE id_evento = MAX(te.id_evento)) id_usuario, - (SELECT id_agente FROM %s WHERE id_evento = MAX(te.id_evento)) id_agente, - (SELECT criticity FROM %s WHERE id_evento = MAX(te.id_evento)) AS criticity, - (SELECT ack_utimestamp FROM %s WHERE id_evento = MAX(te.id_evento)) AS ack_utimestamp, - (SELECT nombre FROM tagente_modulo WHERE id_agente_modulo = te.id_agentmodule) AS module_name, - (SELECT alias FROM tagente WHERE id_agente = te.id_agente) agent_name, - te.id_agente - FROM %s te %s - WHERE 1=1 %s - GROUP BY estado, evento, id_agente, id_agentmodule %s ', - $table, - $table, - $table, - $table, - $table, - $table, - $event_lj, - $sql_post, - $groupby_extra - ); + (SELECT owner_user FROM $table WHERE id_evento = MAX(te.id_evento)) owner_user, + (SELECT id_usuario FROM $table WHERE id_evento = MAX(te.id_evento)) id_usuario, + (SELECT id_agente FROM $table WHERE id_evento = MAX(te.id_evento)) id_agente, + (SELECT criticity FROM $table WHERE id_evento = MAX(te.id_evento)) AS criticity, + (SELECT ack_utimestamp FROM $table WHERE id_evento = MAX(te.id_evento)) AS ack_utimestamp, + (SELECT nombre FROM tagente_modulo WHERE id_agente_modulo = te.id_agentmodule) AS module_name + FROM $table te $event_lj + WHERE 1=1 ".$sql_post.' + GROUP BY estado, evento, id_agente, id_agentmodule'.$groupby_extra; $sql .= ' '.events_get_sql_order($sort_field, $order, 2); $sql .= ' LIMIT '.$offset.','.$pagination; } - // Extract the events by filter (or not) from db. + // Extract the events by filter (or not) from db $events = db_get_all_rows_sql($sql, $history_db); if ($total) { diff --git a/pandora_console/include/styles/events.css b/pandora_console/include/styles/events.css index 648f8a706e..17759213ce 100644 --- a/pandora_console/include/styles/events.css +++ b/pandora_console/include/styles/events.css @@ -52,7 +52,7 @@ div.filter_input_little { form.flex-row div.filter_input.large { flex: 1; - min-width: 650px; + min-width: 470px; } div.filter_input > label, diff --git a/pandora_console/include/styles/tables.css b/pandora_console/include/styles/tables.css index 4cd6052873..5450465638 100644 --- a/pandora_console/include/styles/tables.css +++ b/pandora_console/include/styles/tables.css @@ -245,3 +245,8 @@ tr.disabled_row_user * { color: grey; } + +/* Disable datatables border */ +table.dataTable.info_table.no-footer { + border-bottom: none; +} diff --git a/pandora_console/operation/events/events.php b/pandora_console/operation/events/events.php index 606eab7804..5751a5711e 100644 --- a/pandora_console/operation/events/events.php +++ b/pandora_console/operation/events/events.php @@ -79,8 +79,8 @@ $pagination = get_parameter('filter[pagination]'); $event_view_hr = get_parameter('filter[event_view_hr]', 8); $id_user_ack = get_parameter('filter[id_user_ack]'); $group_rep = get_parameter('filter[group_rep]'); -$tag_with = get_parameter('filter[tag_with]', io_json_mb_encode([])); -$tag_without = get_parameter('filter[tag_without]', io_json_mb_encode([])); +$tag_with = get_parameter('filter[tag_with]', []); +$tag_without = get_parameter('filter[tag_without]', []); $filter_only_alert = get_parameter('filter[filter_only_alert]'); $id_group_filter = get_parameter('filter[id_group_filter]'); $date_from = get_parameter('filter[date_from]'); @@ -91,6 +91,9 @@ $user_comment = get_parameter('filter[user_comment]'); // TAGS. +// Get the tags where the user have permissions in Events reading tasks. +$tags = tags_get_user_tags($config['id_user'], $access); + $tags_select_with = []; $tags_select_without = []; $tag_with_temp = []; @@ -125,7 +128,7 @@ $tabletags_with->cellspacing = 4; $tabletags_with->cellpadding = 4; $tabletags_with->class = 'noshadow'; $tabletags_with->styleTable = 'border: 0px;'; -if (defined('METACONSOLE')) { +if (is_metaconsole()) { $tabletags_with->class = 'nobady'; $tabletags_with->cellspacing = 0; $tabletags_with->cellpadding = 0; @@ -200,7 +203,7 @@ $tabletags_without->width = '100%'; $tabletags_without->cellspacing = 4; $tabletags_without->cellpadding = 4; $tabletags_without->class = 'noshadow'; -if (defined('METACONSOLE')) { +if (is_metaconsole()) { $tabletags_without->class = 'nobady'; $tabletags_without->cellspacing = 0; $tabletags_without->cellpadding = 0; @@ -263,12 +266,6 @@ $data[2] = html_print_select( $tabletags_without->data[] = $data; $tabletags_without->rowclass[] = ''; - -// END OF TAGS. -// Datatables offset, limit. -$start = get_parameter('start', 0); -$length = get_parameter('length', $config['block_size']); - if (io_safe_output($tag_with) == '["0"]') { $tag_with = '[]'; } @@ -277,6 +274,11 @@ if (io_safe_output($tag_without) == '["0"]') { $tag_without = '[]'; } +/* + * END OF TAGS. + */ + + // Ajax responses. if (is_ajax()) { $get_filter_values = get_parameter('get_filter_values', 0); @@ -285,31 +287,36 @@ if (is_ajax()) { $get_event_filters = get_parameter('get_event_filters', 0); $get_events = get_parameter('get_events', 0); $filter = get_parameter('filter', []); + // Datatables offset, limit. + $start = get_parameter('start', 0); + $length = get_parameter('length', $config['block_size']); if ($get_events) { $order = get_datatable_order(true); - $sql_post = ' AND te.utimestamp > UNIX_TIMESTAMP(now() - INTERVAL '.$event_view_hr.' hour)'; - $events = events_get_events_grouped( - // Sql_post. - $sql_post, + $events = events_get_all( + [ + 'te.*', + 'ta.alias as agent_name', + ], + $filter, // Offset. $start, - // Pagination. + // Limit. $length, - // Meta. - false, - // History. - false, - // Total. - false, - // History db. - false, // Order. $order['direction'], // Sort field. $order['field'] ); + $count = events_get_all( + 'count', + $filter + ); + + if ($count !== false) { + $count = $count['0']['nitems']; + } if ($events) { $data = array_reduce( @@ -321,21 +328,6 @@ if (is_ajax()) { ); } - $count = events_get_events_grouped( - // Sql_post. - $sql_post, - // Offset. - $start, - // Pagination. - $length, - // Meta. - false, - // History. - false, - // Total. - true - ); - // RecordsTotal && recordsfiltered resultados totales. echo json_encode( [ @@ -652,9 +644,9 @@ $inputs[] = $in; // Duplicates group { events | agents }. $data = html_print_select( [ - __('All events'), - __('Group events'), - __('Group agents'), + 0 => __('All events'), + 1 => __('Group events'), + 2 => __('Group agents'), ], 'group_rep', $group_rep, @@ -1115,8 +1107,6 @@ try { item.event_type = \'
\' + text + "
"; - console.log(item); - if (item.id_agente > 0) { item.agent_name = \'\' + item.agent_name + \'\'; } @@ -1140,8 +1130,736 @@ ui_require_jquery_file( 'ui.datepicker-'.get_user_language(), 'include/javascript/i18n/' ); + +// End. Load required JS. ?>