diff --git a/pandora_console/include/ajax/events.php b/pandora_console/include/ajax/events.php
index cad5efae1a..d431a83dd7 100644
--- a/pandora_console/include/ajax/events.php
+++ b/pandora_console/include/ajax/events.php
@@ -478,10 +478,13 @@ if ($get_extended_event) {
 
 if ($get_events_details) {
 	$event_ids = explode(',',get_parameter ('event_ids'));
-	$events = db_get_all_rows_filter ('tevento',
-		array ('id_evento' => $event_ids,
-			'order' => 'utimestamp ASC'),
-			array ('evento', 'utimestamp', 'estado', 'criticity', 'id_usuario'));
+	$events = db_get_all_rows_filter (
+		'tevento',
+		array ('id_evento' => $event_ids,'order' => 'utimestamp ASC'),
+		array ('evento', 'utimestamp', 'estado', 'criticity', 'id_usuario'), 
+		'AND', 
+		true
+	);
 	
 	$out = '<table class="eventtable" style="width:100%;height:100%;padding:0px 0px 0px 0px; border-spacing: 0px; margin: 0px 0px 0px 0px;">';
 	$out .= '<tr style="font-size:0px; heigth: 0px; background: #ccc;"><td></td><td></td></tr>';
diff --git a/pandora_console/include/db/mysql.php b/pandora_console/include/db/mysql.php
index 7da9316c65..7101f960de 100644
--- a/pandora_console/include/db/mysql.php
+++ b/pandora_console/include/db/mysql.php
@@ -738,7 +738,7 @@ function mysql_db_get_row_sql ($sql, $search_history_db = false) {
  *
  * @return mixed Array of the row or false in case of error.
  */
-function mysql_db_get_row_filter ($table, $filter, $fields = false, $where_join = 'AND') {
+function mysql_db_get_row_filter ($table, $filter, $fields = false, $where_join = 'AND', $historydb = false) {
 	if (empty ($fields)) {
 		$fields = '*';
 	}
@@ -758,7 +758,7 @@ function mysql_db_get_row_filter ($table, $filter, $fields = false, $where_join
 	
 	$sql = sprintf ('SELECT %s FROM %s %s', $fields, $table, $filter);
 	
-	return db_get_row_sql ($sql);
+	return db_get_row_sql ($sql, $historydb);
 }
 
 /**
diff --git a/pandora_console/include/functions_db.php b/pandora_console/include/functions_db.php
index 70395cb205..d7c3dd2fc5 100644
--- a/pandora_console/include/functions_db.php
+++ b/pandora_console/include/functions_db.php
@@ -416,12 +416,12 @@ function db_get_row ($table, $field_search, $condition, $fields = false) {
  *
  * @return mixed Array of the row or false in case of error.
  */
-function db_get_row_filter($table, $filter, $fields = false, $where_join = 'AND') {
+function db_get_row_filter($table, $filter, $fields = false, $where_join = 'AND', $historydb = false) {
 	global $config;
 	
 	switch ($config["dbtype"]) {
 		case "mysql":
-			return mysql_db_get_row_filter($table, $filter, $fields, $where_join);
+			return mysql_db_get_row_filter($table, $filter, $fields, $where_join, $historydb);
 			break;
 		case "postgresql":
 			return postgresql_db_get_row_filter($table, $filter, $fields, $where_join);
@@ -493,7 +493,7 @@ function db_get_all_rows_sql($sql, $search_history_db = false, $cache = true, $d
  * @param int  $tend              end of search
  * 
  */
-function db_get_module_ranges_unknown($id_agente_modulo, $tstart = false, $tend = false) {
+function db_get_module_ranges_unknown($id_agente_modulo, $tstart = false, $tend = false, $historydb = false) {
 	global $config;
 
 	if (!isset($id_agente_modulo)) {
@@ -521,7 +521,7 @@ function db_get_module_ranges_unknown($id_agente_modulo, $tstart = false, $tend
 	$query .= " AND utimestamp >= $tstart AND utimestamp <= $tend ";
 	$query .= " ORDER BY utimestamp ASC";
 
-	$events = db_get_all_rows_sql($query);
+	$events = db_get_all_rows_sql($query, $historydb);
 
 	if (! is_array($events)){
 		return false;
@@ -582,17 +582,6 @@ function db_uncompress_module_data($id_agente_modulo, $tstart = false, $tend = f
 		return false;
 	}
 
-	if ((!isset($tstart)) || ($tstart === false)) {
-		// Return data from the begining
-		// Get first available utimestamp in active DB
-		$query_first_man_time  = " SELECT utimestamp FROM tagente_datos ";
-		$query_first_man_time .= " WHERE id_agente_modulo = $id_agente_modulo";
-		$query_first_man_time .= " ORDER BY utimestamp ASC LIMIT 1";
-
-		$first_man_time = db_get_all_rows_sql( $query_first_man_time, false);
-		$tstart = $first_man_time[0]['utimestamp'];
-	}
-
 	if ((!isset($tend)) || ($tend === false)) {
 		// Return data until now
 		$tend = time();
@@ -616,53 +605,51 @@ function db_uncompress_module_data($id_agente_modulo, $tstart = false, $tend = f
 		$table = "tagente_datos_string";
 	}
 
-	// Get first available utimestamp in active DB
-	$query  = " SELECT utimestamp, datos FROM $table ";
-	$query .= " WHERE id_agente_modulo=$id_agente_modulo AND utimestamp < $tstart";
-	$query .= " ORDER BY utimestamp DESC LIMIT 1";
+	$result = modules_get_first_date($id_agente_modulo,$tstart);
+	$first_utimestamp = $result["first_utimestamp"];
+	$search_historydb = $result["search_historydb"];
 
-	$ret = db_get_all_rows_sql( $query , $search_historydb);
-
-	if ( ( $ret === false ) || (( isset($ret[0]["utimestamp"]) && ($ret[0]["utimestamp"] > $tstart )))) {
-		// Value older than first retrieved from active DB
-		$search_historydb = true;
-
-		$ret = db_get_all_rows_sql( $query , $search_historydb);
-
-		if ($ret) {
-			$tstart = $ret[0]["utimestamp"];
-		}
-	}
-	else {
-		$first_data["utimestamp"] = $ret[0]["utimestamp"];
-		$first_data["datos"]      = $ret[0]["datos"];
-	}
-
-	if ( ( $ret === false ) || (( isset($ret[0]["utimestamp"]) && ($ret[0]["utimestamp"] > $tstart )))) {
-		// No previous data. -> not init
-		// Avoid false unknown status
-		$first_data["utimestamp"] = time();
+	if ($first_utimestamp === false) {
+		$first_data["utimestamp"] = $tstart;
 		$first_data["datos"]      = false;
 	}
 	else {
-		$first_data["utimestamp"] = $ret[0]["utimestamp"];
-		$first_data["datos"]      = $ret[0]["datos"];
+		$query  = "SELECT datos,utimestamp FROM $table ";
+		$query .= " WHERE id_agente_modulo=$id_agente_modulo ";
+		$query .= " AND utimestamp=" . $first_utimestamp;
+
+		$data = db_get_all_rows_sql($query,$search_historydb);
+		if ($data === false) {
+			// first utimestamp not found in active database
+			// SEARCH HISTORY DB
+			$search_historydb = true;
+			$data = db_get_all_rows_sql($query,$search_historydb);
+		}
+
+		if ($data === false) { // Not init
+			$first_data["utimestamp"] = $tstart;
+			$first_data["datos"]      = false;
+		}
+		else {
+			$first_data["utimestamp"] = $data[0]["utimestamp"];
+			$first_data["datos"]      = $data[0]["datos"];
+
+		}
 	}
 
 	$query  = " SELECT utimestamp, datos FROM $table ";
 	$query .= " WHERE id_agente_modulo=$id_agente_modulo AND utimestamp >= $tstart AND utimestamp <= $tend";
 	$query .= " ORDER BY utimestamp ASC";
-
 	// Retrieve all data from module in given range
 	$raw_data = db_get_all_rows_sql($query, $search_historydb);
 
-	if (($raw_data === false) && ($ret === false)) {
+	if (($raw_data === false) && ($first_utimestamp === $tstart)) {
 		// No data
 		return false;
 	}
 
 	// Retrieve going unknown events in range
-	$unknown_events = db_get_module_ranges_unknown($id_agente_modulo, $tstart, $tend);
+	$unknown_events = db_get_module_ranges_unknown($id_agente_modulo, $tstart, $tend, $search_historydb);
 
 	// Retrieve module_interval to build the template
 	$module_interval = modules_get_interval ($id_agente_modulo);
@@ -685,197 +672,115 @@ function db_uncompress_module_data($id_agente_modulo, $tstart = false, $tend = f
 	$current_timestamp   = $tstart;
 	$last_inserted_value = $first_data["datos"];
 	$last_timestamp      = $first_data["utimestamp"];
-	$data_found          = 0;
+	$last_value    		 = $first_data["datos"];
 
 	// Build template
 	$pool_id = 0;
 	$now = time();
 
-	$in_unknown_status = 0;
-	if (is_array($unknown_events)) {
-		$current_unknown = array_shift($unknown_events);
-	}
-	while ( $current_timestamp < $tend ) {
-		$expected_data_generated = 0;
+	$current_unknown  = array_shift($unknown_events);
+	$current_raw_data = array_shift($raw_data);
 
+	while ( $current_timestamp < $tend ) {
 		$return[$pool_id]["data"] = array();
 		$tmp_data   = array();
-		$data_found = 0;
+		$current_timestamp_end = $current_timestamp + $slice_size;
 
-		if (is_array($unknown_events)) {
-			$i = 0;
-			while ($current_timestamp >= $unknown_events[$i]["time_to"] ) {
-				// Skip unknown events in past
-				array_splice($unknown_events, $i,1);
-				$i++;
-				if (!isset($unknown_events[$i])) {
-					break;
-				}
-			}
-			if (isset($current_unknown)) {
-
-				// check if recovered from unknown status
-				if(is_array($unknown_events) && isset($current_unknown)) {
-					if (   (($current_timestamp+$slice_size) > $current_unknown["time_to"])
-						&& ($current_timestamp < $current_unknown["time_to"])
-						&& ($in_unknown_status == 1) ) {
-						// Recovered from unknown
-
-						if (   ($current_unknown["time_to"] > $current_timestamp)
-							&& ($expected_data_generated == 0) ) {
-							// also add the "expected" data
-							$tmp_data["utimestamp"] = $current_timestamp;
-							if ($in_unknown_status == 1) {
-								$tmp_data["datos"]  = null;
-							}
-							else {
-								$tmp_data["datos"]  = $last_inserted_value;
-							}
-							$return[$pool_id]["utimestamp"] = $current_timestamp;
-							array_push($return[$pool_id]["data"], $tmp_data);
-							$expected_data_generated = 1;
-						}
-
-
-						$tmp_data["utimestamp"] = $current_unknown["time_to"];
-						$tmp_data["datos"]      = $last_inserted_value;
-						// debug purpose
-						//$tmp_data["obs"]        = "event recovery data";
-						
-						$return[$pool_id]["utimestamp"] = $current_timestamp;
-						array_push($return[$pool_id]["data"], $tmp_data);
-						$data_found = 1;
-						$in_unknown_status = 0;
-					}
-
-					if (   (($current_timestamp+$slice_size) > $current_unknown["time_from"])
-						&& (($current_timestamp+$slice_size) < $current_unknown["time_to"])
-						&& ($in_unknown_status == 0) ) {
-						// Add unknown state detected
-
-						if ( $current_unknown["time_from"] < ($current_timestamp+$slice_size)) {
-							if (   ($current_unknown["time_from"] > $current_timestamp)
-								&& ($expected_data_generated == 0) ) {
-								// also add the "expected" data
-								$tmp_data["utimestamp"] = $current_timestamp;
-								if ($in_unknown_status == 1) {
-									$tmp_data["datos"]  = null;
-								}
-								else {
-									$tmp_data["datos"]  = $last_inserted_value;
-								}
-								$return[$pool_id]["utimestamp"] = $current_timestamp;
-								array_push($return[$pool_id]["data"], $tmp_data);
-								$expected_data_generated = 1;
-							}
-
-							$tmp_data["utimestamp"] = $current_unknown["time_from"];
-							$tmp_data["datos"]      = null;
-							// debug purpose
-							//$tmp_data["obs"] = "event data";
-							$return[$pool_id]["utimestamp"] = $current_timestamp;
-							array_push($return[$pool_id]["data"], $tmp_data);
-							$data_found = 1;
-						}
-						$in_unknown_status = 1;
-					}
-
-					if ( ($in_unknown_status == 0) && ($current_timestamp >= $current_unknown["time_to"]) ) {
-						$current_unknown = array_shift($unknown_events);
-					}
-				}
-			} // unknown events handle
-		}
-
-		// Search for data
-		$i=0;
-		if (is_array($raw_data)) {
-			foreach ($raw_data as $data) {
-				if ( ($data["utimestamp"] >= $current_timestamp)
-				  && ($data["utimestamp"] < ($current_timestamp+$slice_size)) ) {
-					// Data in block, push in, and remove from $raw_data (processed)
-
-					if (   ($data["utimestamp"] > $current_timestamp)
-						&& ($expected_data_generated == 0) ) {
-						// also add the "expected" data
-						$tmp_data["utimestamp"] = $current_timestamp;
-						if ($in_unknown_status == 1) {
-							$tmp_data["datos"]  = null;
-						}
-						else {
-							$tmp_data["datos"]  = $last_inserted_value;
-						}
-						//$tmp_data["obs"] = "expected data";
-						$return[$pool_id]["utimestamp"] = $current_timestamp;
-						array_push($return[$pool_id]["data"], $tmp_data);
-						$expected_data_generated = 1;
-					}
-
-					$tmp_data["utimestamp"] = intval($data["utimestamp"]);
-					$tmp_data["datos"]      = $data["datos"];
-					// debug purpose
-					//$tmp_data["obs"] = "real data";
-
-					$return[$pool_id]["utimestamp"] = $current_timestamp;
-					array_push($return[$pool_id]["data"], $tmp_data);
-
-					$last_inserted_value = $data["datos"];
-					$last_timestamp      = intval($data["utimestamp"]);
-
-					unset($raw_data[$i]);
-					$data_found = 1;
-					$in_unknown_status = 0;
-				}
-				elseif ($data["utimestamp"] > ($current_timestamp+$slice_size)) {
-					// Data in future, stop searching new ones
-					break;
-				}
-			}
-			$i++;
-		}
-
-		if ($data_found == 0) {
-			// No data found, lug the last_value until SECONDS_1DAY + 2*modules_get_interval
-			// UNKNOWN!
-
-			if (($current_timestamp > $now) || (($current_timestamp - $last_timestamp) > (SECONDS_1DAY + 2*$module_interval))) {
-				if (isset($last_inserted_value)) {
-					// unhandled unknown status control
-					$unhandled_time_unknown = $current_timestamp - (SECONDS_1DAY + 2*$module_interval) - $last_timestamp;
-					if ($unhandled_time_unknown > 0) {
-						// unhandled unknown status detected. Add to previous pool
-						$tmp_data["utimestamp"] = intval($last_timestamp) +  (SECONDS_1DAY + 2*$module_interval);
-						$tmp_data["datos"]      = null;
-						// debug purpose
-						//$tmp_data["obs"] = "unknown extra";
-						// add to previous pool if needed
-						if (isset($return[$pool_id-1])) {
-							array_push($return[$pool_id-1]["data"], $tmp_data);
-						}
-					}
-				}
-				$last_inserted_value = null;
-			}
-
-			$tmp_data["utimestamp"] = $current_timestamp;
-
-			if ($in_unknown_status == 1) {
-				$tmp_data["datos"]  = null;
-			}
-			else {
-				$tmp_data["datos"]  = $last_inserted_value;
-			}
-			// debug purpose
-			//$tmp_data["obs"] = "virtual data";
+		if ( ( $current_timestamp > $now) || 
+			 ( ($current_timestamp_end - $last_timestamp) > 
+			   (SECONDS_1DAY + 2*$module_interval) ) ) {
+			$tmp_data["utimestamp"] = $last_timestamp + SECONDS_1DAY + 2*$module_interval;
 			
-			$return[$pool_id]["utimestamp"] = $current_timestamp;
+			//check not init
+			$tmp_data["datos"] = $last_value === false ? false : null;
+			
+			// debug purpose
+			//$tmp_data["obs"] = "unknown extra";
 			array_push($return[$pool_id]["data"], $tmp_data);
 		}
 
-		$pool_id++;
-		$current_timestamp += $slice_size;
-	}
+		//insert first slice data
+		$tmp_data["utimestamp"] = $current_timestamp;
+		$tmp_data["datos"]  = $last_inserted_value;
+		// debug purpose
+		//$tmp_data["obs"] = "virtual data";
+		
+		$return[$pool_id]["utimestamp"] = $current_timestamp;
+		array_push($return[$pool_id]["data"], $tmp_data);
 
+		//insert raw data
+		while ( ($current_raw_data != null) &&
+				(  ($current_timestamp_end >= $current_raw_data['utimestamp']) && 
+				   ($current_timestamp     <  $current_raw_data['utimestamp']) ) ) {
+		
+			// Add unknown state detected
+			$tmp_data["utimestamp"] = $current_raw_data["utimestamp"];
+			$tmp_data["datos"]      = $current_raw_data["datos"];
+			// debug purpose
+			//$tmp_data["obs"] = "real data";
+			array_push($return[$pool_id]["data"], $tmp_data);
+
+			$last_value = $current_raw_data["datos"];
+			$last_timestamp = $current_raw_data["utimestamp"];
+			$current_raw_data = array_shift($raw_data);
+		}
+
+		//unknown
+		$data_slices = $return[$pool_id]["data"];
+		while ( ($current_unknown != null) &&
+			  	( ( ($current_unknown['time_from'] != null) &&
+			  	    ($current_timestamp_end >= $current_unknown['time_from']) ) || 
+				  ($current_timestamp_end >= $current_unknown['time_to']) ) ) {
+				
+			if( ( $current_timestamp < $current_unknown['time_from']) && 
+				( $current_timestamp_end >= $current_unknown['time_from'] ) ){
+				// Add unknown state detected
+				$tmp_data["utimestamp"] = $current_unknown["time_from"];
+				$tmp_data["datos"]      = null;
+				// debug purpose
+				//$tmp_data["obs"] = "event data unknown from";
+				array_push($return[$pool_id]["data"], $tmp_data);
+				$current_unknown["time_from"] = null;
+			}
+
+			if( ($current_timestamp < $current_unknown['time_to']) && 
+				($current_timestamp_end >= $current_unknown['time_to'] ) ){
+				$tmp_data["utimestamp"] = $current_unknown["time_to"];
+				$i = count($data_slices) - 1;
+				while ($i >= 0) {
+					if($data_slices[$i]['utimestamp'] <= $current_unknown["time_to"]){
+						$tmp_data["datos"] = 
+							$data_slices[$i]['datos'] == null
+							? $last_value
+							: $data_slices[$i]['datos'];
+						break;
+					}
+					$i--;
+				}
+				
+				// debug purpose
+				//$tmp_data["obs"] = "event data unknown to";
+				array_push($return[$pool_id]["data"], $tmp_data);
+				$current_unknown = array_shift($unknown_events);
+			}
+		}
+
+		//sort current slice 
+		usort(
+			$return[$pool_id]['data'],
+			function ($a, $b) {
+    			if ($a['utimestamp'] == $b['utimestamp']) return 0;
+    			return ($a['utimestamp'] < $b['utimestamp']) ? -1 : 1;
+			}
+		);
+		//put the last slice data like first element of next slice
+		$last_inserted_value = end($return[$pool_id]['data']);
+		$last_inserted_value = $last_inserted_value['datos'];
+		
+		//increment
+		$pool_id++;
+		$current_timestamp = $current_timestamp_end;
+	}
 	return $return;
 }
 
diff --git a/pandora_console/include/functions_graph.php b/pandora_console/include/functions_graph.php
index 88e67981e0..65ad585ef0 100644
--- a/pandora_console/include/functions_graph.php
+++ b/pandora_console/include/functions_graph.php
@@ -550,8 +550,6 @@ function grafico_modulo_sparse_data ($agent_module_id, $period, $show_events,
 	$datelimit = $date - $period;
 	$search_in_history_db = db_search_in_history_db($datelimit);
 	
-	
-	
 	if($force_interval){
 			$resolution = $period/$time_interval;
 	}
@@ -582,18 +580,29 @@ function grafico_modulo_sparse_data ($agent_module_id, $period, $show_events,
 	// Get event data (contains alert data too)
 	$events = array();
 	if ($show_unknown == 1 || $show_events == 1 || $show_alerts == 1) {
-		$events = db_get_all_rows_filter ('tevento',
+		$events = db_get_all_rows_filter (
+			'tevento',
 			array ('id_agentmodule' => $agent_module_id,
 				"utimestamp > $datelimit",
 				"utimestamp < $date",
 				'order' => 'utimestamp ASC'),
-			array ('id_evento', 'evento', 'utimestamp', 'event_type'));
+			array ('id_evento', 'evento', 'utimestamp', 'event_type'), 
+			'AND', 
+			$search_in_history_db
+		);
 		
 		// Get the last event after inverval to know if graph start on unknown
-		$prev_event = db_get_row_filter ('tevento',
+		$prev_event = db_get_row_filter (
+			'tevento',
 			array ('id_agentmodule' => $agent_module_id,
 				"utimestamp <= $datelimit",
-				'order' => 'utimestamp DESC'));
+				'order' => 'utimestamp DESC'
+			),
+			false,
+			'AND',
+			$search_in_history_db	
+		);
+
 		if (isset($prev_event['event_type']) && $prev_event['event_type'] == 'going_unknown') {
 			$start_unknown = true;
 		}
@@ -3954,22 +3963,33 @@ function grafico_modulo_boolean_data ($agent_module_id, $period, $show_events,
 	if ($uncompressed_module) {
 		$avg_only = 1;
 	}
-	$search_in_history_db = db_search_in_history_db($datelimit);
 	
 	// Get event data (contains alert data too)
 	if ($show_unknown == 1 || $show_events == 1 || $show_alerts == 1) {
-		$events = db_get_all_rows_filter('tevento',
+		$events = db_get_all_rows_filter(
+			'tevento',
 			array ('id_agentmodule' => $agent_module_id,
 				"utimestamp > $datelimit",
 				"utimestamp < $date",
-				'order' => 'utimestamp ASC'),
-			array ('evento', 'utimestamp', 'event_type', 'id_evento'));
+				'order' => 'utimestamp ASC'
+			),
+			array ('evento', 'utimestamp', 'event_type', 'id_evento'), 
+			'AND', 
+			$search_in_history_db
+		);
 		
 		// Get the last event after inverval to know if graph start on unknown
-		$prev_event = db_get_row_filter ('tevento',
+		$prev_event = db_get_row_filter (
+			'tevento',
 			array ('id_agentmodule' => $agent_module_id,
 				"utimestamp <= $datelimit",
-				'order' => 'utimestamp DESC'));
+				'order' => 'utimestamp DESC'
+			),
+			false,
+			'AND',
+			$search_in_history_db
+		);
+
 		if (isset($prev_event['event_type']) && $prev_event['event_type'] == 'going_unknown') {
 			$start_unknown = true;
 		}
@@ -4074,7 +4094,7 @@ function grafico_modulo_boolean_data ($agent_module_id, $period, $show_events,
 			$zero = 0;
 			$total = 0;
 			$count = 0;
-			$is_unknown = false;
+			
 			// Read data that falls in the current interval
 			while (isset ($data[$j]) &&
 				$data[$j]['utimestamp'] >= $timestamp &&
@@ -4107,7 +4127,7 @@ function grafico_modulo_boolean_data ($agent_module_id, $period, $show_events,
 			
 			// Is the first point of a unknown interval
 			$first_unknown = false;
-			
+			$check_unknown = false;
 			$event_ids = array();
 			$alert_ids = array();
 			while (isset ($events[$k]) &&
@@ -4126,10 +4146,15 @@ function grafico_modulo_boolean_data ($agent_module_id, $period, $show_events,
 						if ($is_unknown == false) {
 							$first_unknown = true;
 						}
+						else{
+							$first_unknown = false;	
+						}
+						$check_unknown = true;
 						$is_unknown = true;
 					}
 					else if (substr ($events[$k]['event_type'], 0, 5) == 'going') {
 						$is_unknown = false;
+						$first_unknown = false;
 					}
 				}
 				$k++;
@@ -4212,6 +4237,12 @@ function grafico_modulo_boolean_data ($agent_module_id, $period, $show_events,
 					$chart[$timestamp]['unknown'.$series_suffix] = 0;
 				}
 				$chart[$timestamp]['unknown'.$series_suffix] = $unknown_value;
+				
+				if($unknown_value == 0 && $check_unknown == true){
+					$chart[$timestamp]['unknown'.$series_suffix] = 1;
+					$check_unknown = false;
+				}
+				
 				$series_type['unknown'.$series_suffix] = 'area';
 			}
 			
@@ -4342,165 +4373,138 @@ function fullscale_data ( &$chart_data, &$chart_extra_data, &$long_index,
 	global $min_value;
 	global $series_type;
 	global $chart_extra_data;
-	
-	$ranges_unknown = db_get_module_ranges_unknown($agent_module_id, $datelimit, $date);
 
-	$table = "tagente_datos";
-	$module_type_str = modules_get_type_name ($agent_module_id);
-	if (strstr ($module_type_str, 'string') !== false) {
-		$table = "tagente_datos_string";
-	}
+	$first_data = 0;
 
-	$query  = " SELECT utimestamp, datos FROM $table ";
-	$query .= " WHERE id_agente_modulo=$agent_module_id ";
-	$query .= " ORDER BY utimestamp ASC LIMIT 1";
-
-	$ret = db_get_all_rows_sql( $query , true);
-	
-	$first_data = $ret[0]['utimestamp'];
 	$data_uncompress = db_uncompress_module_data($agent_module_id, $datelimit, $date);
-	$i         = 0;
-	$max_value = 0;
-	$min_value = 0;
-	$timestamp_second = 0;
-	if(is_array($data_uncompress)){
-		foreach ($data_uncompress as $v) {
-			foreach ($v['data'] as $key => $value) {
-				$real_date = date("Y M d H:i:s", $value['utimestamp']);
+
+	$chart_data = array();
+	
+	$min_value = PHP_INT_MAX-1;
+	$max_value = PHP_INT_MIN+1;
+	$previous_data = $first_data;
+	$previous_unknown = 0;
+	
+	$i=0;
+	$current_event = $events[0];
+	$prueba = array();
+	foreach ($data_uncompress as $k) {
+		foreach ($k["data"] as $v) {
+			$real_date = date("Y M d H:i:s", $v['utimestamp']);
+
+			if(!$flash_chart){
+				$real_date = date("Y/M/d", $v['utimestamp']);
+				$real_date .= "\n";
+				$real_date .= date("   H:i:s", $v['utimestamp']);
+			}
+
+			$event_ids = array();
+			$alert_ids = array();
+			while (isset($current_event) && ($v['utimestamp'] >= $current_event["utimestamp"]) ) {
+				$event_date = date("Y M d H:i:s", $current_event['utimestamp']);
 				if(!$flash_chart){
-					$real_date = date("Y/M/d", $value['utimestamp']);
-					$real_date .= "\n";
-					$real_date .= date("   H:i:s", $value['utimestamp']);
+					$event_date = date("Y/M/d", $current_event['utimestamp']);
+					$event_date .= "\n";
+					$event_date .= date("   H:i:s", $current_event['utimestamp']);
 				}
-				// Read events and alerts that fall in the current interval
-				$event_value   = 0;
-				$alert_value   = 0;
-				$unknown_value = 0;
-				$event_i       = 0;
-				// Is the first point of a unknown interval
-				$first_unknown = false;
-				
-				$event_ids = array();
-				$alert_ids = array();
-				
-				//
-				if($timestamp_second == 0){
-					$timestamp_second = $value['utimestamp']; 
+	
+				if ($show_events && (strpos($current_event["event_type"], "going") !== false)) {
+					$event_ids[$event_date][] = $current_event["id_evento"];
+	
+					$chart_data[$event_date]["event" . $series_suffix] = 1;
+					$chart_data[$event_date]["alert" . $series_suffix] = NULL;
+					$chart_extra_data[count($chart_data)-1]['events'] = implode (',', $event_ids[$event_date]);
+				}
+				elseif ($show_alerts && (strpos($current_event["event_type"], "alert") !== false)) {
+					$alert_ids[$event_date][] = $current_event["id_evento"];
+	
+					$chart_data[$event_date]["event" . $series_suffix] = NULL;
+					$chart_data[$event_date]["alert" . $series_suffix] = 1;
+					$chart_extra_data[count($chart_data)-1]['alerts'] = implode (',', $alert_ids[$event_date]);
 				}
-				$timestamp_first  = $timestamp_second;
-				$timestamp_second = $value['utimestamp'];
 				
-				foreach ($events as $key => $val) {
-					if( $val['utimestamp'] > $timestamp_first && 
-						$val['utimestamp'] <= $timestamp_second ){
-						if ($show_events == 1) {
-							$event_ids[] = $val['id_evento'];
-							$event_value++;
-						}
-						if ($show_alerts == 1 && substr ($val['event_type'], 0, 5) == 'alert') {
-							$alert_ids[] = $val['id_evento'];
-							$alert_value++;
-						}
-						if ($show_unknown) {
-							if ($val['event_type'] == 'going_unknown') {
-								if ($is_unknown == false) {
-									$first_unknown = true;
-								}
-								$is_unknown = true;
-							}
-							else if (substr ($val['event_type'], 0, 5) == 'going') {
-								$is_unknown = false;
-							}
-						}
+				$chart_data[$event_date]["sum" . $series_suffix] = $previous_data;
+				if($show_unknown) {
+					$chart_data[$event_date]["unknown" . $series_suffix] = $previous_unknown;
+				}
+				$current_event = $events[$i++];
+			}			
+
+			if ($v["datos"] === NULL) {
+				// Unknown
+				if (!isset($chart_data[$real_date]["event" . $series_suffix])) {
+					if($show_events) {
+						$chart_data[$real_date]["event" . $series_suffix] = NULL;
+					}
+					if($show_alerts) {
+						$chart_data[$real_date]["alert" . $series_suffix] = NULL;
 					}
 				}
-
-				if(empty($value['datos'])){
-					if($value['utimestamp'] < $first_data){
-						//$chart_data[$real_date]['unknown'.$series_suffix] = 0;
-						$is_unknown = false;
+	
+				$chart_data[$real_date]["sum" . $series_suffix] = $previous_data;
+				if($show_unknown) {
+					$chart_data[$real_date]["unknown" . $series_suffix] = "1";
+				}
+				$previous_unknown = "1";
+			}
+			elseif($v["datos"] === false) {
+				// Not Init
+				$previous_data = $v["datos"];
+				if (!isset($chart_data[$real_date]["event" . $series_suffix])) {
+					if ($show_events) {
+						$chart_data[$real_date]["event" . $series_suffix] = NULL;
 					}
-					else{
-						//$chart_data[$real_date]['unknown'.$series_suffix] = 1;
-						$first_unknown = true;
+					if ($show_alerts) {
+						$chart_data[$real_date]["alert" . $series_suffix] = NULL;
 					}
 				}
-
-				$timestamp_short = date("Y M d H:i:s", $value['utimestamp']);
-
-				if(!$flash_chart){
-					$timestamp_short = date("Y/M/d", $value['utimestamp']);
-					$timestamp_short .= "\n";
-					$timestamp_short .= date("   H:i:s", $value['utimestamp']);
-				}
-
-
-				$long_index[$timestamp_short] = date(
-					html_entity_decode($config['date_format'], ENT_QUOTES, "UTF-8"), $value['utimestamp']);
-				// In some cases, can be marked as known because a recovery event
-				// was found in same interval. For this cases first_unknown is 
-				// checked too
-				if ($is_unknown || $first_unknown) {
-					$unknown_value++;
-				}
-
-				// Data
-				if ($show_events) {
-					if (!isset($chart_data[$real_date]['event'.$series_suffix])) {
-						$chart_data[$real_date]['event'.$series_suffix] = 0;
-					}
-					
-					$chart_data[$real_date]['event'.$series_suffix] += $event_value;
-					
-					$series_type['event'.$series_suffix] = 'points';
-				}
-
-				if ($show_alerts) {
-					if (!isset($chart_data[$real_date]['alert'.$series_suffix])) {
-						$chart_data[$real_date]['alert'.$series_suffix] = 0;
-					}
-					
-					$chart_data[$real_date]['alert'.$series_suffix] += $alert_value;
-					
-					$series_type['alert'.$series_suffix] = 'points';
-				}
-
-				$chart_data[$real_date]['sum'.$series_suffix] = $value['datos'];
-				
-				if($value['datos'] > $max_value){
-					$max_value = $value['datos'];
-				}
-
-				if($value['datos'] < $min_value){
-					$min_value = $value['datos'];
-				}
 				
-				if ($show_unknown) {
-					if (!isset($chart_data[$real_date]['unknown'.$series_suffix])) {
-						$chart_data[$real_date]['unknown'.$series_suffix] = 0;
-					}	
-					$chart_data[$real_date]['unknown'.$series_suffix] = $unknown_value;
-					$series_type['unknown'.$series_suffix] = 'area';	
+				$chart_data[$real_date]["sum" . $series_suffix] = $v["datos"];
+
+				if($v['datos'] >= $max_value){
+					$max_value = $v['datos'];
 				}
-				
-				if (!empty($event_ids)) {
-					$chart_extra_data[count($chart_data)-1]['events'] = implode(',',$event_ids);
+
+				if($v['datos'] <= $min_value){
+					$min_value = $v['datos'];
 				}
-				if (!empty($alert_ids)) {
-					$chart_extra_data[count($chart_data)-1]['alerts'] = implode(',',$alert_ids);
+
+				if($show_unknown) {
+					$chart_data[$real_date]["unknown" . $series_suffix] = NULL;
+					$previous_unknown = NULL;
 				}
 			}
-		}
-	
-		if (!is_null($percentil) && $percentil) {
-			$avg = array_map(function($item) { return $item['sum']; }, $chart_data);
-			$percentil_result = get_percentile($percentil, $avg);
-			//Fill the data of chart
-			array_walk($chart_data, function(&$item) use ($percentil_result, $series_suffix) {
-				$item['percentil' . $series_suffix] = $percentil_result; });
-			$series_type['percentil' . $series_suffix] = 'line';
+			else {
+				$previous_data = $v["datos"];
+				if (!isset($chart_data[$real_date]["event" . $series_suffix])) {
+					if ($show_events) {
+						$chart_data[$real_date]["event" . $series_suffix] = NULL;
+					}
+					if ($show_alerts) {
+						$chart_data[$real_date]["alert" . $series_suffix] = NULL;
+					}
+				}
+				
+				$chart_data[$real_date]["sum" . $series_suffix] = $v["datos"];
+
+				if($v['datos'] >= $max_value){
+					$max_value = $v['datos'];
+				}
+
+				if($v['datos'] <= $min_value){
+					$min_value = $v['datos'];
+				}
+
+				if($show_unknown) {
+					$chart_data[$real_date]["unknown" . $series_suffix] = NULL;
+					$previous_unknown = NULL;
+				}
+			}	
 		}
 	}
+	$series_type['event'.$series_suffix] = 'points';
+	$series_type['alert'.$series_suffix] = 'points';
+	$series_type['unknown'.$series_suffix] = 'area';
 }
 
 function grafico_modulo_boolean ($agent_module_id, $period, $show_events,
diff --git a/pandora_console/include/functions_modules.php b/pandora_console/include/functions_modules.php
index e46334dbef..92988575a8 100755
--- a/pandora_console/include/functions_modules.php
+++ b/pandora_console/include/functions_modules.php
@@ -2307,50 +2307,53 @@ function modules_change_relation_lock ($id_relation) {
 	return ($result !== false ? $new_value : $old_value);
 }
 
-
-
-function modules_get_count_datas($id_agent_module, $date_init, $date_end) {
-	$interval = modules_get_interval ($id_agent_module);
-	
-	// TODO REMOVE THE TIME IN PLANNED DOWNTIME
-	
-	if (!is_numeric($date_init)) {
-		$date_init = strtotime($date_init);
-	}
-	
-	if (!is_numeric($date_end)) {
-		$date_end = strtotime($date_end);
-	}
-	
-	
-	
-	$first_date = modules_get_first_contact_date($id_agent_module);
-	
-	
-	
-	if ($date_init < $first_date) {
-		$date_init = $first_date;
-	}
-	
-	$diff = $date_end - $date_init;
-	
-	
-	return ($diff / $interval);
-}
-
-
-function modules_get_first_contact_date($id_agent_module) {
+/*
+ * @return utimestamp with the first contact of the module or first contact before datelimit, false if not-init
+ */
+function modules_get_first_date($id_agent_module, $datelimit = 0) {
 	global $config;
 	
-	// TODO REMOVE THE TIME IN PLANNED DOWNTIME
+	//check datatype string or normal
+	$table = "tagente_datos";
+	$module_type_str = modules_get_type_name ($id_agent_module);
+	if (strstr ($module_type_str, 'string') !== false) {
+		$table = "tagente_datos_string";
+	}
+
+	$search_historydb = false;
+
+	// tagente_estado.first_utimestamp is not valid or is not updated. Scan DBs for first utimestamp
+	if ($datelimit > 0) {
+		// get last data before datelimit
+		$query  = " SELECT max(utimestamp) as utimestamp FROM $table ";
+		$query .= " WHERE id_agente_modulo=$id_agent_module ";
+		$query .= " AND utimestamp < $datelimit ";
 	
-	// TODO FOR OTHER KIND OF DATA
+	}
+	else {
+		// get first utimestamp
+		$query  = " SELECT min(utimestamp) as utimestamp FROM $table ";
+		$query .= " WHERE id_agente_modulo=$id_agent_module ";
+	}
 	
-	$first_date = db_get_value('utimestamp', 'tagente_datos',
-		'id_agente_modulo', $id_agent_module,
-		$config['history_db_enabled']);
-	
-	return $first_date;
+
+	// SEARCH ACTIVE DB
+	$data = db_get_all_rows_sql($query,$search_historydb);
+	if (($data === false) || ($data[0]["utimestamp"] === NULL) || ($data[0]["utimestamp"] <= 0)) {
+		// first utimestamp not found in active database
+		// SEARCH HISTORY DB
+		$search_historydb = true;
+		$data = db_get_all_rows_sql($query,$search_historydb);
+	}
+
+	if (($data === false) || ($data[0]["utimestamp"] === NULL) || ($data[0]["utimestamp"] <= 0)) {
+		// Nor active DB nor history DB have the data, the module is not-init
+		return array ("first_utimestamp" => false, "search_historydb" => $search_historydb);
+	}
+
+	// The data has been found
+	return array ("first_utimestamp" => $data[0]["utimestamp"], "search_historydb" => $search_historydb);
+
 }
 
 /**
diff --git a/pandora_console/include/graphs/flot/pandora.flot.js b/pandora_console/include/graphs/flot/pandora.flot.js
index 6dc545b3f5..9c9613420f 100644
--- a/pandora_console/include/graphs/flot/pandora.flot.js
+++ b/pandora_console/include/graphs/flot/pandora.flot.js
@@ -1772,9 +1772,11 @@ function pandoraFlotArea(graph_id, values, labels, labels_long, legend,
 
 				if (timesize+timenewpos > canvaslimit) {
 					$('#timestamp_'+graph_id).css('left', timenewpos - timesize);
+					$('#timestamp_'+graph_id).css('top', 50);
 				}
 				else {
 					$('#timestamp_'+graph_id).css('left', timenewpos);
+					$('#timestamp_'+graph_id).css('top', 50);
 				}
 			}
 			else {
@@ -1817,24 +1819,16 @@ function pandoraFlotArea(graph_id, values, labels, labels_long, legend,
 		plot.unhighlight();
 		if (item && item.series.label != '' && (item.series.label == legend_events || item.series.label == legend_events+series_suffix_str || item.series.label == legend_alerts || item.series.label == legend_alerts+series_suffix_str)) {
 			plot.unhighlight();
-			var canvaslimit = parseInt(plot.offset().left + plot.width());
 			var dataset  = plot.getData();
-			var timenewpos = parseInt(dataset[0].xaxis.p2c(pos.x)+plot.offset().left);
-			var extrasize = parseInt($('#extra_'+graph_id).css('width').split('px')[0]);
-
-			var left_pos;
-			if (extrasize+timenewpos > canvaslimit) {
-				left_pos = timenewpos - extrasize - 20;
-			}
-			else {
-				left_pos = timenewpos - (extrasize / 2);
-			}
 
 			var extra_info = '<i>No info to show</i>';
 			var extra_show = false;
 
-			$('#extra_'+graph_id).css('left',left_pos);
-			$('#extra_'+graph_id).css('top',plot.offset().top + 25);
+			var coord_x = (item.dataIndex/item.series.xaxis.datamax)* (event.target.clientWidth - event.target.offsetLeft + 1) + event.target.offsetLeft;
+
+
+			$('#extra_'+graph_id).css('left',coord_x);
+			$('#extra_'+graph_id).css('top', event.target.offsetTop + 55 );
 
 			switch(item.series.label) {
 				case legend_alerts+series_suffix_str:
diff --git a/pandora_console/operation/agentes/stat_win.php b/pandora_console/operation/agentes/stat_win.php
index ba7965fd63..f0fdf37082 100644
--- a/pandora_console/operation/agentes/stat_win.php
+++ b/pandora_console/operation/agentes/stat_win.php
@@ -111,8 +111,6 @@ $alias = db_get_value ("alias","tagente","id_agente",$id_agent);
 		$id = (int) get_parameter ("id", 0);
 		// Agent id
 		$agent_id = (int) modules_get_agentmodule_agent($id);
-		// Kind module
-		$type_module = modules_get_agentmodule_kind($id);
 		
 		if (empty($id) || empty($agent_id)) {
 			ui_print_error_message(__('There was a problem locating the source of the graph'));
@@ -171,7 +169,6 @@ $alias = db_get_value ("alias","tagente","id_agente",$id_agent);
 		$time_compare_overlapped = get_parameter ("time_compare_overlapped", 0);
 		$unknown_graph = get_parameter_checkbox ("unknown_graph", 1);
 
-		//$type_module == 'predictionserver';
 		$fullscale_sent = get_parameter ("fullscale_sent", 0);
 		if(!$fullscale_sent){
 			if(!isset($config['full_scale_option']) || $config['full_scale_option'] == 0){
@@ -423,14 +420,12 @@ $alias = db_get_value ("alias","tagente","id_agente",$id_agent);
 				break;
 		}
 
-		if($type_module != 'predictionserver'){
-			$data = array();
-			$data[0] = __('Show full scale graph (TIP)');
-			$data[1] = html_print_checkbox ("fullscale", 1, (bool) $fullscale, 
-									true, false);
-			$table->data[] = $data;
-			$table->rowclass[] = '';
-		}
+		$data = array();
+		$data[0] = __('Show full scale graph (TIP)');
+		$data[1] = html_print_checkbox ("fullscale", 1, (bool) $fullscale, 
+								true, false);
+		$table->data[] = $data;
+		$table->rowclass[] = '';
 		
 		$form_table = html_print_table($table, true);