From 93a0ab0dc01c0f7d07832f64849893b583e91ab4 Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Sun, 5 Jul 2026 05:41:48 +0000 Subject: [PATCH 01/13] refactor(usage): drop per-{resourceInternalId} metric templates Removes 34 metric-name templates whose baked dimensions (databaseInternalId, bucketInternalId, siteInternalId, webhookInternalId, resourceInternalId) are now carried as dedicated schema columns: - 15 databases per-DB templates (legacy + documentsdb + vectorsdb one-level) - 4 storage per-bucket templates - 13 compute per-function/per-site templates - 3 sites per-site bandwidth templates - 2 webhooks per-webhook templates Nested {databaseInternalId}.{collectionInternalId}.* templates and every project-level or {resourceType}.* service-level split stay in the catalog. --- app/init/constants.php | 37 ------------------------------------- 1 file changed, 37 deletions(-) diff --git a/app/init/constants.php b/app/init/constants.php index df85fc0925..86cd471391 100644 --- a/app/init/constants.php +++ b/app/init/constants.php @@ -314,8 +314,6 @@ const METRIC_TEAMS = 'teams'; const METRIC_USERS = 'users'; const METRIC_WEBHOOKS_SENT = 'webhooks.events.sent'; const METRIC_WEBHOOKS_FAILED = 'webhooks.events.failed'; -const METRIC_WEBHOOK_ID_SENT = '{webhookInternalId}.webhooks.events.sent'; -const METRIC_WEBHOOK_ID_FAILED = '{webhookInternalId}.webhooks.events.failed'; const METRIC_AUTH_METHOD_PHONE = 'auth.method.phone'; const METRIC_AUTH_METHOD_PHONE_COUNTRY_CODE = METRIC_AUTH_METHOD_PHONE . '.{countryCode}'; const METRIC_MESSAGES = 'messages'; @@ -331,46 +329,31 @@ const METRIC_SESSIONS = 'sessions'; const METRIC_DATABASES = 'databases'; const METRIC_COLLECTIONS = 'collections'; const METRIC_DATABASES_STORAGE = 'databases.storage'; -const METRIC_DATABASE_ID_COLLECTIONS = '{databaseInternalId}.collections'; -const METRIC_DATABASE_ID_STORAGE = '{databaseInternalId}.databases.storage'; const METRIC_DOCUMENTS = 'documents'; -const METRIC_DATABASE_ID_DOCUMENTS = '{databaseInternalId}.documents'; const METRIC_DATABASE_ID_COLLECTION_ID_DOCUMENTS = '{databaseInternalId}.{collectionInternalId}.documents'; const METRIC_DATABASE_ID_COLLECTION_ID_STORAGE = '{databaseInternalId}.{collectionInternalId}.databases.storage'; const METRIC_DATABASES_OPERATIONS_READS = 'databases.operations.reads'; -const METRIC_DATABASE_ID_OPERATIONS_READS = '{databaseInternalId}.databases.operations.reads'; const METRIC_DATABASES_OPERATIONS_WRITES = 'databases.operations.writes'; -const METRIC_DATABASE_ID_OPERATIONS_WRITES = '{databaseInternalId}.databases.operations.writes'; // documentsdb const METRIC_DATABASES_DOCUMENTSDB = 'documentsdb.databases'; const METRIC_COLLECTIONS_DOCUMENTSDB = 'documentsdb.collections'; const METRIC_DATABASES_STORAGE_DOCUMENTSDB = 'documentsdb.databases.storage'; -const METRIC_DATABASE_ID_COLLECTIONS_DOCUMENTSDB = 'documentsdb.{databaseInternalId}.collections'; -const METRIC_DATABASE_ID_STORAGE_DOCUMENTSDB = 'documentsdb.{databaseInternalId}.databases.storage'; const METRIC_DOCUMENTS_DOCUMENTSDB = 'documentsdb.documents'; -const METRIC_DATABASE_ID_DOCUMENTS_DOCUMENTSDB = 'documentsdb.{databaseInternalId}.documents'; const METRIC_DATABASE_ID_COLLECTION_ID_DOCUMENTS_DOCUMENTSDB = 'documentsdb.{databaseInternalId}.{collectionInternalId}.documents'; const METRIC_DATABASE_ID_COLLECTION_ID_STORAGE_DOCUMENTSDB = 'documentsdb.{databaseInternalId}.{collectionInternalId}.databases.storage'; const METRIC_DATABASES_OPERATIONS_READS_DOCUMENTSDB = 'documentsdb.databases.operations.reads'; -const METRIC_DATABASE_ID_OPERATIONS_READS_DOCUMENTSDB = 'documentsdb.{databaseInternalId}.databases.operations.reads'; const METRIC_DATABASES_OPERATIONS_WRITES_DOCUMENTSDB = 'documentsdb.databases.operations.writes'; -const METRIC_DATABASE_ID_OPERATIONS_WRITES_DOCUMENTSDB = 'documentsdb.{databaseInternalId}.databases.operations.writes'; // vectorsdb const METRIC_DATABASES_VECTORSDB = 'vectorsdb.databases'; const METRIC_COLLECTIONS_VECTORSDB = 'vectorsdb.collections'; const METRIC_DATABASES_STORAGE_VECTORSDB = 'vectorsdb.databases.storage'; -const METRIC_DATABASE_ID_COLLECTIONS_VECTORSDB = 'vectorsdb.{databaseInternalId}.collections'; -const METRIC_DATABASE_ID_STORAGE_VECTORSDB = 'vectorsdb.{databaseInternalId}.databases.storage'; const METRIC_DOCUMENTS_VECTORSDB = 'vectorsdb.documents'; -const METRIC_DATABASE_ID_DOCUMENTS_VECTORSDB = 'vectorsdb.{databaseInternalId}.documents'; const METRIC_DATABASE_ID_COLLECTION_ID_DOCUMENTS_VECTORSDB = 'vectorsdb.{databaseInternalId}.{collectionInternalId}.documents'; const METRIC_DATABASE_ID_COLLECTION_ID_STORAGE_VECTORSDB = 'vectorsdb.{databaseInternalId}.{collectionInternalId}.databases.storage'; const METRIC_DATABASES_OPERATIONS_READS_VECTORSDB = 'vectorsdb.databases.operations.reads'; -const METRIC_DATABASE_ID_OPERATIONS_READS_VECTORSDB = 'vectorsdb.{databaseInternalId}.databases.operations.reads'; const METRIC_DATABASES_OPERATIONS_WRITES_VECTORSDB = 'vectorsdb.databases.operations.writes'; -const METRIC_DATABASE_ID_OPERATIONS_WRITES_VECTORSDB = 'vectorsdb.{databaseInternalId}.databases.operations.writes'; const METRIC_EMBEDDINGS_TEXT = 'embeddings.text'; const METRIC_EMBEDDINGS_MODEL_TEXT = 'embeddings.text.{embeddingModel}'; const METRIC_EMBEDDINGS_TEXT_TOTAL_ERROR = 'embeddings.text.totalErrors'; @@ -384,11 +367,7 @@ const METRIC_BUCKETS = 'buckets'; const METRIC_FILES = 'files'; const METRIC_FILES_STORAGE = 'files.storage'; const METRIC_FILES_TRANSFORMATIONS = 'files.transformations'; -const METRIC_BUCKET_ID_FILES_TRANSFORMATIONS = '{bucketInternalId}.files.transformations'; const METRIC_FILES_IMAGES_TRANSFORMED = 'files.imagesTransformed'; -const METRIC_BUCKET_ID_FILES_IMAGES_TRANSFORMED = '{bucketInternalId}.files.imagesTransformed'; -const METRIC_BUCKET_ID_FILES = '{bucketInternalId}.files'; -const METRIC_BUCKET_ID_FILES_STORAGE = '{bucketInternalId}.files.storage'; const METRIC_SITES = 'sites'; const METRIC_FUNCTIONS = 'functions'; const METRIC_DEPLOYMENTS = 'deployments'; @@ -404,19 +383,6 @@ const METRIC_BUILDS_MB_SECONDS = 'builds.mbSeconds'; const METRIC_EXECUTIONS = 'executions'; const METRIC_EXECUTIONS_COMPUTE = 'executions.compute'; const METRIC_EXECUTIONS_MB_SECONDS = 'executions.mbSeconds'; -const METRIC_RESOURCE_TYPE_ID_EXECUTIONS = '{resourceType}.{resourceInternalId}.executions'; -const METRIC_RESOURCE_TYPE_ID_EXECUTIONS_COMPUTE = '{resourceType}.{resourceInternalId}.executions.compute'; -const METRIC_RESOURCE_TYPE_ID_EXECUTIONS_MB_SECONDS = '{resourceType}.{resourceInternalId}.executions.mbSeconds'; -const METRIC_RESOURCE_TYPE_ID_BUILDS_SUCCESS = '{resourceType}.{resourceInternalId}.builds.success'; -const METRIC_RESOURCE_TYPE_ID_BUILDS_FAILED = '{resourceType}.{resourceInternalId}.builds.failed'; -const METRIC_RESOURCE_TYPE_ID_BUILDS_COMPUTE = '{resourceType}.{resourceInternalId}.builds.compute'; -const METRIC_RESOURCE_TYPE_ID_BUILDS_COMPUTE_SUCCESS = '{resourceType}.{resourceInternalId}.builds.compute.success'; -const METRIC_RESOURCE_TYPE_ID_BUILDS_COMPUTE_FAILED = '{resourceType}.{resourceInternalId}.builds.compute.failed'; -const METRIC_RESOURCE_TYPE_ID_BUILDS_MB_SECONDS = '{resourceType}.{resourceInternalId}.builds.mbSeconds'; -const METRIC_RESOURCE_TYPE_ID_BUILDS = '{resourceType}.{resourceInternalId}.builds'; -const METRIC_RESOURCE_TYPE_ID_BUILDS_STORAGE = '{resourceType}.{resourceInternalId}.builds.storage'; -const METRIC_RESOURCE_TYPE_ID_DEPLOYMENTS = '{resourceType}.{resourceInternalId}.deployments'; -const METRIC_RESOURCE_TYPE_ID_DEPLOYMENTS_STORAGE = '{resourceType}.{resourceInternalId}.deployments.storage'; const METRIC_RESOURCE_TYPE_EXECUTIONS = '{resourceType}.executions'; const METRIC_RESOURCE_TYPE_EXECUTIONS_COMPUTE = '{resourceType}.executions.compute'; const METRIC_RESOURCE_TYPE_EXECUTIONS_MB_SECONDS = '{resourceType}.executions.mbSeconds'; @@ -448,9 +414,6 @@ const METRIC_DOMAINS = 'domains'; const METRIC_SITES_REQUESTS = 'sites.requests'; const METRIC_SITES_INBOUND = 'sites.inbound'; const METRIC_SITES_OUTBOUND = 'sites.outbound'; -const METRIC_SITES_ID_REQUESTS = 'sites.{siteInternalId}.requests'; -const METRIC_SITES_ID_INBOUND = 'sites.{siteInternalId}.inbound'; -const METRIC_SITES_ID_OUTBOUND = 'sites.{siteInternalId}.outbound'; const METRIC_AVATARS_SCREENSHOTS_GENERATED = 'avatars.screenshotsGenerated'; const METRIC_FUNCTIONS_RUNTIME = 'functions.runtimes.{runtime}'; const METRIC_SITES_FRAMEWORK = 'sites.frameworks.{framework}'; From 27671e05b0834bfd93f142b7312c08a212d750a1 Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Sun, 5 Jul 2026 05:41:52 +0000 Subject: [PATCH 02/13] feat(usage): declare resource-dimension setter stubs on base Context Adds setService / setResource / setResourceId / setResourceInternalId no-op stubs on the base CE Context, matching the existing pattern for setResourcePath. The Cloud Context subclass overrides these with real storage so per-metric rows carry the dimension in their own columns instead of via a baked metric-name template. --- src/Appwrite/Usage/Context.php | 40 ++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/src/Appwrite/Usage/Context.php b/src/Appwrite/Usage/Context.php index f095eb4981..49e852fed7 100644 --- a/src/Appwrite/Usage/Context.php +++ b/src/Appwrite/Usage/Context.php @@ -71,6 +71,46 @@ class Context return ''; } + public function setService(string $service): self + { + return $this; + } + + public function getService(): string + { + return ''; + } + + public function setResource(string $resource): self + { + return $this; + } + + public function getResource(): string + { + return ''; + } + + public function setResourceId(string $resourceId): self + { + return $this; + } + + public function getResourceId(): string + { + return ''; + } + + public function setResourceInternalId(string $resourceInternalId): self + { + return $this; + } + + public function getResourceInternalId(): string + { + return ''; + } + /** * Reset the context */ From ae4dd9c3d0080ed75d4cddff80bbd9f9919d7d2d Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Sun, 5 Jul 2026 05:42:05 +0000 Subject: [PATCH 03/13] refactor(usage): route resource dims via Context in write path Every emitter that used to bake a resourceInternalId (function, site, webhook, deployment, database, bucket) into the metric-name string now sets that dim on the Context. On CE the setters are no-ops; on cloud they populate dedicated schema columns so the underlying row carries the dimension without a per-ID template. Files touched: RequestCompleted/ExecutionCompleted listener, Builds and Webhooks workers, Screenshots disabledMetrics list, Functions execution create, Migrations resource-import counters, and the shared request init that fans out per-collection database events. --- app/init/resources/request.php | 113 ++++++------------ src/Appwrite/Bus/Listeners/Usage.php | 16 ++- .../Functions/Http/Executions/Create.php | 17 +-- .../Modules/Functions/Workers/Builds.php | 39 +++--- .../Modules/Functions/Workers/Screenshots.php | 3 - src/Appwrite/Platform/Workers/Migrations.php | 14 +-- src/Appwrite/Platform/Workers/Webhooks.php | 10 +- 7 files changed, 86 insertions(+), 126 deletions(-) diff --git a/app/init/resources/request.php b/app/init/resources/request.php index ded4f735a6..a82a9e24a4 100644 --- a/app/init/resources/request.php +++ b/app/init/resources/request.php @@ -761,10 +761,10 @@ return function (Container $context): void { $parts = explode('_', $document->getCollection()); $databaseInternalId = $parts[1] ?? 0; $collectionMetric = $getDatabaseTypePrefixedMetric($databaseType, METRIC_COLLECTIONS); - $databaseIdCollectionMetric = $getDatabaseTypePrefixedMetric($databaseType, METRIC_DATABASE_ID_COLLECTIONS); $usage - ->addMetric($collectionMetric, $value) // per project - ->addMetric(str_replace('{databaseInternalId}', $databaseInternalId, $databaseIdCollectionMetric), $value); + ->setResource('database') + ->setResourceInternalId((string) $databaseInternalId) + ->addMetric($collectionMetric, $value); // per project if ($event === Database::EVENT_DOCUMENT_DELETE) { $usage->addReduce($document); @@ -775,11 +775,11 @@ return function (Container $context): void { $databaseInternalId = $parts[1] ?? 0; $collectionInternalId = $parts[3] ?? 0; $documentsMetric = $getDatabaseTypePrefixedMetric($databaseType, METRIC_DOCUMENTS); - $databaseIdDocumentsMetric = $getDatabaseTypePrefixedMetric($databaseType, METRIC_DATABASE_ID_DOCUMENTS); $databaseIdCollectionIdDocumentsMetric = $getDatabaseTypePrefixedMetric($databaseType, METRIC_DATABASE_ID_COLLECTION_ID_DOCUMENTS); $usage + ->setResource('database') + ->setResourceInternalId((string) $databaseInternalId) ->addMetric($documentsMetric, $value) // per project - ->addMetric(str_replace('{databaseInternalId}', $databaseInternalId, $databaseIdDocumentsMetric), $value) // per database ->addMetric(str_replace(['{databaseInternalId}', '{collectionInternalId}'], [$databaseInternalId, $collectionInternalId], $databaseIdCollectionIdDocumentsMetric), $value); // per collection break; case $document->getCollection() === 'buckets': // buckets @@ -793,10 +793,10 @@ return function (Container $context): void { $parts = explode('_', $document->getCollection()); $bucketInternalId = $parts[1]; $usage + ->setResource('bucket') + ->setResourceInternalId((string) $bucketInternalId) ->addMetric(METRIC_FILES, $value) // per project - ->addMetric(METRIC_FILES_STORAGE, $document->getAttribute('sizeOriginal') * $value) // per project - ->addMetric(str_replace('{bucketInternalId}', $bucketInternalId, METRIC_BUCKET_ID_FILES), $value) // per bucket - ->addMetric(str_replace('{bucketInternalId}', $bucketInternalId, METRIC_BUCKET_ID_FILES_STORAGE), $document->getAttribute('sizeOriginal') * $value); // per bucket + ->addMetric(METRIC_FILES_STORAGE, $document->getAttribute('sizeOriginal') * $value); // per project break; case $document->getCollection() === 'functions': $usage->addMetric(METRIC_FUNCTIONS, $value); // per project @@ -815,13 +815,14 @@ return function (Container $context): void { } break; case $document->getCollection() === 'deployments': + $resourceType = $document->getAttribute('resourceType'); $usage + ->setResource(rtrim($resourceType, 's')) + ->setResourceInternalId((string) $document->getAttribute('resourceInternalId')) ->addMetric(METRIC_DEPLOYMENTS, $value) // per project ->addMetric(METRIC_DEPLOYMENTS_STORAGE, $document->getAttribute('size') * $value) // per project - ->addMetric(str_replace(['{resourceType}'], [$document->getAttribute('resourceType')], METRIC_RESOURCE_TYPE_DEPLOYMENTS), $value) // per function - ->addMetric(str_replace(['{resourceType}'], [$document->getAttribute('resourceType')], METRIC_RESOURCE_TYPE_DEPLOYMENTS_STORAGE), $document->getAttribute('size') * $value) - ->addMetric(str_replace(['{resourceType}', '{resourceInternalId}'], [$document->getAttribute('resourceType'), $document->getAttribute('resourceInternalId')], METRIC_RESOURCE_TYPE_ID_DEPLOYMENTS), $value) // per function - ->addMetric(str_replace(['{resourceType}', '{resourceInternalId}'], [$document->getAttribute('resourceType'), $document->getAttribute('resourceInternalId')], METRIC_RESOURCE_TYPE_ID_DEPLOYMENTS_STORAGE), $document->getAttribute('size') * $value); + ->addMetric(str_replace(['{resourceType}'], [$resourceType], METRIC_RESOURCE_TYPE_DEPLOYMENTS), $value) // per resource type + ->addMetric(str_replace(['{resourceType}'], [$resourceType], METRIC_RESOURCE_TYPE_DEPLOYMENTS_STORAGE), $document->getAttribute('size') * $value); break; default: break; @@ -1214,78 +1215,42 @@ return function (Container $context): void { // Register database event listeners for usage stats collection $documentsMetric = METRIC_DOCUMENTS; - $databaseIdDocumentsMetric = METRIC_DATABASE_ID_DOCUMENTS; $databaseIdCollectionIdDocumentsMetric = METRIC_DATABASE_ID_COLLECTION_ID_DOCUMENTS; if ($databaseType !== DATABASE_TYPE_LEGACY && $databaseType !== DATABASE_TYPE_TABLESDB) { $documentsMetric = $databaseType . '.' . $documentsMetric; - $databaseIdDocumentsMetric = $databaseType . '.' . $databaseIdDocumentsMetric; $databaseIdCollectionIdDocumentsMetric = $databaseType . '.' . $databaseIdCollectionIdDocumentsMetric; } + + $emitDocumentsUsage = function (Document $document, int $value) use ($usage, $documentsMetric, $databaseIdCollectionIdDocumentsMetric): void { + if (! str_starts_with($document->getCollection(), 'database_') || ! str_contains($document->getCollection(), '_collection_')) { + return; + } + + $parts = explode('_', $document->getCollection()); + $databaseInternalId = $parts[1] ?? 0; + $collectionInternalId = $parts[3] ?? 0; + $usage + ->setResource('database') + ->setResourceInternalId((string) $databaseInternalId) + ->addMetric($documentsMetric, $value) // per project + ->addMetric(str_replace(['{databaseInternalId}', '{collectionInternalId}'], [$databaseInternalId, $collectionInternalId], $databaseIdCollectionIdDocumentsMetric), $value); // per collection + }; + $database - ->on(Database::EVENT_DOCUMENT_CREATE, 'calculate-usage', function ($event, $document) use ($usage, $documentsMetric, $databaseIdDocumentsMetric, $databaseIdCollectionIdDocumentsMetric) { - $value = 1; - - if (str_starts_with($document->getCollection(), 'database_') && str_contains($document->getCollection(), '_collection_')) { - $parts = explode('_', $document->getCollection()); - $databaseInternalId = $parts[1] ?? 0; - $collectionInternalId = $parts[3] ?? 0; - $usage - ->addMetric($documentsMetric, $value) // per project - ->addMetric(str_replace('{databaseInternalId}', $databaseInternalId, $databaseIdDocumentsMetric), $value) // per database - ->addMetric(str_replace(['{databaseInternalId}', '{collectionInternalId}'], [$databaseInternalId, $collectionInternalId], $databaseIdCollectionIdDocumentsMetric), $value); // per collection - } + ->on(Database::EVENT_DOCUMENT_CREATE, 'calculate-usage', function ($event, $document) use ($emitDocumentsUsage) { + $emitDocumentsUsage($document, 1); }) - ->on(Database::EVENT_DOCUMENT_DELETE, 'calculate-usage', function ($event, $document) use ($usage, $documentsMetric, $databaseIdDocumentsMetric, $databaseIdCollectionIdDocumentsMetric) { - $value = -1; - - if (str_starts_with($document->getCollection(), 'database_') && str_contains($document->getCollection(), '_collection_')) { - $parts = explode('_', $document->getCollection()); - $databaseInternalId = $parts[1] ?? 0; - $collectionInternalId = $parts[3] ?? 0; - $usage - ->addMetric($documentsMetric, $value) // per project - ->addMetric(str_replace('{databaseInternalId}', $databaseInternalId, $databaseIdDocumentsMetric), $value) // per database - ->addMetric(str_replace(['{databaseInternalId}', '{collectionInternalId}'], [$databaseInternalId, $collectionInternalId], $databaseIdCollectionIdDocumentsMetric), $value); // per collection - } + ->on(Database::EVENT_DOCUMENT_DELETE, 'calculate-usage', function ($event, $document) use ($emitDocumentsUsage) { + $emitDocumentsUsage($document, -1); }) - ->on(Database::EVENT_DOCUMENTS_CREATE, 'calculate-usage', function ($event, $document) use ($usage, $documentsMetric, $databaseIdDocumentsMetric, $databaseIdCollectionIdDocumentsMetric) { - $value = $document->getAttribute('modified', 0); - - if (str_starts_with($document->getCollection(), 'database_') && str_contains($document->getCollection(), '_collection_')) { - $parts = explode('_', $document->getCollection()); - $databaseInternalId = $parts[1] ?? 0; - $collectionInternalId = $parts[3] ?? 0; - $usage - ->addMetric($documentsMetric, $value) // per project - ->addMetric(str_replace('{databaseInternalId}', $databaseInternalId, $databaseIdDocumentsMetric), $value) // per database - ->addMetric(str_replace(['{databaseInternalId}', '{collectionInternalId}'], [$databaseInternalId, $collectionInternalId], $databaseIdCollectionIdDocumentsMetric), $value); // per collection - } + ->on(Database::EVENT_DOCUMENTS_CREATE, 'calculate-usage', function ($event, $document) use ($emitDocumentsUsage) { + $emitDocumentsUsage($document, $document->getAttribute('modified', 0)); }) - ->on(Database::EVENT_DOCUMENTS_DELETE, 'calculate-usage', function ($event, $document) use ($usage, $documentsMetric, $databaseIdDocumentsMetric, $databaseIdCollectionIdDocumentsMetric) { - $value = -1 * $document->getAttribute('modified', 0); - - if (str_starts_with($document->getCollection(), 'database_') && str_contains($document->getCollection(), '_collection_')) { - $parts = explode('_', $document->getCollection()); - $databaseInternalId = $parts[1] ?? 0; - $collectionInternalId = $parts[3] ?? 0; - $usage - ->addMetric($documentsMetric, $value) // per project - ->addMetric(str_replace('{databaseInternalId}', $databaseInternalId, $databaseIdDocumentsMetric), $value) // per database - ->addMetric(str_replace(['{databaseInternalId}', '{collectionInternalId}'], [$databaseInternalId, $collectionInternalId], $databaseIdCollectionIdDocumentsMetric), $value); // per collection - } + ->on(Database::EVENT_DOCUMENTS_DELETE, 'calculate-usage', function ($event, $document) use ($emitDocumentsUsage) { + $emitDocumentsUsage($document, -1 * $document->getAttribute('modified', 0)); }) - ->on(Database::EVENT_DOCUMENTS_UPSERT, 'calculate-usage', function ($event, $document) use ($usage, $documentsMetric, $databaseIdDocumentsMetric, $databaseIdCollectionIdDocumentsMetric) { - $value = $document->getAttribute('created', 0); - - if (str_starts_with($document->getCollection(), 'database_') && str_contains($document->getCollection(), '_collection_')) { - $parts = explode('_', $document->getCollection()); - $databaseInternalId = $parts[1] ?? 0; - $collectionInternalId = $parts[3] ?? 0; - $usage - ->addMetric($documentsMetric, $value) // per project - ->addMetric(str_replace('{databaseInternalId}', $databaseInternalId, $databaseIdDocumentsMetric), $value) // per database - ->addMetric(str_replace(['{databaseInternalId}', '{collectionInternalId}'], [$databaseInternalId, $collectionInternalId], $databaseIdCollectionIdDocumentsMetric), $value); // per collection - } + ->on(Database::EVENT_DOCUMENTS_UPSERT, 'calculate-usage', function ($event, $document) use ($emitDocumentsUsage) { + $emitDocumentsUsage($document, $document->getAttribute('created', 0)); }); return $database; diff --git a/src/Appwrite/Bus/Listeners/Usage.php b/src/Appwrite/Bus/Listeners/Usage.php index 48178f1dee..1d675682bb 100644 --- a/src/Appwrite/Bus/Listeners/Usage.php +++ b/src/Appwrite/Bus/Listeners/Usage.php @@ -64,15 +64,14 @@ class Usage extends Listener $mbSeconds = (int)(($spec['memory'] ?? APP_COMPUTE_MEMORY_DEFAULT) * $duration * ($spec['cpus'] ?? APP_COMPUTE_CPUS_DEFAULT)); $context = (new Context()) + ->setResource(rtrim($resourceType, 's')) + ->setResourceInternalId((string) $resourceInternalId) ->addMetric(METRIC_EXECUTIONS, 1) ->addMetric(str_replace(['{resourceType}'], [$resourceType], METRIC_RESOURCE_TYPE_EXECUTIONS), 1) - ->addMetric(str_replace(['{resourceType}', '{resourceInternalId}'], [$resourceType, $resourceInternalId], METRIC_RESOURCE_TYPE_ID_EXECUTIONS), 1) ->addMetric(METRIC_EXECUTIONS_COMPUTE, $compute) ->addMetric(str_replace(['{resourceType}'], [$resourceType], METRIC_RESOURCE_TYPE_EXECUTIONS_COMPUTE), $compute) - ->addMetric(str_replace(['{resourceType}', '{resourceInternalId}'], [$resourceType, $resourceInternalId], METRIC_RESOURCE_TYPE_ID_EXECUTIONS_COMPUTE), $compute) ->addMetric(METRIC_EXECUTIONS_MB_SECONDS, $mbSeconds) - ->addMetric(str_replace(['{resourceType}'], [$resourceType], METRIC_RESOURCE_TYPE_EXECUTIONS_MB_SECONDS), $mbSeconds) - ->addMetric(str_replace(['{resourceType}', '{resourceInternalId}'], [$resourceType, $resourceInternalId], METRIC_RESOURCE_TYPE_ID_EXECUTIONS_MB_SECONDS), $mbSeconds); + ->addMetric(str_replace(['{resourceType}'], [$resourceType], METRIC_RESOURCE_TYPE_EXECUTIONS_MB_SECONDS), $mbSeconds); $message = new UsageMessage( project: $project, @@ -97,14 +96,13 @@ class Usage extends Listener $outbound = $event->response->getSize(); if ($deployment->getAttribute('resourceType') === 'sites') { - $siteInternalId = $deployment->getAttribute('resourceInternalId', ''); + $siteInternalId = (string) $deployment->getAttribute('resourceInternalId', ''); $usage + ->setResource('site') + ->setResourceInternalId($siteInternalId) ->addMetric(METRIC_SITES_REQUESTS, 1) ->addMetric(METRIC_SITES_INBOUND, $inbound) - ->addMetric(METRIC_SITES_OUTBOUND, $outbound) - ->addMetric(str_replace('{siteInternalId}', $siteInternalId, METRIC_SITES_ID_REQUESTS), 1) - ->addMetric(str_replace('{siteInternalId}', $siteInternalId, METRIC_SITES_ID_INBOUND), $inbound) - ->addMetric(str_replace('{siteInternalId}', $siteInternalId, METRIC_SITES_ID_OUTBOUND), $outbound); + ->addMetric(METRIC_SITES_OUTBOUND, $outbound); } else { $usage ->addMetric(METRIC_NETWORK_REQUESTS, 1) diff --git a/src/Appwrite/Platform/Modules/Functions/Http/Executions/Create.php b/src/Appwrite/Platform/Modules/Functions/Http/Executions/Create.php index 303bf1484b..e8e05ede09 100644 --- a/src/Appwrite/Platform/Modules/Functions/Http/Executions/Create.php +++ b/src/Appwrite/Platform/Modules/Functions/Http/Executions/Create.php @@ -494,16 +494,19 @@ class Create extends Base throw $th; } } finally { + $duration = $execution->getAttribute('duration', 0); + $compute = (int) ($duration * 1000); + $mbSeconds = (int) (($spec['memory'] ?? APP_COMPUTE_MEMORY_DEFAULT) * $duration * ($spec['cpus'] ?? APP_COMPUTE_CPUS_DEFAULT)); + $usage + ->setResource('function') + ->setResourceInternalId((string) $function->getSequence()) ->addMetric(METRIC_EXECUTIONS, 1) ->addMetric(str_replace(['{resourceType}'], [RESOURCE_TYPE_FUNCTIONS], METRIC_RESOURCE_TYPE_EXECUTIONS), 1) - ->addMetric(str_replace(['{resourceType}', '{resourceInternalId}'], [RESOURCE_TYPE_FUNCTIONS, $function->getSequence()], METRIC_RESOURCE_TYPE_ID_EXECUTIONS), 1) - ->addMetric(METRIC_EXECUTIONS_COMPUTE, (int)($execution->getAttribute('duration') * 1000)) // per project - ->addMetric(str_replace(['{resourceType}'], [RESOURCE_TYPE_FUNCTIONS], METRIC_RESOURCE_TYPE_EXECUTIONS_COMPUTE), (int)($execution->getAttribute('duration') * 1000)) // per function - ->addMetric(str_replace(['{resourceType}', '{resourceInternalId}'], [RESOURCE_TYPE_FUNCTIONS, $function->getSequence()], METRIC_RESOURCE_TYPE_ID_EXECUTIONS_COMPUTE), (int)($execution->getAttribute('duration') * 1000)) // per function - ->addMetric(METRIC_EXECUTIONS_MB_SECONDS, (int)(($spec['memory'] ?? APP_COMPUTE_MEMORY_DEFAULT) * $execution->getAttribute('duration', 0) * ($spec['cpus'] ?? APP_COMPUTE_CPUS_DEFAULT))) - ->addMetric(str_replace(['{resourceType}'], [RESOURCE_TYPE_FUNCTIONS], METRIC_RESOURCE_TYPE_EXECUTIONS_MB_SECONDS), (int)(($spec['memory'] ?? APP_COMPUTE_MEMORY_DEFAULT) * $execution->getAttribute('duration', 0) * ($spec['cpus'] ?? APP_COMPUTE_CPUS_DEFAULT))) - ->addMetric(str_replace(['{resourceType}', '{resourceInternalId}'], [RESOURCE_TYPE_FUNCTIONS, $function->getSequence()], METRIC_RESOURCE_TYPE_ID_EXECUTIONS_MB_SECONDS), (int)(($spec['memory'] ?? APP_COMPUTE_MEMORY_DEFAULT) * $execution->getAttribute('duration', 0) * ($spec['cpus'] ?? APP_COMPUTE_CPUS_DEFAULT))) + ->addMetric(METRIC_EXECUTIONS_COMPUTE, $compute) // per project + ->addMetric(str_replace(['{resourceType}'], [RESOURCE_TYPE_FUNCTIONS], METRIC_RESOURCE_TYPE_EXECUTIONS_COMPUTE), $compute) // per resource type + ->addMetric(METRIC_EXECUTIONS_MB_SECONDS, $mbSeconds) + ->addMetric(str_replace(['{resourceType}'], [RESOURCE_TYPE_FUNCTIONS], METRIC_RESOURCE_TYPE_EXECUTIONS_MB_SECONDS), $mbSeconds) ; $execution = $authorization->skip(fn () => $dbForProject->createDocument('executions', $execution)); diff --git a/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php b/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php index 077dc3bc1c..4ac33e9f59 100644 --- a/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php +++ b/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php @@ -1262,41 +1262,40 @@ class Builds extends Action $spec = Config::getParam('specifications')[$resource->getAttribute('buildSpecification', APP_COMPUTE_SPECIFICATION_DEFAULT)]; $cpus = (int) ($spec['cpus'] ?? APP_COMPUTE_CPUS_DEFAULT); $memory = (int) ($spec['memory'] ?? APP_COMPUTE_MEMORY_DEFAULT); + $resourceType = $deployment->getAttribute('resourceType'); + $buildDuration = (int) $deployment->getAttribute('buildDuration', 0) * 1000; + $mbSeconds = (int) ($memory * $deployment->getAttribute('buildDuration', 0) * $cpus); + + $usage + ->setResource(rtrim($resourceType, 's')) + ->setResourceInternalId((string) $resource->getSequence()); switch ($deployment->getAttribute('status')) { case 'ready': $usage ->addMetric(METRIC_BUILDS_SUCCESS, 1) // per project - ->addMetric(METRIC_BUILDS_COMPUTE_SUCCESS, (int) $deployment->getAttribute('buildDuration', 0) * 1000) - ->addMetric(str_replace(['{resourceType}'], [$deployment->getAttribute('resourceType')], METRIC_RESOURCE_TYPE_BUILDS_SUCCESS), 1) // per function - ->addMetric(str_replace(['{resourceType}'], [$deployment->getAttribute('resourceType')], METRIC_RESOURCE_TYPE_BUILDS_COMPUTE_SUCCESS), (int) $deployment->getAttribute('buildDuration', 0) * 1000) - ->addMetric(str_replace(['{resourceType}', '{resourceInternalId}'], [$deployment->getAttribute('resourceType'), $resource->getSequence()], METRIC_RESOURCE_TYPE_ID_BUILDS_SUCCESS), 1) // per function - ->addMetric(str_replace(['{resourceType}', '{resourceInternalId}'], [$deployment->getAttribute('resourceType'), $resource->getSequence()], METRIC_RESOURCE_TYPE_ID_BUILDS_COMPUTE_SUCCESS), (int) $deployment->getAttribute('buildDuration', 0) * 1000); + ->addMetric(METRIC_BUILDS_COMPUTE_SUCCESS, $buildDuration) + ->addMetric(str_replace(['{resourceType}'], [$resourceType], METRIC_RESOURCE_TYPE_BUILDS_SUCCESS), 1) // per resource type + ->addMetric(str_replace(['{resourceType}'], [$resourceType], METRIC_RESOURCE_TYPE_BUILDS_COMPUTE_SUCCESS), $buildDuration); break; case 'failed': $usage ->addMetric(METRIC_BUILDS_FAILED, 1) // per project - ->addMetric(METRIC_BUILDS_COMPUTE_FAILED, (int) $deployment->getAttribute('buildDuration', 0) * 1000) - ->addMetric(str_replace(['{resourceType}'], [$deployment->getAttribute('resourceType')], METRIC_RESOURCE_TYPE_BUILDS_FAILED), 1) // per function - ->addMetric(str_replace(['{resourceType}'], [$deployment->getAttribute('resourceType')], METRIC_RESOURCE_TYPE_BUILDS_COMPUTE_FAILED), (int) $deployment->getAttribute('buildDuration', 0) * 1000) - ->addMetric(str_replace(['{resourceType}', '{resourceInternalId}'], [$deployment->getAttribute('resourceType'), $resource->getSequence()], METRIC_RESOURCE_TYPE_ID_BUILDS_FAILED), 1) // per function - ->addMetric(str_replace(['{resourceType}', '{resourceInternalId}'], [$deployment->getAttribute('resourceType'), $resource->getSequence()], METRIC_RESOURCE_TYPE_ID_BUILDS_COMPUTE_FAILED), (int) $deployment->getAttribute('buildDuration', 0) * 1000); + ->addMetric(METRIC_BUILDS_COMPUTE_FAILED, $buildDuration) + ->addMetric(str_replace(['{resourceType}'], [$resourceType], METRIC_RESOURCE_TYPE_BUILDS_FAILED), 1) // per resource type + ->addMetric(str_replace(['{resourceType}'], [$resourceType], METRIC_RESOURCE_TYPE_BUILDS_COMPUTE_FAILED), $buildDuration); break; } $usage ->addMetric(METRIC_BUILDS, 1) // per project ->addMetric(METRIC_BUILDS_STORAGE, $deployment->getAttribute('buildSize', 0)) - ->addMetric(METRIC_BUILDS_COMPUTE, (int) $deployment->getAttribute('buildDuration', 0) * 1000) - ->addMetric(METRIC_BUILDS_MB_SECONDS, (int) ($memory * $deployment->getAttribute('buildDuration', 0) * $cpus)) - ->addMetric(str_replace(['{resourceType}'], [$deployment->getAttribute('resourceType')], METRIC_RESOURCE_TYPE_BUILDS), 1) // per function - ->addMetric(str_replace(['{resourceType}'], [$deployment->getAttribute('resourceType')], METRIC_RESOURCE_TYPE_BUILDS_STORAGE), $deployment->getAttribute('buildSize', 0)) - ->addMetric(str_replace(['{resourceType}'], [$deployment->getAttribute('resourceType')], METRIC_RESOURCE_TYPE_BUILDS_COMPUTE), (int) $deployment->getAttribute('buildDuration', 0) * 1000) - ->addMetric(str_replace(['{resourceType}'], [$deployment->getAttribute('resourceType')], METRIC_RESOURCE_TYPE_BUILDS_MB_SECONDS), (int) ($memory * $deployment->getAttribute('buildDuration', 0) * $cpus)) - ->addMetric(str_replace(['{resourceType}', '{resourceInternalId}'], [$deployment->getAttribute('resourceType'), $resource->getSequence()], METRIC_RESOURCE_TYPE_ID_BUILDS), 1) // per function - ->addMetric(str_replace(['{resourceType}', '{resourceInternalId}'], [$deployment->getAttribute('resourceType'), $resource->getSequence()], METRIC_RESOURCE_TYPE_ID_BUILDS_STORAGE), $deployment->getAttribute('buildSize', 0)) - ->addMetric(str_replace(['{resourceType}', '{resourceInternalId}'], [$deployment->getAttribute('resourceType'), $resource->getSequence()], METRIC_RESOURCE_TYPE_ID_BUILDS_COMPUTE), (int) $deployment->getAttribute('buildDuration', 0) * 1000) - ->addMetric(str_replace(['{resourceType}', '{resourceInternalId}'], [$deployment->getAttribute('resourceType'), $resource->getSequence()], METRIC_RESOURCE_TYPE_ID_BUILDS_MB_SECONDS), (int) ($memory * $deployment->getAttribute('buildDuration', 0) * $cpus)); + ->addMetric(METRIC_BUILDS_COMPUTE, $buildDuration) + ->addMetric(METRIC_BUILDS_MB_SECONDS, $mbSeconds) + ->addMetric(str_replace(['{resourceType}'], [$resourceType], METRIC_RESOURCE_TYPE_BUILDS), 1) // per resource type + ->addMetric(str_replace(['{resourceType}'], [$resourceType], METRIC_RESOURCE_TYPE_BUILDS_STORAGE), $deployment->getAttribute('buildSize', 0)) + ->addMetric(str_replace(['{resourceType}'], [$resourceType], METRIC_RESOURCE_TYPE_BUILDS_COMPUTE), $buildDuration) + ->addMetric(str_replace(['{resourceType}'], [$resourceType], METRIC_RESOURCE_TYPE_BUILDS_MB_SECONDS), $mbSeconds); // Publish usage metrics if (! $usage->isEmpty()) { diff --git a/src/Appwrite/Platform/Modules/Functions/Workers/Screenshots.php b/src/Appwrite/Platform/Modules/Functions/Workers/Screenshots.php index 35cbe63a15..a3d9aa24b3 100644 --- a/src/Appwrite/Platform/Modules/Functions/Workers/Screenshots.php +++ b/src/Appwrite/Platform/Modules/Functions/Workers/Screenshots.php @@ -151,9 +151,6 @@ class Screenshots extends Action str_replace(["{resourceType}"], [RESOURCE_TYPE_SITES], METRIC_RESOURCE_TYPE_EXECUTIONS), str_replace(["{resourceType}"], [RESOURCE_TYPE_SITES], METRIC_RESOURCE_TYPE_EXECUTIONS_COMPUTE), str_replace(["{resourceType}"], [RESOURCE_TYPE_SITES], METRIC_RESOURCE_TYPE_EXECUTIONS_MB_SECONDS), - str_replace(["{resourceType}", "{resourceInternalId}"], [RESOURCE_TYPE_SITES, $site->getSequence()], METRIC_RESOURCE_TYPE_ID_EXECUTIONS), - str_replace(["{resourceType}", "{resourceInternalId}"], [RESOURCE_TYPE_SITES, $site->getSequence()], METRIC_RESOURCE_TYPE_ID_EXECUTIONS_COMPUTE), - str_replace(["{resourceType}", "{resourceInternalId}"], [RESOURCE_TYPE_SITES, $site->getSequence()], METRIC_RESOURCE_TYPE_ID_EXECUTIONS_MB_SECONDS), ], 'bannerDisabled' => true, 'projectCheckDisabled' => true, diff --git a/src/Appwrite/Platform/Workers/Migrations.php b/src/Appwrite/Platform/Workers/Migrations.php index 60deaa2f00..59fe274e2f 100644 --- a/src/Appwrite/Platform/Workers/Migrations.php +++ b/src/Appwrite/Platform/Workers/Migrations.php @@ -1023,15 +1023,15 @@ class Migrations extends Action case ResourceTable::getName(): $usage - ->addMetric(METRIC_COLLECTIONS, $count) - ->addMetric( - str_replace('{databaseInternalId}', $databaseInternalId, METRIC_DATABASE_ID_COLLECTIONS), - $count - ); + ->setResource('database') + ->setResourceInternalId((string) $databaseInternalId) + ->addMetric(METRIC_COLLECTIONS, $count); break; case ResourceRow::getName(): $usage + ->setResource('database') + ->setResourceInternalId((string) $databaseInternalId) ->addMetric( str_replace( ['{databaseInternalId}','{collectionInternalId}'], @@ -1040,10 +1040,6 @@ class Migrations extends Action ), $count ) - ->addMetric( - str_replace('{databaseInternalId}', $databaseInternalId, METRIC_DATABASE_ID_DOCUMENTS), - $count - ) ->addMetric(METRIC_DOCUMENTS, $count); break; diff --git a/src/Appwrite/Platform/Workers/Webhooks.php b/src/Appwrite/Platform/Workers/Webhooks.php index 5466ade630..4f1e572f04 100644 --- a/src/Appwrite/Platform/Workers/Webhooks.php +++ b/src/Appwrite/Platform/Workers/Webhooks.php @@ -193,16 +193,18 @@ class Webhooks extends Action $error = $logs; $usage = (new UsageContext()) - ->addMetric(METRIC_WEBHOOKS_FAILED, 1) - ->addMetric(str_replace('{webhookInternalId}', $webhook->getSequence(), METRIC_WEBHOOK_ID_FAILED), 1); + ->setResource('webhook') + ->setResourceInternalId((string) $webhook->getSequence()) + ->addMetric(METRIC_WEBHOOKS_FAILED, 1); } else { $dbForPlatform->updateDocument('webhooks', $webhook->getId(), new Document([ 'attempts' => 0, ])); $dbForPlatform->purgeCachedDocument('projects', $project->getId()); $usage = (new UsageContext()) - ->addMetric(METRIC_WEBHOOKS_SENT, 1) - ->addMetric(str_replace('{webhookInternalId}', $webhook->getSequence(), METRIC_WEBHOOK_ID_SENT), 1); + ->setResource('webhook') + ->setResourceInternalId((string) $webhook->getSequence()) + ->addMetric(METRIC_WEBHOOKS_SENT, 1); } $publisherForUsage->enqueue(new UsageMessage( From af067f67ac11bf92dc34e806a036799a4f8aa8d5 Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Sun, 5 Jul 2026 05:42:15 +0000 Subject: [PATCH 04/13] refactor(usage): drop {databaseInternalId} splits on databases operations Removes getDatabasesIdOperationReadMetric / getDatabasesIdOperationWriteMetric from the Documents and Transactions Action bases and drops every caller's per-database addMetric. The project-level databases.operations.reads / writes emit stays as the sole write; per-database attribution is now carried by resource='database' + resourceInternalId on the Context. --- .../Databases/Collections/Documents/Action.php | 16 ---------------- .../Documents/Attribute/Decrement.php | 5 +++-- .../Documents/Attribute/Increment.php | 5 +++-- .../Collections/Documents/Bulk/Delete.php | 5 +++-- .../Collections/Documents/Bulk/Update.php | 5 +++-- .../Collections/Documents/Bulk/Upsert.php | 5 +++-- .../Databases/Collections/Documents/Create.php | 5 +++-- .../Databases/Collections/Documents/Delete.php | 5 +++-- .../Http/Databases/Collections/Documents/Get.php | 5 +++-- .../Databases/Collections/Documents/Update.php | 5 +++-- .../Databases/Collections/Documents/Upsert.php | 5 +++-- .../Databases/Collections/Documents/XList.php | 5 +++-- .../Http/Databases/Transactions/Action.php | 7 ------- .../Http/Databases/Transactions/Update.php | 7 ------- 14 files changed, 33 insertions(+), 52 deletions(-) diff --git a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Action.php b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Action.php index d62782f95e..48ab9b298f 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Action.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Action.php @@ -61,14 +61,6 @@ abstract class Action extends DatabasesAction return $this->databaseType.'.'.METRIC_DATABASES_OPERATIONS_READS; } - protected function getDatabasesIdOperationReadMetric(): string - { - if ($this->databaseType === DATABASE_TYPE_LEGACY || $this->databaseType === DATABASE_TYPE_TABLESDB) { - return METRIC_DATABASE_ID_OPERATIONS_READS; - } - return $this->databaseType.'.'.METRIC_DATABASE_ID_OPERATIONS_READS; - } - protected function getDatabasesOperationWriteMetric(): string { if ($this->databaseType === DATABASE_TYPE_LEGACY || $this->databaseType === DATABASE_TYPE_TABLESDB) { @@ -78,14 +70,6 @@ abstract class Action extends DatabasesAction } - protected function getDatabasesIdOperationWriteMetric(): string - { - if ($this->databaseType === DATABASE_TYPE_LEGACY || $this->databaseType === DATABASE_TYPE_TABLESDB) { - return METRIC_DATABASE_ID_OPERATIONS_WRITES; - } - return $this->databaseType.'.'.METRIC_DATABASE_ID_OPERATIONS_WRITES; - } - /** * Get the plural of the given name. * diff --git a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Attribute/Decrement.php b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Attribute/Decrement.php index 785552ddcb..f03b81c707 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Attribute/Decrement.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Attribute/Decrement.php @@ -205,8 +205,9 @@ class Decrement extends Action ); $usage - ->addMetric($this->getDatabasesOperationWriteMetric(), 1) - ->addMetric(str_replace('{databaseInternalId}', $database->getSequence(), $this->getDatabasesIdOperationWriteMetric()), 1); + ->setResource('database') + ->setResourceInternalId((string) $database->getSequence()) + ->addMetric($this->getDatabasesOperationWriteMetric(), 1); $response->dynamic($document, $this->getResponseModel()); diff --git a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Attribute/Increment.php b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Attribute/Increment.php index ecef3fa3c2..8e7a335255 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Attribute/Increment.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Attribute/Increment.php @@ -205,8 +205,9 @@ class Increment extends Action ); $usage - ->addMetric($this->getDatabasesOperationWriteMetric(), 1) - ->addMetric(str_replace('{databaseInternalId}', $database->getSequence(), $this->getDatabasesIdOperationWriteMetric()), 1); + ->setResource('database') + ->setResourceInternalId((string) $database->getSequence()) + ->addMetric($this->getDatabasesOperationWriteMetric(), 1); $response->dynamic($document, $this->getResponseModel()); diff --git a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Bulk/Delete.php b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Bulk/Delete.php index 4390fd60ce..d427841d70 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Bulk/Delete.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Bulk/Delete.php @@ -193,8 +193,9 @@ class Delete extends Action } $usage - ->addMetric($this->getDatabasesOperationWriteMetric(), \max(1, $modified)) - ->addMetric(str_replace('{databaseInternalId}', $database->getSequence(), $this->getDatabasesIdOperationWriteMetric()), \max(1, $modified)); + ->setResource('database') + ->setResourceInternalId((string) $database->getSequence()) + ->addMetric($this->getDatabasesOperationWriteMetric(), \max(1, $modified)); $response->dynamic(new Document([ 'total' => $modified, diff --git a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Bulk/Update.php b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Bulk/Update.php index f85610b153..665998f7ba 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Bulk/Update.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Bulk/Update.php @@ -224,8 +224,9 @@ class Update extends Action } $usage - ->addMetric($this->getDatabasesOperationWriteMetric(), \max(1, $modified)) - ->addMetric(str_replace('{databaseInternalId}', $database->getSequence(), $this->getDatabasesIdOperationWriteMetric()), \max(1, $modified)); + ->setResource('database') + ->setResourceInternalId((string) $database->getSequence()) + ->addMetric($this->getDatabasesOperationWriteMetric(), \max(1, $modified)); $response->dynamic(new Document([ 'total' => $modified, diff --git a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Bulk/Upsert.php b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Bulk/Upsert.php index cf483e7139..e3969951da 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Bulk/Upsert.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Bulk/Upsert.php @@ -199,8 +199,9 @@ class Upsert extends Action } $usage - ->addMetric($this->getDatabasesOperationWriteMetric(), \max(1, $modified)) - ->addMetric(str_replace('{databaseInternalId}', $database->getSequence(), $this->getDatabasesIdOperationWriteMetric()), \max(1, $modified)); + ->setResource('database') + ->setResourceInternalId((string) $database->getSequence()) + ->addMetric($this->getDatabasesOperationWriteMetric(), \max(1, $modified)); $response->dynamic(new Document([ 'total' => $modified, diff --git a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Create.php b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Create.php index 6d281cd807..0fd6a2ebbb 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Create.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Create.php @@ -501,8 +501,9 @@ class Create extends Action } $usage - ->addMetric($this->getDatabasesOperationWriteMetric(), \max(1, $operations)) - ->addMetric(str_replace('{databaseInternalId}', $database->getSequence(), $this->getDatabasesIdOperationWriteMetric()), \max(1, $operations)); // per collection + ->setResource('database') + ->setResourceInternalId((string) $database->getSequence()) + ->addMetric($this->getDatabasesOperationWriteMetric(), \max(1, $operations)); $response->setStatusCode(SwooleResponse::STATUS_CODE_CREATED); diff --git a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Delete.php b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Delete.php index 17cd5b620b..d6f16923ec 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Delete.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Delete.php @@ -217,8 +217,9 @@ class Delete extends Action ); $usage - ->addMetric(METRIC_DATABASES_OPERATIONS_WRITES, 1) - ->addMetric(str_replace('{databaseInternalId}', $database->getSequence(), METRIC_DATABASE_ID_OPERATIONS_WRITES), 1); // per collection + ->setResource('database') + ->setResourceInternalId((string) $database->getSequence()) + ->addMetric(METRIC_DATABASES_OPERATIONS_WRITES, 1); $response->addHeader('X-Debug-Operations', 1); diff --git a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Get.php b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Get.php index 54fdfc8a5b..2ca1683223 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Get.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Get.php @@ -136,8 +136,9 @@ class Get extends Action ); $usage - ->addMetric($this->getDatabasesOperationReadMetric(), max($operations, 1)) - ->addMetric(str_replace('{databaseInternalId}', $database->getSequence(), $this->getDatabasesIdOperationReadMetric()), $operations); + ->setResource('database') + ->setResourceInternalId((string) $database->getSequence()) + ->addMetric($this->getDatabasesOperationReadMetric(), max($operations, 1)); $response->addHeader('X-Debug-Operations', $operations); diff --git a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Update.php b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Update.php index 914a8cc6a9..0bcfd3ebc7 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Update.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Update.php @@ -250,8 +250,9 @@ class Update extends Action $setCollection($collection, $newDocument); $usage - ->addMetric($this->getDatabasesOperationWriteMetric(), max($operations, 1)) - ->addMetric(str_replace('{databaseInternalId}', $database->getSequence(), $this->getDatabasesIdOperationWriteMetric()), $operations); + ->setResource('database') + ->setResourceInternalId((string) $database->getSequence()) + ->addMetric($this->getDatabasesOperationWriteMetric(), max($operations, 1)); // Handle transaction staging if ($transactionId !== null) { diff --git a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Upsert.php b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Upsert.php index a9f4c5eb0a..b4bf027741 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Upsert.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Upsert.php @@ -262,8 +262,9 @@ class Upsert extends Action $setCollection($collection, $newDocument); $usage - ->addMetric($this->getDatabasesOperationWriteMetric(), \max(1, $operations)) - ->addMetric(str_replace('{databaseInternalId}', $database->getSequence(), $this->getDatabasesIdOperationWriteMetric()), \max(1, $operations)); + ->setResource('database') + ->setResourceInternalId((string) $database->getSequence()) + ->addMetric($this->getDatabasesOperationWriteMetric(), \max(1, $operations)); // Handle transaction staging if ($transactionId !== null) { diff --git a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/XList.php b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/XList.php index c7e3c18db6..4a2f603057 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/XList.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/XList.php @@ -241,8 +241,9 @@ class XList extends Action } $usage - ->addMetric($this->getDatabasesOperationReadMetric(), max($operations, 1)) - ->addMetric(str_replace('{databaseInternalId}', $database->getSequence(), $this->getDatabasesIdOperationReadMetric()), $operations); + ->setResource('database') + ->setResourceInternalId((string) $database->getSequence()) + ->addMetric($this->getDatabasesOperationReadMetric(), max($operations, 1)); $response->dynamic(new Document([ 'total' => $total, diff --git a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Transactions/Action.php b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Transactions/Action.php index ccf9632fef..c742897020 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Transactions/Action.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Transactions/Action.php @@ -25,13 +25,6 @@ abstract class Action extends DatabasesAction return $this->databaseType.'.'.METRIC_DATABASES_OPERATIONS_WRITES; } - protected function getDatabasesIdOperationWriteMetric(): string - { - if ($this->databaseType === LEGACY || $this->databaseType === TABLESDB) { - return METRIC_DATABASE_ID_OPERATIONS_WRITES; - } - return $this->databaseType.'.'.METRIC_DATABASE_ID_OPERATIONS_WRITES; - } public function setHttpPath(string $path): self { diff --git a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Transactions/Update.php b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Transactions/Update.php index a757f0079e..7315daa8d4 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Transactions/Update.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Transactions/Update.php @@ -343,13 +343,6 @@ class Update extends Action $usage->addMetric($this->getDatabasesOperationWriteMetric(), $totalOperations); - foreach ($databaseOperations as $sequence => $count) { - $usage->addMetric( - str_replace('{databaseInternalId}', $sequence, $this->getDatabasesIdOperationWriteMetric()), - $count - ); - } - $dbCache = []; foreach ($operations as $operation) { $databaseInternalId = $operation['databaseInternalId']; From 5f27e3e0464471846f199fcde0489a07257e4c8b Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Sun, 5 Jul 2026 06:36:25 +0000 Subject: [PATCH 05/13] refactor(storage): drop dead stats read on buckets total-size The stats collection on dbForLogs is no longer written to in CE (worker teardown in d2bbbbb7a1), so the per-bucket totalSize lookup only ever returned 0 via the silent catch. Drop the try/catch and the injected getLogsDB/authorization callables. Bucket totalSize stays exposed as 0 to preserve the response contract. --- .../Modules/Storage/Http/Buckets/Get.php | 34 +------------ .../Modules/Storage/Http/Buckets/XList.php | 51 +------------------ 2 files changed, 3 insertions(+), 82 deletions(-) diff --git a/src/Appwrite/Platform/Modules/Storage/Http/Buckets/Get.php b/src/Appwrite/Platform/Modules/Storage/Http/Buckets/Get.php index 0418ef8b45..41bb418819 100644 --- a/src/Appwrite/Platform/Modules/Storage/Http/Buckets/Get.php +++ b/src/Appwrite/Platform/Modules/Storage/Http/Buckets/Get.php @@ -8,9 +8,6 @@ use Appwrite\SDK\Method; use Appwrite\SDK\Response as SDKResponse; use Appwrite\Utopia\Response; use Utopia\Database\Database; -use Utopia\Database\Document; -use Utopia\Database\Query; -use Utopia\Database\Validator\Authorization; use Utopia\Database\Validator\UID; use Utopia\Platform\Action; use Utopia\Platform\Scope\HTTP; @@ -50,9 +47,6 @@ class Get extends Action ->param('bucketId', '', new UID(), 'Bucket unique ID.') ->inject('response') ->inject('dbForProject') - ->inject('project') - ->inject('getLogsDB') - ->inject('authorization') ->callback($this->action(...)); } @@ -60,9 +54,6 @@ class Get extends Action string $bucketId, Response $response, Database $dbForProject, - Document $project, - callable $getLogsDB, - Authorization $authorization, ): void { $bucket = $dbForProject->getDocument('buckets', $bucketId); @@ -70,30 +61,7 @@ class Get extends Action throw new Exception(Exception::STORAGE_BUCKET_NOT_FOUND); } - $metric = str_replace( - '{bucketInternalId}', - $bucket->getSequence(), - METRIC_BUCKET_ID_FILES_STORAGE - ); - - $statsDocId = md5('_inf_' . $metric); - - $totalSize = 0; - - try { - $dbForLogs = $getLogsDB($project); - $storageStats = $authorization->skip(fn () => $dbForLogs->getDocument( - 'stats', - $statsDocId, - [Query::select(['value'])] - )); - - $totalSize = $storageStats->getAttribute('value', 0); - } catch (\Throwable) { - // Stats may not be available, default to 0 - } - - $bucket->setAttribute('totalSize', $totalSize); + $bucket->setAttribute('totalSize', 0); $response->dynamic($bucket, Response::MODEL_BUCKET); } diff --git a/src/Appwrite/Platform/Modules/Storage/Http/Buckets/XList.php b/src/Appwrite/Platform/Modules/Storage/Http/Buckets/XList.php index d8e5cd5ad2..3db0af20ac 100644 --- a/src/Appwrite/Platform/Modules/Storage/Http/Buckets/XList.php +++ b/src/Appwrite/Platform/Modules/Storage/Http/Buckets/XList.php @@ -13,7 +13,6 @@ use Utopia\Database\Document; use Utopia\Database\Exception\Order as OrderException; use Utopia\Database\Exception\Query as QueryException; use Utopia\Database\Query; -use Utopia\Database\Validator\Authorization; use Utopia\Database\Validator\Query\Cursor; use Utopia\Platform\Action; use Utopia\Platform\Scope\HTTP; @@ -56,9 +55,6 @@ class XList extends Action ->param('total', true, new Boolean(true), 'When set to false, the total count returned will be 0 and will not be calculated.', true) ->inject('response') ->inject('dbForProject') - ->inject('project') - ->inject('getLogsDB') - ->inject('authorization') ->callback($this->action(...)); } @@ -68,9 +64,6 @@ class XList extends Action bool $includeTotal, Response $response, Database $dbForProject, - Document $project, - callable $getLogsDB, - Authorization $authorization ) { try { $queries = Query::parseQueries($queries); @@ -111,48 +104,8 @@ class XList extends Action throw new Exception(Exception::GENERAL_QUERY_INVALID, $e->getMessage()); } - if (!empty($buckets)) { - $bucketByStatsId = []; - - foreach ($buckets as $bucket) { - $metric = str_replace( - '{bucketInternalId}', - $bucket->getSequence(), - METRIC_BUCKET_ID_FILES_STORAGE - ); - - $statId = md5('_inf_' . $metric); - - $bucketByStatsId[$statId] = $bucket; - - // set a default - $bucket->setAttribute('totalSize', 0); - } - - try { - $dbForLogs = $getLogsDB($project); - - /* @var array $stats */ - $stats = $authorization->skip(function () use ($dbForLogs, $bucketByStatsId) { - $statsIds = array_keys($bucketByStatsId); - - return $dbForLogs->find('stats', [ - Query::equal('$id', $statsIds), - Query::select(['value']), - ]); - }); - - foreach ($stats as $stat) { - if (!isset($bucketByStatsId[$stat->getId()])) { - continue; - } - - $bucket = $bucketByStatsId[$stat->getId()]; - $bucket->setAttribute('totalSize', $stat->getAttribute('value', 0)); - } - } catch (\Throwable) { - // Stats may not be available, default to 0 - } + foreach ($buckets as $bucket) { + $bucket->setAttribute('totalSize', 0); } $response->dynamic(new Document([ From 14e6d07d61ea35dc193865265549a787a6b296d4 Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Sun, 5 Jul 2026 06:44:59 +0000 Subject: [PATCH 06/13] refactor(databases): attribute transaction writes per source database Cross-database transactions previously fanned out one per-database metric name; the aggregate-only emit that followed the constant drop lost that attribution. Iterate the databaseOperations map so each source database gets its own resource=database + resourceInternalId tagged emit, preserving per-database attribution on cloud writers without reintroducing the {databaseInternalId} template. --- .../Databases/Http/Databases/Transactions/Update.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Transactions/Update.php b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Transactions/Update.php index 7315daa8d4..93ca6d9df5 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Transactions/Update.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Transactions/Update.php @@ -341,7 +341,12 @@ class Update extends Action throw new Exception(Exception::GENERAL_QUERY_INVALID, $e->getMessage()); } - $usage->addMetric($this->getDatabasesOperationWriteMetric(), $totalOperations); + foreach ($databaseOperations as $databaseInternalId => $count) { + $usage + ->setResource('database') + ->setResourceInternalId((string) $databaseInternalId) + ->addMetric($this->getDatabasesOperationWriteMetric(), $count); + } $dbCache = []; foreach ($operations as $operation) { From 856e090aee57d559fda974eb1e846fccd942f980 Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Tue, 21 Jul 2026 07:48:25 +0545 Subject: [PATCH 07/13] refactor(usage): drop response models orphaned by removed /usage endpoints (CLO-4588) The per-resource /usage endpoints (databases/tablesDB/storage list + bucket usage) are dropped in the cloud API in favour of the ClickHouse usage endpoints. CE never exposed these routes but defined their response models; remove the now-unreferenced UsageBuckets/UsageStorage/UsageDatabase(s)/ UsageCollection/UsageTable/UsageSites models, their model registrations, and Response constants. Kept singular UsageFunction/UsageSite/UsageProject/ UsageUsers/UsagePresence (still served). --- app/init/models.php | 14 -- src/Appwrite/Utopia/Response.php | 7 - .../Utopia/Response/Model/UsageBuckets.php | 80 ------ .../Utopia/Response/Model/UsageCollection.php | 54 ---- .../Utopia/Response/Model/UsageDatabase.php | 132 ---------- .../Utopia/Response/Model/UsageDatabases.php | 145 ----------- .../Utopia/Response/Model/UsageSites.php | 236 ------------------ .../Utopia/Response/Model/UsageStorage.php | 80 ------ .../Utopia/Response/Model/UsageTable.php | 54 ---- 9 files changed, 802 deletions(-) delete mode 100644 src/Appwrite/Utopia/Response/Model/UsageBuckets.php delete mode 100644 src/Appwrite/Utopia/Response/Model/UsageCollection.php delete mode 100644 src/Appwrite/Utopia/Response/Model/UsageDatabase.php delete mode 100644 src/Appwrite/Utopia/Response/Model/UsageDatabases.php delete mode 100644 src/Appwrite/Utopia/Response/Model/UsageSites.php delete mode 100644 src/Appwrite/Utopia/Response/Model/UsageStorage.php delete mode 100644 src/Appwrite/Utopia/Response/Model/UsageTable.php diff --git a/app/init/models.php b/app/init/models.php index 05136566d2..55101a27b8 100644 --- a/app/init/models.php +++ b/app/init/models.php @@ -211,10 +211,6 @@ use Appwrite\Utopia\Response\Model\TemplateVariable; use Appwrite\Utopia\Response\Model\Token; use Appwrite\Utopia\Response\Model\Topic; use Appwrite\Utopia\Response\Model\Transaction; -use Appwrite\Utopia\Response\Model\UsageBuckets; -use Appwrite\Utopia\Response\Model\UsageCollection; -use Appwrite\Utopia\Response\Model\UsageDatabase; -use Appwrite\Utopia\Response\Model\UsageDatabases; use Appwrite\Utopia\Response\Model\UsageDocumentsDB; use Appwrite\Utopia\Response\Model\UsageDocumentsDBs; use Appwrite\Utopia\Response\Model\UsageFunction; @@ -222,9 +218,6 @@ use Appwrite\Utopia\Response\Model\UsageFunctions; use Appwrite\Utopia\Response\Model\UsagePresence; use Appwrite\Utopia\Response\Model\UsageProject; use Appwrite\Utopia\Response\Model\UsageSite; -use Appwrite\Utopia\Response\Model\UsageSites; -use Appwrite\Utopia\Response\Model\UsageStorage; -use Appwrite\Utopia\Response\Model\UsageTable; use Appwrite\Utopia\Response\Model\UsageUsers; use Appwrite\Utopia\Response\Model\UsageVectorsDB; use Appwrite\Utopia\Response\Model\UsageVectorsDBs; @@ -497,17 +490,10 @@ Response::setModel(new HealthTime()); Response::setModel(new HealthVersion()); Response::setModel(new Metric()); Response::setModel(new MetricBreakdown()); -Response::setModel(new UsageDatabases()); -Response::setModel(new UsageDatabase()); -Response::setModel(new UsageTable()); -Response::setModel(new UsageCollection()); Response::setModel(new UsageUsers()); Response::setModel(new UsagePresence()); -Response::setModel(new UsageStorage()); -Response::setModel(new UsageBuckets()); Response::setModel(new UsageFunctions()); Response::setModel(new UsageFunction()); -Response::setModel(new UsageSites()); Response::setModel(new UsageSite()); Response::setModel(new UsageProject()); Response::setModel(new Headers()); diff --git a/src/Appwrite/Utopia/Response.php b/src/Appwrite/Utopia/Response.php index 19b5f4d120..156ebe532c 100644 --- a/src/Appwrite/Utopia/Response.php +++ b/src/Appwrite/Utopia/Response.php @@ -30,21 +30,14 @@ class Response extends SwooleResponse public const MODEL_METRIC_BREAKDOWN = 'metricBreakdown'; public const MODEL_ERROR_DEV = 'errorDev'; public const MODEL_BASE_LIST = 'baseList'; - public const MODEL_USAGE_DATABASES = 'usageDatabases'; - public const MODEL_USAGE_DATABASE = 'usageDatabase'; public const MODEL_USAGE_DOCUMENTSDBS = 'usageDocumentsDBs'; public const MODEL_USAGE_DOCUMENTSDB = 'usageDocumentsDB'; public const MODEL_USAGE_VECTORSDBS = 'usageVectorsDBs'; public const MODEL_USAGE_VECTORSDB = 'usageVectorsDB'; - public const MODEL_USAGE_TABLE = 'usageTable'; - public const MODEL_USAGE_COLLECTION = 'usageCollection'; public const MODEL_USAGE_USERS = 'usageUsers'; public const MODEL_USAGE_PRESENCE = 'usagePresence'; - public const MODEL_USAGE_BUCKETS = 'usageBuckets'; - public const MODEL_USAGE_STORAGE = 'usageStorage'; public const MODEL_USAGE_FUNCTIONS = 'usageFunctions'; public const MODEL_USAGE_FUNCTION = 'usageFunction'; - public const MODEL_USAGE_SITES = 'usageSites'; public const MODEL_USAGE_SITE = 'usageSite'; public const MODEL_USAGE_PROJECT = 'usageProject'; diff --git a/src/Appwrite/Utopia/Response/Model/UsageBuckets.php b/src/Appwrite/Utopia/Response/Model/UsageBuckets.php deleted file mode 100644 index ee624a29ad..0000000000 --- a/src/Appwrite/Utopia/Response/Model/UsageBuckets.php +++ /dev/null @@ -1,80 +0,0 @@ -addRule('range', [ - 'type' => self::TYPE_STRING, - 'description' => 'Time range of the usage stats.', - 'default' => '', - 'example' => '30d', - ]) - ->addRule('filesTotal', [ - 'type' => self::TYPE_INTEGER, - 'description' => 'Total aggregated number of bucket files.', - 'default' => 0, - 'example' => 0, - ]) - ->addRule('filesStorageTotal', [ - 'type' => self::TYPE_INTEGER, - 'description' => 'Total aggregated number of bucket files storage (in bytes).', - 'default' => 0, - 'example' => 0, - ]) - ->addRule('files', [ - 'type' => Response::MODEL_METRIC, - 'description' => 'Aggregated number of bucket files per period.', - 'default' => [], - 'example' => [], - 'array' => true - ]) - ->addRule('storage', [ - 'type' => Response::MODEL_METRIC, - 'description' => 'Aggregated number of bucket storage files (in bytes) per period.', - 'default' => [], - 'example' => [], - 'array' => true - ]) - ->addRule('imageTransformations', [ - 'type' => Response::MODEL_METRIC, - 'description' => 'Aggregated number of files transformations per period.', - 'default' => [], - 'example' => [], - 'array' => true - ]) - ->addRule('imageTransformationsTotal', [ - 'type' => self::TYPE_INTEGER, - 'description' => 'Total aggregated number of files transformations.', - 'default' => 0, - 'example' => 0, - ]) - ; - } - - /** - * Get Name - * - * @return string - */ - public function getName(): string - { - return 'UsageBuckets'; - } - - /** - * Get Type - * - * @return string - */ - public function getType(): string - { - return Response::MODEL_USAGE_BUCKETS; - } -} diff --git a/src/Appwrite/Utopia/Response/Model/UsageCollection.php b/src/Appwrite/Utopia/Response/Model/UsageCollection.php deleted file mode 100644 index b2336d65ba..0000000000 --- a/src/Appwrite/Utopia/Response/Model/UsageCollection.php +++ /dev/null @@ -1,54 +0,0 @@ -addRule('range', [ - 'type' => self::TYPE_STRING, - 'description' => 'Time range of the usage stats.', - 'default' => '', - 'example' => '30d', - ]) - ->addRule('documentsTotal', [ - 'type' => self::TYPE_INTEGER, - 'description' => 'Total aggregated number of of documents.', - 'default' => 0, - 'example' => 0, - ]) - ->addRule('documents', [ - 'type' => Response::MODEL_METRIC, - 'description' => 'Aggregated number of documents per period.', - 'default' => [], - 'example' => [], - 'array' => true - ]) - ; - } - - /** - * Get Name - * - * @return string - */ - public function getName(): string - { - return 'UsageCollection'; - } - - /** - * Get Type - * - * @return string - */ - public function getType(): string - { - return Response::MODEL_USAGE_COLLECTION; - } -} diff --git a/src/Appwrite/Utopia/Response/Model/UsageDatabase.php b/src/Appwrite/Utopia/Response/Model/UsageDatabase.php deleted file mode 100644 index 990a2b3ee9..0000000000 --- a/src/Appwrite/Utopia/Response/Model/UsageDatabase.php +++ /dev/null @@ -1,132 +0,0 @@ -addRule('range', [ - 'type' => self::TYPE_STRING, - 'description' => 'Time range of the usage stats.', - 'default' => '', - 'example' => '30d', - ]) - ->addRule('collectionsTotal', [ - 'type' => self::TYPE_INTEGER, - 'description' => 'Total aggregated number of collections.', - 'default' => 0, - 'example' => 0, - ]) - ->addRule('tablesTotal', [ - 'type' => self::TYPE_INTEGER, - 'description' => 'Total aggregated number of tables.', - 'default' => 0, - 'example' => 0, - ]) - ->addRule('documentsTotal', [ - 'type' => self::TYPE_INTEGER, - 'description' => 'Total aggregated number of documents.', - 'default' => 0, - 'example' => 0, - ]) - ->addRule('rowsTotal', [ - 'type' => self::TYPE_INTEGER, - 'description' => 'Total aggregated number of rows.', - 'default' => 0, - 'example' => 0, - ]) - ->addRule('storageTotal', [ - 'type' => self::TYPE_INTEGER, - 'description' => 'Total aggregated number of total storage used in bytes.', - 'default' => 0, - 'example' => 0, - ]) - ->addRule('databaseReadsTotal', [ - 'type' => self::TYPE_INTEGER, - 'description' => 'Total number of databases reads.', - 'default' => 0, - 'example' => 0, - ]) - ->addRule('databaseWritesTotal', [ - 'type' => self::TYPE_INTEGER, - 'description' => 'Total number of databases writes.', - 'default' => 0, - 'example' => 0, - ]) - ->addRule('collections', [ - 'type' => Response::MODEL_METRIC, - 'description' => 'Aggregated number of collections per period.', - 'default' => [], - 'example' => [], - 'array' => true - ]) - ->addRule('tables', [ - 'type' => Response::MODEL_METRIC, - 'description' => 'Aggregated number of tables per period.', - 'default' => [], - 'example' => [], - 'array' => true - ]) - ->addRule('documents', [ - 'type' => Response::MODEL_METRIC, - 'description' => 'Aggregated number of documents per period.', - 'default' => [], - 'example' => [], - 'array' => true - ]) - ->addRule('rows', [ - 'type' => Response::MODEL_METRIC, - 'description' => 'Aggregated number of rows per period.', - 'default' => [], - 'example' => [], - 'array' => true - ]) - ->addRule('storage', [ - 'type' => Response::MODEL_METRIC, - 'description' => 'Aggregated storage used in bytes per period.', - 'default' => [], - 'example' => [], - 'array' => true - ]) - ->addRule('databaseReads', [ - 'type' => Response::MODEL_METRIC, - 'description' => 'An array of aggregated number of database reads.', - 'default' => [], - 'example' => [], - 'array' => true - ]) - ->addRule('databaseWrites', [ - 'type' => Response::MODEL_METRIC, - 'description' => 'An array of aggregated number of database writes.', - 'default' => [], - 'example' => [], - 'array' => true - ]) - ; - } - - /** - * Get Name - * - * @return string - */ - public function getName(): string - { - return 'UsageDatabase'; - } - - /** - * Get Type - * - * @return string - */ - public function getType(): string - { - return Response::MODEL_USAGE_DATABASE; - } -} diff --git a/src/Appwrite/Utopia/Response/Model/UsageDatabases.php b/src/Appwrite/Utopia/Response/Model/UsageDatabases.php deleted file mode 100644 index 67568c858c..0000000000 --- a/src/Appwrite/Utopia/Response/Model/UsageDatabases.php +++ /dev/null @@ -1,145 +0,0 @@ -addRule('range', [ - 'type' => self::TYPE_STRING, - 'description' => 'Time range of the usage stats.', - 'default' => '', - 'example' => '30d', - ]) - ->addRule('databasesTotal', [ - 'type' => self::TYPE_INTEGER, - 'description' => 'Total aggregated number of databases.', - 'default' => 0, - 'example' => 0, - ]) - ->addRule('collectionsTotal', [ - 'type' => self::TYPE_INTEGER, - 'description' => 'Total aggregated number of collections.', - 'default' => 0, - 'example' => 0, - ]) - ->addRule('tablesTotal', [ - 'type' => self::TYPE_INTEGER, - 'description' => 'Total aggregated number of tables.', - 'default' => 0, - 'example' => 0, - ]) - ->addRule('documentsTotal', [ - 'type' => self::TYPE_INTEGER, - 'description' => 'Total aggregated number of documents.', - 'default' => 0, - 'example' => 0, - ]) - ->addRule('rowsTotal', [ - 'type' => self::TYPE_INTEGER, - 'description' => 'Total aggregated number of rows.', - 'default' => 0, - 'example' => 0, - ]) - ->addRule('storageTotal', [ - 'type' => self::TYPE_INTEGER, - 'description' => 'Total aggregated number of total databases storage in bytes.', - 'default' => 0, - 'example' => 0, - ]) - ->addRule('databasesReadsTotal', [ - 'type' => self::TYPE_INTEGER, - 'description' => 'Total number of databases reads.', - 'default' => 0, - 'example' => 0, - ]) - ->addRule('databasesWritesTotal', [ - 'type' => self::TYPE_INTEGER, - 'description' => 'Total number of databases writes.', - 'default' => 0, - 'example' => 0, - ]) - ->addRule('databases', [ - 'type' => Response::MODEL_METRIC, - 'description' => 'Aggregated number of databases per period.', - 'default' => [], - 'example' => [], - 'array' => true - ]) - ->addRule('collections', [ - 'type' => Response::MODEL_METRIC, - 'description' => 'Aggregated number of collections per period.', - 'default' => [], - 'example' => [], - 'array' => true - ]) - ->addRule('tables', [ - 'type' => Response::MODEL_METRIC, - 'description' => 'Aggregated number of tables per period.', - 'default' => [], - 'example' => [], - 'array' => true - ]) - ->addRule('documents', [ - 'type' => Response::MODEL_METRIC, - 'description' => 'Aggregated number of documents per period.', - 'default' => [], - 'example' => [], - 'array' => true - ]) - ->addRule('rows', [ - 'type' => Response::MODEL_METRIC, - 'description' => 'Aggregated number of rows per period.', - 'default' => [], - 'example' => [], - 'array' => true - ]) - ->addRule('storage', [ - 'type' => Response::MODEL_METRIC, - 'description' => 'An array of the aggregated number of databases storage in bytes per period.', - 'default' => [], - 'example' => [], - 'array' => true - ]) - ->addRule('databasesReads', [ - 'type' => Response::MODEL_METRIC, - 'description' => 'An array of aggregated number of database reads.', - 'default' => [], - 'example' => [], - 'array' => true - ]) - ->addRule('databasesWrites', [ - 'type' => Response::MODEL_METRIC, - 'description' => 'An array of aggregated number of database writes.', - 'default' => [], - 'example' => [], - 'array' => true - ]) - ; - } - - /** - * Get Name - * - * @return string - */ - public function getName(): string - { - return 'UsageDatabases'; - } - - /** - * Get Type - * - * @return string - */ - public function getType(): string - { - return Response::MODEL_USAGE_DATABASES; - } -} diff --git a/src/Appwrite/Utopia/Response/Model/UsageSites.php b/src/Appwrite/Utopia/Response/Model/UsageSites.php deleted file mode 100644 index fea87c7718..0000000000 --- a/src/Appwrite/Utopia/Response/Model/UsageSites.php +++ /dev/null @@ -1,236 +0,0 @@ -addRule('range', [ - 'type' => self::TYPE_STRING, - 'description' => 'Time range of the usage stats.', - 'default' => '', - 'example' => '30d', - ]) - ->addRule('sitesTotal', [ - 'type' => self::TYPE_INTEGER, - 'description' => 'Total aggregated number of sites.', - 'default' => 0, - 'example' => 0, - ]) - ->addRule('sites', [ - 'type' => Response::MODEL_METRIC, - 'description' => 'Aggregated number of sites per period.', - 'default' => [], - 'example' => [], - 'array' => true - ]) - ->addRule('deploymentsTotal', [ - 'type' => self::TYPE_INTEGER, - 'description' => 'Total aggregated number of sites deployments.', - 'default' => 0, - 'example' => 0, - ]) - ->addRule('deploymentsStorageTotal', [ - 'type' => self::TYPE_INTEGER, - 'description' => 'Total aggregated sum of sites deployment storage.', - 'default' => 0, - 'example' => 0, - ]) - ->addRule('buildsTotal', [ - 'type' => self::TYPE_INTEGER, - 'description' => 'Total aggregated number of sites build.', - 'default' => 0, - 'example' => 0, - ]) - ->addRule('buildsStorageTotal', [ - 'type' => self::TYPE_INTEGER, - 'description' => 'total aggregated sum of sites build storage.', - 'default' => 0, - 'example' => 0, - ]) - ->addRule('buildsTimeTotal', [ - 'type' => self::TYPE_INTEGER, - 'description' => 'Total aggregated sum of sites build compute time.', - 'default' => 0, - 'example' => 0, - ]) - ->addRule('buildsMbSecondsTotal', [ - 'type' => self::TYPE_INTEGER, - 'description' => 'Total aggregated sum of sites build mbSeconds.', - 'default' => 0, - 'example' => 0, - ]) - ->addRule('executionsTotal', [ - 'type' => self::TYPE_INTEGER, - 'description' => 'Total aggregated number of sites execution.', - 'default' => 0, - 'example' => 0, - ]) - ->addRule('executionsTimeTotal', [ - 'type' => self::TYPE_INTEGER, - 'description' => 'Total aggregated sum of sites execution compute time.', - 'default' => 0, - 'example' => 0, - ]) - ->addRule('executionsMbSecondsTotal', [ - 'type' => self::TYPE_INTEGER, - 'description' => 'Total aggregated sum of sites execution mbSeconds.', - 'default' => 0, - 'example' => 0, - ]) - ->addRule('requestsTotal', [ - 'type' => self::TYPE_INTEGER, - 'description' => 'Total aggregated number of requests.', - 'default' => 0, - 'example' => 0, - ]) - ->addRule('requests', [ - 'type' => Response::MODEL_METRIC, - 'description' => 'Aggregated number of requests per period.', - 'default' => [], - 'example' => [], - 'array' => true - ]) - ->addRule('inboundTotal', [ - 'type' => self::TYPE_INTEGER, - 'description' => 'Total aggregated inbound bandwidth.', - 'default' => 0, - 'example' => 0, - ]) - ->addRule('inbound', [ - 'type' => Response::MODEL_METRIC, - 'description' => 'Aggregated number of inbound bandwidth per period.', - 'default' => [], - 'example' => [], - 'array' => true - ]) - ->addRule('outboundTotal', [ - 'type' => self::TYPE_INTEGER, - 'description' => 'Total aggregated outbound bandwidth.', - 'default' => 0, - 'example' => 0, - ]) - ->addRule('outbound', [ - 'type' => Response::MODEL_METRIC, - 'description' => 'Aggregated number of outbound bandwidth per period.', - 'default' => [], - 'example' => [], - 'array' => true - ]) - ->addRule('deployments', [ - 'type' => Response::MODEL_METRIC, - 'description' => 'Aggregated number of sites deployment per period.', - 'default' => [], - 'example' => [], - 'array' => true - ]) - ->addRule('deploymentsStorage', [ - 'type' => Response::MODEL_METRIC, - 'description' => 'Aggregated number of sites deployment storage per period.', - 'default' => [], - 'example' => [], - 'array' => true - ]) - ->addRule('buildsSuccessTotal', [ - 'type' => self::TYPE_INTEGER, - 'description' => 'Total aggregated number of successful site builds.', - 'default' => 0, - 'example' => 0, - ]) - ->addRule('buildsFailedTotal', [ - 'type' => self::TYPE_INTEGER, - 'description' => 'Total aggregated number of failed site builds.', - 'default' => 0, - 'example' => 0, - ]) - ->addRule('builds', [ - 'type' => Response::MODEL_METRIC, - 'description' => 'Aggregated number of sites build per period.', - 'default' => [], - 'example' => [], - 'array' => true - ]) - ->addRule('buildsStorage', [ - 'type' => Response::MODEL_METRIC, - 'description' => 'Aggregated sum of sites build storage per period.', - 'default' => [], - 'example' => [], - 'array' => true - ]) - ->addRule('buildsTime', [ - 'type' => Response::MODEL_METRIC, - 'description' => 'Aggregated sum of sites build compute time per period.', - 'default' => [], - 'example' => [], - 'array' => true - ]) - ->addRule('buildsMbSeconds', [ - 'type' => Response::MODEL_METRIC, - 'description' => 'Aggregated sum of sites build mbSeconds per period.', - 'default' => [], - 'example' => [], - 'array' => true - ]) - ->addRule('executions', [ - 'type' => Response::MODEL_METRIC, - 'description' => 'Aggregated number of sites execution per period.', - 'default' => [], - 'example' => [], - 'array' => true - ]) - ->addRule('executionsTime', [ - 'type' => Response::MODEL_METRIC, - 'description' => 'Aggregated number of sites execution compute time per period.', - 'default' => [], - 'example' => [], - 'array' => true - ]) - ->addRule('executionsMbSeconds', [ - 'type' => Response::MODEL_METRIC, - 'description' => 'Aggregated number of sites mbSeconds per period.', - 'default' => [], - 'example' => [], - 'array' => true - ]) - ->addRule('buildsSuccess', [ - 'type' => Response::MODEL_METRIC, - 'description' => 'Aggregated number of successful site builds per period.', - 'default' => [], - 'example' => [], - 'array' => true - ]) - ->addRule('buildsFailed', [ - 'type' => Response::MODEL_METRIC, - 'description' => 'Aggregated number of failed site builds per period.', - 'default' => [], - 'example' => [], - 'array' => true - ]) - ; - } - - /** - * Get Name - * - * @return string - */ - public function getName(): string - { - return 'UsageSites'; - } - - /** - * Get Type - * - * @return string - */ - public function getType(): string - { - return Response::MODEL_USAGE_SITES; - } -} diff --git a/src/Appwrite/Utopia/Response/Model/UsageStorage.php b/src/Appwrite/Utopia/Response/Model/UsageStorage.php deleted file mode 100644 index 89b771ccd7..0000000000 --- a/src/Appwrite/Utopia/Response/Model/UsageStorage.php +++ /dev/null @@ -1,80 +0,0 @@ -addRule('range', [ - 'type' => self::TYPE_STRING, - 'description' => 'Time range of the usage stats.', - 'default' => '', - 'example' => '30d', - ]) - ->addRule('bucketsTotal', [ - 'type' => self::TYPE_INTEGER, - 'description' => 'Total aggregated number of buckets', - 'default' => 0, - 'example' => 0, - ]) - ->addRule('filesTotal', [ - 'type' => self::TYPE_INTEGER, - 'description' => 'Total aggregated number of files.', - 'default' => 0, - 'example' => 0, - ]) - ->addRule('filesStorageTotal', [ - 'type' => self::TYPE_INTEGER, - 'description' => 'Total aggregated number of files storage (in bytes).', - 'default' => 0, - 'example' => 0, - ]) - ->addRule('buckets', [ - 'type' => Response::MODEL_METRIC, - 'description' => 'Aggregated number of buckets per period.', - 'default' => [], - 'example' => [], - 'array' => true - ]) - ->addRule('files', [ - 'type' => Response::MODEL_METRIC, - 'description' => 'Aggregated number of files per period.', - 'default' => [], - 'example' => [], - 'array' => true - ]) - ->addRule('storage', [ - 'type' => Response::MODEL_METRIC, - 'description' => 'Aggregated number of files storage (in bytes) per period .', - 'default' => [], - 'example' => [], - 'array' => true - ]) - ; - } - - /** - * Get Name - * - * @return string - */ - public function getName(): string - { - return 'StorageUsage'; - } - - /** - * Get Type - * - * @return string - */ - public function getType(): string - { - return Response::MODEL_USAGE_STORAGE; - } -} diff --git a/src/Appwrite/Utopia/Response/Model/UsageTable.php b/src/Appwrite/Utopia/Response/Model/UsageTable.php deleted file mode 100644 index 553a4b5001..0000000000 --- a/src/Appwrite/Utopia/Response/Model/UsageTable.php +++ /dev/null @@ -1,54 +0,0 @@ -addRule('range', [ - 'type' => self::TYPE_STRING, - 'description' => 'Time range of the usage stats.', - 'default' => '', - 'example' => '30d', - ]) - ->addRule('rowsTotal', [ - 'type' => self::TYPE_INTEGER, - 'description' => 'Total aggregated number of of rows.', - 'default' => 0, - 'example' => 0, - ]) - ->addRule('rows', [ - 'type' => Response::MODEL_METRIC, - 'description' => 'Aggregated number of rows per period.', - 'default' => [], - 'example' => [], - 'array' => true - ]) - ; - } - - /** - * Get Name - * - * @return string - */ - public function getName(): string - { - return 'UsageTable'; - } - - /** - * Get Type - * - * @return string - */ - public function getType(): string - { - return Response::MODEL_USAGE_TABLE; - } -} From 999a8e5123bef2da2d683ca6c21340d71ac840a0 Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Tue, 21 Jul 2026 09:35:16 +0545 Subject: [PATCH 08/13] refactor(usage): drop per-resource breakdown fields from UsageProject model (CLO-4588) Companion to the cloud change dropping the six per-resource breakdown arrays from project usage. Removes executions/buckets/databasesStorage/ functionsStorage/executionsMbSeconds/buildsMbSeconds breakdown rules; keeps authPhoneCountryBreakdown. --- .../Utopia/Response/Model/UsageProject.php | 42 ------------------- 1 file changed, 42 deletions(-) diff --git a/src/Appwrite/Utopia/Response/Model/UsageProject.php b/src/Appwrite/Utopia/Response/Model/UsageProject.php index e00c4bc1dc..2de2f70d2a 100644 --- a/src/Appwrite/Utopia/Response/Model/UsageProject.php +++ b/src/Appwrite/Utopia/Response/Model/UsageProject.php @@ -158,48 +158,6 @@ class UsageProject extends Model 'example' => [], 'array' => true ]) - ->addRule('executionsBreakdown', [ - 'type' => Response::MODEL_METRIC_BREAKDOWN, - 'description' => 'Aggregated breakdown in totals of executions by functions.', - 'default' => [], - 'example' => [], - 'array' => true - ]) - ->addRule('bucketsBreakdown', [ - 'type' => Response::MODEL_METRIC_BREAKDOWN, - 'description' => 'Aggregated breakdown in totals of usage by buckets.', - 'default' => [], - 'example' => [], - 'array' => true - ]) - ->addRule('databasesStorageBreakdown', [ - 'type' => Response::MODEL_METRIC_BREAKDOWN, - 'description' => 'An array of the aggregated breakdown of storage usage by databases.', - 'default' => [], - 'example' => [], - 'array' => true - ]) - ->addRule('executionsMbSecondsBreakdown', [ - 'type' => Response::MODEL_METRIC_BREAKDOWN, - 'description' => 'Aggregated breakdown in totals of execution mbSeconds by functions.', - 'default' => [], - 'example' => [], - 'array' => true - ]) - ->addRule('buildsMbSecondsBreakdown', [ - 'type' => Response::MODEL_METRIC_BREAKDOWN, - 'description' => 'Aggregated breakdown in totals of build mbSeconds by functions.', - 'default' => [], - 'example' => [], - 'array' => true - ]) - ->addRule('functionsStorageBreakdown', [ - 'type' => Response::MODEL_METRIC_BREAKDOWN, - 'description' => 'Aggregated breakdown in totals of functions storage size (in bytes).', - 'default' => [], - 'example' => [], - 'array' => true - ]) ->addRule('authPhoneTotal', [ 'type' => self::TYPE_INTEGER, 'description' => 'Total aggregated number of phone auth.', From a241216e83642d9c2199fbe3dc87260acc9448ca Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Tue, 21 Jul 2026 10:23:48 +0545 Subject: [PATCH 09/13] refactor(usage): stop emitting resource-count metrics already sourced from gauges (CLO-4588) The per-project count/storage metrics (teams/users/databases/collections/ documents/buckets/files/filesStorage/functions/sites/deployments/ deploymentsStorage) are produced by StatsResources as ClickHouse gauges keyed by resourceId, and StatsUsage already skips them from the event write path. Emitting them again from the database usage listener + CSV migration only fed the deprecated MySQL path (and dead addReduce cleanup). Drop those emits; keep sessions, the nested per-collection documents metric, and the resourceType-level deployment metrics (event-sourced, not gauge-owned). --- app/init/resources/request.php | 66 -------------------- src/Appwrite/Platform/Workers/Migrations.php | 14 +---- 2 files changed, 1 insertion(+), 79 deletions(-) diff --git a/app/init/resources/request.php b/app/init/resources/request.php index b31977033c..109329458d 100644 --- a/app/init/resources/request.php +++ b/app/init/resources/request.php @@ -774,90 +774,24 @@ return function (Container $context): void { } switch (true) { - case $document->getCollection() === 'teams': - $usage->addMetric(METRIC_TEAMS, $value); // per project - break; - case $document->getCollection() === 'users': - $usage->addMetric(METRIC_USERS, $value); // per project - if ($event === Database::EVENT_DOCUMENT_DELETE) { - $usage->addReduce($document); - } - break; case $document->getCollection() === 'sessions': // sessions $usage->addMetric(METRIC_SESSIONS, $value); // per project break; - case $document->getCollection() === 'databases': // databases - $metric = $getDatabaseTypePrefixedMetric($databaseType, METRIC_DATABASES); - $usage->addMetric($metric, $value); // per project - - if ($event === Database::EVENT_DOCUMENT_DELETE) { - $usage->addReduce($document); - } - break; - case str_starts_with($document->getCollection(), 'database_') && ! str_contains($document->getCollection(), 'collection'): // collections - $parts = explode('_', $document->getCollection()); - $databaseInternalId = $parts[1] ?? 0; - $collectionMetric = $getDatabaseTypePrefixedMetric($databaseType, METRIC_COLLECTIONS); - $usage - ->setResource('database') - ->setResourceInternalId((string) $databaseInternalId) - ->addMetric($collectionMetric, $value); // per project - - if ($event === Database::EVENT_DOCUMENT_DELETE) { - $usage->addReduce($document); - } - break; case str_starts_with($document->getCollection(), 'database_') && str_contains($document->getCollection(), '_collection_'): // documents $parts = explode('_', $document->getCollection()); $databaseInternalId = $parts[1] ?? 0; $collectionInternalId = $parts[3] ?? 0; - $documentsMetric = $getDatabaseTypePrefixedMetric($databaseType, METRIC_DOCUMENTS); $databaseIdCollectionIdDocumentsMetric = $getDatabaseTypePrefixedMetric($databaseType, METRIC_DATABASE_ID_COLLECTION_ID_DOCUMENTS); $usage ->setResource('database') ->setResourceInternalId((string) $databaseInternalId) - ->addMetric($documentsMetric, $value) // per project ->addMetric(str_replace(['{databaseInternalId}', '{collectionInternalId}'], [$databaseInternalId, $collectionInternalId], $databaseIdCollectionIdDocumentsMetric), $value); // per collection break; - case $document->getCollection() === 'buckets': // buckets - $usage->addMetric(METRIC_BUCKETS, $value); // per project - if ($event === Database::EVENT_DOCUMENT_DELETE) { - $usage - ->addReduce($document); - } - break; - case str_starts_with($document->getCollection(), 'bucket_'): // files - $parts = explode('_', $document->getCollection()); - $bucketInternalId = $parts[1]; - $usage - ->setResource('bucket') - ->setResourceInternalId((string) $bucketInternalId) - ->addMetric(METRIC_FILES, $value) // per project - ->addMetric(METRIC_FILES_STORAGE, $document->getAttribute('sizeOriginal') * $value); // per project - break; - case $document->getCollection() === 'functions': - $usage->addMetric(METRIC_FUNCTIONS, $value); // per project - - if ($event === Database::EVENT_DOCUMENT_DELETE) { - $usage - ->addReduce($document); - } - break; - case $document->getCollection() === 'sites': - $usage->addMetric(METRIC_SITES, $value); // per project - - if ($event === Database::EVENT_DOCUMENT_DELETE) { - $usage - ->addReduce($document); - } - break; case $document->getCollection() === 'deployments': $resourceType = $document->getAttribute('resourceType'); $usage ->setResource(rtrim($resourceType, 's')) ->setResourceInternalId((string) $document->getAttribute('resourceInternalId')) - ->addMetric(METRIC_DEPLOYMENTS, $value) // per project - ->addMetric(METRIC_DEPLOYMENTS_STORAGE, $document->getAttribute('size') * $value) // per project ->addMetric(str_replace(['{resourceType}'], [$resourceType], METRIC_RESOURCE_TYPE_DEPLOYMENTS), $value) // per resource type ->addMetric(str_replace(['{resourceType}'], [$resourceType], METRIC_RESOURCE_TYPE_DEPLOYMENTS_STORAGE), $document->getAttribute('size') * $value); break; diff --git a/src/Appwrite/Platform/Workers/Migrations.php b/src/Appwrite/Platform/Workers/Migrations.php index 3da2952efd..e2f33e2061 100644 --- a/src/Appwrite/Platform/Workers/Migrations.php +++ b/src/Appwrite/Platform/Workers/Migrations.php @@ -1151,17 +1151,6 @@ class Migrations extends Action } switch ($resourceName) { - case ResourceDatabase::getName(): - $usage->addMetric(METRIC_DATABASES, $count); - break; - - case ResourceTable::getName(): - $usage - ->setResource('database') - ->setResourceInternalId((string) $databaseInternalId) - ->addMetric(METRIC_COLLECTIONS, $count); - break; - case ResourceRow::getName(): $usage ->setResource('database') @@ -1173,8 +1162,7 @@ class Migrations extends Action METRIC_DATABASE_ID_COLLECTION_ID_DOCUMENTS ), $count - ) - ->addMetric(METRIC_DOCUMENTS, $count); + ); break; default: From f6128b9110afa424c80b43618ed119e4217d4400 Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Tue, 21 Jul 2026 10:34:21 +0545 Subject: [PATCH 10/13] refactor(usage): drop per-collection document metric event emits (CLO-4588) Document counts and per-collection storage are produced by the StatsResources full-count as ClickHouse gauges broken down by resourceId/resourceType, so the per-event emission (METRIC_DOCUMENTS + METRIC_DATABASE_ID_COLLECTION_ID_DOCUMENTS template) from the database usage listeners is redundant. Remove the emitDocumentsUsage closure + its listeners and the documents switch case, along with the now-unused database-type prefix helper. --- app/init/resources/request.php | 78 ++-------------------------------- 1 file changed, 4 insertions(+), 74 deletions(-) diff --git a/app/init/resources/request.php b/app/init/resources/request.php index 109329458d..70ed653f06 100644 --- a/app/init/resources/request.php +++ b/app/init/resources/request.php @@ -731,31 +731,7 @@ return function (Container $context): void { $dbForProject->getCache()->purge($cacheKey); }; - /** - * Prefix metrics with database type when applicable. - * Avoids prefixing for legacy and tablesdb types to preserve historical metrics. - */ - $getDatabaseTypePrefixedMetric = function (string $databaseType, string $metric): string { - if ( - $databaseType === '' || - $databaseType === DATABASE_TYPE_LEGACY || - $databaseType === DATABASE_TYPE_TABLESDB - ) { - return $metric; - } - - return $databaseType . '.' . $metric; - }; - - // Determine database type from request path, similar to api.php - $path = $request->getURI(); - $databaseType = match (true) { - str_contains($path, '/documentsdb') => DATABASE_TYPE_DOCUMENTSDB, - str_contains($path, '/vectorsdb') => DATABASE_TYPE_VECTORSDB, - default => '', - }; - - $usageDatabaseListener = function (string $event, Document $document, UsageContext $usage) use ($getDatabaseTypePrefixedMetric, $databaseType) { + $usageDatabaseListener = function (string $event, Document $document, UsageContext $usage) { $value = 1; switch ($event) { @@ -777,16 +753,6 @@ return function (Container $context): void { case $document->getCollection() === 'sessions': // sessions $usage->addMetric(METRIC_SESSIONS, $value); // per project break; - case str_starts_with($document->getCollection(), 'database_') && str_contains($document->getCollection(), '_collection_'): // documents - $parts = explode('_', $document->getCollection()); - $databaseInternalId = $parts[1] ?? 0; - $collectionInternalId = $parts[3] ?? 0; - $databaseIdCollectionIdDocumentsMetric = $getDatabaseTypePrefixedMetric($databaseType, METRIC_DATABASE_ID_COLLECTION_ID_DOCUMENTS); - $usage - ->setResource('database') - ->setResourceInternalId((string) $databaseInternalId) - ->addMetric(str_replace(['{databaseInternalId}', '{collectionInternalId}'], [$databaseInternalId, $collectionInternalId], $databaseIdCollectionIdDocumentsMetric), $value); // per collection - break; case $document->getCollection() === 'deployments': $resourceType = $document->getAttribute('resourceType'); $usage @@ -1184,45 +1150,9 @@ return function (Container $context): void { $database->setTimeout($timeout); } - // Register database event listeners for usage stats collection - $documentsMetric = METRIC_DOCUMENTS; - $databaseIdCollectionIdDocumentsMetric = METRIC_DATABASE_ID_COLLECTION_ID_DOCUMENTS; - if ($databaseType !== DATABASE_TYPE_LEGACY && $databaseType !== DATABASE_TYPE_TABLESDB) { - $documentsMetric = $databaseType . '.' . $documentsMetric; - $databaseIdCollectionIdDocumentsMetric = $databaseType . '.' . $databaseIdCollectionIdDocumentsMetric; - } - - $emitDocumentsUsage = function (Document $document, int $value) use ($usage, $documentsMetric, $databaseIdCollectionIdDocumentsMetric): void { - if (! str_starts_with($document->getCollection(), 'database_') || ! str_contains($document->getCollection(), '_collection_')) { - return; - } - - $parts = explode('_', $document->getCollection()); - $databaseInternalId = $parts[1] ?? 0; - $collectionInternalId = $parts[3] ?? 0; - $usage - ->setResource('database') - ->setResourceInternalId((string) $databaseInternalId) - ->addMetric($documentsMetric, $value) // per project - ->addMetric(str_replace(['{databaseInternalId}', '{collectionInternalId}'], [$databaseInternalId, $collectionInternalId], $databaseIdCollectionIdDocumentsMetric), $value); // per collection - }; - - $database - ->on(Database::EVENT_DOCUMENT_CREATE, 'calculate-usage', function ($event, $document) use ($emitDocumentsUsage) { - $emitDocumentsUsage($document, 1); - }) - ->on(Database::EVENT_DOCUMENT_DELETE, 'calculate-usage', function ($event, $document) use ($emitDocumentsUsage) { - $emitDocumentsUsage($document, -1); - }) - ->on(Database::EVENT_DOCUMENTS_CREATE, 'calculate-usage', function ($event, $document) use ($emitDocumentsUsage) { - $emitDocumentsUsage($document, $document->getAttribute('modified', 0)); - }) - ->on(Database::EVENT_DOCUMENTS_DELETE, 'calculate-usage', function ($event, $document) use ($emitDocumentsUsage) { - $emitDocumentsUsage($document, -1 * $document->getAttribute('modified', 0)); - }) - ->on(Database::EVENT_DOCUMENTS_UPSERT, 'calculate-usage', function ($event, $document) use ($emitDocumentsUsage) { - $emitDocumentsUsage($document, $document->getAttribute('created', 0)); - }); + // Document counts and per-collection storage are produced by the + // StatsResources full-count as ClickHouse gauges (broken down by + // resourceId/resourceType), so no per-event usage emission here. return $database; }; From 242d863b8dcc97ed2613149523345cf00af7bfe7 Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Tue, 21 Jul 2026 10:39:20 +0545 Subject: [PATCH 11/13] refactor(usage): drop migration usage-stat emission (CLO-4588) The CSV migration recorded imported-resource counts via the per-collection METRIC_DATABASE_ID_COLLECTION_ID_DOCUMENTS template (affected aggregated resource: ResourceRow). Those counts are recomputed by the StatsResources full-count as ClickHouse gauges, so drop the whole migration usage-stat path: the processMigrationResourceStats method, its call loop, the aggregatedResources buffer, and now-unused imports. --- src/Appwrite/Platform/Workers/Migrations.php | 104 +------------------ 1 file changed, 1 insertion(+), 103 deletions(-) diff --git a/src/Appwrite/Platform/Workers/Migrations.php b/src/Appwrite/Platform/Workers/Migrations.php index e2f33e2061..3ff9c5600d 100644 --- a/src/Appwrite/Platform/Workers/Migrations.php +++ b/src/Appwrite/Platform/Workers/Migrations.php @@ -5,7 +5,6 @@ namespace Appwrite\Platform\Workers; use Ahc\Jwt\JWT; use Appwrite\Event\Message\Mail as MailMessage; use Appwrite\Event\Message\Migration; -use Appwrite\Event\Message\Usage as UsageMessage; use Appwrite\Event\Publisher\Mail as MailPublisher; use Appwrite\Event\Publisher\Usage as UsagePublisher; use Appwrite\Event\Realtime; @@ -35,8 +34,6 @@ use Utopia\Migration\Destinations\OnDuplicate; use Utopia\Migration\Exception as MigrationException; use Utopia\Migration\Resource; use Utopia\Migration\Resources\Database\Database as ResourceDatabase; -use Utopia\Migration\Resources\Database\Row as ResourceRow; -use Utopia\Migration\Resources\Database\Table as ResourceTable; use Utopia\Migration\Source; use Utopia\Migration\Sources\Appwrite as SourceAppwrite; use Utopia\Migration\Sources\CSV; @@ -497,7 +494,6 @@ class Migrations extends Action $tempAPIKey = $this->generateAPIKey($project); $transfer = $source = $destination = null; - $aggregatedResources = []; $caughtError = null; $host = System::getEnv('_APP_MIGRATION_HOST'); @@ -543,40 +539,9 @@ class Migrations extends Action $context = $this->resolveResourceContext($migration); $transfer->runWithResourceSelector( $migration->getAttribute('resources'), - function ($resources) use ($migration, $transfer, $project, $queueForRealtime, &$aggregatedResources) { + function ($resources) use ($migration, $transfer, $project, $queueForRealtime) { $migration->setAttribute('resourceData', json_encode($transfer->getCache())); $migration->setAttribute('statusCounters', json_encode($transfer->getStatusCounters())); - - if (!empty($resources)) { - /** - * @var Resource $resource - */ - $resource = $resources[0]; - $count = count($resources); - $databaseId = null; - $tableId = null; - switch ($resource->getName()) { - case ResourceTable::getName(): - /** @var ResourceTable $resource */ - $databaseId = $resource->getDatabase()->getSequence(); - break; - case ResourceRow::getName(): - /** @var ResourceRow $resource */ - $table = $resource->getTable(); - $databaseId = $table->getDatabase()->getSequence(); - $tableId = $table->getSequence(); - break; - default: - break; - } - $aggregatedResources[] = [ - 'name' => $resource->getName(), - 'count' => $count, - 'databaseId' => $databaseId, - 'tableId' => $tableId - ]; - - } $this->updateMigrationDocument($migration, $project, $queueForRealtime); }, resourceId: $context['resourceId'], @@ -696,19 +661,6 @@ class Migrations extends Action $destination?->error(); } - if ($migration->getAttribute('status', '') === 'completed') { - foreach ($aggregatedResources as $resource) { - $this->processMigrationResourceStats( - $resource, - $usage, - $project, - $publisherForUsage, - $migration->getAttribute('source'), - $authorization, - ...$this->resolveResourceIds($migration), - ); - } - } } finally { $source?->cleanup(); $destination?->cleanup(); @@ -1123,58 +1075,4 @@ class Migrations extends Action return $errors; } - - private function processMigrationResourceStats(array $resources, Context $usage, Document $projectDocument, UsagePublisher $publisherForUsage, string $source, Authorization $authorization, ?string $parentResourceId, ?string $resourceId) - { - $resourceName = $resources['name']; - $count = $resources['count']; - $databaseInternalId = $resources['databaseId']; - $tableInternalId = $resources['tableId']; - - if ($source === CSV::getName()) { - if (empty($parentResourceId) || empty($resourceId)) { - Console::warning("Skipping CSV migration usage stats: missing parent/leaf resource ID (parent: '{$parentResourceId}', leaf: '{$resourceId}')"); - return; - } - $database = $authorization->skip(fn () => $this->dbForProject->getDocument('databases', $parentResourceId)); - if ($database->isEmpty()) { - Console::warning("Skipping CSV migration usage stats: database '{$parentResourceId}' not found"); - return; - } - $table = $authorization->skip(fn () => $this->dbForProject->getDocument('database_' . $database->getSequence(), $resourceId)); - if ($table->isEmpty()) { - Console::warning("Skipping CSV migration usage stats: collection '{$resourceId}' not found in database '{$parentResourceId}'"); - return; - } - $databaseInternalId = (int) $database->getSequence(); - $tableInternalId = (int) $table->getSequence(); - } - - switch ($resourceName) { - case ResourceRow::getName(): - $usage - ->setResource('database') - ->setResourceInternalId((string) $databaseInternalId) - ->addMetric( - str_replace( - ['{databaseInternalId}','{collectionInternalId}'], - [$databaseInternalId, $tableInternalId], - METRIC_DATABASE_ID_COLLECTION_ID_DOCUMENTS - ), - $count - ); - break; - - default: - break; - } - - $message = new UsageMessage( - project: $projectDocument, - metrics: $usage->getMetrics(), - reduce: $usage->getReduce() - ); - $publisherForUsage->enqueue($message); - $usage->reset(); - } } From ab627bc5633c37761995242348e9d3c5d1da1b20 Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Tue, 21 Jul 2026 10:52:29 +0545 Subject: [PATCH 12/13] refactor(usage): remove orphaned per-DB/per-collection metric constants (CLO-4588) METRIC_DATABASE_ID_COLLECTION_ID_DOCUMENTS/STORAGE and their documentsdb/ vectorsdb variants are no longer emitted or read (breakdown moved to resourceId/resourceType); remove the now-unused constants. --- app/init/constants.php | 6 ------ 1 file changed, 6 deletions(-) diff --git a/app/init/constants.php b/app/init/constants.php index 328e6dca18..7914b0324b 100644 --- a/app/init/constants.php +++ b/app/init/constants.php @@ -331,8 +331,6 @@ const METRIC_DATABASES = 'databases'; const METRIC_COLLECTIONS = 'collections'; const METRIC_DATABASES_STORAGE = 'databases.storage'; const METRIC_DOCUMENTS = 'documents'; -const METRIC_DATABASE_ID_COLLECTION_ID_DOCUMENTS = '{databaseInternalId}.{collectionInternalId}.documents'; -const METRIC_DATABASE_ID_COLLECTION_ID_STORAGE = '{databaseInternalId}.{collectionInternalId}.databases.storage'; const METRIC_DATABASES_OPERATIONS_READS = 'databases.operations.reads'; const METRIC_DATABASES_OPERATIONS_WRITES = 'databases.operations.writes'; @@ -341,8 +339,6 @@ const METRIC_DATABASES_DOCUMENTSDB = 'documentsdb.databases'; const METRIC_COLLECTIONS_DOCUMENTSDB = 'documentsdb.collections'; const METRIC_DATABASES_STORAGE_DOCUMENTSDB = 'documentsdb.databases.storage'; const METRIC_DOCUMENTS_DOCUMENTSDB = 'documentsdb.documents'; -const METRIC_DATABASE_ID_COLLECTION_ID_DOCUMENTS_DOCUMENTSDB = 'documentsdb.{databaseInternalId}.{collectionInternalId}.documents'; -const METRIC_DATABASE_ID_COLLECTION_ID_STORAGE_DOCUMENTSDB = 'documentsdb.{databaseInternalId}.{collectionInternalId}.databases.storage'; const METRIC_DATABASES_OPERATIONS_READS_DOCUMENTSDB = 'documentsdb.databases.operations.reads'; const METRIC_DATABASES_OPERATIONS_WRITES_DOCUMENTSDB = 'documentsdb.databases.operations.writes'; @@ -351,8 +347,6 @@ const METRIC_DATABASES_VECTORSDB = 'vectorsdb.databases'; const METRIC_COLLECTIONS_VECTORSDB = 'vectorsdb.collections'; const METRIC_DATABASES_STORAGE_VECTORSDB = 'vectorsdb.databases.storage'; const METRIC_DOCUMENTS_VECTORSDB = 'vectorsdb.documents'; -const METRIC_DATABASE_ID_COLLECTION_ID_DOCUMENTS_VECTORSDB = 'vectorsdb.{databaseInternalId}.{collectionInternalId}.documents'; -const METRIC_DATABASE_ID_COLLECTION_ID_STORAGE_VECTORSDB = 'vectorsdb.{databaseInternalId}.{collectionInternalId}.databases.storage'; const METRIC_DATABASES_OPERATIONS_READS_VECTORSDB = 'vectorsdb.databases.operations.reads'; const METRIC_DATABASES_OPERATIONS_WRITES_VECTORSDB = 'vectorsdb.databases.operations.writes'; const METRIC_EMBEDDINGS_TEXT = 'embeddings.text'; From 302e557d6f23a97a9b8fa457c93773c0e574dc45 Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Tue, 21 Jul 2026 14:43:09 +0545 Subject: [PATCH 13/13] fix(usage): drop now-unused $usage capture from getDatabasesDB (CLO-4588) Removing the per-document usage emission left $usage captured but unused in the getDatabasesDB factory closure, which PHPStan flags (Checks CI failure). Drop it from the closure param, the inner use(), and the DI dependency list. --- app/init/resources/request.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/init/resources/request.php b/app/init/resources/request.php index 70ed653f06..3208c0e096 100644 --- a/app/init/resources/request.php +++ b/app/init/resources/request.php @@ -1132,9 +1132,9 @@ return function (Container $context): void { return new Document([]); }, ['project', 'dbForProject', 'request', 'authorization']); - $context->set('getDatabasesDB', function (DatabaseFactory $databaseFactory, Document $project, Request $request, UsageContext $usage) { + $context->set('getDatabasesDB', function (DatabaseFactory $databaseFactory, Document $project, Request $request) { - return function (Document $database) use ($databaseFactory, $project, $request, $usage): Database { + return function (Document $database) use ($databaseFactory, $project, $request): Database { $databaseType = $database->getAttribute('type', ''); $database = $databaseFactory->tenant( @@ -1157,7 +1157,7 @@ return function (Container $context): void { return $database; }; - }, ['databaseFactory', 'project', 'request', 'usage']); + }, ['databaseFactory', 'project', 'request']); $context->set( 'transactionState',