-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Add clean up task for archive metrics #23907
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
caddoo
wants to merge
5
commits into
dev-19750-timing-metrics
Choose a base branch
from
dev-19776-task
base: dev-19750-timing-metrics
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+154
−0
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
3db1b55
Add clean up task for archive metrics
caddoo 70ee8cf
Add deleted-site purge for archiving metrics
caddoo 06c20b6
Remove not needed setup/teardown
caddoo 97ecc8d
Add strict types
caddoo f07ba9e
Merge branch 'dev-19750-timing-metrics' into dev-19776-task
caddoo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * Matomo - free/libre analytics platform | ||
| * | ||
| * @link https://matomo.org | ||
| * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later | ||
| */ | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Piwik\Plugins\ArchivingMetrics; | ||
|
|
||
| use Piwik\Common; | ||
| use Piwik\Config; | ||
| use Piwik\Date; | ||
| use Piwik\Db; | ||
|
|
||
| class Tasks extends \Piwik\Plugin\Tasks | ||
| { | ||
| public const DEFAULT_RETENTION_DAYS = 180; | ||
|
|
||
| public function schedule() | ||
| { | ||
| $this->weekly('purgeOldMetrics'); | ||
| $this->monthly('purgeMetricsForDeletedSites'); | ||
| } | ||
|
|
||
| /** | ||
| * To test execute the following command: | ||
| * `./console core:run-scheduled-tasks --force "Piwik\Plugins\ArchivingMetrics\Tasks.purgeOldMetrics"` | ||
| */ | ||
| public function purgeOldMetrics() | ||
| { | ||
| $retentionDays = $this->getRetentionDays(); | ||
| if ($retentionDays <= 0) { | ||
| return; | ||
| } | ||
|
|
||
| $cutoff = Date::now()->subDay($retentionDays)->getDatetime(); | ||
| $table = Common::prefixTable('archiving_metrics'); | ||
| Db::query("DELETE FROM {$table} WHERE ts_started < ?", [$cutoff]); | ||
| } | ||
|
|
||
| public function purgeMetricsForDeletedSites() | ||
| { | ||
| $siteTable = Common::prefixTable('site'); | ||
| $table = Common::prefixTable('archiving_metrics'); | ||
| Db::query("DELETE a FROM {$table} a LEFT JOIN {$siteTable} s ON a.idsite = s.idsite WHERE s.idsite IS NULL"); | ||
| } | ||
|
|
||
| private function getRetentionDays(): int | ||
| { | ||
| $config = Config::getInstance(); | ||
| $retentionDays = $config->ArchivingMetrics['retention_days'] ?? self::DEFAULT_RETENTION_DAYS; | ||
| return max(0, (int) $retentionDays); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * Matomo - free/libre analytics platform | ||
| * | ||
| * @link https://matomo.org | ||
| * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later | ||
| */ | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Piwik\Plugins\ArchivingMetrics\tests\Integration; | ||
|
|
||
| use Piwik\Common; | ||
| use Piwik\Config; | ||
| use Piwik\Date; | ||
| use Piwik\Db; | ||
| use Piwik\Plugins\ArchivingMetrics\Tasks; | ||
| use Piwik\Tests\Framework\Fixture; | ||
| use Piwik\Tests\Framework\TestCase\IntegrationTestCase; | ||
|
|
||
| /** | ||
| * @group ArchivingMetrics | ||
| * @group ArchivingMetrics_Tasks | ||
| * @group Plugins | ||
| */ | ||
| class TasksTest extends IntegrationTestCase | ||
| { | ||
| public function testPurgeOldMetricsDeletesRowsOlderThanRetention(): void | ||
| { | ||
| $config = Config::getInstance(); | ||
| $config->ArchivingMetrics = ['retention_days' => 30]; | ||
|
|
||
| $this->insertRow(Date::now()->subDay(31)->getDatetime()); | ||
| $this->insertRow(Date::now()->subDay(5)->getDatetime()); | ||
|
|
||
| $task = new Tasks(); | ||
| $task->purgeOldMetrics(); | ||
|
|
||
| $count = (int) Db::fetchOne('SELECT COUNT(*) FROM ' . Common::prefixTable('archiving_metrics')); | ||
| $this->assertSame(1, $count); | ||
| } | ||
|
|
||
| public function testPurgeOldMetricsDisabledKeepsRows(): void | ||
| { | ||
| $config = Config::getInstance(); | ||
| $config->ArchivingMetrics = ['retention_days' => 0]; | ||
|
|
||
| $this->insertRow(Date::now()->subDay(400)->getDatetime()); | ||
|
|
||
| $task = new Tasks(); | ||
| $task->purgeOldMetrics(); | ||
|
|
||
| $count = (int) Db::fetchOne('SELECT COUNT(*) FROM ' . Common::prefixTable('archiving_metrics')); | ||
| $this->assertSame(1, $count); | ||
| } | ||
|
|
||
| public function testPurgeMetricsForDeletedSitesRemovesOrphanedRows(): void | ||
| { | ||
| $idSite = Fixture::createWebsite('2024-01-01 00:00:00'); | ||
|
|
||
| $this->insertRow(Date::now()->subDay(5)->getDatetime(), $idSite); | ||
| $this->insertRow(Date::now()->subDay(5)->getDatetime(), 9999); | ||
|
|
||
| $task = new Tasks(); | ||
| $task->purgeMetricsForDeletedSites(); | ||
|
|
||
| $count = (int) Db::fetchOne('SELECT COUNT(*) FROM ' . Common::prefixTable('archiving_metrics')); | ||
| $this->assertSame(1, $count); | ||
| } | ||
|
|
||
| private function insertRow(string $tsStarted, int $idSite = 1): void | ||
| { | ||
| $table = Common::prefixTable('archiving_metrics'); | ||
| Db::query( | ||
| "INSERT INTO {$table} (idarchive, idsite, archive_name, date1, date2, period, ts_started, ts_finished, total_time, total_time_exclusive) | ||
| VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", | ||
| [ | ||
| 1, | ||
| $idSite, | ||
| 'done', | ||
| '2025-01-01', | ||
| '2025-01-01', | ||
| 1, | ||
| $tsStarted, | ||
| $tsStarted, | ||
| 123, | ||
| 100, | ||
| ] | ||
| ); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.