Fix bucket stats auth

This commit is contained in:
Jake Barnby 2026-01-08 18:54:52 +13:00
parent e3d6fc123f
commit 4c1417944d
2 changed files with 34 additions and 24 deletions

View file

@ -51,6 +51,7 @@ class Get extends Action
->inject('dbForProject')
->inject('project')
->inject('getLogsDB')
->inject('authorization')
->callback($this->action(...));
}
@ -59,7 +60,8 @@ class Get extends Action
Response $response,
Database $dbForProject,
Document $project,
callable $getLogsDB
callable $getLogsDB,
Authorization $authorization,
): void {
$bucket = $dbForProject->getDocument('buckets', $bucketId);
@ -75,19 +77,20 @@ class Get extends Action
$statsDocId = md5('_inf_' . $metric);
$dbForLogs = call_user_func($getLogsDB, $project);
$storageStats = Authorization::skip(
fn () => $dbForLogs->getDocument(
$totalSize = 0;
try {
$dbForLogs = $getLogsDB($project);
$storageStats = $authorization->skip(fn () => $dbForLogs->getDocument(
'stats',
$statsDocId,
[Query::select(['value'])]
)
);
));
/**
* The value can be 0 if stats were not aggregated when this request was made!
*/
$totalSize = $storageStats->isEmpty() ? 0 : $storageStats->getAttribute('value', 0);
$totalSize = $storageStats->getAttribute('value', 0);
} catch (\Throwable) {
// Stats may not be available, default to 0
}
$bucket->setAttribute('totalSize', $totalSize);

View file

@ -58,6 +58,7 @@ class XList extends Action
->inject('dbForProject')
->inject('project')
->inject('getLogsDB')
->inject('authorization')
->callback($this->action(...));
}
@ -68,7 +69,8 @@ class XList extends Action
Response $response,
Database $dbForProject,
Document $project,
callable $getLogsDB
callable $getLogsDB,
Authorization $authorization
) {
try {
$queries = Query::parseQueries($queries);
@ -117,7 +119,6 @@ class XList extends Action
if (!empty($buckets)) {
$bucketByStatsId = [];
$dbForLogs = call_user_func($getLogsDB, $project);
foreach ($buckets as $bucket) {
$metric = str_replace(
@ -134,22 +135,28 @@ class XList extends Action
$bucket->setAttribute('totalSize', 0);
}
/* @type Document[] $stats */
$stats = Authorization::skip(function () use ($dbForLogs, $bucketByStatsId) {
$statsIds = array_keys($bucketByStatsId);
try {
$dbForLogs = $getLogsDB($project);
return $dbForLogs->find('stats', [
Query::equal('$id', $statsIds),
Query::select(['value']),
]);
});
/* @var array<Document> $stats */
$stats = $authorization->skip(function () use ($dbForLogs, $bucketByStatsId) {
$statsIds = array_keys($bucketByStatsId);
foreach ($stats as $stat) {
$bucket = $bucketByStatsId[$stat->getId()];
return $dbForLogs->find('stats', [
Query::equal('$id', $statsIds),
Query::select(['value']),
]);
});
if ($bucket) {
$bucket->setAttribute('totalSize', $stat->getAttribute('value', 0));
foreach ($stats as $stat) {
$bucket = $bucketByStatsId[$stat->getId()];
if ($bucket) {
$bucket->setAttribute('totalSize', $stat->getAttribute('value', 0));
}
}
} catch (\Throwable) {
// Stats may not be available, default to 0
}
}