From d6a4c2bee9739270d23265fea194aeb35c6c1acb Mon Sep 17 00:00:00 2001 From: guruevi Date: Mon, 8 Sep 2008 20:32:23 +0000 Subject: [PATCH] 2008-09-08 Evi Vanoost * reporting/fgraph.php: Further updating of the queries for the godmode/db directories. Mainly making sure all data outputs (graph or text) have the same order for simple overview. Also fixed some minor issues like the one Sancho fixed (array_multisort). Made some strings translatable * godmode/db/times_incl.php: This file is actually only relied on by db_info.php so I removed it and included the contents in db_info * godmode/db/db_purge.php: Since we went over the threshhold of 1,5M data entries on a P4, the page would timeout from the browser out. So I reworked the page to clarify the variables and adhere to style. Put the delete query in a transaction (since it might be that either timeouts or other errors don't clean the whole database up and then you have to do it manually. Code is also prettier now. * godmode/db/db_main.php: Made it display the actual numbers instead of repeating the string. * godmode/db/db_info_data.php: Moved into db_info.php * godmode/db/db_info.php: Introduced db_info_data.php as a hidden Div. This will allow quicker access without adding another menu. The query will come from cache anyway (SQL cache) so it's faster too * godmode/db/db_event.php: Removed reference to times_incl.php * godmode/db/db_audit.php: Removed reference to times_incl.php git-svn-id: https://svn.code.sf.net/p/pandora/code/trunk@1089 c3f86ba8-e40f-0410-aaad-9ba5e7f4b01f --- pandora_console/ChangeLog | 31 ++ pandora_console/godmode/db/db_audit.php | 7 - pandora_console/godmode/db/db_event.php | 8 - pandora_console/godmode/db/db_info.php | 52 +++- pandora_console/godmode/db/db_info_data.php | 74 ----- pandora_console/godmode/db/db_main.php | 22 +- pandora_console/godmode/db/db_purge.php | 310 ++++++++++---------- pandora_console/godmode/db/times_incl.php | 38 --- pandora_console/reporting/fgraph.php | 162 +++++----- 9 files changed, 306 insertions(+), 398 deletions(-) delete mode 100644 pandora_console/godmode/db/db_info_data.php delete mode 100644 pandora_console/godmode/db/times_incl.php diff --git a/pandora_console/ChangeLog b/pandora_console/ChangeLog index 4fb415d165..8158b137de 100644 --- a/pandora_console/ChangeLog +++ b/pandora_console/ChangeLog @@ -1,3 +1,34 @@ +2008-09-08 Evi Vanoost + + * reporting/fgraph.php: Further updating of the queries for the + godmode/db directories. Mainly making sure all data outputs (graph or + text) have the same order for simple overview. Also fixed some minor + issues like the one Sancho fixed (array_multisort). Made some strings + translatable + + * godmode/db/times_incl.php: This file is actually only relied on by + db_info.php so I removed it and included the contents in db_info + + * godmode/db/db_purge.php: Since we went over the threshhold of 1,5M + data entries on a P4, the page would timeout from the browser out. So + I reworked the page to clarify the variables and adhere to style. Put + the delete query in a transaction (since it might be that either + timeouts or other errors don't clean the whole database up and then + you have to do it manually. Code is also prettier now. + + * godmode/db/db_main.php: Made it display the actual numbers instead of + repeating the string. + + * godmode/db/db_info_data.php: Moved into db_info.php + + * godmode/db/db_info.php: Introduced db_info_data.php as a hidden Div. + This will allow quicker access without adding another menu. The query + will come from cache anyway (SQL cache) so it's faster too + + * godmode/db/db_event.php: Removed reference to times_incl.php + + * godmode/db/db_audit.php: Removed reference to times_incl.php + 2008-09-04 Esteban Sanchez * extensions/update_manager/lib/libupdatemanager_client.php: Updated diff --git a/pandora_console/godmode/db/db_audit.php b/pandora_console/godmode/db/db_audit.php index 357e1ea064..1c94c0f1d7 100644 --- a/pandora_console/godmode/db/db_audit.php +++ b/pandora_console/godmode/db/db_audit.php @@ -37,17 +37,10 @@ if (! give_acl ($config['id_user'], 0, "DM")) { - A more powerful selection (by Agent, by Module, etc). */ -require("godmode/db/times_incl.php"); - -$datos_rango3=0; -$datos_rango2=0; -$datos_rango1=0; - # ADQUIRE DATA PASSED AS FORM PARAMETERS # ====================================== # Purge data using dates -# Purge data using dates if (isset($_POST["purgedb"])){ # Fixed 2005-1-13, nil $from_date = get_parameter_post("date_purge"); $query = sprintf("DELETE FROM `tsesion` WHERE `fecha` < '%s';",$from_date); diff --git a/pandora_console/godmode/db/db_event.php b/pandora_console/godmode/db/db_event.php index 6586667e3e..03f9b44885 100644 --- a/pandora_console/godmode/db/db_event.php +++ b/pandora_console/godmode/db/db_event.php @@ -27,17 +27,9 @@ if (! give_acl ($config['id_user'], 0, "DM")) { return; } - -require ("godmode/db/times_incl.php"); - -$datos_rango3 = 0; -$datos_rango2 = 0; -$datos_rango1 = 0; - # ADQUIRE DATA PASSED AS FORM PARAMETERS # ====================================== # Purge data using dates -# Purge data using dates if (isset ($_POST["date_purge"])){ $from_date = get_parameter_post ("date_purge"); $query = sprintf ("DELETE FROM `tevento` WHERE `timestamp` < '%s'",$from_date); diff --git a/pandora_console/godmode/db/db_info.php b/pandora_console/godmode/db/db_info.php index 3e661dfbbd..dfc13ee6ae 100644 --- a/pandora_console/godmode/db/db_info.php +++ b/pandora_console/godmode/db/db_info.php @@ -41,14 +41,50 @@ if (! give_acl ($config['id_user'], 0, "DM")) { echo "

".__('Database Maintenance')." > "; echo __('Database Information')."

"; -echo ""; -echo "
"; +echo '
'; +echo ''; +echo ''; +echo '
'; echo '

'.__('Modules per agent').'

'; -echo "
"; -echo "

"; -echo "
"; +echo '
'; +echo '

'; echo '

'.__('Packets per agent').'

'; -echo "
"; -echo "

".__('Press here to get DB Info as text').""; -echo "
"; +echo '
'; +echo '
'; +echo ''.__('Press here to get database information as text').''; +echo ''; ?> diff --git a/pandora_console/godmode/db/db_info_data.php b/pandora_console/godmode/db/db_info_data.php deleted file mode 100644 index ca2b14d4f2..0000000000 --- a/pandora_console/godmode/db/db_info_data.php +++ /dev/null @@ -1,74 +0,0 @@ -".__('Database Maintenance')." > "; -echo __('Database Statistics per Agent').""; - -$table->data = array (); -$table->head = array (); -$table->head[0] = __('Agent name'); -$table->head[1] = __('Assigned module'); -$table->head[2] = __('Total data'); - -$sql = "SELECT `id_agente`, `nombre` FROM `tagente`"; -$result = get_db_all_rows_sql ($sql); -foreach ($result as $agent) { - $data = array (); - - $sql = sprintf("SELECT COUNT(`id_agente_modulo`) - FROM `tagente_modulo` WHERE `id_agente` = '%d'", - $agent["id_agente"]); - $assigned = get_db_sql ($sql); - - // for all data_modules belongs to an agent -- simplified, made - // faster - $sql = sprintf ("SELECT COUNT(`id_agente_datos`) - FROM `tagente_datos` WHERE `id_agente` = '%d'", - $agent["id_agente"]); - $total_agente = get_db_sql ($sql); - - $data[0] = ''.$agent["nombre"].''; - $data[1] = $assigned; - $data[2] = $total_agente; - - array_push ($table->data, $data); -} -print_table ($table); -?> diff --git a/pandora_console/godmode/db/db_main.php b/pandora_console/godmode/db/db_main.php index c623bc3355..2da0d8789b 100644 --- a/pandora_console/godmode/db/db_main.php +++ b/pandora_console/godmode/db/db_main.php @@ -36,16 +36,18 @@ if (! give_acl ($config['id_user'], 0, "DM")) { - A more powerful selection (by Agent, by Module, etc). */ -?> -

> -

- + +echo '

'.__('Database Maintenance').' > '.__('Current database maintenance setup').'

+
-: 

-: 

+'.__('Max. time before compact data').': '.$config['days_compact'].' '.__('days').'

+'.__('Max. time before purge').': '.$config['days_purge'].' '.__('days').'

+
-
- -

+
+'.__('Please check your Pandora Server setup and be sure that database maintenance daemon is running. It\'s very important to keep up-to-date database to get the best performance and results in Pandora').' +

-
+ +'; +?> diff --git a/pandora_console/godmode/db/db_purge.php b/pandora_console/godmode/db/db_purge.php index eabe35be7d..f8ccf44f9e 100644 --- a/pandora_console/godmode/db/db_purge.php +++ b/pandora_console/godmode/db/db_purge.php @@ -24,194 +24,190 @@ check_login (); if (! give_acl ($config['id_user'], 0, "DM")) { audit_db($config['id_user'], $REMOTE_ADDR, "ACL Violation", - "Trying to access to Database Purge Section"); + "Trying to access Database Purge Section"); include ("general/noaccess.php"); - return; + exit; +} + +//id_agent = -1: None selected; id_agent = 0: All +if (isset ($_POST["agent"])){ + $id_agent = (int) get_parameter_post ("agent",-1); //Default to none selected +} else { + $id_agent = -1; } -if (isset ($_POST["agent"])){ - $id_agent = get_parameter_post ("agent"); -} else - $id_agent = -1; -echo '

'.__('Database Maintenance').' >'.__('Database purge')."

"; -echo ""; -echo "

"; -echo '

'.__('Get data from agent').'

'; +echo '

'.__('Database Maintenance').' >'.__('Database purge').'

+ +

+

'.__('Get data from agent').'

'; // All data (now) -$purge_all = date ("Y-m-d H:i:s", time ()); - -require("godmode/db/times_incl.php"); +$time["all"] = time (); -$datos_rango3=0; -$datos_rango2=0; -$datos_rango1=0; -$datos_rango0=0; -$datos_rango00=0; -$datos_rango11=0; -$datos_total=0; +// 1 day ago +$time["1day"] = $time["all"]-86400; + +// 3 days ago +$time["3day"] = $time["all"]-(86400*3); + +// 1 week ago +$time["1week"] = $time["all"]-(86400*7); + +// 2 weeks ago +$time["2week"] = $time["all"]-(86400*14); + +// 1 month ago +$time["1month"] = $time["all"]-(86400*30); + +// Three months ago +$time["3month"] = $time["all"]-(86400*90); + +//Init data +$data["1day"] = 0; +$data["3day"] = 0; +$data["1week"] = 0; +$data["2week"] = 0; +$data["1month"] = 0; +$data["3month"] = 0; +$data["total"] = 0; # ADQUIRE DATA PASSED AS FORM PARAMETERS # ====================================== -# Purge data using dates - # Purge data using dates if (isset($_POST["purgedb"])) { - $from_date = get_parameter_post ("date_purge"); - if (isset($id_agent)){ - if ($id_agent != -1) { - echo __('Purge task launched for agent id ').$id_agent." / ".$from_date; - echo "

".__('Please be patient. This operation can be very long in time (5-10 minutes)')."
",__('while deleting data for ').__('Agent')."

"; - if ($id_agent == 0) { - $sql="SELECT * FROM tagente_modulo"; - } else { - $sql=sprintf("SELECT * FROM tagente_modulo WHERE id_agente = %d",$id_agent); - } - $result=get_db_all_rows_sql($sql); - foreach ($result as $row) { - echo __('Deleting records for module ').dame_nombre_modulo_agentemodulo($row["id_agente_modulo"]); - flush(); - //ob_flush(); - echo "
"; - $sql = sprintf("DELETE FROM `tagente_datos` WHERE `id_agente_modulo` = '%d' AND `timestamp` < '%s'",$row["id_agente_modulo"],$from_date); - process_sql ($sql); - $sql = sprintf("DELETE FROM `tagente_datos_inc` WHERE `id_agente_modulo` = '%d' AND `timestamp` < '%s'",$row["id_agente_modulo"],$from_date); - process_sql ($sql); - $sql = sprintf("DELETE FROM `tagente_datos_string` WHERE `id_agente_modulo` = '%d' AND `timestamp` < '%s'",$row["id_agente_modulo"],$from_date); - process_sql ($sql); - } - } else { - echo __('Deleting records for module ').__('All agents'); - flush(); - //ob_flush(); - $query = sprintf("DELETE FROM `tagente_datos` WHERE `timestamp` < '%s'",$from_date); - process_sql ($query); - $query = sprintf("DELETE FROM `tagente_datos_inc` WHERE `timestamp` < '%s'",$from_date); - process_sql ($query); - $query = sprintf("DELETE FROM `tagente_datos_string` WHERE `timestamp` < '%s'",$from_date); - process_sql ($query); + $from_date = get_parameter_post ("date_purge",0); //0: No time selected + if ($id_agent != -1) { + echo __('Purge task launched for agent')." ".dame_nombre_agente ($id_agent)." :: ".__('Data older than')." ".human_time_description ($from_date); + echo "

".__('Please be patient. This operation can take a long time depending on the amount of modules.')."

"; + + $sql = sprintf ("SELECT id_agente_modulo FROM tagente_modulo WHERE id_agente = %d",$id_agent); + $result=get_db_all_rows_sql($sql); + + //Made it in a transaction so it gets done all at once. + process_sql ("SET AUTOCOMMIT=0;"); + process_sql ("START TRANSACTION;"); //We start a transaction for consistency + $errors = 0; + foreach ($result as $row) { + echo __('Deleting records for module')." ".dame_nombre_modulo_agentemodulo ($row["id_agente_modulo"]); + echo "
"; + flush (); //Flush here in case there are errors and the script dies, at least we know where we ended + set_time_limit (); //Reset the time limit just in case + $sql = sprintf("DELETE FROM `tagente_datos` WHERE `id_agente_modulo` = '%d' AND `utimestamp` < '%d'",$row["id_agente_modulo"],$from_date); + if (process_sql ($sql) === false) + $errors++; + $sql = sprintf("DELETE FROM `tagente_datos_inc` WHERE `id_agente_modulo` = '%d' AND `utimestamp` < '%d'",$row["id_agente_modulo"],$from_date); + if (process_sql ($sql) === false) + $errors++; + $sql = sprintf("DELETE FROM `tagente_datos_string` WHERE `id_agente_modulo` = '%d' AND `utimestamp` < '%d'",$row["id_agente_modulo"],$from_date); + if (process_sql ($sql) === false) + $errors++; } - echo "

"; + + if ($errors > 0) { + process_sql ("ROLLBACK;"); //If we have errors, rollback + } else { + process_sql ("COMMIT;"); //Otherwise commit + } + + process_sql ("SET AUTOCOMMIT=1;"); //Set autocommit back to 1 + } else { + //All agents + echo __('Deleting records for all agents'); + flush(); + //ob_flush(); + $query = sprintf("DELETE FROM `tagente_datos` WHERE `utimestamp` < '%d'",$from_date); + process_sql ($query); + $query = sprintf("DELETE FROM `tagente_datos_inc` WHERE `utimestamp` < '%d'",$from_date); + process_sql ($query); + $query = sprintf("DELETE FROM `tagente_datos_string` WHERE `utimestamp` < '%d'",$from_date); + process_sql ($query); } + echo "

"; } # Select Agent for further operations. -?> -
- -
- +
'; - 0)) - echo "'; +print_help_tip (__("Click here to get the data from the agent specified in the select box")); +echo '

'; + +if ($id_agent > 0) { + $title = __('Information on agent').' '.dame_nombre_agente ($id_agent).' '.__('in the database'); } else { - echo "
'> - -

- -".__('Data from agent ').dame_nombre_agente ($id_agent).__(' in the Database').""; +$data["1day"] = get_db_sql (sprintf ("SELECT COUNT(id_agente_datos) FROM tagente_datos WHERE utimestamp > %d %s", $time["1day"], $query)); +$data["3day"] = get_db_sql (sprintf ("SELECT COUNT(id_agente_datos) FROM tagente_datos WHERE utimestamp > %d %s", $time["3day"], $query)); +$data["1week"] = get_db_sql (sprintf ("SELECT COUNT(id_agente_datos) FROM tagente_datos WHERE utimestamp > %d %s", $time["1week"], $query)); +$data["2week"] = get_db_sql (sprintf ("SELECT COUNT(id_agente_datos) FROM tagente_datos WHERE utimestamp > %d %s", $time["2week"], $query)); +$data["1month"] = get_db_sql (sprintf ("SELECT COUNT(id_agente_datos) FROM tagente_datos WHERE utimestamp > %d %s", $time["1month"], $query)); +$data["3month"] = get_db_sql (sprintf ("SELECT COUNT(id_agente_datos) FROM tagente_datos WHERE utimestamp > %d %s", $time["3month"], $query)); +$data["total"] = get_db_sql (sprintf ("SELECT COUNT(id_agente_datos) FROM tagente_datos WHERE 1=1 %s", $query)); - $sql = "SELECT id_agente_modulo FROM tagente_modulo"; - if ($id_agent != 0) { - $sql .= sprintf(" WHERE id_agente = '%d'",$id_agent); - } - - $datos_rango00 += get_db_sql (sprintf("SELECT COUNT(*) FROM `tagente_datos` WHERE `id_agente_modulo` = ANY(%s) AND `timestamp` > '%s'",$sql,$d1)); - $datos_rango0 += get_db_sql (sprintf("SELECT COUNT(*) FROM `tagente_datos` WHERE `id_agente_modulo` = ANY(%s) AND `timestamp` > '%s'",$sql,$d3)); - $datos_rango1 += get_db_sql (sprintf("SELECT COUNT(*) FROM `tagente_datos` WHERE `id_agente_modulo` = ANY(%s) AND `timestamp` > '%s'",$sql,$week)); - $datos_rango11 += get_db_sql (sprintf("SELECT COUNT(*) FROM `tagente_datos` WHERE `id_agente_modulo` = ANY(%s) AND `timestamp` > '%s'",$sql,$week2)); - $datos_rango2 += get_db_sql (sprintf("SELECT COUNT(*) FROM `tagente_datos` WHERE `id_agente_modulo` = ANY(%s) AND `timestamp` > '%s'",$sql,$month)); - $datos_rango3 += get_db_sql (sprintf("SELECT COUNT(*) FROM `tagente_datos` WHERE `id_agente_modulo` = ANY(%s) AND `timestamp` > '%s'",$sql,$month3)); - $datos_total += get_db_sql (sprintf("SELECT COUNT(*) FROM `tagente_datos` WHERE `id_agente_modulo` = ANY(%s)",$sql)); - +if (isset ($table)) { + unset ($table); //since $table is an object, we make sure it's gone first } -?> - - - +$table->width = 300; +$table->border = 0; +$table->class = "databox"; +$table->cellspacing = 4; +$table->cellpadding = 4; - - +$table->data[0] = array (__('Packets less than three months old'), $data["3month"]); +$table->data[1] = array (__('Packets less than one month old'), $data["1month"]); +$table->data[2] = array (__('Packets less than two weeks old'), $data["2week"]); +$table->data[3] = array (__('Packets less than one week old'), $data["1week"]); +$table->data[4] = array (__('Packets less than three days old'), $data["3day"]); +$table->data[5] = array (__('Packets less than one day old'), $data["1day"]); +$table->data[6] = array (''.__('Total number of packets').'', ''.$data["total"].''); - - +print_table ($table); - - - - - - - - - - - -
- - - -
- - - -
- - - -
- - - -
- - - -
- - - -
- - - -
-
-

- +echo '
'; +echo '

'.__('Purge data').'

+
- - + + +
-
+'; +?> diff --git a/pandora_console/godmode/db/times_incl.php b/pandora_console/godmode/db/times_incl.php deleted file mode 100644 index 58056d9b1f..0000000000 --- a/pandora_console/godmode/db/times_incl.php +++ /dev/null @@ -1,38 +0,0 @@ - diff --git a/pandora_console/reporting/fgraph.php b/pandora_console/reporting/fgraph.php index f90fe89ff6..30b36b2b03 100644 --- a/pandora_console/reporting/fgraph.php +++ b/pandora_console/reporting/fgraph.php @@ -22,6 +22,8 @@ require_once ($config["homedir"].'/include/functions.php'); require_once ($config["homedir"].'/include/functions_db.php'); require_once ('Image/Graph.php'); +ini_set ('display_errors', 0); //This is to prevent notices from making the thing not graph + global $config; if (!isset($_SESSION["id_user"])){ @@ -54,13 +56,7 @@ function graphic_error () { */ function dame_fecha ($mh) { $mh *= 60; - $m_year = date ("Y", time() - $mh); - $m_month = date ("m", time() - $mh); - $m_day = date ("d", time() - $mh); - $m_hour = date ("H", time() - $mh); - $m_min = date ("i", time() - $mh); - $m = $m_year."-".$m_month."-".$m_day." ".$m_hour.":".$m_min.":00"; - return $m; + return date ("Y-m-d H:i:00", time() - $mh); } /** @@ -1199,28 +1195,21 @@ function graphic_incident_source ($width=320, $height=200) { function grafico_db_agentes_modulos($width, $height) { $data = array(); $legend = array(); - $sql1="SELECT * FROM tagente"; - $result=mysql_query($sql1); - while ($row=mysql_fetch_array($result)){ - $sql1="SELECT COUNT(id_agente_modulo) FROM tagente_modulo WHERE id_agente = ".$row["id_agente"];; - $result2=mysql_query($sql1); - $row2=mysql_fetch_array($result2); - $data[] = $row2[0]; - $legend[] = $row["nombre"]; - } - // Sort array by bubble method (yes, I study more methods in university, but if you want more speed, please, submit a patch :) - // or much better, pay me to do a special version for you, highly optimized :-)))) - for ($i = 0; $i < sizeof($data); $i++) { - for ($j = $i; $j $data[$i]) { - $temp = $data[$i]; - $temp_label = $legend[$i]; - $data[$i] = $data[$j]; - $legend[$i] = $legend[$j]; - $data[$j] = $temp; - $legend[$j] = $temp_label; - } + + $sql = "SELECT DISTINCT(id_agente), COUNT(id_agente_datos) AS count FROM tagente_datos GROUP BY id_agente ORDER BY count ASC"; + //This query is not the most efficient on itself. But other functions + //use this query so it will a) be fetched from cache and b) it will + //return the result in the same order as the other graph and data + + $result = get_db_all_rows_sql ($sql); + if ($result === false) + $result = array(); + + foreach ($result as $row) { + $data[] = count (get_modules_in_agent ($row["id_agente"])); + $legend[] = dame_nombre_agente ($row["id_agente"]); } + generic_bar_graph ($width, $height, $data, $legend); } @@ -1325,7 +1314,7 @@ function graph_event_module ($width = 300, $height = 200, $id_agent) { $data = array(); $legend = array(); - $sql = sprintf ("SELECT DISTINCT(id_agentmodule) AS id_agentmodule, id_grupo, COUNT(id_agentmodule) AS count FROM tevento WHERE id_agente = %d GROUP BY id_agentmodule",$id_agent); + $sql = sprintf ("SELECT DISTINCT(id_agentmodule) AS id_agentmodule, id_grupo, COUNT(id_agentmodule) AS count FROM tevento WHERE id_agente = %d GROUP BY id_agentmodule ORDER BY count DESC",$id_agent); $result = get_db_all_rows_sql ($sql); if ($result === false) $result = array(); @@ -1343,8 +1332,6 @@ function graph_event_module ($width = 300, $height = 200, $id_agent) { } } - array_multisort ($legend, $data); - $max_items = 6; //Maximum items on the piegraph while (count($data) > $max_items) { //Pops an element off the array until the array is small enough @@ -1437,77 +1424,60 @@ function generic_bar_graph ( $width =380, $height = 200, $data, $legend) { function grafico_db_agentes_paquetes ($width = 380, $height = 300) { $data = array(); $legend = array(); - $sql1="SELECT distinct (id_agente) FROM tagente_datos"; - $result=mysql_query($sql1); - while ($row=mysql_fetch_array($result)){ - if (! is_null($row["id_agente"])){ - $sql1="SELECT COUNT(id_agente) FROM tagente_datos WHERE id_agente = ".$row["id_agente"]; - $result3=mysql_query($sql1); - if ($row3=mysql_fetch_array($result3)){ - $agent_name = dame_nombre_agente($row[0]); - if ($agent_name != ""){ - $data[]= $row3[0]; - $legend[] = str_pad($agent_name,15); - } - } - } - } - // Sort array by bubble method (yes, I study more methods in university, but if you want more speed, please, submit a patch :) - // or much better, pay me to do a special version for you, highly optimized :-)))) - for ($i=0;$i < sizeof($data);$i++){ - for ($j=$i; $j $data[$i]){ - $temp = $data[$i]; - $temp_label = $legend[$i]; - $data[$i] = $data[$j]; - $legend[$i] = $legend[$j]; - $data[$j] = $temp; - $legend[$j] = $temp_label; - } - } + + $sql = "SELECT DISTINCT(id_agente), COUNT(id_agente_datos) AS count FROM tagente_datos GROUP BY id_agente ORDER BY count ASC"; + $result = get_db_all_rows_sql ($sql); + + if ($result === false) + $result = array(); + + foreach ($result as $row) { + $data[] = $row["count"]; + $legend[] = dame_nombre_agente ($row["id_agente"]); + } + generic_bar_graph ($width, $height, $data, $legend); } function grafico_db_agentes_purge ($id_agent, $width, $height) { - if ($id_agent == 0) + //If id_agent is -1 or 0 (depending on what passed it), it should be + //All. Positive id is a possible agent + if ($id_agent < 1) { $id_agent = -1; - // All data (now) - $purge_all=date("Y-m-d H:i:s",time()); - - $data = array(); - $legend = array(); - - $d90 = time()-(2592000*3); - $d30 = time()-2592000; - $d7 = time()-604800; - $d1 = time()-86400; - $fechas = array($d90, $d30, $d7, $d1); - $fechas_label = array("30-90 days","7-30 days","This week","Today"); - - // Calc. total packets - $sql1 = "SELECT COUNT(id_agente_datos) FROM tagente_datos"; - $result2 = mysql_query ($sql1); - $row2 = mysql_fetch_array ($result2); - $total = $row2[0]; - - for ($i = 0; $i < sizeof ($fechas); $i++){ - // 4 x intervals will be enought, increase if your database is very very fast :) - if ($i == 3) { - if ($id_agent == -1) - $sql1="SELECT COUNT(id_agente_datos) FROM tagente_datos WHERE utimestamp >= ".$fechas[$i]; - else - $sql1="SELECT COUNT(id_agente_datos) FROM tagente_datos WHERE id_agente = $id_agent AND utimestamp >= ".$fechas[$i]; - } else { - if ($id_agent == -1) - $sql1="SELECT COUNT(id_agente_datos) FROM tagente_datos WHERE utimestamp >= ".$fechas[$i]." AND utimestamp < ".$fechas[$i+1]; - else - $sql1="SELECT COUNT(id_agente_datos) FROM tagente_datos WHERE id_agente = $id_agent AND utimestamp >= ".$fechas[$i]." AND utimestamp < ".$fechas[$i+1]; - } - $result=mysql_query($sql1); - $row=mysql_fetch_array($result); - $data[] = $row[0]; - $legend[]=$fechas_label[$i]." ( ".format_for_graph($row[0],0)." )"; + $query = ""; + } else { + $query = sprintf (" AND id_agente_modulo = ANY(SELECT id_agente_modulo FROM tagente_modulo WHERE id_agente = '%d' ",$id_agent); } + + // All data (now) + $time["all"] = time (); + + // 1 day ago + $time["1day"] = $time["all"]-86400; + + // 1 week ago + $time["1week"] = $time["all"]-(86400*7); + + // 1 month ago + $time["1month"] = $time["all"]-(86400*30); + + // Three months ago + $time["3month"] = $time["all"]-(86400*90); + + //Init legend + $legend[0] = __("Today"); + $legend[1] = "1 ".__("Week"); + $legend[2] = "1 ".__("Month"); + $legend[3] = "3 ".__("Months"); + $legend[4] = __("Older"); + + $data[0] = get_db_sql (sprintf ("SELECT COUNT(id_agente_datos) FROM tagente_datos WHERE utimestamp > %d %s", $time["1day"], $query)); + $data[1] = get_db_sql (sprintf ("SELECT COUNT(id_agente_datos) FROM tagente_datos WHERE utimestamp > %d %s", $time["1week"], $query)); + $data[2] = get_db_sql (sprintf ("SELECT COUNT(id_agente_datos) FROM tagente_datos WHERE utimestamp > %d %s", $time["1month"], $query)); + $data[3] = get_db_sql (sprintf ("SELECT COUNT(id_agente_datos) FROM tagente_datos WHERE utimestamp > %d %s", $time["3month"], $query)); + $data[4] = get_db_sql (sprintf ("SELECT COUNT(id_agente_datos) FROM tagente_datos WHERE 1=1 %s", $query)); + $data[4] = $data[4] - $data[3]; + generic_pie_graph ($width, $height, $data, $legend); }