From 906de6a802b4bb7f37282207909af3225e637335 Mon Sep 17 00:00:00 2001 From: Bradley Schofield Date: Fri, 2 Aug 2024 15:28:39 +0900 Subject: [PATCH 1/6] Fix queueForUsage not triggering for domain executions --- app/controllers/general.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/app/controllers/general.php b/app/controllers/general.php index c89dd10917..bdd11115f8 100644 --- a/app/controllers/general.php +++ b/app/controllers/general.php @@ -318,6 +318,10 @@ function router(App $utopia, Database $dbForConsole, callable $getProjectDB, Swo /** @var Document $execution */ $execution = Authorization::skip(fn () => $dbForProject->createDocument('executions', $execution)); } + + $queueForUsage + ->setProject($project) + ->trigger(); } $execution->setAttribute('logs', ''); From f5571b4caa621c40f830cabce014db4fc31968d0 Mon Sep 17 00:00:00 2001 From: Bradley Schofield Date: Fri, 2 Aug 2024 16:24:54 +0900 Subject: [PATCH 2/6] Add Metric Tests to Domain Executions --- .../Functions/FunctionsCustomServerTest.php | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/tests/e2e/Services/Functions/FunctionsCustomServerTest.php b/tests/e2e/Services/Functions/FunctionsCustomServerTest.php index 9031faf8d0..bb97c2eb79 100644 --- a/tests/e2e/Services/Functions/FunctionsCustomServerTest.php +++ b/tests/e2e/Services/Functions/FunctionsCustomServerTest.php @@ -1569,6 +1569,19 @@ class FunctionsCustomServerTest extends Scope $this->assertEquals(200, $response['headers']['status-code']); $this->assertEquals($cookie, $response['body']); + // Test Metrics + $response = $this->client->call(Client::METHOD_GET, '/functions/' . $functionId . '/usage', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'] + ], $this->getHeaders()), [ + 'range' => '24h' + ]); + + $this->assertEquals(200, $response['headers']['status-code']); + $this->assertEquals(19, count($response['body'])); + $this->assertEquals('24h', $response['body']['range']); + $this->assertEquals(1, $response['body']['executions']); + // Cleanup : Delete function $response = $this->client->call(Client::METHOD_DELETE, '/functions/' . $functionId, [ 'content-type' => 'application/json', From 7be4196a5818de2b0d4367e644c22f3539e04f92 Mon Sep 17 00:00:00 2001 From: Christy Jacob Date: Sat, 3 Aug 2024 07:22:34 +0000 Subject: [PATCH 3/6] chore: refactor code --- app/controllers/general.php | 6 ++---- composer.lock | 34 +++++++++++++++++----------------- 2 files changed, 19 insertions(+), 21 deletions(-) diff --git a/app/controllers/general.php b/app/controllers/general.php index bdd11115f8..43f19f9c02 100644 --- a/app/controllers/general.php +++ b/app/controllers/general.php @@ -312,16 +312,14 @@ function router(App $utopia, Database $dbForConsole, callable $getProjectDB, Swo ->addMetric(str_replace('{functionInternalId}', $function->getInternalId(), METRIC_FUNCTION_ID_EXECUTIONS_COMPUTE), (int)($execution->getAttribute('duration') * 1000)) // per function ->addMetric(METRIC_EXECUTIONS_MB_SECONDS, (int)(512 * $execution->getAttribute('duration', 0))) ->addMetric(str_replace('{functionInternalId}', $function->getInternalId(), METRIC_FUNCTION_ID_EXECUTIONS_MB_SECONDS), (int)(512 * $execution->getAttribute('duration', 0))) + ->setProject($project) + ->trigger() ; if ($function->getAttribute('logging')) { /** @var Document $execution */ $execution = Authorization::skip(fn () => $dbForProject->createDocument('executions', $execution)); } - - $queueForUsage - ->setProject($project) - ->trigger(); } $execution->setAttribute('logs', ''); diff --git a/composer.lock b/composer.lock index c44d9ad2d2..02bb9e7f4c 100644 --- a/composer.lock +++ b/composer.lock @@ -1720,16 +1720,16 @@ }, { "name": "utopia-php/database", - "version": "0.50.1", + "version": "0.50.2", "source": { "type": "git", "url": "https://github.com/utopia-php/database.git", - "reference": "1745147bef29a9bddf5dd03fd9174ec29e2c26f0" + "reference": "c712d1f6c8ec37886a7a1ad4d60a8cd75dec00aa" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/database/zipball/1745147bef29a9bddf5dd03fd9174ec29e2c26f0", - "reference": "1745147bef29a9bddf5dd03fd9174ec29e2c26f0", + "url": "https://api.github.com/repos/utopia-php/database/zipball/c712d1f6c8ec37886a7a1ad4d60a8cd75dec00aa", + "reference": "c712d1f6c8ec37886a7a1ad4d60a8cd75dec00aa", "shasum": "" }, "require": { @@ -1770,9 +1770,9 @@ ], "support": { "issues": "https://github.com/utopia-php/database/issues", - "source": "https://github.com/utopia-php/database/tree/0.50.1" + "source": "https://github.com/utopia-php/database/tree/0.50.2" }, - "time": "2024-07-26T11:56:05+00:00" + "time": "2024-07-31T10:12:19+00:00" }, { "name": "utopia-php/domains", @@ -1922,16 +1922,16 @@ }, { "name": "utopia-php/framework", - "version": "0.33.6", + "version": "0.33.7", "source": { "type": "git", "url": "https://github.com/utopia-php/http.git", - "reference": "8fe57da0cecd57e3b17cd395b4a666a24f4c07a6" + "reference": "78d293d99a262bd63ece750bbf989c7e0643b825" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/http/zipball/8fe57da0cecd57e3b17cd395b4a666a24f4c07a6", - "reference": "8fe57da0cecd57e3b17cd395b4a666a24f4c07a6", + "url": "https://api.github.com/repos/utopia-php/http/zipball/78d293d99a262bd63ece750bbf989c7e0643b825", + "reference": "78d293d99a262bd63ece750bbf989c7e0643b825", "shasum": "" }, "require": { @@ -1961,9 +1961,9 @@ ], "support": { "issues": "https://github.com/utopia-php/http/issues", - "source": "https://github.com/utopia-php/http/tree/0.33.6" + "source": "https://github.com/utopia-php/http/tree/0.33.7" }, - "time": "2024-03-21T18:10:57+00:00" + "time": "2024-08-01T14:01:04+00:00" }, { "name": "utopia-php/image", @@ -3158,16 +3158,16 @@ }, { "name": "laravel/pint", - "version": "v1.17.0", + "version": "v1.17.1", "source": { "type": "git", "url": "https://github.com/laravel/pint.git", - "reference": "4dba80c1de4b81dc4c4fb10ea6f4781495eb29f5" + "reference": "b5b6f716db298671c1dfea5b1082ec2c0ae7064f" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/laravel/pint/zipball/4dba80c1de4b81dc4c4fb10ea6f4781495eb29f5", - "reference": "4dba80c1de4b81dc4c4fb10ea6f4781495eb29f5", + "url": "https://api.github.com/repos/laravel/pint/zipball/b5b6f716db298671c1dfea5b1082ec2c0ae7064f", + "reference": "b5b6f716db298671c1dfea5b1082ec2c0ae7064f", "shasum": "" }, "require": { @@ -3220,7 +3220,7 @@ "issues": "https://github.com/laravel/pint/issues", "source": "https://github.com/laravel/pint" }, - "time": "2024-07-23T16:40:20+00:00" + "time": "2024-08-01T09:06:33+00:00" }, { "name": "matthiasmullie/minify", From 32a1aaca24e79e11c85376fb42aadcbcf8d44348 Mon Sep 17 00:00:00 2001 From: Christy Jacob Date: Sat, 3 Aug 2024 07:49:11 +0000 Subject: [PATCH 4/6] chore: revertomposer --- composer.lock | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/composer.lock b/composer.lock index 02bb9e7f4c..c44d9ad2d2 100644 --- a/composer.lock +++ b/composer.lock @@ -1720,16 +1720,16 @@ }, { "name": "utopia-php/database", - "version": "0.50.2", + "version": "0.50.1", "source": { "type": "git", "url": "https://github.com/utopia-php/database.git", - "reference": "c712d1f6c8ec37886a7a1ad4d60a8cd75dec00aa" + "reference": "1745147bef29a9bddf5dd03fd9174ec29e2c26f0" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/database/zipball/c712d1f6c8ec37886a7a1ad4d60a8cd75dec00aa", - "reference": "c712d1f6c8ec37886a7a1ad4d60a8cd75dec00aa", + "url": "https://api.github.com/repos/utopia-php/database/zipball/1745147bef29a9bddf5dd03fd9174ec29e2c26f0", + "reference": "1745147bef29a9bddf5dd03fd9174ec29e2c26f0", "shasum": "" }, "require": { @@ -1770,9 +1770,9 @@ ], "support": { "issues": "https://github.com/utopia-php/database/issues", - "source": "https://github.com/utopia-php/database/tree/0.50.2" + "source": "https://github.com/utopia-php/database/tree/0.50.1" }, - "time": "2024-07-31T10:12:19+00:00" + "time": "2024-07-26T11:56:05+00:00" }, { "name": "utopia-php/domains", @@ -1922,16 +1922,16 @@ }, { "name": "utopia-php/framework", - "version": "0.33.7", + "version": "0.33.6", "source": { "type": "git", "url": "https://github.com/utopia-php/http.git", - "reference": "78d293d99a262bd63ece750bbf989c7e0643b825" + "reference": "8fe57da0cecd57e3b17cd395b4a666a24f4c07a6" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/http/zipball/78d293d99a262bd63ece750bbf989c7e0643b825", - "reference": "78d293d99a262bd63ece750bbf989c7e0643b825", + "url": "https://api.github.com/repos/utopia-php/http/zipball/8fe57da0cecd57e3b17cd395b4a666a24f4c07a6", + "reference": "8fe57da0cecd57e3b17cd395b4a666a24f4c07a6", "shasum": "" }, "require": { @@ -1961,9 +1961,9 @@ ], "support": { "issues": "https://github.com/utopia-php/http/issues", - "source": "https://github.com/utopia-php/http/tree/0.33.7" + "source": "https://github.com/utopia-php/http/tree/0.33.6" }, - "time": "2024-08-01T14:01:04+00:00" + "time": "2024-03-21T18:10:57+00:00" }, { "name": "utopia-php/image", @@ -3158,16 +3158,16 @@ }, { "name": "laravel/pint", - "version": "v1.17.1", + "version": "v1.17.0", "source": { "type": "git", "url": "https://github.com/laravel/pint.git", - "reference": "b5b6f716db298671c1dfea5b1082ec2c0ae7064f" + "reference": "4dba80c1de4b81dc4c4fb10ea6f4781495eb29f5" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/laravel/pint/zipball/b5b6f716db298671c1dfea5b1082ec2c0ae7064f", - "reference": "b5b6f716db298671c1dfea5b1082ec2c0ae7064f", + "url": "https://api.github.com/repos/laravel/pint/zipball/4dba80c1de4b81dc4c4fb10ea6f4781495eb29f5", + "reference": "4dba80c1de4b81dc4c4fb10ea6f4781495eb29f5", "shasum": "" }, "require": { @@ -3220,7 +3220,7 @@ "issues": "https://github.com/laravel/pint/issues", "source": "https://github.com/laravel/pint" }, - "time": "2024-08-01T09:06:33+00:00" + "time": "2024-07-23T16:40:20+00:00" }, { "name": "matthiasmullie/minify", From 4801fb816abacfd65fc989301795531fabc1c760 Mon Sep 17 00:00:00 2001 From: Bradley Schofield Date: Mon, 5 Aug 2024 12:21:10 +0900 Subject: [PATCH 5/6] Fix Tests --- .../e2e/Services/Functions/FunctionsCustomServerTest.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/tests/e2e/Services/Functions/FunctionsCustomServerTest.php b/tests/e2e/Services/Functions/FunctionsCustomServerTest.php index bb97c2eb79..e27159fce9 100644 --- a/tests/e2e/Services/Functions/FunctionsCustomServerTest.php +++ b/tests/e2e/Services/Functions/FunctionsCustomServerTest.php @@ -8,6 +8,7 @@ use Tests\E2E\Client; use Tests\E2E\Scopes\ProjectCustom; use Tests\E2E\Scopes\Scope; use Tests\E2E\Scopes\SideServer; +use Utopia\App; use Utopia\Database\Document; use Utopia\Database\Helpers\ID; use Utopia\Database\Query; @@ -1569,18 +1570,19 @@ class FunctionsCustomServerTest extends Scope $this->assertEquals(200, $response['headers']['status-code']); $this->assertEquals($cookie, $response['body']); - // Test Metrics + // Await Aggregation + sleep(App::getEnv('_APP_USAGE_AGGREGATION_INTERVAL', 30)); + $response = $this->client->call(Client::METHOD_GET, '/functions/' . $functionId . '/usage', array_merge([ 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'] ], $this->getHeaders()), [ 'range' => '24h' ]); - + $this->assertEquals(200, $response['headers']['status-code']); $this->assertEquals(19, count($response['body'])); $this->assertEquals('24h', $response['body']['range']); - $this->assertEquals(1, $response['body']['executions']); // Cleanup : Delete function $response = $this->client->call(Client::METHOD_DELETE, '/functions/' . $functionId, [ From 3a91fee41bd8c56b5042d0b672bbe4b1fd2f6f9c Mon Sep 17 00:00:00 2001 From: Bradley Schofield Date: Mon, 5 Aug 2024 12:24:27 +0900 Subject: [PATCH 6/6] Run Linter --- tests/e2e/Services/Functions/FunctionsCustomServerTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/e2e/Services/Functions/FunctionsCustomServerTest.php b/tests/e2e/Services/Functions/FunctionsCustomServerTest.php index e27159fce9..2d9ddb04e9 100644 --- a/tests/e2e/Services/Functions/FunctionsCustomServerTest.php +++ b/tests/e2e/Services/Functions/FunctionsCustomServerTest.php @@ -1579,7 +1579,7 @@ class FunctionsCustomServerTest extends Scope ], $this->getHeaders()), [ 'range' => '24h' ]); - + $this->assertEquals(200, $response['headers']['status-code']); $this->assertEquals(19, count($response['body'])); $this->assertEquals('24h', $response['body']['range']);