diff --git a/pandora_console/godmode/reporting/reporting_builder.item_editor.php b/pandora_console/godmode/reporting/reporting_builder.item_editor.php index ba39816bf7..6562813f2a 100755 --- a/pandora_console/godmode/reporting/reporting_builder.item_editor.php +++ b/pandora_console/godmode/reporting/reporting_builder.item_editor.php @@ -1770,6 +1770,7 @@ $class = 'databox filters'; 'Agent', - 'module' => 'Module', - 'action' => 'Action', - 'template' => 'Template', + 'agent' => __('Agent'), + 'module' => __('Module'), + 'group' => __('Group'), + 'template' => __('Template'), ]; + html_print_select( $valuesGroupBy, - 'group_by[]', + 'group_by', $group_by, '', - '', + __('None'), 0, false, - true, + false, false, '', false, - 'min-width: 500px; max-height: 100px', + '', false, false, false, @@ -3319,8 +3330,6 @@ $class = 'databox filters'; false, false, false, - true, - true, true ); ?> diff --git a/pandora_console/include/functions_agents.php b/pandora_console/include/functions_agents.php index 28f563b8e7..ff8f618f10 100644 --- a/pandora_console/include/functions_agents.php +++ b/pandora_console/include/functions_agents.php @@ -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). * diff --git a/pandora_console/include/functions_alerts.php b/pandora_console/include/functions_alerts.php index ac551944ba..d8cbdeee40 100644 --- a/pandora_console/include/functions_alerts.php +++ b/pandora_console/include/functions_alerts.php @@ -2841,28 +2841,26 @@ function alerts_get_agent_modules( function alerts_get_actions_names($actions, $reduce=false) { - if (empty($actions) === true) { - return []; - } - $where = ''; - if (is_array($actions) === true) { - $where = sprintf( - 'id IN (%s)', - implode(',', $actions) - ); - } else { - $where = sprintf(' id = %d', $actions); + if (empty($actions) === false) { + if (is_array($actions) === true) { + $where = sprintf( + 'WHERE id IN (%s)', + implode(',', $actions) + ); + } else { + $where = sprintf('WHERE id = %d', $actions); + } } - $sqltest = sprintf( + $sql = sprintf( 'SELECT id, `name` FROM talert_actions - WHERE %s', + %s', $where ); - $result = db_get_all_rows_sql($sqltest); + $result = db_get_all_rows_sql($sql); if ($result === false) { $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; + hd(5); + $filter_date = ''; if (isset($filters['period']) === true && 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 = ''; if (isset($filters['group']) === true && 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 = ''; @@ -2906,7 +2912,7 @@ function alerts_get_alert_fired($filters=[], $groupsBy=[]) && empty($filters['agents']) === false ) { $filter_agents = sprintf( - 'AND id_agente IN (%s)', + 'AND tevento.id_agente IN (%s)', implode(',', $filters['agents']) ); } @@ -2916,7 +2922,7 @@ function alerts_get_alert_fired($filters=[], $groupsBy=[]) && empty($filters['modules']) === false ) { $filter_modules = sprintf( - 'AND id_agentmodule IN (%s)', + 'AND tevento.id_agentmodule IN (%s)', implode(',', $filters['modules']) ); } @@ -2926,17 +2932,21 @@ function alerts_get_alert_fired($filters=[], $groupsBy=[]) && empty($filters['templates']) === false ) { $filter_templates = sprintf( - 'AND id_alert_am IN (%s)', + 'AND talert_template_modules.id_alert_template IN (%s)', implode(',', $filters['templates']) ); } + // TODO: ALL; + $actions_names = alerts_get_actions_names($filters['actions'], true); + + $group_array = []; + $filter_actions = ''; $fields_actions = []; if (isset($filters['actions']) === true && empty($filters['actions']) === false ) { - $actions_names = alerts_get_actions_names($filters['actions'], true); $filter_actions .= 'AND ( '; $first = true; foreach ($actions_names as $name_action) { @@ -2945,12 +2955,12 @@ function alerts_get_alert_fired($filters=[], $groupsBy=[]) } $filter_actions .= sprintf( - "JSON_CONTAINS(custom_data, '\"%s\"', '\$.actions')", + "JSON_CONTAINS(tevento.custom_data, '\"%s\"', '\$.actions')", io_safe_output($name_action) ); $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) ); @@ -2959,58 +2969,84 @@ function alerts_get_alert_fired($filters=[], $groupsBy=[]) } $filter_actions .= ' ) '; - } - - $group_array = []; - $fields = ['COUNT(tevento.id_evento) as fired']; - if (isset($groupsBy['group_by']) === true - && empty($filters['group_by']) === false - ) { - foreach ($groupsBy['group_by'] as $groupBy) { - switch ($groupBy) { - case 'module': - $fields[] = 'id_agentmodule'; - $group_array[] = 'id_agentmodule'; - break; - - case 'action': - if (is_array($fields_actions) === true - && empty($fields_actions) === false - ) { - foreach ($fields_actions as $name => $field) { - $fields[] = $field; - $group_array[] = '"'.$name.'"'; - } - } - break; - - case 'template': - $fields[] = 'talert_template_modules.id_alert_template as id_template'; - $group_array[] = 'talert_template_modules.id_alert_template'; - break; - - case 'agent': - $fields[] = 'id_agente as id_agent'; - $group_array[] = 'id_agente'; - break; - - case 'group': - $fields[] = 'id_grupo as id_group'; - $group_array[] = 'id_grupo'; - break; - - default: - // Nothing. - break; - } + } 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) + ); } } - if (isset($groupsBy['lapse']) === true - && empty($filters['lapse']) === false - ) { + if ($total === false) { + if (is_array($fields_actions) === true + && empty($fields_actions) === false + ) { + foreach ($fields_actions as $name => $field) { + $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': + $fields[] = 'tevento.id_agentmodule as module'; + $group_array[] = 'tevento.id_agentmodule'; + $names_search = modules_get_agentmodule_name_array( + array_values($filters['modules']) + ); + break; + + case 'action': + if (is_array($fields_actions) === true + && empty($fields_actions) === false + ) { + foreach ($fields_actions as $name => $field) { + $fields[] = $field; + $group_array[] = '"'.$name.'"'; + } + } + break; + + case 'template': + $fields[] = 'talert_template_modules.id_alert_template as template'; + $group_array[] = 'talert_template_modules.id_alert_template'; + $names_search = alerts_get_templates_name_array( + array_values($filters['templates']) + ); + break; + + case 'agent': + $fields[] = 'tevento.id_agente as agent'; + $group_array[] = 'tevento.id_agente'; + $names_search = agents_get_alias_array( + array_values($filters['agents']) + ); + break; + + case 'group': + $fields[] = 'tevento.id_grupo as `group`'; + $group_array[] = 'tevento.id_grupo'; + break; + + default: + // Nothing. + break; + } + } + + if (isset($groupsBy['lapse']) === true) { $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'] ); @@ -3028,8 +3064,10 @@ function alerts_get_alert_fired($filters=[], $groupsBy=[]) FROM tevento INNER JOIN talert_template_modules 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 != "" - AND event_type="alert_fired" + AND tevento.event_type="alert_fired" %s %s %s @@ -3049,12 +3087,89 @@ function alerts_get_alert_fired($filters=[], $groupsBy=[]) $data = db_get_all_rows_sql($query); - // TODO :XXx - hd($data); + if ($data === false) { + $data = []; + } - foreach ($data as $key => $value) { - if (isset($value['period']) === true) { - hd(date('y-m-d h:i:s', $value['period'])); + if ($total === false) { + if (empty($data) === false) { + $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; } diff --git a/pandora_console/include/functions_modules.php b/pandora_console/include/functions_modules.php index b9a737e1d7..6af27daa71 100755 --- a/pandora_console/include/functions_modules.php +++ b/pandora_console/include/functions_modules.php @@ -3513,7 +3513,8 @@ function get_same_modules($agents, $modules) $name_modules = modules_get_agentmodule_name_array(array_values($modules)); $sql = sprintf( - 'SELECT id_agente_modulo as id + 'SELECT id_agente_modulo as id, + nombre as `name` FROM tagente_modulo WHERE id_agente IN (%s)', implode(',', array_values($agents)) @@ -3528,7 +3529,7 @@ function get_same_modules($agents, $modules) $all = array_reduce( $all, 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']; } diff --git a/pandora_console/include/functions_reporting.php b/pandora_console/include/functions_reporting.php index 1ad9b7cc16..a6c754598b 100755 --- a/pandora_console/include/functions_reporting.php +++ b/pandora_console/include/functions_reporting.php @@ -2670,6 +2670,8 @@ function reporting_alert_report_actions($report, $content) global $config; $return = []; + hd('4'); + $return['type'] = 'alert_report_actions'; if (empty($content['name']) === true) { $content['name'] = __('Alert actions'); @@ -2699,19 +2701,40 @@ function reporting_alert_report_actions($report, $content) $filters = [ 'group' => $id_group, - 'agents' => $agents, - 'modules' => $modules, + 'agents' => json_decode( + io_safe_output(base64_decode($agents)), + true + ), + 'modules' => json_decode( + io_safe_output(base64_decode($modules)), + true + ), 'templates' => $templates, 'actions' => $actions, 'period' => $period, ]; - $goupsBy = [ + $groupsBy = [ 'group_by' => $group_by, '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); } diff --git a/pandora_console/include/functions_reporting_html.php b/pandora_console/include/functions_reporting_html.php index 40463ddcee..34feeee092 100644 --- a/pandora_console/include/functions_reporting_html.php +++ b/pandora_console/include/functions_reporting_html.php @@ -2735,7 +2735,59 @@ function reporting_html_group_configuration($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); }