From 60a4563908ac0a5166e0fae1fa7450d042da8179 Mon Sep 17 00:00:00 2001 From: Steven Date: Thu, 25 Aug 2022 22:10:52 +0000 Subject: [PATCH] Fix usage tests --- app/controllers/api/users.php | 4 ++-- src/Appwrite/Usage/Calculators/TimeSeries.php | 9 ++++----- tests/e2e/General/UsageTest.php | 19 +++++++++++++------ 3 files changed, 19 insertions(+), 13 deletions(-) diff --git a/app/controllers/api/users.php b/app/controllers/api/users.php index eb7307dc7e..63978e5646 100644 --- a/app/controllers/api/users.php +++ b/app/controllers/api/users.php @@ -1109,7 +1109,7 @@ App::get('/v1/users/usage') ]; $metrics = [ - 'users.$all.requests.count', + 'users.$all.count.total', 'users.$all.requests.create', 'users.$all.requests.read', 'users.$all.requests.update', @@ -1161,7 +1161,7 @@ App::get('/v1/users/usage') $usage = new Document([ 'range' => $range, - 'usersCount' => $stats['users.$all.requests.count'] ?? [], + 'usersCount' => $stats['users.$all.count.total'] ?? [], 'usersCreate' => $stats['users.$all.requests.create'] ?? [], 'usersRead' => $stats['users.$all.requests.read'] ?? [], 'usersUpdate' => $stats['users.$all.requests.update'] ?? [], diff --git a/src/Appwrite/Usage/Calculators/TimeSeries.php b/src/Appwrite/Usage/Calculators/TimeSeries.php index 7da4254c94..c2a6b4a141 100644 --- a/src/Appwrite/Usage/Calculators/TimeSeries.php +++ b/src/Appwrite/Usage/Calculators/TimeSeries.php @@ -298,7 +298,7 @@ class TimeSeries extends Calculator * * @return void */ - private function createOrUpdateMetric(string $projectId, int $time, string $period, string $metric, int $value, int $type): void + private function createOrUpdateMetric(string $projectId, string $time, string $period, string $metric, int $value, int $type): void { $id = \md5("{$time}_{$period}_{$metric}"); $this->database->setNamespace('_console'); @@ -347,9 +347,9 @@ class TimeSeries extends Calculator { $start = DateTime::createFromFormat('U', \strtotime($period['startTime']))->format(DateTime::RFC3339); if (!empty($this->latestTime[$metric][$period['key']])) { - $start = DateTime::createFromFormat('U', $this->latestTime[$metric][$period['key']])->format(DateTime::RFC3339); + $start = $this->latestTime[$metric][$period['key']]; } - $end = DateTime::createFromFormat('U', \strtotime('now'))->format(DateTime::RFC3339); + $end = (new DateTime())->format(DateTime::RFC3339); $table = $options['table']; //Which influxdb table to query for this metric $groupBy = empty($options['groupBy']) ? '' : ', ' . implode(', ', array_map(fn($groupBy) => '"' . $groupBy . '" ', $options['groupBy'])); //Some sub level metrics may be grouped by other tags like collectionId, bucketId, etc @@ -387,12 +387,11 @@ class TimeSeries extends Calculator } } - $time = DateTime::createFromFormat('U', \strtotime($point['time']))->format(DateTime::RFC3339); $value = (!empty($point['value'])) ? $point['value'] : 0; $this->createOrUpdateMetric( $projectId, - $time, + $point['time'], $period['key'], $metricUpdated, $value, diff --git a/tests/e2e/General/UsageTest.php b/tests/e2e/General/UsageTest.php index c320fba76c..c21f29992c 100644 --- a/tests/e2e/General/UsageTest.php +++ b/tests/e2e/General/UsageTest.php @@ -8,6 +8,9 @@ use Tests\E2E\Scopes\Scope; use Tests\E2E\Scopes\SideServer; use CURLFile; use Tests\E2E\Services\Functions\FunctionsBase; +use Utopia\Database\DateTime; +use Utopia\Database\Permission; +use Utopia\Database\Role; class UsageTest extends Scope { @@ -291,9 +294,13 @@ class UsageTest extends Scope $res = $this->client->call(Client::METHOD_POST, '/databases/' . $databaseId . '/collections', $headers, [ 'collectionId' => 'unique()', 'name' => $name, - 'permission' => 'collection', - 'read' => ['role:all'], - 'write' => ['role:all'] + 'documentSecurity' => false, + 'permissions' => [ + Permission::read(Role::any()), + Permission::create(Role::any()), + Permission::update(Role::any()), + Permission::delete(Role::any()), + ], ]); $this->assertEquals($name, $res['body']['name']); $this->assertNotEmpty($res['body']['$id']); @@ -472,7 +479,7 @@ class UsageTest extends Scope $this->assertEquals(202, $deployment['headers']['status-code']); $this->assertNotEmpty($deployment['body']['$id']); - $this->assertIsInt($deployment['body']['$createdAt']); + $this->assertEquals(true, DateTime::isValid($deployment['body']['$createdAt'])); $this->assertEquals('index.php', $deployment['body']['entrypoint']); // Wait for deployment to build. @@ -482,8 +489,8 @@ class UsageTest extends Scope $this->assertEquals(200, $response['headers']['status-code']); $this->assertNotEmpty($response['body']['$id']); - $this->assertIsInt($response['body']['$createdAt']); - $this->assertIsInt($response['body']['$updatedAt']); + $this->assertEquals(true, DateTime::isValid($response['body']['$createdAt'])); + $this->assertEquals(true, DateTime::isValid($response['body']['$updatedAt'])); $this->assertEquals($deploymentId, $response['body']['deployment']); $execution = $this->client->call(Client::METHOD_POST, '/functions/' . $functionId . '/executions', $headers, [