1
0
Fork 0
mirror of synced 2024-10-03 10:46:27 +13:00

added inf metric

This commit is contained in:
shimon 2023-11-08 12:05:51 +02:00
parent 9fc7c1384a
commit 0f3c438976

View file

@ -13,6 +13,8 @@ use Utopia\CLI\Console;
use Utopia\Database\Database;
use Utopia\Pools\Group;
use Utopia\Registry\Registry;
use Utopia\Validator\Boolean;
use Utopia\Validator\Hostname;
class DeleteOrphanedProjects extends Action
{
@ -26,25 +28,23 @@ class DeleteOrphanedProjects extends Action
$this
->desc('Get stats for projects')
->param('commit', false, new boolean(true), 'Commit project deletion', true)
->inject('pools')
->inject('cache')
->inject('dbForConsole')
->inject('register')
->callback(function (Group $pools, Cache $cache, Database $dbForConsole, Registry $register) {
$this->action($pools, $cache, $dbForConsole, $register);
->callback(function (bool $commit, Group $pools, Cache $cache, Database $dbForConsole, Registry $register) {
$this->action($commit, $pools, $cache, $dbForConsole, $register);
});
}
public function action(Group $pools, Cache $cache, Database $dbForConsole, Registry $register): void
public function action(bool $commit, Group $pools, Cache $cache, Database $dbForConsole, Registry $register): void
{
Console::title('Delete orphaned projects V1');
Console::success(APP_NAME . ' Delete orphaned projects started');
/** @var array $collections */
$collectionsConfig = Config::getParam('collections', [])['projects'] ?? [];
/* Initialise new Utopia app */
$app = new App('UTC');
$console = $app->getResource('console');
@ -54,7 +54,7 @@ class DeleteOrphanedProjects extends Action
$totalProjects = $dbForConsole->count('projects');
Console::success("Found a total of: {$totalProjects} projects");
$orphans = 0;
$orphans = 1;
$count = 0;
$limit = 30;
$sum = 30;
@ -80,17 +80,22 @@ class DeleteOrphanedProjects extends Action
$dbForProject->setDefaultDatabase('appwrite');
$dbForProject->setNamespace('_' . $project->getInternalId());
$collectionsCreated = $dbForProject->count(Database::METADATA);
$message = ' (' . $collectionsCreated . ') collections where found on project (' . $project->getId() . '))';
if ($collectionsCreated < (count($collectionsConfig) + 2)) {
Console::error($message);
if ($collectionsCreated === 0) {
if ($commit === true) {
Console::info('(' . $orphans . ') deleting project (' . $project->getId() . ')');
$this->deleteProject($dbForConsole, $project->getId());
} else {
Console::log('(' . $orphans . ') project (' . $project->getId() . ')');
}
$orphans++;
} else {
Console::log($message);
}
} catch (\Throwable $th) {
//$dbForConsole->deleteDocument('projects', $project->getId());
//Console::success('Deleting project (' . $project->getId() . ')');
Console::error(' (0) collections where found for project (' . $project->getId() . ')');
if ($commit === true) {
Console::info('(' . $orphans . ') deleting project (' . $project->getId() . ')');
$this->deleteProject($dbForConsole, $project->getId());
} else {
Console::log('(' . $orphans . ') project (' . $project->getId() . ')');
}
$orphans++;
} finally {
$pools
@ -110,6 +115,15 @@ class DeleteOrphanedProjects extends Action
$count = $count + $sum;
}
Console::log('Iterated through ' . $count - 1 . '/' . $totalProjects . ' projects found ' . $orphans . ' orphans');
Console::log('Iterated through ' . $count - 1 . '/' . $totalProjects . ' projects found ' . $orphans - 1 . ' orphans');
}
private function deleteProject(Database $dbForConsole, $projectId): void
{
try {
$dbForConsole->deleteDocument('projects', $projectId);
} catch (\Throwable $th) {
Console::error('Error when trying to delete project (' . $projectId . ') ' . $th->getMessage());
}
}
}