From 8daca7d220f16c0f58ca161799d92be3274dccaa Mon Sep 17 00:00:00 2001
From: samucarc <samuel.carcases>
Date: Mon, 17 Sep 2018 18:57:29 +0200
Subject: [PATCH] Fixed module/agent/group alert report

---
 pandora_console/include/functions_alerts.php  | 18 ++---
 .../include/functions_reporting.php           | 72 ++++++++++++++-----
 .../include/functions_reporting_html.php      |  6 ++
 3 files changed, 63 insertions(+), 33 deletions(-)

diff --git a/pandora_console/include/functions_alerts.php b/pandora_console/include/functions_alerts.php
index 4d89dca614..2a9557ad57 100644
--- a/pandora_console/include/functions_alerts.php
+++ b/pandora_console/include/functions_alerts.php
@@ -1633,23 +1633,13 @@ function get_agent_alert_fired ($id_agent, $id_alert, $period, $date = 0) {
  *
  * @return array An array with all the events happened.
  */
-function get_module_alert_fired ($id_agent_module, $id_alert, $period, $date = 0) {
+function get_module_alert_fired ($id_agent_module, $id_alert) {
 	
-	if (!is_numeric ($date)) {
-		$date = time_w_fixed_tz($date);
-	}
-	if (empty ($date)) {
-		$date = get_system_time();
-	}
-	
-	$datelimit = $date - $period;
-	
-	$sql = sprintf ('SELECT timestamp
+	$sql = sprintf ('SELECT *
 		FROM tevento
-		WHERE id_agentmodule = %d AND utimestamp > %d
-			AND utimestamp <= %d
+		WHERE id_agentmodule = %d
 			AND id_alert_am = %d 
-		ORDER BY timestamp DESC', $id_agent_module, $datelimit, $date, $id_alert);
+		ORDER BY timestamp DESC', $id_agent_module, $id_alert);
 	
 	return db_get_all_rows_sql ($sql);
 }
diff --git a/pandora_console/include/functions_reporting.php b/pandora_console/include/functions_reporting.php
index c5e8e53725..a65c49cfd8 100755
--- a/pandora_console/include/functions_reporting.php
+++ b/pandora_console/include/functions_reporting.php
@@ -2866,14 +2866,28 @@ function reporting_alert_get_fired($id_agent_module, $id_alert_template_module,
 		$id_alert_template_module,
 		$period,
 		$datetime);
+		
+		if (!is_numeric ($datetime)) {
+			$datetime = time_w_fixed_tz($datetime);
+		}
+		if (empty ($datetime)) {
+			$datetime = get_system_time();
+		}
+		
+		$datelimit = $datetime - $period;
 	
+	$empty = '----------------------------';
 	if (empty($firedTimes)) {
 		$firedTimes = array();
-		$firedTimes[0]['timestamp'] = null;
+		$empty = _('There are no alerts defined');
+		$firedTimes[0]['timestamp'] = '';
 	}
 
 	foreach ($firedTimes as $fireTime) {
+		if($fireTime['utimestamp'] > $datelimit && $fireTime['utimestamp'] <= $datetime)
 		$fired[] = $fireTime['timestamp'];
+		else
+		$fired[] = $empty;
 	}
 
 	return $fired;
@@ -2956,6 +2970,16 @@ function reporting_alert_report_group($report, $content) {
 		
 		foreach ($alerts as $template => $actions) {
 
+			$datetime = (int) $report["datetime"];
+			if (!is_numeric ($datetime)) {
+				$datetime = time_w_fixed_tz($datetime);
+			}
+			if (empty ($datetime)) {
+				$datetime = get_system_time();
+			}
+			$period = (int) $content["period"];
+			$datelimit = $datetime - $period;
+			
 			$data_action = array();
 			$data_action['actions'] = array();
 			
@@ -2964,7 +2988,7 @@ function reporting_alert_report_group($report, $content) {
 				foreach ($actions["custom"] as $action) {
 					$data_action[$naction]["name"] = $action["name"];
 					$fired = $action["fired"];
-					if ($fired == 0){
+					if ($fired == 0 || ($fired <= $datelimit || $fired > $datetime)){
 						$data_action[$naction]['fired']  = '----------------------------';
 					}
 					else {
@@ -2977,7 +3001,7 @@ function reporting_alert_report_group($report, $content) {
 				foreach ($actions["default"] as $action) {
 					$data_action[$naction]["name"] = $action["name"];
 					$fired = $action["fired"];
-					if ($fired == 0){
+					if ($fired == 0 || ($fired <= $datelimit || $fired > $datetime)){
 						$data_action[$naction]['fired']  = '----------------------------';
 					}
 					else {
@@ -2990,7 +3014,7 @@ function reporting_alert_report_group($report, $content) {
 				foreach ($actions["unavailable"] as $action) {
 					$data_action[$naction]["name"] = $action["name"];
 					$fired = $action["fired"];
-					if ($fired == 0){
+					if ($fired == 0 || ($fired <= $datelimit || $fired > $datetime)){
 						$data_action[$naction]['fired']  = '----------------------------';
 					}
 					else {
@@ -3010,10 +3034,7 @@ function reporting_alert_report_group($report, $content) {
 													(int) $report["datetime"]);
 			$module_actions["actions"]        = $data_action;
 
-			if ($module_actions["template_fired"][0] !== null)
 			$data_row['alerts'][$ntemplates] = $module_actions;
-			else
-			$data_row = null;
 			$ntemplates++;
 		}
 
@@ -3076,6 +3097,16 @@ function reporting_alert_report_agent($report, $content) {
 		
 		foreach ($alerts as $template => $actions) {
 
+			$datetime = (int) $report["datetime"];
+			if (!is_numeric ($datetime)) {
+				$datetime = time_w_fixed_tz($datetime);
+			}
+			if (empty ($datetime)) {
+				$datetime = get_system_time();
+			}
+			$period = (int) $content["period"];
+			$datelimit = $datetime - $period;
+			
 			$data_action = array();
 			$data_action['actions'] = array();
 			
@@ -3084,7 +3115,7 @@ function reporting_alert_report_agent($report, $content) {
 				foreach ($actions["custom"] as $action) {
 					$data_action[$naction]["name"] = $action["name"];
 					$fired = $action["fired"];
-					if ($fired == 0){
+					if ($fired == 0 || ($fired <= $datelimit || $fired > $datetime)){
 						$data_action[$naction]['fired']  = '----------------------------';
 					}
 					else {
@@ -3097,7 +3128,7 @@ function reporting_alert_report_agent($report, $content) {
 				foreach ($actions["default"] as $action) {
 					$data_action[$naction]["name"] = $action["name"];
 					$fired = $action["fired"];
-					if ($fired == 0){
+					if ($fired == 0 || ($fired <= $datelimit || $fired > $datetime)){
 						$data_action[$naction]['fired']  = '----------------------------';
 					}
 					else {
@@ -3110,7 +3141,7 @@ function reporting_alert_report_agent($report, $content) {
 				foreach ($actions["unavailable"] as $action) {
 					$data_action[$naction]["name"] = $action["name"];
 					$fired = $action["fired"];
-					if ($fired == 0){
+					if ($fired == 0 || ($fired <= $datelimit || $fired > $datetime)){
 						$data_action[$naction]['fired']  = '----------------------------';
 					}
 					else {
@@ -3130,10 +3161,7 @@ function reporting_alert_report_agent($report, $content) {
 													(int) $report["datetime"]);
 			$module_actions["actions"]        = $data_action;
 
-			if ($module_actions["template_fired"][0] !== null)
 			$data_row['alerts'][$ntemplates] = $module_actions;
-			else 
-			$data_row = null;
 			$ntemplates++;
 		}
 
@@ -3199,6 +3227,15 @@ function reporting_alert_report_module($report, $content) {
 	$ntemplates = 0;
 	
 	foreach ($alerts as $template => $actions) {
+		$datetime = (int) $report["datetime"];
+		if (!is_numeric ($datetime)) {
+			$datetime = time_w_fixed_tz($datetime);
+		}
+		if (empty ($datetime)) {
+			$datetime = get_system_time();
+		}
+		$period = (int) $content["period"];
+		$datelimit = $datetime - $period;
 
 		$data_action = array();
 		$data_action['actions'] = array();
@@ -3208,7 +3245,7 @@ function reporting_alert_report_module($report, $content) {
 			foreach ($actions["custom"] as $action) {
 				$data_action[$naction]["name"] = $action["name"];
 				$fired = $action["fired"];
-				if ($fired == 0){
+				if ($fired == 0 || ($fired <= $datelimit || $fired > $datetime)){
 					$data_action[$naction]['fired']  = '----------------------------';
 				}
 				else {
@@ -3221,7 +3258,7 @@ function reporting_alert_report_module($report, $content) {
 			foreach ($actions["default"] as $action) {
 				$data_action[$naction]["name"] = $action["name"];
 				$fired = $action["fired"];
-				if ($fired == 0){
+				if ($fired == 0 || ($fired <= $datelimit || $fired > $datetime)){
 					$data_action[$naction]['fired']  = '----------------------------';
 				}
 				else {
@@ -3234,7 +3271,7 @@ function reporting_alert_report_module($report, $content) {
 			foreach ($actions["unavailable"] as $action) {
 				$data_action[$naction]["name"] = $action["name"];
 				$fired = $action["fired"];
-				if ($fired == 0){
+				if ($fired == 0 || ($fired <= $datelimit || $fired > $datetime)){
 					$data_action[$naction]['fired']  = '----------------------------';
 				}
 				else {
@@ -3254,10 +3291,7 @@ function reporting_alert_report_module($report, $content) {
 												(int) $report["datetime"]);
 		$module_actions["actions"]        = $data_action;
 
-		if ($module_actions["template_fired"][0] !== null)
 		$data_row['alerts'][$ntemplates] = $module_actions;
-		else
-		$data_row = null;
 		$ntemplates++;
 	}
 
diff --git a/pandora_console/include/functions_reporting_html.php b/pandora_console/include/functions_reporting_html.php
index 9ed6ab16e1..fd4d0a2c76 100644
--- a/pandora_console/include/functions_reporting_html.php
+++ b/pandora_console/include/functions_reporting_html.php
@@ -1960,6 +1960,12 @@ function reporting_html_alert_report($table, $item, $pdf = 0) {
 	$table1->data    = array ();
 	$table1->rowspan = array();
 	$table1->valign  = array();
+	
+	if ($item['data'] == null) {
+		$table->data['alerts']['cell'] = ui_print_empty_data ( __('No alerts defined') , '', true) ;
+		return true;
+	}
+	
 	$table1->head['agent']    = __('Agent');
 	$table1->head['module']   = __('Module');
 	$table1->head['template'] = __('Template');