From 4d7144c0d65e54eaffdfa2f59a3cbcc5baa2d388 Mon Sep 17 00:00:00 2001
From: mdtrooper <tres.14159@gmail.com>
Date: Tue, 16 Sep 2014 13:47:41 +0200
Subject: [PATCH] Fixed the server performance stadistics. TIKET #1272

---
 .../include/functions_reporting.php           | 16 ++--
 pandora_console/include/functions_servers.php | 89 +++++++++++++------
 2 files changed, 72 insertions(+), 33 deletions(-)

diff --git a/pandora_console/include/functions_reporting.php b/pandora_console/include/functions_reporting.php
index fec59121cb..8452bf526b 100644
--- a/pandora_console/include/functions_reporting.php
+++ b/pandora_console/include/functions_reporting.php
@@ -962,7 +962,7 @@ function reporting_get_stats_servers($tiny = true) {
 			$tdata[1] = '<span class="big_data">' . format_numeric($server_performance ["total_network_modules"]) . '</span>';
 			
 			$tdata[2] = '<span class="med_data">' .
-				format_numeric($server_performance ["network_modules_rate"], 2) .
+				format_numeric($server_performance["network_modules_rate"], 2) .
 				'</span>';
 			$tdata[3] = html_print_image('images/module.png', true, array('title' => __('Ratio') . ': ' . __('Modules by second'), 'width' => '16px')) . '/sec </span>';
 			
@@ -1009,9 +1009,13 @@ function reporting_get_stats_servers($tiny = true) {
 		if (isset($server_performance ["total_web_modules"])) {
 			$tdata = array();
 			$tdata[0] = html_print_image('images/world.png', true, array('title' => __('Web modules'), 'width' => '25px'));
-			$tdata[1] = '<span class="big_data">' . format_numeric($server_performance ["total_web_modules"]) . '</span>';
+			$tdata[1] = '<span class="big_data">' .
+				format_numeric($server_performance ["total_web_modules"]) .
+				'</span>';
 			
-			$tdata[2] = '<span class="med_data">' . format_numeric($server_performance ["web_modules_rate"], 2) . '</span>';
+			$tdata[2] = '<span class="med_data">' .
+				format_numeric($server_performance ["web_modules_rate"], 2) .
+				'</span>';
 			$tdata[3] = html_print_image('images/module.png', true, array('title' => __('Ratio') . ': ' . __('Modules by second'), 'width' => '16px')) . '/sec </span>';
 			
 			$table_srv->rowclass[] = '';
@@ -1042,8 +1046,10 @@ function reporting_get_stats_servers($tiny = true) {
 		
 		
 		$tdata = array();
-		$tdata[0] = html_print_image('images/lightning_go.png', true, array('title' => __('Total events'), 'width' => '25px'));
-		$tdata[1] = '<span class="big_data">' . format_numeric($system_events) . '</span>';
+		$tdata[0] = html_print_image('images/lightning_go.png', true,
+			array('title' => __('Total events'), 'width' => '25px'));
+		$tdata[1] = '<span class="big_data">' .
+			format_numeric($system_events) . '</span>';
 		
 		$table_srv->colspan[count($table_srv->data)][1] = 3;
 		$table_srv->rowclass[] = '';
diff --git a/pandora_console/include/functions_servers.php b/pandora_console/include/functions_servers.php
index 77ade97139..7d67a0bf86 100644
--- a/pandora_console/include/functions_servers.php
+++ b/pandora_console/include/functions_servers.php
@@ -81,13 +81,21 @@ function servers_get_performance () {
 	$data["local_modules_rate"] = 0;
 	$data["network_modules_rate"] = 0;
 	
-	
 	if ($config["realtimestats"] == 1) {
-		$counts = db_get_all_rows_sql ("SELECT tagente_modulo.id_modulo, COUNT(tagente_modulo.id_agente_modulo) modules
+		
+		$counts = db_get_all_rows_sql ("
+			SELECT tagente_modulo.id_modulo,
+				COUNT(tagente_modulo.id_agente_modulo) modules
 			FROM tagente_modulo, tagente_estado, tagente
 			WHERE tagente_modulo.id_agente_modulo = tagente_estado.id_agente_modulo
-				AND tagente_modulo.disabled = 0 AND delete_pending = 0 AND (utimestamp > 0 OR (id_tipo_modulo = 100 OR (id_tipo_modulo > 21 AND id_tipo_modulo < 23)))
-				AND tagente.disabled = 0 AND tagente.id_agente = tagente_estado.id_agente GROUP BY tagente_modulo.id_modulo");
+				AND tagente.id_agente = tagente_estado.id_agente
+				
+				AND tagente_modulo.disabled = 0
+				AND delete_pending = 0
+				AND (utimestamp > 0 OR (id_tipo_modulo = 100 OR (id_tipo_modulo > 21 AND id_tipo_modulo < 23)))
+				AND tagente.disabled = 0
+				
+			GROUP BY tagente_modulo.id_modulo");
 		
 		if (empty($counts)) {
 			$counts = array();
@@ -123,13 +131,16 @@ function servers_get_performance () {
 		}
 	}
 	else {
-		$counts = db_get_all_rows_sql ("SELECT server_type, my_modules modules FROM tserver GROUP BY server_type");
+		$counts = db_get_all_rows_sql ("
+			SELECT server_type, my_modules modules
+			FROM tserver
+			GROUP BY server_type");
 		
 		if (empty($counts)) {
 			$counts = array();
 		}
 		
-		foreach($counts as $c) {
+		foreach ($counts as $c) {
 			switch ($c['server_type']) {
 				case SERVER_TYPE_DATA:
 					$data["total_local_modules"] = $c['modules'];
@@ -159,7 +170,7 @@ function servers_get_performance () {
 					break;
 			}
 			
-			if($c['server_type'] != SERVER_TYPE_DATA) {
+			if ($c['server_type'] != SERVER_TYPE_DATA) {
 				$data["total_remote_modules"] += $c['modules'];
 			}
 			
@@ -167,13 +178,22 @@ function servers_get_performance () {
 		}
 	}
 	
+	$interval_avgs = array();
+	
 	// Avg of modules interval when modules have module_interval > 0
-	$interval_avgs_modules = db_get_all_rows_sql ("SELECT count(tagente_modulo.id_modulo) modules , tagente_modulo.id_modulo, AVG(tagente_modulo.module_interval) avg_interval
-					FROM tagente_modulo, tagente_estado, tagente
-				WHERE tagente_modulo.id_agente_modulo = tagente_estado.id_agente_modulo
-					AND tagente_modulo.disabled = 0 AND module_interval > 0 AND (utimestamp > 0 OR (id_tipo_modulo = 100 OR (id_tipo_modulo > 21 AND id_tipo_modulo < 23)))
-					AND delete_pending = 0
-					AND tagente.disabled = 0 AND tagente.id_agente = tagente_estado.id_agente GROUP BY tagente_modulo.id_modulo");
+	$interval_avgs_modules = db_get_all_rows_sql ("
+		SELECT count(tagente_modulo.id_modulo) modules ,
+			tagente_modulo.id_modulo,
+			AVG(tagente_modulo.module_interval) avg_interval
+		FROM tagente_modulo, tagente_estado, tagente
+		WHERE tagente_modulo.id_agente_modulo = tagente_estado.id_agente_modulo
+			AND tagente_modulo.disabled = 0
+			AND module_interval > 0
+			AND (utimestamp > 0 OR (id_tipo_modulo = 100 OR (id_tipo_modulo > 21 AND id_tipo_modulo < 23)))
+			AND delete_pending = 0
+			AND tagente.disabled = 0
+			AND tagente.id_agente = tagente_estado.id_agente
+		GROUP BY tagente_modulo.id_modulo");
 	
 	if (empty($interval_avgs_modules)) {
 		$interval_avgs_modules = array();
@@ -186,22 +206,26 @@ function servers_get_performance () {
 	}
 	
 	// Avg of agents interval when modules have module_interval == 0
-	$interval_avgs_agents = db_get_all_rows_sql ("SELECT count(tagente_modulo.id_modulo) modules , tagente_modulo.id_modulo, AVG(tagente.intervalo) avg_interval
-					FROM tagente_modulo, tagente_estado, tagente
-				WHERE tagente_modulo.id_agente_modulo = tagente_estado.id_agente_modulo
-					AND tagente_modulo.disabled = 0 AND module_interval = 0 AND (utimestamp > 0 OR (id_tipo_modulo = 100 OR (id_tipo_modulo >= 21 AND id_tipo_modulo <= 23)))
-					AND delete_pending = 0
-					AND tagente.disabled = 0 AND tagente.id_agente = tagente_estado.id_agente GROUP BY tagente_modulo.id_modulo");
+	$interval_avgs_agents = db_get_all_rows_sql ("
+		SELECT count(tagente_modulo.id_modulo) modules ,
+			tagente_modulo.id_modulo, AVG(tagente.intervalo) avg_interval
+		FROM tagente_modulo, tagente_estado, tagente
+		WHERE tagente_modulo.id_agente_modulo = tagente_estado.id_agente_modulo
+			AND tagente_modulo.disabled = 0
+			AND module_interval = 0
+			AND (utimestamp > 0 OR (id_tipo_modulo = 100 OR (id_tipo_modulo >= 21 AND id_tipo_modulo <= 23)))
+			AND delete_pending = 0
+			AND tagente.disabled = 0
+			AND tagente.id_agente = tagente_estado.id_agente
+		GROUP BY tagente_modulo.id_modulo");
 	
 	if (empty($interval_avgs_agents)) {
 		$interval_avgs_agents = array();
 	}
 	
-	$interval_avgs = array();
-	
 	// Merge with the previous calculated array
-	foreach($interval_avgs_agents as $iaagents) {
-		if(!isset($interval_avgs[$iaagents['id_modulo']]['modules'])) {
+	foreach ($interval_avgs_agents as $iaagents) {
+		if (!isset($interval_avgs[$iaagents['id_modulo']]['modules'])) {
 			$interval_avgs[$iaagents['id_modulo']]['avg_interval'] = $iaagents['avg_interval'];		
 			$interval_avgs[$iaagents['id_modulo']]['modules'] = $iaagents['modules'];
 		}
@@ -215,11 +239,14 @@ function servers_get_performance () {
 		switch($id_modulo) {
 			case MODULE_DATA:
 				$data["avg_interval_local_modules"] = $ia['avg_interval'];
-				$data["local_modules_rate"] = servers_get_rate($data["avg_interval_local_modules"], $data["total_local_modules"]);
+				$data["local_modules_rate"] =
+					servers_get_rate($data["avg_interval_local_modules"], $data["total_local_modules"]);
 				break;
 			case MODULE_NETWORK:
 				$data["avg_interval_network_modules"] = $ia['avg_interval'];
-				$data["network_modules_rate"] = servers_get_rate($data["avg_interval_network_modules"], $data["total_network_modules"]);
+				$data["network_modules_rate"] =
+					servers_get_rate($data["avg_interval_network_modules"],
+						$data["total_network_modules"]);
 				break;
 			case MODULE_PLUGIN:
 				$data["avg_interval_plugin_modules"] = $ia['avg_interval'];
@@ -262,7 +289,7 @@ function servers_get_performance () {
 	
 	$data["remote_modules_rate"] = servers_get_rate($data["avg_interval_remote_modules"], $data["total_remote_modules"]);
 	$data["total_modules_rate"] = servers_get_rate($data["avg_interval_total_modules"], $data["total_modules"]);
-
+	
 	return ($data);
 }
 
@@ -292,7 +319,10 @@ function servers_get_avg_interval($modules_avg_interval1, $modules_avg_interval2
  * @return float number of modules processed by second
  */
 function servers_get_rate($avg_interval, $num_modules) {
-	return $avg_interval > 0 ? ($num_modules / $avg_interval) : 0;
+	
+	return $avg_interval > 0 ?
+		($num_modules / $avg_interval) :
+		0;
 }
 
 /**
@@ -315,7 +345,10 @@ function servers_get_info ($id_server = -1) {
 		$select_id = "";
 	}
 	
-	$sql = "SELECT * FROM tserver".$select_id . " ORDER BY server_type";
+	$sql = "
+		SELECT *
+		FROM tserver " . $select_id . "
+		ORDER BY server_type";
 	$result = db_get_all_rows_sql ($sql);
 	$time = get_system_time ();