diff --git a/app/controllers/api/storage.php b/app/controllers/api/storage.php index f1450ba77f..32d0c9f810 100644 --- a/app/controllers/api/storage.php +++ b/app/controllers/api/storage.php @@ -1478,7 +1478,7 @@ App::get('/v1/storage/:bucketId/usage') $usage = []; if (App::getEnv('_APP_USAGE_STATS', 'enabled') === 'enabled') { - $period = [ + $periods = [ '24h' => [ 'period' => '30m', 'limit' => 48, @@ -1508,12 +1508,14 @@ App::get('/v1/storage/:bucketId/usage') $stats = []; - Authorization::skip(function() use ($dbForInternal, $period, $range, $metrics, &$stats) { + Authorization::skip(function() use ($dbForInternal, $periods, $range, $metrics, &$stats) { foreach ($metrics as $metric) { + $limit = $periods[$range]['limit']; + $period = $periods[$range]['period']; $requestDocs = $dbForInternal->find('stats', [ - new Query('period', Query::TYPE_EQUAL, [$period[$range]['period']]), + new Query('period', Query::TYPE_EQUAL, [$period]), new Query('metric', Query::TYPE_EQUAL, [$metric]), - ], $period[$range]['limit'], 0, ['time'], [Database::ORDER_DESC]); + ], $limit, 0, ['time'], [Database::ORDER_DESC]); $stats[$metric] = []; foreach ($requestDocs as $requestDoc) { @@ -1522,6 +1524,21 @@ App::get('/v1/storage/:bucketId/usage') 'date' => $requestDoc->getAttribute('time'), ]; } + + // backfill metrics with empty values for graphs + $backfill = $limit - \count($requestDocs); + while ($backfill > 0) { + $last = $limit - $backfill - 1; // array index of last added metric + $diff = match($period) { // convert period to seconds for unix timestamp math + '30m' => 1800, + '1d' => 86400, + }; + $stats[$metric][] = [ + 'value' => 0, + 'date' => ($stats[$metric][$last]['date'] ?? \time()) - $diff, // time of last metric minus period + ]; + $backfill--; + } $stats[$metric] = array_reverse($stats[$metric]); } }); diff --git a/app/views/console/storage/bucket.phtml b/app/views/console/storage/bucket.phtml index f716cc3415..9095ab9ce9 100644 --- a/app/views/console/storage/bucket.phtml +++ b/app/views/console/storage/bucket.phtml @@ -293,7 +293,82 @@ $fileLimitHuman = $this->getParam('fileLimitHuman', 0);