Merge branch 'ent-9425-agrupar-eventos-por-id_extra' into 'develop'

new group by events for extraid pandora_enterprise#9425

See merge request artica/pandorafms!5147
This commit is contained in:
Diego Muñoz-Reja 2022-10-24 15:19:21 +00:00
commit aff8fdb005
10 changed files with 144 additions and 67 deletions

View File

@ -442,8 +442,12 @@ $table->data[10][1] = html_print_select(
true
);
$repeated_sel[0] = __('All events');
$repeated_sel[1] = __('Group events');
$repeated_sel = [
EVENT_GROUP_REP_ALL => __('All events'),
EVENT_GROUP_REP_EVENTS => __('Group events'),
EVENT_GROUP_REP_AGENTS => __('Group agents'),
EVENT_GROUP_REP_EXTRAIDS => __('Group extra id'),
];
$table->data[11][0] = '<b>'.__('Repeated').'</b>';
$table->data[11][1] = html_print_select(
$repeated_sel,

View File

@ -90,7 +90,7 @@ $node_id = (int) get_parameter('node_id', 0);
if ($get_comments === true) {
$event = get_parameter('event', false);
$event_rep = get_parameter('event_rep', false);
$event_rep = (int) get_parameter('event_rep', 0);
if ($event === false) {
return __('Failed to retrieve comments');
}
@ -98,7 +98,7 @@ if ($get_comments === true) {
$eventsGrouped = [];
// Consider if the event is grouped.
$whereGrouped = '1=1';
if (isset($event_rep) === true && $event_rep > 0) {
if ($event_rep === EVENT_GROUP_REP_EVENTS) {
// Default grouped message filtering (evento and estado).
$whereGrouped = sprintf(
'`evento` = "%s"',
@ -119,6 +119,11 @@ if ($get_comments === true) {
(int) $event['id_agentmodule']
);
}
} else if ($event_rep === EVENT_GROUP_REP_EXTRAIDS) {
$whereGrouped = sprintf(
'`id_extra` = "%s"',
$event['id_extra']
);
} else {
$whereGrouped = sprintf('`id_evento` = %d', $event['id_evento']);
}
@ -175,7 +180,7 @@ if ($delete_event === true) {
$filter = get_parameter('filter', []);
$id_evento = (int) get_parameter('id_evento', 0);
$server_id = (int) get_parameter('server_id', 0);
$event_rep = get_parameter('event_rep', 0);
$event_rep = (int) get_parameter('event_rep', 0);
try {
if (is_metaconsole() === true
@ -228,7 +233,7 @@ if ($validate_event === true) {
$filter = get_parameter('filter', []);
$id_evento = (int) get_parameter('id_evento', 0);
$server_id = (int) get_parameter('server_id', 0);
$event_rep = get_parameter('event_rep', 0);
$event_rep = (int) get_parameter('event_rep', 0);
try {
if (is_metaconsole() === true
@ -240,7 +245,7 @@ if ($validate_event === true) {
if ($event_rep === 0) {
// Disable group by when there're result is unique.
$filter['group_rep'] = 0;
$filter['group_rep'] = EVENT_GROUP_REP_ALL;
}
// Check acl.
@ -285,7 +290,7 @@ if ($in_process_event === true) {
$filter = get_parameter('filter', []);
$id_evento = (int) get_parameter('id_evento', 0);
$server_id = (int) get_parameter('server_id', 0);
$event_rep = get_parameter('event_rep', 0);
$event_rep = (int) get_parameter('event_rep', 0);
try {
if (is_metaconsole() === true
@ -297,7 +302,7 @@ if ($in_process_event === true) {
if ($event_rep === 0) {
// Disable group by when there're result is unique.
$filter['group_rep'] = 0;
$filter['group_rep'] = EVENT_GROUP_REP_ALL;
}
// Check acl.
@ -466,7 +471,6 @@ if ($get_filter_values) {
$event_filter = [
'status' => EVENT_NO_VALIDATED,
'event_view_hr' => $config['event_view_hr'],
'group_rep' => 1,
'tag_with' => [],
'tag_without' => [],
'history' => false,
@ -483,7 +487,7 @@ if ($get_filter_values) {
'time_to' => '',
'severity' => '',
'event_type' => '',
'group_rep' => 0,
'group_rep' => EVENT_GROUP_REP_ALL,
'id_group' => 0,
'id_group_filter' => 0,
'group_name' => 'All',
@ -2418,7 +2422,7 @@ if ($get_events_fired) {
'id_agent_module' => 0,
'pagination' => 0,
'id_user_ack' => 0,
'group_rep' => 0,
'group_rep' => EVENT_GROUP_REP_ALL,
'tag_with' => [],
'tag_without' => [],
'filter_only_alert' => -1,

View File

@ -45,6 +45,11 @@ define('EVENT_VALIDATE', 1);
define('EVENT_PROCESS', 2);
define('EVENT_NO_VALIDATED', 3);
// Events group by constants.
define('EVENT_GROUP_REP_ALL', 0);
define('EVENT_GROUP_REP_EVENTS', 1);
define('EVENT_GROUP_REP_AGENTS', 2);
define('EVENT_GROUP_REP_EXTRAIDS', 3);
// Agents disabled status.
define('AGENT_ENABLED', 0);

View File

@ -11246,7 +11246,7 @@ function get_events_with_user($trash1, $trash2, $other, $returnType, $user_in_db
$id_user_ack = 0;
$event_view_hr = 0;
$tag = '';
$group_rep = 0;
$group_rep = EVENT_GROUP_REP_ALL;
$utimestamp_upper = 0;
$utimestamp_bottom = 0;
$id_alert_template = -1;
@ -11449,7 +11449,7 @@ function get_events_with_user($trash1, $trash2, $other, $returnType, $user_in_db
$alert_join = ' INNER JOIN talert_template_modules ON '.$table_events.'.id_alert_am=talert_template_modules.id';
}
if ($group_rep == 0) {
if ($group_rep == EVENT_GROUP_REP_ALL) {
if ($filter['total']) {
$sql = 'SELECT COUNT(*)
FROM '.$table_events.'
@ -15922,7 +15922,7 @@ function api_set_create_event_filter($name, $thrash1, $other, $thrash3)
$id_user_ack = (in_array($other['data'][9], $users)) ? $other['data'][9] : 0;
$group_rep = ($other['data'][10] == 0 || $other['data'][10] == 1) ? $other['data'][10] : 0;
$group_rep = ($other['data'][10] == EVENT_GROUP_REP_ALL || $other['data'][10] == EVENT_GROUP_REP_EVENTS) ? $other['data'][10] : EVENT_GROUP_REP_ALL;
$date_from = (preg_match('/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/', $other['data'][11])) ? $other['data'][11] : '0000-00-00';
@ -16149,7 +16149,7 @@ function api_set_update_event_filter($id_event_filter, $thrash1, $other, $thrash
break;
case 11:
$values['group_rep'] = ($other['data'][11] == 0 || $other['data'][11] == 1) ? $other['data'][11] : 0;
$values['group_rep'] = ($other['data'][11] == EVENT_GROUP_REP_ALL || $other['data'][11] == EVENT_GROUP_REP_EVENTS) ? $other['data'][11] : EVENT_GROUP_REP_ALL;
break;
case 12:

View File

@ -382,14 +382,14 @@ function events_delete($id_evento, $filter=null, $history=false, $force_node=fal
}
if (isset($filter) === false
|| is_array($filter) === true
|| is_array($filter) === false
) {
$filter = ['group_rep' => 0];
$filter = ['group_rep' => EVENT_GROUP_REP_ALL];
}
switch ($filter['group_rep']) {
case '0':
case '2':
case EVENT_GROUP_REP_ALL:
case EVENT_GROUP_REP_AGENTS:
default:
// No groups option direct update.
$delete_sql = sprintf(
@ -399,7 +399,8 @@ function events_delete($id_evento, $filter=null, $history=false, $force_node=fal
);
break;
case '1':
case EVENT_GROUP_REP_EVENTS:
case EVENT_GROUP_REP_EXTRAIDS:
// Group by events.
$sql = events_get_all(
['te.*'],
@ -418,8 +419,16 @@ function events_delete($id_evento, $filter=null, $history=false, $force_node=fal
true
);
$target_ids = db_get_all_rows_sql(
sprintf(
if ((int) $filter['group_rep'] === EVENT_GROUP_REP_EXTRAIDS) {
$sql = sprintf(
'SELECT tu.id_evento FROM tevento tu INNER JOIN ( %s ) tf
ON tu.id_extra = tf.id_extra
AND tf.max_id_evento = %d',
$sql,
$id_evento
);
} else {
$sql = sprintf(
'SELECT tu.id_evento FROM tevento tu INNER JOIN ( %s ) tf
ON tu.estado = tf.estado
AND tu.evento = tf.evento
@ -428,8 +437,10 @@ function events_delete($id_evento, $filter=null, $history=false, $force_node=fal
AND tf.max_id_evento = %d',
$sql,
$id_evento
)
);
);
}
$target_ids = db_get_all_rows_sql($sql);
// Try to avoid deadlock while updating full set.
if ($target_ids !== false && count($target_ids) > 0) {
@ -475,12 +486,12 @@ function events_update_status($id_evento, $status, $filter=null)
}
if (isset($filter) === false || is_array($filter) === false) {
$filter = ['group_rep' => 0];
$filter = ['group_rep' => EVENT_GROUP_REP_ALL];
}
switch ($filter['group_rep']) {
case '0':
case '2':
case EVENT_GROUP_REP_ALL:
case EVENT_GROUP_REP_AGENTS:
default:
// No groups option direct update.
$update_sql = sprintf(
@ -492,7 +503,8 @@ function events_update_status($id_evento, $status, $filter=null)
);
break;
case '1':
case EVENT_GROUP_REP_EVENTS:
case EVENT_GROUP_REP_EXTRAIDS:
// Group by events.
$sql = events_get_all(
['te.*'],
@ -511,8 +523,16 @@ function events_update_status($id_evento, $status, $filter=null)
true
);
$target_ids = db_get_all_rows_sql(
sprintf(
if ((int) $filter['group_rep'] === EVENT_GROUP_REP_EXTRAIDS) {
$sql = sprintf(
'SELECT tu.id_evento FROM tevento tu INNER JOIN ( %s ) tf
ON tu.id_extra = tf.id_extra
AND tf.max_id_evento = %d',
$sql,
$id_evento
);
} else {
$sql = sprintf(
'SELECT tu.id_evento FROM tevento tu INNER JOIN ( %s ) tf
ON tu.estado = tf.estado
AND tu.evento = tf.evento
@ -521,8 +541,10 @@ function events_update_status($id_evento, $status, $filter=null)
AND tf.max_id_evento = %d',
$sql,
$id_evento
)
);
);
}
$target_ids = db_get_all_rows_sql($sql);
// Try to avoid deadlock while updating full set.
if ($target_ids !== false && count($target_ids) > 0) {
@ -1367,7 +1389,10 @@ function events_get_all(
// Order.
$order_by = '';
if (isset($order, $sort_field) === true) {
if (isset($filter['group_rep']) === true && $filter['group_rep'] == 1) {
if (isset($filter['group_rep']) === true
&& $filter['group_rep'] === EVENT_GROUP_REP_EVENTS
&& $filter['group_rep'] === EVENT_GROUP_REP_EXTRAIDS
) {
$order_by = events_get_sql_order('MAX('.$sort_field.')', $order);
} else {
$order_by = events_get_sql_order($sort_field, $order);
@ -1401,22 +1426,22 @@ function events_get_all(
$group_by = 'GROUP BY ';
$tagente_join = 'LEFT';
if (isset($filter['group_rep']) === false) {
$filter['group_rep'] = 0;
$filter['group_rep'] = EVENT_GROUP_REP_ALL;
}
switch ($filter['group_rep']) {
case '0':
case EVENT_GROUP_REP_ALL:
default:
// All events.
$group_by = '';
break;
case '1':
case EVENT_GROUP_REP_EVENTS:
// Group by events.
$group_by .= 'te.evento, te.id_agente, te.id_agentmodule';
break;
case '2':
case EVENT_GROUP_REP_AGENTS:
// Group by agents.
$tagente_join = 'INNER';
$group_by = '';
@ -1430,6 +1455,11 @@ function events_get_all(
);
}
break;
case EVENT_GROUP_REP_EXTRAIDS:
// Group by events.
$group_by .= 'te.id_extra';
break;
}
$tgrupo_join = 'LEFT';
@ -1516,7 +1546,9 @@ function events_get_all(
}
}
if ((int) $filter['group_rep'] === 1 && $count === false) {
if (((int) $filter['group_rep'] === EVENT_GROUP_REP_EVENTS
|| (int) $filter['group_rep'] === EVENT_GROUP_REP_EXTRAIDS) && $count === false
) {
$sql = sprintf(
'SELECT %s
%s
@ -1547,7 +1579,9 @@ function events_get_all(
%s
%s
%s JOIN tgrupo tg
ON %s',
ON %s
%s
%s',
join(',', $fields),
$group_selects_trans,
$tevento,
@ -1576,7 +1610,8 @@ function events_get_all(
join(' ', $agent_join_filters),
$tgrupo_join,
join(' ', $tgrupo_join_filters),
join(' ', $sql_filters)
join(' ', $sql_filters),
$order_by
);
} else {
$sql = sprintf(
@ -2900,9 +2935,9 @@ function events_get_agent(
// Group by agent.
if ((bool) $show_summary_group === true) {
$filters['group_rep'] = 1;
$filters['group_rep'] = EVENT_GROUP_REP_EVENTS;
} else {
$filters['group_rep'] = 2;
$filters['group_rep'] = EVENT_GROUP_REP_AGENTS;
}
$events = Event::search(
@ -5153,7 +5188,7 @@ function events_get_count_events_validated_by_user($data)
*
* @return string SQL.
*/
function events_get_sql_order($sort_field='timestamp', $sort='DESC', $group_rep=0, $only_fields=false)
function events_get_sql_order($sort_field='timestamp', $sort='DESC', $group_rep=EVENT_GROUP_REP_ALL, $only_fields=false)
{
$sort_field_translated = $sort_field;
switch ($sort_field) {
@ -5174,7 +5209,7 @@ function events_get_sql_order($sort_field='timestamp', $sort='DESC', $group_rep=
break;
case 'timestamp':
$sort_field_translated = ($group_rep == 0) ? 'timestamp' : 'timestamp_last';
$sort_field_translated = ($group_rep == EVENT_GROUP_REP_ALL) ? 'timestamp' : 'timestamp_last';
break;
case 'user_id':
@ -5201,6 +5236,14 @@ function events_get_sql_order($sort_field='timestamp', $sort='DESC', $group_rep=
$sort_field_translated = 'id_extra';
break;
case 'agent_name':
$sort_field_translated = 'ta.nombre';
break;
case 'module_custom_id':
$sort_field_translated = 'am.custom_id';
break;
default:
$sort_field_translated = $sort_field;
break;

View File

@ -4011,7 +4011,7 @@ function reporting_groups_nodes($content)
}
// Grouped.
$filters['group_rep'] = 1;
$filters['group_rep'] = EVENT_GROUP_REP_EVENTS;
$events = Event::search(
[

View File

@ -6513,7 +6513,7 @@ function ui_print_breadcrums($tab_name)
/**
* Show last comment
*
* @param array $comments array with comments
* @param string $comments String with comments.
*
* @return string HTML string with the last comment of the events.
*/
@ -6537,31 +6537,45 @@ function ui_print_comments($comments)
foreach ($comments_array as $comm) {
// Show the comments more recent first.
if (is_array($comm)) {
$last_comment[] = array_reverse($comm);
$order_utimestamp = array_reduce(
$comm,
function ($carry, $item) {
$carry[$item['utimestamp']] = $item;
return $carry;
}
);
$key_max_utimestamp = max(array_keys($order_utimestamp));
$last_comment = $order_utimestamp[$key_max_utimestamp];
}
}
if (empty($last_comment) === true) {
return '';
}
// Only show the last comment. If commment its too long,the comment will short with ...
// If $config['prominent_time'] is timestamp the date show Month, day, hour and minutes.
// Else show comments hours ago
if ($last_comment[0][0]['action'] != 'Added comment') {
$last_comment[0][0]['comment'] = $last_comment[0][0]['action'];
if ($last_comment['action'] != 'Added comment') {
$last_comment['comment'] = $last_comment['action'];
}
$short_comment = substr($last_comment[0][0]['comment'], 0, 20);
$short_comment = substr($last_comment['comment'], 0, 20);
if ($config['prominent_time'] == 'timestamp') {
$comentario = '<i>'.date($config['date_format'], $last_comment[0][0]['utimestamp']).'&nbsp;('.$last_comment[0][0]['id_user'].'):&nbsp;'.$last_comment[0][0]['comment'].'';
$comentario = '<i>'.date($config['date_format'], $last_comment['utimestamp']).'&nbsp;('.$last_comment['id_user'].'):&nbsp;'.$last_comment['comment'].'';
if (strlen($comentario) > '200px') {
$comentario = '<i>'.date($config['date_format'], $last_comment[0][0]['utimestamp']).'&nbsp;('.$last_comment[0][0]['id_user'].'):&nbsp;'.$short_comment.'...';
$comentario = '<i>'.date($config['date_format'], $last_comment['utimestamp']).'&nbsp;('.$last_comment['id_user'].'):&nbsp;'.$short_comment.'...';
}
} else {
$rest_time = (time() - $last_comment[0][0]['utimestamp']);
$rest_time = (time() - $last_comment['utimestamp']);
$time_last = (($rest_time / 60) / 60);
$comentario = '<i>'.number_format($time_last, 0).'&nbsp; Hours &nbsp;('.$last_comment[0][0]['id_user'].'):&nbsp;'.$last_comment[0][0]['comment'].'';
$comentario = '<i>'.number_format($time_last, 0).'&nbsp; Hours &nbsp;('.$last_comment['id_user'].'):&nbsp;'.$last_comment['comment'].'';
if (strlen($comentario) > '200px') {
$comentario = '<i>'.number_format($time_last, 0).'&nbsp; Hours &nbsp;('.$last_comment[0][0]['id_user'].'):&nbsp;'.$short_comment.'...';
$comentario = '<i>'.number_format($time_last, 0).'&nbsp; Hours &nbsp;('.$last_comment['id_user'].'):&nbsp;'.$short_comment.'...';
}
}

View File

@ -357,10 +357,10 @@ class Events
);
if (isset($group_rep) === false) {
$group_rep = 0;
$group_rep = EVENT_GROUP_REP_ALL;
}
if ((int) $group_rep !== 0) {
if ((int) $group_rep !== EVENT_GROUP_REP_ALL) {
if ($event['event_rep'] <= 1) {
$event['event_repeated'] = '<i>'.__('No').'</i>';
} else {
@ -1068,7 +1068,7 @@ class Events
$filters['id_agent'] = $this->id_agent;
}
$filters['group_rep'] = 1;
$filters['group_rep'] = EVENT_GROUP_REP_EVENTS;
if (isset($this->limit) === true
&& $this->limit !== -1

View File

@ -84,7 +84,7 @@ ui_require_javascript_file('pandora_events');
$default_filter = [
'status' => EVENT_NO_VALIDATED,
'event_view_hr' => $config['event_view_hr'],
'group_rep' => 1,
'group_rep' => EVENT_GROUP_REP_EVENTS,
'tag_with' => [],
'tag_without' => [],
'history' => false,
@ -360,6 +360,10 @@ if (is_ajax() === true) {
$order['field'] = 'agent_name';
break;
case 'if(te.ack_utimestamp > 0, from_unixtime(te.ack_utimestamp),"") as ack_utimestamp':
$order['field'] = 'ack_utimestamp';
break;
default:
$order['field'] = $field;
break;
@ -1665,9 +1669,10 @@ $inputs[] = $in;
// Duplicates group { events | agents }.
$data = html_print_select(
[
0 => __('All events'),
1 => __('Group events'),
2 => __('Group agents'),
EVENT_GROUP_REP_ALL => __('All events'),
EVENT_GROUP_REP_EVENTS => __('Group events'),
EVENT_GROUP_REP_AGENTS => __('Group agents'),
EVENT_GROUP_REP_EXTRAIDS => __('Group extra id'),
],
'group_rep',
$group_rep,
@ -2300,12 +2305,14 @@ try {
$active_filters_div .= '<div>';
$active_filters_div .= '<div class="label box-shadow">'.__('Duplicated').'</div>';
$active_filters_div .= '<div id="summary_duplicates" class="content">';
if ($group_rep == 0) {
if ($group_rep == EVENT_GROUP_REP_ALL) {
$active_filters_div .= __('All events.');
} else if ($group_rep == 1) {
} else if ($group_rep == EVENT_GROUP_REP_EVENTS) {
$active_filters_div .= __('Group events');
} else if ($group_rep == 2) {
} else if ($group_rep == EVENT_GROUP_REP_AGENTS) {
$active_filters_div .= __('Group agents.');
} else if ($group_rep == EVENT_GROUP_REP_EXTRAIDS) {
$active_filters_div .= __('Group extra id.');
}
$active_filters_div .= '</div>';

View File

@ -410,7 +410,7 @@ if ($access_console_node === true) {
$user_event_filter = [
'status' => EVENT_NO_VALIDATED,
'event_view_hr' => $config['event_view_hr'],
'group_rep' => 1,
'group_rep' => EVENT_GROUP_REP_EVENTS,
'tag_with' => [],
'tag_without' => [],
'history' => false,