From 39d4924fe82e2d0a71eba466df77a223a2982dd0 Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Thu, 19 Aug 2021 13:59:23 +0545 Subject: [PATCH] storage total stats --- app/controllers/api/projects.php | 24 ++++++------------------ app/tasks/usage.php | 20 +++++++++++++++++++- 2 files changed, 25 insertions(+), 19 deletions(-) diff --git a/app/controllers/api/projects.php b/app/controllers/api/projects.php index bea5332f1..b655f7a6f 100644 --- a/app/controllers/api/projects.php +++ b/app/controllers/api/projects.php @@ -296,6 +296,9 @@ App::get('/v1/projects/:projectId/usage') $filesCount = $dbForInternal->findOne('stats', [new Query('metric', Query::TYPE_EQUAL, ['storage.files.count'])], 0, ['time'], [Database::ORDER_DESC]); $filesTotal = $filesCount ? $filesCount->getAttribute('value', 0) : 0; + + $storageTotal = $dbForInternal->findOne('stats', [new Query('metric', Query::TYPE_EQUAL, ['storage.total'])], 0, ['time'], [Database::ORDER_DESC]); + $storage = $storageTotal ? $storageTotal->getAttribute('value', 0) : 0; Authorization::reset(); @@ -335,24 +338,9 @@ App::get('/v1/projects/:projectId/usage') 'data' => [], 'total' => $usersTotal, ], - // 'storage' => [ - // 'total' => $projectDB->getCount( - // [ - // 'attribute' => 'sizeOriginal', - // 'filters' => [ - // '$collection=files', - // ], - // ] - // ) + - // $projectDB->getCount( - // [ - // 'attribute' => 'size', - // 'filters' => [ - // '$collection=tags', - // ], - // ] - // ), - // ], + 'storage' => [ + 'total' => $storage, + ], ]); }); diff --git a/app/tasks/usage.php b/app/tasks/usage.php index ffa2df490..3f938e46b 100644 --- a/app/tasks/usage.php +++ b/app/tasks/usage.php @@ -51,6 +51,10 @@ use Utopia\Database\Validator\Authorization; * database.documents.count * database.collections.{collectionId}.documents.count * + * Totals + * + * storage.total + * */ $cli @@ -263,6 +267,20 @@ $cli foreach ($projects as $project) { $id = $project->getId(); + + // get total storage + $dbForProject->setNamespace('project_' . $id . '_internal'); + $storageTotal = $dbForProject->sum('files', 'sizeOriginal') + $dbForProject->sum('tags', 'size'); + + $dbForProject->createDocument('stats', new Document([ + '$id' => $dbForProject->getId(), + 'period' => '15m', + 'time' => time(), + 'metric' => 'storage.total', + 'value' => $storageTotal, + 'type' => 1, + ])); + $collections = [ 'users' => [ 'namespace' => 'internal', @@ -310,7 +328,7 @@ $cli $dbForProject->setNamespace("project_{$id}_{$subOptions['namespace']}"); $count = $dbForProject->count($parent->getId()); $subCollectionsCounts[$subCollection] = ($subCollectionCounts[$subCollection] ?? 0) + $count; - + $dbForProject->setNamespace("project_{$id}_internal"); $dbForProject->createDocument('stats', new Document([ '$id' => $dbForProject->getId(),