From ac2085ffa83bc4e2fd0810c2f3b2c4f6ddcbfe22 Mon Sep 17 00:00:00 2001 From: Bhaskar Singh Date: Sat, 18 Mar 2023 22:03:15 +0530 Subject: [PATCH] Updated error when _APP_USAGE_STATS is disabled for usage --- app/config/errors.php | 5 +++++ app/console | 2 +- app/controllers/api/databases.php | 9 +++++++++ app/controllers/api/functions.php | 6 ++++++ app/controllers/api/projects.php | 3 +++ app/controllers/api/storage.php | 6 ++++++ app/controllers/api/users.php | 3 +++ src/Appwrite/Extend/Exception.php | 1 + 8 files changed, 34 insertions(+), 1 deletion(-) diff --git a/app/config/errors.php b/app/config/errors.php index 266e017e93..852b103dc5 100644 --- a/app/config/errors.php +++ b/app/config/errors.php @@ -88,6 +88,11 @@ return [ 'description' => 'The request cannot be fulfilled with the current protocol. Please check the value of the _APP_OPTIONS_FORCE_HTTPS environment variable.', 'code' => 500, ], + Exception::GENERAL_USAGE_DISABLED => [ + 'name' => Exception::GENERAL_USAGE_DISABLED, + 'description' => 'Usage stats is not configured. Please check the value of the _APP_USAGE_STATS environment variable of your Appwrite server.', + 'code' => 501, + ], /** User Errors */ Exception::USER_COUNT_EXCEEDED => [ diff --git a/app/console b/app/console index 5e2a40c1e3..d30a4070d0 160000 --- a/app/console +++ b/app/console @@ -1 +1 @@ -Subproject commit 5e2a40c1e397bd341a432698c9d76a3f96315841 +Subproject commit d30a4070d0bdf8fe89be23a141e261a8a8688246 diff --git a/app/controllers/api/databases.php b/app/controllers/api/databases.php index 9ec46817ac..4c80bfe645 100644 --- a/app/controllers/api/databases.php +++ b/app/controllers/api/databases.php @@ -2471,6 +2471,9 @@ App::get('/v1/databases/usage') ->action(function (string $range, Response $response, Database $dbForProject) { $usage = []; + if (App::getEnv('_APP_USAGE_STATS', 'enabled') != 'enabled') { + throw new Exception(Exception::GENERAL_USAGE_DISABLED); + } if (App::getEnv('_APP_USAGE_STATS', 'enabled') == 'enabled') { $periods = [ '24h' => [ @@ -2590,6 +2593,9 @@ App::get('/v1/databases/:databaseId/usage') ->action(function (string $databaseId, string $range, Response $response, Database $dbForProject) { $usage = []; + if (App::getEnv('_APP_USAGE_STATS', 'enabled') != 'enabled') { + throw new Exception(Exception::GENERAL_USAGE_DISABLED); + } if (App::getEnv('_APP_USAGE_STATS', 'enabled') == 'enabled') { $periods = [ '24h' => [ @@ -2710,6 +2716,9 @@ App::get('/v1/databases/:databaseId/collections/:collectionId/usage') } $usage = []; + if (App::getEnv('_APP_USAGE_STATS', 'enabled') != 'enabled') { + throw new Exception(Exception::GENERAL_USAGE_DISABLED); + } if (App::getEnv('_APP_USAGE_STATS', 'enabled') == 'enabled') { $periods = [ '24h' => [ diff --git a/app/controllers/api/functions.php b/app/controllers/api/functions.php index 049d2cb850..f91784bb22 100644 --- a/app/controllers/api/functions.php +++ b/app/controllers/api/functions.php @@ -234,6 +234,9 @@ App::get('/v1/functions/:functionId/usage') } $usage = []; + if (App::getEnv('_APP_USAGE_STATS', 'enabled') != 'enabled') { + throw new Exception(Exception::GENERAL_USAGE_DISABLED); + } if (App::getEnv('_APP_USAGE_STATS', 'enabled') == 'enabled') { $periods = [ '24h' => [ @@ -337,6 +340,9 @@ App::get('/v1/functions/usage') ->action(function (string $range, Response $response, Database $dbForProject) { $usage = []; + if (App::getEnv('_APP_USAGE_STATS', 'enabled') != 'enabled') { + throw new Exception(Exception::GENERAL_USAGE_DISABLED); + } if (App::getEnv('_APP_USAGE_STATS', 'enabled') == 'enabled') { $periods = [ '24h' => [ diff --git a/app/controllers/api/projects.php b/app/controllers/api/projects.php index 29a193748d..b615b3555c 100644 --- a/app/controllers/api/projects.php +++ b/app/controllers/api/projects.php @@ -268,6 +268,9 @@ App::get('/v1/projects/:projectId/usage') } $usage = []; + if (App::getEnv('_APP_USAGE_STATS', 'enabled') != 'enabled') { + throw new Exception(Exception::GENERAL_USAGE_DISABLED); + } if (App::getEnv('_APP_USAGE_STATS', 'enabled') == 'enabled') { $periods = [ '24h' => [ diff --git a/app/controllers/api/storage.php b/app/controllers/api/storage.php index 271f2af6b3..ff8dd3ca2c 100644 --- a/app/controllers/api/storage.php +++ b/app/controllers/api/storage.php @@ -1461,6 +1461,9 @@ App::get('/v1/storage/usage') ->action(function (string $range, Response $response, Database $dbForProject) { $usage = []; + if (App::getEnv('_APP_USAGE_STATS', 'enabled') != 'enabled') { + throw new Exception(Exception::GENERAL_USAGE_DISABLED); + } if (App::getEnv('_APP_USAGE_STATS', 'enabled') === 'enabled') { $periods = [ '24h' => [ @@ -1578,6 +1581,9 @@ App::get('/v1/storage/:bucketId/usage') } $usage = []; + if (App::getEnv('_APP_USAGE_STATS', 'enabled') != 'enabled') { + throw new Exception(Exception::GENERAL_USAGE_DISABLED); + } if (App::getEnv('_APP_USAGE_STATS', 'enabled') === 'enabled') { $periods = [ '24h' => [ diff --git a/app/controllers/api/users.php b/app/controllers/api/users.php index 2a84c06675..96d6017848 100644 --- a/app/controllers/api/users.php +++ b/app/controllers/api/users.php @@ -1114,6 +1114,9 @@ App::get('/v1/users/usage') ->action(function (string $range, string $provider, Response $response, Database $dbForProject) { $usage = []; + if (App::getEnv('_APP_USAGE_STATS', 'enabled') != 'enabled') { + throw new Exception(Exception::GENERAL_USAGE_DISABLED); + } if (App::getEnv('_APP_USAGE_STATS', 'enabled') == 'enabled') { $periods = [ '24h' => [ diff --git a/src/Appwrite/Extend/Exception.php b/src/Appwrite/Extend/Exception.php index 8b240aa971..f072fcfad3 100644 --- a/src/Appwrite/Extend/Exception.php +++ b/src/Appwrite/Extend/Exception.php @@ -51,6 +51,7 @@ class Exception extends \Exception public const GENERAL_CURSOR_NOT_FOUND = 'general_cursor_not_found'; public const GENERAL_SERVER_ERROR = 'general_server_error'; public const GENERAL_PROTOCOL_UNSUPPORTED = 'general_protocol_unsupported'; + public const GENERAL_USAGE_DISABLED = 'general_usage_disabled'; /** Users */ public const USER_COUNT_EXCEEDED = 'user_count_exceeded';