diff options
author | diosmosis <benaka@piwik.pro> | 2015-09-17 05:36:20 +0300 |
---|---|---|
committer | diosmosis <benaka@piwik.pro> | 2015-09-23 10:57:32 +0300 |
commit | bcbb7bcf3452ca369eb85e09174f1b16361c63c2 (patch) | |
tree | 5283ccb35877d3de28258f80cc63d1cd88313833 /plugins/SegmentEditor | |
parent | 2633e93aaefa0e811288a667e12faa703434e8c1 (diff) |
Decorate LogQueryBuilder in SegmentEditor through DI to add idSegments matching the segment being archiving in a comment in SQL. Includes addition of SegmentEditor::getSegmentsToAutoArchive() API method which caches the result so each individual aggregation query doesn't trigger an SQL query.
Diffstat (limited to 'plugins/SegmentEditor')
-rw-r--r-- | plugins/SegmentEditor/API.php | 32 | ||||
-rw-r--r-- | plugins/SegmentEditor/Model.php | 9 | ||||
-rw-r--r-- | plugins/SegmentEditor/SegmentQueryDecorator.php | 62 | ||||
-rw-r--r-- | plugins/SegmentEditor/config/config.php | 7 |
4 files changed, 105 insertions, 5 deletions
diff --git a/plugins/SegmentEditor/API.php b/plugins/SegmentEditor/API.php index 358f0d2b3e..d0a9adfb1d 100644 --- a/plugins/SegmentEditor/API.php +++ b/plugins/SegmentEditor/API.php @@ -9,6 +9,7 @@ namespace Piwik\Plugins\SegmentEditor; use Exception; +use Piwik\Cache\Transient as TransientCache; use Piwik\Common; use Piwik\Date; use Piwik\Db; @@ -28,9 +29,15 @@ class API extends \Piwik\Plugin\API */ private $model; - public function __construct(Model $model) + /** + * @var TransientCache + */ + private $transientCache; + + public function __construct(Model $model, TransientCache $transientCache) { $this->model = $model; + $this->transientCache = $transientCache; } protected function checkSegmentValue($definition, $idSite) @@ -361,6 +368,29 @@ class API extends \Piwik\Plugin\API } /** + * Returns stored segments that are set to be archived during cron archiving. + * + * @param bool|false $idSite + * @return array + */ + public function getSegmentsToAutoArchive($idSite = false) + { + if (!empty($idSite)) { + Piwik::checkUserHasAdminAccess($idSite); + } else { + Piwik::checkUserHasSuperUserAccess(); + } + + $cacheKey = 'SegmentEditor.getSegmentsToAutoArchive_' . (!empty($idSite) ? $idSite : 'enabled_all'); + if (!$this->transientCache->contains($cacheKey)) { + $result = $this->model->getSegmentsToAutoArchive($idSite); + + $this->transientCache->save($cacheKey, $result); + } + return $this->transientCache->fetch($cacheKey); + } + + /** * @return string */ private function getMessageCannotEditSegmentCreatedBySuperUser() diff --git a/plugins/SegmentEditor/Model.php b/plugins/SegmentEditor/Model.php index 7f312557f9..5c7f3eec6f 100644 --- a/plugins/SegmentEditor/Model.php +++ b/plugins/SegmentEditor/Model.php @@ -51,13 +51,14 @@ class Model $bind = array(); $whereIdSite = ''; - if (!empty($idSite)) { - $whereIdSite = 'enable_only_idsite = ? OR '; + if (empty($idSite)) { + $whereIdSite .= 'enable_only_idsite = 0 AND'; + } else if ($idSite != 'all') { + $whereIdSite = '(enable_only_idsite = ? OR enable_only_idsite = 0) AND'; $bind[] = $idSite; } - $sql = $this->buildQuerySortedByName("($whereIdSite enable_only_idsite = 0) - AND deleted = 0 AND auto_archive = 1"); + $sql = $this->buildQuerySortedByName("$whereIdSite deleted = 0 AND auto_archive = 1"); $segments = $this->getDb()->fetchAll($sql, $bind); diff --git a/plugins/SegmentEditor/SegmentQueryDecorator.php b/plugins/SegmentEditor/SegmentQueryDecorator.php new file mode 100644 index 0000000000..ec9f712c6c --- /dev/null +++ b/plugins/SegmentEditor/SegmentQueryDecorator.php @@ -0,0 +1,62 @@ +<?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\SegmentEditor; + +use Piwik\Access; +use Piwik\DataAccess\LogQueryBuilder; +use Piwik\Segment\SegmentExpression; + +/** + * Decorates segment sub-queries in archiving queries w/ the idSegment of the segment, if + * a stored segment exists. + * + * This class is configured for use in SegmentEditor's DI config. + */ +class SegmentQueryDecorator extends LogQueryBuilder +{ + /** + * @var API + */ + private $segmentEditorApi; + + public function __construct(API $segmentEditorApi) + { + $this->segmentEditorApi = $segmentEditorApi; + } + + public function getSelectQueryString(SegmentExpression $segmentExpression, $select, $from, $where, $bind, $groupBy, + $orderBy, $limit) + { + $result = parent::getSelectQueryString($segmentExpression, $select, $from, $where, $bind, $groupBy, $orderBy, + $limit); + + $idSegments = $this->getSegmentIdOfExpression($segmentExpression); + if (!empty($idSegments)) { + $result['sql'] = "/* idSegments = [" . implode(', ', $idSegments) . "] */\n" . $result['sql']; + } + + return $result; + } + + private function getSegmentIdOfExpression(SegmentExpression $segmentExpression) + { + $segmentEditorApi = $this->segmentEditorApi; + $allSegments = Access::doAsSuperUser(function () use ($segmentEditorApi) { + return $segmentEditorApi->getSegmentsToAutoArchive('all'); + }); + + $idSegments = array(); + foreach ($allSegments as $segment) { + if ($segmentExpression->getSegmentString() == $segment['definition']) { + $idSegments[] = $segment['idsegment']; + } + } + return $idSegments; + } +} diff --git a/plugins/SegmentEditor/config/config.php b/plugins/SegmentEditor/config/config.php new file mode 100644 index 0000000000..ada7b93ac9 --- /dev/null +++ b/plugins/SegmentEditor/config/config.php @@ -0,0 +1,7 @@ +<?php + +return array( + + 'Piwik\DataAccess\LogQueryBuilder' => DI\get('Piwik\Plugins\SegmentEditor\SegmentQueryDecorator'), + +); |