From 59af8d9c86644b71b35ec15adcf25b0b895260e4 Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Thu, 21 Apr 2022 10:30:32 +0000 Subject: [PATCH] aggregate `storage.total` as well --- app/tasks/usage.php | 51 +++++++++++++++++++++++++++++++++++++++------ 1 file changed, 45 insertions(+), 6 deletions(-) diff --git a/app/tasks/usage.php b/app/tasks/usage.php index aa8dd2371c..0aff6cf513 100644 --- a/app/tasks/usage.php +++ b/app/tasks/usage.php @@ -418,7 +418,7 @@ $cli // Get total storage $dbForProject->setNamespace('_' . $projectId); - $storageTotal = $dbForProject->sum('deployments', 'size'); + $deploymentsTotal = $dbForProject->sum('deployments', 'size'); $time = (int) (floor(time() / 1800) * 1800); // Time rounded to nearest 30 minutes $id = \md5($time . '_30m_storage.deployments.total'); //Construct unique id for each metric using time, period and metric @@ -431,14 +431,14 @@ $cli 'period' => '30m', 'time' => $time, 'metric' => 'storage.deployments.total', - 'value' => $storageTotal, + 'value' => $deploymentsTotal, 'type' => 1, ])); } else { $dbForProject->updateDocument( 'stats', $document->getId(), - $document->setAttribute('value', $storageTotal) + $document->setAttribute('value', $deploymentsTotal) ); } $time = (int) (floor(time() / 86400) * 86400); // Time rounded to nearest day @@ -450,14 +450,14 @@ $cli 'period' => '1d', 'time' => $time, 'metric' => 'storage.deployments.total', - 'value' => $storageTotal, + 'value' => $deploymentsTotal, 'type' => 1, ])); } else { $dbForProject->updateDocument( 'stats', $document->getId(), - $document->setAttribute('value', $storageTotal) + $document->setAttribute('value', $deploymentsTotal) ); } } catch(\Exception $e) { @@ -714,7 +714,7 @@ $cli } /** - * Inserting project level sums for sub collections like storage.total + * Inserting project level sums for sub collections like storage.files.total */ foreach ($subCollectionTotals as $subCollection => $count) { $dbForProject->setNamespace("_{$projectId}"); @@ -754,6 +754,45 @@ $cli $dbForProject->updateDocument('stats', $document->getId(), $document->setAttribute('value', $count)); } + + // aggregate storage.total = storage.files.total + storage.deployments.total + if($metricPrefix === 'storage' && $subCollection === 'files') { + Console::info("Aggregating `storage.total`"); + $metric = 'storage.total'; + $time = (int) (floor(time() / 1800) * 1800); // Time rounded to nearest 30 minutes + $id = \md5($time . '_30m_' . $metric); //Construct unique id for each metric using time, period and metric + $document = $dbForProject->getDocument('stats', $id); + if ($document->isEmpty()) { + $dbForProject->createDocument('stats', new Document([ + '$id' => $id, + 'time' => $time, + 'period' => '30m', + 'metric' => $metric, + 'value' => $count + $deploymentsTotal, + 'type' => 1, + ])); + } else { + $dbForProject->updateDocument('stats', $document->getId(), + $document->setAttribute('value', $count)); + } + + $time = (int) (floor(time() / 86400) * 86400); // Time rounded to nearest day + $id = \md5($time . '_1d_' . $metric); //Construct unique id for each metric using time, period and metric + $document = $dbForProject->getDocument('stats', $id); + if ($document->isEmpty()) { + $dbForProject->createDocument('stats', new Document([ + '$id' => $id, + 'time' => $time, + 'period' => '1d', + 'metric' => $metric, + 'value' => $count + $deploymentsTotal, + 'type' => 1, + ])); + } else { + $dbForProject->updateDocument('stats', $document->getId(), + $document->setAttribute('value', $count)); + } + } } } catch (\Exception$e) { Console::warning("Failed to save database counters data for project {$collection}: {$e->getMessage()}");