From f571979259fa181a4d99219e885972d2c7b06210 Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Fri, 24 Jun 2022 06:08:25 +0000 Subject: [PATCH] fix usage collection --- src/Appwrite/Stats/UsageDB.php | 24 ++++++------------------ 1 file changed, 6 insertions(+), 18 deletions(-) diff --git a/src/Appwrite/Stats/UsageDB.php b/src/Appwrite/Stats/UsageDB.php index 1561e52a66..44f9e92bb0 100644 --- a/src/Appwrite/Stats/UsageDB.php +++ b/src/Appwrite/Stats/UsageDB.php @@ -28,9 +28,7 @@ class UsageDB extends Usage $period = $options['key']; $time = (int) (floor(time() / $options['multiplier']) * $options['multiplier']); $id = \md5("{$time}_{$period}_{$metric}"); - $this->database->setNamespace('_console'); - $project = $this->database->getDocument('projects', $projectId); - $this->database->setNamespace('_' . $project->getInternalId()); + $this->database->setNamespace('_' . $projectId); try { $document = $this->database->getDocument('stats', $id); @@ -73,21 +71,15 @@ class UsageDB extends Usage */ private function foreachDocument(string $projectId, string $collection, array $queries, callable $callback): void { - if ($projectId === 'console') { - return; - } - $limit = 50; $results = []; $sum = $limit; $latestDocument = null; - $this->database->setNamespace('_console'); - $project = $this->database->getDocument('projects', $projectId); - $this->database->setNamespace('_' . $project->getInternalId()); + $this->database->setNamespace('_' . $projectId); while ($sum === $limit) { try { - $results = $this->database->find($collection, $queries, $limit, cursor: $latestDocument); + $results = $this->database->find($collection, $queries, $limit, cursor:$latestDocument); } catch (\Exception $e) { if (is_callable($this->errorHandler)) { call_user_func($this->errorHandler, $e, "fetch_documents_project_{$projectId}_collection_{$collection}"); @@ -124,9 +116,7 @@ class UsageDB extends Usage */ private function sum(string $projectId, string $collection, string $attribute, string $metric): int { - $this->database->setNamespace('_console'); - $project = $this->database->getDocument('projects', $projectId); - $this->database->setNamespace('_' . $project->getInternalId()); + $this->database->setNamespace('_' . $projectId); try { $sum = (int) $this->database->sum($collection, $attribute); @@ -153,9 +143,7 @@ class UsageDB extends Usage */ private function count(string $projectId, string $collection, string $metric): int { - $this->database->setNamespace('_console'); - $project = $this->database->getDocument('projects', $projectId); - $this->database->setNamespace('_' . $project->getInternalId()); + $this->database->setNamespace('_' . $projectId); try { $count = $this->database->count($collection); @@ -279,7 +267,7 @@ class UsageDB extends Usage public function collect(): void { $this->foreachDocument('console', 'projects', [], function (Document $project) { - $projectId = $project->getId(); + $projectId = $project->getInternalId(); $this->usersStats($projectId); $this->databaseStats($projectId);