[Piwik-svn] r457 - in trunk: . modules modules/LogStats

svnmaster at piwik.org svnmaster at piwik.org
Tue May 6 23:01:12 CEST 2008


Author: matt
Date: 2008-05-06 23:01:04 +0200 (Tue, 06 May 2008)
New Revision: 457

Modified:
   trunk/modules/ArchiveProcessing.php
   trunk/modules/LogStats.php
   trunk/modules/LogStats/Db.php
   trunk/modules/Piwik.php
   trunk/piwik.php
Log:
- when database is unavailable, piwik.php display a gif 1*1 
- fixed profiling in piwik.php. Enable profiling by turning DEBUG = true in /piwik.php

Modified: trunk/modules/ArchiveProcessing.php
===================================================================
--- trunk/modules/ArchiveProcessing.php	2008-05-05 21:03:43 UTC (rev 456)
+++ trunk/modules/ArchiveProcessing.php	2008-05-06 21:01:04 UTC (rev 457)
@@ -253,9 +253,7 @@
 		{
 			if($this->period->isFinished())
 			{
-//				echo "<br>date end = ".$this->period->getDateEnd();
 				$this->maxTimestampArchive = $this->period->getDateEnd()->setTime('00:00:00')->addDay(1)->getTimestamp();
-//				echo "<br>max = ". date("Y-m-d H:i:s",$this->maxTimestampArchive);
 			}
 			else
 			{
@@ -281,24 +279,15 @@
 		if($this->idArchive === false
 			|| $this->debugAlwaysArchive)
 		{
-//			Piwik::printMemoryUsage('Before loading subperiods');
 			$this->archivesSubperiods = $this->loadSubperiodsArchive();
-//			Piwik::printMemoryUsage('After loading subperiods');
 			$this->initCompute();
-//			Piwik::printMemoryUsage('After init compute');
 			$this->compute();
-//			Piwik::printMemoryUsage('After compute');
 			$this->postCompute();
-//			Piwik::printMemoryUsage('After post compute');
-
 			// we execute again the isArchived that does some initialization work
 			$this->idArchive = $this->isArchived();
-			
-//			Piwik::log("New archive computed, id = {$this->idArchive}");
 		}
 		else
 		{
-			//Piwik::log("Archive already available, id = {$this->idArchive}");
 			$this->isThereSomeVisits = true;
 		}
 		

Modified: trunk/modules/LogStats/Db.php
===================================================================
--- trunk/modules/LogStats/Db.php	2008-05-05 21:03:43 UTC (rev 456)
+++ trunk/modules/LogStats/Db.php	2008-05-06 21:01:04 UTC (rev 457)
@@ -19,13 +19,13 @@
 
 class Piwik_LogStats_Db 
 {
-	private $connection;
+	private $connection = null;
 	private $username;
 	private $password;
 	
 	static private $profiling = false;
 
-	protected $queriesProfiling;
+	protected $queriesProfiling = array();
 	
 	/**
 	 * Builds the DB object
@@ -76,22 +76,24 @@
 	 */
 	public function connect() 
 	{
-		try {
-			$pdoConnect = new PDO($this->dsn, $this->username, $this->password);
-			$pdoConnect->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
-			$this->connection = $pdoConnect;
+		$pdoConnect = new PDO($this->dsn, $this->username, $this->password);
+		$pdoConnect->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
+		$this->connection = $pdoConnect;
 
-			// we erase the password from this object "just in case" it could be printed 
-			$this->password = '';
-		} catch (PDOException $e) {
-			
-			// we erase the password from this object "just in case" it could be printed 
-			$this->password = '';
-			
-			// we don't throw the PDO Exception directly as it may contain sensitive information
-			throw new Exception("Error connecting database: ".$e->getMessage());
-		}
+		// we delete the password from this object "just in case" it could be printed 
+		$this->password = '';
 	}
+	
+	/**
+	 * Disconnects from the Mysql server
+	 *
+	 * @return void
+	 */
+	public function disconnect()
+	{
+		$this->recordProfiling();
+		$this->connection = null;
+	}
 
 	/**
 	 * Returns the table name prefixed by the table prefix.
@@ -120,6 +122,10 @@
 	{
 		try {
 			$sth = $this->query( $query, $parameters );
+			if($sth === false)
+			{
+				return false;
+			}
 			return $sth->fetchAll(PDO::FETCH_ASSOC);
 		} catch (PDOException $e) {
 			throw new Exception("Error query: ".$e->getMessage());
@@ -139,7 +145,11 @@
 	{
 		try {
 			$sth = $this->query( $query, $parameters );
-			return $sth->fetch(PDO::FETCH_ASSOC);
+			if($sth === false)
+			{
+				return false;
+			}
+			return $sth->fetch(PDO::FETCH_ASSOC);			
 		} catch (PDOException $e) {
 			throw new Exception("Error query: ".$e->getMessage());
 		}
@@ -151,12 +161,16 @@
 	 * @param string Query 
 	 * @param array Parameters to bind
 	 * 
+	 * @return PDOStatement or false if failed
 	 * @throw Exception if an exception occured
 	 */
 	public function query($query, $parameters = array()) 
 	{
-		try {
-			
+		if(is_null($this->connection))
+		{
+			return false;
+		}
+		try {			
 			if(self::$profiling)
 			{
 				require_once "Timer.php";
@@ -189,16 +203,21 @@
 	 */
 	public function lastInsertId()
 	{
-		return  $this->connection->lastInsertId();
+		return $this->connection->lastInsertId();
 	}
 	
 	/**
 	 * When destroyed, if SQL profiled enabled, logs the SQL profiling information
 	 */
-	public function __destruct()
+	public function recordProfiling()
 	{
 		if(self::$profiling)
 		{
+			if(is_null($this->connection)) 
+			{
+				return;
+			}
+		
 			// turn off the profiler so we don't profile the following queries 
 			self::$profiling = false;
 			

Modified: trunk/modules/LogStats.php
===================================================================
--- trunk/modules/LogStats.php	2008-05-05 21:03:43 UTC (rev 456)
+++ trunk/modules/LogStats.php	2008-05-06 21:01:04 UTC (rev 457)
@@ -50,6 +50,10 @@
 	
 	protected $urlToRedirect;
 	
+	/**
+	 *
+	 * @var Piwik_LogStats_Db
+	 */
 	protected $db = null;
 	
 	const STATE_NOTHING_TO_NOTICE = 1;
@@ -82,7 +86,8 @@
 										$configDb['username'], 
 										$configDb['password'], 
 										$configDb['dbname']
-							);  
+							);
+							  
 		$this->db->connect();
 	}
 
@@ -179,9 +184,13 @@
 		
 		if( $this->processVisit() )
 		{
-			$this->connectDatabase();
-			$visit = $this->getNewVisitObject();
-			$visit->handle();
+			try {
+				$this->connectDatabase();
+				$visit = $this->getNewVisitObject();
+				$visit->handle();
+			} catch (PDOException $e) {
+				$this->setState(self::STATE_LOGGING_DISABLE);
+			}				
 		}
 		$this->endProcess();
 	}	
@@ -217,6 +226,16 @@
 			break;
 		}
 		printDebug("End of the page.");
+		
+		if($GLOBALS['DEBUGPIWIK'] === true)
+		{
+			Piwik::printLogStatsSQLProfiling($this->db);
+		}
+		
+		if(isset($this->db))
+		{
+			$this->db->disconnect();
+		}
 	}
 	
 	protected function outputTransparentGif()

Modified: trunk/modules/Piwik.php
===================================================================
--- trunk/modules/Piwik.php	2008-05-05 21:03:43 UTC (rev 456)
+++ trunk/modules/Piwik.php	2008-05-06 21:01:04 UTC (rev 457)
@@ -212,17 +212,28 @@
 		Piwik::log("Total queries = $queryCount (total sql time = ".round($totalTime,2)."s)");
 	}
 	
-	static public function printLogStatsSQLProfiling()
+	static public function printLogStatsSQLProfiling( $db = null )
 	{
 		function maxSumMsFirst($a,$b)
 		{
 			return $a['sum_time_ms'] < $b['sum_time_ms'];
 		}
 		
-		$db = Zend_Registry::get('db');
+		if(is_null($db))
+		{
+			$db = Zend_Registry::get('db');
+			$tableName = Piwik::prefixTable('log_profiling');
+		}
+		else
+		{
+			$tableName = $db->prefixTable('log_profiling');
+		}
 		$all = $db->fetchAll('	SELECT *, sum_time_ms / count as avg_time_ms 
-								FROM '.Piwik::prefixTable('log_profiling') 
-						);
+								FROM '.$tableName );
+		if($all === false) 
+		{
+			return;
+		}
 		usort($all, 'maxSumMsFirst');
 		
 		$str='<br><br>Query Profiling<br>----------------------<br>';

Modified: trunk/piwik.php
===================================================================
--- trunk/piwik.php	2008-05-05 21:03:43 UTC (rev 456)
+++ trunk/piwik.php	2008-05-06 21:01:04 UTC (rev 457)
@@ -46,7 +46,7 @@
 require_once "LogStats/Db.php";
 require_once "LogStats/Visit.php";
 
-$GLOBALS['DEBUGPIWIK'] =  false;
+$GLOBALS['DEBUGPIWIK'] = false;
 
 if($GLOBALS['DEBUGPIWIK'] === true)
 {	
@@ -56,11 +56,15 @@
 	set_error_handler('Piwik_ErrorHandler');
 	set_exception_handler('Piwik_ExceptionHandler');
 	printDebug($_GET);
+	Piwik_LogStats_Db::enableProfiling();
+	Piwik::createConfigObject();
+	Piwik::createLogObject();
 }
 
 ob_start();
 $process = new Piwik_LogStats;
 $process->main();
 ob_end_flush();
+
 printDebug($_COOKIE);
 



More information about the Piwik-svn mailing list