Merge branch 'ent-8273-modificar-item-last-value-en-reports' into 'develop'

Report item last value shows last value on selected interval

See merge request artica/pandorafms!4660
This commit is contained in:
Daniel Rodriguez 2022-02-07 17:22:11 +00:00
commit 3fbbaf031c
2 changed files with 99 additions and 50 deletions

View File

@ -1080,14 +1080,23 @@ function modules_get_agentmodule($id_agentmodule)
} }
function modules_get_table_data($id_agent_module) /**
* Gets data table for agent module
*
* @param integer|null $id_agent_module Id agentmodule.
* @param integer|null $id_type Id module type.
* @return void
*/
function modules_get_table_data(?int $id_agent_module, ?int $id_type)
{ {
$id_type = db_get_value( if ($id_type === null) {
'id_tipo_modulo', $id_type = db_get_value(
'tagente_modulo', 'id_tipo_modulo',
'id_agente_modulo', 'tagente_modulo',
$id_agent_module 'id_agente_modulo',
); $id_agent_module
);
}
$name_type = db_get_value('nombre', 'ttipo_modulo', 'id_tipo', $id_type); $name_type = db_get_value('nombre', 'ttipo_modulo', 'id_tipo', $id_type);

View File

@ -66,6 +66,9 @@ define('REPORT_STATUS_NOT_INIT', 3);
define('REPORT_STATUS_DOWNTIME', 4); define('REPORT_STATUS_DOWNTIME', 4);
define('REPORT_STATUS_IGNORED', 5); define('REPORT_STATUS_IGNORED', 5);
// Clases.
use PandoraFMS\Module;
function reporting_user_can_see_report($id_report, $id_user=null) function reporting_user_can_see_report($id_report, $id_user=null)
{ {
@ -699,7 +702,9 @@ function reporting_make_reporting_data(
case 'last_value': case 'last_value':
$report['contents'][] = reporting_last_value( $report['contents'][] = reporting_last_value(
$report, $report,
$content $content,
$datetime,
$period
); );
break; break;
@ -4071,46 +4076,44 @@ function reporting_database_serialized($report, $content)
/** /**
* Show last value and state of module. * Show last value and state of module.
* *
* @param array $report Data report. * @param array $report Data report.
* @param array $content Content report. * @param array $content Content report.
* @param integer $datetime Date limit of report.
* @param integer $init_date Init date of report.
* *
* @return array * @return array
*/ */
function reporting_last_value($report, $content) function reporting_last_value($report, $content, $datetime, $period)
{ {
global $config; global $config;
try {
$id_meta = null;
if (is_metaconsole()) {
$id_meta = metaconsole_get_id_server($content['server_name']);
$server = metaconsole_get_connection_by_id($id_meta);
}
$module = new Module($content['id_agent_module'], false, $server['id']);
} catch (\Exception $e) {
$result = [];
return reporting_check_structure_content($result);
}
$return['type'] = 'last_value'; $return['type'] = 'last_value';
if (empty($content['name'])) { if (empty($content['name'])) {
$content['name'] = __('Last Value'); $content['name'] = __('Last Value');
} }
if (is_metaconsole()) { $id_agent = $module->agent()->id_agente();
$id_meta = metaconsole_get_id_server($content['server_name']);
$server = metaconsole_get_connection_by_id($id_meta);
if (metaconsole_connect($server) != NOERR) {
$result = [];
return reporting_check_structure_content($result);
}
}
$id_agent = agents_get_module_id(
$content['id_agent_module']
);
$agent_alias = agents_get_alias($id_agent);
$module_name = modules_get_agentmodule_name(
$content['id_agent_module']
);
$id_agent_module = $content['id_agent_module']; $id_agent_module = $content['id_agent_module'];
$agent_description = agents_get_description($id_agent); $agent_alias = $module->agent()->alias();
$agent_group = agents_get_agent_group($id_agent); $module_name = $module->name();
$agent_address = agents_get_address($id_agent); $agent_description = $module->agent()->comentarios();
$agent_group = $module->agent()->group();
$module_description = modules_get_agentmodule_descripcion( $agent_address = $module->agent()->field['direccion'];
$id_agent_module $module_description = $module->descripcion();
);
$items_label = [ $items_label = [
'type' => $return['type'], 'type' => $return['type'],
@ -4138,22 +4141,63 @@ function reporting_last_value($report, $content)
$return['pagebreak'] = $content['pagebreak']; $return['pagebreak'] = $content['pagebreak'];
$return['subtitle'] = $agent_alias.' - '.$module_name; $return['subtitle'] = $agent_alias.' - '.$module_name;
$return['description'] = $content['description']; $return['description'] = $content['description'];
$return['date'] = reporting_get_date_text($report, $content); $return['agent_name_db'] = $module->agent()->id_agente();
$return['agent_name_db'] = agents_get_name($id_agent);
$return['agent_name'] = $agent_alias; $return['agent_name'] = $agent_alias;
$return['module_name'] = $module_name; $return['module_name'] = $module_name;
$return['date'] = reporting_get_date_text($report, $content);
$sql = sprintf( $result = $module->getStatus()->toArray();
'SELECT *
FROM tagente_estado
WHERE id_agente_modulo = %s',
$content['id_agent_module']
);
$result = db_get_row_sql($sql); if ($result === false
|| $result['estado'] == AGENT_MODULE_STATUS_NO_DATA
|| $result['estado'] == AGENT_MODULE_STATUS_NOT_INIT
) {
$result['utimestamp'] = '';
$result['datos'] = __('No data to display within the selected interval');
}
if ($result === false) { if ($datetime < $result['utimestamp']) {
$result = []; $id_tipo_modulo = $module->id_tipo_modulo();
$table_data = modules_get_table_data(null, $id_tipo_modulo);
if ($period !== null) {
$sql = sprintf(
'SELECT datos, utimestamp FROM %s WHERE id_agente_modulo = %d AND utimestamp BETWEEN %d AND %d ORDER BY utimestamp DESC',
$table_data,
$id_agent_module,
($datetime - $period),
$datetime
);
} else {
$sql = sprintf(
'SELECT datos, utimestamp FROM %s WHERE id_agente_modulo = %d AND utimestamp <= %d ORDER BY utimestamp DESC',
$table_data,
$id_agent_module,
$datetime
);
}
$search_in_history_db = db_search_in_history_db($datetime);
try {
$module->connectNode();
} catch (\Exception $e) {
// Do not link items if failed to find them.
$module->restoreConnection();
}
$datos = db_get_row_sql($sql, $search_in_history_db);
// Restore if needed.
$module->restoreConnection();
if ($datos !== false) {
$result['datos'] = $datos['datos'];
$result['utimestamp'] = $datos['utimestamp'];
} else {
$result['utimestamp'] = '-';
$result['estado'] = AGENT_MODULE_STATUS_NO_DATA;
$result['datos'] = __('No data to display within the selected interval');
}
} }
$result['agent_name'] = $agent_alias; $result['agent_name'] = $agent_alias;
@ -4161,10 +4205,6 @@ function reporting_last_value($report, $content)
$return['data'] = $result; $return['data'] = $result;
if (is_metaconsole()) {
metaconsole_restore_db();
}
return reporting_check_structure_content($return); return reporting_check_structure_content($return);
} }