Merge branch 'swoole-and-functions' of github.com:appwrite/appwrite into feat-add-webhook-events-to-the-users-service

This commit is contained in:
Eldad Fux committed 2020-09-10 23:55:29 +03:00
commit a54928782e
120 files changed
+21668 -23341

No files matched your search

+179 -45
View File
@@ -1,15 +1,16 @@
<?php
use Appwrite\Database\Database;
use Appwrite\Database\Document;
use Appwrite\Database\Validator\UID;
use Appwrite\Storage\Storage;
use Appwrite\Storage\Validator\File;
use Appwrite\Storage\Validator\FileSize;
use Appwrite\Storage\Validator\FileType;
use Appwrite\Storage\Validator\Upload;
use Appwrite\Utopia\Response;
use Appwrite\Task\Validator\Cron;
use Utopia\App;
use Utopia\Response;
use Utopia\Validator\ArrayList;
use Utopia\Validator\Assoc;
use Utopia\Validator\Text;
@@ -43,7 +44,7 @@ App::post('/v1/functions')
],
'dateCreated' => time(),
'dateUpdated' => time(),
'status' => 'paused',
'status' => 'disabled',
'name' => $name,
'env' => $env,
'tag' => '',
@@ -59,10 +60,8 @@ App::post('/v1/functions')
throw new Exception('Failed saving function to DB', 500);
}
$response
->setStatusCode(Response::STATUS_CODE_CREATED)
->json($function->getArrayCopy())
;
$response->setStatusCode(Response::STATUS_CODE_CREATED);
$response->dynamic($function, Response::MODEL_FUNCTION);
}, ['response', 'projectDB']);
App::get('/v1/functions')
@@ -90,7 +89,10 @@ App::get('/v1/functions')
],
]);
$response->json(['sum' => $projectDB->getSum(), 'functions' => $results]);
$response->dynamic(new Document([
'sum' => $projectDB->getSum(),
'functions' => $results
]), Response::MODEL_FUNCTION_LIST);
}, ['response', 'projectDB']);
App::get('/v1/functions/:functionId')
@@ -106,12 +108,125 @@ App::get('/v1/functions/:functionId')
$function = $projectDB->getDocument($functionId);
if (empty($function->getId()) || Database::SYSTEM_COLLECTION_FUNCTIONS != $function->getCollection()) {
throw new Exception('function not found', 404);
throw new Exception('Function not found', 404);
}
$response->json($function->getArrayCopy());
$response->dynamic($function, Response::MODEL_FUNCTION);
}, ['response', 'projectDB']);
App::get('/v1/functions/:functionId/usage')
->desc('Get Function Usage')
->groups(['api', 'functions'])
->label('scope', 'functions.read')
->label('sdk.platform', [APP_PLATFORM_CONSOLE])
->label('sdk.namespace', 'functions')
->label('sdk.method', 'getUsage')
->param('functionId', '', function () { return new UID(); }, 'Function unique ID.')
->param('range', '30d', function () { return new WhiteList(['24h', '7d', '30d', '90d']); }, 'Date range.', true)
->action(function ($functionId, $range, $response, $project, $projectDB, $register) {
/** @var Appwrite\Utopia\Response $response */
/** @var Appwrite\Database\Document $project */
/** @var Appwrite\Database\Database $consoleDB */
/** @var Appwrite\Database\Database $projectDB */
/** @var Utopia\Registry\Registry $register */
$function = $projectDB->getDocument($functionId);
if (empty($function->getId()) || Database::SYSTEM_COLLECTION_FUNCTIONS != $function->getCollection()) {
throw new Exception('Function not found', 404);
}
$period = [
'24h' => [
'start' => DateTime::createFromFormat('U', \strtotime('-24 hours')),
'end' => DateTime::createFromFormat('U', \strtotime('+1 hour')),
'group' => '30m',
],
'7d' => [
'start' => DateTime::createFromFormat('U', \strtotime('-7 days')),
'end' => DateTime::createFromFormat('U', \strtotime('now')),
'group' => '1d',
],
'30d' => [
'start' => DateTime::createFromFormat('U', \strtotime('-30 days')),
'end' => DateTime::createFromFormat('U', \strtotime('now')),
'group' => '1d',
],
'90d' => [
'start' => DateTime::createFromFormat('U', \strtotime('-90 days')),
'end' => DateTime::createFromFormat('U', \strtotime('now')),
'group' => '1d',
],
];
$client = $register->get('influxdb');
$executions = [];
$failures = [];
$compute = [];
if ($client) {
$start = $period[$range]['start']->format(DateTime::RFC3339);
$end = $period[$range]['end']->format(DateTime::RFC3339);
$database = $client->selectDB('telegraf');
// Executions
$result = $database->query('SELECT sum(value) AS "value" FROM "appwrite_usage_executions_all" WHERE time > \''.$start.'\' AND time < \''.$end.'\' AND "metric_type"=\'counter\' AND "project"=\''.$project->getId().'\' AND "functionId"=\''.$function->getId().'\' GROUP BY time('.$period[$range]['group'].') FILL(null)');
$points = $result->getPoints();
foreach ($points as $point) {
$executions[] = [
'value' => (!empty($point['value'])) ? $point['value'] : 0,
'date' => \strtotime($point['time']),
];
}
// Failures
$result = $database->query('SELECT sum(value) AS "value" FROM "appwrite_usage_executions_all" WHERE time > \''.$start.'\' AND time < \''.$end.'\' AND "metric_type"=\'counter\' AND "project"=\''.$project->getId().'\' AND "functionId"=\''.$function->getId().'\' AND "functionStatus"=\'failed\' GROUP BY time('.$period[$range]['group'].') FILL(null)');
$points = $result->getPoints();
foreach ($points as $point) {
$failures[] = [
'value' => (!empty($point['value'])) ? $point['value'] : 0,
'date' => \strtotime($point['time']),
];
}
// Compute
$result = $database->query('SELECT sum(value) AS "value" FROM "appwrite_usage_executions_time" WHERE time > \''.$start.'\' AND time < \''.$end.'\' AND "metric_type"=\'counter\' AND "project"=\''.$project->getId().'\' AND "functionId"=\''.$function->getId().'\' GROUP BY time('.$period[$range]['group'].') FILL(null)');
$points = $result->getPoints();
foreach ($points as $point) {
$compute[] = [
'value' => round((!empty($point['value'])) ? $point['value'] / 1000 : 0, 2), // minutes
'date' => \strtotime($point['time']),
];
}
}
$response->json([
'range' => $range,
'executions' => [
'data' => $executions,
'total' => \array_sum(\array_map(function ($item) {
return $item['value'];
}, $executions)),
],
'failures' => [
'data' => $failures,
'total' => \array_sum(\array_map(function ($item) {
return $item['value'];
}, $failures)),
],
'compute' => [
'data' => $compute,
'total' => \array_sum(\array_map(function ($item) {
return $item['value'];
}, $compute)),
],
]);
}, ['response', 'project', 'projectDB', 'register']);
App::put('/v1/functions/:functionId')
->groups(['api', 'functions'])
->desc('Update Function')
@@ -151,7 +266,7 @@ App::put('/v1/functions/:functionId')
throw new Exception('Failed saving function to DB', 500);
}
$response->json($function->getArrayCopy());
$response->dynamic($function, Response::MODEL_FUNCTION);
}, ['response', 'projectDB']);
App::patch('/v1/functions/:functionId/tag')
@@ -189,7 +304,7 @@ App::patch('/v1/functions/:functionId/tag')
throw new Exception('Failed saving function to DB', 500);
}
$response->json($function->getArrayCopy());
$response->dynamic($function, Response::MODEL_FUNCTION);
}, ['response', 'projectDB']);
App::delete('/v1/functions/:functionId')
@@ -201,7 +316,12 @@ App::delete('/v1/functions/:functionId')
->label('sdk.method', 'delete')
->label('sdk.description', '/docs/references/functions/delete-function.md')
->param('functionId', '', function () { return new UID(); }, 'Function unique ID.')
->action(function ($functionId, $response, $projectDB) {
->action(function ($functionId, $response, $project, $projectDB, $deletes) {
/** @var Appwrite\Utopia\Response $response */
/** @var Appwrite\Database\Document $project */
/** @var Appwrite\Database\Database $projectDB */
/** @var Appwrite\Event\Event $deletes */
$function = $projectDB->getDocument($functionId);
if (empty($function->getId()) || Database::SYSTEM_COLLECTION_FUNCTIONS != $function->getCollection()) {
@@ -212,8 +332,12 @@ App::delete('/v1/functions/:functionId')
throw new Exception('Failed to remove function from DB', 500);
}
$deletes
->setParam('document', $function->getArrayCopy())
;
$response->noContent();
}, ['response', 'projectDB']);
}, ['response', 'project', 'projectDB', 'deletes']);
App::post('/v1/functions/:functionId/tags')
->groups(['api', 'functions'])
@@ -276,11 +400,11 @@ App::post('/v1/functions/:functionId/tags')
'read' => [],
'write' => [],
],
'dateCreated' => time(),
'functionId' => $function->getId(),
'dateCreated' => time(),
'command' => $command,
'codePath' => $path,
'codeSize' => $size,
'path' => $path,
'size' => $size,
]);
if (false === $tag) {
@@ -288,13 +412,11 @@ App::post('/v1/functions/:functionId/tags')
}
$usage
->setParam('storage', $tag->getAttribute('codeSize', 0))
->setParam('storage', $tag->getAttribute('size', 0))
;
$response
->setStatusCode(Response::STATUS_CODE_CREATED)
->json($tag->getArrayCopy())
;
$response->setStatusCode(Response::STATUS_CODE_CREATED);
$response->dynamic($tag, Response::MODEL_TAG);
}, ['request', 'response', 'projectDB', 'usage']);
App::get('/v1/functions/:functionId/tags')
@@ -330,7 +452,10 @@ App::get('/v1/functions/:functionId/tags')
],
]);
$response->json(['sum' => $projectDB->getSum(), 'tags' => $results]);
$response->dynamic(new Document([
'sum' => $projectDB->getSum(),
'tags' => $results
]), Response::MODEL_TAG_LIST);
}, ['response', 'projectDB']);
App::get('/v1/functions/:functionId/tags/:tagId')
@@ -360,7 +485,7 @@ App::get('/v1/functions/:functionId/tags/:tagId')
throw new Exception('Tag not found', 404);
}
$response->json($tag->getArrayCopy());
$response->dynamic($tag, Response::MODEL_TAG);
}, ['response', 'projectDB']);
App::delete('/v1/functions/:functionId/tags/:tagId')
@@ -392,14 +517,24 @@ App::delete('/v1/functions/:functionId/tags/:tagId')
$device = Storage::getDevice('functions');
if ($device->delete($tag->getAttribute('codePath', ''))) {
if ($device->delete($tag->getAttribute('path', ''))) {
if (!$projectDB->deleteDocument($tag->getId())) {
throw new Exception('Failed to remove tag from DB', 500);
}
}
if($function->getAttribute('tag') === $tag->getId()) { // Reset function tag
$function = $projectDB->updateDocument(array_merge($function->getArrayCopy(), [
'tag' => '',
]));
if (false === $function) {
throw new Exception('Failed saving function to DB', 500);
}
}
$usage
->setParam('storage', $tag->getAttribute('codeSize', 0) * -1)
->setParam('storage', $tag->getAttribute('size', 0) * -1)
;
$response->noContent();
@@ -414,9 +549,9 @@ App::post('/v1/functions/:functionId/executions')
->label('sdk.method', 'createExecution')
->label('sdk.description', '/docs/references/functions/create-execution.md')
->param('functionId', '', function () { return new UID(); }, 'Function unique ID.')
->param('async', 1, function () { return new Range(0, 1); }, 'Execute code asynchronously. Pass 1 for true, 0 for false. Default value is 1.', true)
->action(function ($functionId, $async, $response, $project, $projectDB) {
/** @var Utopia\Response $response */
// ->param('async', 1, function () { return new Range(0, 1); }, 'Execute code asynchronously. Pass 1 for true, 0 for false. Default value is 1.', true)
->action(function ($functionId, /*$async,*/ $response, $project, $projectDB) {
/** @var Appwrite\Utopia\Response $response */
/** @var Appwrite\Database\Document $project */
/** @var Appwrite\Database\Database $projectDB */
@@ -444,6 +579,7 @@ App::post('/v1/functions/:functionId/executions')
],
'dateCreated' => time(),
'functionId' => $function->getId(),
'trigger' => 'http', // http / schedule / event
'status' => 'waiting', // waiting / processing / completed / failed
'exitCode' => 0,
'stdout' => '',
@@ -454,22 +590,17 @@ App::post('/v1/functions/:functionId/executions')
if (false === $execution) {
throw new Exception('Failed saving execution to DB', 500);
}
if((bool)$async) {
// Issue a TLS certificate when domain is verified
Resque::enqueue('v1-functions', 'FunctionsV1', [
'projectId' => $project->getId(),
'functionId' => $function->getId(),
'executionId' => $execution->getId(),
'functionTag' => $tag->getId(),
'functionTrigger' => 'API',
]);
}
// Issue a TLS certificate when domain is verified
Resque::enqueue('v1-functions', 'FunctionsV1', [
'projectId' => $project->getId(),
'functionId' => $function->getId(),
'executionId' => $execution->getId(),
'trigger' => 'http',
]);
$response
->setStatusCode(Response::STATUS_CODE_CREATED)
->json($execution->getArrayCopy())
;
$response->setStatusCode(Response::STATUS_CODE_CREATED);
$response->dynamic($execution, Response::MODEL_EXECUTION);
}, ['response', 'project', 'projectDB']);
App::get('/v1/functions/:functionId/executions')
@@ -505,7 +636,10 @@ App::get('/v1/functions/:functionId/executions')
],
]);
$response->json(['sum' => $projectDB->getSum(), 'executions' => $results]);
$response->dynamic(new Document([
'sum' => $projectDB->getSum(),
'executions' => $results
]), Response::MODEL_EXECUTION_LIST);
}, ['response', 'projectDB']);
App::get('/v1/functions/:functionId/executions/:executionId')
@@ -535,5 +669,5 @@ App::get('/v1/functions/:functionId/executions/:executionId')
throw new Exception('Execution not found', 404);
}
$response->json($execution->getArrayCopy());
$response->dynamic($execution, Response::MODEL_EXECUTION);
}, ['response', 'projectDB']);