1
0
Fork 0
mirror of synced 2024-10-03 02:37:40 +13:00

e2e tests

This commit is contained in:
shimon 2022-12-15 11:45:43 +02:00
parent 285bfe9fd7
commit 7601c9047b
6 changed files with 24 additions and 22 deletions

View file

@ -18,7 +18,6 @@ use Utopia\Validator\ArrayList;
use Utopia\Validator\JSON;
use Utopia\Database\Database;
use Utopia\Database\Document;
use Utopia\Database\DateTime;
use Utopia\Database\Query;
use Utopia\Database\Validator\Authorization;
use Utopia\Database\Validator\Key;

View file

@ -6,6 +6,7 @@ use Appwrite\Event\Build;
use Appwrite\Event\Delete;
use Appwrite\Event\Event;
use Appwrite\Event\Func;
use Appwrite\Event\Usage;
use Appwrite\Event\Validator\Event as ValidatorEvent;
use Appwrite\Extend\Exception;
use Appwrite\Utopia\Database\Validator\CustomId;
@ -851,7 +852,8 @@ App::post('/v1/functions/:functionId/executions')
->inject('events')
->inject('mode')
->inject('queueForFunctions')
->action(function (string $functionId, string $data, bool $async, Response $response, Document $project, Database $dbForProject, Document $user, Event $events, string $mode, Func $queueForFunctions) {
->inject('queueForUsage')
->action(function (string $functionId, string $data, bool $async, Response $response, Document $project, Database $dbForProject, Document $user, Event $events, string $mode, Func $queueForFunctions, Usage $queueForUsage) {
$function = Authorization::skip(fn () => $dbForProject->getDocument('functions', $functionId));
@ -993,6 +995,13 @@ App::post('/v1/functions/:functionId/executions')
$execution->setAttribute('stdout', $executionResponse['stdout']);
$execution->setAttribute('stderr', $executionResponse['stderr']);
$execution->setAttribute('duration', $executionResponse['duration']);
/**
* Sync execution compute usage from
*/
$queueForUsage
->addMetric('executions.compute', (int)($executionResponse['duration'] * 1000))// per project
->addMetric("{$function->getId()}" . ".executions.compute", (int)($executionResponse['duration'] * 1000))// per function
;
} catch (\Throwable $th) {
$interval = (new \DateTime())->diff(new \DateTime($execution->getCreatedAt()));
$execution

View file

@ -270,16 +270,13 @@ $databaseListener = function (string $event, array $args, Document $project, Usa
->addMetric("builds", $value) // per project
->addMetric("builds.compute", $document->getAttribute('duration') * $value) // per project
->addMetric("{$deployment['resourceId']}" . ".builds", $value) // per function
->addMetric("{$deployment['resourceId']}" . ".builds.compute", ($document->getAttribute('duration') * 1000) * $value) // per function
->addMetric("{$deployment['resourceId']}" . ".builds.compute", (int)($document->getAttribute('duration') * 1000) * $value) // per function
;
break;
case $document->getCollection() === 'executions':
var_dump($document);
$queueForUsage
->addMetric("executions", $value) // per project
->addMetric("executions.compute", $document->getAttribute('duration') * $value) // per project
->addMetric("{$document['functionId']}" . ".executions", $value) // per function
->addMetric("{$document['functionId']}" . ".executions.compute", ($document->getAttribute('duration') * 1000) * $value) // per function
;
break;
default:
@ -626,8 +623,6 @@ App::shutdown()
}
}
var_dump($mode);
var_dump($project->getId());
if (
$project->getId() !== 'console' && $mode !== APP_MODE_ADMIN
) {

View file

@ -101,6 +101,7 @@ Server::setResource('execute', function () {
/**
* Usage
*/
$queueForUsage
->addMetric('executions', 1) // per project
->addMetric("{$function->getId()}" . ".executions", 1); // per function
@ -213,8 +214,8 @@ Server::setResource('execute', function () {
/** Trigger usage queue */
$queueForUsage
->setProject($project)
->addMetric('executions.compute', $execution->getAttribute('duration'))// per project
->addMetric("{$function->getId()}" . ".executions.compute", $execution->getAttribute('duration') * 1000)// per function
->addMetric('executions.compute', (int)($execution->getAttribute('duration') * 1000))// per project
->addMetric("{$function->getId()}" . ".executions.compute", (int)($execution->getAttribute('duration') * 1000))// per function
->trigger()
;
};

View file

@ -18,14 +18,11 @@ class Usage extends Event
* Add metric.
*
* @param string $key
* @param int|float $value
* @param int $value
* @return self
*/
public function addMetric(string $key, int|float $value): self
public function addMetric(string $key, int $value): self
{
if($key == 'executions.compute'){
var_dump($value);
}
$this->metrics[] = [
'key' => $key,
'value' => $value,
@ -43,6 +40,8 @@ class Usage extends Event
{
$client = new Client($this->queue, $this->connection);
var_dump($this->metrics);
return $client->enqueue([
'project' => $this->getProject(),
'metrics' => $this->metrics,

View file

@ -256,9 +256,7 @@ class UsageTest extends Scope
$this->assertEquals(30, count($res['storage']));
$this->assertEquals($requestsCount, $res['requests'][array_key_last($res['requests'])]['value']);
$this->validateDates($res['requests']);
var_dump($storageTotal);
var_dump($res['storage'][array_key_last($res['storage'])]['value']);
exit;
$this->assertEquals($storageTotal, $res['storage'][array_key_last($res['storage'])]['value']);
$this->validateDates($res['storage']);
@ -516,6 +514,7 @@ class UsageTest extends Scope
$this->assertEquals($functionId, $execution['body']['functionId']);
$executionTime += (int) ($execution['body']['duration'] * 1000);
if ($execution['body']['status'] == 'failed') {
$failures++;
} elseif ($execution['body']['status'] == 'completed') {
@ -553,6 +552,7 @@ class UsageTest extends Scope
} elseif ($execution['body']['status'] == 'completed') {
$executions++;
}
$executionTime += (int) ($execution['body']['duration'] * 1000);
$data = array_merge($data, [
@ -590,18 +590,17 @@ class UsageTest extends Scope
$this->assertIsArray($response['body']['executionsCompute']);
$response = $response['body'];
var_dump($executions);
var_dump($response['executions'][array_key_last($response['executions'])]['value']);
exit;
$this->assertEquals($executions, $response['executions'][array_key_last($response['executions'])]['value']);
$this->validateDates($response['executions']);
$this->assertEquals($executionTime, $response['executionsCompute'][array_key_last($response['executionsCompute'])]['value']);
$this->validateDates($response['executionsCompute']);
$response = $this->client->call(Client::METHOD_GET, '/functions/usage?range=30d', $data['consoleHeaders']);
$this->assertEquals(200, $response['headers']['status-code']);
$this->assertEquals(6, count($response['body']));
$this->assertEquals(8, count($response['body']));
$this->assertEquals($response['body']['range'], '30d');
$this->assertIsArray($response['body']['functions']);
$this->assertIsArray($response['body']['deployments']);