From a89ab565526a522ff8fadc38adeacf32a4ba07f5 Mon Sep 17 00:00:00 2001 From: ramonn Date: Wed, 23 Dec 2009 17:36:32 +0000 Subject: [PATCH] 2009-12-23 Ramon Novoa * include/functions_db.php: Fixed avg. calculation in custom reports. git-svn-id: https://svn.code.sf.net/p/pandora/code/trunk@2238 c3f86ba8-e40f-0410-aaad-9ba5e7f4b01f --- pandora_console/ChangeLog | 4 ++ pandora_console/include/functions_db.php | 47 ++++++++++++++++-------- 2 files changed, 36 insertions(+), 15 deletions(-) diff --git a/pandora_console/ChangeLog b/pandora_console/ChangeLog index dacb139395..87b85400dc 100644 --- a/pandora_console/ChangeLog +++ b/pandora_console/ChangeLog @@ -1,3 +1,7 @@ +2009-12-23 Ramon Novoa + + * include/functions_db.php: Fixed avg. calculation in custom reports. + 2009-12-23 Miguel de Dios * godmode/agentes/planned_downtime.php: fixes bug in several fields of diff --git a/pandora_console/include/functions_db.php b/pandora_console/include/functions_db.php index 624b52e284..6c09ab1e5d 100644 --- a/pandora_console/include/functions_db.php +++ b/pandora_console/include/functions_db.php @@ -2487,27 +2487,44 @@ function get_agentmodule_data_average ($id_agent_module, $period, $date = 0) { $datelimit = $date - $period; - $sql = sprintf ("SELECT SUM(datos), COUNT(*) FROM tagente_datos + $sql = sprintf ("SELECT * FROM tagente_datos WHERE id_agente_modulo = %d AND utimestamp > %d AND utimestamp <= %d ORDER BY utimestamp ASC", $id_agent_module, $datelimit, $date); - $values = get_db_row_sql ($sql, true); - - $sum = (float) $values[0]; - $total = (int) $values[1]; - - /* Get also the previous data before the selected interval. */ - $previous_data = get_previous_data ($id_agent_module, $datelimit); - - if ($previous_data) { - return ($previous_data['datos'] + $sum) / ($total + 1); - } elseif ($total > 0) { - return $sum / $total; + $values = get_db_all_rows_sql ($sql, true); + if ($values === false) { + $values = array (); } - - return 0; + + /* Get also the previous data before the selected interval. */ + $sum = 0; + $total = 0; + $module_interval = get_module_interval ($id_agent_module); + $previous_data = get_previous_data ($id_agent_module, $datelimit); + if ($previous_data !== false) { + $values = array_merge (array ($previous_data), $values); + } + + foreach ($values as $data) { + $interval_total = 1; + $interval_sum = $data['datos']; + if ($previous_data !== false && $data['utimestamp'] - $previous_data['utimestamp'] > $module_interval) { + $interval_total = round (($data['utimestamp'] - $previous_data['utimestamp']) / $module_interval, 0); + $interval_sum *= $interval_total; + + } + $total += $interval_total; + $sum += $interval_sum; + $previous_data = $data; + } + + if ($total == 0) { + return 0; + } + + return $sum / $total; } /**