From b89e04f5611c93c020454da7ee33503f9a87f192 Mon Sep 17 00:00:00 2001 From: shimon Date: Thu, 22 Dec 2022 19:18:54 +0200 Subject: [PATCH] add internalId to collections --- app/config/collections.php | 57 +++++++++++++++++++++++++++++-- app/controllers/api/functions.php | 4 +++ app/controllers/shared/api.php | 6 ++-- app/workers/builds.php | 10 +++--- app/workers/functions.php | 2 +- 5 files changed, 69 insertions(+), 10 deletions(-) diff --git a/app/config/collections.php b/app/config/collections.php index 430671a5c5..96ad8bf922 100644 --- a/app/config/collections.php +++ b/app/config/collections.php @@ -61,7 +61,6 @@ $collections = [ ], ], ], - 'collections' => [ '$collection' => ID::custom('databases'), '$id' => ID::custom('collections'), @@ -2371,6 +2370,17 @@ $collections = [ '$id' => ID::custom('deployments'), 'name' => 'Deployments', 'attributes' => [ + [ + '$id' => ID::custom('resourceInternalId'), + 'type' => Database::VAR_STRING, + 'format' => '', + 'size' => Database::LENGTH_KEY, + 'signed' => true, + 'required' => false, + 'default' => null, + 'array' => false, + 'filters' => [], + ], [ '$id' => ID::custom('resourceId'), 'type' => Database::VAR_STRING, @@ -2393,6 +2403,17 @@ $collections = [ 'array' => false, 'filters' => [], ], + [ + '$id' => ID::custom('buildInternalId'), + 'type' => Database::VAR_STRING, + 'format' => '', + 'size' => Database::LENGTH_KEY, + 'signed' => true, + 'required' => false, + 'default' => null, + 'array' => false, + 'filters' => [], + ], [ '$id' => ID::custom('buildId'), 'type' => Database::VAR_STRING, @@ -2573,6 +2594,17 @@ $collections = [ 'array' => false, 'filters' => [], ], + [ + '$id' => 'deploymentInternalId', + 'type' => Database::VAR_STRING, + 'format' => '', + 'size' => Database::LENGTH_KEY, + 'signed' => true, + 'required' => false, + 'default' => null, + 'array' => false, + 'filters' => [], + ], [ '$id' => ID::custom('deploymentId'), 'type' => Database::VAR_STRING, @@ -2689,6 +2721,17 @@ $collections = [ '$id' => ID::custom('executions'), 'name' => 'Executions', 'attributes' => [ + [ + '$id' => ID::custom('functionInternalId'), + 'type' => Database::VAR_STRING, + 'format' => '', + 'size' => Database::LENGTH_KEY, + 'signed' => true, + 'required' => false, + 'default' => null, + 'array' => false, + 'filters' => [], + ], [ '$id' => ID::custom('functionId'), 'type' => Database::VAR_STRING, @@ -2700,6 +2743,17 @@ $collections = [ 'array' => false, 'filters' => [], ], + [ + '$id' => ID::custom('deploymentInternalId'), + 'type' => Database::VAR_STRING, + 'format' => '', + 'size' => Database::LENGTH_KEY, + 'signed' => true, + 'required' => false, + 'default' => null, + 'array' => false, + 'filters' => [], + ], [ '$id' => ID::custom('deploymentId'), 'type' => Database::VAR_STRING, @@ -3318,7 +3372,6 @@ $collections = [ ], ], ], - 'files' => [ '$collection' => ID::custom('buckets'), '$id' => ID::custom('files'), diff --git a/app/controllers/api/functions.php b/app/controllers/api/functions.php index c048404937..baff261f80 100644 --- a/app/controllers/api/functions.php +++ b/app/controllers/api/functions.php @@ -526,6 +526,7 @@ App::post('/v1/functions/:functionId/deployments') Permission::update(Role::any()), Permission::delete(Role::any()), ], + 'resourceInternalId' => $function->getInternalId(), 'resourceId' => $function->getId(), 'resourceType' => 'functions', 'entrypoint' => $entrypoint, @@ -556,6 +557,7 @@ App::post('/v1/functions/:functionId/deployments') Permission::update(Role::any()), Permission::delete(Role::any()), ], + 'resourceInternalId' => $function->getInternalId(), 'resourceId' => $function->getId(), 'resourceType' => 'functions', 'entrypoint' => $entrypoint, @@ -902,7 +904,9 @@ App::post('/v1/functions/:functionId/executions') $execution = Authorization::skip(fn () => $dbForProject->createDocument('executions', new Document([ '$id' => $executionId, '$permissions' => !$user->isEmpty() ? [Permission::read(Role::user($user->getId()))] : [], + 'functionInternalId' => $function->getInternalId(), 'functionId' => $function->getId(), + 'deploymentInternalId' => $deployment->getInternalId(), 'deploymentId' => $deployment->getId(), 'trigger' => 'http', // http / schedule / event 'status' => $async ? 'waiting' : 'processing', // waiting / processing / completed / failed diff --git a/app/controllers/shared/api.php b/app/controllers/shared/api.php index d126ac926e..bfcf6f1a2e 100644 --- a/app/controllers/shared/api.php +++ b/app/controllers/shared/api.php @@ -194,14 +194,14 @@ $databaseListener = function (string $event, Document $document, Document $proje $queueForUsage ->addMetric("deployments", $value) // per project ->addMetric("deployments.storage", $document->getAttribute('size') * $value) // per project - ->addMetric("{$document->getAttribute('resourceType')}" . "." . "{$document->getAttribute('resourceId')}" . ".deployments", $value)// per function - ->addMetric("{$document->getAttribute('resourceType')}" . "." . "{$document->getAttribute('resourceId')}" . ".deployments.storage", $document->getAttribute('size') * $value) // per function + ->addMetric("{$document->getAttribute('resourceType')}" . "." . "{$document->getAttribute('resourceInternalId')}" . ".deployments", $value)// per function + ->addMetric("{$document->getAttribute('resourceType')}" . "." . "{$document->getAttribute('resourceInternalId')}" . ".deployments.storage", $document->getAttribute('size') * $value) // per function ; break; case $document->getCollection() === 'executions': $queueForUsage ->addMetric("executions", $value) // per project - ->addMetric("{$document->getAttribute('functionId')}" . ".executions", $value) // per function + ->addMetric("{$document->getAttribute('functionInternalId')}" . ".executions", $value) // per function ; break; default: diff --git a/app/workers/builds.php b/app/workers/builds.php index e763326785..523ef42679 100644 --- a/app/workers/builds.php +++ b/app/workers/builds.php @@ -96,6 +96,7 @@ class BuildsV1 extends Worker '$id' => $buildId, '$permissions' => [], 'startTime' => $startTime, + 'deploymentInternalId' => $deployment->getInternalId(), 'deploymentId' => $deployment->getId(), 'status' => 'processing', 'path' => '', @@ -107,7 +108,8 @@ class BuildsV1 extends Worker 'stderr' => '', 'duration' => 0 ])); - $deployment->setAttribute('buildId', $buildId); + $deployment->setAttribute('buildId', $build->getId()); + $deployment->setAttribute('buildInternalId', $build->getInternalId()); $deployment = $dbForProject->updateDocument('deployments', $deployment->getId(), $deployment); } else { $build = $dbForProject->getDocument('builds', $buildId); @@ -254,9 +256,9 @@ class BuildsV1 extends Worker ->addMetric("builds", 1) // per project ->addMetric("builds.storage", $build->getAttribute('size', 0)) ->addMetric("builds.compute", $build->getAttribute('duration', 0)) - ->addMetric("{$function->getId()}" . ".builds", 1) // per function - ->addMetric("{$function->getId()}" . ".builds.storage", $build->getAttribute('size', 0)) - ->addMetric("{$function->getId()}" . ".builds.compute", $build->getAttribute('duration', 0)) + ->addMetric("{$function->getInternalId()}" . ".builds", 1) // per function + ->addMetric("{$function->getInternalId()}" . ".builds.storage", $build->getAttribute('size', 0)) + ->addMetric("{$function->getInternalId()}" . ".builds.compute", $build->getAttribute('duration', 0)) ->trigger() ; } diff --git a/app/workers/functions.php b/app/workers/functions.php index 63e1b5582e..aa869ec3c4 100644 --- a/app/workers/functions.php +++ b/app/workers/functions.php @@ -215,7 +215,7 @@ Server::setResource('execute', function () { $queueForUsage ->setProject($project) ->addMetric('executions.compute', (int)($execution->getAttribute('duration') * 1000))// per project - ->addMetric("{$function->getId()}" . ".executions.compute", (int)($execution->getAttribute('duration') * 1000))// per function + ->addMetric("{$function->getInternalId()}" . ".executions.compute", (int)($execution->getAttribute('duration') * 1000))// per function ->trigger() ; };