diff options
author | diosmosis <diosmosis@users.noreply.github.com> | 2018-11-24 06:18:29 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-11-24 06:18:29 +0300 |
commit | bd0de0c5979de6091d7399f6ec70de0875c45048 (patch) | |
tree | f2fbe88c63de8cd0fe46bbc989e2656dd37ad643 | |
parent | b136e24f399be080ed6ac7f7c060323fccba5081 (diff) |
In LogAggregator, allow the use of a complex dimension w/ an already defined select as. (#13729)
* In LogAggregator, allow the use of a complex dimension w/ an already defined select as.
* Add integration test for change.
-rw-r--r-- | core/DataAccess/LogAggregator.php | 13 | ||||
-rw-r--r-- | core/DataTable/Map.php | 9 | ||||
-rw-r--r-- | tests/PHPUnit/Integration/DataAccess/LogAggregatorTest.php | 61 |
3 files changed, 69 insertions, 14 deletions
diff --git a/core/DataAccess/LogAggregator.php b/core/DataAccess/LogAggregator.php index ac87149c69..1801fa207e 100644 --- a/core/DataAccess/LogAggregator.php +++ b/core/DataAccess/LogAggregator.php @@ -423,6 +423,7 @@ class LogAggregator * @param $dimensions * @param $tableName * @param bool $appendSelectAs + * @param bool $parseSelectAs * @return mixed */ protected function getSelectDimensions($dimensions, $tableName, $appendSelectAs = true) @@ -432,11 +433,15 @@ class LogAggregator if (!is_numeric($selectAs)) { $selectAsString = $selectAs; - } else { - // if function, do not alias or prefix - if ($this->isFieldFunctionOrComplexExpression($field)) { - $selectAsString = $appendSelectAs = false; + } else if ($this->isFieldFunctionOrComplexExpression($field)) { + // if complex expression has a select as, use it + if (!$appendSelectAs && preg_match('/\s+AS\s+(.*?)\s*$/', $field, $matches)) { + $field = $matches[1]; + continue; } + + // if function w/o select as, do not alias or prefix + $selectAsString = $appendSelectAs = false; } $isKnownField = !in_array($field, array('referrer_data')); diff --git a/core/DataTable/Map.php b/core/DataTable/Map.php index e767d2f49a..7d91cf57ef 100644 --- a/core/DataTable/Map.php +++ b/core/DataTable/Map.php @@ -158,6 +158,15 @@ class Map implements DataTableInterface } /** + * @param string $label + * @return bool + */ + public function hasTable($label) + { + return isset($this->array[$label]); + } + + /** * Returns the first element in the Map's array. * * @return DataTable|Map|false diff --git a/tests/PHPUnit/Integration/DataAccess/LogAggregatorTest.php b/tests/PHPUnit/Integration/DataAccess/LogAggregatorTest.php index 1d31dbbaa9..40bb181fef 100644 --- a/tests/PHPUnit/Integration/DataAccess/LogAggregatorTest.php +++ b/tests/PHPUnit/Integration/DataAccess/LogAggregatorTest.php @@ -14,7 +14,7 @@ use Piwik\Date; use Piwik\Period; use Piwik\Segment; use Piwik\Site; -use Piwik\Tests\Framework\Fixture; +use Piwik\Tests\Fixtures\OneVisitorTwoVisits; use Piwik\Tests\Framework\TestCase\IntegrationTestCase; /** @@ -26,6 +26,11 @@ use Piwik\Tests\Framework\TestCase\IntegrationTestCase; class LogAggregatorTest extends IntegrationTestCase { /** + * @var OneVisitorTwoVisits + */ + public static $fixture; + + /** * @var LogAggregator */ private $logAggregator; @@ -36,12 +41,8 @@ class LogAggregatorTest extends IntegrationTestCase $idSite = 1; - if (!Fixture::siteCreated($idSite)) { - Fixture::createWebsite('2014-01-01 00:00:00'); - } - $site = new Site($idSite); - $date = Date::factory('2012-01-01'); + $date = Date::factory('2010-03-06'); $period = Period\Factory::build('month', $date); $segment = new Segment('', array($site->getId())); @@ -65,8 +66,8 @@ class LogAggregatorTest extends IntegrationTestCase ORDER BY 5', 'bind' => array ( - 0 => '2012-01-01 00:00:00', - 1 => '2012-01-31 23:59:59', + 0 => '2010-03-01 00:00:00', + 1 => '2010-03-31 23:59:59', 2 => 1 ) ); @@ -88,12 +89,52 @@ class LogAggregatorTest extends IntegrationTestCase ORDER BY 5', 'bind' => array ( - 0 => '2012-01-01 00:00:00', - 1 => '2012-01-31 23:59:59', + 0 => '2010-03-01 00:00:00', + 1 => '2010-03-31 23:59:59', 2 => 1 ) ); $this->assertSame($expected, $query); } + public function test_queryVisitsByDimension_withComplexDimensionSelect() + { + $dimensions = [ + 'CASE WHEN HOUR(log_visit.visit_first_action_time) <= 11 THEN \'l\'' . + 'ELSE \'r\'' . + 'END AS label', + ]; + + /** @var \Zend_Db_Statement $query */ + $query = $this->logAggregator->queryVisitsByDimension($dimensions); + $result = $query->fetchAll(); + + $expected = [ + [ + 'label' => 'l', + 1 => '1', + 2 => '1', + 3 => '7', + 4 => '7', + 5 => '1621', + 6 => '0', + 7 => '1', + 39 => '0', + ], + [ + 'label' => 'r', + 1 => '1', + 2 => '1', + 3 => '1', + 4 => '1', + 5 => '0', + 6 => '1', + 7 => '1', + 39 => '0', + ], + ]; + $this->assertEquals($expected, $result); + } } + +LogAggregatorTest::$fixture = new OneVisitorTwoVisits(); |