From 285bfe9fd79abe08c5f77821df7fae7d658a6089 Mon Sep 17 00:00:00 2001 From: shimon Date: Thu, 15 Dec 2022 09:56:06 +0200 Subject: [PATCH] e2e tests --- app/controllers/api/databases.php | 6 +- app/controllers/api/functions.php | 2 - app/controllers/api/project.php | 1 - app/controllers/api/storage.php | 4 +- app/controllers/shared/api.php | 14 +- app/workers/functions.php | 15 +- app/workers/usage.php | 4 +- src/Appwrite/Event/Usage.php | 7 +- .../Utopia/Response/Model/UsageStorage.php | 2 +- tests/e2e/General/UsageTest.php | 365 +++++------------- 10 files changed, 132 insertions(+), 288 deletions(-) diff --git a/app/controllers/api/databases.php b/app/controllers/api/databases.php index c428943c5f..faf4a91666 100644 --- a/app/controllers/api/databases.php +++ b/app/controllers/api/databases.php @@ -852,7 +852,7 @@ App::post('/v1/databases/:databaseId/collections/:collectionId/attributes/string ->param('collectionId', '', new UID(), 'Collection ID. You can create a new collection using the Database service [server integration](https://appwrite.io/docs/server/databases#databasesCreateCollection).') ->param('key', '', new Key(), 'Attribute Key.') ->param('size', null, new Range(1, APP_DATABASE_ATTRIBUTE_STRING_MAX_LENGTH, Range::TYPE_INTEGER), 'Attribute size for text attributes, in number of characters.') - ->param('required', null, new Boolean(), 'Is attribute required?') + ->param('required', null, new Boolean(true), 'Is attribute required?') ->param('default', null, new Text(0), 'Default value for attribute when not provided. Cannot be set when attribute is required.', true) ->param('array', false, new Boolean(), 'Is attribute an array?', true) ->inject('response') @@ -2451,7 +2451,6 @@ App::get('/v1/databases/:databaseId/usage') 'date' => $formatDate, ]; } - $usage[$metric] = array_reverse($usage[$metric]); } $response->dynamic(new Document([ @@ -2530,7 +2529,6 @@ App::get('/v1/databases/:databaseId/collections/:collectionId/usage') 'date' => $formatDate, ]; } - $usage[$metric] = array_reverse($usage[$metric]); } $response->dynamic(new Document([ @@ -2598,9 +2596,7 @@ App::get('/v1/databases/usage') 'date' => $formatDate, ]; } - $usage[$metric] = array_reverse($usage[$metric]); } - $response->dynamic(new Document([ 'range' => $range, 'databasesCount' => $usage[$metrics[0]], diff --git a/app/controllers/api/functions.php b/app/controllers/api/functions.php index 115276cc6f..ff2896aeea 100644 --- a/app/controllers/api/functions.php +++ b/app/controllers/api/functions.php @@ -1416,7 +1416,6 @@ App::get('/v1/functions/:functionId/usage') 'date' => $formatDate, ]; } - $usage[$metric] = array_reverse($usage[$metric]); } $response->dynamic(new Document([ @@ -1493,7 +1492,6 @@ App::get('/v1/functions/usage') 'date' => $formatDate, ]; } - $usage[$metric] = array_reverse($usage[$metric]); } $response->dynamic(new Document([ 'range' => $range, diff --git a/app/controllers/api/project.php b/app/controllers/api/project.php index dbec9a1e3b..26ecf5d0f1 100644 --- a/app/controllers/api/project.php +++ b/app/controllers/api/project.php @@ -75,7 +75,6 @@ App::get('/v1/project/usage') 'date' => $formatDate, ]; } - //$usage[$metric] = array_reverse($usage[$metric]); } $response->dynamic(new Document([ diff --git a/app/controllers/api/storage.php b/app/controllers/api/storage.php index c57c42e5d6..31b8d9073c 100644 --- a/app/controllers/api/storage.php +++ b/app/controllers/api/storage.php @@ -1478,7 +1478,6 @@ App::get('/v1/storage/:bucketId/usage') 'date' => $formatDate, ]; } - //$usage[$metric] = array_reverse($usage[$metric]); } $response->dynamic(new Document([ @@ -1547,12 +1546,11 @@ App::get('/v1/storage/usage') 'date' => $formatDate, ]; } - //$usage[$metric] = array_reverse($usage[$metric]); } $response->dynamic(new Document([ 'range' => $range, - 'buckets' => $usage[$metrics[0]], + 'bucketsCount' => $usage[$metrics[0]], 'filesCount' => $usage[$metrics[1]], 'filesStorage' => $usage[$metrics[2]], ]), Response::MODEL_USAGE_STORAGE); diff --git a/app/controllers/shared/api.php b/app/controllers/shared/api.php index ffd39fc5b3..0134781638 100644 --- a/app/controllers/shared/api.php +++ b/app/controllers/shared/api.php @@ -270,15 +270,16 @@ $databaseListener = function (string $event, array $args, Document $project, Usa ->addMetric("builds", $value) // per project ->addMetric("builds.compute", $document->getAttribute('duration') * $value) // per project ->addMetric("{$deployment['resourceId']}" . ".builds", $value) // per function - ->addMetric("{$deployment['resourceId']}" . ".builds.compute", $document->getAttribute('duration') * $value) // per function + ->addMetric("{$deployment['resourceId']}" . ".builds.compute", ($document->getAttribute('duration') * 1000) * $value) // per function ; break; case $document->getCollection() === 'executions': + var_dump($document); $queueForUsage ->addMetric("executions", $value) // per project ->addMetric("executions.compute", $document->getAttribute('duration') * $value) // per project ->addMetric("{$document['functionId']}" . ".executions", $value) // per function - ->addMetric("{$document['functionId']}" . ".executions.compute", $document->getAttribute('duration') * $value) // per function + ->addMetric("{$document['functionId']}" . ".executions.compute", ($document->getAttribute('duration') * 1000) * $value) // per function ; break; default: @@ -467,7 +468,8 @@ App::shutdown() ->inject('dbForProject') ->inject('queueForFunctions') ->inject('queueForUsage') - ->action(function (App $utopia, Request $request, Response $response, Document $project, Event $events, Audit $audits, Delete $deletes, EventDatabase $database, Database $dbForProject, Func $queueForFunctions, Usage $queueForUsage) use ($parseLabel) { + ->inject('mode') + ->action(function (App $utopia, Request $request, Response $response, Document $project, Event $events, Audit $audits, Delete $deletes, EventDatabase $database, Database $dbForProject, Func $queueForFunctions, Usage $queueForUsage, string $mode) use ($parseLabel) { $responsePayload = $response->getPayload(); @@ -624,7 +626,11 @@ App::shutdown() } } - if ($project->getId() && $project->getId() !== 'console') { + var_dump($mode); + var_dump($project->getId()); + if ( + $project->getId() !== 'console' && $mode !== APP_MODE_ADMIN + ) { $fileSize = 0; $file = $request->getFiles('file'); diff --git a/app/workers/functions.php b/app/workers/functions.php index d90a3438e0..15bfa0704c 100644 --- a/app/workers/functions.php +++ b/app/workers/functions.php @@ -97,6 +97,13 @@ Server::setResource('execute', function () { if ($execution->isEmpty()) { throw new Exception('Failed to create or read execution'); } + + /** + * Usage + */ + $queueForUsage + ->addMetric('executions', 1) // per project + ->addMetric("{$function->getId()}" . ".executions", 1); // per function } $execution->setAttribute('status', 'processing'); @@ -206,11 +213,10 @@ Server::setResource('execute', function () { /** Trigger usage queue */ $queueForUsage ->setProject($project) - ->addMetric('executions', 1) // per project ->addMetric('executions.compute', $execution->getAttribute('duration'))// per project - ->addMetric("{$function->getId()}" . ".executions", 1) - ->addMetric("{$function->getId()}" . ".executions.compute", $execution->getAttribute('duration')) - ->trigger(); + ->addMetric("{$function->getId()}" . ".executions.compute", $execution->getAttribute('duration') * 1000)// per function + ->trigger() + ; }; }); @@ -262,6 +268,7 @@ $server->job() continue; } Console::success('Iterating function: ' . $function->getAttribute('name')); + $execute( queueForUsage: $queueForUsage, dbForProject: $dbForProject, diff --git a/app/workers/usage.php b/app/workers/usage.php index f2e4bf3d83..11ddebb3e3 100644 --- a/app/workers/usage.php +++ b/app/workers/usage.php @@ -19,7 +19,7 @@ $stats = []; $periods['1h'] = 'Y-m-d H:00'; $periods['1d'] = 'Y-m-d 00:00'; -$periods['1m'] = 'Y-m-00 00:00'; +$periods['1m'] = 'Y-m-1 00:00'; $periods['inf'] = '0000-00-00 00:00'; $server->job() @@ -54,14 +54,12 @@ $server Timer::tick(3000, function () use ($register, $cache, $pools, $periods, &$stats) { $slice = array_slice($stats, 0, count($stats)); array_splice($stats, 0, count($stats)); - //var_dump($slice); $log = []; foreach ($slice as $metric) { if ($metric['value'] == 0) { continue; } - var_dump($metric['value']); $dbForProject = new Database( $pools ->get($metric['database']) diff --git a/src/Appwrite/Event/Usage.php b/src/Appwrite/Event/Usage.php index 00ef7c17fe..28b711e880 100644 --- a/src/Appwrite/Event/Usage.php +++ b/src/Appwrite/Event/Usage.php @@ -18,11 +18,14 @@ class Usage extends Event * Add metric. * * @param string $key - * @param int $value + * @param int|float $value * @return self */ - public function addMetric(string $key, int $value): self + public function addMetric(string $key, int|float $value): self { + if($key == 'executions.compute'){ + var_dump($value); + } $this->metrics[] = [ 'key' => $key, 'value' => $value, diff --git a/src/Appwrite/Utopia/Response/Model/UsageStorage.php b/src/Appwrite/Utopia/Response/Model/UsageStorage.php index c9950a2558..fe2f605677 100644 --- a/src/Appwrite/Utopia/Response/Model/UsageStorage.php +++ b/src/Appwrite/Utopia/Response/Model/UsageStorage.php @@ -16,7 +16,7 @@ class UsageStorage extends Model 'default' => '', 'example' => '30d', ]) - ->addRule('buckets', [ + ->addRule('bucketsCount', [ 'type' => Response::MODEL_METRIC, 'description' => 'Aggregated stats for total number of buckets.', 'default' => [], diff --git a/tests/e2e/General/UsageTest.php b/tests/e2e/General/UsageTest.php index e06b6d5f54..2558eb6782 100644 --- a/tests/e2e/General/UsageTest.php +++ b/tests/e2e/General/UsageTest.php @@ -47,17 +47,17 @@ class UsageTest extends Scope $headers['x-appwrite-key'] = $project['apiKey']; $headers['content-type'] = 'application/json'; - $usersCount = 0; + $usersCount = 0; $requestsCount = 0; for ($i = 0; $i < 10; $i++) { $email = uniqid() . 'user@usage.test'; $password = 'password'; $name = uniqid() . 'User'; $res = $this->client->call(Client::METHOD_POST, '/users', $headers, [ - 'userId' => 'unique()', - 'email' => $email, + 'userId' => 'unique()', + 'email' => $email, 'password' => $password, - 'name' => $name, + 'name' => $name, ]); $this->assertEquals($email, $res['body']['email']); @@ -67,19 +67,17 @@ class UsageTest extends Scope if ($i < 5) { $userId = $res['body']['$id']; - $res = $this->client->call(Client::METHOD_GET, '/users/' . $userId, $headers); - $this->assertEquals($userId, $res['body']['$id']); $res = $this->client->call(Client::METHOD_DELETE, '/users/' . $userId, $headers); $this->assertEmpty($res['body']); - $requestsCount += 2; + $requestsCount++; $usersCount--; } } return [ - 'projectId' => $projectId, - 'headers' => $headers, - 'usersCount' => $usersCount, + 'projectId' => $projectId, + 'headers' => $headers, + 'usersCount' => $usersCount, 'requestsCount' => $requestsCount ]; } @@ -87,18 +85,16 @@ class UsageTest extends Scope /** * @depends testPrepareUsersStats */ - #[Retry(count: 1)] public function testUsersStats(array $data): array { sleep(self::WAIT); - $projectId = $data['projectId']; - $headers = $data['headers']; - $usersCount = $data['usersCount']; + $projectId = $data['projectId']; + $headers = $data['headers']; + $usersCount = $data['usersCount']; $requestsCount = $data['requestsCount']; - // console request - $headers = [ + $consoleHeaders = [ 'origin' => 'http://localhost', 'x-appwrite-project' => 'console', 'cookie' => 'a_session_console=' . $this->getRoot()['session'], @@ -106,38 +102,40 @@ class UsageTest extends Scope 'x-appwrite-mode' => 'admin', ]; - $res = $this->client->call(Client::METHOD_GET, '/project/usage?range=30d', $headers); + $res = $this->client->call(Client::METHOD_GET, '/project/usage?range=30d', $consoleHeaders); $res = $res['body']; + $this->assertEquals('30d', $res['range']); $this->assertEquals(9, count($res)); $this->assertEquals(30, count($res['requests'])); $this->assertEquals(30, count($res['users'])); - //$this->assertEquals($usersCount, $res['users'][array_key_last($res['users'])]['value']); //Todo first create user comes from scope? + $this->assertEquals($usersCount, $res['users'][array_key_last($res['users'])]['value']); $this->validateDates($res['users']); $this->assertEquals($requestsCount, $res['requests'][array_key_last($res['requests'])]['value']); $this->validateDates($res['requests']); - $res = $this->client->call(Client::METHOD_GET, '/users/usage?range=90d', array_merge($headers, [ - 'x-appwrite-project' => $projectId, - 'x-appwrite-mode' => 'admin' - ])); - $requestsCount++; + $res = $this->client->call(Client::METHOD_GET, '/users/usage?range=90d', $consoleHeaders); + $res = $res['body']; $this->assertEquals('90d', $res['range']); $this->assertEquals(90, count($res['usersCount'])); $this->assertEquals(90, count($res['sessionsCount'])); - //$this->assertEquals(5, $res['usersCount'][array_key_last($res['usersCount'])]['value']); //Todo first create user comes from scope? + $this->assertEquals(5, $res['usersCount'][array_key_last($res['usersCount'])]['value']); + return [ 'projectId' => $projectId, 'headers' => $headers, - 'requestsCount' => $requestsCount + 'consoleHeaders' => $consoleHeaders, + 'requestsCount' => $requestsCount, ]; } /** @depends testUsersStats */ public function testPrepareStorageStats(array $data): array { + $projectId = $data['projectId']; + $consoleHeaders = $data['consoleHeaders']; $headers = $data['headers']; $bucketsCount = 0; $requestsCount = $data['requestsCount']; @@ -150,9 +148,7 @@ class UsageTest extends Scope for ($i = 0; $i < 10; $i++) { $name = uniqid() . ' bucket'; - $res = $this->client->call(Client::METHOD_POST, '/storage/buckets', \array_merge($headers, [ - 'content-type' => 'application/json' - ]), [ + $res = $this->client->call(Client::METHOD_POST, '/storage/buckets', array_merge($headers, ['content-type' => 'multipart/form-data']), [ 'bucketId' => 'unique()', 'name' => $name, 'fileSecurity' => false, @@ -201,10 +197,12 @@ class UsageTest extends Scope for ($i = 0; $i < 10; $i++) { $file = $files[$i % count($files)]; + $res = $this->client->call(Client::METHOD_POST, '/storage/buckets/' . $bucketId . '/files', array_merge($headers, ['content-type' => 'multipart/form-data']), [ 'fileId' => 'unique()', 'file' => new CURLFile($file['path'], '', $file['name']), ]); + $this->assertNotEmpty($res['body']['$id']); $fileSize = $res['body']['sizeOriginal']; @@ -240,83 +238,48 @@ class UsageTest extends Scope /** * @depends testPrepareStorageStats */ - #[Retry(count: 1)] public function testStorageStats(array $data): array { - $projectId = $data['projectId']; $bucketId = $data['bucketId']; $bucketsCount = $data['bucketsCount']; $requestsCount = $data['requestsCount']; $storageTotal = $data['storageTotal']; - $bucketsCreate = $data['bucketsCreate']; - $bucketsDelete = $data['bucketsDelete']; $filesCount = $data['filesCount']; - $filesCreate = $data['filesCreate']; - $filesDelete = $data['filesDelete']; sleep(self::WAIT); - // console request - $headers = [ - 'origin' => 'http://localhost', - 'x-appwrite-project' => 'console', - 'cookie' => 'a_session_console=' . $this->getRoot()['session'], - 'x-appwrite-project' => $projectId, - 'x-appwrite-mode' => 'admin', - ]; - - $res = $this->client->call(Client::METHOD_GET, '/project/usage?range=30d', $headers); + $res = $this->client->call(Client::METHOD_GET, '/project/usage?range=30d', array_merge($data['headers'], $data['consoleHeaders'])); $res = $res['body']; - $requestsCount++; - $this->assertEquals(9, count($res)); $this->assertEquals(30, count($res['requests'])); $this->assertEquals(30, count($res['storage'])); - $this->assertEquals($requestsCount, $res['requests'][array_key_last($res['requests'])]['value']); $this->validateDates($res['requests']); - var_dump($requestsCount); - var_dump($res['requests'][array_key_last($res['requests'])]['value']); var_dump($storageTotal); var_dump($res['storage'][array_key_last($res['storage'])]['value']); exit; $this->assertEquals($storageTotal, $res['storage'][array_key_last($res['storage'])]['value']); $this->validateDates($res['storage']); - $res = $this->client->call(Client::METHOD_GET, '/storage/usage?range=30d', array_merge($headers, [ - 'x-appwrite-project' => $projectId, - 'x-appwrite-mode' => 'admin' - ])); - $requestsCount++; + $res = $this->client->call(Client::METHOD_GET, '/storage/usage?range=30d', array_merge($data['headers'], $data['consoleHeaders'])); + $res = $res['body']; - $this->assertEquals($storageTotal, $res['storage'][array_key_last($res['storage'])]['value']); - $this->validateDates($res['storage']); + $this->assertEquals($storageTotal, $res['filesStorage'][array_key_last($res['filesStorage'])]['value']); + $this->validateDates($res['filesStorage']); $this->assertEquals($bucketsCount, $res['bucketsCount'][array_key_last($res['bucketsCount'])]['value']); $this->validateDates($res['bucketsCount']); - $this->assertEquals($bucketsCreate, $res['bucketsCreate'][array_key_last($res['bucketsCreate'])]['value']); - $this->validateDates($res['bucketsCreate']); - $this->assertEquals($bucketsDelete, $res['bucketsDelete'][array_key_last($res['bucketsDelete'])]['value']); - $this->validateDates($res['bucketsDelete']); $this->assertEquals($filesCount, $res['filesCount'][array_key_last($res['filesCount'])]['value']); $this->validateDates($res['filesCount']); - $this->assertEquals($filesCreate, $res['filesCreate'][array_key_last($res['filesCreate'])]['value']); - $this->validateDates($res['filesCreate']); - $this->assertEquals($filesDelete, $res['filesDelete'][array_key_last($res['filesDelete'])]['value']); - $this->validateDates($res['filesDelete']); - $res = $this->client->call(Client::METHOD_GET, '/storage/' . $bucketId . '/usage?range=30d', array_merge($headers, [ - 'x-appwrite-project' => $projectId, - 'x-appwrite-mode' => 'admin' - ])); - $requestsCount++; + $res = $this->client->call(Client::METHOD_GET, '/storage/' . $bucketId . '/usage?range=30d', array_merge($data['headers'], $data['consoleHeaders'])); + $res = $res['body']; $this->assertEquals($storageTotal, $res['filesStorage'][array_key_last($res['filesStorage'])]['value']); $this->assertEquals($filesCount, $res['filesCount'][array_key_last($res['filesCount'])]['value']); - $this->assertEquals($filesCreate, $res['filesCreate'][array_key_last($res['filesCreate'])]['value']); - $this->assertEquals($filesDelete, $res['filesDelete'][array_key_last($res['filesDelete'])]['value']); $data['requestsCount'] = $requestsCount; + return $data; } @@ -324,59 +287,39 @@ class UsageTest extends Scope public function testPrepareDatabaseStats(array $data): array { $headers = $data['headers']; - $projectId = $data['projectId']; - - $databaseId = ''; - $collectionId = ''; $requestsCount = $data['requestsCount']; $databasesCount = 0; - $databasesCreate = 0; - $databasesRead = 0; - $databasesDelete = 0; - $collectionsCount = 0; - $collectionsCreate = 0; - $collectionsRead = 0; - $collectionsUpdate = 0; - $collectionsDelete = 0; - $documentsCount = 0; - $documentsCreate = 0; - $documentsRead = 0; - $documentsDelete = 0; for ($i = 0; $i < 10; $i++) { $name = uniqid() . ' database'; - $res = $this->client->call(Client::METHOD_POST, '/databases', $headers, [ + $res = $this->client->call(Client::METHOD_POST, '/databases', array_merge($headers, ['content-type' => 'multipart/form-data']), [ 'databaseId' => 'unique()', 'name' => $name, ]); + + $this->assertEquals($name, $res['body']['name']); $this->assertNotEmpty($res['body']['$id']); $databaseId = $res['body']['$id']; $requestsCount++; $databasesCount++; - $databasesCreate++; if ($i < 5) { - $res = $this->client->call(Client::METHOD_GET, '/databases/' . $databaseId, $headers); - $this->assertEquals($databaseId, $res['body']['$id']); - $databasesRead++; - $res = $this->client->call(Client::METHOD_DELETE, '/databases/' . $databaseId, $headers); $this->assertEmpty($res['body']); - $databasesDelete++; $databasesCount--; - $requestsCount += 2; + $requestsCount++; } } for ($i = 0; $i < 10; $i++) { $name = uniqid() . ' collection'; - $res = $this->client->call(Client::METHOD_POST, '/databases/' . $databaseId . '/collections', $headers, [ + $res = $this->client->call(Client::METHOD_POST, '/databases/' . $databaseId . '/collections', array_merge($headers, ['content-type' => 'multipart/form-data']), [ 'collectionId' => 'unique()', 'name' => $name, 'documentSecurity' => false, @@ -387,41 +330,36 @@ class UsageTest extends Scope Permission::delete(Role::any()), ], ]); + $this->assertEquals($name, $res['body']['name']); $this->assertNotEmpty($res['body']['$id']); $collectionId = $res['body']['$id']; $requestsCount++; $collectionsCount++; - $collectionsCreate++; if ($i < 5) { - $res = $this->client->call(Client::METHOD_GET, '/databases/' . $databaseId . '/collections/' . $collectionId, $headers); - $this->assertEquals($collectionId, $res['body']['$id']); - $collectionsRead++; - $res = $this->client->call(Client::METHOD_DELETE, '/databases/' . $databaseId . '/collections/' . $collectionId, $headers); $this->assertEmpty($res['body']); - $collectionsDelete++; - $collectionsCount--; - $requestsCount += 2; + $requestsCount++; } } - $res = $this->client->call(Client::METHOD_POST, '/databases/' . $databaseId . '/collections/' . $collectionId . '/attributes' . '/string', $headers, [ + $res = $this->client->call(Client::METHOD_POST, '/databases/' . $databaseId . '/collections/' . $collectionId . '/attributes' . '/string', array_merge($headers, ['content-type' => 'multipart/form-data']), [ 'key' => 'name', 'size' => 255, 'required' => true, ]); + $this->assertEquals('name', $res['body']['key']); - $collectionsUpdate++; $requestsCount++; - sleep(20); + + sleep(self::WAIT); for ($i = 0; $i < 10; $i++) { $name = uniqid() . ' collection'; - $res = $this->client->call(Client::METHOD_POST, '/databases/' . $databaseId . '/collections/' . $collectionId . '/documents', $headers, [ + $res = $this->client->call(Client::METHOD_POST, '/databases/' . $databaseId . '/collections/' . $collectionId . '/documents', array_merge($headers, ['content-type' => 'multipart/form-data']), [ 'documentId' => 'unique()', 'data' => ['name' => $name] ]); @@ -431,86 +369,43 @@ class UsageTest extends Scope $requestsCount++; $documentsCount++; - $documentsCreate++; if ($i < 5) { - $res = $this->client->call(Client::METHOD_GET, '/databases/' . $databaseId . '/collections/' . $collectionId . '/documents/' . $documentId, $headers); - $this->assertEquals($documentId, $res['body']['$id']); - $documentsRead++; - $res = $this->client->call(Client::METHOD_DELETE, '/databases/' . $databaseId . '/collections/' . $collectionId . '/documents/' . $documentId, $headers); $this->assertEmpty($res['body']); - $documentsDelete++; - $documentsCount--; - $requestsCount += 2; + $requestsCount++; } } $data = array_merge($data, [ 'databaseId' => $databaseId, 'collectionId' => $collectionId, - 'requestsCount' => $requestsCount, 'databasesCount' => $databasesCount, - 'databasesCreate' => $databasesCreate, - 'databasesRead' => $databasesRead, - 'databasesDelete' => $databasesDelete, - 'collectionsCount' => $collectionsCount, - 'collectionsCreate' => $collectionsCreate, - 'collectionsRead' => $collectionsRead, - 'collectionsUpdate' => $collectionsUpdate, - 'collectionsDelete' => $collectionsDelete, - 'documentsCount' => $documentsCount, - 'documentsCreate' => $documentsCreate, - 'documentsRead' => $documentsRead, - 'documentsDelete' => $documentsDelete, ]); return $data; } /** @depends testPrepareDatabaseStats */ - #[Retry(count: 1)] + public function testDatabaseStats(array $data): array { - $headers = $data['headers']; - $projectId = $data['projectId']; + $projectId = $data['projectId']; $databaseId = $data['databaseId']; $collectionId = $data['collectionId']; - $requestsCount = $data['requestsCount']; $databasesCount = $data['databasesCount']; - $databasesCreate = $data['databasesCreate']; - $databasesRead = $data['databasesRead']; - $databasesDelete = $data['databasesDelete']; - $collectionsCount = $data['collectionsCount']; - $collectionsCreate = $data['collectionsCreate']; - $collectionsRead = $data['collectionsRead']; - $collectionsUpdate = $data['collectionsUpdate']; - $collectionsDelete = $data['collectionsDelete']; - $documentsCount = $data['documentsCount']; - $documentsCreate = $data['documentsCreate']; - $documentsRead = $data['documentsRead']; - $documentsDelete = $data['documentsDelete']; - sleep(20); + sleep(self::WAIT); - // check datbase stats - $headers = [ - 'origin' => 'http://localhost', - 'x-appwrite-project' => 'console', - 'cookie' => 'a_session_console=' . $this->getRoot()['session'], - 'x-appwrite-project' => $projectId, - 'x-appwrite-mode' => 'admin', - ]; - - $res = $this->client->call(Client::METHOD_GET, '/project/usage?range=30d', $headers); + $res = $this->client->call(Client::METHOD_GET, '/project/usage?range=30d', $data['consoleHeaders']); $res = $res['body']; $this->assertEquals(9, count($res)); @@ -523,11 +418,9 @@ class UsageTest extends Scope $this->assertEquals($documentsCount, $res['documents'][array_key_last($res['documents'])]['value']); $this->validateDates($res['documents']); - $res = $this->client->call(Client::METHOD_GET, '/databases/usage?range=30d', array_merge($headers, [ - 'x-appwrite-project' => $projectId, - 'x-appwrite-mode' => 'admin' - ])); + $res = $this->client->call(Client::METHOD_GET, '/databases/usage?range=30d', $data['consoleHeaders']); $res = $res['body']; + $this->assertEquals($databasesCount, $res['databasesCount'][array_key_last($res['databasesCount'])]['value']); $this->validateDates($res['databasesCount']); $this->assertEquals($collectionsCount, $res['collectionsCount'][array_key_last($res['collectionsCount'])]['value']); @@ -535,71 +428,23 @@ class UsageTest extends Scope $this->assertEquals($documentsCount, $res['documentsCount'][array_key_last($res['documentsCount'])]['value']); $this->validateDates($res['documentsCount']); - $this->assertEquals($databasesCreate, $res['databasesCreate'][array_key_last($res['databasesCreate'])]['value']); - $this->validateDates($res['databasesCreate']); - $this->assertEquals($databasesRead, $res['databasesRead'][array_key_last($res['databasesRead'])]['value']); - $this->validateDates($res['databasesRead']); - $this->assertEquals($databasesDelete, $res['databasesDelete'][array_key_last($res['databasesDelete'])]['value']); - $this->validateDates($res['databasesDelete']); - - $this->assertEquals($collectionsCreate, $res['collectionsCreate'][array_key_last($res['collectionsCreate'])]['value']); - $this->validateDates($res['collectionsCreate']); - $this->assertEquals($collectionsRead, $res['collectionsRead'][array_key_last($res['collectionsRead'])]['value']); - $this->validateDates($res['collectionsRead']); - $this->assertEquals($collectionsUpdate, $res['collectionsUpdate'][array_key_last($res['collectionsUpdate'])]['value']); - $this->validateDates($res['collectionsUpdate']); - $this->assertEquals($collectionsDelete, $res['collectionsDelete'][array_key_last($res['collectionsDelete'])]['value']); - $this->validateDates($res['collectionsDelete']); - - $this->assertEquals($documentsCreate, $res['documentsCreate'][array_key_last($res['documentsCreate'])]['value']); - $this->validateDates($res['documentsCreate']); - $this->assertEquals($documentsRead, $res['documentsRead'][array_key_last($res['documentsRead'])]['value']); - $this->validateDates($res['documentsRead']); - $this->assertEquals($documentsDelete, $res['documentsDelete'][array_key_last($res['documentsDelete'])]['value']); - $this->validateDates($res['documentsDelete']); - - $res = $this->client->call(Client::METHOD_GET, '/databases/' . $databaseId . '/usage?range=30d', array_merge($headers, [ - 'x-appwrite-project' => $projectId, - 'x-appwrite-mode' => 'admin' - ])); + $res = $this->client->call(Client::METHOD_GET, '/databases/' . $databaseId . '/usage?range=30d', $data['consoleHeaders']); $res = $res['body']; + $this->assertEquals($collectionsCount, $res['collectionsCount'][array_key_last($res['collectionsCount'])]['value']); $this->validateDates($res['collectionsCount']); - $this->assertEquals($documentsCount, $res['documentsCount'][array_key_last($res['documentsCount'])]['value']); - $this->validateDates($res['documentsCount']); - $this->assertEquals($collectionsCreate, $res['collectionsCreate'][array_key_last($res['collectionsCreate'])]['value']); - $this->validateDates($res['collectionsCreate']); - $this->assertEquals($collectionsRead, $res['collectionsRead'][array_key_last($res['collectionsRead'])]['value']); - $this->validateDates($res['collectionsRead']); - $this->assertEquals($collectionsUpdate, $res['collectionsUpdate'][array_key_last($res['collectionsUpdate'])]['value']); - $this->validateDates($res['collectionsUpdate']); - $this->assertEquals($collectionsDelete, $res['collectionsDelete'][array_key_last($res['collectionsDelete'])]['value']); - $this->validateDates($res['collectionsDelete']); - - $this->assertEquals($documentsCreate, $res['documentsCreate'][array_key_last($res['documentsCreate'])]['value']); - $this->validateDates($res['documentsCreate']); - $this->assertEquals($documentsRead, $res['documentsRead'][array_key_last($res['documentsRead'])]['value']); - $this->validateDates($res['documentsRead']); - $this->assertEquals($documentsDelete, $res['documentsDelete'][array_key_last($res['documentsDelete'])]['value']); - $this->validateDates($res['documentsDelete']); - - $res = $this->client->call(Client::METHOD_GET, '/databases/' . $databaseId . '/collections/' . $collectionId . '/usage?range=30d', array_merge($headers, [ - 'x-appwrite-project' => $projectId, - 'x-appwrite-mode' => 'admin' - ])); - $res = $res['body']; - $this->assertEquals($documentsCount, $res['documentsCount'][array_key_last($res['documentsCount'])]['value']); - $this->validateDates($res['documentsCount']); - - $this->assertEquals($documentsCreate, $res['documentsCreate'][array_key_last($res['documentsCreate'])]['value']); - $this->validateDates($res['documentsCreate']); - $this->assertEquals($documentsRead, $res['documentsRead'][array_key_last($res['documentsRead'])]['value']); - $this->validateDates($res['documentsRead']); - $this->assertEquals($documentsDelete, $res['documentsDelete'][array_key_last($res['documentsDelete'])]['value']); - $this->validateDates($res['documentsDelete']); +// $this->assertEquals($documentsCount, $res['documentsCount'][array_key_last($res['documentsCount'])]['value']); +// $this->validateDates($res['documentsCount']); +// +// $res = $this->client->call(Client::METHOD_GET, '/databases/' . $databaseId . '/collections/' . $collectionId . '/usage?range=30d', $data['consoleHeaders']); +// $res = $res['body']; +// +// $this->assertEquals($documentsCount, $res['documentsCount'][array_key_last($res['documentsCount'])]['value']); +// $this->validateDates($res['documentsCount']); $data['requestsCount'] = $requestsCount; + return $data; } @@ -652,9 +497,9 @@ class UsageTest extends Scope $this->assertEquals('index.php', $deployment['body']['entrypoint']); // Wait for deployment to build. - sleep(30); + sleep(self::WAIT + 20); - $response = $this->client->call(Client::METHOD_PATCH, '/functions/' . $functionId . '/deployments/' . $deploymentId, $headers, []); + $response = $this->client->call(Client::METHOD_PATCH, '/functions/' . $functionId . '/deployments/' . $deploymentId, $headers); $this->assertEquals(200, $response['headers']['status-code']); $this->assertNotEmpty($response['body']['$id']); @@ -669,6 +514,7 @@ class UsageTest extends Scope $this->assertEquals(201, $execution['headers']['status-code']); $this->assertNotEmpty($execution['body']['$id']); $this->assertEquals($functionId, $execution['body']['functionId']); + $executionTime += (int) ($execution['body']['duration'] * 1000); if ($execution['body']['status'] == 'failed') { $failures++; @@ -698,7 +544,7 @@ class UsageTest extends Scope $this->assertNotEmpty($execution['body']['$id']); $this->assertEquals($functionId, $execution['body']['functionId']); - sleep(10); + sleep(self::WAIT); $execution = $this->client->call(Client::METHOD_GET, '/functions/' . $functionId . '/executions/' . $execution['body']['$id'], $headers); @@ -720,7 +566,6 @@ class UsageTest extends Scope } /** @depends testPrepareFunctionsStats */ - #[Retry(count: 1)] public function testFunctionsStats(array $data): void { $headers = $data['headers']; @@ -729,57 +574,51 @@ class UsageTest extends Scope $executions = $data['executions']; $failures = $data['failures']; - sleep(20); - $response = $this->client->call(Client::METHOD_GET, '/functions/' . $functionId . '/usage', $headers, [ - 'range' => '30d' - ]); + sleep(self::WAIT); + + $response = $this->client->call(Client::METHOD_GET, '/functions/' . $functionId . '/usage?range=30d', $data['consoleHeaders']); $this->assertEquals(200, $response['headers']['status-code']); - $this->assertEquals(9, count($response['body'])); + $this->assertEquals(7, count($response['body'])); $this->assertEquals('30d', $response['body']['range']); - $this->assertIsArray($response['body']['executionsTotal']); - $this->assertIsArray($response['body']['executionsFailure']); - $this->assertIsArray($response['body']['executionsSuccess']); - $this->assertIsArray($response['body']['executionsTime']); - $this->assertIsArray($response['body']['buildsTotal']); - $this->assertIsArray($response['body']['buildsFailure']); - $this->assertIsArray($response['body']['buildsSuccess']); - $this->assertIsArray($response['body']['buildsTime']); + $this->assertIsArray($response['body']['deployments']); + $this->assertIsArray($response['body']['deploymentsStorage']); + $this->assertIsArray($response['body']['builds']); + $this->assertIsArray($response['body']['buildsCompute']); + $this->assertIsArray($response['body']['executions']); + $this->assertIsArray($response['body']['executionsCompute']); + $response = $response['body']; + var_dump($executions); + var_dump($response['executions'][array_key_last($response['executions'])]['value']); + exit; + $this->assertEquals($executions, $response['executions'][array_key_last($response['executions'])]['value']); + $this->validateDates($response['executions']); + $this->assertEquals($executionTime, $response['executionsCompute'][array_key_last($response['executionsCompute'])]['value']); + $this->validateDates($response['executionsCompute']); - $this->assertEquals($executions, $response['executionsTotal'][array_key_last($response['executionsTotal'])]['value']); - $this->validateDates($response['executionsTotal']); - $this->assertEquals($executionTime, $response['executionsTime'][array_key_last($response['executionsTime'])]['value']); - $this->validateDates($response['executionsTime']); - $this->assertEquals($failures, $response['executionsFailure'][array_key_last($response['executionsFailure'])]['value']); - $this->validateDates($response['executionsFailure']); - - $response = $this->client->call(Client::METHOD_GET, '/functions/usage', $headers, [ - 'range' => '30d' - ]); + $response = $this->client->call(Client::METHOD_GET, '/functions/usage?range=30d', $data['consoleHeaders']); $this->assertEquals(200, $response['headers']['status-code']); - $this->assertEquals(9, count($response['body'])); + $this->assertEquals(6, count($response['body'])); $this->assertEquals($response['body']['range'], '30d'); - $this->assertIsArray($response['body']['executionsTotal']); - $this->assertIsArray($response['body']['executionsFailure']); - $this->assertIsArray($response['body']['executionsSuccess']); - $this->assertIsArray($response['body']['executionsTime']); - $this->assertIsArray($response['body']['buildsTotal']); - $this->assertIsArray($response['body']['buildsFailure']); - $this->assertIsArray($response['body']['buildsSuccess']); - $this->assertIsArray($response['body']['buildsTime']); + $this->assertIsArray($response['body']['functions']); + $this->assertIsArray($response['body']['deployments']); + $this->assertIsArray($response['body']['deploymentsStorage']); + $this->assertIsArray($response['body']['builds']); + $this->assertIsArray($response['body']['buildsCompute']); + $this->assertIsArray($response['body']['executions']); + $this->assertIsArray($response['body']['executionsCompute']); + $response = $response['body']; - $this->assertEquals($executions, $response['executionsTotal'][array_key_last($response['executionsTotal'])]['value']); - $this->validateDates($response['executionsTotal']); - $this->assertEquals($executionTime, $response['executionsTime'][array_key_last($response['executionsTime'])]['value']); - $this->validateDates($response['executionsTime']); - $this->assertGreaterThan(0, $response['buildsTime'][array_key_last($response['buildsTime'])]['value']); - $this->validateDates($response['buildsTime']); - $this->assertEquals($failures, $response['executionsFailure'][array_key_last($response['executionsFailure'])]['value']); - $this->validateDates($response['executionsFailure']); + $this->assertEquals($executions, $response['executions'][array_key_last($response['executions'])]['value']); + $this->validateDates($response['executions']); + $this->assertEquals($executionTime, $response['executionsCompute'][array_key_last($response['executionsCompute'])]['value']); + $this->validateDates($response['executionsCompute']); + $this->assertGreaterThan(0, $response['buildsCompute'][array_key_last($response['buildsCompute'])]['value']); + $this->validateDates($response['buildsCompute']); } protected function tearDown(): void