diff options
-rw-r--r-- | core/Archive.php | 4 | ||||
-rw-r--r-- | core/ArchiveProcessor.php | 10 | ||||
-rw-r--r-- | core/ArchiveProcessor/Loader.php | 174 | ||||
-rw-r--r-- | core/ArchiveProcessor/PluginsArchiver.php | 78 |
4 files changed, 120 insertions, 146 deletions
diff --git a/core/Archive.php b/core/Archive.php index e0232f698f..f8fa4957cd 100644 --- a/core/Archive.php +++ b/core/Archive.php @@ -775,7 +775,7 @@ class Archive return $plugin; } - /**getNumberOfVisits + /** * @param $archiveGroups * @param $site * @param $period @@ -793,7 +793,7 @@ class Archive $this->initializeArchiveIdCache($doneFlag); $parameters->setRequestedPlugin($plugin); - $idArchive = $processing->prepareArchiveId(); + $idArchive = $processing->prepareArchive(); if($idArchive) { $this->idarchives[$doneFlag][$periodString][] = $idArchive; diff --git a/core/ArchiveProcessor.php b/core/ArchiveProcessor.php index 31c240e616..d21a2d6a88 100644 --- a/core/ArchiveProcessor.php +++ b/core/ArchiveProcessor.php @@ -103,14 +103,18 @@ class ArchiveProcessor /** * @var int */ - protected $numberOfVisits; - protected $numberOfVisitsConverted; + protected $numberOfVisits = false; + protected $numberOfVisitsConverted = false; - public function __construct(Parameters $params, ArchiveWriter $archiveWriter, $visits, $visitsConverted) + public function __construct(Parameters $params, ArchiveWriter $archiveWriter) { $this->params = $params; $this->logAggregator = new LogAggregator($params); $this->archiveWriter = $archiveWriter; + } + + public function setNumberOfVisits($visits, $visitsConverted) + { $this->numberOfVisits = $visits; $this->numberOfVisitsConverted = $visitsConverted; } diff --git a/core/ArchiveProcessor/Loader.php b/core/ArchiveProcessor/Loader.php index dd8c7e2fe6..9704dbf424 100644 --- a/core/ArchiveProcessor/Loader.php +++ b/core/ArchiveProcessor/Loader.php @@ -24,16 +24,6 @@ use Piwik\Period; class Loader { /** - * @var int Cached number of visits cached - */ - protected $visitsMetricCached = false; - - /** - * @var int Cached number of visits with conversions - */ - protected $convertedVisitsMetricCached = false; - - /** * Is the current archive temporary. ie. * - today * - current week / month / year @@ -57,135 +47,93 @@ class Loader $this->params = $params; } - public function prepareArchiveId() - { - $idArchive = $this->prepareArchive(); - - if ($this->isThereSomeVisits()) { - return $idArchive; - } - return false; - } - - /** - * A flag mechanism to store whether visits were selected from archive - * - * @param $visitsMetricCached - * @param bool $convertedVisitsMetricCached - */ - protected function setNumberOfVisits($visitsMetricCached, $convertedVisitsMetricCached = false) - { - if ($visitsMetricCached === false) { - $this->visitsMetricCached = $this->convertedVisitsMetricCached = false; - } else { - $this->visitsMetricCached = (int)$visitsMetricCached; - $this->convertedVisitsMetricCached = (int)$convertedVisitsMetricCached; - } - } - - protected function getNumberOfVisits() - { - return $this->visitsMetricCached; - } - - protected function getNumberOfVisitsConverted() - { - return $this->convertedVisitsMetricCached; - } - /** * @return bool */ - protected function isThereSomeVisits() + protected function isThereSomeVisits($visits) { - return $this->getNumberOfVisits() > 0; + return $visits > 0; } /** * @return bool */ - protected function isVisitsCountAlreadyProcessed() + protected function isVisitsCountAlreadyProcessed($visits) { - return $this->getNumberOfVisits() !== false; + return $visits !== false; } - protected function prepareArchive() + public function prepareArchive() { - $idArchive = $this->loadExistingArchiveIdFromDb(); + list($idArchive, $visits) = $this->loadExistingArchiveIdFromDb(); if (!empty($idArchive)) { return $idArchive; } - $this->prepareCoreMetricsArchive(); - return $this->computeNewArchive($enforceProcessCoreMetricsOnly = false); + // Archive was not found, let's see if there's any visit in this period + $this->prepareCoreMetricsArchive($visits); + + list($idArchive, $visits) = $this->computeNewArchive($enforceProcessCoreMetricsOnly = false, $visits); + + if ($this->isThereSomeVisits($visits)) { + return $idArchive; + } + return false; } - protected function prepareCoreMetricsArchive() + protected function prepareCoreMetricsArchive($visits) { $createSeparateArchiveForCoreMetrics = !$this->doesRequestedPluginIncludeVisitsSummary() - && !$this->isVisitsCountAlreadyProcessed(); + && !$this->isVisitsCountAlreadyProcessed($visits); if ($createSeparateArchiveForCoreMetrics) { $requestedPlugin = $this->params->getRequestedPlugin(); $this->params->setRequestedPlugin('VisitsSummary'); - $this->computeNewArchive($enforceProcessCoreMetricsOnly = true); + $this->computeNewArchive($enforceProcessCoreMetricsOnly = true, $visits); $this->params->setRequestedPlugin($requestedPlugin); - - if (!$this->isVisitsCountAlreadyProcessed()) { - throw new \Exception("prepareArchive() is expected to set number of visits to a numeric value."); - } } } - - protected function computeNewArchive($enforceProcessCoreMetricsOnly) + protected function computeNewArchive($enforceProcessCoreMetricsOnly, $visits) { $isArchiveDay = $this->params->isDayArchive(); $archiveWriter = new ArchiveWriter($this->params->getSite()->getId(), $this->params->getSegment(), $this->params->getPeriod(), $this->params->getRequestedPlugin(), $this->isArchiveTemporary()); $archiveWriter->initNewArchive(); - $archiveProcessor = $this->makeArchiveProcessor($archiveWriter); + $pluginsArchiver = new PluginsArchiver($archiveWriter, $this->params); - if (!$this->isVisitsCountAlreadyProcessed() + if (!$this->isVisitsCountAlreadyProcessed($visits) || $this->doesRequestedPluginIncludeVisitsSummary() || $enforceProcessCoreMetricsOnly ) { + $metrics = $pluginsArchiver->callAggregateCoreMetrics(); - if($isArchiveDay) { - $metrics = $this->aggregateDayVisitsMetrics($archiveProcessor); - } else { - $metrics = $this->aggregateMultipleVisitMetrics($archiveProcessor); - } - - if (empty($metrics)) { - $this->setNumberOfVisits(false); - } else { - $this->setNumberOfVisits($metrics['nb_visits'], $metrics['nb_visits_converted']); + if (!empty($metrics)) { + $pluginsArchiver->archiveProcessor->setNumberOfVisits($metrics['nb_visits'], $metrics['nb_visits_converted']); + $visits = $metrics['nb_visits']; } } - $this->params->logStatusDebug( $this->isArchiveTemporary() ); - $archiveProcessor = $this->makeArchiveProcessor($archiveWriter); - - if ($this->isThereSomeVisits() + if ($this->isThereSomeVisits($visits) && !$enforceProcessCoreMetricsOnly ) { - $pluginsArchiver = new PluginsArchiver($archiveProcessor); - $pluginsArchiver->callPluginsAggregate(); + $pluginsArchiver->callAggregateAllPlugins(); } + $this->params->logStatusDebug( $this->isArchiveTemporary() ); + $archiveWriter->finalizeArchive(); - if ($this->isThereSomeVisits() && !$isArchiveDay) { + if ($this->isThereSomeVisits($visits) && !$isArchiveDay) { ArchiveSelector::purgeOutdatedArchives($this->params->getPeriod()->getDateStart()); } - return $archiveWriter->getIdArchive(); + return array($archiveWriter->getIdArchive(), $visits); } protected function doesRequestedPluginIncludeVisitsSummary() @@ -217,6 +165,10 @@ class Loader */ protected function loadExistingArchiveIdFromDb() { + if ($this->isArchivingForcedToTrigger()) { + return array(false, false); + } + $minDatetimeArchiveProcessedUTC = $this->getMinTimeArchiveProcessed(); $site = $this->params->getSite(); $period = $this->params->getPeriod(); @@ -225,46 +177,9 @@ class Loader $idAndVisits = ArchiveSelector::getArchiveIdAndVisits($site, $period, $segment, $minDatetimeArchiveProcessedUTC, $requestedPlugin); if (!$idAndVisits) { - return false; + return array(false, false); } - list($idArchive, $visits, $visitsConverted) = $idAndVisits; - - if ($this->isArchivingForcedToTrigger()) { - $idArchive = false; - $visits = $visitsConverted = false; - } - - $this->setNumberOfVisits($visits, $visitsConverted); - - - return $idArchive; - } - - protected function aggregateDayVisitsMetrics(ArchiveProcessor $archiveProcessor) - { - $query = $archiveProcessor->getLogAggregator()->queryVisitsByDimension(); - $data = $query->fetch(); - - $metrics = $this->convertMetricsIdToName($data); - $archiveProcessor->insertNumericRecords($metrics); - return $metrics; - } - - protected function convertMetricsIdToName($data) - { - $metrics = array(); - foreach ($data as $metricId => $value) { - $readableMetric = Metrics::$mappingFromIdToName[$metricId]; - $metrics[$readableMetric] = $value; - } - return $metrics; - } - - protected function aggregateMultipleVisitMetrics(ArchiveProcessor $archiveProcessor) - { - $toSum = Metrics::getVisitsMetricNames(); - $metrics = $archiveProcessor->aggregateNumericMetrics($toSum); - return $metrics; + return $idAndVisits; } /** @@ -305,20 +220,5 @@ class Loader } return $this->temporaryArchive; } - - /** - * @param $archiveWriter - * @return ArchiveProcessor - */ - protected function makeArchiveProcessor($archiveWriter) - { - $archiveProcessor = new ArchiveProcessor($this->params, $archiveWriter, $this->getNumberOfVisits(), $this->getNumberOfVisitsConverted()); - - if (!$this->params->isDayArchive()) { - $subPeriods = $this->params->getPeriod()->getSubperiods(); - $archiveProcessor->archive = Archive::factory($this->params->getSegment(), $subPeriods, array($this->params->getSite()->getId())); - } - return $archiveProcessor; - } } diff --git a/core/ArchiveProcessor/PluginsArchiver.php b/core/ArchiveProcessor/PluginsArchiver.php index d707701e6c..5a0964f566 100644 --- a/core/ArchiveProcessor/PluginsArchiver.php +++ b/core/ArchiveProcessor/PluginsArchiver.php @@ -11,7 +11,10 @@ namespace Piwik\ArchiveProcessor; +use Piwik\Archive; use Piwik\ArchiveProcessor; +use Piwik\DataAccess\ArchiveWriter; +use Piwik\Metrics; use Piwik\Plugin\Archiver; /** @@ -23,19 +26,43 @@ class PluginsArchiver /** * @param ArchiveProcessor $archiveProcessor */ - protected $archiveProcessor; + public $archiveProcessor; + + /** + * @var Parameters + */ + protected $params; /** * @var Archiver[] $archivers */ private static $archivers = array(); - public function __construct(ArchiveProcessor $archiveProcessor) + public function __construct(ArchiveWriter $archiveWriter, Parameters $params) + { + $this->params = $params; + $this->archiveProcessor = $this->makeArchiveProcessor($archiveWriter); + } + + /** + * If period is day, will get the core metrics (including visits) from the logs. + * If period is != day, will sum the core metrics from the existing archives. + * @return array Core metrics + */ + public function callAggregateCoreMetrics() { - $this->archiveProcessor = $archiveProcessor; + if($this->params->isDayArchive()) { + return $this->aggregateDayVisitsMetrics(); + } + return $this->aggregateMultipleVisitsMetrics(); } - public function callPluginsAggregate() + /** + * Instantiates the Archiver class in each plugin that defines it, + * and triggers Aggregation processing on these plugins. + * + */ + public function callAggregateAllPlugins() { $pluginBeingProcessed = $this->archiveProcessor->getParams()->getRequestedPlugin(); $isAggregateForDay = $this->archiveProcessor->getParams()->isDayArchive(); @@ -105,4 +132,47 @@ class PluginsArchiver return false; } + + /** + * @param $archiveWriter + * @return ArchiveProcessor + */ + protected function makeArchiveProcessor($archiveWriter) + { + $archiveProcessor = new ArchiveProcessor($this->params, $archiveWriter); + + if (!$this->params->isDayArchive()) { + $subPeriods = $this->params->getPeriod()->getSubperiods(); + $archiveProcessor->archive = Archive::factory($this->params->getSegment(), $subPeriods, array($this->params->getSite()->getId())); + } + return $archiveProcessor; + } + + protected function aggregateDayVisitsMetrics() + { + $query = $this->archiveProcessor->getLogAggregator()->queryVisitsByDimension(); + $data = $query->fetch(); + + $metrics = $this->convertMetricsIdToName($data); + $this->archiveProcessor->insertNumericRecords($metrics); + return $metrics; + } + + protected function convertMetricsIdToName($data) + { + $metrics = array(); + foreach ($data as $metricId => $value) { + $readableMetric = Metrics::$mappingFromIdToName[$metricId]; + $metrics[$readableMetric] = $value; + } + return $metrics; + } + + protected function aggregateMultipleVisitsMetrics() + { + $toSum = Metrics::getVisitsMetricNames(); + $metrics = $this->archiveProcessor->aggregateNumericMetrics($toSum); + return $metrics; + } + }
\ No newline at end of file |