diff --git a/app/controllers/api/databases.php b/app/controllers/api/databases.php index c8903538d0..025c73daf7 100644 --- a/app/controllers/api/databases.php +++ b/app/controllers/api/databases.php @@ -3665,6 +3665,7 @@ App::get('/v1/databases/usage') METRIC_DATABASES, METRIC_COLLECTIONS, METRIC_DOCUMENTS, + METRIC_DATABASES_STORAGE, ]; Authorization::skip(function () use ($dbForProject, $days, $metrics, &$stats) { @@ -3715,9 +3716,11 @@ App::get('/v1/databases/usage') 'databasesTotal' => $usage[$metrics[0]]['total'], 'collectionsTotal' => $usage[$metrics[1]]['total'], 'documentsTotal' => $usage[$metrics[2]]['total'], + 'databasesStorageTotal' => $usage[$metrics[3]]['total'], 'databases' => $usage[$metrics[0]]['data'], 'collections' => $usage[$metrics[1]]['data'], 'documents' => $usage[$metrics[2]]['data'], + 'databasesStorage' => $usage[$metrics[3]]['data'], ]), Response::MODEL_USAGE_DATABASES); }); @@ -3749,6 +3752,7 @@ App::get('/v1/databases/:databaseId/usage') $metrics = [ str_replace('{databaseInternalId}', $database->getInternalId(), METRIC_DATABASE_ID_COLLECTIONS), str_replace('{databaseInternalId}', $database->getInternalId(), METRIC_DATABASE_ID_DOCUMENTS), + str_replace('{databaseInternalId}', $database->getInternalId(), METRIC_DATABASE_ID_STORAGE) ]; Authorization::skip(function () use ($dbForProject, $days, $metrics, &$stats) { @@ -3799,8 +3803,10 @@ App::get('/v1/databases/:databaseId/usage') 'range' => $range, 'collectionsTotal' => $usage[$metrics[0]]['total'], 'documentsTotal' => $usage[$metrics[1]]['total'], + 'storageTotal' => $usage[$metrics[2]]['total'], 'collections' => $usage[$metrics[0]]['data'], 'documents' => $usage[$metrics[1]]['data'], + 'storage' => $usage[$metrics[2]]['data'], ]), Response::MODEL_USAGE_DATABASE); }); diff --git a/app/controllers/api/project.php b/app/controllers/api/project.php index a3e07927d6..a8f3a7695e 100644 --- a/app/controllers/api/project.php +++ b/app/controllers/api/project.php @@ -42,6 +42,7 @@ App::get('/v1/project/usage') METRIC_EXECUTIONS, METRIC_DOCUMENTS, METRIC_DATABASES, + METRIC_DATABASES_STORAGE, METRIC_USERS, METRIC_BUCKETS, METRIC_FILES_STORAGE @@ -144,6 +145,22 @@ App::get('/v1/project/usage') ]; }, $dbForProject->find('buckets')); + $databasesStorageBreakdown = array_map(function ($database) use ($dbForProject) { + $id = $database->getId(); + $name = $database->getAttribute('name'); + $metric = str_replace('{databaseInternalId}', $database->getInternalId(), METRIC_DATABASE_ID_STORAGE); + $value = $dbForProject->findOne('stats', [ + Query::equal('metric', [$metric]), + Query::equal('period', ['inf']) + ]); + + return [ + 'resourceId' => $id, + 'name' => $name, + 'value' => $value['value'] ?? 0, + ]; + }, $dbForProject->find('databases')); + // merge network inbound + outbound $projectBandwidth = []; foreach ($usage[METRIC_NETWORK_INBOUND] as $item) { @@ -173,11 +190,13 @@ App::get('/v1/project/usage') 'executionsTotal' => $total[METRIC_EXECUTIONS], 'documentsTotal' => $total[METRIC_DOCUMENTS], 'databasesTotal' => $total[METRIC_DATABASES], + 'databasesStorageTotal' => $total[METRIC_DATABASES_STORAGE], 'usersTotal' => $total[METRIC_USERS], 'bucketsTotal' => $total[METRIC_BUCKETS], 'filesStorageTotal' => $total[METRIC_FILES_STORAGE], 'executionsBreakdown' => $executionsBreakdown, - 'bucketsBreakdown' => $bucketsBreakdown + 'bucketsBreakdown' => $bucketsBreakdown, + 'databasesStorageBreakdown' => $databasesStorageBreakdown, ]), Response::MODEL_USAGE_PROJECT); }); diff --git a/app/init.php b/app/init.php index 65493cb95c..a61e0e4385 100644 --- a/app/init.php +++ b/app/init.php @@ -217,8 +217,9 @@ const METRIC_MESSAGES_COUNTRY_CODE = '{countryCode}.messages'; 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}.storage'; +const METRIC_DATABASE_ID_STORAGE = '{databaseInternalId}.storage'; const METRIC_DOCUMENTS = 'documents'; const METRIC_DATABASE_ID_DOCUMENTS = '{databaseInternalId}.documents'; const METRIC_DATABASE_ID_COLLECTION_ID_DOCUMENTS = '{databaseInternalId}.{collectionInternalId}.documents'; diff --git a/src/Appwrite/Utopia/Response/Model/UsageDatabase.php b/src/Appwrite/Utopia/Response/Model/UsageDatabase.php index d4733f2568..4486170390 100644 --- a/src/Appwrite/Utopia/Response/Model/UsageDatabase.php +++ b/src/Appwrite/Utopia/Response/Model/UsageDatabase.php @@ -28,6 +28,12 @@ class UsageDatabase extends Model 'default' => 0, 'example' => 0, ]) + ->addRule('storageTotal', [ + 'type' => self::TYPE_INTEGER, + 'description' => 'Total aggregated sum of databases storage size (in bytes)', + 'default' => 0, + 'example' => 0, + ]) ->addRule('collections', [ 'type' => Response::MODEL_METRIC, 'description' => 'Aggregated number of collections per period.', @@ -42,6 +48,13 @@ class UsageDatabase extends Model 'example' => [], 'array' => true ]) + ->addRule('storage', [ + 'type' => Response::MODEL_METRIC, + 'description' => 'Aggregated storage size (in bytes) per period.', + 'default' => [], + 'example' => [], + 'array' => true + ]); ; } diff --git a/src/Appwrite/Utopia/Response/Model/UsageDatabases.php b/src/Appwrite/Utopia/Response/Model/UsageDatabases.php index f775f9489d..82d644350a 100644 --- a/src/Appwrite/Utopia/Response/Model/UsageDatabases.php +++ b/src/Appwrite/Utopia/Response/Model/UsageDatabases.php @@ -34,6 +34,12 @@ class UsageDatabases extends Model 'default' => 0, 'example' => 0, ]) + ->addRule('databasesStorageTotal', [ + 'type' => self::TYPE_INTEGER, + 'description' => 'Total aggregated sum of databases storage size (in bytes)', + 'default' => 0, + 'example' => 0, + ]) ->addRule('databases', [ 'type' => Response::MODEL_METRIC, 'description' => 'Aggregated number of databases per period.', @@ -55,6 +61,13 @@ class UsageDatabases extends Model 'example' => [], 'array' => true ]) + ->addRule('databasesStorage', [ + 'type' => Response::MODEL_METRIC, + 'description' => 'Aggregated sum of databases storage size (in bytes) per period.', + 'default' => [], + 'example' => [], + 'array' => true + ]) ; } diff --git a/src/Appwrite/Utopia/Response/Model/UsageProject.php b/src/Appwrite/Utopia/Response/Model/UsageProject.php index b3aaac2a6c..648ad1ec42 100644 --- a/src/Appwrite/Utopia/Response/Model/UsageProject.php +++ b/src/Appwrite/Utopia/Response/Model/UsageProject.php @@ -28,6 +28,12 @@ class UsageProject extends Model 'default' => 0, 'example' => 0, ]) + ->addRule('databasesStorageTotal', [ + 'type' => self::TYPE_INTEGER, + 'description' => 'Total aggregated sum of databases storage size (in bytes)', + 'default' => 0, + 'example' => 0, + ]) ->addRule('usersTotal', [ 'type' => self::TYPE_INTEGER, 'description' => 'Total aggregated number of users.', @@ -88,6 +94,13 @@ class UsageProject extends Model 'example' => [], 'array' => true ]) + ->addRule('databasesStorageBreakdown', [ + 'type' => Response::MODEL_METRIC_BREAKDOWN, + 'description' => 'Aggregated breakdown in totals of usage by databases storage.', + 'default' => [], + 'example' => [], + 'array' => true + ]) ; }