From 12e4f49cba3fc81bd626676f005c9e152205281d Mon Sep 17 00:00:00 2001 From: shimon Date: Wed, 15 May 2024 20:25:29 +0300 Subject: [PATCH 1/8] adding requests count on error --- app/controllers/shared/api.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/app/controllers/shared/api.php b/app/controllers/shared/api.php index 7b218e050a..810254ae6d 100644 --- a/app/controllers/shared/api.php +++ b/app/controllers/shared/api.php @@ -15,13 +15,11 @@ use Appwrite\Extend\Exception as AppwriteException; use Appwrite\Messaging\Adapter\Realtime; use Appwrite\Utopia\Request; use Appwrite\Utopia\Response; -use Appwrite\Utopia\View; use Utopia\Abuse\Abuse; use Utopia\Abuse\Adapters\TimeLimit; use Utopia\App; use Utopia\Cache\Adapter\Filesystem; use Utopia\Cache\Cache; -use Utopia\CLI\Console; use Utopia\Config\Config; use Utopia\Database\Database; use Utopia\Database\DateTime; From dae9cb995c17fc5667f176e9ed0aa00a5b9fa4f1 Mon Sep 17 00:00:00 2001 From: shimon Date: Thu, 16 May 2024 21:21:21 +0300 Subject: [PATCH 2/8] adding requests count on error --- app/controllers/shared/api.php | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/app/controllers/shared/api.php b/app/controllers/shared/api.php index 810254ae6d..d1073ee8e7 100644 --- a/app/controllers/shared/api.php +++ b/app/controllers/shared/api.php @@ -26,10 +26,7 @@ use Utopia\Database\DateTime; use Utopia\Database\Document; use Utopia\Database\Helpers\Role; use Utopia\Database\Validator\Authorization; -use Utopia\DSN\DSN; -use Utopia\Logger\Log; use Utopia\Logger\Log\User; -use Utopia\Logger\Logger; use Utopia\System\System; use Utopia\Validator\WhiteList; @@ -750,7 +747,7 @@ App::error() ->inject('response') ->inject('project') ->inject('queueForUsage') - ->action(function (Throwable $error, Request $request, Response $response, Document $project, Audit $queueForAudits, Usage $queueForUsage) { + ->action(function (Throwable $error, Request $request, Response $response, Document $project, Audit $queueForAudits, Usage $queueForUsage) { if ($error instanceof AppwriteException) { $publish = $error->isPublishable(); @@ -777,4 +774,4 @@ App::error() ->trigger(); } - }); \ No newline at end of file + }); From e1ac0d622ee92b9f61edc1cc94c93ca0f19db8c5 Mon Sep 17 00:00:00 2001 From: shimon Date: Sun, 19 May 2024 10:55:14 +0300 Subject: [PATCH 3/8] adding requests count on error --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 8865a32d91..e247799645 100644 --- a/composer.json +++ b/composer.json @@ -57,7 +57,7 @@ "utopia-php/fetch": "0.2.*", "utopia-php/image": "0.6.*", "utopia-php/locale": "0.4.*", - "utopia-php/logger": "0.3.*", + "utopia-php/logger": "0.5.*", "utopia-php/messaging": "0.10.*", "utopia-php/migration": "0.4.*", "utopia-php/orchestration": "0.9.*", From 8aabb92fdaccec0bd7964e9e7d3b95d13a82ac8e Mon Sep 17 00:00:00 2001 From: shimon Date: Sun, 19 May 2024 13:50:25 +0300 Subject: [PATCH 4/8] adding requests count on error --- app/controllers/shared/api.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/app/controllers/shared/api.php b/app/controllers/shared/api.php index d1073ee8e7..91e7f87113 100644 --- a/app/controllers/shared/api.php +++ b/app/controllers/shared/api.php @@ -27,6 +27,7 @@ use Utopia\Database\Document; use Utopia\Database\Helpers\Role; use Utopia\Database\Validator\Authorization; use Utopia\Logger\Log\User; +use Utopia\Logger\Logger; use Utopia\System\System; use Utopia\Validator\WhiteList; @@ -755,6 +756,10 @@ App::error() $publish = $error->getCode() === 0 || $error->getCode() >= 500; } + if ($error->getCode() >= 400 && $error->getCode() < 500) { + $publish = true; + } + if ($publish && $project->getId() !== 'console') { if (!Auth::isPrivilegedUser(Authorization::getRoles())) { $fileSize = 0; From 87cf7776df0cc6402a29179e8b769b511679f88c Mon Sep 17 00:00:00 2001 From: shimon Date: Thu, 23 May 2024 10:59:19 +0300 Subject: [PATCH 5/8] lint --- app/controllers/shared/api.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/app/controllers/shared/api.php b/app/controllers/shared/api.php index 91e7f87113..172ded7f31 100644 --- a/app/controllers/shared/api.php +++ b/app/controllers/shared/api.php @@ -27,7 +27,6 @@ use Utopia\Database\Document; use Utopia\Database\Helpers\Role; use Utopia\Database\Validator\Authorization; use Utopia\Logger\Log\User; -use Utopia\Logger\Logger; use Utopia\System\System; use Utopia\Validator\WhiteList; @@ -757,8 +756,8 @@ App::error() } if ($error->getCode() >= 400 && $error->getCode() < 500) { - $publish = true; - } + $publish = true; + } if ($publish && $project->getId() !== 'console') { if (!Auth::isPrivilegedUser(Authorization::getRoles())) { From a22d98296c81e92e5c629b9a0f93ffc3f6873804 Mon Sep 17 00:00:00 2001 From: shimon Date: Thu, 23 May 2024 16:33:40 +0300 Subject: [PATCH 6/8] lint --- app/controllers/shared/api.php | 2 +- composer.lock | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/controllers/shared/api.php b/app/controllers/shared/api.php index d44efce45d..72708d9b88 100644 --- a/app/controllers/shared/api.php +++ b/app/controllers/shared/api.php @@ -746,7 +746,7 @@ App::error() ->inject('response') ->inject('project') ->inject('queueForUsage') - ->action(function (Throwable $error, Request $request, Response $response, Document $project, Audit $queueForAudits, Usage $queueForUsage) { + ->action(function (Throwable $error, Request $request, Response $response, Document $project, Usage $queueForUsage) { if ($error instanceof AppwriteException) { $publish = $error->isPublishable(); diff --git a/composer.lock b/composer.lock index bfe8fc0730..8acbbed541 100644 --- a/composer.lock +++ b/composer.lock @@ -5613,5 +5613,5 @@ "platform-overrides": { "php": "8.3" }, - "plugin-api-version": "2.6.0" + "plugin-api-version": "2.2.0" } From c996dbcdc911b08525ece4f9de28419e8eb0caf7 Mon Sep 17 00:00:00 2001 From: shimon Date: Thu, 23 May 2024 16:39:23 +0300 Subject: [PATCH 7/8] lint --- app/controllers/general.php | 24 +++++++++++++++++++- app/controllers/shared/api.php | 41 +--------------------------------- 2 files changed, 24 insertions(+), 41 deletions(-) diff --git a/app/controllers/general.php b/app/controllers/general.php index f1be7bd614..ca57b27ec6 100644 --- a/app/controllers/general.php +++ b/app/controllers/general.php @@ -2,6 +2,7 @@ require_once __DIR__ . '/../init.php'; +use Appwrite\Auth\Auth; use Appwrite\Event\Certificate; use Appwrite\Event\Event; use Appwrite\Event\Usage; @@ -646,7 +647,8 @@ App::error() ->inject('project') ->inject('logger') ->inject('log') - ->action(function (Throwable $error, App $utopia, Request $request, Response $response, Document $project, ?Logger $logger, Log $log) { + ->inject('queueForUsage') + ->action(function (Throwable $error, App $utopia, Request $request, Response $response, Document $project, ?Logger $logger, Log $log, Usage $queueForUsage) { $version = System::getEnv('_APP_VERSION', 'UNKNOWN'); $route = $utopia->getRoute(); $class = \get_class($error); @@ -735,6 +737,26 @@ App::error() } } + if ($publish && $project->getId() !== 'console') { + if (!Auth::isPrivilegedUser(Authorization::getRoles())) { + $fileSize = 0; + $file = $request->getFiles('file'); + if (!empty($file)) { + $fileSize = (\is_array($file['size']) && isset($file['size'][0])) ? $file['size'][0] : $file['size']; + } + + $queueForUsage + ->addMetric(METRIC_NETWORK_REQUESTS, 1) + ->addMetric(METRIC_NETWORK_INBOUND, $request->getSize() + $fileSize) + ->addMetric(METRIC_NETWORK_OUTBOUND, $response->getSize()); + } + + $queueForUsage + ->setProject($project) + ->trigger(); + } + + if ($logger && $publish) { try { /** @var Utopia\Database\Document $user */ diff --git a/app/controllers/shared/api.php b/app/controllers/shared/api.php index 72708d9b88..54f9da27c1 100644 --- a/app/controllers/shared/api.php +++ b/app/controllers/shared/api.php @@ -738,43 +738,4 @@ App::init() if (System::getEnv('_APP_USAGE_STATS', 'enabled') !== 'enabled') { throw new Exception(Exception::GENERAL_USAGE_DISABLED); } - }); - -App::error() - ->inject('error') - ->inject('request') - ->inject('response') - ->inject('project') - ->inject('queueForUsage') - ->action(function (Throwable $error, Request $request, Response $response, Document $project, Usage $queueForUsage) { - - if ($error instanceof AppwriteException) { - $publish = $error->isPublishable(); - } else { - $publish = $error->getCode() === 0 || $error->getCode() >= 500; - } - - if ($error->getCode() >= 400 && $error->getCode() < 500) { - $publish = true; - } - - if ($publish && $project->getId() !== 'console') { - if (!Auth::isPrivilegedUser(Authorization::getRoles())) { - $fileSize = 0; - $file = $request->getFiles('file'); - if (!empty($file)) { - $fileSize = (\is_array($file['size']) && isset($file['size'][0])) ? $file['size'][0] : $file['size']; - } - - $queueForUsage - ->addMetric(METRIC_NETWORK_REQUESTS, 1) - ->addMetric(METRIC_NETWORK_INBOUND, $request->getSize() + $fileSize) - ->addMetric(METRIC_NETWORK_OUTBOUND, $response->getSize()); - } - - $queueForUsage - ->setProject($project) - ->trigger(); - } - - }); + }); \ No newline at end of file From a2ef77f0f593b403bab295e820de11f489026610 Mon Sep 17 00:00:00 2001 From: shimon Date: Thu, 23 May 2024 16:47:37 +0300 Subject: [PATCH 8/8] lint --- app/controllers/shared/api.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/shared/api.php b/app/controllers/shared/api.php index 54f9da27c1..1afd6b652e 100644 --- a/app/controllers/shared/api.php +++ b/app/controllers/shared/api.php @@ -738,4 +738,4 @@ App::init() if (System::getEnv('_APP_USAGE_STATS', 'enabled') !== 'enabled') { throw new Exception(Exception::GENERAL_USAGE_DISABLED); } - }); \ No newline at end of file + });