From 070b7d827013a39a42bf8a09edbc6b40f1ea01c4 Mon Sep 17 00:00:00 2001 From: Christy Jacob Date: Fri, 20 Aug 2021 14:30:39 +0530 Subject: [PATCH] feat(usage): added usage endpoint to storage api --- app/controllers/api/storage.php | 72 +++++++++++++++++---------------- 1 file changed, 37 insertions(+), 35 deletions(-) diff --git a/app/controllers/api/storage.php b/app/controllers/api/storage.php index daecfb64e6..d167ee88b7 100644 --- a/app/controllers/api/storage.php +++ b/app/controllers/api/storage.php @@ -706,40 +706,42 @@ App::get('/v1/storage/:bucketId/usage') $stats[$metric] = array_reverse($stats[$metric]); } + + Authorization::reset(); + + $create = $stats["storage.buckets.$bucketId.files.create"] ?? []; + $read = $stats["storage.buckets.$bucketId.files.read"] ?? []; + $update = $stats["storage.buckets.$bucketId.files.update"] ?? []; + $delete = $stats["storage.buckets.$bucketId.files.delete"] ?? []; + + $response->json([ + 'range' => $range, + 'create' => [ + 'data' => $create, + 'total' => \array_sum(\array_map(function ($item) { + return $item['value']; + }, $create)), + ], + 'read' => [ + 'data' => $read, + 'total' => \array_sum(\array_map(function ($item) { + return $item['value']; + }, $read)), + ], + 'update' => [ + 'data' => $update, + 'total' => \array_sum(\array_map(function ($item) { + return $item['value']; + }, $update)), + ], + 'delete' => [ + 'data' => $delete, + 'total' => \array_sum(\array_map(function ($item) { + return $item['value']; + }, $delete)), + ], + ]); + } else { + $response->json([]); } - - Authorization::reset(); - - $create = $stats["storage.buckets.$bucketId.files.create"] ?? []; - $read = $stats["storage.buckets.$bucketId.files.read"] ?? []; - $update = $stats["storage.buckets.$bucketId.files.update"] ?? []; - $delete = $stats["storage.buckets.$bucketId.files.delete"] ?? []; - - $response->json([ - 'range' => $range, - 'create' => [ - 'data' => $create, - 'total' => \array_sum(\array_map(function ($item) { - return $item['value']; - }, $create)), - ], - 'read' => [ - 'data' => $read, - 'total' => \array_sum(\array_map(function ($item) { - return $item['value']; - }, $read)), - ], - 'update' => [ - 'data' => $update, - 'total' => \array_sum(\array_map(function ($item) { - return $item['value']; - }, $update)), - ], - 'delete' => [ - 'data' => $delete, - 'total' => \array_sum(\array_map(function ($item) { - return $item['value']; - }, $delete)), - ], - ]); }); \ No newline at end of file