Skip to content

Commit

Permalink
Move progress related code to a dedicated class
Browse files Browse the repository at this point in the history
  • Loading branch information
Quetzacoalt91 committed Jul 18, 2024
1 parent da6f226 commit c96da96
Show file tree
Hide file tree
Showing 21 changed files with 176 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0)
*/

namespace PrestaShop\Module\AutoUpgrade\UpgradeTools;
namespace PrestaShop\Module\AutoUpgrade\Progress;

class Backlog
{
Expand Down
109 changes: 109 additions & 0 deletions classes/Progress/CompletionCalculator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?php

/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License 3.0 (AFL-3.0)
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/AFL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to [email protected] so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to https://devdocs.prestashop.com/ for more information.
*
* @author PrestaShop SA and Contributors <[email protected]>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0)
*/

namespace PrestaShop\Module\AutoUpgrade\Progress;

use PrestaShop\Module\AutoUpgrade\Parameters\UpgradeConfiguration;
use PrestaShop\Module\AutoUpgrade\Task\Rollback\RestoreDb;
use PrestaShop\Module\AutoUpgrade\Task\Rollback\RestoreFiles;
use PrestaShop\Module\AutoUpgrade\Task\Rollback\Rollback;
use PrestaShop\Module\AutoUpgrade\Task\Rollback\RollbackComplete;
use PrestaShop\Module\AutoUpgrade\Task\Upgrade\BackupDb;
use PrestaShop\Module\AutoUpgrade\Task\Upgrade\BackupFiles;
use PrestaShop\Module\AutoUpgrade\Task\Upgrade\CleanDatabase;
use PrestaShop\Module\AutoUpgrade\Task\Upgrade\Download;
use PrestaShop\Module\AutoUpgrade\Task\Upgrade\Unzip;
use PrestaShop\Module\AutoUpgrade\Task\Upgrade\UpgradeComplete;
use PrestaShop\Module\AutoUpgrade\Task\Upgrade\UpgradeDb;
use PrestaShop\Module\AutoUpgrade\Task\Upgrade\UpgradeFiles;
use PrestaShop\Module\AutoUpgrade\Task\Upgrade\UpgradeModules;
use PrestaShop\Module\AutoUpgrade\Task\Upgrade\UpgradeNow;

class CompletionCalculator
{
/** @var UpgradeConfiguration */
private $upgradeConfiguration;

public function __construct(UpgradeConfiguration $upgradeConfiguration)
{
$this->upgradeConfiguration = $upgradeConfiguration;
}

/**
* The key baseWithoutBackup exists while the backup and upgrade are on the same workflow
*
* @return array<string, array{base:int, baseWithoutBackup:int|null}>
*/
private static function getPercentages(): array
{
return [
// Upgrade (+ backup)
UpgradeNow::class => ['base' => 0],
Download::class => ['base' => 5, 'baseWithoutBackup' => 10],
Unzip::class => ['base' => 10, 'baseWithoutBackup' => 20],
BackupFiles::class => ['base' => 20],
BackupDb::class => ['base' => 40],
UpgradeFiles::class => ['base' => 50, 'baseWithoutBackup' => 40],
UpgradeDb::class => ['base' => 70, 'baseWithoutBackup' => 60],
UpgradeModules::class => ['base' => 90, 'baseWithoutBackup' => 80],
CleanDatabase::class => ['base' => 100],
UpgradeComplete::class => ['base' => 100],

// Restore
Rollback::class => ['base' => 0],
RestoreFiles::class => ['base' => 33],
RestoreDb::class => ['base' => 66],
RollbackComplete::class => ['base' => 100],
];
}

/**
* @return int<0, 100>
*/
public function getBasePercentageOfTask(string $taskName): int
{
$withoutBackup = !$this->upgradeConfiguration->shouldBackupFiles();
$percentages = self::getPercentages()[$taskName];

if ($withoutBackup && $percentages['baseWithoutBackup'] !== null) {
return $percentages['baseWithoutBackup'];
}

return $percentages['base'];
}

/**
* @return int<0, 100>
*/
public function computePercentage(Backlog $backlog, string $currentTaskClass, string $nextTaskClass): int
{
$currentBaseProgress = $this->getBasePercentageOfTask($currentTaskClass);
$nextBaseProgress = $this->getBasePercentageOfTask($nextTaskClass);

return $currentBaseProgress + (($nextBaseProgress - $currentBaseProgress) * ($backlog->getInitialTotal() - $backlog->getRemainingTotal()) / $backlog->getInitialTotal());
}
}
21 changes: 0 additions & 21 deletions classes/Task/AbstractTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,10 @@
use PrestaShop\Module\AutoUpgrade\Analytics;
use PrestaShop\Module\AutoUpgrade\Log\Logger;
use PrestaShop\Module\AutoUpgrade\UpgradeContainer;
use PrestaShop\Module\AutoUpgrade\UpgradeTools\Backlog;
use PrestaShop\Module\AutoUpgrade\UpgradeTools\Translator;

abstract class AbstractTask
{
/** @var int|null */
const BASE_PROGRESS = null;

/**
* This constant exists while the backup and upgrade are on the same workflow
* @var int|null
*/
const BASE_PROGRESS_WITHOUT_BACKUP = null;

/**
* usage : key = the step you want to skip
* value = the next step you want instead
Expand Down Expand Up @@ -154,17 +144,6 @@ private function checkTaskMayRun(): void
}
}

protected function computeProgressionPercentage(Backlog $backlog, string $nextTaskClass): void
{
$withoutBackup = !$this->container->getUpgradeConfiguration()->shouldBackupFiles();
$baseProgress = $withoutBackup && static::BASE_PROGRESS_WITHOUT_BACKUP !== null ? static::BASE_PROGRESS_WITHOUT_BACKUP : static::BASE_PROGRESS;
$nextBaseProgress = $withoutBackup && $nextTaskClass::BASE_PROGRESS_WITHOUT_BACKUP !== null ? $nextTaskClass::BASE_PROGRESS_WITHOUT_BACKUP : $nextTaskClass::BASE_PROGRESS;

$this->container->getState()->setProgressPercentage(
$baseProgress + (($nextBaseProgress - $baseProgress) * ($backlog->getInitialTotal() - $backlog->getRemainingTotal()) / $backlog->getInitialTotal())
);
}

public function setErrorFlag(): void
{
$this->error = true;
Expand Down
8 changes: 4 additions & 4 deletions classes/Task/Rollback/RestoreDb.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@

use Exception;
use PrestaShop\Module\AutoUpgrade\Parameters\UpgradeFileNames;
use PrestaShop\Module\AutoUpgrade\Progress\Backlog;
use PrestaShop\Module\AutoUpgrade\Task\AbstractTask;
use PrestaShop\Module\AutoUpgrade\Task\ExitCode;
use PrestaShop\Module\AutoUpgrade\UpgradeContainer;
use PrestaShop\Module\AutoUpgrade\UpgradeTools\Backlog;
use PrestaShop\Module\AutoUpgrade\UpgradeTools\Database;

/**
Expand All @@ -42,14 +42,14 @@ class RestoreDb extends AbstractTask
{
const TASK_TYPE = 'rollback';

const BASE_PROGRESS = 66;

/**
* @throws Exception
*/
public function run(): int
{
$this->container->getState()->setProgressPercentage(static::BASE_PROGRESS);
$this->container->getState()->setProgressPercentage(
$this->container->getCompletionCalculator()->getBasePercentageOfTask(self::class)
);

$databaseTools = new Database($this->container->getDb());
$ignore_stats_table = [
Expand Down
6 changes: 3 additions & 3 deletions classes/Task/Rollback/RestoreFiles.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@ class RestoreFiles extends AbstractTask
{
const TASK_TYPE = 'rollback';

const BASE_PROGRESS = 33;

/**
* @throws Exception
*/
public function run(): int
{
$this->container->getState()->setProgressPercentage(static::BASE_PROGRESS);
$this->container->getState()->setProgressPercentage(
$this->container->getCompletionCalculator()->getBasePercentageOfTask(self::class)
);

// loop
$this->next = 'restoreFiles';
Expand Down
6 changes: 3 additions & 3 deletions classes/Task/Rollback/Rollback.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@ class Rollback extends AbstractTask
{
const TASK_TYPE = 'rollback';

const BASE_PROGRESS = 0;

/**
* @throws Exception
*/
public function run(): int
{
$this->container->getState()->setProgressPercentage(static::BASE_PROGRESS);
$this->container->getState()->setProgressPercentage(
$this->container->getCompletionCalculator()->getBasePercentageOfTask(self::class)
);

// 1st, need to analyse what was wrong.
$restoreName = $this->container->getState()->getRestoreName();
Expand Down
2 changes: 0 additions & 2 deletions classes/Task/Rollback/RollbackComplete.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ class RollbackComplete extends AbstractTask
{
const TASK_TYPE = 'rollback';

const BASE_PROGRESS = 100;

public function run(): int
{
$this->logger->info($this->translator->trans('Restoration process done. Congratulations! You can now reactivate your shop.'));
Expand Down
12 changes: 7 additions & 5 deletions classes/Task/Upgrade/BackupDb.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,16 @@

use Exception;
use PrestaShop\Module\AutoUpgrade\Parameters\UpgradeFileNames;
use PrestaShop\Module\AutoUpgrade\Progress\Backlog;
use PrestaShop\Module\AutoUpgrade\Task\AbstractTask;
use PrestaShop\Module\AutoUpgrade\Task\ExitCode;
use PrestaShop\Module\AutoUpgrade\Tools14;
use PrestaShop\Module\AutoUpgrade\UpgradeContainer;
use PrestaShop\Module\AutoUpgrade\UpgradeTools\Backlog;

class BackupDb extends AbstractTask
{
const TASK_TYPE = 'upgrade';

const BASE_PROGRESS = 40;

/**
* @throws Exception
*/
Expand Down Expand Up @@ -79,7 +77,9 @@ public function run(): int

// INIT LOOP
if (!$this->container->getFileConfigurationStorage()->exists(UpgradeFileNames::DB_TABLES_TO_BACKUP_LIST)) {
$this->container->getState()->setProgressPercentage(static::BASE_PROGRESS);
$this->container->getState()->setProgressPercentage(
$this->container->getCompletionCalculator()->getBasePercentageOfTask(self::class)
);

if (!is_dir($this->container->getProperty(UpgradeContainer::BACKUP_PATH) . DIRECTORY_SEPARATOR . $this->container->getState()->getBackupName())) {
mkdir($this->container->getProperty(UpgradeContainer::BACKUP_PATH) . DIRECTORY_SEPARATOR . $this->container->getState()->getBackupName());
Expand Down Expand Up @@ -266,7 +266,9 @@ public function run(): int
$fp = null;
}

$this->computeProgressionPercentage($tablesToBackup, UpgradeFiles::class);
$this->container->getState()->setProgressPercentage(
$this->container->getCompletionCalculator()->computePercentage($tablesToBackup, self::class, UpgradeFiles::class)
);
$this->container->getFileConfigurationStorage()->save($tablesToBackup->dump(), UpgradeFileNames::DB_TABLES_TO_BACKUP_LIST);

if ($tablesToBackup->getRemainingTotal()) {
Expand Down
12 changes: 7 additions & 5 deletions classes/Task/Upgrade/BackupFiles.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,15 @@

use Exception;
use PrestaShop\Module\AutoUpgrade\Parameters\UpgradeFileNames;
use PrestaShop\Module\AutoUpgrade\Progress\Backlog;
use PrestaShop\Module\AutoUpgrade\Task\AbstractTask;
use PrestaShop\Module\AutoUpgrade\Task\ExitCode;
use PrestaShop\Module\AutoUpgrade\UpgradeContainer;
use PrestaShop\Module\AutoUpgrade\UpgradeTools\Backlog;

class BackupFiles extends AbstractTask
{
const TASK_TYPE = 'upgrade';

const BASE_PROGRESS = 20;

/**
* @throws Exception
*/
Expand All @@ -65,7 +63,9 @@ public function run(): int
}

if (!$this->container->getFileConfigurationStorage()->exists(UpgradeFileNames::FILES_TO_BACKUP_LIST)) {
$this->container->getState()->setProgressPercentage(static::BASE_PROGRESS);
$this->container->getState()->setProgressPercentage(
$this->container->getCompletionCalculator()->getBasePercentageOfTask(self::class)
);

/** @todo : only add files and dir listed in "originalPrestashopVersion" list */
$filesToBackup = $this->container->getFilesystemAdapter()->listFilesInDir($this->container->getProperty(UpgradeContainer::PS_ROOT_PATH), 'backup', false);
Expand Down Expand Up @@ -100,7 +100,9 @@ public function run(): int
return ExitCode::FAIL;
}
$this->container->getFileConfigurationStorage()->save($backlog->dump(), UpgradeFileNames::FILES_TO_BACKUP_LIST);
$this->computeProgressionPercentage($backlog, BackupDb::class);
$this->container->getState()->setProgressPercentage(
$this->container->getCompletionCalculator()->computePercentage($backlog, self::class, BackupDb::class)
);
} else {
$this->stepDone = true;
$this->status = 'ok';
Expand Down
6 changes: 3 additions & 3 deletions classes/Task/Upgrade/CleanDatabase.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ class CleanDatabase extends AbstractTask
{
const TASK_TYPE = 'upgrade';

const BASE_PROGRESS = 100;

public function run(): int
{
$this->container->getState()->setProgressPercentage(static::BASE_PROGRESS);
$this->container->getState()->setProgressPercentage(
$this->container->getCompletionCalculator()->getBasePercentageOfTask(self::class)
);

// Clean tabs order
foreach ($this->container->getDb()->ExecuteS('SELECT DISTINCT id_parent FROM ' . _DB_PREFIX_ . 'tab') as $parent) {
Expand Down
5 changes: 1 addition & 4 deletions classes/Task/Upgrade/Download.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,6 @@ class Download extends AbstractTask
{
const TASK_TYPE = 'upgrade';

const BASE_PROGRESS = 5;
const BASE_PROGRESS_WITHOUT_BACKUP = 10;

/**
* @throws Exception
*/
Expand All @@ -57,7 +54,7 @@ public function run(): int
}

$this->container->getState()->setProgressPercentage(
$this->container->getUpgradeConfiguration()->shouldBackupFiles() ? static::BASE_PROGRESS : static::BASE_PROGRESS_WITHOUT_BACKUP
$this->container->getCompletionCalculator()->getBasePercentageOfTask(self::class)
);

$upgrader = $this->container->getUpgrader();
Expand Down
5 changes: 1 addition & 4 deletions classes/Task/Upgrade/Unzip.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,6 @@ class Unzip extends AbstractTask
{
const TASK_TYPE = 'upgrade';

const BASE_PROGRESS = 10;
const BASE_PROGRESS_WITHOUT_BACKUP = 20;

/**
* @throws Exception
*/
Expand All @@ -53,7 +50,7 @@ public function run(): int
$destExtract = $this->container->getProperty(UpgradeContainer::LATEST_PATH);

$this->container->getState()->setProgressPercentage(
$this->container->getUpgradeConfiguration()->shouldBackupFiles() ? static::BASE_PROGRESS : static::BASE_PROGRESS_WITHOUT_BACKUP
$this->container->getCompletionCalculator()->getBasePercentageOfTask(self::class)
);

if (file_exists($destExtract)) {
Expand Down
6 changes: 3 additions & 3 deletions classes/Task/Upgrade/UpgradeComplete.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@ class UpgradeComplete extends AbstractTask
{
const TASK_TYPE = 'upgrade';

const BASE_PROGRESS = 100;

/**
* @throws Exception
*/
public function run(): int
{
$this->container->getState()->setProgressPercentage(static::BASE_PROGRESS);
$this->container->getState()->setProgressPercentage(
$this->container->getCompletionCalculator()->getBasePercentageOfTask(self::class)
);

$this->logger->info($this->container->getState()->getWarningExists() ?
$this->translator->trans('Upgrade process done, but some warnings have been found.') :
Expand Down
Loading

0 comments on commit c96da96

Please sign in to comment.