mirror of
https://github.com/appwrite/appwrite
synced 2026-05-23 00:49:02 +00:00
address comments.
This commit is contained in:
parent
68fc6b0600
commit
904b7312d6
2 changed files with 38 additions and 56 deletions
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue