From 7601c9047bde618850d8507355478a4cd00de506 Mon Sep 17 00:00:00 2001 From: shimon Date: Thu, 15 Dec 2022 11:45:43 +0200 Subject: [PATCH] e2e tests --- app/controllers/api/databases.php | 1 - app/controllers/api/functions.php | 11 ++++++++++- app/controllers/shared/api.php | 7 +------ app/workers/functions.php | 5 +++-- src/Appwrite/Event/Usage.php | 9 ++++----- tests/e2e/General/UsageTest.php | 13 ++++++------- 6 files changed, 24 insertions(+), 22 deletions(-) diff --git a/app/controllers/api/databases.php b/app/controllers/api/databases.php index faf4a91666..583be90f1d 100644 --- a/app/controllers/api/databases.php +++ b/app/controllers/api/databases.php @@ -18,7 +18,6 @@ use Utopia\Validator\ArrayList; use Utopia\Validator\JSON; use Utopia\Database\Database; use Utopia\Database\Document; -use Utopia\Database\DateTime; use Utopia\Database\Query; use Utopia\Database\Validator\Authorization; use Utopia\Database\Validator\Key; diff --git a/app/controllers/api/functions.php b/app/controllers/api/functions.php index ff2896aeea..90d80adb28 100644 --- a/app/controllers/api/functions.php +++ b/app/controllers/api/functions.php @@ -6,6 +6,7 @@ use Appwrite\Event\Build; use Appwrite\Event\Delete; use Appwrite\Event\Event; use Appwrite\Event\Func; +use Appwrite\Event\Usage; use Appwrite\Event\Validator\Event as ValidatorEvent; use Appwrite\Extend\Exception; use Appwrite\Utopia\Database\Validator\CustomId; @@ -851,7 +852,8 @@ App::post('/v1/functions/:functionId/executions') ->inject('events') ->inject('mode') ->inject('queueForFunctions') - ->action(function (string $functionId, string $data, bool $async, Response $response, Document $project, Database $dbForProject, Document $user, Event $events, string $mode, Func $queueForFunctions) { + ->inject('queueForUsage') + ->action(function (string $functionId, string $data, bool $async, Response $response, Document $project, Database $dbForProject, Document $user, Event $events, string $mode, Func $queueForFunctions, Usage $queueForUsage) { $function = Authorization::skip(fn () => $dbForProject->getDocument('functions', $functionId)); @@ -993,6 +995,13 @@ App::post('/v1/functions/:functionId/executions') $execution->setAttribute('stdout', $executionResponse['stdout']); $execution->setAttribute('stderr', $executionResponse['stderr']); $execution->setAttribute('duration', $executionResponse['duration']); + /** + * Sync execution compute usage from + */ + $queueForUsage + ->addMetric('executions.compute', (int)($executionResponse['duration'] * 1000))// per project + ->addMetric("{$function->getId()}" . ".executions.compute", (int)($executionResponse['duration'] * 1000))// per function + ; } catch (\Throwable $th) { $interval = (new \DateTime())->diff(new \DateTime($execution->getCreatedAt())); $execution diff --git a/app/controllers/shared/api.php b/app/controllers/shared/api.php index 0134781638..328d9542b0 100644 --- a/app/controllers/shared/api.php +++ b/app/controllers/shared/api.php @@ -270,16 +270,13 @@ $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') * 1000) * $value) // per function + ->addMetric("{$deployment['resourceId']}" . ".builds.compute", (int)($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') * 1000) * $value) // per function ; break; default: @@ -626,8 +623,6 @@ App::shutdown() } } - var_dump($mode); - var_dump($project->getId()); if ( $project->getId() !== 'console' && $mode !== APP_MODE_ADMIN ) { diff --git a/app/workers/functions.php b/app/workers/functions.php index 15bfa0704c..cb9817d73e 100644 --- a/app/workers/functions.php +++ b/app/workers/functions.php @@ -101,6 +101,7 @@ Server::setResource('execute', function () { /** * Usage */ + $queueForUsage ->addMetric('executions', 1) // per project ->addMetric("{$function->getId()}" . ".executions", 1); // per function @@ -213,8 +214,8 @@ Server::setResource('execute', function () { /** Trigger usage queue */ $queueForUsage ->setProject($project) - ->addMetric('executions.compute', $execution->getAttribute('duration'))// per project - ->addMetric("{$function->getId()}" . ".executions.compute", $execution->getAttribute('duration') * 1000)// per function + ->addMetric('executions.compute', (int)($execution->getAttribute('duration') * 1000))// per project + ->addMetric("{$function->getId()}" . ".executions.compute", (int)($execution->getAttribute('duration') * 1000))// per function ->trigger() ; }; diff --git a/src/Appwrite/Event/Usage.php b/src/Appwrite/Event/Usage.php index 28b711e880..8cec45d84c 100644 --- a/src/Appwrite/Event/Usage.php +++ b/src/Appwrite/Event/Usage.php @@ -18,14 +18,11 @@ class Usage extends Event * Add metric. * * @param string $key - * @param int|float $value + * @param int $value * @return self */ - public function addMetric(string $key, int|float $value): self + public function addMetric(string $key, int $value): self { - if($key == 'executions.compute'){ - var_dump($value); - } $this->metrics[] = [ 'key' => $key, 'value' => $value, @@ -43,6 +40,8 @@ class Usage extends Event { $client = new Client($this->queue, $this->connection); + var_dump($this->metrics); + return $client->enqueue([ 'project' => $this->getProject(), 'metrics' => $this->metrics, diff --git a/tests/e2e/General/UsageTest.php b/tests/e2e/General/UsageTest.php index 2558eb6782..ca6a06afad 100644 --- a/tests/e2e/General/UsageTest.php +++ b/tests/e2e/General/UsageTest.php @@ -256,9 +256,7 @@ class UsageTest extends Scope $this->assertEquals(30, count($res['storage'])); $this->assertEquals($requestsCount, $res['requests'][array_key_last($res['requests'])]['value']); $this->validateDates($res['requests']); - 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']); @@ -516,6 +514,7 @@ class UsageTest extends Scope $this->assertEquals($functionId, $execution['body']['functionId']); $executionTime += (int) ($execution['body']['duration'] * 1000); + if ($execution['body']['status'] == 'failed') { $failures++; } elseif ($execution['body']['status'] == 'completed') { @@ -553,6 +552,7 @@ class UsageTest extends Scope } elseif ($execution['body']['status'] == 'completed') { $executions++; } + $executionTime += (int) ($execution['body']['duration'] * 1000); $data = array_merge($data, [ @@ -590,18 +590,17 @@ class UsageTest extends Scope $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']); $response = $this->client->call(Client::METHOD_GET, '/functions/usage?range=30d', $data['consoleHeaders']); $this->assertEquals(200, $response['headers']['status-code']); - $this->assertEquals(6, count($response['body'])); + $this->assertEquals(8, count($response['body'])); $this->assertEquals($response['body']['range'], '30d'); $this->assertIsArray($response['body']['functions']); $this->assertIsArray($response['body']['deployments']);