Skip to content

Commit

Permalink
refs #5054 some code improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
tsteur committed May 7, 2014
1 parent da9cd11 commit 49cb0c5
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 59 deletions.
3 changes: 0 additions & 3 deletions plugins/PrivacyManager/ReportsPurger.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ public function __construct($deleteReportsOlderThan, $keepBasicMetrics, $reportP
*/
public function purgeData($optimize = false)
{
// find archive tables to purge
list($oldNumericTables, $oldBlobTables) = $this->getArchiveTablesToPurge();

// process blob tables first, since archive status is stored in the numeric archives
Expand All @@ -122,9 +121,7 @@ public function purgeData($optimize = false)
}
}

// deal with numeric tables
if (!empty($oldNumericTables)) {

foreach ($oldNumericTables as $table) {
$conditions = array("name NOT LIKE 'done%'");
$bind = array();
Expand Down
93 changes: 37 additions & 56 deletions tests/PHPUnit/Integration/PurgeDataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,9 @@ public function test_purgeData_keepAllExceptDay()
$this->assertHasOneDownload('year');

$deleteReportsOlderThan = 1;
$keepBasicMetrics = true;
$reportPeriodsToKeep = array(2,3,4,5);
$purger = $this->createReportsPurger($deleteReportsOlderThan, $reportPeriodsToKeep, $keepBasicMetrics);
$purger->purgeData();
$keepBasicMetrics = true;
$reportPeriodsToKeep = array(2,3,4,5);
$this->purgeData($deleteReportsOlderThan, $reportPeriodsToKeep, $keepBasicMetrics);

$this->assertHasNoDownload('day');
$this->assertHasOneDownload('week');
Expand All @@ -56,11 +55,12 @@ public function test_purgeData_keepOnlyDay()
$this->assertHasOneDownload('year');

$deleteReportsOlderThan = 1;
$keepBasicMetrics = true;
$reportPeriodsToKeep = array(1);
$purger = $this->createReportsPurger($deleteReportsOlderThan, $reportPeriodsToKeep, $keepBasicMetrics);
$purger->purgeData();
$keepBasicMetrics = true;
$reportPeriodsToKeep = array(1);
$this->purgeData($deleteReportsOlderThan, $reportPeriodsToKeep, $keepBasicMetrics);

$this->assertNumVisits(2, 'day');
$this->assertNumVisits(2, 'week');
$this->assertHasOneDownload('day');
$this->assertHasNoDownload('week');
$this->assertHasNoDownload('month');
Expand All @@ -75,10 +75,9 @@ public function test_purgeData_shouldNotPurgeAnything_IfDeleteReportsOlderThanIs
$this->assertHasOneDownload('year');

$deleteReportsOlderThan = 1000;
$keepBasicMetrics = true;
$reportPeriodsToKeep = array(1,2,3,4,5);
$purger = $this->createReportsPurger($deleteReportsOlderThan, $reportPeriodsToKeep, $keepBasicMetrics);
$purger->purgeData();
$keepBasicMetrics = true;
$reportPeriodsToKeep = array(1,2,3,4,5);
$this->purgeData($deleteReportsOlderThan, $reportPeriodsToKeep, $keepBasicMetrics);

$this->assertHasOneDownload('day');
$this->assertHasOneDownload('week');
Expand All @@ -94,10 +93,9 @@ public function test_purgeData_shouldPurgeAllPeriodsExceptBasicMetrics_IfNoPerio
$this->assertHasOneDownload('year');

$deleteReportsOlderThan = 1;
$keepBasicMetrics = true;
$reportPeriodsToKeep = array();
$purger = $this->createReportsPurger($deleteReportsOlderThan, $reportPeriodsToKeep, $keepBasicMetrics);
$purger->purgeData();
$keepBasicMetrics = true;
$reportPeriodsToKeep = array();
$this->purgeData($deleteReportsOlderThan, $reportPeriodsToKeep, $keepBasicMetrics);

$this->assertNumVisits(2, 'day');
$this->assertNumVisits(2, 'week');
Expand All @@ -117,10 +115,9 @@ public function test_purgeData_shouldPurgeEverything_IfNoPeriodToKeepIsGivenAndB
$this->assertHasOneDownload('year');

$deleteReportsOlderThan = 1;
$keepBasicMetrics = false;
$reportPeriodsToKeep = array();
$purger = $this->createReportsPurger($deleteReportsOlderThan, $reportPeriodsToKeep, $keepBasicMetrics);
$purger->purgeData();
$keepBasicMetrics = false;
$reportPeriodsToKeep = array();
$this->purgeData($deleteReportsOlderThan, $reportPeriodsToKeep, $keepBasicMetrics);

$this->assertNumVisits(0, 'day');
$this->assertNumVisits(0, 'week');
Expand All @@ -132,42 +129,6 @@ public function test_purgeData_shouldPurgeEverything_IfNoPeriodToKeepIsGivenAndB
$this->assertHasNoDownload('year');
}

private function getDownloadApiRequestUrl($period)
{
return 'method=Actions.getDownloads'
. '&idSite=' . self::$fixture->idSite
. '&date=' . self::$fixture->dateTime
. '&period='. $period
. '&format=original';
}

private function createReportsPurger($deleteReportsOlderThan, $reportPeriodsToKeep, $keepBasicMetrics)
{
$metricsToKeep = PrivacyManager::getAllMetricsToKeep();
$maxRowsToDeletePerQuery = 100000;
$keepSegmentReports = false;

return new ReportsPurger($deleteReportsOlderThan, $keepBasicMetrics, $reportPeriodsToKeep,
$keepSegmentReports, $metricsToKeep, $maxRowsToDeletePerQuery);
}

public function getApiForTesting()
{
$idSite = self::$fixture->idSite;
$dateTime = self::$fixture->dateTime;

$apiToCall = array('Actions.getDownloads');

$apiToTest = array(
array($apiToCall,
array('idSite' => $idSite,
'date' => $dateTime,
'periods' => array('month')))
);

return $apiToTest;
}

private function assertNumVisits($expectedNumVisits, $period)
{
$url = 'method=VisitsSummary.getVisits'
Expand All @@ -193,6 +154,26 @@ private function assertHasNoDownload($period)
$table = $api->process();
$this->assertEquals(0, $table->getRowsCount(), $period . ' should not have a download but has one');
}

private function getDownloadApiRequestUrl($period)
{
return 'method=Actions.getDownloads'
. '&idSite=' . self::$fixture->idSite
. '&date=' . self::$fixture->dateTime
. '&period='. $period
. '&format=original';
}

private function purgeData($deleteReportsOlderThan, $reportPeriodsToKeep, $keepBasicMetrics)
{
$metricsToKeep = PrivacyManager::getAllMetricsToKeep();
$maxRowsToDeletePerQuery = 100000;
$keepSegmentReports = false;

$purger = new ReportsPurger($deleteReportsOlderThan, $keepBasicMetrics, $reportPeriodsToKeep,
$keepSegmentReports, $metricsToKeep, $maxRowsToDeletePerQuery);
$purger->purgeData();
}
}

Test_Piwik_Integration_PurgeDataTest::$fixture = new Test_Piwik_Fixture_OneVisitorTwoVisits();
Expand Down

0 comments on commit 49cb0c5

Please sign in to comment.