Skip to content
Extraits de code Groupes Projets
Valider 25412712 rédigé par diosmosis's avatar diosmosis
Parcourir les fichiers

Support lastN in database:optimize-archive-tables and add a test for the command.

parent bc218dc6
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
...@@ -33,7 +33,7 @@ class ArchiveTableCreator ...@@ -33,7 +33,7 @@ class ArchiveTableCreator
protected static function getTable(Date $date, $type) protected static function getTable(Date $date, $type)
{ {
$tableNamePrefix = "archive_" . $type; $tableNamePrefix = "archive_" . $type;
$tableName = $tableNamePrefix . "_" . $date->toString('Y_m'); $tableName = $tableNamePrefix . "_" . self::getTableMonthFromDate($date);
$tableName = Common::prefixTable($tableName); $tableName = Common::prefixTable($tableName);
self::createArchiveTablesIfAbsent($tableName, $tableNamePrefix); self::createArchiveTablesIfAbsent($tableName, $tableNamePrefix);
...@@ -100,6 +100,11 @@ class ArchiveTableCreator ...@@ -100,6 +100,11 @@ class ArchiveTableCreator
return $date; return $date;
} }
public static function getTableMonthFromDate(Date $date)
{
return $date->toString('Y_m');
}
public static function getTypeFromTableName($tableName) public static function getTypeFromTableName($tableName)
{ {
if (strpos($tableName, 'archive_numeric_') !== false) { if (strpos($tableName, 'archive_numeric_') !== false) {
......
...@@ -15,6 +15,7 @@ use Piwik\Db; ...@@ -15,6 +15,7 @@ use Piwik\Db;
use Piwik\Plugin\ConsoleCommand; use Piwik\Plugin\ConsoleCommand;
use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Output\OutputInterface;
/** /**
...@@ -32,6 +33,7 @@ class OptimizeArchiveTables extends ConsoleCommand ...@@ -32,6 +33,7 @@ class OptimizeArchiveTables extends ConsoleCommand
$this->addArgument("dates", InputArgument::IS_ARRAY | InputArgument::REQUIRED, $this->addArgument("dates", InputArgument::IS_ARRAY | InputArgument::REQUIRED,
"The months of the archive tables to optimize. Use '" . self::ALL_TABLES_STRING. "' for all dates or '" . "The months of the archive tables to optimize. Use '" . self::ALL_TABLES_STRING. "' for all dates or '" .
self::CURRENT_MONTH_STRING . "' to optimize the current month only."); self::CURRENT_MONTH_STRING . "' to optimize the current month only.");
$this->addOption('dry-run', null, InputOption::VALUE_NONE, 'For testing purposes.');
$this->setHelp("This command can be used to ease or automate maintenance. Instead of manually running " $this->setHelp("This command can be used to ease or automate maintenance. Instead of manually running "
. "OPTIMIZE TABLE queries, the command can be used.\n\nYou should run the command if you find your " . "OPTIMIZE TABLE queries, the command can be used.\n\nYou should run the command if you find your "
. "archive tables grow and do not shrink after purging. Optimizing them will reclaim some space."); . "archive tables grow and do not shrink after purging. Optimizing them will reclaim some space.");
...@@ -39,20 +41,25 @@ class OptimizeArchiveTables extends ConsoleCommand ...@@ -39,20 +41,25 @@ class OptimizeArchiveTables extends ConsoleCommand
protected function execute(InputInterface $input, OutputInterface $output) protected function execute(InputInterface $input, OutputInterface $output)
{ {
$dryRun = $input->getOption('dry-run');
$tableMonths = $this->getTableMonthsToOptimize($input); $tableMonths = $this->getTableMonthsToOptimize($input);
foreach ($tableMonths as $month) { foreach ($tableMonths as $month) {
$this->optimizeTable($output, 'archive_numeric_' . $month); $this->optimizeTable($output, $dryRun, 'archive_numeric_' . $month);
$this->optimizeTable($output, 'archive_blob_' . $month); $this->optimizeTable($output, $dryRun, 'archive_blob_' . $month);
} }
} }
private function optimizeTable(OutputInterface $output, $table) private function optimizeTable(OutputInterface $output, $dryRun, $table)
{ {
$output->write("Optimizing table '$table'..."); $output->write("Optimizing table '$table'...");
$table = Common::prefixTable($table); if ($dryRun) {
Db::optimizeTables($table, $force = true); $output->write("[dry-run, not optimising table]");
} else {
Db::optimizeTables(Common::prefixTable($table), $force = true);
}
$output->writeln("Done."); $output->writeln("Done.");
} }
...@@ -67,14 +74,25 @@ class OptimizeArchiveTables extends ConsoleCommand ...@@ -67,14 +74,25 @@ class OptimizeArchiveTables extends ConsoleCommand
return $this->getAllArchiveTableMonths(); return $this->getAllArchiveTableMonths();
} else if ($dateSpecifier == self::CURRENT_MONTH_STRING) { } else if ($dateSpecifier == self::CURRENT_MONTH_STRING) {
$now = Date::factory('now'); $now = Date::factory('now');
return array($now->toString('Y') . '_' . $now->toString('m')); return array(ArchiveTableCreator::getTableMonthFromDate($now));
} else if (strpos($dateSpecifier, 'last') === 0) {
$lastN = substr($dateSpecifier, 4);
if (!ctype_digit($lastN)) {
throw new \Exception("Invalid lastN specifier '$lastN'. The end must be an integer, eg, last1 or last2.");
}
if ($lastN <= 0) {
throw new \Exception("Invalid lastN value '$lastN'.");
}
return $this->getLastNTableMonths((int)$lastN);
} }
} }
$tableMonths = array(); $tableMonths = array();
foreach ($dateSpecifier as $date) { foreach ($dateSpecifier as $date) {
$date = Date::factory($date); $date = Date::factory($date);
$tableMonths[] = $date->toString('Y') . '_' . $date->toString('m'); $tableMonths[] = ArchiveTableCreator::getTableMonthFromDate($date);
} }
return $tableMonths; return $tableMonths;
} }
...@@ -87,4 +105,20 @@ class OptimizeArchiveTables extends ConsoleCommand ...@@ -87,4 +105,20 @@ class OptimizeArchiveTables extends ConsoleCommand
} }
return $tableMonths; return $tableMonths;
} }
/**
* @param int $lastN
* @return string[]
*/
private function getLastNTableMonths($lastN)
{
$now = Date::factory('now');
$result = array();
for ($i = 0; $i < $lastN; ++$i) {
$date = $now->subMonth($i + 1);
$result[] = ArchiveTableCreator::getTableMonthFromDate($date);
}
return $result;
}
} }
<?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\Plugins\CoreAdminHome\tests\Integration\Commands;
use Piwik\DataAccess\ArchiveTableCreator;
use Piwik\Date;
use Piwik\Tests\Framework\TestCase\ConsoleCommandTestCase;
/**
* @group Core
*/
class OptimizeArchiveTablesTest extends ConsoleCommandTestCase
{
public static function setUpBeforeClass()
{
parent::setUpBeforeClass();
ArchiveTableCreator::getNumericTable(Date::factory('2015-01-01'));
ArchiveTableCreator::getNumericTable(Date::factory('2015-02-02'));
ArchiveTableCreator::getNumericTable(Date::factory('2015-03-03'));
}
/**
* @dataProvider getDatesToTest
*/
public function test_Command_OptimizesCorrectTables($dates, $expectedOptimizedTableDates)
{
$code = $this->applicationTester->run(array(
'command' => 'database:optimize-archive-tables',
'dates' => $dates,
'--dry-run' => true,
));
$this->assertEquals(0, $code, $this->getCommandDisplayOutputErrorMessage());
$output = $this->applicationTester->getDisplay();
preg_match_all('/Optimizing table \'([^\']+)\'/', $output, $matches);
$tablesOptimized = $matches[1];
$expectedOptimizedTables = array();
foreach ($expectedOptimizedTableDates as $date) {
$expectedOptimizedTables[] = 'archive_numeric_' . $date;
$expectedOptimizedTables[] = 'archive_blob_' . $date;
}
$this->assertEquals($expectedOptimizedTables, $tablesOptimized);
}
public function getDatesToTest()
{
return array(
array(
array('all'),
array('2015_01', '2015_02', '2015_03'),
),
array(
array('now'),
array(date('Y_m')),
),
array(
array('2015-01-01', '2015-02-03', '2014-01-01', '2013-05-12', '2015-05-05'),
array('2015_01', '2015_02', '2014_01', '2013_05', '2015_05'),
),
array(
array('last1'),
array(Date::factory('now')->subMonth(1)->toString('Y_m')),
),
array(
array('last5'),
array(
Date::factory('now')->subMonth(1)->toString('Y_m'),
Date::factory('now')->subMonth(2)->toString('Y_m'),
Date::factory('now')->subMonth(3)->toString('Y_m'),
Date::factory('now')->subMonth(4)->toString('Y_m'),
Date::factory('now')->subMonth(5)->toString('Y_m'),
),
),
);
}
}
\ No newline at end of file
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Veuillez vous inscrire ou vous pour commenter