From 904b7312d64a48d844b9fc064166c186ed9e0745 Mon Sep 17 00:00:00 2001 From: Darshan Date: Wed, 7 Jan 2026 18:22:36 +0530 Subject: [PATCH] address comments. --- .../Modules/Storage/Http/Buckets/Get.php | 15 +--- .../Modules/Storage/Http/Buckets/XList.php | 79 ++++++++----------- 2 files changed, 38 insertions(+), 56 deletions(-) diff --git a/src/Appwrite/Platform/Modules/Storage/Http/Buckets/Get.php b/src/Appwrite/Platform/Modules/Storage/Http/Buckets/Get.php index 61954c0a00..5c3515122b 100644 --- a/src/Appwrite/Platform/Modules/Storage/Http/Buckets/Get.php +++ b/src/Appwrite/Platform/Modules/Storage/Http/Buckets/Get.php @@ -67,17 +67,6 @@ class Get extends Action throw new Exception(Exception::STORAGE_BUCKET_NOT_FOUND); } - $dbForLogs = call_user_func($getLogsDB, $project); - $this->addBucketStorageSize($dbForLogs, $bucket); - - $response->dynamic($bucket, Response::MODEL_BUCKET); - } - - /** - * Adds the latest aggregated bucket storage size from logs DB stats. - */ - private function addBucketStorageSize(Database $dbForLogs, Document $bucket): void - { $metric = str_replace( '{bucketInternalId}', $bucket->getSequence(), @@ -85,6 +74,8 @@ class Get extends Action ); $statsDocId = md5('_inf_' . $metric); + + $dbForLogs = call_user_func($getLogsDB, $project); $storageStats = Authorization::skip( fn () => $dbForLogs->getDocument( 'stats', @@ -99,5 +90,7 @@ class Get extends Action $totalSize = $storageStats->isEmpty() ? 0 : $storageStats->getAttribute('value', 0); $bucket->setAttribute('totalSize', $totalSize); + + $response->dynamic($bucket, Response::MODEL_BUCKET); } } diff --git a/src/Appwrite/Platform/Modules/Storage/Http/Buckets/XList.php b/src/Appwrite/Platform/Modules/Storage/Http/Buckets/XList.php index a7a785304c..a2c880ce08 100644 --- a/src/Appwrite/Platform/Modules/Storage/Http/Buckets/XList.php +++ b/src/Appwrite/Platform/Modules/Storage/Http/Buckets/XList.php @@ -116,8 +116,41 @@ class XList extends Action } if (!empty($buckets)) { + $bucketByStatsId = []; $dbForLogs = call_user_func($getLogsDB, $project); - $this->addBucketStorageSizes($dbForLogs, $buckets); + + foreach ($buckets as $bucket) { + $metric = str_replace( + '{bucketInternalId}', + $bucket->getSequence(), + METRIC_BUCKET_ID_FILES_STORAGE + ); + + $statId = md5('_inf_' . $metric); + + $bucketByStatsId[$statId] = $bucket; + + // set a default + $bucket->setAttribute('totalSize', 0); + } + + /* @type Document[] $stats */ + $stats = Authorization::skip(function () use ($dbForLogs, $bucketByStatsId) { + $statsIds = array_keys($bucketByStatsId); + + return $dbForLogs->find('stats', [ + Query::equal('$id', $statsIds), + Query::select(['value']), + ]); + }); + + foreach ($stats as $stat) { + $bucket = $bucketByStatsId[$stat->getId()]; + + if ($bucket) { + $bucket->setAttribute('totalSize', $stat->getAttribute('value', 0)); + } + } } $response->dynamic(new Document([ @@ -125,48 +158,4 @@ class XList extends Action 'total' => $total, ]), Response::MODEL_BUCKET_LIST); } - - /** - * Adds the latest aggregated bucket storage sizes from logs DB stats. - */ - private function addBucketStorageSizes(Database $dbForLogs, array $buckets): void - { - $bucketByStatsId = []; - - foreach ($buckets as $bucket) { - $metric = str_replace( - '{bucketInternalId}', - $bucket->getSequence(), - METRIC_BUCKET_ID_FILES_STORAGE - ); - - $statId = md5('_inf_' . $metric); - - $bucketByStatsId[$statId] = $bucket; - - // set a default - $bucket->setAttribute('totalSize', 0); - } - - /* @type Document[] $stats */ - $stats = Authorization::skip(function () use ($dbForLogs, $bucketByStatsId) { - $statsIds = array_keys($bucketByStatsId); - - return $dbForLogs->find('stats', [ - Query::equal('$id', $statsIds), - Query::select(['value']), - ]); - }); - - foreach ($stats as $stat) { - $bucket = $bucketByStatsId[$stat->getId()]; - - if ($bucket) { - $bucket->setAttribute( - 'totalSize', - $stat->getAttribute('value', 0) - ); - } - } - } }