From f3074bc024b64b043d658146d0be9132b724e64a Mon Sep 17 00:00:00 2001 From: Christy Jacob Date: Fri, 20 Aug 2021 15:34:57 +0530 Subject: [PATCH] feat(usage): some refactoring --- app/controllers/api/database.php | 58 ++++++++++++++++--------------- app/controllers/api/functions.php | 2 ++ 2 files changed, 32 insertions(+), 28 deletions(-) diff --git a/app/controllers/api/database.php b/app/controllers/api/database.php index 86f4029cdb..f2bf0329ca 100644 --- a/app/controllers/api/database.php +++ b/app/controllers/api/database.php @@ -316,35 +316,37 @@ App::get('/v1/database/usage') $stats[$metric] = array_reverse($stats[$metric]); } + + $documentsCount = $dbForInternal->findOne('stats', [new Query('metric', Query::TYPE_EQUAL, ['database.documents.count'])], 0, ['time'], [Database::ORDER_DESC]); + $documentsTotal = $documentsCount ? $documentsCount->getAttribute('value', 0) : 0; + + $collectionsCount = $dbForInternal->findOne('stats', [new Query('metric', Query::TYPE_EQUAL, ['n + '])], 0, ['time'], [Database::ORDER_DESC]); + $collectionsTotal = $collectionsCount ? $collectionsCount->getAttribute('value', 0) : 0; + + Authorization::reset(); + + $response->json([ + 'range' => $range, + 'stats' => $stats, + 'requests' => [ + 'data' => $stats['requests'] ?? [], + 'total' => \array_sum(\array_map(function ($item) { + return $item['value']; + }, $stats['requests'] ?? [])), + ], + 'documents' => [ + 'data' => [], + 'total' => $documentsTotal, + ], + 'collections' => [ + 'data' => [], + 'total' => $collectionsTotal, + ] + ]); + } else { + $response->json([]); } - - $documentsCount = $dbForInternal->findOne('stats', [new Query('metric', Query::TYPE_EQUAL, ['database.documents.count'])], 0, ['time'], [Database::ORDER_DESC]); - $documentsTotal = $documentsCount ? $documentsCount->getAttribute('value', 0) : 0; - - $collectionsCount = $dbForInternal->findOne('stats', [new Query('metric', Query::TYPE_EQUAL, ['n - '])], 0, ['time'], [Database::ORDER_DESC]); - $collectionsTotal = $collectionsCount ? $collectionsCount->getAttribute('value', 0) : 0; - - Authorization::reset(); - - $response->json([ - 'range' => $range, - 'stats' => $stats, - 'requests' => [ - 'data' => $stats['requests'] ?? [], - 'total' => \array_sum(\array_map(function ($item) { - return $item['value']; - }, $stats['requests'] ?? [])), - ], - 'documents' => [ - 'data' => [], - 'total' => $documentsTotal, - ], - 'collections' => [ - 'data' => [], - 'total' => $collectionsTotal, - ] - ]); }); diff --git a/app/controllers/api/functions.php b/app/controllers/api/functions.php index 0f7a4f9a99..ee3088ce18 100644 --- a/app/controllers/api/functions.php +++ b/app/controllers/api/functions.php @@ -323,6 +323,8 @@ App::get('/v1/functions/:functionId/usage2') $stats[$metric] = array_reverse($stats[$metric]); } + + Authorization::reset(); $executions = $stats["functions.$functionId.executions"] ?? []; $failures = $stats["functions.$functionId.failures"] ?? [];