wip event list filters

This commit is contained in:
fbsanchez 2019-06-07 22:44:02 +02:00
parent d4d4509266
commit f67bf8c2b7
4 changed files with 114 additions and 81 deletions

View File

@ -88,10 +88,12 @@ 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).
* @param array $fields Fields to retrieve.
* @param array $filter Filters to be applied.
* @param integer $offset Offset (pagination).
* @param integer $limit Limit (pagination).
* @param string $order Sort order.
* @param string $sort_field Sort field.
*
* @return array Events.
* @throws Exception On error.
@ -107,6 +109,7 @@ function events_get_all(
global $config;
if (!is_array($filter)) {
error_log('[events_get_all] Filter must be an array.');
throw new Exception('[events_get_all] Filter must be an array.');
}
@ -115,39 +118,61 @@ function events_get_all(
$fields = ['te.*'];
$count = true;
} else if (!is_array($fields)) {
error_log('[events_get_all] Fields must be an array or "count".');
throw new Exception('[events_get_all] Fields must be an array or "count".');
}
$hour_filter = '';
$sql_filters = [];
if (isset($filter['event_view_hr'])) {
$hour_filter = sprintf(
$sql_filters[] = 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(
$sql_filters[] = sprintf(
' AND id_agente = %d ',
$filter['id_agent']
);
}
$table = events_get_events_table($meta, $history);
if (!empty($filter['event_type']) && $filter['event_type'] != 'all') {
if ($filter['event_type'] == 'warning'
|| $filter['event_type'] == 'critical'
|| $filter['event_type'] == 'normal'
) {
$sql_filters[] = ' AND event_type LIKE "%'.$filter['event_type'].'%"';
} else if ($filter['event_type'] == 'not_normal') {
$sql_filters[] = ' AND (event_type LIKE "%warning%"
OR event_type LIKE "%critical%"
OR event_type LIKE "%unknown%")';
} else {
$sql_filters[] = ' AND event_type = "'.$filter['event_type'].'"';
}
}
if (isset($filter['severity']) && $filter['severity'] > 0) {
$sql_filters[] = sprintf(
' AND criticity = %d ',
$filter['severity']
);
}
$table = events_get_events_table($meta, $history);
$tevento = sprintf(
'(SELECT *
FROM %s
WHERE 1=1 %s %s) te',
WHERE 1=1 %s) te',
$table,
$hour_filter,
$agent_id_filter
join(' ', $sql_filters)
);
$agent_name_filter = '';
// Reset array sql filters.
$sql_filters = [];
if (!empty($filter['agent_alias'])) {
$agent_name_filter = sprintf(
$sql_filters[] = sprintf(
' AND ta.alias = "%s" ',
$filter['agent_alias']
);
@ -203,7 +228,7 @@ function events_get_all(
%s
%s
WHERE 1=1
%s
'.join(' ', $sql_filters).'
%s
%s
',

View File

@ -3065,7 +3065,12 @@ function ui_print_datatable(array $parameters)
alert(error);
} else {';
if (isset($parameters['ajax_postprocess'])) {
$js .= 'json.data.forEach('.$parameters['ajax_postprocess'].');';
$js .= '
if (json.data) {
json.data.forEach('.$parameters['ajax_postprocess'].');
} else {
json.data = {};
}';
}
$js .= '

View File

@ -124,6 +124,7 @@ table.dataTable tbody td {
padding-left: 5px;
padding-top: 0;
vertical-align: middle;
width: 40%;
padding-bottom: 0;
}
@ -141,3 +142,7 @@ table.dataTable tbody td {
fieldset {
margin: 0 auto;
}
.event.flex-row.h100p.nowrap div {
max-width: 98%;
}

View File

@ -89,6 +89,69 @@ $source = get_parameter('filter[source]');
$id_extra = get_parameter('filter[id_extra]');
$user_comment = get_parameter('filter[user_comment]');
// Ajax responses.
if (is_ajax()) {
$get_filter_values = get_parameter('get_filter_values', 0);
$save_event_filter = get_parameter('save_event_filter', 0);
$update_event_filter = get_parameter('update_event_filter', 0);
$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);
$events = events_get_all(
[
'te.*',
'ta.alias as agent_name',
],
$filter,
// Offset.
$start,
// Limit.
$length,
// 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(
$events,
function ($carry, $item) {
$carry[] = (object) $item;
return $carry;
}
);
}
// RecordsTotal && recordsfiltered resultados totales.
echo json_encode(
[
'data' => $data,
'recordsTotal' => $count,
'recordsFiltered' => $count,
]
);
}
// AJAX section ends.
exit;
}
// TAGS.
// Get the tags where the user have permissions in Events reading tasks.
@ -278,71 +341,6 @@ if (io_safe_output($tag_without) == '["0"]') {
* END OF TAGS.
*/
// Ajax responses.
if (is_ajax()) {
$get_filter_values = get_parameter('get_filter_values', 0);
$save_event_filter = get_parameter('save_event_filter', 0);
$update_event_filter = get_parameter('update_event_filter', 0);
$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);
$events = events_get_all(
[
'te.*',
'ta.alias as agent_name',
],
$filter,
// Offset.
$start,
// Limit.
$length,
// 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(
$events,
function ($carry, $item) {
$carry[] = (object) $item;
return $carry;
}
);
}
// RecordsTotal && recordsfiltered resultados totales.
echo json_encode(
[
'data' => $data,
'recordsTotal' => $count,
'recordsFiltered' => $count,
]
);
}
// AJAX section ends.
exit;
}
// View.
$pure = get_parameter('pure', 0);
$url = ui_get_full_url('index.php?sec=eventos&sec2=operation/events/events');