From a1c74a9043812cd133d65a5a23a4a72ae0cf1928 Mon Sep 17 00:00:00 2001 From: Thomas Steur Date: Sun, 21 Dec 2014 23:43:30 +0100 Subject: invalidate reports if needed when building an archive requested via web --- core/Archive.php | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) (limited to 'core/Archive.php') diff --git a/core/Archive.php b/core/Archive.php index b407465a45..76a619f0bb 100644 --- a/core/Archive.php +++ b/core/Archive.php @@ -10,8 +10,10 @@ namespace Piwik; use Piwik\Archive\Parameters; use Piwik\ArchiveProcessor\Rules; +use Piwik\DataAccess\ArchiveInvalidator; use Piwik\DataAccess\ArchiveSelector; use Piwik\Period\Factory as PeriodFactory; +use Piwik\Plugins\CoreAdminHome\API as CoreAdminHomeApi; /** * The **Archive** class is used to query cached analytics statistics @@ -160,6 +162,11 @@ class Archive */ private $params; + /** + * \Piwik\Cache\Cache + */ + private static $cache; + /** * @param Parameters $params * @param bool $forceIndexedBySite Whether to force index the result of a query by site ID. @@ -480,6 +487,32 @@ class Archive return $recordName . "_" . $id; } + private function invalidatedReportsIfNeeded() + { + if (is_null(self::$cache)) { + self::$cache = Cache::getTransientCache(); + } + + $id = 'Archive.RememberedReportsInvalidated'; + + if (self::$cache->contains($id)) { + return; + } + + self::$cache->save($id, 1); + + $invalidator = new ArchiveInvalidator(); + $sitesPerDays = $invalidator->getRememberedArchivedReportsThatShouldBeInvalidated(); + + foreach ($sitesPerDays as $date => $siteIds) { + if (!empty($siteIds)) { + // an advanced version would only invalidate siteIds for $this->params->getIdSites() but would make + // everything way more complex eg the cache above and which siteIds we pass here... + CoreAdminHomeApi::getInstance()->invalidateArchivedReports($siteIds, $date); + } + } + } + /** * Queries archive tables for data and returns the result. * @param array|string $archiveNames @@ -489,6 +522,8 @@ class Archive */ private function get($archiveNames, $archiveDataType, $idSubtable = null) { + $this->invalidatedReportsIfNeeded(); + if (!is_array($archiveNames)) { $archiveNames = array($archiveNames); } -- cgit v1.2.3