1
0
Fork 0
mirror of synced 2024-05-17 11:12:41 +12:00
appwrite/tests/e2e/General/UsageTest.php

765 lines
33 KiB
PHP
Raw Normal View History

2022-06-30 16:27:11 +12:00
<?php
2022-07-04 21:31:14 +12:00
namespace Tests\E2E\General;
2022-08-31 11:35:46 +12:00
use Appwrite\Tests\Retry;
2022-06-30 16:27:11 +12:00
use Tests\E2E\Client;
use Tests\E2E\Scopes\ProjectCustom;
use Tests\E2E\Scopes\Scope;
use Tests\E2E\Scopes\SideServer;
2022-07-05 18:53:57 +12:00
use CURLFile;
2022-07-10 14:12:49 +12:00
use Tests\E2E\Services\Functions\FunctionsBase;
2022-08-26 10:10:52 +12:00
use Utopia\Database\DateTime;
use Utopia\Database\Permission;
use Utopia\Database\Role;
2022-06-30 16:27:11 +12:00
2022-06-30 21:40:27 +12:00
class UsageTest extends Scope
{
2022-06-30 16:27:11 +12:00
use ProjectCustom;
use SideServer;
2022-07-10 14:12:49 +12:00
use FunctionsBase;
2022-06-30 16:27:11 +12:00
2022-06-30 21:33:40 +12:00
protected string $projectId;
2022-06-30 21:40:27 +12:00
2022-07-27 20:28:23 +12:00
protected function setUp(): void
2022-06-30 21:40:27 +12:00
{
2022-07-27 20:28:23 +12:00
parent::setUp();
}
2022-06-30 16:27:11 +12:00
2022-09-16 11:48:09 +12:00
protected static string $formatTz = 'Y-m-d\TH:i:s.vP';
protected function validateDates(array $metrics): void
{
foreach ($metrics as $metric) {
$this->assertIsObject(\DateTime::createFromFormat("Y-m-d\TH:i:s.vP", $metric['date']));
}
}
public function testPrepareUsersStats(): array
2022-07-27 20:28:23 +12:00
{
2022-07-28 19:28:00 +12:00
$project = $this->getProject(true);
$projectId = $project['$id'];
$headers['x-appwrite-project'] = $project['$id'];
$headers['x-appwrite-key'] = $project['apiKey'];
$headers['content-type'] = 'application/json';
2022-07-04 01:10:26 +12:00
$usersCount = 0;
$requestsCount = 0;
2022-06-30 21:40:27 +12:00
for ($i = 0; $i < 10; $i++) {
2022-06-30 16:27:11 +12:00
$email = uniqid() . 'user@usage.test';
$password = 'password';
$name = uniqid() . 'User';
2022-07-28 19:28:00 +12:00
$res = $this->client->call(Client::METHOD_POST, '/users', $headers, [
2022-06-30 16:27:11 +12:00
'userId' => 'unique()',
'email' => $email,
'password' => $password,
'name' => $name,
]);
2022-06-30 21:33:40 +12:00
$this->assertEquals($email, $res['body']['email']);
$this->assertNotEmpty($res['body']['$id']);
2022-07-04 01:10:26 +12:00
$usersCount++;
$requestsCount++;
2022-07-03 14:42:13 +12:00
2022-07-04 00:11:43 +12:00
if ($i < 5) {
2022-07-03 14:42:13 +12:00
$userId = $res['body']['$id'];
2022-07-28 19:28:00 +12:00
$res = $this->client->call(Client::METHOD_GET, '/users/' . $userId, $headers);
2022-07-03 14:42:13 +12:00
$this->assertEquals($userId, $res['body']['$id']);
2022-07-28 19:28:00 +12:00
$res = $this->client->call(Client::METHOD_DELETE, '/users/' . $userId, $headers);
2022-07-03 14:42:13 +12:00
$this->assertEmpty($res['body']);
2022-07-04 01:10:26 +12:00
$requestsCount += 2;
$usersCount--;
2022-07-03 14:42:13 +12:00
}
2022-06-30 16:27:11 +12:00
}
2022-07-03 14:42:13 +12:00
return [
'projectId' => $projectId,
'headers' => $headers,
'usersCount' => $usersCount,
'requestsCount' => $requestsCount
];
}
/**
* @depends testPrepareUsersStats
*/
#[Retry(count: 1)]
public function testUsersStats(array $data): array
{
2022-07-06 17:16:25 +12:00
sleep(35);
2022-06-30 21:33:40 +12:00
$projectId = $data['projectId'];
$headers = $data['headers'];
$usersCount = $data['usersCount'];
$requestsCount = $data['requestsCount'];
2022-06-30 21:33:40 +12:00
// console request
2022-07-28 21:01:03 +12:00
$cheaders = [
2022-06-30 21:33:40 +12:00
'origin' => 'http://localhost',
'x-appwrite-project' => 'console',
'cookie' => 'a_session_console=' . $this->getRoot()['session'],
];
2022-06-30 21:40:27 +12:00
2022-07-28 21:01:03 +12:00
$res = $this->client->call(Client::METHOD_GET, '/projects/' . $projectId . '/usage?range=30d', $cheaders);
2022-06-30 21:33:40 +12:00
$res = $res['body'];
2022-11-09 02:38:43 +13:00
$this->assertEquals(9, count($res));
2022-06-30 21:33:40 +12:00
$this->assertEquals(30, count($res['requests']));
$this->assertEquals(30, count($res['users']));
2022-07-04 01:10:26 +12:00
$this->assertEquals($usersCount, $res['users'][array_key_last($res['users'])]['value']);
2022-09-16 11:48:09 +12:00
$this->validateDates($res['users']);
2022-07-04 01:10:26 +12:00
$this->assertEquals($requestsCount, $res['requests'][array_key_last($res['requests'])]['value']);
2022-09-16 11:48:09 +12:00
$this->validateDates($res['requests']);
2022-07-04 01:10:26 +12:00
2022-07-28 21:01:03 +12:00
$res = $this->client->call(Client::METHOD_GET, '/users/usage?range=30d', array_merge($cheaders, [
2022-07-28 19:28:00 +12:00
'x-appwrite-project' => $projectId,
2022-07-04 01:10:26 +12:00
'x-appwrite-mode' => 'admin'
]));
$res = $res['body'];
$this->assertEquals(10, $res['usersCreate'][array_key_last($res['usersCreate'])]['value']);
2022-09-16 11:48:09 +12:00
$this->validateDates($res['usersCreate']);
2022-07-04 01:10:26 +12:00
$this->assertEquals(5, $res['usersRead'][array_key_last($res['usersRead'])]['value']);
2022-09-16 11:48:09 +12:00
$this->validateDates($res['usersRead']);
2022-07-04 01:10:26 +12:00
$this->assertEquals(5, $res['usersDelete'][array_key_last($res['usersDelete'])]['value']);
2022-09-16 11:48:09 +12:00
$this->validateDates($res['usersDelete']);
2022-07-28 19:28:00 +12:00
2022-07-28 21:01:03 +12:00
return ['projectId' => $projectId, 'headers' => $headers, 'requestsCount' => $requestsCount];
2022-07-04 01:10:26 +12:00
}
2022-07-28 19:28:00 +12:00
/** @depends testUsersStats */
public function testPrepareStorageStats(array $data): array
2022-07-04 21:00:07 +12:00
{
2022-07-28 19:28:00 +12:00
$projectId = $data['projectId'];
$headers = $data['headers'];
2022-07-04 01:10:26 +12:00
$bucketId = '';
$bucketsCount = 0;
2022-07-28 21:01:03 +12:00
$requestsCount = $data['requestsCount'];
2022-07-04 01:10:26 +12:00
$storageTotal = 0;
$bucketsCreate = 0;
$bucketsDelete = 0;
$bucketsRead = 0;
$filesCount = 0;
2022-07-04 21:00:07 +12:00
$filesRead = 0;
$filesCreate = 0;
$filesDelete = 0;
2022-07-04 01:10:26 +12:00
for ($i = 0; $i < 10; $i++) {
$name = uniqid() . ' bucket';
2022-07-28 19:28:00 +12:00
$res = $this->client->call(Client::METHOD_POST, '/storage/buckets', $headers, [
2022-07-04 01:10:26 +12:00
'bucketId' => 'unique()',
'name' => $name,
'permission' => 'bucket'
]);
$this->assertEquals($name, $res['body']['name']);
$this->assertNotEmpty($res['body']['$id']);
$bucketId = $res['body']['$id'];
2022-07-04 21:00:07 +12:00
2022-07-04 01:10:26 +12:00
$bucketsCreate++;
$bucketsCount++;
$requestsCount++;
if ($i < 5) {
2022-07-28 19:28:00 +12:00
$res = $this->client->call(Client::METHOD_GET, '/storage/buckets/' . $bucketId, $headers);
2022-07-04 01:10:26 +12:00
$this->assertEquals($bucketId, $res['body']['$id']);
$bucketsRead++;
2022-07-28 19:28:00 +12:00
$res = $this->client->call(Client::METHOD_DELETE, '/storage/buckets/' . $bucketId, $headers);
2022-07-04 01:10:26 +12:00
$this->assertEmpty($res['body']);
$bucketsDelete++;
2022-07-04 21:00:07 +12:00
2022-07-04 01:10:26 +12:00
$requestsCount += 2;
$bucketsCount--;
}
}
// upload some files
2022-07-04 21:00:07 +12:00
$files = [
[
'path' => realpath(__DIR__ . '/../../resources/logo.png'),
'name' => 'logo.png',
],
[
'path' => realpath(__DIR__ . '/../../resources/file.png'),
'name' => 'file.png',
],
[
'path' => realpath(__DIR__ . '/../../resources/disk-a/kitten-3.gif'),
'name' => 'kitten-3.gif',
],
[
'path' => realpath(__DIR__ . '/../../resources/disk-a/kitten-1.jpg'),
'name' => 'kitten-1.jpg',
],
2022-09-16 11:48:09 +12:00
];
2022-07-04 21:00:07 +12:00
2022-07-04 01:10:26 +12:00
for ($i = 0; $i < 10; $i++) {
2022-07-04 21:00:07 +12:00
$file = $files[$i % count($files)];
2022-07-28 19:28:00 +12:00
$res = $this->client->call(Client::METHOD_POST, '/storage/buckets/' . $bucketId . '/files', array_merge($headers, ['content-type' => 'multipart/form-data']), [
2022-07-04 01:10:26 +12:00
'fileId' => 'unique()',
2022-07-04 21:00:07 +12:00
'file' => new CURLFile($file['path'], '', $file['name']),
2022-07-04 01:10:26 +12:00
]);
$this->assertNotEmpty($res['body']['$id']);
2022-07-04 21:00:07 +12:00
$fileSize = $res['body']['sizeOriginal'];
$storageTotal += $fileSize;
2022-07-04 01:10:26 +12:00
$filesCount++;
2022-07-04 21:00:07 +12:00
$filesCreate++;
2022-07-04 01:10:26 +12:00
$requestsCount++;
2022-07-04 21:00:07 +12:00
$fileId = $res['body']['$id'];
2022-07-04 01:10:26 +12:00
if ($i < 5) {
2022-07-28 19:28:00 +12:00
$res = $this->client->call(Client::METHOD_GET, '/storage/buckets/' . $bucketId . '/files/' . $fileId, $headers);
2022-07-04 21:00:07 +12:00
$this->assertEquals($fileId, $res['body']['$id']);
$filesRead++;
2022-07-28 19:28:00 +12:00
$res = $this->client->call(Client::METHOD_DELETE, '/storage/buckets/' . $bucketId . '/files/' . $fileId, $headers);
2022-07-04 01:10:26 +12:00
$this->assertEmpty($res['body']);
2022-07-04 21:00:07 +12:00
$filesDelete++;
2022-07-04 01:10:26 +12:00
$requestsCount += 2;
$filesCount--;
2022-07-04 21:00:07 +12:00
$storageTotal -= $fileSize;
2022-07-04 01:10:26 +12:00
}
}
2022-07-04 21:00:07 +12:00
return array_merge($data, [
'bucketId' => $bucketId,
'bucketsCount' => $bucketsCount,
'requestsCount' => $requestsCount,
'storageTotal' => $storageTotal,
'bucketsCreate' => $bucketsCreate,
'bucketsDelete' => $bucketsDelete,
'bucketsRead' => $bucketsRead,
'filesCount' => $filesCount,
'filesRead' => $filesRead,
'filesCreate' => $filesCreate,
'filesDelete' => $filesDelete,
]);
}
/**
* @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'];
$bucketsRead = $data['bucketsRead'];
$filesCount = $data['filesCount'];
$filesRead = $data['filesRead'];
$filesCreate = $data['filesCreate'];
$filesDelete = $data['filesDelete'];
2022-07-06 17:16:25 +12:00
sleep(35);
2022-07-04 01:10:26 +12:00
// console request
$headers = [
'origin' => 'http://localhost',
'x-appwrite-project' => 'console',
'cookie' => 'a_session_console=' . $this->getRoot()['session'],
];
2022-07-28 19:28:00 +12:00
$res = $this->client->call(Client::METHOD_GET, '/projects/' . $projectId . '/usage?range=30d', $headers);
2022-07-04 01:10:26 +12:00
$res = $res['body'];
2022-11-09 03:16:10 +13:00
$this->assertEquals(9, count($res));
2022-07-04 01:10:26 +12:00
$this->assertEquals(30, count($res['requests']));
2022-07-04 21:00:07 +12:00
$this->assertEquals(30, count($res['storage']));
2022-07-04 01:10:26 +12:00
$this->assertEquals($requestsCount, $res['requests'][array_key_last($res['requests'])]['value']);
2022-09-16 11:48:09 +12:00
$this->validateDates($res['requests']);
2022-07-04 21:00:07 +12:00
$this->assertEquals($storageTotal, $res['storage'][array_key_last($res['storage'])]['value']);
2022-09-16 11:48:09 +12:00
$this->validateDates($res['storage']);
2022-07-03 14:42:13 +12:00
2022-07-04 21:00:07 +12:00
$res = $this->client->call(Client::METHOD_GET, '/storage/usage?range=30d', array_merge($headers, [
2022-07-28 19:28:00 +12:00
'x-appwrite-project' => $projectId,
2022-07-03 14:42:13 +12:00
'x-appwrite-mode' => 'admin'
]));
$res = $res['body'];
2022-08-13 15:44:57 +12:00
$this->assertEquals($storageTotal, $res['storage'][array_key_last($res['storage'])]['value']);
2022-09-16 11:48:09 +12:00
$this->validateDates($res['storage']);
2022-07-04 21:00:07 +12:00
$this->assertEquals($bucketsCount, $res['bucketsCount'][array_key_last($res['bucketsCount'])]['value']);
2022-09-16 11:48:09 +12:00
$this->validateDates($res['bucketsCount']);
2022-07-04 21:00:07 +12:00
$this->assertEquals($bucketsRead, $res['bucketsRead'][array_key_last($res['bucketsRead'])]['value']);
2022-09-16 11:48:09 +12:00
$this->validateDates($res['bucketsRead']);
2022-07-04 21:00:07 +12:00
$this->assertEquals($bucketsCreate, $res['bucketsCreate'][array_key_last($res['bucketsCreate'])]['value']);
2022-09-16 11:48:09 +12:00
$this->validateDates($res['bucketsCreate']);
2022-07-04 21:00:07 +12:00
$this->assertEquals($bucketsDelete, $res['bucketsDelete'][array_key_last($res['bucketsDelete'])]['value']);
2022-09-16 11:48:09 +12:00
$this->validateDates($res['bucketsDelete']);
2022-07-04 21:00:07 +12:00
$this->assertEquals($filesCount, $res['filesCount'][array_key_last($res['filesCount'])]['value']);
2022-09-16 11:48:09 +12:00
$this->validateDates($res['filesCount']);
2022-07-04 21:00:07 +12:00
$this->assertEquals($filesRead, $res['filesRead'][array_key_last($res['filesRead'])]['value']);
2022-09-16 11:48:09 +12:00
$this->validateDates($res['filesRead']);
2022-07-04 21:00:07 +12:00
$this->assertEquals($filesCreate, $res['filesCreate'][array_key_last($res['filesCreate'])]['value']);
2022-09-16 11:48:09 +12:00
$this->validateDates($res['filesCreate']);
2022-07-04 21:00:07 +12:00
$this->assertEquals($filesDelete, $res['filesDelete'][array_key_last($res['filesDelete'])]['value']);
2022-09-16 11:48:09 +12:00
$this->validateDates($res['filesDelete']);
2022-07-04 21:00:07 +12:00
$res = $this->client->call(Client::METHOD_GET, '/storage/' . $bucketId . '/usage?range=30d', array_merge($headers, [
2022-07-28 19:28:00 +12:00
'x-appwrite-project' => $projectId,
2022-07-04 21:00:07 +12:00
'x-appwrite-mode' => 'admin'
]));
$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($filesRead, $res['filesRead'][array_key_last($res['filesRead'])]['value']);
$this->assertEquals($filesCreate, $res['filesCreate'][array_key_last($res['filesCreate'])]['value']);
$this->assertEquals($filesDelete, $res['filesDelete'][array_key_last($res['filesDelete'])]['value']);
2022-07-28 19:28:00 +12:00
2022-07-28 21:01:03 +12:00
$data['requestsCount'] = $requestsCount;
2022-07-28 19:28:00 +12:00
return $data;
2022-06-30 16:27:11 +12:00
}
2022-07-28 21:01:03 +12:00
/** @depends testStorageStats */
public function testPrepareDatabaseStats(array $data): array
2022-07-05 18:53:57 +12:00
{
2022-07-28 19:28:00 +12:00
$headers = $data['headers'];
$projectId = $data['projectId'];
2022-07-05 18:53:57 +12:00
$databaseId = '';
$collectionId = '';
2022-07-05 19:11:27 +12:00
2022-07-28 21:01:03 +12:00
$requestsCount = $data['requestsCount'];
2022-07-05 18:53:57 +12:00
$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';
2022-07-28 19:28:00 +12:00
$res = $this->client->call(Client::METHOD_POST, '/databases', $headers, [
2022-07-05 18:53:57 +12:00
'databaseId' => 'unique()',
'name' => $name,
]);
$this->assertEquals($name, $res['body']['name']);
$this->assertNotEmpty($res['body']['$id']);
$databaseId = $res['body']['$id'];
$requestsCount++;
$databasesCount++;
$databasesCreate++;
2022-07-05 19:11:27 +12:00
if ($i < 5) {
2022-07-28 19:28:00 +12:00
$res = $this->client->call(Client::METHOD_GET, '/databases/' . $databaseId, $headers);
2022-07-05 18:53:57 +12:00
$this->assertEquals($databaseId, $res['body']['$id']);
$databasesRead++;
2022-07-28 19:28:00 +12:00
$res = $this->client->call(Client::METHOD_DELETE, '/databases/' . $databaseId, $headers);
2022-07-05 18:53:57 +12:00
$this->assertEmpty($res['body']);
$databasesDelete++;
$databasesCount--;
$requestsCount += 2;
}
}
for ($i = 0; $i < 10; $i++) {
$name = uniqid() . ' collection';
2022-07-28 19:28:00 +12:00
$res = $this->client->call(Client::METHOD_POST, '/databases/' . $databaseId . '/collections', $headers, [
2022-07-05 18:53:57 +12:00
'collectionId' => 'unique()',
'name' => $name,
2022-08-26 10:10:52 +12:00
'documentSecurity' => false,
'permissions' => [
Permission::read(Role::any()),
Permission::create(Role::any()),
Permission::update(Role::any()),
Permission::delete(Role::any()),
],
2022-07-05 18:53:57 +12:00
]);
$this->assertEquals($name, $res['body']['name']);
$this->assertNotEmpty($res['body']['$id']);
$collectionId = $res['body']['$id'];
$requestsCount++;
$collectionsCount++;
$collectionsCreate++;
2022-07-05 19:11:27 +12:00
if ($i < 5) {
2022-07-28 19:28:00 +12:00
$res = $this->client->call(Client::METHOD_GET, '/databases/' . $databaseId . '/collections/' . $collectionId, $headers);
2022-07-05 18:53:57 +12:00
$this->assertEquals($collectionId, $res['body']['$id']);
$collectionsRead++;
2022-07-28 19:28:00 +12:00
$res = $this->client->call(Client::METHOD_DELETE, '/databases/' . $databaseId . '/collections/' . $collectionId, $headers);
2022-07-05 18:53:57 +12:00
$this->assertEmpty($res['body']);
$collectionsDelete++;
$collectionsCount--;
$requestsCount += 2;
}
}
2022-07-28 19:28:00 +12:00
$res = $this->client->call(Client::METHOD_POST, '/databases/' . $databaseId . '/collections/' . $collectionId . '/attributes' . '/string', $headers, [
2022-07-05 18:53:57 +12:00
'key' => 'name',
'size' => 255,
'required' => true,
]);
$this->assertEquals('name', $res['body']['key']);
$collectionsUpdate++;
$requestsCount++;
sleep(10);
for ($i = 0; $i < 10; $i++) {
$name = uniqid() . ' collection';
2022-07-28 19:28:00 +12:00
$res = $this->client->call(Client::METHOD_POST, '/databases/' . $databaseId . '/collections/' . $collectionId . '/documents', $headers, [
2022-07-05 18:53:57 +12:00
'documentId' => 'unique()',
'data' => ['name' => $name]
]);
$this->assertEquals($name, $res['body']['name']);
$this->assertNotEmpty($res['body']['$id']);
$documentId = $res['body']['$id'];
$requestsCount++;
$documentsCount++;
$documentsCreate++;
2022-07-05 19:11:27 +12:00
if ($i < 5) {
2022-07-28 19:28:00 +12:00
$res = $this->client->call(Client::METHOD_GET, '/databases/' . $databaseId . '/collections/' . $collectionId . '/documents/' . $documentId, $headers);
2022-07-05 18:53:57 +12:00
$this->assertEquals($documentId, $res['body']['$id']);
$documentsRead++;
2022-07-28 19:28:00 +12:00
$res = $this->client->call(Client::METHOD_DELETE, '/databases/' . $databaseId . '/collections/' . $collectionId . '/documents/' . $documentId, $headers);
2022-07-05 18:53:57 +12:00
$this->assertEmpty($res['body']);
$documentsDelete++;
$documentsCount--;
$requestsCount += 2;
}
}
$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'];
$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'];
2022-07-06 13:56:47 +12:00
sleep(35);
2022-07-05 18:53:57 +12:00
// check datbase stats
$headers = [
'origin' => 'http://localhost',
'x-appwrite-project' => 'console',
'cookie' => 'a_session_console=' . $this->getRoot()['session'],
];
2022-07-28 19:28:00 +12:00
$res = $this->client->call(Client::METHOD_GET, '/projects/' . $projectId . '/usage?range=30d', $headers);
2022-07-05 18:53:57 +12:00
$res = $res['body'];
2022-11-09 03:16:10 +13:00
$this->assertEquals(9, count($res));
2022-07-05 18:53:57 +12:00
$this->assertEquals(30, count($res['requests']));
$this->assertEquals(30, count($res['storage']));
$this->assertEquals($requestsCount, $res['requests'][array_key_last($res['requests'])]['value']);
2022-09-16 11:48:09 +12:00
$this->validateDates($res['requests']);
2022-11-09 22:29:13 +13:00
$this->assertEquals($databasesCount, $res['databases'][array_key_last($res['databases'])]['value']);
$this->validateDates($res['databases']);
2022-07-05 18:53:57 +12:00
$this->assertEquals($documentsCount, $res['documents'][array_key_last($res['documents'])]['value']);
2022-09-16 11:48:09 +12:00
$this->validateDates($res['documents']);
2022-07-05 18:53:57 +12:00
$res = $this->client->call(Client::METHOD_GET, '/databases/usage?range=30d', array_merge($headers, [
2022-07-28 19:28:00 +12:00
'x-appwrite-project' => $projectId,
2022-07-05 18:53:57 +12:00
'x-appwrite-mode' => 'admin'
]));
$res = $res['body'];
$this->assertEquals($databasesCount, $res['databasesCount'][array_key_last($res['databasesCount'])]['value']);
2022-09-16 11:48:09 +12:00
$this->validateDates($res['databasesCount']);
2022-07-05 18:53:57 +12:00
$this->assertEquals($collectionsCount, $res['collectionsCount'][array_key_last($res['collectionsCount'])]['value']);
2022-09-16 11:48:09 +12:00
$this->validateDates($res['collectionsCount']);
2022-07-05 18:53:57 +12:00
$this->assertEquals($documentsCount, $res['documentsCount'][array_key_last($res['documentsCount'])]['value']);
2022-09-16 11:48:09 +12:00
$this->validateDates($res['documentsCount']);
2022-07-05 18:53:57 +12:00
2022-07-06 13:56:47 +12:00
$this->assertEquals($databasesCreate, $res['databasesCreate'][array_key_last($res['databasesCreate'])]['value']);
2022-09-16 11:48:09 +12:00
$this->validateDates($res['databasesCreate']);
2022-07-05 18:53:57 +12:00
$this->assertEquals($databasesRead, $res['databasesRead'][array_key_last($res['databasesRead'])]['value']);
2022-09-16 11:48:09 +12:00
$this->validateDates($res['databasesRead']);
2022-07-06 13:56:47 +12:00
$this->assertEquals($databasesDelete, $res['databasesDelete'][array_key_last($res['databasesDelete'])]['value']);
2022-09-16 11:48:09 +12:00
$this->validateDates($res['databasesDelete']);
2022-07-05 19:11:27 +12:00
2022-07-05 18:53:57 +12:00
$this->assertEquals($collectionsCreate, $res['collectionsCreate'][array_key_last($res['collectionsCreate'])]['value']);
2022-09-16 11:48:09 +12:00
$this->validateDates($res['collectionsCreate']);
2022-07-05 18:53:57 +12:00
$this->assertEquals($collectionsRead, $res['collectionsRead'][array_key_last($res['collectionsRead'])]['value']);
2022-09-16 11:48:09 +12:00
$this->validateDates($res['collectionsRead']);
2022-07-05 18:53:57 +12:00
$this->assertEquals($collectionsUpdate, $res['collectionsUpdate'][array_key_last($res['collectionsUpdate'])]['value']);
2022-09-16 11:48:09 +12:00
$this->validateDates($res['collectionsUpdate']);
2022-07-05 18:53:57 +12:00
$this->assertEquals($collectionsDelete, $res['collectionsDelete'][array_key_last($res['collectionsDelete'])]['value']);
2022-09-16 11:48:09 +12:00
$this->validateDates($res['collectionsDelete']);
2022-07-05 19:11:27 +12:00
2022-07-05 18:53:57 +12:00
$this->assertEquals($documentsCreate, $res['documentsCreate'][array_key_last($res['documentsCreate'])]['value']);
2022-09-16 11:48:09 +12:00
$this->validateDates($res['documentsCreate']);
2022-07-05 18:53:57 +12:00
$this->assertEquals($documentsRead, $res['documentsRead'][array_key_last($res['documentsRead'])]['value']);
2022-09-16 11:48:09 +12:00
$this->validateDates($res['documentsRead']);
2022-07-05 18:53:57 +12:00
$this->assertEquals($documentsDelete, $res['documentsDelete'][array_key_last($res['documentsDelete'])]['value']);
2022-09-16 11:48:09 +12:00
$this->validateDates($res['documentsDelete']);
2022-07-05 18:53:57 +12:00
$res = $this->client->call(Client::METHOD_GET, '/databases/' . $databaseId . '/usage?range=30d', array_merge($headers, [
2022-07-28 19:28:00 +12:00
'x-appwrite-project' => $projectId,
2022-07-05 18:53:57 +12:00
'x-appwrite-mode' => 'admin'
]));
$res = $res['body'];
$this->assertEquals($collectionsCount, $res['collectionsCount'][array_key_last($res['collectionsCount'])]['value']);
2022-09-16 11:48:09 +12:00
$this->validateDates($res['collectionsCount']);
2022-07-05 18:53:57 +12:00
$this->assertEquals($documentsCount, $res['documentsCount'][array_key_last($res['documentsCount'])]['value']);
2022-09-16 11:48:09 +12:00
$this->validateDates($res['documentsCount']);
2022-07-05 18:53:57 +12:00
$this->assertEquals($collectionsCreate, $res['collectionsCreate'][array_key_last($res['collectionsCreate'])]['value']);
2022-09-16 11:48:09 +12:00
$this->validateDates($res['collectionsCreate']);
2022-07-05 18:53:57 +12:00
$this->assertEquals($collectionsRead, $res['collectionsRead'][array_key_last($res['collectionsRead'])]['value']);
2022-09-16 11:48:09 +12:00
$this->validateDates($res['collectionsRead']);
2022-07-05 18:53:57 +12:00
$this->assertEquals($collectionsUpdate, $res['collectionsUpdate'][array_key_last($res['collectionsUpdate'])]['value']);
2022-09-16 11:48:09 +12:00
$this->validateDates($res['collectionsUpdate']);
2022-07-05 18:53:57 +12:00
$this->assertEquals($collectionsDelete, $res['collectionsDelete'][array_key_last($res['collectionsDelete'])]['value']);
2022-09-16 11:48:09 +12:00
$this->validateDates($res['collectionsDelete']);
2022-07-05 19:11:27 +12:00
2022-07-05 18:53:57 +12:00
$this->assertEquals($documentsCreate, $res['documentsCreate'][array_key_last($res['documentsCreate'])]['value']);
2022-09-16 11:48:09 +12:00
$this->validateDates($res['documentsCreate']);
2022-07-05 18:53:57 +12:00
$this->assertEquals($documentsRead, $res['documentsRead'][array_key_last($res['documentsRead'])]['value']);
2022-09-16 11:48:09 +12:00
$this->validateDates($res['documentsRead']);
2022-07-05 18:53:57 +12:00
$this->assertEquals($documentsDelete, $res['documentsDelete'][array_key_last($res['documentsDelete'])]['value']);
2022-09-16 11:48:09 +12:00
$this->validateDates($res['documentsDelete']);
2022-07-05 18:53:57 +12:00
$res = $this->client->call(Client::METHOD_GET, '/databases/' . $databaseId . '/collections/' . $collectionId . '/usage?range=30d', array_merge($headers, [
2022-07-28 19:28:00 +12:00
'x-appwrite-project' => $projectId,
2022-07-05 18:53:57 +12:00
'x-appwrite-mode' => 'admin'
]));
$res = $res['body'];
$this->assertEquals($documentsCount, $res['documentsCount'][array_key_last($res['documentsCount'])]['value']);
2022-09-16 11:48:09 +12:00
$this->validateDates($res['documentsCount']);
2022-07-05 18:53:57 +12:00
$this->assertEquals($documentsCreate, $res['documentsCreate'][array_key_last($res['documentsCreate'])]['value']);
2022-09-16 11:48:09 +12:00
$this->validateDates($res['documentsCreate']);
2022-07-05 18:53:57 +12:00
$this->assertEquals($documentsRead, $res['documentsRead'][array_key_last($res['documentsRead'])]['value']);
2022-09-16 11:48:09 +12:00
$this->validateDates($res['documentsRead']);
2022-07-05 18:53:57 +12:00
$this->assertEquals($documentsDelete, $res['documentsDelete'][array_key_last($res['documentsDelete'])]['value']);
2022-09-16 11:48:09 +12:00
$this->validateDates($res['documentsDelete']);
2022-07-28 19:28:00 +12:00
2022-07-28 21:01:03 +12:00
$data['requestsCount'] = $requestsCount;
2022-07-28 19:28:00 +12:00
return $data;
2022-07-05 18:53:57 +12:00
}
2022-08-18 13:49:41 +12:00
2022-07-28 21:01:03 +12:00
/** @depends testDatabaseStats */
public function testPrepareFunctionsStats(array $data): array
2022-07-05 18:53:57 +12:00
{
2022-08-18 13:49:41 +12:00
$headers = $data['headers'];
2022-07-10 14:12:49 +12:00
$functionId = '';
2022-07-17 22:30:58 +12:00
$executionTime = 0;
2022-07-10 14:12:49 +12:00
$executions = 0;
$failures = 0;
2022-08-18 13:49:41 +12:00
$response1 = $this->client->call(Client::METHOD_POST, '/functions', $headers, [
2022-07-10 14:12:49 +12:00
'functionId' => 'unique()',
'name' => 'Test',
'runtime' => 'php-8.0',
'vars' => [
'funcKey1' => 'funcValue1',
'funcKey2' => 'funcValue2',
'funcKey3' => 'funcValue3',
],
'events' => [
'users.*.create',
'users.*.delete',
],
'schedule' => '0 0 1 1 *',
'timeout' => 10,
]);
$functionId = $response1['body']['$id'] ?? '';
$this->assertEquals(201, $response1['headers']['status-code']);
$this->assertNotEmpty($response1['body']['$id']);
2022-08-18 13:49:41 +12:00
$code = realpath(__DIR__ . '/../../resources/functions') . "/php/code.tar.gz";
$this->packageCode('php');
2022-07-10 14:12:49 +12:00
2022-08-18 13:49:41 +12:00
$deployment = $this->client->call(Client::METHOD_POST, '/functions/' . $functionId . '/deployments', array_merge($headers, ['content-type' => 'multipart/form-data',]), [
2022-07-10 14:12:49 +12:00
'entrypoint' => 'index.php',
'code' => new CURLFile($code, 'application/x-gzip', \basename($code)),
2022-07-17 21:33:24 +12:00
'activate' => true
2022-07-10 14:12:49 +12:00
]);
$deploymentId = $deployment['body']['$id'] ?? '';
2022-08-17 22:36:30 +12:00
$this->assertEquals(202, $deployment['headers']['status-code']);
2022-07-10 14:12:49 +12:00
$this->assertNotEmpty($deployment['body']['$id']);
2022-08-26 10:10:52 +12:00
$this->assertEquals(true, DateTime::isValid($deployment['body']['$createdAt']));
2022-07-10 14:12:49 +12:00
$this->assertEquals('index.php', $deployment['body']['entrypoint']);
// Wait for deployment to build.
sleep(30);
2022-07-17 21:33:24 +12:00
2022-08-18 13:49:41 +12:00
$response = $this->client->call(Client::METHOD_PATCH, '/functions/' . $functionId . '/deployments/' . $deploymentId, $headers, []);
2022-07-17 21:33:24 +12:00
$this->assertEquals(200, $response['headers']['status-code']);
$this->assertNotEmpty($response['body']['$id']);
2022-08-26 10:10:52 +12:00
$this->assertEquals(true, DateTime::isValid($response['body']['$createdAt']));
$this->assertEquals(true, DateTime::isValid($response['body']['$updatedAt']));
2022-07-17 21:33:24 +12:00
$this->assertEquals($deploymentId, $response['body']['deployment']);
2022-07-17 22:30:58 +12:00
2022-08-18 13:49:41 +12:00
$execution = $this->client->call(Client::METHOD_POST, '/functions/' . $functionId . '/executions', $headers, [
2022-07-10 14:12:49 +12:00
'async' => false,
]);
2022-07-17 22:30:58 +12:00
2022-07-10 14:12:49 +12:00
$this->assertEquals(201, $execution['headers']['status-code']);
$this->assertNotEmpty($execution['body']['$id']);
$this->assertEquals($functionId, $execution['body']['functionId']);
2022-09-10 08:19:09 +12:00
$executionTime += (int) ($execution['body']['duration'] * 1000);
2022-08-18 13:50:55 +12:00
if ($execution['body']['status'] == 'failed') {
2022-08-18 13:49:41 +12:00
$failures++;
2022-08-18 17:13:30 +12:00
} elseif ($execution['body']['status'] == 'completed') {
2022-08-18 13:49:41 +12:00
$executions++;
}
2022-07-17 22:30:58 +12:00
2022-08-18 13:49:41 +12:00
$execution = $this->client->call(Client::METHOD_POST, '/functions/' . $functionId . '/executions', $headers, [
2022-08-17 22:36:30 +12:00
'async' => false,
2022-07-17 21:33:24 +12:00
]);
2022-07-17 22:30:58 +12:00
2022-07-17 21:33:24 +12:00
$this->assertEquals(201, $execution['headers']['status-code']);
$this->assertNotEmpty($execution['body']['$id']);
$this->assertEquals($functionId, $execution['body']['functionId']);
2022-08-18 13:50:55 +12:00
if ($execution['body']['status'] == 'failed') {
2022-08-18 13:49:41 +12:00
$failures++;
2022-08-18 17:13:30 +12:00
} elseif ($execution['body']['status'] == 'completed') {
2022-08-18 13:49:41 +12:00
$executions++;
}
2022-09-10 09:47:24 +12:00
$executionTime += (int) ($execution['body']['duration'] * 1000);
2022-07-17 21:33:24 +12:00
$data = array_merge($data, [
'functionId' => $functionId,
'executionTime' => $executionTime,
'executions' => $executions,
'failures' => $failures,
]);
return $data;
}
/** @depends testPrepareFunctionsStats */
#[Retry(count: 1)]
public function testFunctionsStats(array $data): void
{
$headers = $data['headers'];
$functionId = $data['functionId'];
$executionTime = $data['executionTime'];
$executions = $data['executions'];
$failures = $data['failures'];
2022-08-17 22:36:30 +12:00
sleep(25);
2022-07-10 14:12:49 +12:00
2022-08-18 13:49:41 +12:00
$response = $this->client->call(Client::METHOD_GET, '/functions/' . $functionId . '/usage', $headers, [
2022-07-10 14:12:49 +12:00
'range' => '30d'
]);
2022-07-17 22:30:58 +12:00
$this->assertEquals(200, $response['headers']['status-code']);
2022-08-17 22:36:30 +12:00
$this->assertEquals(9, count($response['body']));
2022-07-17 22:30:58 +12:00
$this->assertEquals('30d', $response['body']['range']);
2022-08-17 22:36:30 +12:00
$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']);
2022-07-17 21:33:24 +12:00
$response = $response['body'];
2022-07-10 14:12:49 +12:00
2022-08-17 22:36:30 +12:00
$this->assertEquals($executions, $response['executionsTotal'][array_key_last($response['executionsTotal'])]['value']);
2022-09-16 11:48:09 +12:00
$this->validateDates($response['executionsTotal']);
2022-08-18 12:36:25 +12:00
$this->assertEquals($executionTime, $response['executionsTime'][array_key_last($response['executionsTime'])]['value']);
2022-09-16 11:48:09 +12:00
$this->validateDates($response['executionsTime']);
2022-08-17 22:36:30 +12:00
$this->assertEquals($failures, $response['executionsFailure'][array_key_last($response['executionsFailure'])]['value']);
2022-09-16 11:48:09 +12:00
$this->validateDates($response['executionsFailure']);
2022-07-10 14:12:49 +12:00
2022-08-18 13:49:41 +12:00
$response = $this->client->call(Client::METHOD_GET, '/functions/usage', $headers, [
2022-07-17 22:30:58 +12:00
'range' => '30d'
]);
$this->assertEquals(200, $response['headers']['status-code']);
2022-08-17 22:36:30 +12:00
$this->assertEquals(9, count($response['body']));
2022-07-17 22:30:58 +12:00
$this->assertEquals($response['body']['range'], '30d');
2022-08-17 22:36:30 +12:00
$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']);
2022-07-17 22:30:58 +12:00
$response = $response['body'];
2022-08-17 22:36:30 +12:00
$this->assertEquals($executions, $response['executionsTotal'][array_key_last($response['executionsTotal'])]['value']);
2022-09-16 11:48:09 +12:00
$this->validateDates($response['executionsTotal']);
2022-08-17 22:36:30 +12:00
$this->assertEquals($executionTime, $response['executionsTime'][array_key_last($response['executionsTime'])]['value']);
2022-09-16 11:48:09 +12:00
$this->validateDates($response['executionsTime']);
2022-08-17 22:36:30 +12:00
$this->assertGreaterThan(0, $response['buildsTime'][array_key_last($response['buildsTime'])]['value']);
2022-09-16 11:48:09 +12:00
$this->validateDates($response['buildsTime']);
2022-08-17 22:36:30 +12:00
$this->assertEquals($failures, $response['executionsFailure'][array_key_last($response['executionsFailure'])]['value']);
2022-09-16 11:48:09 +12:00
$this->validateDates($response['executionsFailure']);
2022-08-17 22:36:30 +12:00
}
2022-07-27 20:28:23 +12:00
protected function tearDown(): void
{
$this->usersCount = 0;
$this->requestsCount = 0;
2022-07-28 19:28:00 +12:00
$projectId = '';
$headers = [];
2022-07-27 20:28:23 +12:00
}
2022-06-30 21:40:27 +12:00
}