1
0
Fork 0
mirror of synced 2024-06-02 19:04:49 +12:00

Merge branch 'feat-database-indexing' into feat-db-search-attribute

This commit is contained in:
Eldad A. Fux 2021-08-14 21:58:56 +03:00 committed by GitHub
commit 52132c22b6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
38 changed files with 1173 additions and 445 deletions

View file

@ -238,7 +238,6 @@ RUN chmod +x /usr/local/bin/doctor && \
chmod +x /usr/local/bin/worker-deletes && \
chmod +x /usr/local/bin/worker-functions && \
chmod +x /usr/local/bin/worker-mails && \
chmod +x /usr/local/bin/worker-usage && \
chmod +x /usr/local/bin/worker-webhooks
# Letsencrypt Permissions

View file

@ -106,7 +106,7 @@ $attributesCallback = function ($attribute, $response, $dbForExternal, $database
]);
$database
->setParam('type', CREATE_TYPE_ATTRIBUTE)
->setParam('type', DATABASE_TYPE_CREATE_ATTRIBUTE)
->setParam('document', $attribute)
;
@ -182,10 +182,11 @@ App::get('/v1/database/collections')
->param('search', '', new Text(256), 'Search term to filter your list results. Max length: 256 chars.', true)
->param('limit', 25, new Range(0, 100), 'Results limit value. By default will return maximum 25 results. Maximum of 100 results allowed per request.', true)
->param('offset', 0, new Range(0, 40000), 'Results offset. The default value is 0. Use this param to manage pagination.', true)
->param('after', '', new UID(), 'ID of the collection used as the starting point for the query, excluding the collection itself. Should be used for efficient pagination when working with large sets of data.', true)
->param('orderType', 'ASC', new WhiteList(['ASC', 'DESC'], true), 'Order result by ASC or DESC order.', true)
->inject('response')
->inject('dbForExternal')
->action(function ($search, $limit, $offset, $orderType, $response, $dbForExternal) {
->action(function ($search, $limit, $offset, $after, $orderType, $response, $dbForExternal) {
/** @var Appwrite\Utopia\Response $response */
/** @var Utopia\Database\Database $dbForExternal */
@ -195,8 +196,16 @@ App::get('/v1/database/collections')
$queries[] = new Query('name', Query::TYPE_SEARCH, [$search]);
}
if (!empty($after)) {
$afterCollection = $dbForExternal->getDocument('collections', $after);
if ($afterCollection->isEmpty()) {
throw new Exception("Collection '{$after}' for the 'after' value not found.", 400);
}
}
$response->dynamic(new Document([
'collections' => $dbForExternal->find(Database::COLLECTIONS, $queries, $limit, $offset, ['_id'], [$orderType]),
'collections' => $dbForExternal->find(Database::COLLECTIONS, $queries, $limit, $offset, [], [$orderType], $afterCollection ?? null),
'sum' => $dbForExternal->count(Database::COLLECTIONS, $queries, APP_LIMIT_COUNT),
]), Response::MODEL_COLLECTION_LIST);
});
@ -721,24 +730,24 @@ App::delete('/v1/database/collections/:collectionId/attributes/:attributeId')
throw new Exception('Collection not found', 404);
}
$attributes = $collection->getAttributes();
/** @var Document[] $attributes */
$attributes = $collection->getAttribute('attributes');
// Search for attribute
$attributeIndex = array_search($attributeId, array_column($attributes, '$id'));
// find attribute in collection
$attribute = null;
foreach ($attributes as $a) {
if ($a->getId() === $attributeId) {
$attribute = $a->setAttribute('$collection', $collectionId); // set the collectionId
break; // break once attribute is found
}
}
if ($attributeIndex === false) {
if (\is_null($attribute)) {
throw new Exception('Attribute not found', 404);
}
$attribute = new Document([\array_merge($attributes[$attributeIndex], [
'collectionId' => $collectionId,
])]);
$type = $attribute->getAttribute('type', '');
$format = $attribute->getAttribute('format', '');
$database
->setParam('type', DELETE_TYPE_ATTRIBUTE)
->setParam('type', DATABASE_TYPE_DELETE_ATTRIBUTE)
->setParam('document', $attribute)
;
@ -768,7 +777,7 @@ App::post('/v1/database/collections/:collectionId/indexes')
->label('sdk.response.type', Response::CONTENT_TYPE_JSON)
->label('sdk.response.model', Response::MODEL_INDEX)
->param('collectionId', '', new UID(), 'Collection unique ID. You can create a new collection using the Database service [server integration](/docs/server/database#createCollection).')
->param('id', null, new Key(), 'Index ID.')
->param('indexId', null, new Key(), 'Index ID.')
->param('type', null, new WhiteList([Database::INDEX_KEY, Database::INDEX_FULLTEXT, Database::INDEX_UNIQUE, Database::INDEX_SPATIAL, Database::INDEX_ARRAY]), 'Index type.')
->param('attributes', null, new ArrayList(new Key()), 'Array of attributes to index.')
->param('orders', [], new ArrayList(new WhiteList(['ASC', 'DESC'], false, Database::VAR_STRING)), 'Array of index orders.', true)
@ -776,7 +785,7 @@ App::post('/v1/database/collections/:collectionId/indexes')
->inject('dbForExternal')
->inject('database')
->inject('audits')
->action(function ($collectionId, $id, $type, $attributes, $orders, $response, $dbForExternal, $database, $audits) {
->action(function ($collectionId, $indexId, $type, $attributes, $orders, $response, $dbForExternal, $database, $audits) {
/** @var Appwrite\Utopia\Response $response */
/** @var Utopia\Database\Database $dbForExternal */
/** @var Appwrite\Event\Event $database */
@ -812,7 +821,7 @@ App::post('/v1/database/collections/:collectionId/indexes')
$lengths[$key] = ($attributeType === Database::VAR_STRING) ? $attributeSize : null;
}
$success = $dbForExternal->addIndexInQueue($collectionId, $id, $type, $attributes, $lengths, $orders);
$success = $dbForExternal->addIndexInQueue($collectionId, $indexId, $type, $attributes, $lengths, $orders);
// Database->createIndex() does not return a document
// So we need to create one for the response
@ -820,7 +829,7 @@ App::post('/v1/database/collections/:collectionId/indexes')
// TODO@kodumbeats should $lengths be a part of the response model?
$index = new Document([
'$collection' => $collectionId,
'$id' => $id,
'$id' => $indexId,
'type' => $type,
'attributes' => $attributes,
'lengths' => $lengths,
@ -828,7 +837,7 @@ App::post('/v1/database/collections/:collectionId/indexes')
]);
$database
->setParam('type', CREATE_TYPE_INDEX)
->setParam('type', DATABASE_TYPE_CREATE_INDEX)
->setParam('document', $index)
;
@ -953,21 +962,24 @@ App::delete('/v1/database/collections/:collectionId/indexes/:indexId')
throw new Exception('Collection not found', 404);
}
/** @var Document[] $indexes */
$indexes = $collection->getAttribute('indexes');
// // Search for index
$indexIndex = array_search($indexId, array_column($indexes, '$id'));
// find attribute in collection
$index= null;
foreach ($indexes as $i) {
if ($i->getId() === $indexId) {
$index = $i->setAttribute('$collection', $collectionId); // set the collectionId
break; // break once index is found
}
}
if ($indexIndex === false) {
if (\is_null($index)) {
throw new Exception('Index not found', 404);
}
$index = new Document([\array_merge($indexes[$indexIndex], [
'collectionId' => $collectionId,
])]);
$database
->setParam('type', DELETE_TYPE_INDEX)
->setParam('type', DATABASE_TYPE_DELETE_INDEX)
->setParam('document', $index)
;
@ -1063,12 +1075,12 @@ App::get('/v1/database/collections/:collectionId/documents')
->param('queries', [], new ArrayList(new Text(128)), 'Array of query strings.', true)
->param('limit', 25, new Range(0, 100), 'Maximum number of documents to return in response. Use this value to manage pagination. By default will return maximum 25 results. Maximum of 100 results allowed per request.', true)
->param('offset', 0, new Range(0, 900000000), 'Offset value. The default value is 0. Use this param to manage pagination.', true)
// TODO@kodumbeats 'after' param for pagination
->param('after', '', new UID(), 'ID of the document used as the starting point for the query, excluding the document itself. Should be used for efficient pagination when working with large sets of data.', true)
->param('orderAttributes', [], new ArrayList(new Text(128)), 'Array of attributes used to sort results.', true)
->param('orderTypes', [], new ArrayList(new WhiteList(['DESC', 'ASC'], true)), 'Array of order directions for sorting attribtues. Possible values are DESC for descending order, or ASC for ascending order.', true)
->inject('response')
->inject('dbForExternal')
->action(function ($collectionId, $queries, $limit, $offset, $orderAttributes, $orderTypes, $response, $dbForExternal) {
->action(function ($collectionId, $queries, $limit, $offset, $after, $orderAttributes, $orderTypes, $response, $dbForExternal) {
/** @var Appwrite\Utopia\Response $response */
/** @var Utopia\Database\Database $dbForExternal */
@ -1094,7 +1106,15 @@ App::get('/v1/database/collections/:collectionId/documents')
throw new Exception($validator->getDescription(), 400);
}
$documents = $dbForExternal->find($collectionId, $queries, $limit, $offset, $orderAttributes, $orderTypes);
if (!empty($after)) {
$afterDocument = $dbForExternal->getDocument($collectionId, $after);
if ($afterDocument->isEmpty()) {
throw new Exception("Document '{$after}' for the 'after' value not found.", 400);
}
}
$documents = $dbForExternal->find($collectionId, $queries, $limit, $offset, $orderAttributes, $orderTypes, $afterDocument ?? null);
$response->dynamic(new Document([
'sum' => \count($documents),

View file

@ -90,10 +90,11 @@ App::get('/v1/functions')
->param('search', '', new Text(256), 'Search term to filter your list results. Max length: 256 chars.', true)
->param('limit', 25, new Range(0, 100), 'Results limit value. By default will return maximum 25 results. Maximum of 100 results allowed per request.', true)
->param('offset', 0, new Range(0, 2000), 'Results offset. The default value is 0. Use this param to manage pagination.', true)
->param('after', '', new UID(), 'ID of the function used as the starting point for the query, excluding the function itself. Should be used for efficient pagination when working with large sets of data.', true)
->param('orderType', 'ASC', new WhiteList(['ASC', 'DESC'], true), 'Order result by ASC or DESC order.', true)
->inject('response')
->inject('dbForInternal')
->action(function ($search, $limit, $offset, $orderType, $response, $dbForInternal) {
->action(function ($search, $limit, $offset, $after, $orderType, $response, $dbForInternal) {
/** @var Appwrite\Utopia\Response $response */
/** @var Utopia\Database\Database $dbForInternal */
@ -103,8 +104,16 @@ App::get('/v1/functions')
$queries[] = new Query('search', Query::TYPE_SEARCH, [$search]);
}
if (!empty($after)) {
$afterFunction = $dbForInternal->getDocument('functions', $after);
if ($afterFunction->isEmpty()) {
throw new Exception("Function '{$after}' for the 'after' value not found.", 400);
}
}
$response->dynamic(new Document([
'functions' => $dbForInternal->find('functions', $queries, $limit, $offset, ['_id'], [$orderType]),
'functions' => $dbForInternal->find('functions', $queries, $limit, $offset, [], [$orderType], $afterFunction ?? null),
'sum' => $dbForInternal->count('functions', $queries, APP_LIMIT_COUNT),
]), Response::MODEL_FUNCTION_LIST);
});
@ -515,10 +524,11 @@ App::get('/v1/functions/:functionId/tags')
->param('search', '', new Text(256), 'Search term to filter your list results. Max length: 256 chars.', true)
->param('limit', 25, new Range(0, 100), 'Results limit value. By default will return maximum 25 results. Maximum of 100 results allowed per request.', true)
->param('offset', 0, new Range(0, 2000), 'Results offset. The default value is 0. Use this param to manage pagination.', true)
->param('after', '', new UID(), 'ID of the tag used as the starting point for the query, excluding the tag itself. Should be used for efficient pagination when working with large sets of data.', true)
->param('orderType', 'ASC', new WhiteList(['ASC', 'DESC'], true), 'Order result by ASC or DESC order.', true)
->inject('response')
->inject('dbForInternal')
->action(function ($functionId, $search, $limit, $offset, $orderType, $response, $dbForInternal) {
->action(function ($functionId, $search, $limit, $offset, $after, $orderType, $response, $dbForInternal) {
/** @var Appwrite\Utopia\Response $response */
/** @var Utopia\Database\Database $dbForInternal */
@ -535,8 +545,16 @@ App::get('/v1/functions/:functionId/tags')
}
$queries[] = new Query('functionId', Query::TYPE_EQUAL, [$function->getId()]);
$results = $dbForInternal->find('tags', $queries, $limit, $offset, ['_id'], [$orderType]);
if (!empty($after)) {
$afterTag = $dbForInternal->getDocument('tags', $after);
if ($afterTag->isEmpty()) {
throw new Exception("Tag '{$after}' for the 'after' value not found.", 400);
}
}
$results = $dbForInternal->find('tags', $queries, $limit, $offset, [], [$orderType], $afterTag ?? null);
$sum = $dbForInternal->count('tags', $queries, APP_LIMIT_COUNT);
$response->dynamic(new Document([
@ -763,12 +781,13 @@ App::get('/v1/functions/:functionId/executions')
->param('functionId', '', new UID(), 'Function unique ID.')
->param('limit', 25, new Range(0, 100), 'Results limit value. By default will return maximum 25 results. Maximum of 100 results allowed per request.', true)
->param('offset', 0, new Range(0, 2000), 'Results offset. The default value is 0. Use this param to manage pagination.', true)
->param('after', '', new UID(), 'ID of the execution used as the starting point for the query, excluding the execution itself. Should be used for efficient pagination when working with large sets of data.', true)
->inject('response')
->inject('dbForInternal')
->action(function ($functionId, $limit, $offset, $response, $dbForInternal) {
->action(function ($functionId, $limit, $offset, $after, $response, $dbForInternal) {
/** @var Appwrite\Utopia\Response $response */
/** @var Utopia\Database\Database $dbForInternal */
Authorization::disable();
$function = $dbForInternal->getDocument('functions', $functionId);
Authorization::reset();
@ -777,10 +796,18 @@ App::get('/v1/functions/:functionId/executions')
throw new Exception('Function not found', 404);
}
if (!empty($after)) {
$afterExecution = $dbForInternal->getDocument('executions', $after);
if ($afterExecution->isEmpty()) {
throw new Exception("Execution '{$after}' for the 'after' value not found.", 400);
}
}
$results = $dbForInternal->find('executions', [
new Query('functionId', Query::TYPE_EQUAL, [$function->getId()]),
], $limit, $offset, ['_id'], [Database::ORDER_DESC]);
], $limit, $offset, [], [Database::ORDER_DESC], $afterExecution ?? null);
$sum = $dbForInternal->count('executions', [
new Query('functionId', Query::TYPE_EQUAL, [$function->getId()]),
], APP_LIMIT_COUNT);

View file

@ -164,10 +164,11 @@ App::get('/v1/projects')
->param('search', '', new Text(256), 'Search term to filter your list results. Max length: 256 chars.', true)
->param('limit', 25, new Range(0, 100), 'Results limit value. By default will return maximum 25 results. Maximum of 100 results allowed per request.', true)
->param('offset', 0, new Range(0, 2000), 'Results offset. The default value is 0. Use this param to manage pagination.', true)
->param('after', '', new UID(), 'ID of the project used as the starting point for the query, excluding the project itself. Should be used for efficient pagination when working with large sets of data.', true)
->param('orderType', 'ASC', new WhiteList(['ASC', 'DESC'], true), 'Order result by ASC or DESC order.', true)
->inject('response')
->inject('dbForConsole')
->action(function ($search, $limit, $offset, $orderType, $response, $dbForConsole) {
->action(function ($search, $limit, $offset, $after, $orderType, $response, $dbForConsole) {
/** @var Appwrite\Utopia\Response $response */
/** @var Utopia\Database\Database $dbForConsole */
@ -177,7 +178,15 @@ App::get('/v1/projects')
$queries[] = new Query('search', Query::TYPE_SEARCH, [$search]);
}
$results = $dbForConsole->find('projects', $queries, $limit, $offset, ['_id'], [$orderType]);
if (!empty($after)) {
$afterProject = $dbForConsole->getDocument('projects', $after);
if ($afterProject->isEmpty()) {
throw new Exception("Project '{$after}' for the 'after' value not found.", 400);
}
}
$results = $dbForConsole->find('projects', $queries, $limit, $offset, [], [$orderType], $afterProject ?? null);
$sum = $dbForConsole->count('projects', $queries, APP_LIMIT_COUNT);
$response->dynamic(new Document([

View file

@ -173,10 +173,11 @@ App::get('/v1/storage/files')
->param('search', '', new Text(256), 'Search term to filter your list results. Max length: 256 chars.', true)
->param('limit', 25, new Range(0, 100), 'Results limit value. By default will return maximum 25 results. Maximum of 100 results allowed per request.', true)
->param('offset', 0, new Range(0, 2000), 'Results offset. The default value is 0. Use this param to manage pagination.', true)
->param('after', '', new UID(), 'ID of the file used as the starting point for the query, excluding the file itself. Should be used for efficient pagination when working with large sets of data.', true)
->param('orderType', 'ASC', new WhiteList(['ASC', 'DESC'], true), 'Order result by ASC or DESC order.', true)
->inject('response')
->inject('dbForInternal')
->action(function ($search, $limit, $offset, $orderType, $response, $dbForInternal) {
->action(function ($search, $limit, $offset, $after, $orderType, $response, $dbForInternal) {
/** @var Appwrite\Utopia\Response $response */
/** @var Utopia\Database\Database $dbForInternal */
@ -186,8 +187,16 @@ App::get('/v1/storage/files')
$queries[] = new Query('search', Query::TYPE_SEARCH, [$search]);
}
if (!empty($after)) {
$afterFile = $dbForInternal->getDocument('files', $after);
if ($afterFile->isEmpty()) {
throw new Exception("File '{$after}' for the 'after' value not found.", 400);
}
}
$response->dynamic(new Document([
'files' => $dbForInternal->find('files', $queries, $limit, $offset, ['_id'], [$orderType]),
'files' => $dbForInternal->find('files', $queries, $limit, $offset, [], [$orderType], $afterFile ?? null),
'sum' => $dbForInternal->count('files', $queries, APP_LIMIT_COUNT),
]), Response::MODEL_FILE_LIST);
});

View file

@ -100,10 +100,11 @@ App::get('/v1/teams')
->param('search', '', new Text(256), 'Search term to filter your list results. Max length: 256 chars.', true)
->param('limit', 25, new Range(0, 100), 'Results limit value. By default will return maximum 25 results. Maximum of 100 results allowed per request.', true)
->param('offset', 0, new Range(0, 2000), 'Results offset. The default value is 0. Use this param to manage pagination.', true)
->param('after', '', new UID(), 'ID of the team used as the starting point for the query, excluding the team itself. Should be used for efficient pagination when working with large sets of data.', true)
->param('orderType', 'ASC', new WhiteList(['ASC', 'DESC'], true), 'Order result by ASC or DESC order.', true)
->inject('response')
->inject('dbForInternal')
->action(function ($search, $limit, $offset, $orderType, $response, $dbForInternal) {
->action(function ($search, $limit, $offset, $after, $orderType, $response, $dbForInternal) {
/** @var Appwrite\Utopia\Response $response */
/** @var Utopia\Database\Database $dbForInternal */
@ -113,7 +114,15 @@ App::get('/v1/teams')
$queries[] = new Query('search', Query::TYPE_SEARCH, [$search]);
}
$results = $dbForInternal->find('teams', $queries, $limit, $offset, ['_id'], [$orderType]);
if (!empty($after)) {
$afterTeam = $dbForInternal->getDocument('teams', $after);
if ($afterTeam->isEmpty()) {
throw new Exception("Team '{$after}' for the 'after' value not found.", 400);
}
}
$results = $dbForInternal->find('teams', $queries, $limit, $offset, [], [$orderType], $afterTeam ?? null);
$sum = $dbForInternal->count('teams', $queries, APP_LIMIT_COUNT);
$response->dynamic(new Document([
@ -424,10 +433,11 @@ App::get('/v1/teams/:teamId/memberships')
->param('search', '', new Text(256), 'Search term to filter your list results. Max length: 256 chars.', true)
->param('limit', 25, new Range(0, 100), 'Results limit value. By default will return maximum 25 results. Maximum of 100 results allowed per request.', true)
->param('offset', 0, new Range(0, 2000), 'Results offset. The default value is 0. Use this param to manage pagination.', true)
->param('after', '', new UID(), 'ID of the membership used as the starting point for the query, excluding the membership itself. Should be used for efficient pagination when working with large sets of data.', true)
->param('orderType', 'ASC', new WhiteList(['ASC', 'DESC'], true), 'Order result by ASC or DESC order.', true)
->inject('response')
->inject('dbForInternal')
->action(function ($teamId, $search, $limit, $offset, $orderType, $response, $dbForInternal) {
->action(function ($teamId, $search, $limit, $offset, $after, $orderType, $response, $dbForInternal) {
/** @var Appwrite\Utopia\Response $response */
/** @var Utopia\Database\Database $dbForInternal */
@ -437,7 +447,15 @@ App::get('/v1/teams/:teamId/memberships')
throw new Exception('Team not found', 404);
}
$memberships = $dbForInternal->find('memberships', [new Query('teamId', Query::TYPE_EQUAL, [$teamId])], $limit, $offset, ['_id'], [$orderType]);
if (!empty($after)) {
$afterMembership = $dbForInternal->getDocument('memberships', $after);
if ($afterMembership->isEmpty()) {
throw new Exception("Membership '{$after}' for the 'after' value not found.", 400);
}
}
$memberships = $dbForInternal->find('memberships', [new Query('teamId', Query::TYPE_EQUAL, [$teamId])], $limit, $offset, [], [$orderType], $afterMembership ?? null);
$sum = $dbForInternal->count('memberships', [new Query('teamId', Query::TYPE_EQUAL, [$teamId])], APP_LIMIT_COUNT);
$users = [];

View file

@ -85,13 +85,22 @@ App::get('/v1/users')
->param('search', '', new Text(256), 'Search term to filter your list results. Max length: 256 chars.', true)
->param('limit', 25, new Range(0, 100), 'Results limit value. By default will return maximum 25 results. Maximum of 100 results allowed per request.', true)
->param('offset', 0, new Range(0, 2000), 'Results offset. The default value is 0. Use this param to manage pagination.', true)
->param('after', '', new UID(), 'ID of the user used as the starting point for the query, excluding the user itself. Should be used for efficient pagination when working with large sets of data.', true)
->param('orderType', 'ASC', new WhiteList(['ASC', 'DESC'], true), 'Order result by ASC or DESC order.', true)
->inject('response')
->inject('dbForInternal')
->action(function ($search, $limit, $offset, $orderType, $response, $dbForInternal) {
->action(function ($search, $limit, $offset, $after, $orderType, $response, $dbForInternal) {
/** @var Appwrite\Utopia\Response $response */
/** @var Utopia\Database\Database $dbForInternal */
if (!empty($after)) {
$afterUser = $dbForInternal->getDocument('users', $after);
if ($afterUser->isEmpty()) {
throw new Exception('User for after not found', 400);
}
}
$queries = [];
if (!empty($search)) {

View file

@ -53,7 +53,6 @@ App::init(function ($utopia, $request, $response, $console, $project, $dbForCons
'domain' => $domain->get(),
]);
$certificate = $dbForConsole->createDocument('certificates', $certificate);
Authorization::enable();
Console::info('Issuing a TLS certificate for the master domain (' . $domain->get() . ') in a few seconds...');
@ -63,10 +62,11 @@ App::init(function ($utopia, $request, $response, $console, $project, $dbForCons
'validateTarget' => false,
'validateCNAME' => false,
]);
} else {
Authorization::enable(); // ensure authorization is reenabled
}
$domains[$domain->get()] = true;
Authorization::reset(); // ensure authorization is re-enabled
}
Config::setParam('domains', $domains);
}

View file

@ -18,7 +18,7 @@ App::init(function ($utopia, $request, $response, $project, $user, $register, $e
/** @var Utopia\Registry\Registry $register */
/** @var Appwrite\Event\Event $events */
/** @var Appwrite\Event\Event $audits */
/** @var Appwrite\Event\Event $usage */
/** @var Appwrite\Stats\Stats $usage */
/** @var Appwrite\Event\Event $deletes */
/** @var Appwrite\Event\Event $database */
/** @var Appwrite\Event\Event $functions */
@ -162,14 +162,14 @@ App::init(function ($utopia, $request, $project) {
}, ['utopia', 'request', 'project'], 'auth');
App::shutdown(function ($utopia, $request, $response, $project, $events, $audits, $usage, $deletes, $database, $mode) {
App::shutdown(function ($utopia, $request, $response, $project, $register, $events, $audits, $usage, $deletes, $database, $mode) {
/** @var Utopia\App $utopia */
/** @var Utopia\Swoole\Request $request */
/** @var Appwrite\Utopia\Response $response */
/** @var Utopia\Database\Document $project */
/** @var Appwrite\Event\Event $events */
/** @var Appwrite\Event\Event $audits */
/** @var Appwrite\Event\Event $usage */
/** @var Appwrite\Stats\Stats $usage */
/** @var Appwrite\Event\Event $deletes */
/** @var Appwrite\Event\Event $database */
/** @var Appwrite\Event\Event $functions */
@ -215,8 +215,8 @@ App::shutdown(function ($utopia, $request, $response, $project, $events, $audits
$usage
->setParam('networkRequestSize', $request->getSize() + $usage->getParam('storage'))
->setParam('networkResponseSize', $response->getSize())
->trigger()
->submit()
;
}
}, ['utopia', 'request', 'response', 'project', 'events', 'audits', 'usage', 'deletes', 'database', 'mode'], 'api');
}, ['utopia', 'request', 'response', 'project', 'register', 'events', 'audits', 'usage', 'deletes', 'database', 'mode'], 'api');

View file

@ -29,6 +29,7 @@ use Appwrite\Network\Validator\Email;
use Appwrite\Network\Validator\IP;
use Appwrite\Network\Validator\URL;
use Appwrite\OpenSSL\OpenSSL;
use Appwrite\Stats\Stats;
use Utopia\App;
use Utopia\View;
use Utopia\Config\Config;
@ -76,18 +77,18 @@ const APP_SOCIAL_DISCORD = 'https://appwrite.io/discord';
const APP_SOCIAL_DISCORD_CHANNEL = '564160730845151244';
const APP_SOCIAL_DEV = 'https://dev.to/appwrite';
const APP_SOCIAL_STACKSHARE = 'https://stackshare.io/appwrite';
// Creation Types
const CREATE_TYPE_ATTRIBUTE = 'newAttribute';
const CREATE_TYPE_INDEX = 'newIndex';
// Deletion Types
const DELETE_TYPE_ATTRIBUTE = 'attribute';
const DELETE_TYPE_INDEX = 'index';
// Database Worker Types
const DATABASE_TYPE_CREATE_ATTRIBUTE = 'createAttribute';
const DATABASE_TYPE_CREATE_INDEX = 'createIndex';
const DATABASE_TYPE_DELETE_ATTRIBUTE = 'deleteAttribute';
const DATABASE_TYPE_DELETE_INDEX = 'deleteIndex';
// Deletes Worker Types
const DELETE_TYPE_DOCUMENT = 'document';
const DELETE_TYPE_EXECUTIONS = 'executions';
const DELETE_TYPE_AUDIT = 'audit';
const DELETE_TYPE_ABUSE = 'abuse';
const DELETE_TYPE_CERTIFICATES = 'certificates';
// Mail Types
// Mail Worker Types
const MAIL_TYPE_VERIFICATION = 'verification';
const MAIL_TYPE_RECOVERY = 'recovery';
const MAIL_TYPE_INVITATION = 'invitation';
@ -291,6 +292,7 @@ $register->set('statsd', function () { // Register DB connection
return $statsd;
});
$register->set('smtp', function () {
$mail = new PHPMailer(true);
@ -421,7 +423,7 @@ App::setResource('audits', function($register) {
}, ['register']);
App::setResource('usage', function($register) {
return new Event(Event::USAGE_QUEUE_NAME, Event::USAGE_CLASS_NAME);
return new Stats($register->get('statsd'));
}, ['register']);
App::setResource('mails', function($register) {

View file

@ -22,8 +22,8 @@ $maxCells = 10;
</div>
<div data-ui-modal class="modal width-large box close" data-button-hide="on" data-open-event="open-json">
<button type="button" class="close pull-end" data-ui-modal-close=""><i class="icon-cancel"></i></button>
<button type="button" class="close pull-end" data-ui-modal-close=""><i class="icon-cancel"></i></button>
<h2>JSON View</h2>
<div class="margin-bottom">
@ -80,52 +80,52 @@ $maxCells = 10;
<div data-ls-if="({{project-documents.sum}})">
<div class="margin-bottom-small margin-end-small text-align-end text-size-small"><span data-ls-bind="{{project-documents.sum}}"></span> documents found</div>
<div class="box margin-bottom y-scroll">
<table class="vertical">
<thead>
<tr>
<?php foreach($rules as $i => $rule):
if($i > $maxCells) {
break;
}
$label = (isset($rule['label'])) ? $rule['label'] : '';
?>
<th width="120"><?php echo $this->escape($label); ?></th>
<?php endforeach; ?>
<?php foreach ($rules as $i => $rule):
if ($i > $maxCells) {
break;
}
$label = (isset($rule['label'])) ? $rule['label'] : '';
?>
<th width="120"><?php echo $this->escape($label); ?></th>
<?php endforeach;?>
</tr>
</thead>
<tbody data-ls-loop="project-documents.documents" data-ls-as="node">
<tr>
<?php foreach($rules as $i => $rule):
if($i > $maxCells) {
break;
}
$label = $rule['label'] ?? '';
$key = $rule['key'] ?? '';
$type = $rule['type'] ?? '';
$array = $rule['array'] ?? '';
?>
<td data-title="<?php echo $this->escape($label); ?>: " class="text-size-small text-height-small">
<a data-ls-attrs="href=/console/database/document?id={{node.$id}}&collection={{router.params.id}}&project={{router.params.project}}&buster={{project-collection.dateUpdated}}">
<?php if(!$array): ?>
<?php switch($type):
case 'fileId': ?>
<img data-ls-if="{{node.<?php echo $this->escape($key); ?>}} != ''" src="" data-ls-attrs="src={{env.ENDPOINT}}/v1/storage/files/{{node.<?php echo $this->escape($key); ?>}}/preview?width=65&height=65&project={{router.params.project}}&mode=admin" class="avatar" width="30" height="30" loading="lazy" />
<?php break; ?>
<?php case 'document': ?>
{...}
<?php break; ?>
<?php default: ?>
<span data-ls-bind="{{node.<?php echo $this->escape($key); ?>}}" data-ls-attrs="title={{node.<?php echo $this->escape($key); ?>}}"></span>
<?php break; ?>
<?php endswitch; ?>
<?php else: ?>
<?php foreach ($rules as $i => $rule):
if ($i > $maxCells) {
break;
}
$label = $rule['label'] ?? '';
$key = $rule['key'] ?? '';
$type = $rule['type'] ?? '';
$array = $rule['array'] ?? '';
?>
<td data-title="<?php echo $this->escape($label); ?>: " class="text-size-small text-height-small">
<a data-ls-attrs="href=/console/database/document?id={{node.$id}}&collection={{router.params.id}}&project={{router.params.project}}&buster={{project-collection.dateUpdated}}">
<?php if (!$array): ?>
<?php switch ($type):
case 'fileId': ?>
<img data-ls-if="{{node.<?php echo $this->escape($key); ?>}} != ''" src="" data-ls-attrs="src={{env.ENDPOINT}}/v1/storage/files/{{node.<?php echo $this->escape($key); ?>}}/preview?width=65&height=65&project={{router.params.project}}&mode=admin" class="avatar" width="30" height="30" loading="lazy" />
<?php break;?>
<?php case 'document': ?>
{...}
<?php break;?>
<?php default: ?>
<span data-ls-bind="{{node.<?php echo $this->escape($key); ?>}}" data-ls-attrs="title={{node.<?php echo $this->escape($key); ?>}}"></span>
<?php break;?>
<?php endswitch;?>
<?php else: ?>
[...]
<?php endif; ?>
<?php endif;?>
</a>
</td>
<?php endforeach; ?>
<?php endforeach;?>
</tr>
</tbody>
</table>
@ -215,7 +215,7 @@ $maxCells = 10;
<div class="toggle list sorts" data-ls-ui-open>
<i class="icon-up-open pull-end margin-top-tiny"></i>
<i class="icon-down-open pull-end margin-top-tiny"></i>
<h4 class="margin-bottom">
<div class="pull-start margin-end-large margin-bottom-small">
<button type="button" disabled class="margin-bottom strip round" data-move-down><i class="icon-down-dir"></i></button>
@ -248,7 +248,7 @@ $maxCells = 10;
<fieldset data-ls-attrs="name=rules" data-cast-to="array">
<input name="$id" type="hidden" data-ls-bind="{{rule.$id}}" />
<input name="$collection" type="hidden" data-ls-bind="{{rule.$collection}}" />
<div class="row thin">
<div class="col span-6">
<label data-ls-attrs="for=rule-label-{{rule.$id}}">Label
@ -287,11 +287,11 @@ $maxCells = 10;
<option value="document">Document (Embeded)</option>
</optgroup>
</select>
<div class="margin-bottom">
<input name="required" type="hidden" data-forms-switch data-ls-bind="{{rule.required}}" data-cast-to="boolean" /> &nbsp; Required <span class="tooltip" data-tooltip="Mark whether this is a required attribute"><i class="icon-info-circled"></i></span>
</div>
<div class="margin-bottom">
<input name="array" type="hidden" data-forms-switch data-ls-bind="{{rule.array}}" data-cast-to="boolean" /> &nbsp; Array <span class="tooltip" data-tooltip="Mark whether this attribute should act as an array"><i class="icon-info-circled"></i></span>
</div>
@ -314,16 +314,16 @@ $maxCells = 10;
<div class="toggle margin-bottom margin-top" data-ls-ui-open data-button-aria="Open Permissions">
<i class="icon-plus pull-end margin-top-tiny"></i>
<i class="icon-minus pull-end margin-top-tiny"></i>
<h3 class="margin-bottom-large">Permissions</h3>
<label for="collection-read">Read Access <span class="text-size-small">(<a data-ls-attrs="href={{env.HOME}}/docs/permissions" target="_blank" rel="noopener">Learn more</a>)</span></label>
<input type="hidden" id="collection-read" name="read" data-forms-tags data-cast-to="json" data-ls-bind="{{project-collection.$permissions.read}}" placeholder="User ID, Team ID or Role" />
<div class="text-fade text-size-xs margin-top-negative-small margin-bottom">Add * for wildcard access</div>
<div class="text-fade text-size-xs margin-top-negative-small margin-bottom">Add 'role:all' for wildcard access</div>
<label for="collection-write">Write Access <span class="text-size-small">(<a data-ls-attrs="href={{env.HOME}}/docs/permissions" target="_blank" rel="noopener">Learn more</a>)</label>
<input type="hidden" id="collection-write" name="write" data-forms-tags data-cast-to="json" data-ls-bind="{{project-collection.$permissions.write}}" placeholder="User ID, Team ID or Role" />
<div class="text-fade text-size-xs margin-top-negative-small margin-bottom">Add * for wildcard access</div>
<div class="text-fade text-size-xs margin-top-negative-small margin-bottom">Add 'role:all' for wildcard access</div>
</div>
<button>Update</button>
@ -402,9 +402,9 @@ $maxCells = 10;
<input type="hidden" name="path" value="project-collection.rules" />
<input type="hidden" name="type" value="append" />
<fieldset name="value" data-cast-to="object">
<input name="$id" type="hidden" value="" />
<input name="$id" type="hidden" value="" />
<input name="$collection" type="hidden" value="rules" />
<div class="row thin">
@ -445,11 +445,11 @@ $maxCells = 10;
<option value="document">Document (Embeded)</option>
</optgroup>
</select>
<input name="default" type="hidden" value="" />
<input name="required" type="hidden" value="false" data-cast-to="boolean" />
<input name="array" type="hidden" value="false" data-cast-to="boolean" />
</fieldset>
<hr class="margin-top-no" />

View file

@ -1,8 +1,8 @@
<?php
use Appwrite\Database\Database;
use Utopia\View;
use Appwrite\Database\Validator\Authorization;
use Utopia\View;
$collection = $this->getParam('collection', null);
$searchFiles = $this->getParam('searchFiles', null);
@ -16,18 +16,18 @@ $collections = [];
<?php echo $searchFiles->render(); ?>
<?php foreach($rules as $rule): // Form to append child document
<?php foreach ($rules as $rule): // Form to append child document
$key = $rule['key'] ?? '';
$label = $rule['label'] ?? '';
$type = $rule['type'] ?? '';
$list = $rule['list'] ?? [];
?>
<?php foreach($list as $item):
if($item === $collection->getId()) {
?>
<?php foreach ($list as $item):
if ($item === $collection->getId()) {
continue; // Do not allow rec recrusion
}
if(!isset($collections[$item])) {
if (!isset($collections[$item])) {
Authorization::disable(); //TODO Try and avoid calling the DB from the template
$collections[$item] = $db->getDocument($item, false);
Authorization::reset();
@ -35,7 +35,7 @@ $collections = [];
$childCollection = $collections[$item];
if(!$childCollection->getId() || $childCollection->getCollection() !== Database::SYSTEM_COLLECTION_COLLECTIONS) {
if (!$childCollection->getId() || $childCollection->getCollection() !== Database::SYSTEM_COLLECTION_COLLECTIONS) {
continue;
}
@ -43,114 +43,114 @@ $collections = [];
->setParam('collection', $childCollection)
;
echo $searchDocuments->render(); ?>
<?php endforeach; ?>
<?php endforeach; ?>
echo $searchDocuments->render();?>
<?php endforeach;?>
<?php endforeach;?>
<?php foreach($rules as $rule): // Form to append child document
<?php foreach ($rules as $rule): // Form to append child document
$key = $rule['key'] ?? '';
$label = $rule['label'] ?? '';
$type = $rule['type'] ?? '';
$list = $rule['list'] ?? [];
$array = $rule['array'] ?? false;
?>
<?php if($type !== 'document' && $array): ?>
<form class="margin-bottom-no"
data-service="container.path"
data-event="collection-child-<?php echo $this->escape($namespace.'.'.$key); ?>"
data-scope="window.ls"
data-success="reset">
<input type="hidden" name="path" value="<?php echo $this->escape($namespace.'.'.$key); ?>" />
<input type="hidden" name="type" value="append" />
<input type="hidden" name="value" value="" />
</form>
<?php endif; ?>
<?php if($type !== 'document'): ?>
<?php continue; ?>
<?php endif; ?>
<?php foreach($list as $item):
if($item === $collection->getId()) {
continue; // Do not allow rec recrusion
}
if(!isset($collections[$item])) {
Authorization::disable(); //TODO Try and avoid calling the DB from the template
$collections[$item] = $db->getDocument($item, false);
Authorization::reset();
}
$childCollection = $collections[$item];
if(!$childCollection->getId() || $childCollection->getCollection() !== Database::SYSTEM_COLLECTION_COLLECTIONS) {
continue;
}
?>
<form class="margin-bottom-no"
data-service="container.path"
data-event="collection-child-<?php echo $this->escape($namespace.'.'.$key); ?>-<?php echo $this->escape($childCollection->getId()); ?>"
data-scope="window.ls"
data-success="reset">
<input type="hidden" name="path" value="<?php echo $this->escape($namespace.'.'.$key); ?>" />
<input type="hidden" name="type" value="append" />
<fieldset name="value" data-cast-to="object">
<input name="$id" type="hidden" value="" />
<input name="$collection" type="hidden" value="<?php echo $this->escape($childCollection->getId()); ?>" />
</fieldset>
</form>
<?php if ($type !== 'document' && $array): ?>
<script type="text/html" id="collection-<?php echo ($array) ? 'array-' : ''; ?><?php echo $this->escape($childCollection->getId()); ?>">
<?php
<form class="margin-bottom-no"
data-service="container.path"
data-event="collection-child-<?php echo $this->escape($namespace . '.' . $key); ?>"
data-scope="window.ls"
data-success="reset">
$comp = new View(__DIR__.'/form.phtml');
$comp
->setParam('collection', $childCollection)
->setParam('namespace', ($array) ? 'node' : $namespace.'.'.$key)
->setParam('key', $key)
->setParam('array', $array)
->setParam('parent', 0)
;
<input type="hidden" name="path" value="<?php echo $this->escape($namespace . '.' . $key); ?>" />
<input type="hidden" name="type" value="append" />
<input type="hidden" name="value" value="" />
</form>
echo $comp->render();
?>
</script>
<?php endforeach; ?>
<?php endforeach; ?>
<?php endif;?>
<?php foreach($rules as $rule): // Form to remove array $index key
<?php if ($type !== 'document'): ?>
<?php continue;?>
<?php endif;?>
<?php foreach ($list as $item):
if ($item === $collection->getId()) {
continue; // Do not allow rec recrusion
}
if (!isset($collections[$item])) {
Authorization::disable(); //TODO Try and avoid calling the DB from the template
$collections[$item] = $db->getDocument($item, false);
Authorization::reset();
}
$childCollection = $collections[$item];
if (!$childCollection->getId() || $childCollection->getCollection() !== Database::SYSTEM_COLLECTION_COLLECTIONS) {
continue;
}
?>
<form class="margin-bottom-no"
data-service="container.path"
data-event="collection-child-<?php echo $this->escape($namespace . '.' . $key); ?>-<?php echo $this->escape($childCollection->getId()); ?>"
data-scope="window.ls"
data-success="reset">
<input type="hidden" name="path" value="<?php echo $this->escape($namespace . '.' . $key); ?>" />
<input type="hidden" name="type" value="append" />
<fieldset name="value" data-cast-to="object">
<input name="$id" type="hidden" value="" />
<input name="$collection" type="hidden" value="<?php echo $this->escape($childCollection->getId()); ?>" />
</fieldset>
</form>
<script type="text/html" id="collection-<?php echo ($array) ? 'array-' : ''; ?><?php echo $this->escape($childCollection->getId()); ?>">
<?php
$comp = new View(__DIR__ . '/form.phtml');
$comp
->setParam('collection', $childCollection)
->setParam('namespace', ($array) ? 'node' : $namespace . '.' . $key)
->setParam('key', $key)
->setParam('array', $array)
->setParam('parent', 0)
;
echo $comp->render();
?>
</script>
<?php endforeach;?>
<?php endforeach;?>
<?php foreach ($rules as $rule): // Form to remove array $index key
$key = $rule['key'] ?? '';
$label = $rule['label'] ?? '';
$type = $rule['type'] ?? '';
$list = $rule['list'] ?? false;
$array = $rule['array'] ?? false;
if(!$array) {
if (!$array) {
continue;
}
?>
<ul data-ls-loop="<?php echo $this->escape($namespace.'.'.$key); ?>" data-ls-as="xxx">
<li>
<form
data-service="container.path"
data-event="splice-<?php echo $this->escape($namespace.'.'.$key); ?>-{{$index}}"
data-scope="window.ls"
data-success="reset">
?>
<ul data-ls-loop="<?php echo $this->escape($namespace . '.' . $key); ?>" data-ls-as="xxx">
<li>
<form
data-service="container.path"
data-event="splice-<?php echo $this->escape($namespace . '.' . $key); ?>-{{$index}}"
data-scope="window.ls"
data-success="reset">
<input type="hidden" name="path" value="<?php echo $this->escape($namespace.'.'.$key); ?>" />
<input type="hidden" name="type" value="splice" />
<input type="hidden" name="value" data-ls-bind="{{$index}}" />
</form>
</li>
</ul>
<?php endforeach; ?>
<input type="hidden" name="path" value="<?php echo $this->escape($namespace . '.' . $key); ?>" />
<input type="hidden" name="type" value="splice" />
<input type="hidden" name="value" data-ls-bind="{{$index}}" />
</form>
</li>
</ul>
<?php endforeach;?>
<div
data-service="database.getDocument"
@ -173,8 +173,8 @@ $collections = [];
</div>
<div data-ui-modal class="modal width-large box close" data-button-hide="on" data-open-event="open-json">
<button type="button" class="close pull-end" data-ui-modal-close=""><i class="icon-cancel"></i></button>
<button type="button" class="close pull-end" data-ui-modal-close=""><i class="icon-cancel"></i></button>
<h2>JSON View</h2>
<div class="margin-bottom">
@ -215,57 +215,57 @@ $collections = [];
<!-- <div class="document-nav" data-forms-nav>
<ul class="text-align-end margin-end-small">
<?php foreach($rules as $rule): // Form to append child document
$key = $rule['key'] ?? '';
$label = $rule['label'] ?? '';
$type = $rule['type'] ?? '';
$list = $rule['list'] ?? [];
?>
<li class="text-size-small">
<span class="link text-fade" data-forms-nav-link="<?php echo $this->escape($key); ?>"><?php echo $this->escape($label); ?></span>
</li>
<?php endforeach; ?>
<?php foreach ($rules as $rule): // Form to append child document
$key = $rule['key'] ?? '';
$label = $rule['label'] ?? '';
$type = $rule['type'] ?? '';
$list = $rule['list'] ?? [];
?>
<li class="text-size-small">
<span class="link text-fade" data-forms-nav-link="<?php echo $this->escape($key); ?>"><?php echo $this->escape($label); ?></span>
</li>
<?php endforeach;?>
</ul>
</div> -->
<?php if(empty($rules)): ?>
<?php if (empty($rules)): ?>
<div class="margin-bottom-xl margin-top-xl margin-end margin-start text-align-center">
<h4 class="text-fade text-size-small">No attribute rules added yet.<br /><br /><a data-ls-attrs="href=/console/database/collection/settings?id={{router.params.collection}}&project={{router.params.project}}">Update Collection</a></h4>
</div>
<?php else: ?>
<?php
$comp = new View(__DIR__.'/form.phtml');
$comp
->setParam('collection', $collection)
->setParam('collections', $collections)
->setParam('namespace', $namespace)
->setParam('key', 'data')
->setParam('parent', 1)
;
$comp = new View(__DIR__ . '/form.phtml');
echo $comp->render();
?>
$comp
->setParam('collection', $collection)
->setParam('collections', $collections)
->setParam('namespace', $namespace)
->setParam('key', 'data')
->setParam('parent', 1)
;
echo $comp->render();
?>
<div class="toggle margin-bottom" data-ls-ui-open data-button-aria="Open Permissions">
<i class="icon-plus pull-end margin-top-tiny"></i>
<i class="icon-minus pull-end margin-top-tiny"></i>
<h3 class="margin-bottom-large">Permissions</h3>
<label for="collection-read">Read Access <span class="text-size-small">(<a data-ls-attrs="href={{env.HOME}}/docs/permissions" target="_blank" rel="noopener">Learn more</a>)</span></label>
<input type="hidden" id="collection-read" name="read" data-forms-tags data-cast-to="json" data-ls-bind="{{project-document.$permissions.read}}" placeholder="User ID, Team ID or Role" />
<div class="text-fade text-size-xs margin-top-negative-small margin-bottom">Add * for wildcard access</div>
<div class="text-fade text-size-xs margin-top-negative-small margin-bottom">Add 'role:all' for wildcard access</div>
<label for="collection-write">Write Access <span class="text-size-small">(<a data-ls-attrs="href={{env.HOME}}/docs/permissions" target="_blank" rel="noopener">Learn more</a>)</label>
<input type="hidden" id="collection-write" name="write" data-forms-tags data-cast-to="json" data-ls-bind="{{project-document.$permissions.write}}" placeholder="User ID, Team ID or Role" />
<div class="text-fade text-size-xs margin-top-negative-small margin-bottom">Add * for wildcard access</div>
<div class="text-fade text-size-xs margin-top-negative-small margin-bottom">Add 'role:all' for wildcard access</div>
</div>
<button data-ls-if="({{project-document.$id}})">Update</button>
<button data-ls-if="(!{{project-document.$id}})">Create</button>
<?php endif; ?>
<?php endif;?>
</div>
</form>

View file

@ -452,8 +452,8 @@ $usageStatsEnabled = $this->getParam('usageStatsEnabled', true);
<input name="name" id="function-name" type="text" autocomplete="off" data-ls-bind="{{project-function.name}}" data-forms-text-direction required placeholder="Function Name" maxlength="128" />
<label for="execute">Execute Access <span class="tooltip small" data-tooltip="Choose who can execute this function using the client API."><i class="icon-info-circled"></i></span> <span class="text-size-small">(<a data-ls-attrs="href={{env.HOME}}/docs/permissions" target="_blank" rel="noopener">Learn more</a>)</span></label>
<input type="hidden" id="execute" name="execute" data-forms-tags data-cast-to="json" data-ls-bind="{{project-function.$permissions.execute}}" placeholder="User ID, Team ID or Role" />
<div class="text-fade text-size-xs margin-top-negative-small margin-bottom">Add * for wildcard access</div>
<input type="hidden" id="execute" name="execute" data-forms-tags data-cast-to="json" data-ls-bind="{{project-function.execute}}" placeholder="User ID, Team ID or Role" />
<div class="text-fade text-size-xs margin-top-negative-small margin-bottom">Add 'role:all' for wildcard access</div>
<label for="timeout">Timeout (seconds) <span class="tooltip small" data-tooltip="Limit the execution time of your function."><i class="icon-info-circled"></i></span></label>
<input name="timeout" id="function-timeout" type="number" autocomplete="off" data-ls-bind="{{project-function.timeout}}" min="1" max="<?php echo $this->escape($timeout); ?>" data-cast-to="integer" />

View file

@ -107,11 +107,11 @@ $fileLimitHuman = $this->getParam('fileLimitHuman', 0);
<label for="file-read">Read Access (<a data-ls-attrs="href={{env.HOME}}/docs/permissions" target="_blank" rel="noopener">Learn more</a>)</label>
<input type="hidden" data-ls-attrs="id=file-read-{{file.$id}}" name="read" data-forms-tags data-cast-to="json" data-ls-bind="{{file.$read}}" placeholder="User ID, Team ID or Role" />
<div class="text-fade text-size-xs margin-top-negative-small margin-bottom">Add * for wildcard access</div>
<div class="text-fade text-size-xs margin-top-negative-small margin-bottom">Add 'role:all' for wildcard access</div>
<label for="file-write">Write Access (<a data-ls-attrs="href={{env.HOME}}/docs/permissions" target="_blank" rel="noopener">Learn more</a>)</label>
<input type="hidden" data-ls-attrs="id=file-write-{{file.$id}}" name="write" data-forms-tags data-cast-to="json" data-ls-bind="{{file.$write}}" placeholder="User ID, Team ID or Role" />
<div class="text-fade text-size-xs margin-top-negative-small margin-bottom">Add * for wildcard access</div>
<div class="text-fade text-size-xs margin-top-negative-small margin-bottom">Add 'role:all' for wildcard access</div>
</form>
<form class="strip"
@ -255,11 +255,11 @@ $fileLimitHuman = $this->getParam('fileLimitHuman', 0);
<label for="file-read">Read Access (<a data-ls-attrs="href={{env.HOME}}/docs/permissions" target="_blank" rel="noopener">Learn more</a>)</label>
<input type="hidden" id="file-read" name="read" data-forms-tags data-cast-to="json" value="<?php echo htmlentities(json_encode(['role:all'])); ?>" placeholder="User ID, Team ID or Role" />
<div class="text-fade text-size-xs margin-top-negative-small margin-bottom">Add * for wildcard access</div>
<div class="text-fade text-size-xs margin-top-negative-small margin-bottom">Add 'role:all' for wildcard access</div>
<label for="file-write">Write Access (<a data-ls-attrs="href={{env.HOME}}/docs/permissions" target="_blank" rel="noopener">Learn more</a>)</label>
<input type="hidden" id="file-write" name="write" data-forms-tags data-cast-to="json" value="" placeholder="User ID, Team ID or Role" />
<div class="text-fade text-size-xs margin-top-negative-small margin-bottom">Add * for wildcard access</div>
<div class="text-fade text-size-xs margin-top-negative-small margin-bottom">Add 'role:all' for wildcard access</div>
<footer>
<button type="submit">Create</button> &nbsp; <button data-ui-modal-close="" type="button" class="reverse">Cancel</button>

View file

@ -3,6 +3,7 @@
use Appwrite\Resque\Worker;
use Utopia\CLI\Console;
use Utopia\Database\Document;
use Utopia\Database\Validator\Authorization;
require_once __DIR__.'/../workers.php';
@ -21,24 +22,26 @@ class DatabaseV1 extends Worker
{
$projectId = $this->args['projectId'] ?? '';
$type = $this->args['type'] ?? '';
Authorization::disable();
switch (strval($type)) {
case CREATE_TYPE_ATTRIBUTE:
case DATABASE_TYPE_CREATE_ATTRIBUTE:
$attribute = $this->args['document'] ?? '';
$attribute = new Document($attribute);
$this->createAttribute($attribute, $projectId);
break;
case DELETE_TYPE_ATTRIBUTE:
case DATABASE_TYPE_DELETE_ATTRIBUTE:
$attribute = $this->args['document'] ?? '';
$attribute = new Document($attribute);
$this->deleteAttribute($attribute, $projectId);
break;
case CREATE_TYPE_INDEX:
case DATABASE_TYPE_CREATE_INDEX:
$index = $this->args['document'] ?? '';
$index = new Document($index);
$this->createIndex($index, $projectId);
break;
case DELETE_TYPE_INDEX:
case DATABASE_TYPE_DELETE_INDEX:
$index = $this->args['document'] ?? '';
$index = new Document($index);
$this->deleteIndex($index, $projectId);
@ -49,6 +52,7 @@ class DatabaseV1 extends Worker
break;
}
Authorization::reset();
}
public function shutdown(): void

View file

@ -2,6 +2,7 @@
use Appwrite\Event\Event;
use Appwrite\Resque\Worker;
use Appwrite\Stats\Stats;
use Appwrite\Utopia\Response\Model\Execution;
use Cron\CronExpression;
use Swoole\Runtime;
@ -134,8 +135,6 @@ class FunctionsV1 extends Worker
public function run(): void
{
global $register;
$projectId = $this->args['projectId'] ?? '';
$functionId = $this->args['functionId'] ?? '';
$webhooks = $this->args['webhooks'] ?? [];
@ -279,7 +278,7 @@ class FunctionsV1 extends Worker
*/
public function execute(string $trigger, string $projectId, string $executionId, Database $database, Document $function, string $event = '', string $eventData = '', string $data = '', array $webhooks = [], string $userId = '', string $jwt = ''): void
{
global $list;
global $list, $register;
$runtimes = Config::getParam('runtimes');
@ -477,21 +476,22 @@ class FunctionsV1 extends Worker
->setParam('eventData', $execution->getArrayCopy(array_keys($executionModel->getRules())));
$executionUpdate->trigger();
$usage = new Event('v1-usage', 'UsageV1');
$usage
->setParam('projectId', $projectId)
->setParam('functionId', $function->getId())
->setParam('functionExecution', 1)
->setParam('functionStatus', $functionStatus)
->setParam('functionExecutionTime', $executionTime * 1000) // ms
->setParam('networkRequestSize', 0)
->setParam('networkResponseSize', 0)
;
if(App::getEnv('_APP_USAGE_STATS', 'enabled') == 'enabled') {
$usage->trigger();
$statsd = $register->get('statsd');
$usage = new Stats($statsd);
$usage
->setParam('projectId', $projectId)
->setParam('functionId', $function->getId())
->setParam('functionExecution', 1)
->setParam('functionStatus', $functionStatus)
->setParam('functionExecutionTime', $executionTime * 1000) // ms
->setParam('networkRequestSize', 0)
->setParam('networkResponseSize', 0)
->submit()
;
}
$this->cleanup();

View file

@ -1,71 +0,0 @@
<?php
use Appwrite\Resque\Worker;
use Utopia\App;
use Utopia\CLI\Console;
require_once __DIR__.'/../workers.php';
Console::title('Usage V1 Worker');
Console::success(APP_NAME.' usage worker v1 has started');
class UsageV1 extends Worker
{
/**
* @var array
*/
public $args = [];
public function init(): void
{
}
public function run(): void
{
global $register;
/** @var \Domnikl\Statsd\Client $statsd */
$statsd = $register->get('statsd', true);
$projectId = $this->args['projectId'] ?? '';
$storage = $this->args['storage'] ?? 0;
$networkRequestSize = $this->args['networkRequestSize'] ?? 0;
$networkResponseSize = $this->args['networkResponseSize'] ?? 0;
$httpMethod = $this->args['httpMethod'] ?? '';
$httpRequest = $this->args['httpRequest'] ?? 0;
$functionId = $this->args['functionId'] ?? '';
$functionExecution = $this->args['functionExecution'] ?? 0;
$functionExecutionTime = $this->args['functionExecutionTime'] ?? 0;
$functionStatus = $this->args['functionStatus'] ?? '';
$tags = ",project={$projectId},version=".App::getEnv('_APP_VERSION', 'UNKNOWN');
// the global namespace is prepended to every key (optional)
$statsd->setNamespace('appwrite.usage');
if($httpRequest >= 1) {
$statsd->increment('requests.all'.$tags.',method='.\strtolower($httpMethod));
}
if($functionExecution >= 1) {
$statsd->increment('executions.all'.$tags.',functionId='.$functionId.',functionStatus='.$functionStatus);
$statsd->count('executions.time'.$tags.',functionId='.$functionId, $functionExecutionTime);
}
$statsd->count('network.inbound'.$tags, $networkRequestSize);
$statsd->count('network.outbound'.$tags, $networkResponseSize);
$statsd->count('network.all'.$tags, $networkRequestSize + $networkResponseSize);
if($storage >= 1) {
$statsd->count('storage.all'.$tags, $storage);
}
}
public function shutdown(): void
{
}
}

View file

@ -7,4 +7,4 @@ else
REDIS_BACKEND="redis://${_APP_REDIS_USER}:${_APP_REDIS_PASS}@${_APP_REDIS_HOST}:${_APP_REDIS_PORT}"
fi
QUEUE='v1-database' APP_INCLUDE='/usr/src/code/app/workers/database.php' php /usr/src/code/vendor/bin/resque -dopcache.preload=opcache.preload=/usr/src/code/app/preload.php
INTERVAL=0.1 QUEUE='v1-database' APP_INCLUDE='/usr/src/code/app/workers/database.php' php /usr/src/code/vendor/bin/resque -dopcache.preload=opcache.preload=/usr/src/code/app/preload.php

View file

@ -1,10 +0,0 @@
#!/bin/sh
if [ -z "$_APP_REDIS_USER" ] && [ -z "$_APP_REDIS_PASS" ]
then
REDIS_BACKEND="${_APP_REDIS_HOST}:${_APP_REDIS_PORT}"
else
REDIS_BACKEND="redis://${_APP_REDIS_USER}:${_APP_REDIS_PASS}@${_APP_REDIS_HOST}:${_APP_REDIS_PORT}"
fi
INTERVAL=1 QUEUE='v1-usage' APP_INCLUDE='/usr/src/code/app/workers/usage.php' php /usr/src/code/vendor/bin/resque -dopcache.preload=opcache.preload=/usr/src/code/app/preload.php

View file

@ -45,7 +45,7 @@
"utopia-php/cache": "0.4.*",
"utopia-php/cli": "0.11.*",
"utopia-php/config": "0.2.*",
"utopia-php/database": "0.6.*",
"utopia-php/database": "0.7.*",
"utopia-php/locale": "0.4.*",
"utopia-php/registry": "0.5.*",
"utopia-php/preloader": "0.2.*",

44
composer.lock generated
View file

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "c2bda60e7b774a0c813f52033a268c6c",
"content-hash": "7de5dc8a9fe3cbc14696c685d1cdddee",
"packages": [
{
"name": "adhocore/jwt",
@ -1666,22 +1666,22 @@
},
{
"name": "utopia-php/abuse",
"version": "0.6.1",
"version": "0.6.2",
"source": {
"type": "git",
"url": "https://github.com/utopia-php/abuse.git",
"reference": "c9078aa3a87750d66060f0ed7642e03e5815da17"
"reference": "4cd9c16610f7398d2e1737663ef682fa721ae736"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/utopia-php/abuse/zipball/c9078aa3a87750d66060f0ed7642e03e5815da17",
"reference": "c9078aa3a87750d66060f0ed7642e03e5815da17",
"url": "https://api.github.com/repos/utopia-php/abuse/zipball/4cd9c16610f7398d2e1737663ef682fa721ae736",
"reference": "4cd9c16610f7398d2e1737663ef682fa721ae736",
"shasum": ""
},
"require": {
"ext-pdo": "*",
"php": ">=7.4",
"utopia-php/database": "0.6.*"
"utopia-php/database": "0.7.*"
},
"require-dev": {
"phpunit/phpunit": "^9.4",
@ -1713,9 +1713,9 @@
],
"support": {
"issues": "https://github.com/utopia-php/abuse/issues",
"source": "https://github.com/utopia-php/abuse/tree/0.6.1"
"source": "https://github.com/utopia-php/abuse/tree/0.6.2"
},
"time": "2021-08-03T19:31:07+00:00"
"time": "2021-08-13T07:52:34+00:00"
},
{
"name": "utopia-php/analytics",
@ -1774,22 +1774,22 @@
},
{
"name": "utopia-php/audit",
"version": "0.6.1",
"version": "0.6.2",
"source": {
"type": "git",
"url": "https://github.com/utopia-php/audit.git",
"reference": "971dcd5c88309656df31ac20f326d3ac8b555594"
"reference": "2ec39a53eb98a5f9d230550ad56c7c04de5d77df"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/utopia-php/audit/zipball/971dcd5c88309656df31ac20f326d3ac8b555594",
"reference": "971dcd5c88309656df31ac20f326d3ac8b555594",
"url": "https://api.github.com/repos/utopia-php/audit/zipball/2ec39a53eb98a5f9d230550ad56c7c04de5d77df",
"reference": "2ec39a53eb98a5f9d230550ad56c7c04de5d77df",
"shasum": ""
},
"require": {
"ext-pdo": "*",
"php": ">=7.4",
"utopia-php/database": "0.6.*"
"utopia-php/database": "0.7.*"
},
"require-dev": {
"phpunit/phpunit": "^9.3",
@ -1821,9 +1821,9 @@
],
"support": {
"issues": "https://github.com/utopia-php/audit/issues",
"source": "https://github.com/utopia-php/audit/tree/0.6.1"
"source": "https://github.com/utopia-php/audit/tree/0.6.2"
},
"time": "2021-08-03T19:29:34+00:00"
"time": "2021-08-13T08:05:20+00:00"
},
{
"name": "utopia-php/cache",
@ -1984,16 +1984,16 @@
},
{
"name": "utopia-php/database",
"version": "0.6.1",
"version": "0.7.0",
"source": {
"type": "git",
"url": "https://github.com/utopia-php/database.git",
"reference": "59d9d34164b6fb896bc43085a9a82a292b43473a"
"reference": "46c4a99347397e362a9429826e1888b0aefb2056"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/utopia-php/database/zipball/59d9d34164b6fb896bc43085a9a82a292b43473a",
"reference": "59d9d34164b6fb896bc43085a9a82a292b43473a",
"url": "https://api.github.com/repos/utopia-php/database/zipball/46c4a99347397e362a9429826e1888b0aefb2056",
"reference": "46c4a99347397e362a9429826e1888b0aefb2056",
"shasum": ""
},
"require": {
@ -2041,9 +2041,9 @@
],
"support": {
"issues": "https://github.com/utopia-php/database/issues",
"source": "https://github.com/utopia-php/database/tree/0.6.1"
"source": "https://github.com/utopia-php/database/tree/0.7.0"
},
"time": "2021-08-05T17:19:16+00:00"
"time": "2021-08-10T19:09:58+00:00"
},
{
"name": "utopia-php/domains",
@ -6278,5 +6278,5 @@
"platform-overrides": {
"php": "8.0"
},
"plugin-api-version": "2.1.0"
"plugin-api-version": "2.0.0"
}

View file

@ -121,26 +121,6 @@ services:
- _APP_FUNCTIONS_MEMORY
- _APP_FUNCTIONS_MEMORY_SWAP
- _APP_FUNCTIONS_RUNTIMES
appwrite-worker-usage:
entrypoint: worker-usage
container_name: appwrite-worker-usage
build:
context: .
networks:
- appwrite
volumes:
- ./app:/usr/src/code/app
- ./src:/usr/src/code/src
depends_on:
- redis
- telegraf
environment:
- _APP_ENV
- _APP_REDIS_HOST
- _APP_REDIS_PORT
- _APP_REDIS_USER
- _APP_REDIS_PASS
- _APP_STATSD_HOST
- _APP_STATSD_PORT
@ -308,6 +288,8 @@ services:
- _APP_FUNCTIONS_MEMORY
- _APP_FUNCTIONS_MEMORY_SWAP
- _APP_USAGE_STATS
- _APP_STATSD_HOST
- _APP_STATSD_PORT
- DOCKERHUB_PULL_USERNAME
- DOCKERHUB_PULL_PASSWORD

View file

@ -2372,8 +2372,10 @@ code.innerHTML=value;Prism.highlightElement(code);div.scrollTop=0;};element.addE
function syncA(){element.value=picker.value;update();}
function syncB(){picker.value=element.value;}
element.parentNode.insertBefore(preview,element);update();syncB();}});})(window);(function(window){"use strict";window.ls.container.get("view").add({selector:"data-forms-copy",controller:function(element,alerts,document,window){var button=window.document.createElement("i");button.type="button";button.className="icon-docs note copy";button.style.cursor="pointer";element.parentNode.insertBefore(button,element.nextSibling);var copy=function(event){let disabled=element.disabled;element.disabled=false;element.focus();element.select();document.execCommand("Copy");if(document.selection){document.selection.empty();}else if(window.getSelection){window.getSelection().removeAllRanges();}
element.disabled=disabled;element.blur();alerts.add({text:"Copied to clipboard",class:""},3000);};button.addEventListener("click",copy);}});})(window);(function(window){"use strict";window.ls.container.get("view").add({selector:"data-custom-id",controller:function(element,sdk,console,window){let prevData="";let idType=element.getAttribute('data-id-type');const div=window.document.createElement("div");div.className="input-copy";const button=window.document.createElement("i");button.type="button";button.style.cursor="pointer";const writer=window.document.createElement("input");writer.type="text";writer.setAttribute("maxlength",element.getAttribute("maxlength"));const placeholder=element.getAttribute("placeholder");if(placeholder){writer.setAttribute("placeholder",placeholder);}
const info=window.document.createElement("div");info.className="text-fade text-size-xs margin-top-negative-small margin-bottom";div.appendChild(writer);div.appendChild(button);element.parentNode.insertBefore(div,element);element.parentNode.insertBefore(info,div.nextSibling);const switchType=function(event){if(idType=="custom"){idType="auto";setIdType(idType);}else{idType="custom";setIdType(idType);}}
element.disabled=disabled;element.blur();alerts.add({text:"Copied to clipboard",class:""},3000);};button.addEventListener("click",copy);}});})(window);(function(window){"use strict";window.ls.container.get("view").add({selector:"data-custom-id",controller:function(element,sdk,console,window){let prevData="";let idType=element.getAttribute('data-id-type');let disableSwitch=element.getAttribute('data-disable-switch');const div=window.document.createElement("div");if(disableSwitch!=="true"){div.className="input-copy";}
const button=window.document.createElement("i");button.type="button";button.style.cursor="pointer";const writer=window.document.createElement("input");writer.type="text";writer.setAttribute("maxlength",element.getAttribute("maxlength"));const placeholder=element.getAttribute("placeholder");if(placeholder){writer.setAttribute("placeholder",placeholder);}
const info=window.document.createElement("div");info.className="text-fade text-size-xs margin-top-negative-small margin-bottom";div.appendChild(writer);if(disableSwitch!=="true"){div.appendChild(button);}
element.parentNode.insertBefore(div,element);element.parentNode.insertBefore(info,div.nextSibling);const switchType=function(event){if(idType=="custom"){idType="auto";setIdType(idType);}else{idType="custom";setIdType(idType);}}
const validate=function(event){const[service,method]=element.dataset["validator"].split('.');const value=event.target.value;if(value.length<1){event.target.setCustomValidity("ID is required");}else{switch(service){case'projects':setValidity(console[service][method](value),event.target);break;default:setValidity(sdk[service][method](value),event.target);}}}
const setValidity=async function(promise,target){try{await promise;target.setCustomValidity("ID already exists");}catch(e){target.setCustomValidity("");}}
const setIdType=function(idType){if(idType=="custom"){element.setAttribute("data-id-type",idType);info.innerHTML="Allowed Characters A-Z, a-z, 0-9, and non-leading underscore";if(prevData==='auto-generated'){prevData=""}

View file

@ -356,8 +356,10 @@ code.innerHTML=value;Prism.highlightElement(code);div.scrollTop=0;};element.addE
function syncA(){element.value=picker.value;update();}
function syncB(){picker.value=element.value;}
element.parentNode.insertBefore(preview,element);update();syncB();}});})(window);(function(window){"use strict";window.ls.container.get("view").add({selector:"data-forms-copy",controller:function(element,alerts,document,window){var button=window.document.createElement("i");button.type="button";button.className="icon-docs note copy";button.style.cursor="pointer";element.parentNode.insertBefore(button,element.nextSibling);var copy=function(event){let disabled=element.disabled;element.disabled=false;element.focus();element.select();document.execCommand("Copy");if(document.selection){document.selection.empty();}else if(window.getSelection){window.getSelection().removeAllRanges();}
element.disabled=disabled;element.blur();alerts.add({text:"Copied to clipboard",class:""},3000);};button.addEventListener("click",copy);}});})(window);(function(window){"use strict";window.ls.container.get("view").add({selector:"data-custom-id",controller:function(element,sdk,console,window){let prevData="";let idType=element.getAttribute('data-id-type');const div=window.document.createElement("div");div.className="input-copy";const button=window.document.createElement("i");button.type="button";button.style.cursor="pointer";const writer=window.document.createElement("input");writer.type="text";writer.setAttribute("maxlength",element.getAttribute("maxlength"));const placeholder=element.getAttribute("placeholder");if(placeholder){writer.setAttribute("placeholder",placeholder);}
const info=window.document.createElement("div");info.className="text-fade text-size-xs margin-top-negative-small margin-bottom";div.appendChild(writer);div.appendChild(button);element.parentNode.insertBefore(div,element);element.parentNode.insertBefore(info,div.nextSibling);const switchType=function(event){if(idType=="custom"){idType="auto";setIdType(idType);}else{idType="custom";setIdType(idType);}}
element.disabled=disabled;element.blur();alerts.add({text:"Copied to clipboard",class:""},3000);};button.addEventListener("click",copy);}});})(window);(function(window){"use strict";window.ls.container.get("view").add({selector:"data-custom-id",controller:function(element,sdk,console,window){let prevData="";let idType=element.getAttribute('data-id-type');let disableSwitch=element.getAttribute('data-disable-switch');const div=window.document.createElement("div");if(disableSwitch!=="true"){div.className="input-copy";}
const button=window.document.createElement("i");button.type="button";button.style.cursor="pointer";const writer=window.document.createElement("input");writer.type="text";writer.setAttribute("maxlength",element.getAttribute("maxlength"));const placeholder=element.getAttribute("placeholder");if(placeholder){writer.setAttribute("placeholder",placeholder);}
const info=window.document.createElement("div");info.className="text-fade text-size-xs margin-top-negative-small margin-bottom";div.appendChild(writer);if(disableSwitch!=="true"){div.appendChild(button);}
element.parentNode.insertBefore(div,element);element.parentNode.insertBefore(info,div.nextSibling);const switchType=function(event){if(idType=="custom"){idType="auto";setIdType(idType);}else{idType="custom";setIdType(idType);}}
const validate=function(event){const[service,method]=element.dataset["validator"].split('.');const value=event.target.value;if(value.length<1){event.target.setCustomValidity("ID is required");}else{switch(service){case'projects':setValidity(console[service][method](value),event.target);break;default:setValidity(sdk[service][method](value),event.target);}}}
const setValidity=async function(promise,target){try{await promise;target.setCustomValidity("ID already exists");}catch(e){target.setCustomValidity("");}}
const setIdType=function(idType){if(idType=="custom"){element.setAttribute("data-id-type",idType);info.innerHTML="Allowed Characters A-Z, a-z, 0-9, and non-leading underscore";if(prevData==='auto-generated'){prevData=""}

View file

@ -5,9 +5,12 @@
controller: function (element, sdk, console, window) {
let prevData = "";
let idType = element.getAttribute('data-id-type');
let disableSwitch = element.getAttribute('data-disable-switch');
const div = window.document.createElement("div");
div.className = "input-copy";
if(disableSwitch !== "true") {
div.className = "input-copy";
}
const button = window.document.createElement("i");
button.type = "button";
@ -25,7 +28,9 @@
info.className = "text-fade text-size-xs margin-top-negative-small margin-bottom";
div.appendChild(writer);
div.appendChild(button);
if(disableSwitch !== "true") {
div.appendChild(button);
}
element.parentNode.insertBefore(div, element);
element.parentNode.insertBefore(info, div.nextSibling);

View file

@ -0,0 +1,129 @@
<?php
namespace Appwrite\Stats;
use Utopia\App;
class Stats
{
/**
* @var array
*/
protected $params = [];
/**
* @var mixed
*/
protected $statsd;
/**
* @var string
*/
protected $namespace = 'appwrite.usage';
/**
* Event constructor.
*
* @param mixed $statsd
*/
public function __construct($statsd)
{
$this->statsd = $statsd;
}
/**
* @param string $key
* @param mixed $value
*
* @return $this
*/
public function setParam(string $key, $value): self
{
$this->params[$key] = $value;
return $this;
}
/**
* @param string $key
*
* @return mixed|null
*/
public function getParam(string $key)
{
return (isset($this->params[$key])) ? $this->params[$key] : null;
}
/**
* @param string $namespace
*
* @return $this
*/
public function setNamespace(string $namespace): self
{
$this->namespace = $namespace;
return $this;
}
/**
* @return string
*/
public function getNamespace()
{
return $this->namespace;
}
/**
* Submit data to StatsD.
*/
public function submit(): void
{
$projectId = $this->params['projectId'] ?? '';
$storage = $this->params['storage'] ?? 0;
$networkRequestSize = $this->params['networkRequestSize'] ?? 0;
$networkResponseSize = $this->params['networkResponseSize'] ?? 0;
$httpMethod = $this->params['httpMethod'] ?? '';
$httpRequest = $this->params['httpRequest'] ?? 0;
$functionId = $this->params['functionId'] ?? '';
$functionExecution = $this->params['functionExecution'] ?? 0;
$functionExecutionTime = $this->params['functionExecutionTime'] ?? 0;
$functionStatus = $this->params['functionStatus'] ?? '';
$tags = ",project={$projectId},version=" . App::getEnv('_APP_VERSION', 'UNKNOWN');
// the global namespace is prepended to every key (optional)
$this->statsd->setNamespace($this->namespace);
if ($httpRequest >= 1) {
$this->statsd->increment('requests.all' . $tags . ',method=' . \strtolower($httpMethod));
}
if ($functionExecution >= 1) {
$this->statsd->increment('executions.all' . $tags . ',functionId=' . $functionId . ',functionStatus=' . $functionStatus);
$this->statsd->count('executions.time' . $tags . ',functionId=' . $functionId, $functionExecutionTime);
}
$this->statsd->count('network.inbound' . $tags, $networkRequestSize);
$this->statsd->count('network.outbound' . $tags, $networkResponseSize);
$this->statsd->count('network.all' . $tags, $networkRequestSize + $networkResponseSize);
if ($storage >= 1) {
$this->statsd->count('storage.all' . $tags, $storage);
}
$this->reset();
}
public function reset(): self
{
$this->params = [];
$this->namespace = 'appwrite.usage';
return $this;
}
}

View file

@ -16,12 +16,12 @@ class Func extends Model
'default' => '',
'example' => '5e5ea5c16897e',
])
->addRule('$permissions', [
'type' => Response::MODEL_PERMISSIONS,
'description' => 'Function permissions.',
'default' => new \stdClass,
'example' => new \stdClass,
'array' => false,
->addRule('execute', [
'type' => self::TYPE_STRING,
'description' => 'Document execute permissions.',
'default' => '',
'example' => 'role:all',
'array' => true,
])
->addRule('name', [
'type' => self::TYPE_STRING,

View file

@ -43,16 +43,19 @@ trait DatabaseBase
'required' => true,
]);
sleep(2);
$releaseYear = $this->client->call(Client::METHOD_POST, '/database/collections/' . $data['moviesId'] . '/attributes/integer', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey']
]), [
'attributeId' => 'releaseYear',
'size' => 0,
'required' => true,
]);
sleep(2);
$actors = $this->client->call(Client::METHOD_POST, '/database/collections/' . $data['moviesId'] . '/attributes/string', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
@ -61,7 +64,6 @@ trait DatabaseBase
'attributeId' => 'actors',
'size' => 256,
'required' => false,
'default' => null,
'array' => true,
]);
@ -88,7 +90,7 @@ trait DatabaseBase
$this->assertEquals($actors['body']['array'], true);
// wait for database worker to create attributes
sleep(10);
sleep(5);
$movies = $this->client->call(Client::METHOD_GET, '/database/collections/' . $data['moviesId'], array_merge([
'content-type' => 'application/json',
@ -120,7 +122,7 @@ trait DatabaseBase
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey']
]), [
'id' => 'titleIndex',
'indexId' => 'titleIndex',
'type' => 'fulltext',
'attributes' => ['title'],
]);
@ -294,6 +296,115 @@ trait DatabaseBase
return [];
}
/**
* @depends testCreateDocument
*/
public function testListDocumentsAfterPagination(array $data):array
{
/**
* Test after without order.
*/
$base = $this->client->call(Client::METHOD_GET, '/database/collections/' . $data['moviesId'] . '/documents', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()));
$this->assertEquals('Captain America', $base['body']['documents'][0]['title']);
$this->assertEquals('Spider-Man: Far From Home', $base['body']['documents'][1]['title']);
$this->assertEquals('Spider-Man: Homecoming', $base['body']['documents'][2]['title']);
$this->assertCount(3, $base['body']['documents']);
$documents = $this->client->call(Client::METHOD_GET, '/database/collections/' . $data['moviesId'] . '/documents', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'after' => $base['body']['documents'][0]['$id']
]);
$this->assertEquals($base['body']['documents'][1]['$id'], $documents['body']['documents'][0]['$id']);
$this->assertEquals($base['body']['documents'][2]['$id'], $documents['body']['documents'][1]['$id']);
$this->assertCount(2, $documents['body']['documents']);
$documents = $this->client->call(Client::METHOD_GET, '/database/collections/' . $data['moviesId'] . '/documents', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'after' => $base['body']['documents'][2]['$id']
]);
$this->assertEmpty($documents['body']['documents']);
/**
* Test with ASC order and after.
*/
$base = $this->client->call(Client::METHOD_GET, '/database/collections/' . $data['moviesId'] . '/documents', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'orderAttributes' => ['releaseYear'],
'orderTypes' => ['ASC'],
]);
$this->assertEquals(1944, $base['body']['documents'][0]['releaseYear']);
$this->assertEquals(2017, $base['body']['documents'][1]['releaseYear']);
$this->assertEquals(2019, $base['body']['documents'][2]['releaseYear']);
$this->assertCount(3, $base['body']['documents']);
$documents = $this->client->call(Client::METHOD_GET, '/database/collections/' . $data['moviesId'] . '/documents', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'orderAttributes' => ['releaseYear'],
'orderTypes' => ['ASC'],
'after' => $base['body']['documents'][1]['$id']
]);
$this->assertEquals($base['body']['documents'][2]['$id'], $documents['body']['documents'][0]['$id']);
$this->assertCount(1, $documents['body']['documents']);
/**
* Test with DESC order and after.
*/
$base = $this->client->call(Client::METHOD_GET, '/database/collections/' . $data['moviesId'] . '/documents', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'orderAttributes' => ['releaseYear'],
'orderTypes' => ['DESC'],
]);
$this->assertEquals(1944, $base['body']['documents'][2]['releaseYear']);
$this->assertEquals(2017, $base['body']['documents'][1]['releaseYear']);
$this->assertEquals(2019, $base['body']['documents'][0]['releaseYear']);
$this->assertCount(3, $base['body']['documents']);
$documents = $this->client->call(Client::METHOD_GET, '/database/collections/' . $data['moviesId'] . '/documents', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'orderAttributes' => ['releaseYear'],
'orderTypes' => ['DESC'],
'after' => $base['body']['documents'][1]['$id']
]);
$this->assertEquals($base['body']['documents'][2]['$id'], $documents['body']['documents'][0]['$id']);
$this->assertCount(1, $documents['body']['documents']);
/**
* Test after with unknown document.
*/
$documents = $this->client->call(Client::METHOD_GET, '/database/collections/' . $data['moviesId'] . '/documents', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'after' => 'unknown'
]);
$this->assertEquals($documents['headers']['status-code'], 400);
return [];
}
/**
* @depends testCreateDocument
*/
@ -645,7 +756,7 @@ trait DatabaseBase
// $this->assertEquals('Minimum value must be lesser than maximum value', $invalidRange['body']['message']);
// wait for worker to add attributes
sleep(10);
sleep(15);
$collection = $this->client->call(Client::METHOD_GET, '/database/collections/' . $collectionId, array_merge([
'content-type' => 'application/json',
@ -654,7 +765,7 @@ trait DatabaseBase
]), []);
$this->assertCount(7, $collection['body']['attributes']);
$this->assertCount(0, $collection['body']['attributesInQueue']);
// $this->assertCount(0, $collection['body']['attributesInQueue']);
/**
* Test for successful validation

View file

@ -13,7 +13,87 @@ class DatabaseCustomServerTest extends Scope
use ProjectCustom;
use SideServer;
public function testDeleteCollection()
public function testListCollections()
{
/**
* Test for SUCCESS
*/
$test1 = $this->client->call(Client::METHOD_POST, '/database/collections', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey']
]), [
'name' => 'Test 1',
'collectionId' => 'first',
'read' => ['role:all'],
'write' => ['role:all'],
]);
$test2 = $this->client->call(Client::METHOD_POST, '/database/collections', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey']
]), [
'name' => 'Test 2',
'collectionId' => 'second',
'read' => ['role:all'],
'write' => ['role:all'],
]);
$collections = $this->client->call(Client::METHOD_GET, '/database/collections', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()));
$this->assertEquals(2, $collections['body']['sum']);
$this->assertEquals($test1['body']['$id'], $collections['body']['collections'][0]['$id']);
$this->assertEquals($test2['body']['$id'], $collections['body']['collections'][1]['$id']);
/**
* Test for Order
*/
$base = array_reverse($collections['body']['collections']);
$collections = $this->client->call(Client::METHOD_GET, '/database/collections', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'orderType' => 'DESC'
]);
$this->assertEquals(2, $collections['body']['sum']);
$this->assertEquals($base[0]['$id'], $collections['body']['collections'][0]['$id']);
$this->assertEquals($base[1]['$id'], $collections['body']['collections'][1]['$id']);
/**
* Test for After
*/
$base = $this->client->call(Client::METHOD_GET, '/database/collections', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()));
$collections = $this->client->call(Client::METHOD_GET, '/database/collections', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'after' => $base['body']['collections'][0]['$id']
]);
$this->assertCount(1, $collections['body']['collections']);
$this->assertEquals($base['body']['collections'][1]['$id'], $collections['body']['collections'][0]['$id']);
$collections = $this->client->call(Client::METHOD_GET, '/database/collections', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'after' => $base['body']['collections'][1]['$id']
]);
$this->assertCount(0, $collections['body']['collections']);
$this->assertEmpty($collections['body']['collections']);
}
public function testDeleteAttribute(): array
{
/**
* Test for SUCCESS
@ -54,7 +134,59 @@ class DatabaseCustomServerTest extends Scope
'required' => true,
]);
// wait for database worker to finish creating attributes
$unneeded = $this->client->call(Client::METHOD_POST, '/database/collections/' . $actors['body']['$id'] . '/attributes/string', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey']
]), [
'attributeId' => 'unneeded',
'size' => 256,
'required' => true,
]);
// Wait for database worker to finish creating attributes
sleep(5);
$index = $this->client->call(Client::METHOD_POST, '/database/collections/' . $actors['body']['$id'] . '/indexes', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey']
]), [
'indexId' => 'key_lastName',
'type' => 'key',
'attributes' => [
'lastName',
],
]);
// Wait for database worker to finish creating index
sleep(5);
$collection = $this->client->call(Client::METHOD_GET, '/database/collections/' . $actors['body']['$id'], array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey']
]), []);
$unneededId = $unneeded['body']['$id'];
$this->assertEquals($collection['body']['$id'], $firstName['body']['$collection']);
$this->assertEquals($collection['body']['$id'], $lastName['body']['$collection']);
$this->assertIsArray($collection['body']['attributes']);
$this->assertCount(3, $collection['body']['attributes']);
$this->assertEquals($collection['body']['attributes'][0]['$id'], $firstName['body']['$id']);
$this->assertEquals($collection['body']['attributes'][1]['$id'], $lastName['body']['$id']);
$this->assertEquals($collection['body']['attributes'][2]['$id'], $unneeded['body']['$id']);
$this->assertCount(1, $collection['body']['indexes']);
$this->assertEquals($collection['body']['indexes'][0]['$id'], $index['body']['$id']);
// Delete attribute
$this->client->call(Client::METHOD_DELETE, '/database/collections/' . $actors ['body']['$id'] . '/attributes/' . $unneededId, array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey']
]));
sleep(5);
$collection = $this->client->call(Client::METHOD_GET, '/database/collections/' . $actors['body']['$id'], array_merge([
@ -70,8 +202,45 @@ class DatabaseCustomServerTest extends Scope
$this->assertEquals($collection['body']['attributes'][0]['$id'], $firstName['body']['$id']);
$this->assertEquals($collection['body']['attributes'][1]['$id'], $lastName['body']['$id']);
return [
'collectionId' => $actors['body']['$id'],
'indexId' => $index['body']['$id'],
];
}
/**
* @depends testDeleteAttribute
*/
public function testDeleteIndex($data): array
{
$index = $this->client->call(Client::METHOD_DELETE, '/database/collections/' . $data['collectionId'] . '/indexes/'. $data['indexId'], array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey']
]));
// Wait for database worker to finish deleting index
sleep(5);
$collection = $this->client->call(Client::METHOD_GET, '/database/collections/' . $data['collectionId'], array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey']
]), []);
$this->assertCount(0, $collection['body']['indexes']);
return $data;
}
/**
* @depends testDeleteIndex
*/
public function testDeleteCollection($data)
{
$collectionId = $data['collectionId'];
// Add Documents to the collection
$document1 = $this->client->call(Client::METHOD_POST, '/database/collections/' . $actors['body']['$id'] . '/documents', array_merge([
$document1 = $this->client->call(Client::METHOD_POST, '/database/collections/' . $collectionId . '/documents', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
@ -84,7 +253,7 @@ class DatabaseCustomServerTest extends Scope
'write' => ['user:'.$this->getUser()['$id']],
]);
$document2 = $this->client->call(Client::METHOD_POST, '/database/collections/' . $actors['body']['$id'] . '/documents', array_merge([
$document2 = $this->client->call(Client::METHOD_POST, '/database/collections/' . $collectionId . '/documents', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
@ -98,7 +267,7 @@ class DatabaseCustomServerTest extends Scope
]);
$this->assertEquals($document1['headers']['status-code'], 201);
$this->assertEquals($document1['body']['$collection'], $actors['body']['$id']);
$this->assertEquals($document1['body']['$collection'], $collectionId);
$this->assertIsArray($document1['body']['$read']);
$this->assertIsArray($document1['body']['$write']);
$this->assertCount(1, $document1['body']['$read']);
@ -107,7 +276,7 @@ class DatabaseCustomServerTest extends Scope
$this->assertEquals($document1['body']['lastName'], 'Holland');
$this->assertEquals($document2['headers']['status-code'], 201);
$this->assertEquals($document2['body']['$collection'], $actors['body']['$id']);
$this->assertEquals($document2['body']['$collection'], $collectionId);
$this->assertIsArray($document2['body']['$read']);
$this->assertIsArray($document2['body']['$write']);
$this->assertCount(1, $document2['body']['$read']);
@ -116,7 +285,7 @@ class DatabaseCustomServerTest extends Scope
$this->assertEquals($document2['body']['lastName'], 'Jackson');
// Delete the actors collection
$response = $this->client->call(Client::METHOD_DELETE, '/database/collections/'.$actors['body']['$id'], array_merge([
$response = $this->client->call(Client::METHOD_DELETE, '/database/collections/' . $collectionId , array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey']
@ -126,7 +295,7 @@ class DatabaseCustomServerTest extends Scope
$this->assertEquals($response['body'],"");
// Try to get the collection and check if it has been deleted
$response = $this->client->call(Client::METHOD_GET, '/database/collections/'.$actors['body']['$id'], array_merge([
$response = $this->client->call(Client::METHOD_GET, '/database/collections/' . $collectionId , array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id']
], $this->getHeaders()));

View file

@ -112,7 +112,7 @@ class FunctionsCustomClientTest extends Scope
]);
$this->assertEquals(201, $execution['headers']['status-code']);
$execution = $this->client->call(Client::METHOD_POST, '/functions/'.$function['body']['$id'].'/executions', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
@ -121,7 +121,7 @@ class FunctionsCustomClientTest extends Scope
]);
$this->assertEquals(401, $execution['headers']['status-code']);
return [];
}
@ -187,7 +187,7 @@ class FunctionsCustomClientTest extends Scope
$this->assertEquals(201, $execution['headers']['status-code']);
$executionId = $execution['body']['$id'] ?? '';
sleep(10);
$executions = $this->client->call(Client::METHOD_GET, '/functions/'.$functionId.'/executions/'.$executionId, [
@ -212,19 +212,52 @@ class FunctionsCustomClientTest extends Scope
$this->assertEquals($this->getUser()['$id'], $output['APPWRITE_FUNCTION_USER_ID']);
$this->assertNotEmpty($output['APPWRITE_FUNCTION_JWT']);
$executions = $this->client->call(Client::METHOD_GET, '/functions/'.$functionId.'/executions', [
return [
'functionId' => $functionId
];
}
/**
* @depends testCreateCustomExecution
*/
public function testListExecutions(array $data)
{
$functionId = $data['functionId'];
$projectId = $this->getProject()['$id'];
$apikey = $this->getProject()['apiKey'];
$execution = $this->client->call(Client::METHOD_POST, '/functions/'.$functionId.'/executions', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $projectId,
], $this->getHeaders()), [
'data' => 'foobar',
]);
$this->assertEquals(201, $execution['headers']['status-code']);
sleep(10);
$base = $this->client->call(Client::METHOD_GET, '/functions/'.$functionId.'/executions', [
'content-type' => 'application/json',
'x-appwrite-project' => $projectId,
'x-appwrite-key' => $apikey,
]);
$this->assertEquals(200, $executions['headers']['status-code']);
$this->assertCount(1, $executions['body']['executions']);
$this->assertEquals('completed', $executions['body']['executions'][0]['status']);
$this->assertStringContainsString('foobar', $executions['body']['executions'][0]['stdout']);
$this->assertStringContainsString($this->getUser()['$id'], $executions['body']['executions'][0]['stdout']);
return [];
}
$this->assertEquals(200, $base['headers']['status-code']);
$this->assertCount(2, $base['body']['executions']);
$this->assertEquals('completed', $base['body']['executions'][0]['status']);
$this->assertEquals('completed', $base['body']['executions'][1]['status']);
$executions = $this->client->call(Client::METHOD_GET, '/functions/'.$functionId.'/executions', [
'content-type' => 'application/json',
'x-appwrite-project' => $projectId,
'x-appwrite-key' => $apikey,
], [
'after' => $base['body']['executions'][0]['$id']
]);
$this->assertCount(1, $executions['body']['executions']);
$this->assertEquals($base['body']['executions'][1]['$id'], $executions['body']['executions'][0]['$id']);
}
}

View file

@ -77,16 +77,51 @@ class FunctionsCustomServerTest extends Scope
/**
* Test for SUCCESS
*/
$function = $this->client->call(Client::METHOD_GET, '/functions', array_merge([
$response = $this->client->call(Client::METHOD_POST, '/functions', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'functionId' => 'unique()',
'name' => 'Test 2',
'runtime' => 'php-8.0',
'vars' => [
'funcKey1' => 'funcValue1',
'funcKey2' => 'funcValue2',
'funcKey3' => 'funcValue3',
],
'events' => [
'account.create',
'account.delete',
],
'schedule' => '0 0 1 1 *',
'timeout' => 10,
]);
$this->assertNotEmpty($response['body']['$id']);
$functions = $this->client->call(Client::METHOD_GET, '/functions', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()));
$this->assertEquals($function['headers']['status-code'], 200);
$this->assertEquals($function['body']['sum'], 1);
$this->assertIsArray($function['body']['functions']);
$this->assertCount(1, $function['body']['functions']);
$this->assertEquals($function['body']['functions'][0]['name'], 'Test');
$this->assertEquals($functions['headers']['status-code'], 200);
$this->assertEquals($functions['body']['sum'], 2);
$this->assertIsArray($functions['body']['functions']);
$this->assertCount(2, $functions['body']['functions']);
$this->assertEquals($functions['body']['functions'][0]['name'], 'Test');
$this->assertEquals($functions['body']['functions'][1]['name'], 'Test 2');
$response = $this->client->call(Client::METHOD_GET, '/functions', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'after' => $functions['body']['functions'][0]['$id']
]);
$this->assertEquals($response['headers']['status-code'], 200);
$this->assertCount(1, $response['body']['functions']);
$this->assertEquals($response['body']['functions'][0]['name'], 'Test 2');
return $data;
}

View file

@ -63,7 +63,7 @@ class ProjectsConsoleClientTest extends Scope
]);
$this->assertEquals(400, $response['headers']['status-code']);
$response = $this->client->call(Client::METHOD_POST, '/projects', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
@ -97,6 +97,60 @@ class ProjectsConsoleClientTest extends Scope
$this->assertEquals($id, $response['body']['projects'][0]['$id']);
$this->assertEquals('Project Test', $response['body']['projects'][0]['name']);
/**
* Test after pagination
*/
$team = $this->client->call(Client::METHOD_POST, '/teams', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'teamId' => 'unique()',
'name' => 'Project Test 2',
]);
$this->assertEquals(201, $team['headers']['status-code']);
$this->assertEquals('Project Test 2', $team['body']['name']);
$this->assertNotEmpty($team['body']['$id']);
$response = $this->client->call(Client::METHOD_POST, '/projects', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'projectId' => 'unique()',
'name' => 'Project Test 2',
'teamId' => $team['body']['$id'],
]);
$this->assertEquals(201, $response['headers']['status-code']);
$this->assertNotEmpty($response['body']['$id']);
$this->assertEquals('Project Test 2', $response['body']['name']);
$this->assertEquals($team['body']['$id'], $response['body']['teamId']);
$this->assertArrayHasKey('platforms', $response['body']);
$this->assertArrayHasKey('webhooks', $response['body']);
$this->assertArrayHasKey('keys', $response['body']);
$response = $this->client->call(Client::METHOD_GET, '/projects', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()));
$this->assertEquals(200, $response['headers']['status-code']);
$this->assertNotEmpty($response['body']);
$this->assertCount(2, $response['body']['projects']);
$this->assertEquals('Project Test', $response['body']['projects'][0]['name']);
$this->assertEquals('Project Test 2', $response['body']['projects'][1]['name']);
$response = $this->client->call(Client::METHOD_GET, '/projects', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()),[
'after' => $response['body']['projects'][0]['$id']
]);
$this->assertEquals(200, $response['headers']['status-code']);
$this->assertNotEmpty($response['body']);
$this->assertCount(1, $response['body']['projects']);
$this->assertEquals('Project Test 2', $response['body']['projects'][0]['name']);
/**
* Test for FAILURE
*/

View file

@ -35,7 +35,7 @@ trait StorageBase
*/
return ['fileId' => $file['body']['$id']];
}
/**
* @depends testCreateFile
*/
@ -158,19 +158,49 @@ trait StorageBase
/**
* Test for SUCCESS
*/
$file = $this->client->call(Client::METHOD_POST, '/storage/files', array_merge([
'content-type' => 'multipart/form-data',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'fileId' => 'unique()',
'file' => new CURLFile(realpath(__DIR__ . '/../../../resources/logo.png'), 'image/png', 'logo.png'),
'read' => ['role:all'],
'write' => ['role:all'],
]);
$this->assertEquals($file['headers']['status-code'], 201);
$this->assertNotEmpty($file['body']['$id']);
$this->assertIsInt($file['body']['dateCreated']);
$this->assertEquals('logo.png', $file['body']['name']);
$this->assertEquals('image/png', $file['body']['mimeType']);
$this->assertEquals(47218, $file['body']['sizeOriginal']);
$files = $this->client->call(Client::METHOD_GET, '/storage/files', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()));
], $this->getHeaders()), [
'limit' => 2
]);
$this->assertEquals(200, $files['headers']['status-code']);
$this->assertGreaterThan(0, $files['body']['sum']);
$this->assertGreaterThan(0, count($files['body']['files']));
$this->assertCount(2, $files['body']['files']);
$response = $this->client->call(Client::METHOD_GET, '/storage/files', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'limit' => 1,
'after' => $files['body']['files'][0]['$id']
]);
$this->assertEquals(200, $response['headers']['status-code']);
$this->assertEquals($files['body']['files'][1]['$id'], $response['body']['files'][0]['$id']);
$this->assertCount(1, $response['body']['files']);
/**
* Test for FAILURE
*/
return $data;
}

View file

@ -133,7 +133,7 @@ trait TeamsBase
$this->assertGreaterThan(0, $response['body']['sum']);
$this->assertIsInt($response['body']['sum']);
$this->assertCount(2, $response['body']['teams']);
$response = $this->client->call(Client::METHOD_GET, '/teams', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
@ -145,7 +145,7 @@ trait TeamsBase
$this->assertGreaterThan(0, $response['body']['sum']);
$this->assertIsInt($response['body']['sum']);
$this->assertGreaterThan(2, $response['body']['teams']);
$response = $this->client->call(Client::METHOD_GET, '/teams', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
@ -158,7 +158,7 @@ trait TeamsBase
$this->assertIsInt($response['body']['sum']);
$this->assertCount(1, $response['body']['teams']);
$this->assertEquals('Manchester United', $response['body']['teams'][0]['name']);
$response = $this->client->call(Client::METHOD_GET, '/teams', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
@ -172,6 +172,32 @@ trait TeamsBase
$this->assertCount(1, $response['body']['teams']);
$this->assertEquals('Manchester United', $response['body']['teams'][0]['name']);
$teams = $this->client->call(Client::METHOD_GET, '/teams', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'limit' => 2,
]);
$this->assertEquals(200, $teams['headers']['status-code']);
$this->assertGreaterThan(0, $teams['body']['sum']);
$this->assertIsInt($teams['body']['sum']);
$this->assertCount(2, $teams['body']['teams']);
$response = $this->client->call(Client::METHOD_GET, '/teams', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'limit' => 1,
'after' => $teams['body']['teams'][0]['$id']
]);
$this->assertEquals(200, $response['headers']['status-code']);
$this->assertGreaterThan(0, $response['body']['sum']);
$this->assertIsInt($response['body']['sum']);
$this->assertCount(1, $response['body']['teams']);
$this->assertEquals($teams['body']['teams'][1]['$id'], $response['body']['teams'][0]['$id']);
/**
* Test for FAILURE
*/

View file

@ -138,6 +138,35 @@ trait TeamsBaseClient
];
}
/**
* @depends testCreateTeamMembership
*/
public function testListTeamMemberships($data): void
{
$memberships = $this->client->call(Client::METHOD_GET, '/teams/'.$data['teamUid'].'/memberships', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()));
$this->assertEquals(200, $memberships['headers']['status-code']);
$this->assertIsInt($memberships['body']['sum']);
$this->assertNotEmpty($memberships['body']['memberships']);
$this->assertCount(2, $memberships['body']['memberships']);
$response = $this->client->call(Client::METHOD_GET, '/teams/'.$data['teamUid'].'/memberships', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'after' => $memberships['body']['memberships'][0]['$id']
]);
$this->assertEquals(200, $response['headers']['status-code']);
$this->assertIsInt($response['body']['sum']);
$this->assertNotEmpty($response['body']['memberships']);
$this->assertCount(1, $response['body']['memberships']);
$this->assertEquals($memberships['body']['memberships'][1]['$id'], $response['body']['memberships'][0]['$id']);
}
/**
* @depends testCreateTeamMembership
*/

View file

@ -50,6 +50,42 @@ trait UsersBase
return ['userId' => $user['body']['$id']];
}
/**
* @depends testCreateUser
*/
public function testListUsers(array $data): void
{
/**
* Test for SUCCESS
*/
$response = $this->client->call(Client::METHOD_GET, '/users', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()));
$this->assertEquals($response['headers']['status-code'], 200);
$this->assertNotEmpty($response['body']);
$this->assertNotEmpty($response['body']['users']);
$this->assertCount(2, $response['body']['users']);
$this->assertEquals($response['body']['users'][0]['$id'], $data['userId']);
$this->assertEquals($response['body']['users'][1]['$id'], 'user1');
$response = $this->client->call(Client::METHOD_GET, '/users', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'after' => $response['body']['users'][0]['$id']
]);
$this->assertEquals($response['headers']['status-code'], 200);
$this->assertNotEmpty($response['body']);
$this->assertNotEmpty($response['body']['users']);
$this->assertCount(1, $response['body']['users']);
$this->assertEquals($response['body']['users'][0]['$id'], 'user1');
}
/**
* @depends testCreateUser
*/

View file

@ -63,7 +63,7 @@ class WebhooksCustomServerTest extends Scope
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey']
]), [
'id' => 'fullname',
'indexId' => 'fullname',
'type' => 'key',
'attributes' => ['lastName', 'firstName'],
'orders' => ['ASC', 'ASC'],

View file

@ -0,0 +1,69 @@
<?php
namespace Appwrite\Tests;
use Appwrite\Stats\Stats;
use PHPUnit\Framework\TestCase;
use Utopia\App;
class StatsTest extends TestCase
{
/**
* @var Stats
*/
protected $object = null;
public function setUp(): void
{
$host = App::getEnv('_APP_STATSD_HOST', 'telegraf');
$port = App::getEnv('_APP_STATSD_PORT', 8125);
$connection = new \Domnikl\Statsd\Connection\UdpSocket($host, $port);
$statsd = new \Domnikl\Statsd\Client($connection);
$this->object = new Stats($statsd);
}
public function tearDown(): void
{
}
public function testNamespace()
{
$this->object->setNamespace('appwritetest.usage');
$this->assertEquals('appwritetest.usage', $this->object->getNamespace());
}
public function testParams()
{
$this->object
->setParam('projectId', 'appwrite_test')
->setParam('networkRequestSize', 100)
;
$this->assertEquals('appwrite_test', $this->object->getParam('projectId'));
$this->assertEquals(100, $this->object->getParam('networkRequestSize'));
$this->object->submit();
$this->assertEquals(null, $this->object->getParam('projectId'));
$this->assertEquals(null, $this->object->getParam('networkRequestSize'));
}
public function testReset()
{
$this->object
->setParam('projectId', 'appwrite_test')
->setParam('networkRequestSize', 100)
;
$this->assertEquals('appwrite_test', $this->object->getParam('projectId'));
$this->assertEquals(100, $this->object->getParam('networkRequestSize'));
$this->object->reset();
$this->assertEquals(null, $this->object->getParam('projectId'));
$this->assertEquals(null, $this->object->getParam('networkRequestSize'));
$this->assertEquals('appwrite.usage', $this->object->getNamespace());
}
}