From 3b71215b4f8b72de3f1d7a6e4bcd3d5951677ba5 Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Tue, 28 Jan 2025 21:55:11 +1300 Subject: [PATCH] Fix merge --- app/config/errors.php | 2 +- app/controllers/api/databases.php | 92 +++++++++++++++++------------ composer.json | 2 +- composer.lock | 96 +++++++------------------------ src/Appwrite/Extend/Exception.php | 4 +- src/Appwrite/Utopia/Response.php | 9 +-- 6 files changed, 80 insertions(+), 125 deletions(-) diff --git a/app/config/errors.php b/app/config/errors.php index 93eefc30d4..00b9e660d2 100644 --- a/app/config/errors.php +++ b/app/config/errors.php @@ -1024,7 +1024,7 @@ return [ 'code' => 400, ], - /* Slow queries */ + /* Slow queries */ Exception::QUERY_BLOCKED => [ 'name' => Exception::QUERY_BLOCKED, 'description' => 'Request blocked after exceeding the maximum number of timeouts', diff --git a/app/controllers/api/databases.php b/app/controllers/api/databases.php index e9492081d4..962a3bd70b 100644 --- a/app/controllers/api/databases.php +++ b/app/controllers/api/databases.php @@ -18,7 +18,6 @@ use Appwrite\Utopia\Database\Validator\Queries\Databases; use Appwrite\Utopia\Database\Validator\Queries\Indexes; use Appwrite\Utopia\Request; use Appwrite\Utopia\Response; -use Appwrite\Utopia\Request; use MaxMind\Db\Reader; use Utopia\App; use Utopia\Audit\Audit; @@ -462,7 +461,7 @@ App::init() $request->getParam('queries') ])); /** @var Document $document */ - $document = Authorization::skip(fn() => $dbForProject->getDocument('slowQueries', $key)); + $document = Authorization::skip(fn () => $dbForProject->getDocument('slowQueries', $key)); if ($document->getAttribute('blocked') === true) { throw new Exception(Exception::QUERY_BLOCKED); } @@ -474,7 +473,7 @@ App::error() ->inject('request') ->inject('dbForProject') ->action(function (Throwable $error, Request $request, Database $dbForProject) { - if ($error instanceof Timeout) { + if ($error instanceof TimeoutException) { $route = Request::getRoute(); $collectionId = $route->getParamValue('collectionId'); $databaseId = $route->getParamValue('databaseId'); @@ -482,7 +481,7 @@ App::error() $queriesValidator = new ArrayList(new Text(APP_LIMIT_ARRAY_ELEMENT_SIZE), APP_LIMIT_ARRAY_PARAMS_SIZE); if (!$queriesValidator->isValid($queries)) { - App::setResource('error', fn() => new Exception(Exception::GENERAL_SERVER_ERROR)); + App::setResource('error', fn () => new Exception(Exception::GENERAL_SERVER_ERROR)); return; } @@ -493,9 +492,9 @@ App::error() ])); /** @var Document $document */ - $document = Authorization::skip(fn() => $dbForProject->getDocument('slowQueries', $key)); + $document = Authorization::skip(fn () => $dbForProject->getDocument('slowQueries', $key)); if ($document->isEmpty()) { - $document = Authorization::skip(fn()=>$dbForProject->createDocument('slowQueries', new Document([ + $document = Authorization::skip(fn () => $dbForProject->createDocument('slowQueries', new Document([ '$id' => $key, 'blocked' => false, 'count' => 1, @@ -510,15 +509,15 @@ App::error() if ($document->getAttribute('count') >= $max) { $document->setAttribute('blocked', true); } - $document = Authorization::skip(fn() => $dbForProject->updateDocument('slowQueries', $document->getId(), $document)); + $document = Authorization::skip(fn () => $dbForProject->updateDocument('slowQueries', $document->getId(), $document)); } if ($document->getAttribute('blocked') === true) { - App::setResource('error', fn() => new Exception(Exception::QUERY_BLOCKED)); + App::setResource('error', fn () => new Exception(Exception::QUERY_BLOCKED)); return; } - App::setResource('error', fn() => new Exception(Exception::QUERY_TIMEOUT)); + App::setResource('error', fn () => new Exception(Exception::QUERY_TIMEOUT)); } }); @@ -997,8 +996,7 @@ App::get('/v1/databases/:databaseId/collections') ->param('search', '', new Text(256), 'Search term to filter your list results. Max length: 256 chars.', true) ->inject('response') ->inject('dbForProject') - ->inject('mode') - ->action(function (string $databaseId, array $queries, string $search, Response $response, Database $dbForProject, string $mode) { + ->action(function (string $databaseId, array $queries, string $search, Response $response, Database $dbForProject) { $database = Authorization::skip(fn () => $dbForProject->getDocument('databases', $databaseId)); @@ -1290,8 +1288,7 @@ App::delete('/v1/databases/:databaseId/collections/:collectionId') ->inject('dbForProject') ->inject('queueForDatabase') ->inject('queueForEvents') - ->inject('mode') - ->action(function (string $databaseId, string $collectionId, Response $response, Database $dbForProject, EventDatabase $queueForDatabase, Event $queueForEvents, string $mode) { + ->action(function (string $databaseId, string $collectionId, Response $response, Database $dbForProject, EventDatabase $queueForDatabase, Event $queueForEvents) { $database = Authorization::skip(fn () => $dbForProject->getDocument('databases', $databaseId)); @@ -3514,13 +3511,13 @@ App::get('/v1/databases/:databaseId/collections/:collectionId/documents') $cursor->setValue($cursorDocument); } - $timeout = App::getEnv('_APP_SLOW_QUERIES') === 'enabled' - ? App::getEnv('_APP_SLOW_QUERIES_TIMEOUT') - : null; + $timeout = App::getEnv('_APP_SLOW_QUERIES') === 'enabled' + ? App::getEnv('_APP_SLOW_QUERIES_TIMEOUT') + : null; if (App::isDevelopment() && intval($request->getHeader('x-appwrite-timeout')) > 0) { $timeout = intval($request->getHeader('x-appwrite-timeout')); - $dbForProject->setTimeout($timeout); + $dbForProject->setTimeout($timeout); } $documents = $dbForProject->find('database_' . $database->getInternalId() . '_collection_' . $collection->getInternalId(), $queries); @@ -4529,13 +4526,18 @@ App::get('/v1/databases/:databaseId/slow-queries') ->groups(['api', 'database']) ->label('docs', false) ->label('scope', 'documents.read') - ->label('sdk.auth', [APP_AUTH_TYPE_ADMIN]) - ->label('sdk.namespace', 'databases') - ->label('sdk.method', 'listSlowQueries') - ->label('sdk.description', '/docs/references/databases/list-slow-queries.md') - ->label('sdk.response.code', Response::STATUS_CODE_OK) - ->label('sdk.response.type', Response::CONTENT_TYPE_JSON) - ->label('sdk.response.model', Response::MODEL_SLOW_QUERY_LIST) + ->label('sdk', new Method( + namespace: 'databases', + name: 'listSlowQueries', + description: '/docs/references/databases/list-slow-queries.md', + auth: [AuthType::ADMIN], + responses: [ + new SDKResponse( + code: Response::STATUS_CODE_OK, + model: Response::MODEL_SLOW_QUERY_LIST, + ) + ] + )) ->param('databaseId', '', new UID(), 'Database ID.') ->param('queries', [], new ArrayList(new Text(APP_LIMIT_ARRAY_ELEMENT_SIZE), APP_LIMIT_ARRAY_PARAMS_SIZE), 'Array of query strings generated using the Query class provided by the SDK. [Learn more about queries](https://appwrite.io/docs/databases#querying-documents). Maximum of ' . APP_LIMIT_ARRAY_PARAMS_SIZE . ' queries are allowed, each ' . APP_LIMIT_ARRAY_ELEMENT_SIZE . ' characters long.', true) ->inject('response') @@ -4544,7 +4546,7 @@ App::get('/v1/databases/:databaseId/slow-queries') $queries = Query::parseQueries($queries); $queries[] = Query::equal('databaseId', [$databaseId]); - $cursor = Query::getByType($queries, [Query::TYPE_CURSORAFTER, Query::TYPE_CURSORBEFORE]); + $cursor = Query::getByType($queries, [Query::TYPE_CURSOR_AFTER, Query::TYPE_CURSOR_BEFORE]); $cursor = reset($cursor); if ($cursor) { $documentId = $cursor->getValue(); @@ -4568,12 +4570,18 @@ App::get('/v1/databases/:databaseId/slow-queries/:slowQueryId') ->label('scope', 'documents.read') ->label('usage.metric', 'documents.{scope}.requests.read') ->label('sdk.auth', [APP_AUTH_TYPE_ADMIN]) - ->label('sdk.namespace', 'databases') - ->label('sdk.method', 'getSlowQuery') - ->label('sdk.description', '/docs/references/databases/get-slow-query.md') - ->label('sdk.response.code', Response::STATUS_CODE_OK) - ->label('sdk.response.type', Response::CONTENT_TYPE_JSON) - ->label('sdk.response.model', Response::MODEL_SLOW_QUERY) + ->label('sdk', new Method( + namespace: 'databases', + name: 'getSlowQuery', + description: '/docs/references/databases/get-slow-query.md', + auth: [AuthType::ADMIN], + responses: [ + new SDKResponse( + code: Response::STATUS_CODE_OK, + model: Response::MODEL_SLOW_QUERY, + ) + ] + )) ->param('databaseId', '', new UID(), 'Database ID.') ->param('slowQueryId', '', new UID(), 'Document ID.') ->inject('response') @@ -4592,24 +4600,32 @@ App::delete('/v1/databases/:databaseId/slow-queries/:slowQueryId') ->desc('Delete slow query') ->desc('List Documents') ->groups(['api', 'database']) - ->label('docs', false) ->label('scope', 'documents.write') ->label('usage.metric', 'documents.{scope}.requests.delete') - ->label('sdk.auth', [APP_AUTH_TYPE_ADMIN]) - ->label('sdk.namespace', 'databases') - ->label('sdk.method', 'deleteSlowQuery') - ->label('sdk.description', '/docs/references/databases/delete-slow-query.md') - ->label('sdk.response.code', Response::STATUS_CODE_NOCONTENT) - ->label('sdk.response.model', Response::MODEL_NONE) + ->label('docs', false) + ->label('sdk', new Method( + namespace: 'databases', + name: 'deleteSlowQuery', + description: '/docs/references/databases/delete-slow-query.md', + auth: [AuthType::ADMIN], + responses: [ + new SDKResponse( + code: Response::STATUS_CODE_NOCONTENT, + model: Response::MODEL_NONE, + ) + ] + )) ->param('databaseId', '', new UID(), 'Database ID.') ->param('slowQueryId', '', new UID(), 'Document ID.') ->inject('response') ->inject('dbForProject') ->action(function (string $databaseId, string $slowQueryId, Response $response, Database $dbForProject) { $document = $dbForProject->getDocument('slowQueries', $slowQueryId); + if ($document->isEmpty() || $document->getAttribute('databaseId') !== $databaseId) { throw new Exception(Exception::DOCUMENT_NOT_FOUND); } + $dbForProject->deleteDocument('slowQueries', $slowQueryId); $response->noContent(); diff --git a/composer.json b/composer.json index e024cff1c6..846e1295b3 100644 --- a/composer.json +++ b/composer.json @@ -30,7 +30,7 @@ } }, "require": { - "php": ">=8.0.0", + "php": ">=8.3", "ext-curl": "*", "ext-imagick": "*", "ext-mbstring": "*", diff --git a/composer.lock b/composer.lock index 91b90ca2e0..a5807f90e3 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "8927ec7d3cfa460ce223e4c13cf61ada", + "content-hash": "3e133c2809a4e665c7d30b10f21f8aa5", "packages": [ { "name": "adhocore/jwt", @@ -1237,16 +1237,16 @@ }, { "name": "open-telemetry/api", - "version": "1.2.0", + "version": "1.2.1", "source": { "type": "git", "url": "https://github.com/opentelemetry-php/api.git", - "reference": "351a30baa79699de3de3a814c8ccc7b52ccdfb1d" + "reference": "74b1a03263be8c5acb578f41da054b4bac3af4a0" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/opentelemetry-php/api/zipball/351a30baa79699de3de3a814c8ccc7b52ccdfb1d", - "reference": "351a30baa79699de3de3a814c8ccc7b52ccdfb1d", + "url": "https://api.github.com/repos/opentelemetry-php/api/zipball/74b1a03263be8c5acb578f41da054b4bac3af4a0", + "reference": "74b1a03263be8c5acb578f41da054b4bac3af4a0", "shasum": "" }, "require": { @@ -1303,7 +1303,7 @@ "issues": "https://github.com/open-telemetry/opentelemetry-php/issues", "source": "https://github.com/open-telemetry/opentelemetry-php" }, - "time": "2025-01-08T23:50:34+00:00" + "time": "2025-01-20T23:35:16+00:00" }, { "name": "open-telemetry/context", @@ -1493,16 +1493,16 @@ }, { "name": "open-telemetry/sdk", - "version": "1.2.0", + "version": "1.2.1", "source": { "type": "git", "url": "https://github.com/opentelemetry-php/sdk.git", - "reference": "9a1c3b866239dbff291e5cc555bb7793eab08127" + "reference": "96aeaee5b7cb8c0bc4af7ff4717b429f2d9f67e1" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/opentelemetry-php/sdk/zipball/9a1c3b866239dbff291e5cc555bb7793eab08127", - "reference": "9a1c3b866239dbff291e5cc555bb7793eab08127", + "url": "https://api.github.com/repos/opentelemetry-php/sdk/zipball/96aeaee5b7cb8c0bc4af7ff4717b429f2d9f67e1", + "reference": "96aeaee5b7cb8c0bc4af7ff4717b429f2d9f67e1", "shasum": "" }, "require": { @@ -1579,7 +1579,7 @@ "issues": "https://github.com/open-telemetry/opentelemetry-php/issues", "source": "https://github.com/open-telemetry/opentelemetry-php" }, - "time": "2025-01-08T23:50:34+00:00" + "time": "2025-01-09T23:17:14+00:00" }, { "name": "open-telemetry/sem-conv", @@ -5384,8 +5384,6 @@ }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/storage/zipball/eda6651ac16884dc2a79ecb984ea591ba1ed498c", - "reference": "eda6651ac16884dc2a79ecb984ea591ba1ed498c", "url": "https://api.github.com/repos/nikic/PHP-Parser/zipball/447a020a1f875a434d62f2a401f53b82a396e494", "reference": "447a020a1f875a434d62f2a401f53b82a396e494", "shasum": "" @@ -5432,7 +5430,6 @@ "issues": "https://github.com/nikic/PHP-Parser/issues", "source": "https://github.com/nikic/PHP-Parser/tree/v5.4.0" }, - "time": "2023-03-15T00:16:34+00:00" "time": "2024-12-30T11:07:19+00:00" }, { @@ -5604,70 +5601,18 @@ }, "time": "2023-10-30T13:38:26+00:00" }, - { - "name": "phpbench/dom", - "version": "0.3.3", - "source": { - "type": "git", - "url": "https://github.com/phpbench/dom.git", - "reference": "786a96db538d0def931f5b19225233ec42ec7a72" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/phpbench/dom/zipball/786a96db538d0def931f5b19225233ec42ec7a72", - "reference": "786a96db538d0def931f5b19225233ec42ec7a72", - "shasum": "" - }, - "require": { - "ext-dom": "*", - "php": "^7.3||^8.0" - }, - "require-dev": { - "friendsofphp/php-cs-fixer": "^3.14", - "phpstan/phpstan": "^1.10", - "phpunit/phpunit": "^8.0||^9.0" - }, - "type": "library", - "extra": { - "branch-alias": { - "dev-master": "1.0-dev" - } - }, - "autoload": { - "psr-4": { - "PhpBench\\Dom\\": "lib/" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Daniel Leech", - "email": "daniel@dantleech.com" - } - ], - "description": "DOM wrapper to simplify working with the PHP DOM implementation", - "support": { - "issues": "https://github.com/phpbench/dom/issues", - "source": "https://github.com/phpbench/dom/tree/0.3.3" - }, - "abandoned": true, - "time": "2023-03-06T23:46:57+00:00" - }, { "name": "phpbench/phpbench", - "version": "1.3.1", + "version": "1.4.0", "source": { "type": "git", "url": "https://github.com/phpbench/phpbench.git", - "reference": "a3e1ef08d9d7736d43a7fbd444893d6a073c0ca0" + "reference": "4248817222514421cba466bfa7adc7d8932345d4" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/phpbench/phpbench/zipball/a3e1ef08d9d7736d43a7fbd444893d6a073c0ca0", - "reference": "a3e1ef08d9d7736d43a7fbd444893d6a073c0ca0", + "url": "https://api.github.com/repos/phpbench/phpbench/zipball/4248817222514421cba466bfa7adc7d8932345d4", + "reference": "4248817222514421cba466bfa7adc7d8932345d4", "shasum": "" }, "require": { @@ -5680,7 +5625,6 @@ "ext-tokenizer": "*", "php": "^8.1", "phpbench/container": "^2.2", - "phpbench/dom": "~0.3.3", "psr/log": "^1.1 || ^2.0 || ^3.0", "seld/jsonlint": "^1.1", "symfony/console": "^6.1 || ^7.0", @@ -5699,8 +5643,8 @@ "phpstan/extension-installer": "^1.1", "phpstan/phpstan": "^1.0", "phpstan/phpstan-phpunit": "^1.0", - "phpunit/phpunit": "^10.4", - "rector/rector": "^0.18.11 || ^1.0.0", + "phpunit/phpunit": "^10.4 || ^11.0", + "rector/rector": "^1.2", "symfony/error-handler": "^6.1 || ^7.0", "symfony/var-dumper": "^6.1 || ^7.0" }, @@ -5745,7 +5689,7 @@ ], "support": { "issues": "https://github.com/phpbench/phpbench/issues", - "source": "https://github.com/phpbench/phpbench/tree/1.3.1" + "source": "https://github.com/phpbench/phpbench/tree/1.4.0" }, "funding": [ { @@ -5753,7 +5697,7 @@ "type": "github" } ], - "time": "2024-06-30T11:04:37+00:00" + "time": "2025-01-26T19:54:45+00:00" }, { "name": "phpdocumentor/reflection-common", @@ -8563,7 +8507,7 @@ "prefer-stable": false, "prefer-lowest": false, "platform": { - "php": ">=8.0.0", + "php": ">=8.3", "ext-curl": "*", "ext-imagick": "*", "ext-mbstring": "*", diff --git a/src/Appwrite/Extend/Exception.php b/src/Appwrite/Extend/Exception.php index 1385f879de..a9a3c36d3a 100644 --- a/src/Appwrite/Extend/Exception.php +++ b/src/Appwrite/Extend/Exception.php @@ -261,8 +261,8 @@ class Exception extends \Exception public const MIGRATION_IN_PROGRESS = 'migration_in_progress'; public const MIGRATION_PROVIDER_ERROR = 'migration_provider_error'; - - /** Timeout */ + + /** Timeout */ public const QUERY_BLOCKED = 'query_blocked'; public const QUERY_TIMEOUT = 'query_timeout'; diff --git a/src/Appwrite/Utopia/Response.php b/src/Appwrite/Utopia/Response.php index 9f2e55c310..c9491e2a36 100644 --- a/src/Appwrite/Utopia/Response.php +++ b/src/Appwrite/Utopia/Response.php @@ -2,10 +2,6 @@ namespace Appwrite\Utopia; -use Exception; -use Utopia\Swoole\Response as SwooleResponse; -use Swoole\Http\Response as SwooleHTTPResponse; -use Utopia\Database\Document; use Appwrite\Utopia\Fetch\BodyMultipart; use Appwrite\Utopia\Response\Filter; use Appwrite\Utopia\Response\Model; @@ -88,6 +84,7 @@ use Appwrite\Utopia\Response\Model\ProviderRepository; use Appwrite\Utopia\Response\Model\Rule; use Appwrite\Utopia\Response\Model\Runtime; use Appwrite\Utopia\Response\Model\Session; +use Appwrite\Utopia\Response\Model\SlowQuery; use Appwrite\Utopia\Response\Model\Specification; use Appwrite\Utopia\Response\Model\Subscriber; use Appwrite\Utopia\Response\Model\Target; @@ -110,13 +107,11 @@ use Appwrite\Utopia\Response\Model\UsageStorage; use Appwrite\Utopia\Response\Model\UsageUsers; use Appwrite\Utopia\Response\Model\User; use Appwrite\Utopia\Response\Model\Variable; -use Appwrite\Utopia\Response\Model\SlowQuery; use Appwrite\Utopia\Response\Model\VcsContent; use Appwrite\Utopia\Response\Model\Webhook; use Exception; use JsonException; use Swoole\Http\Response as SwooleHTTPResponse; -// Keep last use Utopia\Database\Document; use Utopia\Swoole\Response as SwooleResponse; @@ -784,7 +779,7 @@ class Response extends SwooleResponse /** * Function to add a response filter, the order of filters are first in - first out. * - * @param $filter the response filter to set + * @param $filter Filter The response filter to set * * @return void */