diff --git a/app/tasks/usage.php b/app/tasks/usage.php index d1aeab2e84..7068a6a86c 100644 --- a/app/tasks/usage.php +++ b/app/tasks/usage.php @@ -9,6 +9,7 @@ use InfluxDB\Database as InfluxDatabase; use Utopia\App; use Utopia\CLI\Console; use Utopia\Database\Database as UtopiaDatabase; +use Utopia\Database\Document; use Utopia\Database\Validator\Authorization; use Utopia\Logger\Log; use Utopia\Validator\WhiteList; @@ -50,10 +51,10 @@ $logError = function (Throwable $error, string $action = 'syncUsageStats') use ( Console::warning($error->getTraceAsString()); }; -function aggregateTimeseries(UtopiaDatabase $database, InfluxDatabase $influxDB, callable $logError): void +function aggregateTimeseries(UtopiaDatabase $database, InfluxDatabase $influxDB, callable $getProjectDB, callable $logError): void { $interval = (int) App::getEnv('_APP_USAGE_TIMESERIES_INTERVAL', '30'); // 30 seconds (by default) - $usage = new TimeSeries($database, $influxDB, $logError); + $usage = new TimeSeries($database, $influxDB, $getProjectDB, $logError); Console::loop(function () use ($interval, $usage) { $now = date('d-m-Y H:i:s', time()); @@ -68,11 +69,11 @@ function aggregateTimeseries(UtopiaDatabase $database, InfluxDatabase $influxDB, }, $interval); } -function aggregateDatabase(UtopiaDatabase $database, callable $logError): void +function aggregateDatabase(UtopiaDatabase $database, callable $getProjectDB, callable $logError): void { $interval = (int) App::getEnv('_APP_USAGE_DATABASE_INTERVAL', '900'); // 15 minutes (by default) - $usage = new Database($database, $logError); - $aggregrator = new Aggregator($database, $logError); + $usage = new Database($database, $getProjectDB, $logError); + $aggregrator = new Aggregator($database, $getProjectDB, $logError); Console::loop(function () use ($interval, $usage, $aggregrator) { $now = date('d-m-Y H:i:s', time()); @@ -97,13 +98,14 @@ $cli $database = getConsoleDB(); $influxDB = getInfluxDB(); + $getProjectDB = fn (Document $project) => getProjectDB($project); switch ($type) { case 'timeseries': - aggregateTimeseries($database, $influxDB, $logError); + aggregateTimeseries($database, $influxDB, $getProjectDB, $logError); break; case 'database': - aggregateDatabase($database, $logError); + aggregateDatabase($database, $getProjectDB, $logError); break; default: Console::error("Unsupported usage aggregation type"); diff --git a/src/Appwrite/Usage/Calculators/Aggregator.php b/src/Appwrite/Usage/Calculators/Aggregator.php index 67cb18fe56..2121897419 100644 --- a/src/Appwrite/Usage/Calculators/Aggregator.php +++ b/src/Appwrite/Usage/Calculators/Aggregator.php @@ -9,10 +9,8 @@ use Utopia\Database\Query; class Aggregator extends Database { - protected function aggregateDatabaseMetrics(string $projectId): void + protected function aggregateDatabaseMetrics(Document $project): void { - $this->database->setNamespace('_' . $projectId); - $databasesGeneralMetrics = [ 'databases.$all.requests.create', 'databases.$all.requests.read', @@ -29,8 +27,8 @@ class Aggregator extends Database ]; foreach ($databasesGeneralMetrics as $metric) { - $this->aggregateDailyMetric($projectId, $metric); - $this->aggregateMonthlyMetric($projectId, $metric); + $this->aggregateDailyMetric($project, $metric); + $this->aggregateMonthlyMetric($project, $metric); } $databasesDatabaseMetrics = [ @@ -44,12 +42,12 @@ class Aggregator extends Database 'documents.databaseId.requests.delete', ]; - $this->foreachDocument($projectId, 'databases', [], function (Document $database) use ($databasesDatabaseMetrics, $projectId) { + $this->foreachDocument($project, 'databases', [], function (Document $database) use ($databasesDatabaseMetrics, $project) { $databaseId = $database->getId(); foreach ($databasesDatabaseMetrics as $metric) { $metric = str_replace('databaseId', $databaseId, $metric); - $this->aggregateDailyMetric($projectId, $metric); - $this->aggregateMonthlyMetric($projectId, $metric); + $this->aggregateDailyMetric($project, $metric); + $this->aggregateMonthlyMetric($project, $metric); } $databasesCollectionMetrics = [ @@ -59,21 +57,19 @@ class Aggregator extends Database 'documents.' . $databaseId . '/collectionId.requests.delete', ]; - $this->foreachDocument($projectId, 'database_' . $database->getInternalId(), [], function (Document $collection) use ($databasesCollectionMetrics, $projectId) { + $this->foreachDocument($project, 'database_' . $database->getInternalId(), [], function (Document $collection) use ($databasesCollectionMetrics, $project) { $collectionId = $collection->getId(); foreach ($databasesCollectionMetrics as $metric) { $metric = str_replace('collectionId', $collectionId, $metric); - $this->aggregateDailyMetric($projectId, $metric); - $this->aggregateMonthlyMetric($projectId, $metric); + $this->aggregateDailyMetric($project, $metric); + $this->aggregateMonthlyMetric($project, $metric); } }); }); } - protected function aggregateStorageMetrics(string $projectId): void + protected function aggregateStorageMetrics(Document $project): void { - $this->database->setNamespace('_' . $projectId); - $storageGeneralMetrics = [ 'buckets.$all.requests.create', 'buckets.$all.requests.read', @@ -86,8 +82,8 @@ class Aggregator extends Database ]; foreach ($storageGeneralMetrics as $metric) { - $this->aggregateDailyMetric($projectId, $metric); - $this->aggregateMonthlyMetric($projectId, $metric); + $this->aggregateDailyMetric($project, $metric); + $this->aggregateMonthlyMetric($project, $metric); } $storageBucketMetrics = [ @@ -97,20 +93,18 @@ class Aggregator extends Database 'files.bucketId.requests.delete', ]; - $this->foreachDocument($projectId, 'buckets', [], function (Document $bucket) use ($storageBucketMetrics, $projectId) { + $this->foreachDocument($project, 'buckets', [], function (Document $bucket) use ($storageBucketMetrics, $project) { $bucketId = $bucket->getId(); foreach ($storageBucketMetrics as $metric) { $metric = str_replace('bucketId', $bucketId, $metric); - $this->aggregateDailyMetric($projectId, $metric); - $this->aggregateMonthlyMetric($projectId, $metric); + $this->aggregateDailyMetric($project, $metric); + $this->aggregateMonthlyMetric($project, $metric); } }); } - protected function aggregateFunctionMetrics(string $projectId): void + protected function aggregateFunctionMetrics(Document $project): void { - $this->database->setNamespace('_' . $projectId); - $functionsGeneralMetrics = [ 'project.$all.compute.total', 'project.$all.compute.time', @@ -125,8 +119,8 @@ class Aggregator extends Database ]; foreach ($functionsGeneralMetrics as $metric) { - $this->aggregateDailyMetric($projectId, $metric); - $this->aggregateMonthlyMetric($projectId, $metric); + $this->aggregateDailyMetric($project, $metric); + $this->aggregateMonthlyMetric($project, $metric); } $functionMetrics = [ @@ -140,17 +134,17 @@ class Aggregator extends Database 'builds.functionId.compute.time', ]; - $this->foreachDocument($projectId, 'functions', [], function (Document $function) use ($functionMetrics, $projectId) { + $this->foreachDocument($project, 'functions', [], function (Document $function) use ($functionMetrics, $project) { $functionId = $function->getId(); foreach ($functionMetrics as $metric) { $metric = str_replace('functionId', $functionId, $metric); - $this->aggregateDailyMetric($projectId, $metric); - $this->aggregateMonthlyMetric($projectId, $metric); + $this->aggregateDailyMetric($project, $metric); + $this->aggregateMonthlyMetric($project, $metric); } }); } - protected function aggregateUsersMetrics(string $projectId): void + protected function aggregateUsersMetrics(Document $project): void { $metrics = [ 'users.$all.requests.create', @@ -162,50 +156,50 @@ class Aggregator extends Database ]; foreach ($metrics as $metric) { - $this->aggregateDailyMetric($projectId, $metric); - $this->aggregateMonthlyMetric($projectId, $metric); + $this->aggregateDailyMetric($project, $metric); + $this->aggregateMonthlyMetric($project, $metric); } } - protected function aggregateGeneralMetrics(string $projectId): void + protected function aggregateGeneralMetrics(Document $project): void { - $this->aggregateDailyMetric($projectId, 'project.$all.network.requests'); - $this->aggregateDailyMetric($projectId, 'project.$all.network.bandwidth'); - $this->aggregateDailyMetric($projectId, 'project.$all.network.inbound'); - $this->aggregateDailyMetric($projectId, 'project.$all.network.outbound'); - $this->aggregateMonthlyMetric($projectId, 'project.$all.network.requests'); - $this->aggregateMonthlyMetric($projectId, 'project.$all.network.bandwidth'); - $this->aggregateMonthlyMetric($projectId, 'project.$all.network.inbound'); - $this->aggregateMonthlyMetric($projectId, 'project.$all.network.outbound'); + $this->aggregateDailyMetric($project, 'project.$all.network.requests'); + $this->aggregateDailyMetric($project, 'project.$all.network.bandwidth'); + $this->aggregateDailyMetric($project, 'project.$all.network.inbound'); + $this->aggregateDailyMetric($project, 'project.$all.network.outbound'); + $this->aggregateMonthlyMetric($project, 'project.$all.network.requests'); + $this->aggregateMonthlyMetric($project, 'project.$all.network.bandwidth'); + $this->aggregateMonthlyMetric($project, 'project.$all.network.inbound'); + $this->aggregateMonthlyMetric($project, 'project.$all.network.outbound'); } - protected function aggregateDailyMetric(string $projectId, string $metric): void + protected function aggregateDailyMetric(Document $project, string $metric): void { $beginOfDay = DateTime::createFromFormat('Y-m-d\TH:i:s.v', \date('Y-m-d\T00:00:00.000'))->format(DateTime::RFC3339); $endOfDay = DateTime::createFromFormat('Y-m-d\TH:i:s.v', \date('Y-m-d\T23:59:59.999'))->format(DateTime::RFC3339); - $this->database->setNamespace('_' . $projectId); - $value = (int) $this->database->sum('stats', 'value', [ + $database = call_user_func($this->getProjectDB, $project); + $value = (int) $database->sum('stats', 'value', [ Query::equal('metric', [$metric]), Query::equal('period', ['30m']), Query::greaterThanEqual('time', $beginOfDay), Query::lessThanEqual('time', $endOfDay), ]); - $this->createOrUpdateMetric($projectId, $metric, '1d', $beginOfDay, $value); + $this->createOrUpdateMetric($database, $project->getId(), $metric, '1d', $beginOfDay, $value); } - protected function aggregateMonthlyMetric(string $projectId, string $metric): void + protected function aggregateMonthlyMetric(Document $project, string $metric): void { $beginOfMonth = DateTime::createFromFormat('Y-m-d\TH:i:s.v', \date('Y-m-01\T00:00:00.000'))->format(DateTime::RFC3339); $endOfMonth = DateTime::createFromFormat('Y-m-d\TH:i:s.v', \date('Y-m-t\T23:59:59.999'))->format(DateTime::RFC3339); - $this->database->setNamespace('_' . $projectId); - $value = (int) $this->database->sum('stats', 'value', [ + $database = call_user_func($this->getProjectDB, $project); + $value = (int) $database->sum('stats', 'value', [ Query::equal('metric', [$metric]), Query::equal('period', ['1d']), Query::greaterThanEqual('time', $beginOfMonth), Query::lessThanEqual('time', $endOfMonth), ]); - $this->createOrUpdateMetric($projectId, $metric, '1mo', $beginOfMonth, $value); + $this->createOrUpdateMetric($database, $project->getId(), $metric, '1mo', $beginOfMonth, $value); } /** @@ -216,16 +210,12 @@ class Aggregator extends Database */ public function collect(): void { - $this->foreachDocument('console', 'projects', [], function (Document $project) { - $projectId = $project->getInternalId(); - - // Aggregate new metrics from already collected usage metrics - // for lower time period (1day and 1 month metric from 30 minute metrics) - $this->aggregateGeneralMetrics($projectId); - $this->aggregateFunctionMetrics($projectId); - $this->aggregateDatabaseMetrics($projectId); - $this->aggregateStorageMetrics($projectId); - $this->aggregateUsersMetrics($projectId); + $this->foreachDocument(new Document(['$id' => 'console']), 'projects', [], function (Document $project) { + $this->aggregateGeneralMetrics($project); + $this->aggregateFunctionMetrics($project); + $this->aggregateDatabaseMetrics($project); + $this->aggregateStorageMetrics($project); + $this->aggregateUsersMetrics($project); }); } } diff --git a/src/Appwrite/Usage/Calculators/Database.php b/src/Appwrite/Usage/Calculators/Database.php index 63ad8b5bf5..ac8c2876a8 100644 --- a/src/Appwrite/Usage/Calculators/Database.php +++ b/src/Appwrite/Usage/Calculators/Database.php @@ -24,9 +24,10 @@ class Database extends Calculator ], ]; - public function __construct(UtopiaDatabase $database, callable $errorHandler = null) + public function __construct(UtopiaDatabase $database, callable $getProjectDB, callable $errorHandler = null) { $this->database = $database; + $this->getProjectDB = $getProjectDB; $this->errorHandler = $errorHandler; } @@ -35,7 +36,8 @@ class Database extends Calculator * * Create given metric for each defined period * - * @param string $projectId + * @param UtopiaDatabase $database + * @param Document $project * @param string $metric * @param int $value * @param bool $monthly @@ -43,7 +45,7 @@ class Database extends Calculator * @throws Authorization * @throws Structure */ - protected function createPerPeriodMetric(string $projectId, string $metric, int $value, bool $monthly = false): void + protected function createPerPeriodMetric(UtopiaDatabase $database, string $projectId, string $metric, int $value, bool $monthly = false): void { foreach ($this->periods as $options) { $period = $options['key']; @@ -56,13 +58,13 @@ class Database extends Calculator } else { throw new Exception("Period type not found", 500); } - $this->createOrUpdateMetric($projectId, $metric, $period, $time, $value); + $this->createOrUpdateMetric($database, $projectId, $metric, $period, $time, $value); } // Required for billing if ($monthly) { $time = DateTime::createFromFormat('Y-m-d\TH:i:s.v', \date('Y-m-01\T00:00:00.000'))->format(DateTime::RFC3339); - $this->createOrUpdateMetric($projectId, $metric, '1mo', $time, $value); + $this->createOrUpdateMetric($database, $projectId, $metric, '1mo', $time, $value); } } @@ -71,7 +73,8 @@ class Database extends Calculator * * Create or update each metric in the stats collection for the given project * - * @param string $projectId + * @param UtopiaDatabase $database + * @param String $projectId * @param string $metric * @param string $period * @param string $time @@ -81,15 +84,14 @@ class Database extends Calculator * @throws Authorization * @throws Structure */ - protected function createOrUpdateMetric(string $projectId, string $metric, string $period, string $time, int $value): void + protected function createOrUpdateMetric(UtopiaDatabase $database, String $projectId, string $metric, string $period, string $time, int $value): void { $id = \md5("{$time}_{$period}_{$metric}"); - $this->database->setNamespace('_' . $projectId); try { - $document = $this->database->getDocument('stats', $id); + $document = $database->getDocument('stats', $id); if ($document->isEmpty()) { - $this->database->createDocument('stats', new Document([ + $database->createDocument('stats', new Document([ '$id' => $id, 'period' => $period, 'time' => $time, @@ -98,7 +100,7 @@ class Database extends Calculator 'type' => 2, // these are cumulative metrics ])); } else { - $this->database->updateDocument( + $database->updateDocument( 'stats', $document->getId(), $document->setAttribute('value', $value) @@ -118,7 +120,7 @@ class Database extends Calculator * * Call provided callback for each document in the collection * - * @param string $projectId + * @param Document $project * @param string $collection * @param array $queries * @param callable $callback @@ -126,13 +128,13 @@ class Database extends Calculator * @return void * @throws Exception */ - protected function foreachDocument(string $projectId, string $collection, array $queries, callable $callback): void + protected function foreachDocument(Document $project, string $collection, array $queries, callable $callback): void { $limit = 50; $results = []; $sum = $limit; $latestDocument = null; - $this->database->setNamespace($projectId === 'console' ? $projectId : '_' . $projectId); + $database = $project->getId() == 'console' ? $this->database : call_user_func($this->getProjectDB, $project); while ($sum === $limit) { try { @@ -143,7 +145,7 @@ class Database extends Calculator $results = $this->database->find($collection, \array_merge($paginationQueries, $queries)); } catch (\Exception $e) { if (is_callable($this->errorHandler)) { - call_user_func($this->errorHandler, $e, "fetch_documents_project_{$projectId}_collection_{$collection}"); + call_user_func($this->errorHandler, $e, "fetch_documents_project_{$project->getId()}_collection_{$collection}"); return; } else { throw $e; @@ -169,6 +171,7 @@ class Database extends Calculator * * Calculate sum of an attribute of documents in collection * + * @param UtopiaDatabase $database * @param string $projectId * @param string $collection * @param string $attribute @@ -177,16 +180,15 @@ class Database extends Calculator * @return int * @throws Exception */ - private function sum(string $projectId, string $collection, string $attribute, string $metric = null, int $multiplier = 1): int + private function sum(UtopiaDatabase $database, string $projectId, string $collection, string $attribute, string $metric = null, int $multiplier = 1): int { - $this->database->setNamespace('_' . $projectId); try { - $sum = $this->database->sum($collection, $attribute); + $sum = $database->sum($collection, $attribute); $sum = (int) ($sum * $multiplier); if (!is_null($metric)) { - $this->createPerPeriodMetric($projectId, $metric, $sum); + $this->createPerPeriodMetric($database, $projectId, $metric, $sum); } return $sum; } catch (Exception $e) { @@ -204,6 +206,7 @@ class Database extends Calculator * * Count number of documents in collection * + * @param UtopiaDatabase $database * @param string $projectId * @param string $collection * @param ?string $metric @@ -211,14 +214,14 @@ class Database extends Calculator * @return int * @throws Exception */ - private function count(string $projectId, string $collection, ?string $metric = null): int + private function count(UtopiaDatabase $database, string $projectId, string $collection, ?string $metric = null): int { $this->database->setNamespace('_' . $projectId); try { $count = $this->database->count($collection); if (!is_null($metric)) { - $this->createPerPeriodMetric($projectId, (string) $metric, $count); + $this->createPerPeriodMetric($database, $projectId, (string) $metric, $count); } return $count; } catch (Exception $e) { @@ -236,14 +239,15 @@ class Database extends Calculator * * Total sum of storage used by deployments * + * @param UtopiaDatabase $database * @param string $projectId * * @return int * @throws Exception */ - private function deploymentsTotal(string $projectId): int + private function deploymentsTotal(UtopiaDatabase $database, string $projectId): int { - return $this->sum($projectId, 'deployments', 'size', 'deployments.$all.storage.size'); + return $this->sum($database, $projectId, 'deployments', 'size', 'deployments.$all.storage.size'); } /** @@ -251,14 +255,15 @@ class Database extends Calculator * * Metric: users.count * + * @param UtopiaDatabase $database * @param string $projectId * * @return void * @throws Exception */ - private function usersStats(string $projectId): void + private function usersStats(UtopiaDatabase $database, string $projectId): void { - $this->count($projectId, 'users', 'users.$all.count.total'); + $this->count($database, $projectId, 'users', 'users.$all.count.total'); } /** @@ -267,35 +272,36 @@ class Database extends Calculator * Metrics: buckets.$all.count.total, files.$all.count.total, files.bucketId,count.total, * files.$all.storage.size, files.bucketId.storage.size, project.$all.storage.size * - * @param string $projectId + * @param UtopiaDatabase $database + * @param Document $project * * @return void * @throws Authorization * @throws Structure */ - private function storageStats(string $projectId): void + private function storageStats(UtopiaDatabase $database, Document $project): void { $projectFilesTotal = 0; $projectFilesCount = 0; $metric = 'buckets.$all.count.total'; - $this->count($projectId, 'buckets', $metric); + $this->count($database, $project->getId(), 'buckets', $metric); - $this->foreachDocument($projectId, 'buckets', [], function ($bucket) use (&$projectFilesCount, &$projectFilesTotal, $projectId,) { + $this->foreachDocument($project, 'buckets', [], function ($bucket) use (&$projectFilesCount, &$projectFilesTotal, $project, $database) { $metric = "files.{$bucket->getId()}.count.total"; - $count = $this->count($projectId, 'bucket_' . $bucket->getInternalId(), $metric); + $count = $this->count($database, $project->getId(), 'bucket_' . $bucket->getInternalId(), $metric); $projectFilesCount += $count; $metric = "files.{$bucket->getId()}.storage.size"; - $sum = $this->sum($projectId, 'bucket_' . $bucket->getInternalId(), 'sizeOriginal', $metric); + $sum = $this->sum($database, $project->getId(), 'bucket_' . $bucket->getInternalId(), 'sizeOriginal', $metric); $projectFilesTotal += $sum; }); - $this->createPerPeriodMetric($projectId, 'files.$all.count.total', $projectFilesCount); - $this->createPerPeriodMetric($projectId, 'files.$all.storage.size', $projectFilesTotal); + $this->createPerPeriodMetric($database, $project->getId(), 'files.$all.count.total', $projectFilesCount); + $this->createPerPeriodMetric($database, $project->getId(), 'files.$all.storage.size', $projectFilesTotal); - $deploymentsTotal = $this->deploymentsTotal($projectId); - $this->createPerPeriodMetric($projectId, 'project.$all.storage.size', $projectFilesTotal + $deploymentsTotal); + $deploymentsTotal = $this->deploymentsTotal($database, $project->getId()); + $this->createPerPeriodMetric($database, $project->getId(), 'project.$all.storage.size', $projectFilesTotal + $deploymentsTotal); } /** @@ -305,38 +311,39 @@ class Database extends Calculator * Metrics: databases.$all.count.total, collections.$all.count.total, collections.databaseId.count.total, * documents.$all.count.all, documents.databaseId.count.total, documents.databaseId/collectionId.count.total * - * @param string $projectId + * @param UtopiaDatabase $database + * @param Document $project * * @return void * @throws Authorization * @throws Structure */ - private function databaseStats(string $projectId): void + private function databaseStats(UtopiaDatabase $database, Document $project): void { $projectDocumentsCount = 0; $projectCollectionsCount = 0; - $this->count($projectId, 'databases', 'databases.$all.count.total'); + $this->count($database, $project->getId(), 'databases', 'databases.$all.count.total'); - $this->foreachDocument($projectId, 'databases', [], function ($database) use (&$projectDocumentsCount, &$projectCollectionsCount, $projectId) { + $this->foreachDocument($project, 'databases', [], function ($database) use (&$projectDocumentsCount, &$projectCollectionsCount, $project) { $metric = "collections.{$database->getId()}.count.total"; - $count = $this->count($projectId, 'database_' . $database->getInternalId(), $metric); + $count = $this->count($database, $project->getId(), 'database_' . $database->getInternalId(), $metric); $projectCollectionsCount += $count; $databaseDocumentsCount = 0; - $this->foreachDocument($projectId, 'database_' . $database->getInternalId(), [], function ($collection) use (&$projectDocumentsCount, &$databaseDocumentsCount, $projectId, $database) { + $this->foreachDocument($project, 'database_' . $database->getInternalId(), [], function ($collection) use (&$projectDocumentsCount, &$databaseDocumentsCount, $project, $database) { $metric = "documents.{$database->getId()}/{$collection->getId()}.count.total"; - $count = $this->count($projectId, 'database_' . $database->getInternalId() . '_collection_' . $collection->getInternalId(), $metric); + $count = $this->count($database, $project->getId(), 'database_' . $database->getInternalId() . '_collection_' . $collection->getInternalId(), $metric); $projectDocumentsCount += $count; $databaseDocumentsCount += $count; }); - $this->createPerPeriodMetric($projectId, "documents.{$database->getId()}.count.total", $databaseDocumentsCount); + $this->createPerPeriodMetric($database, $project->getId(), "documents.{$database->getId()}.count.total", $databaseDocumentsCount); }); - $this->createPerPeriodMetric($projectId, 'collections.$all.count.total', $projectCollectionsCount); - $this->createPerPeriodMetric($projectId, 'documents.$all.count.total', $projectDocumentsCount); + $this->createPerPeriodMetric($database, $project->getId(), 'collections.$all.count.total', $projectCollectionsCount); + $this->createPerPeriodMetric($database, $project->getId(), 'documents.$all.count.total', $projectDocumentsCount); } /** @@ -349,12 +356,11 @@ class Database extends Calculator */ public function collect(): void { - $this->foreachDocument('console', 'projects', [], function (Document $project) { - $projectId = $project->getInternalId(); - - $this->usersStats($projectId); - $this->databaseStats($projectId); - $this->storageStats($projectId); + $this->foreachDocument(new Document(['$id' => 'console']), 'projects', [], function (Document $project) { + $database = call_user_func($this->getProjectDB, $project); + $this->usersStats($database, $project->getId()); + $this->databaseStats($database, $project); + $this->storageStats($database, $project); }); } } diff --git a/src/Appwrite/Usage/Calculators/TimeSeries.php b/src/Appwrite/Usage/Calculators/TimeSeries.php index bd9a36c088..392984c161 100644 --- a/src/Appwrite/Usage/Calculators/TimeSeries.php +++ b/src/Appwrite/Usage/Calculators/TimeSeries.php @@ -14,6 +14,7 @@ class TimeSeries extends Calculator protected Database $database; protected $errorHandler; private array $latestTime = []; + private mixed $getProjectDB; // all the mertics that we are collecting protected array $metrics = [ @@ -278,10 +279,11 @@ class TimeSeries extends Calculator 'startTime' => '-24 hours', ]; - public function __construct(Database $database, InfluxDatabase $influxDB, callable $errorHandler = null) + public function __construct(Database $database, InfluxDatabase $influxDB, callable $getProjectDB, callable $errorHandler = null) { $this->database = $database; $this->influxDB = $influxDB; + $this->getProjectDB = $getProjectDB; $this->errorHandler = $errorHandler; } @@ -303,10 +305,10 @@ class TimeSeries extends Calculator $id = \md5("{$time}_{$period}_{$metric}"); $this->database->setNamespace('console'); $project = $this->database->getDocument('projects', $projectId); - $this->database->setNamespace('_' . $project->getInternalId()); + $database = call_user_func($this->getProjectDB, $project); try { - $document = $this->database->getDocument('stats', $id); + $document = $database->getDocument('stats', $id); if ($document->isEmpty()) { $this->database->createDocument('stats', new Document([ '$id' => $id, @@ -317,7 +319,7 @@ class TimeSeries extends Calculator 'type' => $type, ])); } else { - $this->database->updateDocument( + $database->updateDocument( 'stats', $document->getId(), $document->setAttribute('value', $value)