From e99f8d5d2382eb9813cd66f2d70c6c69483c6479 Mon Sep 17 00:00:00 2001 From: matt Date: Sun, 20 Jan 2008 17:31:22 +0000 Subject: - all graphs are now showing data ending on the date the user is currently selecting. - you can now use the format date=YYYY-MM-DD,YYYY-MM-DD in all APIs calls to request data for each period of the given range - fixed bug in Period.php that returned periods in reverse order - changed default font to georgia (test) --- modules/Archive.php | 8 +- modules/Period.php | 59 +++++++-- modules/ViewDataTable/Graph.php | 6 + modules/ViewDataTable/Sparkline.php | 16 +-- plugins/Home/Controller.php | 80 ++++++++++-- plugins/Home/templates/datatable.css | 21 +--- plugins/Home/templates/index.tpl | 67 +++++----- tests/modules/Period.test.php | 228 ++++++++++++++++++++++++++++++++++- 8 files changed, 403 insertions(+), 82 deletions(-) diff --git a/modules/Archive.php b/modules/Archive.php index 8ec1e9acbb..9eac608caa 100644 --- a/modules/Archive.php +++ b/modules/Archive.php @@ -60,12 +60,14 @@ abstract class Piwik_Archive $oSite = new Piwik_Site($idSite); if(is_string($oDate) - && ereg('^(last|previous){1}([0-9]*)$', $oDate, $regs)) + && ( + ereg('^(last|previous){1}([0-9]*)$', $oDate, $regs) + || ereg('^([0-9]{4}-[0-9]{1,2}-[0-9]{1,2}),([0-9]{4}-[0-9]{1,2}-[0-9]{1,2})$', $oDate, $regs) + ) + ) { require_once 'Archive/Array.php'; - $archive = new Piwik_Archive_Array($oSite, $period, $oDate); - } else { diff --git a/modules/Period.php b/modules/Period.php index 5222fbdf95..9fe0d2fda2 100644 --- a/modules/Period.php +++ b/modules/Period.php @@ -30,7 +30,7 @@ abstract class Piwik_Period protected $label = null; protected static $unknowPeriodException = "The period '%s' is not supported. Try 'day' or 'week' or 'month' or 'year'"; - public function __construct( $date ) + protected function __construct( $date ) { $this->checkInputDate( $date ); $this->date = clone $date; @@ -68,6 +68,10 @@ abstract class Piwik_Period */ public function getDateStart() { + if(!$this->subperiodsProcessed) + { + $this->generate(); + } if(count($this->subperiods) == 0) { return $this->getDate(); @@ -205,6 +209,10 @@ abstract class Piwik_Period public function get( $part= null ) { + if(!$this->subperiodsProcessed) + { + $this->generate(); + } return $this->date->get($part); } @@ -221,15 +229,15 @@ class Piwik_Period_Range extends Piwik_Period { $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) @@ -256,7 +264,7 @@ class Piwik_Period_Range extends Piwik_Period } return $startDate; } - + protected function getMaxN($lastN) { switch($this->strPeriod) @@ -279,13 +287,18 @@ class Piwik_Period_Range extends Piwik_Period } return $lastN; } + public function setDefaultEndDate( Piwik_Date $oDate) + { + $this->defaultEndDate = $oDate; + } + protected function generate() { if($this->subperiodsProcessed) { return; } - $this->subperiodsProcessed = true; + parent::generate(); if(ereg('(last|previous)([0-9]*)', $this->strDate, $regs)) { @@ -293,13 +306,21 @@ class Piwik_Period_Range extends Piwik_Period $lastOrPrevious = $regs[1]; + if(!is_null($this->defaultEndDate)) + { + $defaultEndDate = $this->defaultEndDate; + } + else + { + $defaultEndDate = Piwik_Date::today(); + } if($lastOrPrevious == 'last') { - $endDate = Piwik_Date::today(); + $endDate = $defaultEndDate; } elseif($lastOrPrevious == 'previous') { - $endDate = $this->removePeriod(Piwik_Date::today(), 1); + $endDate = $this->removePeriod($defaultEndDate, 1); } // last1 means only one result ; last2 means 2 results so we remove only 1 to the days/weeks/etc @@ -310,19 +331,33 @@ class Piwik_Period_Range extends Piwik_Period $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 $strDate seems incorrect"); + 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); - $this->addSubperiod($endSubperiod); - for($i = $lastN; $i > 0 ; $i--) + $arrayPeriods= array(); + $arrayPeriods[] = $endSubperiod; + while($endDate->isLater($startDate)) { $endDate = $this->removePeriod($endDate, 1); $subPeriod = Piwik_Period::factory($this->strPeriod, $endDate); - $this->addSubperiod( $subPeriod ); + $arrayPeriods[] = $subPeriod ; + } + $arrayPeriods = array_reverse($arrayPeriods); + foreach($arrayPeriods as $period) + { + $this->addSubperiod($period); } // var_dump($this->toString());exit; } diff --git a/modules/ViewDataTable/Graph.php b/modules/ViewDataTable/Graph.php index a509c0df9f..dcf0c040d4 100644 --- a/modules/ViewDataTable/Graph.php +++ b/modules/ViewDataTable/Graph.php @@ -19,6 +19,7 @@ abstract class Piwik_ViewDataTable_Graph extends Piwik_ViewDataTable protected $width = 400; protected $height = 250; + function init($currentControllerName, $currentControllerAction, $moduleNameAndMethod ) @@ -34,6 +35,11 @@ abstract class Piwik_ViewDataTable_Graph extends Piwik_ViewDataTable $this->parametersToModify = array( 'viewDataTable' => $this->valueParameterViewDataTable); } + public function setParametersToModify($array) + { + $this->parametersToModify = array_merge($this->parametersToModify, $array); + } + public function main() { if($this->mainAlreadyExecuted) diff --git a/modules/ViewDataTable/Sparkline.php b/modules/ViewDataTable/Sparkline.php index 6687984294..dd051f4732 100644 --- a/modules/ViewDataTable/Sparkline.php +++ b/modules/ViewDataTable/Sparkline.php @@ -56,7 +56,8 @@ class Piwik_Sparkline_Graph $sparkline = new Sparkline_Line(); // $sparkline->SetColorHtml('lineColor', '000000'); - $sparkline->SetColor('lineColor', 0,0,0); + $sparkline->SetColor('lineColor', 22,44,74); // dark blue +// $sparkline->SetColor('lineColor', 0,119,204); $sparkline->SetColorHtml('red', '#FF7F7F'); $sparkline->SetColorHtml('blue', '#55AAFF'); $sparkline->SetColorHtml('green', '#75BF7C'); @@ -90,16 +91,17 @@ class Piwik_Sparkline_Graph // $sparkline->SetYMin(0); $sparkline->SetPadding(2); // setpadding is additive - $sparkline->SetPadding(13, - 6 * strlen(" $last[1]"), + $sparkline->SetPadding(13,//font height + 3,//4 * (strlen("$last[1]")), 0, //imagefontheight(FONT_2), 0); - $sparkline->SetFeaturePoint($min[0]-1,$min[1]+2,'red', 5, $min[1], TEXT_TOP,FONT_2); - $sparkline->SetFeaturePoint($max[0]-1,$max[1],'green', 5, $max[1], TEXT_TOP,FONT_2); - $sparkline->SetFeaturePoint($last[0]-1, $last[1], 'blue',5, " $last[1]", TEXT_RIGHT,FONT_2); + $font = FONT_2; + $sparkline->SetFeaturePoint($min[0]-1,$min[1]+2,'red', 5, $min[1], TEXT_TOP,$font); + $sparkline->SetFeaturePoint($max[0]-1,$max[1], 'green', 5, $max[1], TEXT_TOP,$font); + $sparkline->SetFeaturePoint($last[0]-1, $last[1], 'blue',5);//, " $last[1]", TEXT_RIGHT,$font); $sparkline->SetLineSize(3); // for renderresampled, linesize is on virtual image - $sparkline->RenderResampled(130, 30, 'black'); + $sparkline->RenderResampled(100, 30, 'lineColor'); $this->sparkline = $sparkline; } diff --git a/plugins/Home/Controller.php b/plugins/Home/Controller.php index 164c8ae99e..0db1a018d8 100644 --- a/plugins/Home/Controller.php +++ b/plugins/Home/Controller.php @@ -24,6 +24,18 @@ class Piwik_Home_Controller extends Piwik_Controller { parent::__construct(); $this->currentControllerName = 'Home'; + + $this->strDate = Piwik_Common::getRequestVar('date'); + + // the date looks like YYYY-MM-DD we can build it + try{ + $this->date = Piwik_Date::factory($this->strDate); + $this->strDate = $this->date->toString(); + } catch(Exception $e){ + // the date looks like YYYY-MM-DD,YYYY-MM-DD or other format + // case the date looks like a range + $this->date = null; + } } function getDefaultAction() { @@ -41,9 +53,56 @@ class Piwik_Home_Controller extends Piwik_Controller echo $view->render(); } + /** + * + * @param array paramsToSet = array( 'date' => 'last50', 'viewDataTable' =>'sparkline' ) + */ + function getGraphParamsModified($paramsToSet = array()) + { + if(!isset($paramsToSet['range'])) + { + $range = 'last30'; + } + else + { + $range = $paramsToSet['range']; + } + + if(!isset($paramsToSet['date'])) + { + $endDate = $this->strDate; + } + else + { + $endDate = $paramsToSet['date']; + } + + if(!isset($paramsToSet['period'])) + { + $period = Piwik_Common::getRequestVar('period'); + } + else + { + $period = $paramsToSet['period']; + } + + $last30Relative = new Piwik_Period_Range($period, $range ); + + $last30Relative->setDefaultEndDate(new Piwik_Date($endDate)); + + $paramDate = $last30Relative->getDateStart()->toString() . "," . $last30Relative->getDateEnd()->toString(); + + $params = array_merge($paramsToSet , array( 'date' => $paramDate ) ); + + return $params; + } + function getUrlSparkline( $action ) { - $params = array('action' => $action, 'date' => 'last30', 'viewDataTable' => 'sparkline'); + $params = $this->getGraphParamsModified( + array( 'viewDataTable' => 'sparkline', + 'action' => $action) + ); $url = Piwik_Url::getCurrentQueryStringWithParametersModified($params); return $url; } @@ -53,6 +112,15 @@ class Piwik_Home_Controller extends Piwik_Controller require_once "ViewDataTable/Graph.php"; $view = Piwik_ViewDataTable::factory(null, 'graphEvolution'); $view->init( $this->currentControllerName, $currentControllerAction, $apiMethod ); + + // if the date is not yet a nicely formatted date range ie. YYYY-MM-DD,YYYY-MM-DD we build it + // otherwise the current controller action is being called with the good date format already so it's fine + // see constructor + if( !is_null($this->date)) + { + $view->setParametersToModify( $this->getGraphParamsModified( array('date'=>$this->strDate))); + } + return $view; } @@ -95,10 +163,8 @@ class Piwik_Home_Controller extends Piwik_Controller function index() { $view = new Piwik_View('Home/templates/index.tpl'); - - $oDate = Piwik_Date::factory(Piwik_Common::getRequestVar('date')); - $date = $oDate->toString(); - $view->date = $date; + + $view->date = $this->strDate; $view->period = Piwik_Common::getRequestVar('period'); $view->idSite = Piwik_Common::getRequestVar('idSite'); @@ -374,9 +440,7 @@ List of the public methods for the class Piwik_Actions_API function getLastDistinctKeywordsGraph( $fetch = false ) { - require_once "ViewDataTable/Graph.php"; - $view = Piwik_ViewDataTable::factory(null, 'graphEvolution'); - $view->init( $this->currentControllerName, __FUNCTION__, "Referers.getNumberOfDistinctKeywords" ); + $view = $this->getLastUnitGraph(__FUNCTION__, "Referers.getNumberOfDistinctKeywords"); return $this->renderView($view, $fetch); } diff --git a/plugins/Home/templates/datatable.css b/plugins/Home/templates/datatable.css index 2f6af91de8..9803a6a209 100644 --- a/plugins/Home/templates/datatable.css +++ b/plugins/Home/templates/datatable.css @@ -1,8 +1,3 @@ -* { - font-family: "Trebuchet MS", Verdana, Arial, Helvetica, sans-serif; - font-size:1em; -} - /* main data table */ table.dataTable th.columnSorted { @@ -50,7 +45,7 @@ table.dataTable td { background: #fff; } -table.dataTable td,table.dataTable td a { +table.dataTable td, table.dataTable td a { margin:0; text-decoration:none; color: #4f6b72; @@ -73,6 +68,10 @@ table.dataTable td.labelodd { background: #F7FAFA url(images/bullet2.gif) no-repeat; color: #797268; } +/* A link in a column in the DataTable */ +table.dataTable td #urlLink { + display:none; +} /* a datatable inside another datatable */ table.subDataTable img { @@ -132,12 +131,6 @@ table.subDataTable thead th { } /* misc SPAN and DIV */ - -/* A link in a column in the DataTable */ -table td #urlLink { - display:none; -} - #dataTablePages { color:grey; font-weight:bold; @@ -178,9 +171,6 @@ table td #urlLink { font-size:0.8em; color:#C3C6D8; } -div.subDataTable { - font-size:0.8em; -} #dataTableNext, #dataTablePrevious, #dataTableSearchPattern, #loadingDataTable { display:none; @@ -193,7 +183,6 @@ div.subDataTable { #loadingDataTable { font-size: 1em; - font-weight:bold; color:#193B6C; padding:0.5em; } \ No newline at end of file diff --git a/plugins/Home/templates/index.tpl b/plugins/Home/templates/index.tpl index 1bb15ee34d..863a3d746d 100644 --- a/plugins/Home/templates/index.tpl +++ b/plugins/Home/templates/index.tpl @@ -32,7 +32,10 @@ var minDateDay = {$minDateDay}; {literal} {/literal} +{literal} + +{/literal} + + Piwik # open source web analytics

@@ -197,7 +223,7 @@ tr td.label img.plusMinus {
- Loading data... + Loading data... @@ -207,32 +233,13 @@ tr td.label img.plusMinus {

Report

- {literal} - - {/literal} -

test -

{$nbVisits} visits

-

{$nbUniqVisitors} unique visitors

-

{$nbActions} actions (page views)

-

{$sumVisitLength|sumtime} total time spent by the visitors

-

{$maxActions} max actions

-

{$bounceCount} visitors have bounced (left the site directly)

+

{$nbVisits} visits

+

{$nbUniqVisitors} unique visitors

+

{$nbActions} actions (page views)

+

{$sumVisitLength|sumtime} total time spent by the visitors

+

{$maxActions} max actions

+

{$bounceCount} visitors have bounced (left the site directly)





diff --git a/tests/modules/Period.test.php b/tests/modules/Period.test.php index 3ab913fe98..ce8deb3c53 100644 --- a/tests/modules/Period.test.php +++ b/tests/modules/Period.test.php @@ -596,7 +596,8 @@ class Test_Piwik_Period extends UnitTestCase $correct=array( $today->toString(), ); - + $correct = array_reverse($correct); + $this->assertEqual( $range->getNumberOfSubperiods(), 1); $this->assertEqual( $range->isFinished(), false); $this->assertEqual( $range->toString(), $correct); @@ -612,7 +613,8 @@ class Test_Piwik_Period extends UnitTestCase $today->toString(), $today->subDay(1)->toString() ); - + $correct = array_reverse($correct); + $this->assertEqual( $range->getNumberOfSubperiods(), 2); $this->assertEqual( $range->isFinished(), false); $this->assertEqual( $range->toString(), $correct); @@ -629,7 +631,8 @@ class Test_Piwik_Period extends UnitTestCase { $correct[]=$today->subDay($i)->toString(); } - + $correct = array_reverse($correct); + $this->assertEqual( $range->getNumberOfSubperiods(), 50); $this->assertEqual( $range->isFinished(), false); $this->assertEqual( $range->toString(), $correct); @@ -646,12 +649,219 @@ class Test_Piwik_Period extends UnitTestCase { $correct[]=$yesterday->subDay($i)->toString(); } - + $correct = array_reverse($correct); + $this->assertEqual( $range->getNumberOfSubperiods(), 3); $this->assertEqual( $range->isFinished(), true); $this->assertEqual( $range->toString(), $correct); } + // test range date1,date2 + function test_range_comma1() + { + + $range = new Piwik_Period_Range( 'day', '2008-01-01,2008-01-03' ); + + $correct = array( + '2008-01-01', + '2008-01-02', + '2008-01-03', + ); + + $this->assertEqual( $range->getNumberOfSubperiods(), count($correct)); + $this->assertEqual( $range->isFinished(), true); + $this->assertEqual( $range->toString(), $correct); + } + + // test range date1,date2 + function test_range_comma2() + { + + $range = new Piwik_Period_Range( 'day', '2007-12-22,2008-01-03' ); + + $correct = array( + '2007-12-22', + '2007-12-23', + '2007-12-24', + '2007-12-25', + '2007-12-26', + '2007-12-27', + '2007-12-28', + '2007-12-29', + '2007-12-30', + '2007-12-31', + '2008-01-01', + '2008-01-02', + '2008-01-03', + ); + + $this->assertEqual( $range->getNumberOfSubperiods(), count($correct)); + $this->assertEqual( $range->isFinished(), true); + $this->assertEqual( $range->toString(), $correct); + } + // test range date1,date2 + function test_range_weekcomma1() + { + + $range = new Piwik_Period_Range( 'week', '2007-12-22,2008-01-03' ); + + $correct = array( + array( + + '2007-12-17', + '2007-12-18', + '2007-12-19', + '2007-12-20', + '2007-12-21', + '2007-12-22', + '2007-12-23', + ), + array( + '2007-12-24', + '2007-12-25', + '2007-12-26', + '2007-12-27', + '2007-12-28', + '2007-12-29', + '2007-12-30', + ), + array( + '2007-12-31', + '2008-01-01', + '2008-01-02', + '2008-01-03', + '2008-01-04', + '2008-01-05', + '2008-01-06', + ) + ); + + $this->assertEqual( $range->getNumberOfSubperiods(), count($correct)); + $this->assertEqual( $range->isFinished(), true); + $this->assertEqual( $range->toString(), $correct); + } + // test range date1,date2 + function test_range_yearcomma1() + { + + $range = new Piwik_Period_Range( 'year', '2006-12-22,2007-01-03' ); + + $correct = array( + array ( + 0 => '2006-01-01', + 1 => '2006-02-01', + 2 => '2006-03-01', + 3 => '2006-04-01', + 4 => '2006-05-01', + 5 => '2006-06-01', + 6 => '2006-07-01', + 7 => '2006-08-01', + 8 => '2006-09-01', + 9 => '2006-10-01', + 10 => '2006-11-01', + 11 => '2006-12-01', + ), + 1 => + array ( + 0 => '2007-01-01', + 1 => '2007-02-01', + 2 => '2007-03-01', + 3 => '2007-04-01', + 4 => '2007-05-01', + 5 => '2007-06-01', + 6 => '2007-07-01', + 7 => '2007-08-01', + 8 => '2007-09-01', + 9 => '2007-10-01', + 10 => '2007-11-01', + 11 => '2007-12-01', + ), + ); + $this->assertEqual( $range->getNumberOfSubperiods(), count($correct)); + $this->assertEqual( $range->isFinished(), true); + $this->assertEqual( $range->toString(), $correct); + } + // test range date1,date2 + function test_range_monthcomma1() + { + + $range = new Piwik_Period_Range( 'month', '2006-12-22,2007-01-03' ); + + $correct = array( + array( + '2006-12-01', + '2006-12-02', + '2006-12-03', + '2006-12-04', + '2006-12-05', + '2006-12-06', + '2006-12-07', + '2006-12-08', + '2006-12-09', + '2006-12-10', + '2006-12-11', + '2006-12-12', + '2006-12-13', + '2006-12-14', + '2006-12-15', + '2006-12-16', + '2006-12-17', + '2006-12-18', + '2006-12-19', + '2006-12-20', + '2006-12-21', + '2006-12-22', + '2006-12-23', + '2006-12-24', + '2006-12-25', + '2006-12-26', + '2006-12-27', + '2006-12-28', + '2006-12-29', + '2006-12-30', + '2006-12-31', + ), + array( + + '2007-01-01', + '2007-01-02', + '2007-01-03', + '2007-01-04', + '2007-01-05', + '2007-01-06', + '2007-01-07', + '2007-01-08', + '2007-01-09', + '2007-01-10', + '2007-01-11', + '2007-01-12', + '2007-01-13', + '2007-01-14', + '2007-01-15', + '2007-01-16', + '2007-01-17', + '2007-01-18', + '2007-01-19', + '2007-01-20', + '2007-01-21', + '2007-01-22', + '2007-01-23', + '2007-01-24', + '2007-01-25', + '2007-01-26', + '2007-01-27', + '2007-01-28', + '2007-01-29', + '2007-01-30', + '2007-01-31', + ), + ); + + $this->assertEqual( $range->getNumberOfSubperiods(), count($correct)); + $this->assertEqual( $range->isFinished(), true); + $this->assertEqual( $range->toString(), $correct); + } + // test range WEEK function test_range_week() { @@ -667,7 +877,9 @@ class Test_Piwik_Period extends UnitTestCase $correct[]= $week->toString(); } - + $correct = array_reverse($correct); + + $this->assertEqual( $range->getNumberOfSubperiods(), 50); $this->assertEqual( $range->isFinished(), false); $this->assertEqual( $range->toString(), $correct); @@ -687,6 +899,7 @@ class Test_Piwik_Period extends UnitTestCase $correct[]= $week->toString(); } + $correct = array_reverse($correct); $this->assertEqual( $range->getNumberOfSubperiods(), 20); $this->assertEqual( $range->isFinished(), false); @@ -708,7 +921,9 @@ class Test_Piwik_Period extends UnitTestCase $correct[]= $week->toString(); } - + $correct = array_reverse($correct); + + $this->assertEqual( $range->getNumberOfSubperiods(), 10); $this->assertEqual( $range->isFinished(), true); $this->assertEqual( $range->toString(), $correct); @@ -729,6 +944,7 @@ class Test_Piwik_Period extends UnitTestCase $correct[]= $week->toString(); } + $correct = array_reverse($correct); $this->assertEqual( $range->getNumberOfSubperiods(), 10); $this->assertEqual( $range->isFinished(), false); -- cgit v1.2.3