From 3069828eff80e45a5f9ce661729adcebbb70fa6c Mon Sep 17 00:00:00 2001 From: mattab Date: Sun, 21 Apr 2013 12:44:29 +1200 Subject: [PATCH] Refs #3906 * Speeding up the suggestion list by only selecting visitors that have a value set for the requested segment --- core/Segment.php | 6 +----- core/SegmentExpression.php | 12 ++++++++++- plugins/API/API.php | 10 ++++++---- plugins/Live/API.php | 6 ------ ...ider__API.getSuggestedValuesForSegment.xml | 4 +--- ...eTest__Live.getLastVisitsDetails_month.xml | 20 +++++++++---------- ...Support__Live.getLastVisitsDetails_day.xml | 4 ++-- ...ecified__Live.getLastVisitsDetails_day.xml | 4 ++-- ...Ordered__Live.getLastVisitsDetails_day.xml | 4 ++-- ...thItems__Live.getLastVisitsDetails_day.xml | 6 +++--- 10 files changed, 38 insertions(+), 38 deletions(-) diff --git a/core/Segment.php b/core/Segment.php index 6ba00c64781..8b1e30ccd6e 100644 --- a/core/Segment.php +++ b/core/Segment.php @@ -91,13 +91,10 @@ protected function getCleanedExpression($expression) throw new Exception("You do not have enough permission to access the segment " . $name); } -// $this->segmentsHumanReadable[] = $segment['name'] . " " . -// $this->getNameForMatchType($matchType) . -// $value; - // apply presentation filter if (isset($segment['sqlFilter']) && !empty($segment['sqlFilter']) + && $matchType != Piwik_SegmentExpression::MATCH_IS_NOT_NULL ) { $value = call_user_func($segment['sqlFilter'], $value, $segment['sqlSegment'], $matchType, $name); @@ -184,7 +181,6 @@ public function getSelectQuery($select, $from, $where = false, $bind = array(), } else { $sql = $this->buildSelectQuery($select, $from, $where, $orderBy, $groupBy); } - return array( 'sql' => $sql, 'bind' => $bind diff --git a/core/SegmentExpression.php b/core/SegmentExpression.php index 9340aec4bdb..c6ddfddf4d4 100644 --- a/core/SegmentExpression.php +++ b/core/SegmentExpression.php @@ -27,6 +27,9 @@ class Piwik_SegmentExpression const MATCH_CONTAINS = '=@'; const MATCH_DOES_NOT_CONTAIN = '!@'; + // Note: undocumented for now, only used in API.getSuggestedValuesForSegment + const MATCH_IS_NOT_NULL = '::'; + // Special case, since we look up Page URLs/Page titles in a sub SQL query const MATCH_ACTIONS_CONTAINS = 'IN'; @@ -66,6 +69,7 @@ public function parseSubExpressions() . self::MATCH_LESS_OR_EQUAL . '|' . self::MATCH_LESS . '|' . self::MATCH_CONTAINS . '|' + . self::MATCH_IS_NOT_NULL . '|' . self::MATCH_DOES_NOT_CONTAIN . '){1}(.+)/'; $match = preg_match($pattern, $operand, $matches); @@ -180,6 +184,11 @@ protected function getSqlMatchFromDefinition($def, &$availableTables) $value = '%' . $this->escapeLikeString($value) . '%'; break; + case self::MATCH_IS_NOT_NULL: + $sqlMatch = 'IS NOT NULL AND ('.$field.' <> \'\' OR '.$field.' = 0)'; + $value = null; + break; + case self::MATCH_ACTIONS_CONTAINS: // this match type is not accessible from the outside // (it won't be matched in self::parseSubExpressions()) @@ -193,7 +202,8 @@ protected function getSqlMatchFromDefinition($def, &$availableTables) break; } - if ($matchType === self::MATCH_ACTIONS_CONTAINS) { + if ($matchType === self::MATCH_ACTIONS_CONTAINS + || is_null($value)) { $sqlExpression = "$field $sqlMatch"; } else { $sqlExpression = "$field $sqlMatch ?"; diff --git a/plugins/API/API.php b/plugins/API/API.php index 6eac5c7f1b8..db6a1e2070b 100644 --- a/plugins/API/API.php +++ b/plugins/API/API.php @@ -1663,15 +1663,17 @@ public function getSuggestedValuesForSegment($segmentName, $idSite) &date=$startDate,today &format=original &serialize=0 - &flat=1 - &segment="; + &flat=1"; + + // Select non empty fields only + $requestLastVisits.= "&segment=$segmentName" . Piwik_SegmentExpression::MATCH_IS_NOT_NULL . "null"; // By default Live fetches all actions for all visitors, but we'd rather do this only when required if(!$doesSegmentNeedActionsInfo) { $requestLastVisits .= "&doNotFetchActions=1"; - $requestLastVisits .= "&filter_limit=10000"; - } else { $requestLastVisits .= "&filter_limit=1000"; + } else { + $requestLastVisits .= "&filter_limit=500"; } $request = new Piwik_API_Request($requestLastVisits); diff --git a/plugins/Live/API.php b/plugins/Live/API.php index 14f7712190b..d2d74a74bfd 100644 --- a/plugins/Live/API.php +++ b/plugins/Live/API.php @@ -398,7 +398,6 @@ private function loadLastVisitorDetailsFromDatabase($idSite, $period = false, $d GROUP BY sub.idvisit ORDER BY $orderByParent "; - try { $data = Piwik_FetchAll($sql, $subQuery['bind']); } catch (Exception $e) { @@ -432,11 +431,6 @@ private function cleanVisitorDetails(&$visitorDetails, $idSite) /** * @param $visitorDetailsArray * @param $actionsLimit - * @param $actionDetail - * @param $ecommerceDetail - * @param $ecommerceConversion - * @param $detail - * @param $details * @param $timezone * @return array */ diff --git a/tests/PHPUnit/Integration/expected/test_AutoSuggestAPITest_provider__API.getSuggestedValuesForSegment.xml b/tests/PHPUnit/Integration/expected/test_AutoSuggestAPITest_provider__API.getSuggestedValuesForSegment.xml index b406b0dc48a..c234bed59e9 100644 --- a/tests/PHPUnit/Integration/expected/test_AutoSuggestAPITest_provider__API.getSuggestedValuesForSegment.xml +++ b/tests/PHPUnit/Integration/expected/test_AutoSuggestAPITest_provider__API.getSuggestedValuesForSegment.xml @@ -1,4 +1,2 @@ - - Unknown - \ No newline at end of file + \ No newline at end of file diff --git a/tests/PHPUnit/Integration/expected/test_ManyVisitorsOneWebsiteTest__Live.getLastVisitsDetails_month.xml b/tests/PHPUnit/Integration/expected/test_ManyVisitorsOneWebsiteTest__Live.getLastVisitsDetails_month.xml index a4b34bfc7bc..f4fb13bd4dd 100644 --- a/tests/PHPUnit/Integration/expected/test_ManyVisitorsOneWebsiteTest__Live.getLastVisitsDetails_month.xml +++ b/tests/PHPUnit/Integration/expected/test_ManyVisitorsOneWebsiteTest__Live.getLastVisitsDetails_month.xml @@ -42,7 +42,7 @@ 12:34:06 12 - 11 + @@ -180,7 +180,7 @@ 12:34:06 12 - 12 + @@ -301,7 +301,7 @@ 12:34:06 12 - 11 + @@ -422,7 +422,7 @@ 12:34:06 12 - 12 + @@ -543,7 +543,7 @@ 12:34:06 12 - 11 + @@ -681,7 +681,7 @@ 12:34:06 12 - 12 + @@ -802,7 +802,7 @@ 12:34:06 12 - 11 + @@ -923,7 +923,7 @@ 12:34:06 12 - 12 + @@ -1044,7 +1044,7 @@ 12:34:06 12 - 11 + @@ -1182,7 +1182,7 @@ 12:34:06 12 - 12 + diff --git a/tests/PHPUnit/Integration/expected/test_OneVisitorTwoVisits_withCookieSupport__Live.getLastVisitsDetails_day.xml b/tests/PHPUnit/Integration/expected/test_OneVisitorTwoVisits_withCookieSupport__Live.getLastVisitsDetails_day.xml index ce3c08fe99d..b052ae38fae 100644 --- a/tests/PHPUnit/Integration/expected/test_OneVisitorTwoVisits_withCookieSupport__Live.getLastVisitsDetails_day.xml +++ b/tests/PHPUnit/Integration/expected/test_OneVisitorTwoVisits_withCookieSupport__Live.getLastVisitsDetails_day.xml @@ -43,7 +43,7 @@ 12:34:06 12 - 12 + @@ -227,7 +227,7 @@ 12:34:06 12 - 11 + diff --git a/tests/PHPUnit/Integration/expected/test_OneVisitor_NoKeywordSpecified__Live.getLastVisitsDetails_day.xml b/tests/PHPUnit/Integration/expected/test_OneVisitor_NoKeywordSpecified__Live.getLastVisitsDetails_day.xml index 33db83e25d8..0d99dbf9395 100644 --- a/tests/PHPUnit/Integration/expected/test_OneVisitor_NoKeywordSpecified__Live.getLastVisitsDetails_day.xml +++ b/tests/PHPUnit/Integration/expected/test_OneVisitor_NoKeywordSpecified__Live.getLastVisitsDetails_day.xml @@ -32,7 +32,7 @@ 12:34:06 12 - 13 + @@ -135,7 +135,7 @@ 12:34:06 12 - 11 + diff --git a/tests/PHPUnit/Integration/expected/test_ecommerceOrderWithItems_LiveEcommerceStatusOrdered__Live.getLastVisitsDetails_day.xml b/tests/PHPUnit/Integration/expected/test_ecommerceOrderWithItems_LiveEcommerceStatusOrdered__Live.getLastVisitsDetails_day.xml index 8ab5189d8ef..944ebbc5c0a 100644 --- a/tests/PHPUnit/Integration/expected/test_ecommerceOrderWithItems_LiveEcommerceStatusOrdered__Live.getLastVisitsDetails_day.xml +++ b/tests/PHPUnit/Integration/expected/test_ecommerceOrderWithItems_LiveEcommerceStatusOrdered__Live.getLastVisitsDetails_day.xml @@ -70,7 +70,7 @@ 12:34:06 12 - 6 + @@ -277,7 +277,7 @@ 12:34:06 12 - 2 + diff --git a/tests/PHPUnit/Integration/expected/test_ecommerceOrderWithItems__Live.getLastVisitsDetails_day.xml b/tests/PHPUnit/Integration/expected/test_ecommerceOrderWithItems__Live.getLastVisitsDetails_day.xml index 8addc1e69d9..debade25ce1 100644 --- a/tests/PHPUnit/Integration/expected/test_ecommerceOrderWithItems__Live.getLastVisitsDetails_day.xml +++ b/tests/PHPUnit/Integration/expected/test_ecommerceOrderWithItems__Live.getLastVisitsDetails_day.xml @@ -146,7 +146,7 @@ 12:34:06 12 - 7 + @@ -472,7 +472,7 @@ 12:34:06 12 - 2 + @@ -680,7 +680,7 @@ 12:34:06 12 - 0 +