Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Steur <thomas.steur@gmail.com>2015-02-13 22:13:11 +0300
committerThomas Steur <thomas.steur@gmail.com>2015-02-13 22:13:11 +0300
commit10a528b3d6ff3dc3269f9d5d408caea1e52d3ed4 (patch)
treec4cacde4b4c245de853420c78124ac1a769ecef1 /plugins
parent806bedba1a43d7d78b687d1d23a6b861a2b70035 (diff)
refs #4633 rename segment filter to segment, fixed a bug re visitTime and extracted code into a filter
Diffstat (limited to 'plugins')
-rw-r--r--plugins/DevicesDetection/API.php6
-rw-r--r--plugins/Referrers/API.php10
-rw-r--r--plugins/UserCountry/API.php4
-rw-r--r--plugins/VisitTime/API.php22
-rw-r--r--plugins/VisitTime/DataTable/Filter/AddSegmentByLabelInUTC.php55
-rw-r--r--plugins/VisitTime/tests/Unit/AddSegmentByLabelInUTCTest.php114
6 files changed, 181 insertions, 30 deletions
diff --git a/plugins/DevicesDetection/API.php b/plugins/DevicesDetection/API.php
index f1904c3946..36fcd8fb95 100644
--- a/plugins/DevicesDetection/API.php
+++ b/plugins/DevicesDetection/API.php
@@ -55,7 +55,7 @@ class API extends \Piwik\Plugin\API
$this->ensureDefaultRowsInTable($dataTable);
$mapping = DeviceParserAbstract::getAvailableDeviceTypeNames();
- $dataTable->filter('AddSegmentFilterByLabelMapping', array('deviceType', $mapping));
+ $dataTable->filter('AddSegmentByLabelMapping', array('deviceType', $mapping));
$dataTable->filter('ColumnCallbackAddMetadata', array('label', 'logo', __NAMESPACE__ . '\getDeviceTypeLogo'));
$dataTable->filter('ColumnCallbackReplace', array('label', __NAMESPACE__ . '\getDeviceTypeLabel'));
return $dataTable;
@@ -198,7 +198,7 @@ class API extends \Piwik\Plugin\API
$dataTable = $this->getDataTable('DevicesDetection_osVersions', $idSite, $period, $date, $segment);
$segments = array('operatingSystemCode', 'operatingSystemVersion');
- $dataTable->filter('AddSegmentFilter', array($segments, Archiver::BROWSER_SEPARATOR));
+ $dataTable->filter('AddSegmentByLabel', array($segments, Archiver::BROWSER_SEPARATOR));
$dataTable->filter('ColumnCallbackAddMetadata', array('label', 'logo', __NAMESPACE__ . '\getOsLogo'));
// use GroupBy filter to avoid duplicate rows if old (UserSettings) and new (DevicesDetection) reports were combined
$dataTable->filter('GroupBy', array('label', __NAMESPACE__ . '\getOsFullName'));
@@ -257,7 +257,7 @@ class API extends \Piwik\Plugin\API
$dataTable = $this->getDataTable('DevicesDetection_browserVersions', $idSite, $period, $date, $segment);
$segments = array('browserCode', 'browserVersion');
- $dataTable->filter('AddSegmentFilter', array($segments, Archiver::BROWSER_SEPARATOR));
+ $dataTable->filter('AddSegmentByLabel', array($segments, Archiver::BROWSER_SEPARATOR));
$dataTable->filter('ColumnCallbackAddMetadata', array('label', 'logo', __NAMESPACE__ . '\getBrowserLogo'));
$dataTable->filter('ColumnCallbackReplace', array('label', __NAMESPACE__ . '\getBrowserNameWithVersion'));
return $dataTable;
diff --git a/plugins/Referrers/API.php b/plugins/Referrers/API.php
index 992842a294..dbafc293e2 100644
--- a/plugins/Referrers/API.php
+++ b/plugins/Referrers/API.php
@@ -235,8 +235,8 @@ class API extends \Piwik\Plugin\API
{
$dataTable = $this->getDataTable(Archiver::SEARCH_ENGINES_RECORD_NAME, $idSite, $period, $date, $segment, $expanded);
- $dataTable->filter('AddSegmentFilter', array('referrerName'));
- $dataTable->queueFilter('PrependSegmentFilter', array('referrerType==search;'));
+ $dataTable->filter('AddSegmentByLabel', array('referrerName'));
+ $dataTable->queueFilter('PrependSegment', array('referrerType==search;'));
$dataTable->queueFilter('ColumnCallbackAddMetadata', array('label', 'url', __NAMESPACE__ . '\getSearchEngineUrlFromName'));
$dataTable->queueFilter('MetadataCallbackAddMetadata', array('url', 'logo', __NAMESPACE__ . '\getSearchEngineLogoFromUrl'));
return $dataTable;
@@ -277,8 +277,8 @@ class API extends \Piwik\Plugin\API
{
$dataTable = $this->getDataTable(Archiver::CAMPAIGNS_RECORD_NAME, $idSite, $period, $date, $segment, $expanded);
- $dataTable->filter('AddSegmentFilter', array('referrerName'));
- $dataTable->queueFilter('PrependSegmentFilter', array('referrerType==campaign;'));
+ $dataTable->filter('AddSegmentByLabel', array('referrerName'));
+ $dataTable->queueFilter('PrependSegment', array('referrerType==campaign;'));
return $dataTable;
}
@@ -292,7 +292,7 @@ class API extends \Piwik\Plugin\API
public function getWebsites($idSite, $period, $date, $segment = false, $expanded = false)
{
$dataTable = $this->getDataTable(Archiver::WEBSITES_RECORD_NAME, $idSite, $period, $date, $segment, $expanded);
- $dataTable->filter('AddSegmentFilter', array('referrerName'));
+ $dataTable->filter('AddSegmentByLabel', array('referrerName'));
return $dataTable;
}
diff --git a/plugins/UserCountry/API.php b/plugins/UserCountry/API.php
index c9fcfeec78..38408b39e6 100644
--- a/plugins/UserCountry/API.php
+++ b/plugins/UserCountry/API.php
@@ -70,7 +70,7 @@ class API extends \Piwik\Plugin\API
$dataTable = $this->getDataTable(Archiver::REGION_RECORD_NAME, $idSite, $period, $date, $segment);
$segments = array('regionCode', 'countryCode');
- $dataTable->filter('AddSegmentFilter', array($segments, Archiver::LOCATION_SEPARATOR));
+ $dataTable->filter('AddSegmentByLabel', array($segments, Archiver::LOCATION_SEPARATOR));
$separator = Archiver::LOCATION_SEPARATOR;
$unk = Visit::UNKNOWN_CODE;
@@ -115,7 +115,7 @@ class API extends \Piwik\Plugin\API
$dataTable = $this->getDataTable(Archiver::CITY_RECORD_NAME, $idSite, $period, $date, $segment);
$segments = array('city', 'regionCode', 'countryCode');
- $dataTable->filter('AddSegmentFilter', array($segments, Archiver::LOCATION_SEPARATOR));
+ $dataTable->filter('AddSegmentByLabel', array($segments, Archiver::LOCATION_SEPARATOR));
$separator = Archiver::LOCATION_SEPARATOR;
$unk = Visit::UNKNOWN_CODE;
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);
+ }
+
+}