Merge pull request #7124 from appwrite/usage-auto-skip-fix

wrapping create stats query
This commit is contained in:
Christy Jacob 2023-11-15 10:38:47 -08:00 committed by GitHub
commit 6826a9efe0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 72 additions and 44 deletions

View file

@ -2,17 +2,16 @@
namespace Appwrite\Platform\Tasks; namespace Appwrite\Platform\Tasks;
use PHPMailer\PHPMailer\PHPMailer;
use Utopia\App; use Utopia\App;
use Utopia\Config\Config; use Utopia\Config\Config;
use Utopia\Database\Query; use Utopia\Database\Query;
use Utopia\Database\Validator\Authorization;
use Utopia\Platform\Action; use Utopia\Platform\Action;
use Utopia\Cache\Cache; use Utopia\Cache\Cache;
use Utopia\CLI\Console; use Utopia\CLI\Console;
use Utopia\Database\Database; use Utopia\Database\Database;
use Utopia\Pools\Group; use Utopia\Pools\Group;
use Utopia\Registry\Registry; use Utopia\Registry\Registry;
use Utopia\Validator\Boolean;
class DeleteOrphanedProjects extends Action class DeleteOrphanedProjects extends Action
{ {
@ -25,18 +24,19 @@ class DeleteOrphanedProjects extends Action
{ {
$this $this
->desc('Get stats for projects') ->desc('Delete orphaned projects')
->param('commit', false, new Boolean(true), 'Commit project deletion', true)
->inject('pools') ->inject('pools')
->inject('cache') ->inject('cache')
->inject('dbForConsole') ->inject('dbForConsole')
->inject('register') ->inject('register')
->callback(function (Group $pools, Cache $cache, Database $dbForConsole, Registry $register) { ->callback(function (bool $commit, Group $pools, Cache $cache, Database $dbForConsole, Registry $register) {
$this->action($pools, $cache, $dbForConsole, $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::title('Delete orphaned projects V1');
@ -54,7 +54,8 @@ class DeleteOrphanedProjects extends Action
$totalProjects = $dbForConsole->count('projects'); $totalProjects = $dbForConsole->count('projects');
Console::success("Found a total of: {$totalProjects} projects"); Console::success("Found a total of: {$totalProjects} projects");
$orphans = 0; $orphans = 1;
$cnt = 0;
$count = 0; $count = 0;
$limit = 30; $limit = 30;
$sum = 30; $sum = 30;
@ -79,19 +80,43 @@ class DeleteOrphanedProjects extends Action
$dbForProject = new Database($adapter, $cache); $dbForProject = new Database($adapter, $cache);
$dbForProject->setDefaultDatabase('appwrite'); $dbForProject->setDefaultDatabase('appwrite');
$dbForProject->setNamespace('_' . $project->getInternalId()); $dbForProject->setNamespace('_' . $project->getInternalId());
$collectionsCreated = $dbForProject->count(Database::METADATA); $collectionsCreated = 0;
$message = ' (' . $collectionsCreated . ') collections where found on project (' . $project->getId() . '))'; $cnt++;
if ($collectionsCreated < (count($collectionsConfig) + 2)) { if ($dbForProject->exists($dbForProject->getDefaultDatabase(), Database::METADATA)) {
Console::error($message); $collectionsCreated = $dbForProject->count(Database::METADATA);
$orphans++;
} else {
Console::log($message);
} }
} catch (\Throwable $th) {
//$dbForConsole->deleteDocument('projects', $project->getId()); $msg = '(' . $cnt . ') found (' . $collectionsCreated . ') collections on project (' . $project->getInternalId() . ') , database (' . $project['database'] . ')';
//Console::success('Deleting project (' . $project->getId() . ')'); /**
Console::error(' (0) collections where found for project (' . $project->getId() . ')'); * +2 = audit+abuse
*/
if ($collectionsCreated === (count($collectionsConfig) + 2)) {
Console::log($msg . ' ignoring....');
continue;
}
Console::log($msg);
if ($collectionsCreated > 0) {
$collections = $dbForProject->find(Database::METADATA, []);
foreach ($collections as $collection) {
if ($commit) {
$dbForProject->deleteCollection($collection->getId());
$dbForConsole->deleteCachedCollection($collection->getId());
}
Console::info('--Deleting collection (' . $collection->getId() . ') project no (' . $project->getInternalId() . ')');
}
}
if ($commit) {
$dbForConsole->deleteDocument('projects', $project->getId());
$dbForConsole->deleteCachedDocument('projects', $project->getId());
}
Console::info('--Deleting project no (' . $project->getInternalId() . ')');
$orphans++; $orphans++;
} catch (\Throwable $th) {
Console::error('Error: ' . $th->getMessage());
} finally { } finally {
$pools $pools
->get($db) ->get($db)
@ -110,6 +135,6 @@ class DeleteOrphanedProjects extends Action
$count = $count + $sum; $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');
} }
} }

View file

@ -8,6 +8,7 @@ use Utopia\Database\Database;
use Utopia\Database\Document; use Utopia\Database\Document;
use InfluxDB\Database as InfluxDatabase; use InfluxDB\Database as InfluxDatabase;
use DateTime; use DateTime;
use Utopia\Database\Validator\Authorization;
use Utopia\Registry\Registry; use Utopia\Registry\Registry;
class TimeSeries extends Calculator class TimeSeries extends Calculator
@ -426,32 +427,34 @@ class TimeSeries extends Calculator
$project = $this->database->getDocument('projects', $projectId); $project = $this->database->getDocument('projects', $projectId);
$database = call_user_func($this->getProjectDB, $project); $database = call_user_func($this->getProjectDB, $project);
try { Authorization::skip(function () use ($database, $id, $period, $time, $metric, $value, $type, $projectId) {
$document = $database->getDocument('stats', $id); try {
if ($document->isEmpty()) { $document = $database->getDocument('stats', $id);
$database->createDocument('stats', new Document([ if ($document->isEmpty()) {
'$id' => $id, $database->createDocument('stats', new Document([
'period' => $period, '$id' => $id,
'time' => $time, 'period' => $period,
'metric' => $metric, 'time' => $time,
'value' => $value, 'metric' => $metric,
'type' => $type, 'value' => $value,
'region' => $this->region, 'type' => $type,
])); 'region' => $this->region,
} else { ]));
$database->updateDocument( } else {
'stats', $database->updateDocument(
$document->getId(), 'stats',
$document->setAttribute('value', $value) $document->getId(),
); $document->setAttribute('value', $value)
);
}
} catch (\Exception $e) { // if projects are deleted this might fail
if (is_callable($this->errorHandler)) {
call_user_func($this->errorHandler, $e, "sync_project_{$projectId}_metric_{$metric}");
} else {
throw $e;
}
} }
} catch (\Exception $e) { // if projects are deleted this might fail });
if (is_callable($this->errorHandler)) {
call_user_func($this->errorHandler, $e, "sync_project_{$projectId}_metric_{$metric}");
} else {
throw $e;
}
}
$this->register->get('pools')->reclaim(); $this->register->get('pools')->reclaim();
} }