[Piwik-svn] r536 - in trunk: . modules modules/ArchiveProcessing modules/Period
svnmaster at piwik.org
svnmaster at piwik.org
Fri Jun 27 03:32:27 CEST 2008
Author: matt
Date: 2008-06-27 03:32:25 +0200 (Fri, 27 Jun 2008)
New Revision: 536
Added:
trunk/modules/Period/
trunk/modules/Period/Day.php
trunk/modules/Period/Month.php
trunk/modules/Period/Range.php
trunk/modules/Period/Week.php
trunk/modules/Period/Year.php
Modified:
trunk/TODO
trunk/modules/ArchiveProcessing.php
trunk/modules/ArchiveProcessing/Period.php
trunk/modules/Period.php
Log:
- cleaning/refactoring before bug fixing
Modified: trunk/TODO
===================================================================
--- trunk/TODO 2008-06-26 23:52:19 UTC (rev 535)
+++ trunk/TODO 2008-06-27 01:32:25 UTC (rev 536)
@@ -12,14 +12,11 @@
API request shouldnt be SO DataTable aware
the logic of printing out nice datatable should be in some datattable class
-
ACTIONS limit seem not right, seem limited to 50 instead of 200 for root
put limit configuration in config files
rename "Others" on a per API method basis "Other keywords" "Other websites" otherwise it's not clear at all
merge config file instead of annoying file selection?
-see if can remove translationAvailable in all plugins
-ECLIPSE WINDOWS LINE BREAKS
deactivate online logs
Modified: trunk/modules/ArchiveProcessing/Period.php
===================================================================
--- trunk/modules/ArchiveProcessing/Period.php 2008-06-26 23:52:19 UTC (rev 535)
+++ trunk/modules/ArchiveProcessing/Period.php 2008-06-27 01:32:25 UTC (rev 536)
@@ -174,19 +174,43 @@
foreach($this->archives as $archive)
{
$archive->preFetchBlob($name);
-
$datatableToSum = $archive->getDataTable($name);
-
$archive->loadSubDataTables($name, $datatableToSum);
-
$table->addDataTable($datatableToSum);
-
$archive->freeBlob($name);
}
return $table;
}
+ protected function initCompute()
+ {
+ parent::initCompute();
+ $this->archives = $this->loadSubperiodsArchive();
+ }
+
/**
+ * Returns the ID of the archived subperiods.
+ *
+ * @return array Array of the idArchive of the subperiods
+ */
+ protected function loadSubperiodsArchive()
+ {
+ $periods = array();
+
+ // we first compute every subperiod of the archive
+ foreach($this->period->getSubperiods() as $period)
+ {
+ $archivePeriod = new Piwik_Archive_Single;
+ $archivePeriod->setSite( $this->site );
+ $archivePeriod->setPeriod( $period );
+ $archivePeriod->prepareArchive();
+
+ $periods[] = $archivePeriod;
+ }
+ return $periods;
+ }
+
+ /**
* Main method to process logs for a period.
* The only logic done here is computing the number of visits, actions, etc.
*
@@ -197,8 +221,6 @@
*/
protected function compute()
{
- $this->archives = $this->archivesSubperiods;
-
$this->archiveNumericValuesMax( 'max_actions' );
$toSum = array(
'nb_uniq_visitors',
@@ -210,7 +232,6 @@
$record = $this->archiveNumericValuesSum($toSum);
$this->isThereSomeVisits = ($record['nb_visits']->value != 0);
-
if($this->isThereSomeVisits === false)
{
return;
Modified: trunk/modules/ArchiveProcessing.php
===================================================================
--- trunk/modules/ArchiveProcessing.php 2008-06-26 23:52:19 UTC (rev 535)
+++ trunk/modules/ArchiveProcessing.php 2008-06-27 01:32:25 UTC (rev 536)
@@ -300,7 +300,6 @@
protected function launchArchiving()
{
$this->initCompute();
- $this->archivesSubperiods = $this->loadSubperiodsArchive();
$this->compute();
$this->postCompute();
// we execute again the isArchived that does some initialization work
@@ -474,28 +473,6 @@
}
/**
- * Returns the ID of the archived subperiods.
- *
- * @return array Array of the idArchive of the subperiods
- */
- protected function loadSubperiodsArchive()
- {
- $periods = array();
-
- // we first compute every subperiod of the archive
- foreach($this->period->getSubperiods() as $period)
- {
- $archivePeriod = new Piwik_Archive_Single;
- $archivePeriod->setSite( $this->site );
- $archivePeriod->setPeriod( $period );
- $archivePeriod->prepareArchive();
-
- $periods[] = $archivePeriod;
- }
- return $periods;
- }
-
- /**
* Returns the idArchive if the archive is available in the database.
* Returns false if the archive needs to be computed.
*
Added: trunk/modules/Period/Day.php
===================================================================
--- trunk/modules/Period/Day.php (rev 0)
+++ trunk/modules/Period/Day.php 2008-06-27 01:32:25 UTC (rev 536)
@@ -0,0 +1,40 @@
+<?php
+
+/**
+ *
+ * @package Piwik_Period
+ */
+class Piwik_Period_Day extends Piwik_Period
+{
+ protected $label = 'day';
+
+ public function getPrettyString()
+ {
+ $out = $this->getDateStart()->toString() ;
+ return $out;
+ }
+
+ public function isFinished()
+ {
+ $todayMidnight = Piwik_Date::today();
+ if($this->date->isEarlier($todayMidnight))
+ {
+ return true;
+ }
+ }
+
+ public function getNumberOfSubperiods()
+ {
+ return 0;
+ }
+
+ public function addSubperiod( $date )
+ {
+ throw new Exception("Adding a subperiod is not supported for Piwik_Period_Day");
+ }
+
+ public function toString()
+ {
+ return $this->date->toString("Y-m-d");
+ }
+}
Added: trunk/modules/Period/Month.php
===================================================================
--- trunk/modules/Period/Month.php (rev 0)
+++ trunk/modules/Period/Month.php 2008-06-27 01:32:25 UTC (rev 536)
@@ -0,0 +1,48 @@
+<?php
+/**
+ *
+ * @package Piwik_Period
+ */
+class Piwik_Period_Month extends Piwik_Period
+{
+ protected $label = 'month';
+
+ public function getPrettyString()
+ {
+ $out = $this->getDateStart()->toString('Y-m');
+ return $out;
+ }
+
+ protected function generate()
+ {
+ if($this->subperiodsProcessed)
+ {
+ return;
+ }
+ parent::generate();
+
+ $date = $this->date;
+
+ $startMonth = $date->setDay(1);
+ $currentDay = clone $startMonth;
+ while($currentDay->compareMonth($startMonth) == 0)
+ {
+ $this->addSubperiod(new Piwik_Period_Day($currentDay));
+ $currentDay = $currentDay->addDay(1);
+ }
+ }
+
+ public function isFinished()
+ {
+ if(!$this->subperiodsProcessed)
+ {
+ $this->generate();
+ }
+ // a month is finished
+ // if current month > month AND current year == year
+ // OR if current year > year
+ $year = $this->date->get("Y");
+ return ( date("m") > $this->date->get("m") && date("Y") == $year)
+ || date("Y") > $year;
+ }
+}
Added: trunk/modules/Period/Range.php
===================================================================
--- trunk/modules/Period/Range.php (rev 0)
+++ trunk/modules/Period/Range.php 2008-06-27 01:32:25 UTC (rev 536)
@@ -0,0 +1,158 @@
+<?php
+
+/**
+ * from a starting date to an ending date
+ *
+ */
+class Piwik_Period_Range extends Piwik_Period
+{
+ public function __construct( $strPeriod, $strDate )
+ {
+ $this->strPeriod = $strPeriod;
+ $this->strDate = $strDate;
+ $this->defaultEndDate = null;
+ }
+
+ public function getPrettyString()
+ {
+ $out = "From ".$this->getDateStart()->toString() . " to " . $this->getDateEnd()->toString();
+ return $out;
+ }
+
+ protected function removePeriod( $date, $n )
+ {
+ switch($this->strPeriod)
+ {
+ case 'day':
+ $startDate = $date->subDay( $n );
+ break;
+
+ case 'week':
+ $startDate = $date->subDay( $n * 7 );
+ break;
+
+ case 'month':
+ $startDate = $date->subMonth( $n );
+ break;
+
+ case 'year':
+ $startDate = $date->subMonth( 12 * $n );
+ break;
+
+ default:
+ throw new Exception(sprintf(self::$unknowPeriodException, $this->strPeriod));
+ break;
+ }
+ return $startDate;
+ }
+
+ protected function getMaxN($lastN)
+ {
+ switch($this->strPeriod)
+ {
+ case 'day':
+ $lastN = min( $lastN, 5*365 );
+ break;
+
+ case 'week':
+ $lastN = min( $lastN, 5*52 );
+ break;
+
+ case 'month':
+ $lastN = min( $lastN, 5*12 );
+ break;
+
+ case 'year':
+ $lastN = min( $lastN, 10 );
+ break;
+ }
+ return $lastN;
+ }
+ public function setDefaultEndDate( Piwik_Date $oDate)
+ {
+ $this->defaultEndDate = $oDate;
+ }
+
+ protected function generate()
+ {
+ if($this->subperiodsProcessed)
+ {
+ return;
+ }
+ parent::generate();
+
+ if(ereg('(last|previous)([0-9]*)', $this->strDate, $regs))
+ {
+ $lastN = $regs[2];
+
+ $lastOrPrevious = $regs[1];
+
+ if(!is_null($this->defaultEndDate))
+ {
+ $defaultEndDate = $this->defaultEndDate;
+ }
+ else
+ {
+ $defaultEndDate = Piwik_Date::today();
+ }
+ if($lastOrPrevious == 'last')
+ {
+ $endDate = $defaultEndDate;
+ }
+ elseif($lastOrPrevious == 'previous')
+ {
+ $endDate = $this->removePeriod($defaultEndDate, 1);
+ }
+
+ // last1 means only one result ; last2 means 2 results so we remove only 1 to the days/weeks/etc
+ $lastN--;
+ $lastN = abs($lastN);
+
+ $lastN = $this->getMaxN($lastN);
+
+ $startDate = $this->removePeriod($endDate, $lastN);
+ }
+ elseif(ereg('([0-9]{4}-[0-9]{1,2}-[0-9]{1,2}),([0-9]{4}-[0-9]{1,2}-[0-9]{1,2})', $this->strDate, $regs))
+ {
+ $strDateStart = $regs[1];
+ $strDateEnd = $regs[2];
+
+ $startDate = new Piwik_Date($strDateStart);
+ $endDate = new Piwik_Date($strDateEnd);
+ }
+ else
+ {
+ throw new Exception("The date '$this->strDate' is not a date range. Should have the following format: 'lastN' or 'previousN' or 'YYYY-MM-DD,YYYY-MM-DD'.");
+ }
+
+ $endSubperiod = Piwik_Period::factory($this->strPeriod, $endDate);
+
+ $arrayPeriods= array();
+ $arrayPeriods[] = $endSubperiod;
+ while($endDate->isLater($startDate))
+ {
+ $endDate = $this->removePeriod($endDate, 1);
+ $subPeriod = Piwik_Period::factory($this->strPeriod, $endDate);
+ $arrayPeriods[] = $subPeriod ;
+ }
+ $arrayPeriods = array_reverse($arrayPeriods);
+ foreach($arrayPeriods as $period)
+ {
+ $this->addSubperiod($period);
+ }
+ }
+
+ function toString()
+ {
+ if(!$this->subperiodsProcessed)
+ {
+ $this->generate();
+ }
+ $range = array();
+ foreach($this->subperiods as $element)
+ {
+ $range[] = $element->toString();
+ }
+ return $range;
+ }
+}
\ No newline at end of file
Added: trunk/modules/Period/Week.php
===================================================================
--- trunk/modules/Period/Week.php (rev 0)
+++ trunk/modules/Period/Week.php 2008-06-27 01:32:25 UTC (rev 536)
@@ -0,0 +1,41 @@
+<?php
+
+/**
+ *
+ * @package Piwik_Period
+ */
+class Piwik_Period_Week extends Piwik_Period
+{
+ protected $label = 'week';
+
+ public function getPrettyString()
+ {
+ $out = $this->getDateStart()->toString() . " to " . $this->getDateEnd()->toString();
+ return $out;
+ }
+
+ protected function generate()
+ {
+ if($this->subperiodsProcessed)
+ {
+ return;
+ }
+ parent::generate();
+ $date = $this->date;
+
+ if( $date->toString('N') > 1)
+ {
+ $date = $date->subDay($date->toString('N')-1);
+ }
+
+ $startWeek = $date;
+
+ $currentDay = clone $startWeek;
+ while($currentDay->compareWeek($startWeek) == 0)
+ {
+ $this->addSubperiod(new Piwik_Period_Day($currentDay) );
+ $currentDay = $currentDay->addDay(1);
+ }
+ }
+
+}
Added: trunk/modules/Period/Year.php
===================================================================
--- trunk/modules/Period/Year.php (rev 0)
+++ trunk/modules/Period/Year.php 2008-06-27 01:32:25 UTC (rev 536)
@@ -0,0 +1,49 @@
+<?php
+
+require_once "Period/Month.php";
+/**
+ *
+ * @package Piwik_Period
+ */
+class Piwik_Period_Year extends Piwik_Period
+{
+ protected $label = 'year';
+
+ public function getPrettyString()
+ {
+ $out = $this->getDateStart()->toString('Y');
+ return $out;
+ }
+
+ protected function generate()
+ {
+ if($this->subperiodsProcessed)
+ {
+ return;
+ }
+ parent::generate();
+
+ $year = $this->date->get("Y");
+ for($i=1; $i<=12; $i++)
+ {
+ $this->addSubperiod( new Piwik_Period_Month(
+ new Piwik_Date("$year-$i-01")
+ )
+ );
+ }
+ }
+
+ function toString()
+ {
+ if(!$this->subperiodsProcessed)
+ {
+ $this->generate();
+ }
+ $stringMonth = array();
+ foreach($this->subperiods as $month)
+ {
+ $stringMonth[] = $month->get("Y")."-".$month->get("m")."-01";
+ }
+ return $stringMonth;
+ }
+}
Modified: trunk/modules/Period.php
===================================================================
--- trunk/modules/Period.php 2008-06-26 23:52:19 UTC (rev 535)
+++ trunk/modules/Period.php 2008-06-27 01:32:25 UTC (rev 536)
@@ -9,6 +9,7 @@
* @package Piwik_Helper
*/
+require_once "Period/Day.php";
/**
* Creating a new Piwik_Period subclass:
*
@@ -28,8 +29,10 @@
protected $subperiods = array();
protected $subperiodsProcessed = false;
protected $label = null;
+ protected $date = null;
protected static $unknowPeriodException = "The period '%s' is not supported. Try 'day' or 'week' or 'month' or 'year'";
+
protected function __construct( $date )
{
$this->checkInputDate( $date );
@@ -44,14 +47,17 @@
break;
case 'week':
+ require_once "Period/Week.php";
return new Piwik_Period_Week($date);
break;
case 'month':
+ require_once "Period/Month.php";
return new Piwik_Period_Month($date);
break;
case 'year':
+ require_once "Period/Year.php";
return new Piwik_Period_Year($date);
break;
@@ -156,6 +162,7 @@
* Returns Period_Day for a period made of days (week, month),
* Period_Month for a period made of months (year)
*
+ * @return array
*/
public function getSubperiods()
{
@@ -211,6 +218,11 @@
return $dateString;
}
+ public function __toString()
+ {
+ return $this->toString();
+ }
+
public function get( $part= null )
{
if(!$this->subperiodsProcessed)
@@ -223,350 +235,4 @@
abstract public function getPrettyString();
}
-/**
- * from a starting date to an ending date
- *
- */
-class Piwik_Period_Range extends Piwik_Period
-{
- public function __construct( $strPeriod, $strDate )
- {
- $this->strPeriod = $strPeriod;
- $this->strDate = $strDate;
- $this->defaultEndDate = null;
- }
-
- public function getPrettyString()
- {
- $out = "From ".$this->getDateStart()->toString() . " to " . $this->getDateEnd()->toString();
- return $out;
- }
-
- protected function removePeriod( $date, $n )
- {
- switch($this->strPeriod)
- {
- case 'day':
- $startDate = $date->subDay( $n );
- break;
-
- case 'week':
- $startDate = $date->subDay( $n * 7 );
- break;
-
- case 'month':
- $startDate = $date->subMonth( $n );
- break;
-
- case 'year':
- $startDate = $date->subMonth( 12 * $n );
- break;
-
- default:
- throw new Exception(sprintf(self::$unknowPeriodException, $this->strPeriod));
- break;
- }
- return $startDate;
- }
-
- protected function getMaxN($lastN)
- {
- switch($this->strPeriod)
- {
- case 'day':
- $lastN = min( $lastN, 5*365 );
- break;
-
- case 'week':
- $lastN = min( $lastN, 5*52 );
- break;
-
- case 'month':
- $lastN = min( $lastN, 5*12 );
- break;
-
- case 'year':
- $lastN = min( $lastN, 10 );
- break;
- }
- return $lastN;
- }
- public function setDefaultEndDate( Piwik_Date $oDate)
- {
- $this->defaultEndDate = $oDate;
- }
- protected function generate()
- {
- if($this->subperiodsProcessed)
- {
- return;
- }
- parent::generate();
-
- if(ereg('(last|previous)([0-9]*)', $this->strDate, $regs))
- {
- $lastN = $regs[2];
-
- $lastOrPrevious = $regs[1];
-
- if(!is_null($this->defaultEndDate))
- {
- $defaultEndDate = $this->defaultEndDate;
- }
- else
- {
- $defaultEndDate = Piwik_Date::today();
- }
- if($lastOrPrevious == 'last')
- {
- $endDate = $defaultEndDate;
- }
- elseif($lastOrPrevious == 'previous')
- {
- $endDate = $this->removePeriod($defaultEndDate, 1);
- }
-
- // last1 means only one result ; last2 means 2 results so we remove only 1 to the days/weeks/etc
- $lastN--;
- $lastN = abs($lastN);
-
- $lastN = $this->getMaxN($lastN);
-
- $startDate = $this->removePeriod($endDate, $lastN);
- }
- elseif(ereg('([0-9]{4}-[0-9]{1,2}-[0-9]{1,2}),([0-9]{4}-[0-9]{1,2}-[0-9]{1,2})', $this->strDate, $regs))
- {
- $strDateStart = $regs[1];
- $strDateEnd = $regs[2];
-
- $startDate = new Piwik_Date($strDateStart);
- $endDate = new Piwik_Date($strDateEnd);
- }
- else
- {
- throw new Exception("The date '$this->strDate' is not a date range. Should have the following format: 'lastN' or 'previousN' or 'YYYY-MM-DD,YYYY-MM-DD'.");
- }
-
- $endSubperiod = Piwik_Period::factory($this->strPeriod, $endDate);
-
- $arrayPeriods= array();
- $arrayPeriods[] = $endSubperiod;
- while($endDate->isLater($startDate))
- {
- $endDate = $this->removePeriod($endDate, 1);
- $subPeriod = Piwik_Period::factory($this->strPeriod, $endDate);
- $arrayPeriods[] = $subPeriod ;
- }
- $arrayPeriods = array_reverse($arrayPeriods);
- foreach($arrayPeriods as $period)
- {
- $this->addSubperiod($period);
- }
-// var_dump($this->toString());exit;
- }
-
- function toString()
- {
- if(!$this->subperiodsProcessed)
- {
- $this->generate();
- }
- $range = array();
- foreach($this->subperiods as $element)
- {
- $range[] = $element->toString();
- }
- return $range;
- }
-}
-
-/**
- *
- * @package Piwik_Period
- */
-class Piwik_Period_Day extends Piwik_Period
-{
- protected $label = 'day';
- protected $date = null;
-
- public function __construct( $date )
- {
- parent::__construct($date);
- }
-
- public function getPrettyString()
- {
- $out = $this->getDateStart()->toString() ;
- return $out;
- }
- public function isFinished()
- {
- $todayMidnight = Piwik_Date::today();
- if($this->date->isEarlier($todayMidnight))
- {
- return true;
- }
- }
-
- public function getNumberOfSubperiods()
- {
- return 0;
- }
-
- public function addSubperiod( $date )
- {
- throw new Exception("Adding a subperiod is not supported for Piwik_Period_Day");
- }
- public function toString()
- {
- return $this->date->toString("Y-m-d");
- }
-
-
-}
-
-/**
- *
- * @package Piwik_Period
- */
-class Piwik_Period_Week extends Piwik_Period
-{
- protected $label = 'week';
- public function __construct( $date )
- {
- parent::__construct($date);
- }
-
- public function getPrettyString()
- {
- $out = $this->getDateStart()->toString() . " to " . $this->getDateEnd()->toString();
- return $out;
- }
- protected function generate()
- {
- if($this->subperiodsProcessed)
- {
- return;
- }
- parent::generate();
- $date = $this->date;
-
- if( $date->toString('N') > 1)
- {
- $date = $date->subDay($date->toString('N')-1);
- }
-
- $startWeek = $date;
-
- $currentDay = clone $startWeek;
- while($currentDay->compareWeek($startWeek) == 0)
- {
- $this->addSubperiod(new Piwik_Period_Day($currentDay) );
- $currentDay = $currentDay->addDay(1);
- }
- }
-
-}
-
-/**
- *
- * @package Piwik_Period
- */
-class Piwik_Period_Month extends Piwik_Period
-{
- protected $label = 'month';
- public function __construct( $date )
- {
- parent::__construct($date);
- }
-
- public function getPrettyString()
- {
- $out = $this->getDateStart()->toString('Y-m');
- return $out;
- }
- protected function generate()
- {
- if($this->subperiodsProcessed)
- {
- return;
- }
- parent::generate();
-
- $date = $this->date;
-
- $startMonth = $date->setDay(1);
- $currentDay = clone $startMonth;
- while($currentDay->compareMonth($startMonth) == 0)
- {
- $this->addSubperiod(new Piwik_Period_Day($currentDay));
- $currentDay = $currentDay->addDay(1);
- }
- }
-
- public function isFinished()
- {
- if(!$this->subperiodsProcessed)
- {
- $this->generate();
- }
- // a month is finished
- // if current month > month AND current year == year
- // OR if current year > year
- $year = $this->date->get("Y");
- return ( date("m") > $this->date->get("m") && date("Y") == $year)
- || date("Y") > $year;
- }
-}
-
-/**
- *
- * @package Piwik_Period
- */
-class Piwik_Period_Year extends Piwik_Period
-{
- protected $label = 'year';
- public function __construct( $date )
- {
- parent::__construct($date);
- }
-
- public function getPrettyString()
- {
- $out = $this->getDateStart()->toString('Y');
- return $out;
- }
- protected function generate()
- {
- if($this->subperiodsProcessed)
- {
- return;
- }
- parent::generate();
-
- $year = $this->date->get("Y");
- for($i=1; $i<=12; $i++)
- {
- $this->addSubperiod( new Piwik_Period_Month(
- new Piwik_Date("$year-$i-01")
- )
- );
- }
- }
-
- function toString()
- {
- if(!$this->subperiodsProcessed)
- {
- $this->generate();
- }
- $stringMonth = array();
- foreach($this->subperiods as $month)
- {
- $stringMonth[] = $month->get("Y")."-".$month->get("m")."-01";
- }
- return $stringMonth;
- }
-}
-
More information about the Piwik-svn
mailing list