diff options
author | Thomas Steur <thomas.steur@gmail.com> | 2015-02-13 22:13:11 +0300 |
---|---|---|
committer | Thomas Steur <thomas.steur@gmail.com> | 2015-02-13 22:13:11 +0300 |
commit | 10a528b3d6ff3dc3269f9d5d408caea1e52d3ed4 (patch) | |
tree | c4cacde4b4c245de853420c78124ac1a769ecef1 /plugins/VisitTime | |
parent | 806bedba1a43d7d78b687d1d23a6b861a2b70035 (diff) |
refs #4633 rename segment filter to segment, fixed a bug re visitTime and extracted code into a filter
Diffstat (limited to 'plugins/VisitTime')
-rw-r--r-- | plugins/VisitTime/API.php | 22 | ||||
-rw-r--r-- | plugins/VisitTime/DataTable/Filter/AddSegmentByLabelInUTC.php | 55 | ||||
-rw-r--r-- | plugins/VisitTime/tests/Unit/AddSegmentByLabelInUTCTest.php | 114 |
3 files changed, 171 insertions, 20 deletions
diff --git a/plugins/VisitTime/API.php b/plugins/VisitTime/API.php index c78534c193..928e8c95f3 100644 --- a/plugins/VisitTime/API.php +++ b/plugins/VisitTime/API.php @@ -52,30 +52,12 @@ class API extends \Piwik\Plugin\API $table = $this->getDataTable(Archiver::SERVER_TIME_RECORD_NAME, $idSite, $period, $date, $segment); $timezone = Site::getTimezoneFor($idSite); - - $range = Range::parseDateRange($date); - - if (!empty($range[2])) { - $endDate = Date::factory($range[2]); - } else if (!empty($range[1])) { - $endDate = Date::factory($range[1]); - } else { - $endDate = Date::factory($date); - } - - $table->filter('AddSegmentValue', array(function ($label) use ($timezone, $endDate) { - $hour = str_pad($label, 2, 0, STR_PAD_LEFT); - $time = $hour . ':00:00'; - - $dateInTimezone = $endDate->setTime($time)->setTimezone($timezone); - $hourInTz = $dateInTimezone->getHourInUTC(); - - return $hourInTz; - })); + $table->filter('Piwik\Plugins\VisitTime\DataTable\Filter\AddSegmentByLabelInUTC', array($timezone, $period, $date)); if ($hideFutureHoursWhenToday) { $table = $this->removeHoursInFuture($table, $idSite, $period, $date); } + return $table; } diff --git a/plugins/VisitTime/DataTable/Filter/AddSegmentByLabelInUTC.php b/plugins/VisitTime/DataTable/Filter/AddSegmentByLabelInUTC.php new file mode 100644 index 0000000000..0808c8c68c --- /dev/null +++ b/plugins/VisitTime/DataTable/Filter/AddSegmentByLabelInUTC.php @@ -0,0 +1,55 @@ +<?php +/** + * Piwik - free/libre analytics platform + * + * @link http://piwik.org + * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later + * + */ +namespace Piwik\Plugins\VisitTime\DataTable\Filter; + +use Piwik\DataTable; +use Piwik\Period; + +/** + * Adds a segment value to each row by interpreting the label value as hour in the website's timezone and + * converting the hour to UTC. + * + * **Basic usage example** + * + * $dataTable->filter('AddSegmentByLabelInUTC', array($idSite = 'UTC+1', $period = 'day', $date = 'today'); + */ +class AddSegmentByLabelInUTC extends DataTable\Filter\AddSegmentValue +{ + private $timezone; + private $date; + + /** + * @param DataTable $table + * @param int $timezone The timezone of the current selected site / the timezone of the labels + * @param string $period The requested period and date is needed to respect daylight saving etc. + * @param string $date + */ + public function __construct($table, $timezone, $period, $date) + { + $this->timezone = $timezone; + $this->date = Period\Factory::build($period, $date)->getDateEnd(); + + $self = $this; + + parent::__construct($table, function ($label) use ($self) { + $hour = str_pad($label, 2, 0, STR_PAD_LEFT); + + return $self->convertHourToUtc($hour); + }); + } + + public function convertHourToUTC($hour) + { + $dateWithHour = $this->date->setTime($hour . ':00:00'); + $dateInTimezone = $dateWithHour->setTimezone($this->timezone); + $hourInUTC = $dateInTimezone->getHourUTC(); + + return $hourInUTC; + } +}
\ No newline at end of file diff --git a/plugins/VisitTime/tests/Unit/AddSegmentByLabelInUTCTest.php b/plugins/VisitTime/tests/Unit/AddSegmentByLabelInUTCTest.php new file mode 100644 index 0000000000..bf0c828321 --- /dev/null +++ b/plugins/VisitTime/tests/Unit/AddSegmentByLabelInUTCTest.php @@ -0,0 +1,114 @@ +<?php +/** + * Piwik - free/libre analytics platform + * + * @link http://piwik.org + * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later + */ + +namespace Piwik\Plugins\VisitTime\tests\Unit; + +use Piwik\DataTable\Row; +use Piwik\DataTable; +use Piwik\Tests\Framework\TestCase\UnitTestCase; + +/** + * @group VisitTime + * @group AddSegmentByLabelInUTCTest + * @group Plugins + */ +class AddSegmentByLabelInUTCTest extends UnitTestCase +{ + private $filter = 'Piwik\Plugins\VisitTime\DataTable\Filter\AddSegmentByLabelInUTC'; + + /** + * @var DataTable + */ + private $table; + + public function setUp() + { + $this->table = new DataTable(); + $this->addRow(array('label' => '0')); + $this->addRow(array('label' => '1')); + $this->addRow(array('label' => '2')); + $this->addRow(array('label' => '12')); + $this->addRow(array('label' => '13')); + $this->addRow(array('label' => '14')); + $this->addRow(array('label' => '20')); + } + + private function addRow($columns) + { + $this->table->addRow($this->buildRow($columns)); + } + + private function buildRow($columns) + { + return new Row(array(Row::COLUMNS => $columns)); + } + + public function test_filter_shouldNotChangeHoursIfTimezoneIsUTCAlready() + { + $this->table->filter($this->filter, array('UTC', 'day', 'today')); + + $this->assertSegmentValues(array('0', '1', '2', '12', '13', '14', '20')); + } + + public function test_filter_shouldConvertHoursFromTimezoneIntoUTC_Minus1() + { + $this->table->filter($this->filter, array('UTC-1', 'day', 'today')); + $this->assertSegmentValues(array('1', '2', '3', '13', '14', '15', '21')); + } + + public function test_filter_shouldConvertHoursFromTimezoneIntoUTC_Plus1() + { + $this->table->filter($this->filter, array('UTC+1', 'day', 'today')); + $this->assertSegmentValuesInUTCplus1(); + } + + public function test_filter_shouldHandleRangePeriod_Plus1() + { + $this->table->filter($this->filter, array('UTC+1', 'range', '2015-02-02,2015-02-14')); + $this->assertSegmentValuesInUTCplus1(); + } + + public function test_filter_shouldHandleRangePeriodWithLast7_Plus1() + { + $this->table->filter($this->filter, array('UTC+1', 'range', 'last7')); + $this->assertSegmentValuesInUTCplus1(); + } + + public function test_filter_shouldHandleDayPeriodWithRange_Plus1() + { + $this->table->filter($this->filter, array('UTC+1', 'day', '2015-02-02,2015-02-14')); + $this->assertSegmentValuesInUTCplus1(); + } + + public function test_filter_shouldHandleWeekWithLast7_Plus1() + { + $this->table->filter($this->filter, array('UTC+1', 'day', 'last7')); + $this->assertSegmentValuesInUTCplus1(); + } + + public function test_filter_shouldIgnoreSummaryRow() + { + $row = $this->buildRow(array('label' => 'other')); + $this->table->addSummaryRow($row); + $this->table->filter($this->filter, array('UTC', 'day', 'today')); + + $this->assertFalse($row->getMetadata('segmentValue')); + } + + private function assertSegmentValuesInUTCplus1() + { + $this->assertSegmentValues(array('23', '0', '1', '11', '12', '13', '19')); + } + + private function assertSegmentValues($expectedSegmentValues) + { + $segmentValues = $this->table->getRowsMetadata('segmentValue'); + $this->assertSame($expectedSegmentValues, $segmentValues); + } + +} |