From 1160fd800c887abfb568c656de9526bfbf856843 Mon Sep 17 00:00:00 2001
From: mdtrooper <tres.14159@gmail.com>
Date: Tue, 26 May 2015 17:20:34 +0200
Subject: [PATCH] Fixed the 'availability' item in report. (Merge from
 5d25b7c8e6ddf41b33b220e00d63a26a101908ab)

---
 pandora_console/include/functions.php         | 24 ++-----
 pandora_console/include/functions_modules.php | 72 ++++++++-----------
 .../include/functions_reporting.php           | 11 ++-
 3 files changed, 41 insertions(+), 66 deletions(-)

diff --git a/pandora_console/include/functions.php b/pandora_console/include/functions.php
index c3c234e185..097ecc67a1 100644
--- a/pandora_console/include/functions.php
+++ b/pandora_console/include/functions.php
@@ -464,29 +464,19 @@ function human_time_description_raw ($seconds, $exactly = false, $units = 'large
 			$returnTime .= "$hours $hoursString ";
 		}
 		
-		$mins = floor($seconds / SECONDS_1MINUTE);
+		$mins = floor($seconds / 60);
 		
 		if ($mins != 0) {
-			$seconds = $seconds - ($mins * SECONDS_1MINUTE);
+			$seconds = $seconds - ($mins * 60);
+			
+			$returnTime .= "$mins $minutesString ";
 			
-			if ($hours == 0) {
-				$returnTime .= "$mins $minutesString ";
-			}
-			else {
-				$returnTime = sprintf("%02d",$hours) . ':' .
-					sprintf("%02d",$mins);
-			}
 		}
 		
+		$seconds = (int) $seconds;
+		
 		if ($seconds != 0) {
-			if ($hours == 0) {
-				$returnTime .= "$seconds $secondsString ";
-			}
-			else {
-				$returnTime = sprintf("%02d",$hours) . ':' .
-					sprintf("%02d",$mins) . ':' .
-					sprintf("%02d",$seconds);
-			}
+			$returnTime .= "$seconds $secondsString ";
 		}
 		
 		$return = ' ';
diff --git a/pandora_console/include/functions_modules.php b/pandora_console/include/functions_modules.php
index ed6f1ee4ce..2700eb46e2 100755
--- a/pandora_console/include/functions_modules.php
+++ b/pandora_console/include/functions_modules.php
@@ -2141,14 +2141,8 @@ function modules_get_count_datas($id_agent_module, $date_init, $date_end) {
 	return ($diff / $interval);
 }
 
-function modules_get_data_with_value($id_agent_module, $date_init,
-	$date_end, $value, $split_interval = false) {
-	
-	global $config;
-	
-	// TODO REMOVE THE TIME IN PLANNED DOWNTIME
-	
-	// TODO FOR OTHER KIND OF DATA
+function modules_get_count_data_with_value($id_agent_module, $date_init,
+	$date_end, $value) {
 	
 	if (!is_numeric($date_init)) {
 		$date_init = strtotime($date_init);
@@ -2158,58 +2152,50 @@ function modules_get_data_with_value($id_agent_module, $date_init,
 		$date_end = strtotime($date_end);
 	}
 	
-	$sql = "
-		SELECT *
+	$sql = "SELECT *
 		FROM tagente_datos
 		WHERE
-			datos = " . (int)$value . "
-			AND id_agente_modulo = " . (int)$id_agent_module . "
+			id_agente_modulo = " . (int)$id_agent_module . "
 			AND (utimestamp >= " . $date_init . " AND utimestamp <= " . $date_end . ")";
 	
-	$data = db_get_all_rows_sql($sql,
-		$config['history_db_enabled']);
+	$data = db_get_all_rows_sql($sql, $config['history_db_enabled']);
 	
 	if (empty($data)) {
 		$data = array();
 	}
 	
-	if ($split_interval) {
-		$temp = array();
-		$previous_utimestamp = false;
-		foreach ($data as $row) {
-			if ($previous_utimestamp === false) {
-				$previous_utimestamp = $row['utimestamp'];
-				
-				$temp[] = $row;
+	$interval = modules_get_interval($id_agent_module);
+	
+	$total_time_with_value = 0;
+	$on_value_detected = false;
+	$timestamp_with_value = null;
+	
+	foreach ($data as $row) {
+		if ($row['datos'] == $value) {
+			if (!$on_value_detected) {
+				$timestamp_with_value = $row['utimestamp'];
+				$on_value_detected = true;
 			}
-			else {
-				$diff = $row['utimestamp'] - $previous_utimestamp;
+		}
+		else {
+			if ($on_value_detected) {
+				$total_time_with_value 
+					+= $row['utimestamp'] - $timestamp_with_value;
 				
-				$interval = modules_get_interval($id_agent_module);
-				
-				if ($diff > $interval) {
-					$fake_count = (int)($diff / $interval);
-					
-					$fake = $row;
-					for ($iterator = 1; $iterator <= $fake_count; $iterator++) {
-						$fake['utimestamp'] = $previous_utimestamp + ($iterator * $interval);
-						$temp[] = $fake;
-					}
-				}
-				else {
-					$temp[] = $row;
-				}
-				
-				$previous_utimestamp = $row['utimestamp'];
-				
-				$data = $temp;
+				$on_value_detected = false;
 			}
 		}
 	}
 	
-	return $data;
+	if ($on_value_detected && !empty($data)) {
+		$total_time_with_value 
+			+= $date_end - $timestamp_with_value;
+	}
+	
+	return $total_time_with_value / $interval;
 }
 
+
 function modules_get_first_contact_date($id_agent_module) {
 	global $config;
 	
diff --git a/pandora_console/include/functions_reporting.php b/pandora_console/include/functions_reporting.php
index 74e03b8434..9116f02785 100755
--- a/pandora_console/include/functions_reporting.php
+++ b/pandora_console/include/functions_reporting.php
@@ -3715,12 +3715,11 @@ function reporting_availability($report, $content) {
 			$percent_ok = 0;
 		}
 		else {
-			$count_fails = count(
-				modules_get_data_with_value(
-					$item['id_agent_module'],
-					$report["datetime"] - $content['period'],
-					$report["datetime"],
-					0, true));
+			$count_fails = modules_get_count_data_with_value(
+				$item['id_agent_module'],
+				$report["datetime"] - $content['period'],
+				$report["datetime"],
+				0);
 			$percent_ok = (($count_checks - $count_fails) * 100) / $count_checks;
 			$percent_fail = 100 - $percent_ok;