From 1e29ec8d59acf7dbf51aee14501224ff0c0b6bb9 Mon Sep 17 00:00:00 2001 From: shimon Date: Mon, 8 May 2023 10:32:29 +0300 Subject: [PATCH 1/2] added bucketInternalId to file --- app/config/collections.php | 11 +++++++++++ app/controllers/api/storage.php | 2 ++ app/controllers/shared/api.php | 6 ++---- app/workers/usage.php | 4 ++-- 4 files changed, 17 insertions(+), 6 deletions(-) diff --git a/app/config/collections.php b/app/config/collections.php index 5648bfe6ea..12904a1e20 100644 --- a/app/config/collections.php +++ b/app/config/collections.php @@ -3410,6 +3410,17 @@ $collections = [ 'default' => null, 'filters' => [], ], + [ + 'array' => false, + '$id' => ID::custom('bucketInternalId'), + 'type' => Database::VAR_STRING, + 'format' => '', + 'size' => Database::LENGTH_KEY, + 'signed' => true, + 'required' => true, + 'default' => null, + 'filters' => [], + ], [ '$id' => ID::custom('name'), 'type' => Database::VAR_STRING, diff --git a/app/controllers/api/storage.php b/app/controllers/api/storage.php index bee37d0b1e..ab5febf8fa 100644 --- a/app/controllers/api/storage.php +++ b/app/controllers/api/storage.php @@ -561,6 +561,7 @@ App::post('/v1/storage/buckets/:bucketId/files') '$id' => $fileId, '$permissions' => $permissions, 'bucketId' => $bucket->getId(), + 'bucketInternalId' => $bucket->getInternalId(), 'name' => $fileName, 'path' => $path, 'signature' => $fileHash, @@ -610,6 +611,7 @@ App::post('/v1/storage/buckets/:bucketId/files') '$id' => ID::custom($fileId), '$permissions' => $permissions, 'bucketId' => $bucket->getId(), + 'bucketInternalId' => $bucket->getInternalId(), 'name' => $fileName, 'path' => $path, 'signature' => '', diff --git a/app/controllers/shared/api.php b/app/controllers/shared/api.php index f7d80b21a7..9fe0574df5 100644 --- a/app/controllers/shared/api.php +++ b/app/controllers/shared/api.php @@ -112,13 +112,11 @@ $databaseListener = function (string $event, Document $document, Document $proje } break; case str_starts_with($document->getCollection(), 'bucket_'): // files - $parts = explode('_', $document->getCollection()); - $bucketInternalId = $parts[1]; $queueForUsage ->addMetric(METRIC_FILES, $value) // per project ->addMetric(METRIC_FILES_STORAGE, $document->getAttribute('sizeOriginal') * $value) // per project - ->addMetric(str_replace('{bucketInternalId}', $bucketInternalId, METRIC_BUCKET_ID_FILES), $value) // per bucket - ->addMetric(str_replace('{bucketInternalId}', $bucketInternalId, METRIC_BUCKET_ID_FILES_STORAGE), $document->getAttribute('sizeOriginal') * $value); // per bucket + ->addMetric(str_replace('{bucketInternalId}', $document->getAttribute('bucketInternalId'), METRIC_BUCKET_ID_FILES), $value) // per bucket + ->addMetric(str_replace('{bucketInternalId}', $document->getAttribute('bucketInternalId'), METRIC_BUCKET_ID_FILES_STORAGE), $document->getAttribute('sizeOriginal') * $value); // per bucket break; case $document->getCollection() === 'functions': $queueForUsage diff --git a/app/workers/usage.php b/app/workers/usage.php index b0778f4cd4..135e64872d 100644 --- a/app/workers/usage.php +++ b/app/workers/usage.php @@ -28,8 +28,8 @@ $periods['inf'] = '0000-00-00 00:00'; const INFINITI_PERIOD = '_inf_'; /** - * On Documents that tied by relations like functions>deployments>build || documents>collection>database || buckets>files - * When we remove a parent document we need to deduct his children aggregation from the project scope + * On Documents that tied by relations like functions>deployments>build || documents>collection>database || buckets>files. + * When we remove a parent document we need to deduct his children aggregation from the project scope. */ Server::setResource('reduce', function (Cache $cache, Registry $register, $pools) { return function ($database, $projectInternalId, Document $document, array &$metrics) use ($pools, $cache, $register): void { From 13a4000312d694c55eb25df89f61ef42689b5920 Mon Sep 17 00:00:00 2001 From: shimon Date: Mon, 8 May 2023 13:45:53 +0300 Subject: [PATCH 2/2] fixed typo --- app/workers/usage.php | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/app/workers/usage.php b/app/workers/usage.php index 135e64872d..497550dd40 100644 --- a/app/workers/usage.php +++ b/app/workers/usage.php @@ -25,7 +25,7 @@ $periods['1d'] = 'Y-m-d 00:00'; //$periods['1m'] = 'Y-m-1 00:00'; $periods['inf'] = '0000-00-00 00:00'; -const INFINITI_PERIOD = '_inf_'; +const INFINITY_PERIOD = '_inf_'; /** * On Documents that tied by relations like functions>deployments>build || documents>collection>database || buckets>files. @@ -55,8 +55,8 @@ Server::setResource('reduce', function (Cache $cache, Registry $register, $pools } break; case $document->getCollection() === 'databases': // databases - $collections = $dbForProject->getDocument('stats', md5(INFINITI_PERIOD . str_replace('{databaseInternalId}', $document->getInternalId(), METRIC_DATABASE_ID_COLLECTIONS))); - $documents = $dbForProject->getDocument('stats', md5(INFINITI_PERIOD . str_replace('{databaseInternalId}', $document->getInternalId(), METRIC_DATABASE_ID_DOCUMENTS))); + $collections = $dbForProject->getDocument('stats', md5(INFINITY_PERIOD . str_replace('{databaseInternalId}', $document->getInternalId(), METRIC_DATABASE_ID_COLLECTIONS))); + $documents = $dbForProject->getDocument('stats', md5(INFINITY_PERIOD . str_replace('{databaseInternalId}', $document->getInternalId(), METRIC_DATABASE_ID_DOCUMENTS))); if (!empty($collections['value'])) { $metrics[] = [ 'key' => METRIC_COLLECTIONS, @@ -74,7 +74,7 @@ Server::setResource('reduce', function (Cache $cache, Registry $register, $pools case str_starts_with($document->getCollection(), 'database_') && !str_contains($document->getCollection(), 'collection'): //collections $parts = explode('_', $document->getCollection()); $databaseInternalId = $parts[1] ?? 0; - $documents = $dbForProject->getDocument('stats', md5(INFINITI_PERIOD . str_replace(['{databaseInternalId}', '{collectionInternalId}'], [$databaseInternalId, $document->getInternalId()], METRIC_DATABASE_ID_COLLECTION_ID_DOCUMENTS))); + $documents = $dbForProject->getDocument('stats', md5(INFINITY_PERIOD . str_replace(['{databaseInternalId}', '{collectionInternalId}'], [$databaseInternalId, $document->getInternalId()], METRIC_DATABASE_ID_COLLECTION_ID_DOCUMENTS))); if (!empty($documents['value'])) { $metrics[] = [ @@ -89,8 +89,8 @@ Server::setResource('reduce', function (Cache $cache, Registry $register, $pools break; case $document->getCollection() === 'buckets': - $files = $dbForProject->getDocument('stats', md5(INFINITI_PERIOD . str_replace('{bucketInternalId}', $document->getInternalId(), METRIC_BUCKET_ID_FILES))); - $storage = $dbForProject->getDocument('stats', md5(INFINITI_PERIOD . str_replace('{bucketInternalId}', $document->getInternalId(), METRIC_BUCKET_ID_FILES_STORAGE))); + $files = $dbForProject->getDocument('stats', md5(INFINITY_PERIOD . str_replace('{bucketInternalId}', $document->getInternalId(), METRIC_BUCKET_ID_FILES))); + $storage = $dbForProject->getDocument('stats', md5(INFINITY_PERIOD . str_replace('{bucketInternalId}', $document->getInternalId(), METRIC_BUCKET_ID_FILES_STORAGE))); if (!empty($files['value'])) { $metrics[] = [ @@ -108,13 +108,13 @@ Server::setResource('reduce', function (Cache $cache, Registry $register, $pools break; case $document->getCollection() === 'functions': - $deployments = $dbForProject->getDocument('stats', md5(INFINITI_PERIOD . str_replace(['{resourceType}', '{resourceInternalId}'], ['functions', $document->getInternalId()], METRIC_FUNCTION_ID_DEPLOYMENTS))); - $deploymentsStorage = $dbForProject->getDocument('stats', md5(INFINITI_PERIOD . str_replace(['{resourceType}', '{resourceInternalId}'], ['functions', $document->getInternalId()], METRIC_FUNCTION_ID_DEPLOYMENTS_STORAGE))); - $builds = $dbForProject->getDocument('stats', md5(INFINITI_PERIOD . str_replace('{functionInternalId}', $document->getInternalId(), METRIC_FUNCTION_ID_BUILDS))); - $buildsStorage = $dbForProject->getDocument('stats', md5(INFINITI_PERIOD . str_replace('{functionInternalId}', $document->getInternalId(), METRIC_FUNCTION_ID_BUILDS_STORAGE))); - $buildsCompute = $dbForProject->getDocument('stats', md5(INFINITI_PERIOD . str_replace('{functionInternalId}', $document->getInternalId(), METRIC_FUNCTION_ID_BUILDS_COMPUTE))); - $executions = $dbForProject->getDocument('stats', md5(INFINITI_PERIOD . str_replace('{functionInternalId}', $document->getInternalId(), METRIC_FUNCTION_ID_EXECUTIONS))); - $executionsCompute = $dbForProject->getDocument('stats', md5(INFINITI_PERIOD . str_replace('{functionInternalId}', $document->getInternalId(), METRIC_FUNCTION_ID_EXECUTIONS_COMPUTE))); + $deployments = $dbForProject->getDocument('stats', md5(INFINITY_PERIOD . str_replace(['{resourceType}', '{resourceInternalId}'], ['functions', $document->getInternalId()], METRIC_FUNCTION_ID_DEPLOYMENTS))); + $deploymentsStorage = $dbForProject->getDocument('stats', md5(INFINITY_PERIOD . str_replace(['{resourceType}', '{resourceInternalId}'], ['functions', $document->getInternalId()], METRIC_FUNCTION_ID_DEPLOYMENTS_STORAGE))); + $builds = $dbForProject->getDocument('stats', md5(INFINITY_PERIOD . str_replace('{functionInternalId}', $document->getInternalId(), METRIC_FUNCTION_ID_BUILDS))); + $buildsStorage = $dbForProject->getDocument('stats', md5(INFINITY_PERIOD . str_replace('{functionInternalId}', $document->getInternalId(), METRIC_FUNCTION_ID_BUILDS_STORAGE))); + $buildsCompute = $dbForProject->getDocument('stats', md5(INFINITY_PERIOD . str_replace('{functionInternalId}', $document->getInternalId(), METRIC_FUNCTION_ID_BUILDS_COMPUTE))); + $executions = $dbForProject->getDocument('stats', md5(INFINITY_PERIOD . str_replace('{functionInternalId}', $document->getInternalId(), METRIC_FUNCTION_ID_EXECUTIONS))); + $executionsCompute = $dbForProject->getDocument('stats', md5(INFINITY_PERIOD . str_replace('{functionInternalId}', $document->getInternalId(), METRIC_FUNCTION_ID_EXECUTIONS_COMPUTE))); if (!empty($deployments['value'])) { $metrics[] = [