diff options
author | m.kurzeja <m.kurzeja@clearcode.cc> | 2014-09-08 12:06:16 +0400 |
---|---|---|
committer | m.kurzeja <m.kurzeja@clearcode.cc> | 2014-09-08 12:06:16 +0400 |
commit | 395862b1e076c4072521291c6c702c2e2e94a7db (patch) | |
tree | a3e38da3b9a2c3a3bc43509b61b5bae2a5cfc7a8 | |
parent | c5eca515544f358e9ec09bd0c81e7cf2d4be97b1 (diff) |
Added purging of invalidated archives in scheduled tasks
-rw-r--r-- | core/DataAccess/ArchivePurger.php | 30 | ||||
-rw-r--r-- | plugins/CoreAdminHome/API.php | 6 | ||||
-rw-r--r-- | plugins/CoreAdminHome/Tasks.php | 8 |
3 files changed, 42 insertions, 2 deletions
diff --git a/core/DataAccess/ArchivePurger.php b/core/DataAccess/ArchivePurger.php index e0f94409a1..b65e8ab919 100644 --- a/core/DataAccess/ArchivePurger.php +++ b/core/DataAccess/ArchivePurger.php @@ -22,6 +22,36 @@ use Piwik\Piwik; */ class ArchivePurger { + public static function purgeInvalidatedArchives() + { + $archiveTables = ArchiveTableCreator::getTablesArchivesInstalled(); + + foreach ($archiveTables as $archiveTable) { + $query = ' + SELECT t1.idarchive FROM `' . $archiveTable . '` t1 + INNER JOIN `piwik_archive_numeric_2014_09` t2 + ON t1.idarchive != t2.idarchive AND t1.name = t2.name AND t1.idsite=t2.idsite + AND t1.date1=t2.date1 AND t1.date2=t2.date2 AND t1.period=t2.period + WHERE t1.value = 4 AND t2.value IN(' . ArchiveWriter::DONE_OK . ', ' . ArchiveWriter::DONE_OK_TEMPORARY . ') AND t1.name LIKE \'done%\''; + $result = Db::fetchAll($query); + + if (count($result) > 0) { + $archiveIds = array_map( + function ($elm) { + return $elm['idarchive']; + }, + $result + ); + + Db::query( + 'DELETE FROM `' . $archiveTable . '` WHERE idarchive IN (' . implode(', ', $archiveIds) . ')' + ); + } + + } + } + + public static function purgeOutdatedArchives(Date $dateStart) { $purgeArchivesOlderThan = Rules::shouldPurgeOutdatedArchives($dateStart); diff --git a/plugins/CoreAdminHome/API.php b/plugins/CoreAdminHome/API.php index e18bd3f031..7abceb2430 100644 --- a/plugins/CoreAdminHome/API.php +++ b/plugins/CoreAdminHome/API.php @@ -10,6 +10,7 @@ namespace Piwik\Plugins\CoreAdminHome; use Exception; use Piwik\DataAccess\ArchiveTableCreator; +use Piwik\DataAccess\ArchiveWriter; use Piwik\Date; use Piwik\Db; use Piwik\Option; @@ -152,13 +153,14 @@ class API extends \Piwik\Plugin\API $sql = $bind = array(); $datesToDeleteInTable = array_unique($datesToDeleteInTable); foreach ($datesToDeleteInTable as $dateToDelete) { - $sql[] = '(date1 <= ? AND ? <= date2)'; + $sql[] = '(date1 <= ? AND ? <= date2 AND name LIKE \'done%\')'; $bind[] = $dateToDelete; $bind[] = $dateToDelete; } $sql = implode(" OR ", $sql); - $query = "DELETE FROM $table " . + $query = "UPDATE $table " . + " SET value = " . ArchiveWriter::DONE_INVALIDATED . " WHERE ( $sql ) " . " AND idsite IN (" . implode(",", $idSites) . ")"; Db::query($query, $bind); diff --git a/plugins/CoreAdminHome/Tasks.php b/plugins/CoreAdminHome/Tasks.php index 5f08b4f096..3f82dba9e6 100644 --- a/plugins/CoreAdminHome/Tasks.php +++ b/plugins/CoreAdminHome/Tasks.php @@ -20,6 +20,9 @@ class Tasks extends \Piwik\Plugin\Tasks // general data purge on older archive tables, executed daily $this->daily('purgeOutdatedArchives', null, self::HIGH_PRIORITY); + // general data purge on invalidated archive records, executed daily + $this->daily('purgeInvalidatedArchives', null, self::HIGH_PRIORITY); + // lowest priority since tables should be optimized after they are modified $this->daily('optimizeArchiveTable', null, self::LOWEST_PRIORITY); } @@ -38,6 +41,11 @@ class Tasks extends \Piwik\Plugin\Tasks } } + public function purgeInvalidatedArchives() + { + ArchivePurger::purgeInvalidatedArchives(); + } + public function optimizeArchiveTable() { $archiveTables = ArchiveTableCreator::getTablesArchivesInstalled(); |