wip reports alert actions

This commit is contained in:
Daniel Barbero Martin 2021-11-29 17:55:09 +01:00
parent 75354a97d9
commit e8bf86a789
6 changed files with 338 additions and 99 deletions

View File

@ -1770,6 +1770,7 @@ $class = 'databox filters';
<?php <?php
if (empty($id_agents) === true) { if (empty($id_agents) === true) {
$all_modules = []; $all_modules = [];
$modules_select = [];
} else { } else {
$all_modules = db_get_all_rows_sql( $all_modules = db_get_all_rows_sql(
sprintf( sprintf(
@ -1791,12 +1792,12 @@ $class = 'databox filters';
); );
$all_modules = array_unique($all_modules); $all_modules = array_unique($all_modules);
}
$modules_select = get_same_modules( $modules_select = get_same_modules(
array_values($id_agents), array_values($id_agents),
array_values($idAgentModule) array_values($idAgentModule)
); );
}
html_print_select( html_print_select(
$all_modules, $all_modules,
@ -1859,6 +1860,15 @@ $class = 'databox filters';
); );
} }
$alert_templates = array_reduce(
$alert_templates,
function ($carry, $item) {
$carry[$item['id']] = $item['name'];
return $carry;
},
[]
);
html_print_select( html_print_select(
$alert_templates, $alert_templates,
'alert_templates[]', 'alert_templates[]',
@ -3293,24 +3303,25 @@ $class = 'databox filters';
<td> <td>
<?php <?php
$valuesGroupBy = [ $valuesGroupBy = [
'agent' => 'Agent', 'agent' => __('Agent'),
'module' => 'Module', 'module' => __('Module'),
'action' => 'Action', 'group' => __('Group'),
'template' => 'Template', 'template' => __('Template'),
]; ];
html_print_select( html_print_select(
$valuesGroupBy, $valuesGroupBy,
'group_by[]', 'group_by',
$group_by, $group_by,
'', '',
'', __('None'),
0, 0,
false, false,
true, false,
false, false,
'', '',
false, false,
'min-width: 500px; max-height: 100px', '',
false, false,
false, false,
false, false,
@ -3319,8 +3330,6 @@ $class = 'databox filters';
false, false,
false, false,
false, false,
true,
true,
true true
); );
?> ?>

View File

@ -1649,6 +1649,45 @@ function agents_get_name($id_agent, $case='none')
} }
/**
* Get the agents names of an agent.
*
* @param array $array_ids Agents ids.
*
* @return array Id => name.
*/
function agents_get_alias_array($array_ids)
{
if (is_array($array_ids) === false || empty($array_ids) === true) {
return [];
}
$sql = sprintf(
'SELECT id_agente as id, alias as `name`
FROM tagente
WHERE id_agente IN (%s)',
implode(',', $array_ids)
);
$result = db_get_all_rows_sql($sql);
if ($result === false) {
$result = [];
}
$result = array_reduce(
$result,
function ($carry, $item) {
$carry[$item['id']] = $item['name'];
return $carry;
},
[]
);
return $result;
}
/** /**
* Get alias of an agent (cached function). * Get alias of an agent (cached function).
* *

View File

@ -2841,28 +2841,26 @@ function alerts_get_agent_modules(
function alerts_get_actions_names($actions, $reduce=false) function alerts_get_actions_names($actions, $reduce=false)
{ {
if (empty($actions) === true) {
return [];
}
$where = ''; $where = '';
if (empty($actions) === false) {
if (is_array($actions) === true) { if (is_array($actions) === true) {
$where = sprintf( $where = sprintf(
'id IN (%s)', 'WHERE id IN (%s)',
implode(',', $actions) implode(',', $actions)
); );
} else { } else {
$where = sprintf(' id = %d', $actions); $where = sprintf('WHERE id = %d', $actions);
}
} }
$sqltest = sprintf( $sql = sprintf(
'SELECT id, `name` 'SELECT id, `name`
FROM talert_actions FROM talert_actions
WHERE %s', %s',
$where $where
); );
$result = db_get_all_rows_sql($sqltest); $result = db_get_all_rows_sql($sql);
if ($result === false) { if ($result === false) {
$result = []; $result = [];
@ -2883,22 +2881,30 @@ function alerts_get_actions_names($actions, $reduce=false)
} }
function alerts_get_alert_fired($filters=[], $groupsBy=[]) function alerts_get_alert_fired($filters=[], $groupsBy=[], $total=false)
{ {
global $config; global $config;
hd(5);
$filter_date = ''; $filter_date = '';
if (isset($filters['period']) === true if (isset($filters['period']) === true
&& empty($filters['period']) === false && empty($filters['period']) === false
) { ) {
$filter_date = sprintf('AND utimestamp > %d', (time() - $filters['period'])); $filter_date = sprintf(
'AND tevento.utimestamp > %d',
(time() - $filters['period'])
);
} }
$filter_group = ''; $filter_group = '';
if (isset($filters['group']) === true if (isset($filters['group']) === true
&& empty($filters['group']) === false && empty($filters['group']) === false
) { ) {
$filter_group = sprintf('AND id_grupo = %d', $filters['group']); $filter_group = sprintf(
'AND tevento.id_grupo = %d',
$filters['group']
);
} }
$filter_agents = ''; $filter_agents = '';
@ -2906,7 +2912,7 @@ function alerts_get_alert_fired($filters=[], $groupsBy=[])
&& empty($filters['agents']) === false && empty($filters['agents']) === false
) { ) {
$filter_agents = sprintf( $filter_agents = sprintf(
'AND id_agente IN (%s)', 'AND tevento.id_agente IN (%s)',
implode(',', $filters['agents']) implode(',', $filters['agents'])
); );
} }
@ -2916,7 +2922,7 @@ function alerts_get_alert_fired($filters=[], $groupsBy=[])
&& empty($filters['modules']) === false && empty($filters['modules']) === false
) { ) {
$filter_modules = sprintf( $filter_modules = sprintf(
'AND id_agentmodule IN (%s)', 'AND tevento.id_agentmodule IN (%s)',
implode(',', $filters['modules']) implode(',', $filters['modules'])
); );
} }
@ -2926,17 +2932,21 @@ function alerts_get_alert_fired($filters=[], $groupsBy=[])
&& empty($filters['templates']) === false && empty($filters['templates']) === false
) { ) {
$filter_templates = sprintf( $filter_templates = sprintf(
'AND id_alert_am IN (%s)', 'AND talert_template_modules.id_alert_template IN (%s)',
implode(',', $filters['templates']) implode(',', $filters['templates'])
); );
} }
// TODO: ALL;
$actions_names = alerts_get_actions_names($filters['actions'], true);
$group_array = [];
$filter_actions = ''; $filter_actions = '';
$fields_actions = []; $fields_actions = [];
if (isset($filters['actions']) === true if (isset($filters['actions']) === true
&& empty($filters['actions']) === false && empty($filters['actions']) === false
) { ) {
$actions_names = alerts_get_actions_names($filters['actions'], true);
$filter_actions .= 'AND ( '; $filter_actions .= 'AND ( ';
$first = true; $first = true;
foreach ($actions_names as $name_action) { foreach ($actions_names as $name_action) {
@ -2945,12 +2955,12 @@ function alerts_get_alert_fired($filters=[], $groupsBy=[])
} }
$filter_actions .= sprintf( $filter_actions .= sprintf(
"JSON_CONTAINS(custom_data, '\"%s\"', '\$.actions')", "JSON_CONTAINS(tevento.custom_data, '\"%s\"', '\$.actions')",
io_safe_output($name_action) io_safe_output($name_action)
); );
$fields_actions[$name_action] = sprintf( $fields_actions[$name_action] = sprintf(
"SUM(JSON_CONTAINS(custom_data, '\"%s\"', '\$.actions')) as '%s'", "SUM(JSON_CONTAINS(tevento.custom_data, '\"%s\"', '\$.actions')) as '%s'",
io_safe_output($name_action), io_safe_output($name_action),
io_safe_output($name_action) io_safe_output($name_action)
); );
@ -2959,18 +2969,41 @@ function alerts_get_alert_fired($filters=[], $groupsBy=[])
} }
$filter_actions .= ' ) '; $filter_actions .= ' ) ';
} else {
foreach ($actions_names as $name_action) {
$fields[] = sprintf(
"SUM(JSON_CONTAINS(tevento.custom_data, '\"%s\"', '\$.actions')) as '%s'",
io_safe_output($name_action),
io_safe_output($name_action)
);
}
} }
$group_array = []; if ($total === false) {
$fields = ['COUNT(tevento.id_evento) as fired']; if (is_array($fields_actions) === true
if (isset($groupsBy['group_by']) === true && empty($fields_actions) === false
&& empty($filters['group_by']) === false
) { ) {
foreach ($groupsBy['group_by'] as $groupBy) { foreach ($fields_actions as $name => $field) {
switch ($groupBy) { $fields[] = $field;
}
}
$names_modules = modules_get_agentmodule_name_array(
array_values($filters['modules'])
);
} else {
$fields = ['COUNT(tevento.id_evento) as fired'];
}
$names_search = [];
if (isset($groupsBy['group_by']) === true) {
switch ($groupsBy['group_by']) {
case 'module': case 'module':
$fields[] = 'id_agentmodule'; $fields[] = 'tevento.id_agentmodule as module';
$group_array[] = 'id_agentmodule'; $group_array[] = 'tevento.id_agentmodule';
$names_search = modules_get_agentmodule_name_array(
array_values($filters['modules'])
);
break; break;
case 'action': case 'action':
@ -2985,18 +3018,24 @@ function alerts_get_alert_fired($filters=[], $groupsBy=[])
break; break;
case 'template': case 'template':
$fields[] = 'talert_template_modules.id_alert_template as id_template'; $fields[] = 'talert_template_modules.id_alert_template as template';
$group_array[] = 'talert_template_modules.id_alert_template'; $group_array[] = 'talert_template_modules.id_alert_template';
$names_search = alerts_get_templates_name_array(
array_values($filters['templates'])
);
break; break;
case 'agent': case 'agent':
$fields[] = 'id_agente as id_agent'; $fields[] = 'tevento.id_agente as agent';
$group_array[] = 'id_agente'; $group_array[] = 'tevento.id_agente';
$names_search = agents_get_alias_array(
array_values($filters['agents'])
);
break; break;
case 'group': case 'group':
$fields[] = 'id_grupo as id_group'; $fields[] = 'tevento.id_grupo as `group`';
$group_array[] = 'id_grupo'; $group_array[] = 'tevento.id_grupo';
break; break;
default: default:
@ -3004,13 +3043,10 @@ function alerts_get_alert_fired($filters=[], $groupsBy=[])
break; break;
} }
} }
}
if (isset($groupsBy['lapse']) === true if (isset($groupsBy['lapse']) === true) {
&& empty($filters['lapse']) === false
) {
$fields[] = sprintf( $fields[] = sprintf(
'ROUND((CEILING(UNIX_TIMESTAMP(`timestamp`) / %d) * %d)) AS period', 'ROUND((CEILING(UNIX_TIMESTAMP(tevento.timestamp) / %d) * %d)) AS Period',
(int) $groupsBy['lapse'], (int) $groupsBy['lapse'],
(int) $groupsBy['lapse'] (int) $groupsBy['lapse']
); );
@ -3028,8 +3064,10 @@ function alerts_get_alert_fired($filters=[], $groupsBy=[])
FROM tevento FROM tevento
INNER JOIN talert_template_modules INNER JOIN talert_template_modules
ON talert_template_modules.id = tevento.id_alert_am ON talert_template_modules.id = tevento.id_alert_am
INNER JOIN talert_templates
ON talert_templates.id = talert_template_modules.id_alert_template
WHERE custom_data != "" WHERE custom_data != ""
AND event_type="alert_fired" AND tevento.event_type="alert_fired"
%s %s
%s %s
%s %s
@ -3049,12 +3087,89 @@ function alerts_get_alert_fired($filters=[], $groupsBy=[])
$data = db_get_all_rows_sql($query); $data = db_get_all_rows_sql($query);
// TODO :XXx if ($data === false) {
hd($data); $data = [];
}
foreach ($data as $key => $value) { if ($total === false) {
if (isset($value['period']) === true) { if (empty($data) === false) {
hd(date('y-m-d h:i:s', $value['period'])); $data = array_reduce(
$data,
function ($carry, $item) use ($groupsBy) {
$period = $item['Period'];
$grby = $item[$groupsBy['group_by']];
unset($item['Period']);
unset($item[$groupsBy['group_by']]);
$carry[$period][$grby] = $item;
return $carry;
},
[]
);
$first_element = array_shift($data);
$first_element = array_shift($first_element);
$clone = [];
foreach ($first_element as $key_clone => $value_clone) {
$clone[$key_clone] = 0;
}
$result = [];
foreach ($data as $period => $array_data) {
foreach ($names_search as $id => $name) {
foreach ($array_data as $grby => $values) {
if ($grby === $id) {
$values[$groupsBy['group_by']] = $name;
$result[$period][$id] = $values;
} else {
$clone[$groupsBy['group_by']] = $name;
$result[$period][$id] = $clone;
} }
} }
}
}
$data = $result;
}
}
return $data;
}
/**
* Get the templates names of an agent.
*
* @param array $array_ids Templates ids.
*
* @return array Id => name.
*/
function alerts_get_templates_name_array($array_ids)
{
if (is_array($array_ids) === false || empty($array_ids) === true) {
return [];
}
$sql = sprintf(
'SELECT id, `name`
FROM talert_templates
WHERE id IN (%s)',
implode(',', $array_ids)
);
$result = db_get_all_rows_sql($sql);
if ($result === false) {
$result = [];
}
$result = array_reduce(
$result,
function ($carry, $item) {
$carry[$item['id']] = $item['name'];
return $carry;
},
[]
);
return $result;
} }

View File

@ -3513,7 +3513,8 @@ function get_same_modules($agents, $modules)
$name_modules = modules_get_agentmodule_name_array(array_values($modules)); $name_modules = modules_get_agentmodule_name_array(array_values($modules));
$sql = sprintf( $sql = sprintf(
'SELECT id_agente_modulo as id 'SELECT id_agente_modulo as id,
nombre as `name`
FROM tagente_modulo FROM tagente_modulo
WHERE id_agente IN (%s)', WHERE id_agente IN (%s)',
implode(',', array_values($agents)) implode(',', array_values($agents))
@ -3528,7 +3529,7 @@ function get_same_modules($agents, $modules)
$all = array_reduce( $all = array_reduce(
$all, $all,
function ($carry, $item) use ($name_modules) { function ($carry, $item) use ($name_modules) {
if (isset($name_modules[$item['id']]) === true) { if (array_search($item['name'], $name_modules)) {
$carry[$item['id']] = $item['id']; $carry[$item['id']] = $item['id'];
} }

View File

@ -2670,6 +2670,8 @@ function reporting_alert_report_actions($report, $content)
global $config; global $config;
$return = []; $return = [];
hd('4');
$return['type'] = 'alert_report_actions'; $return['type'] = 'alert_report_actions';
if (empty($content['name']) === true) { if (empty($content['name']) === true) {
$content['name'] = __('Alert actions'); $content['name'] = __('Alert actions');
@ -2699,19 +2701,40 @@ function reporting_alert_report_actions($report, $content)
$filters = [ $filters = [
'group' => $id_group, 'group' => $id_group,
'agents' => $agents, 'agents' => json_decode(
'modules' => $modules, io_safe_output(base64_decode($agents)),
true
),
'modules' => json_decode(
io_safe_output(base64_decode($modules)),
true
),
'templates' => $templates, 'templates' => $templates,
'actions' => $actions, 'actions' => $actions,
'period' => $period, 'period' => $period,
]; ];
$goupsBy = [ $groupsBy = [
'group_by' => $group_by, 'group_by' => $group_by,
'lapse' => $lapse, 'lapse' => $lapse,
]; ];
alerts_get_alert_fired($filters, $goupsBy); $return['filters'] = $filters;
$return['groupsBy'] = $groupsBy;
$return['data']['data'] = alerts_get_alert_fired(
$filters,
$groupsBy,
false
);
if ((bool) $show_summary === true) {
$return['data']['summary'] = alerts_get_alert_fired(
$filters,
$groupsBy,
true
);
}
return reporting_check_structure_content($return); return reporting_check_structure_content($return);
} }

View File

@ -2735,7 +2735,59 @@ function reporting_html_group_configuration($table, $item, $pdf=0)
function reporting_html_alert_report_actions($table, $item, $pdf=0) function reporting_html_alert_report_actions($table, $item, $pdf=0)
{ {
hd($item); hd(6);
$data = $item['data'];
$filters = $item['filters'];
$groupsBy = $item['groupsBy'];
$output = '';
if (isset($data['data']) === true
&& empty($data['data']) === false
) {
if (isset($groupsBy['lapse']) === true
&& empty($groupsBy['lapse']) === false
) {
}
foreach ($data['data'] as $period => $data_array) {
$output .= get_alert_table($data_array);
}
} else {
// TODO: SMS FAIL.
}
if ($pdf === 0) {
$table->colspan['alert_report_action']['cell'] = 3;
$table->cellstyle['alert_report_action']['cell'] = 'text-align: center;';
$table->data['alert_report_action']['cell'] = $output;
} else {
return $output;
}
}
function get_alert_table($data)
{
$table = new StdCLass();
$table->width = '100%';
$table->data = [];
$table->head = [];
$head = array_shift($data);
hd($head);
foreach (array_reverse(array_keys($head)) as $name) {
$table->head[$name] = $name;
}
foreach ($data as $key => $params) {
foreach (array_reverse($params) as $name => $value) {
$table->data[$key][$name] = $value;
}
}
return html_print_table($table, true);
} }