Skip to content

Commit

Permalink
Merge pull request #7113 from piwik/allow_disable_range_archiving
Browse files Browse the repository at this point in the history
Add undocumented (ie, unsupported) INI config option to disable forcing range archiving on browser request.
  • Loading branch information
diosmosis committed Feb 4, 2015
2 parents 9f2728e + 98cf703 commit f2fc752
Show file tree
Hide file tree
Showing 3 changed files with 155 additions and 4 deletions.
19 changes: 15 additions & 4 deletions core/ArchiveProcessor/Rules.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

use Exception;
use Piwik\Config;
use Piwik\Container\StaticContainer;
use Piwik\DataAccess\ArchiveWriter;
use Piwik\Date;
use Piwik\Log;
Expand Down Expand Up @@ -228,18 +229,28 @@ public static function getTodayArchiveTimeToLiveDefault()

public static function isArchivingDisabledFor(array $idSites, Segment $segment, $periodLabel)
{
$generalConfig = Config::getInstance()->General;

if ($periodLabel == 'range') {
return false;
if (!isset($generalConfig['archiving_range_force_on_browser_request'])
|| $generalConfig['archiving_range_force_on_browser_request'] != false
) {
return false;
} else {
Log::debug("Not forcing archiving for range period.");
}
}

$processOneReportOnly = !self::shouldProcessReportsAllPlugins($idSites, $segment, $periodLabel);
$isArchivingDisabled = !self::isRequestAuthorizedToArchive() || self::$archivingDisabledByTests;

if ($processOneReportOnly) {

if ($processOneReportOnly
&& $periodLabel != 'range'
) {
// When there is a segment, we disable archiving when browser_archiving_disabled_enforce applies
if (!$segment->isEmpty()
&& $isArchivingDisabled
&& Config::getInstance()->General['browser_archiving_disabled_enforce']
&& $generalConfig['browser_archiving_disabled_enforce']
&& !SettingsServer::isArchivePhpTriggered() // Only applies when we are not running core:archive command
) {
Log::debug("Archiving is disabled because of config setting browser_archiving_disabled_enforce=1");
Expand Down
6 changes: 6 additions & 0 deletions tests/PHPUnit/Framework/TestCase/IntegrationTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public static function setUpBeforeClass()
{
static::configureFixture(static::$fixture);
parent::setUpBeforeClass();
static::beforeTableDataCached();

self::$tableData = self::getDbTablesWithData();
}
Expand Down Expand Up @@ -98,6 +99,11 @@ protected static function configureFixture($fixture)
$fixture->createSuperUser = false;
$fixture->configureComponents = false;
}

protected static function beforeTableDataCached()
{
// empty
}
}

IntegrationTestCase::$fixture = new Fixture();
134 changes: 134 additions & 0 deletions tests/PHPUnit/Integration/ArchiveTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
<?php
/**
* Piwik - free/libre analytics platform
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
namespace Piwik\Tests\Integration;

use Piwik\Archive;
use Piwik\ArchiveProcessor\Rules;
use Piwik\Common;
use Piwik\Config;
use Piwik\DataAccess\ArchiveTableCreator;
use Piwik\Date;
use Piwik\Db;
use Piwik\Piwik;
use Piwik\Tests\Fixtures\OneVisitorTwoVisits;
use Piwik\Tests\Framework\Fixture;
use Piwik\Tests\Framework\TestCase\IntegrationTestCase;

/**
* @group Core
*/
class ArchiveTest extends IntegrationTestCase
{
/**
* @var OneVisitorTwoVisits
*/
public static $fixture;

public function tearDown()
{
parent::tearDown();

unset($_GET['trigger']);
}

protected static function configureFixture($fixture)
{
$fixture->createSuperUser = true;
}

protected static function beforeTableDataCached()
{
$date = Date::factory('2010-03-01');

$archiveTableCreator = new ArchiveTableCreator();
$archiveTableCreator->getBlobTable($date);
$archiveTableCreator->getNumericTable($date);
}

public function getForceOptionsForForceArchivingOnBrowserRequest()
{
return array(
array(1),
array(null)
);
}

/**
* @dataProvider getForceOptionsForForceArchivingOnBrowserRequest
*/
public function test_ArchivingIsLaunchedForRanges_WhenForceOnBrowserRequest_IsTruthy($optionValue)
{
$this->archiveDataForIndividualDays();

Config::getInstance()->General['archiving_range_force_on_browser_request'] = $optionValue;
Rules::setBrowserTriggerArchiving(false);

$data = $this->initiateArchivingForRange();

$this->assertNotEmpty($data);
$this->assertArchiveTablesAreNotEmpty('2010_03');
}

public function test_ArchivingIsNotLaunchedForRanges_WhenForceOnBrowserRequest_IsFalse()
{
$this->archiveDataForIndividualDays();

Config::getInstance()->General['archiving_range_force_on_browser_request'] = 0;
Rules::setBrowserTriggerArchiving(false);

$data = $this->initiateArchivingForRange();

$this->assertEmpty($data);
$this->assertArchiveTablesAreEmpty('2010_03');
}

public function test_ArchiveIsLaunched_WhenForceOnBrowserRequest_IsFalse_AndArchivePhpTriggered()
{
$this->archiveDataForIndividualDays();

Config::getInstance()->General['archiving_range_force_on_browser_request'] = 0;
$_GET['trigger'] = 'archivephp';
Rules::setBrowserTriggerArchiving(false);

$data = $this->initiateArchivingForRange();

$this->assertNotEmpty($data);
$this->assertArchiveTablesAreNotEmpty('2010_03');
}

private function assertArchiveTablesAreNotEmpty($tableMonth)
{
$this->assertNotEquals(0, $this->getRangeArchiveTableCount('archive_numeric', $tableMonth));
}

private function assertArchiveTablesAreEmpty($tableMonth)
{
$this->assertEquals(0, $this->getRangeArchiveTableCount('archive_numeric', $tableMonth));
$this->assertEquals(0, $this->getRangeArchiveTableCount('archive_blob', $tableMonth));
}

private function getRangeArchiveTableCount($tableType, $tableMonth)
{
$table = Common::prefixTable($tableType . '_' . $tableMonth);
return Db::fetchOne("SELECT COUNT(*) FROM $table WHERE period = " . Piwik::$idPeriods['range']);
}

private function initiateArchivingForRange()
{
$archive = Archive::build(self::$fixture->idSite, 'range', '2010-03-04,2010-03-07');
return $archive->getNumeric('nb_visits');
}

private function archiveDataForIndividualDays()
{
$archive = Archive::build(self::$fixture->idSite, 'day', '2010-03-04,2010-03-07');
return $archive->getNumeric('nb_visits');
}
}

ArchiveTest::$fixture = new OneVisitorTwoVisits();

0 comments on commit f2fc752

Please sign in to comment.