1
0
Fork 0
mirror of synced 2024-07-08 16:06:02 +12:00

aggregate storage.total as well

This commit is contained in:
Damodar Lohani 2022-04-21 10:30:32 +00:00
parent 3cd953d2cf
commit 59af8d9c86

View file

@ -418,7 +418,7 @@ $cli
// Get total storage // Get total storage
$dbForProject->setNamespace('_' . $projectId); $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 $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 $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', 'period' => '30m',
'time' => $time, 'time' => $time,
'metric' => 'storage.deployments.total', 'metric' => 'storage.deployments.total',
'value' => $storageTotal, 'value' => $deploymentsTotal,
'type' => 1, 'type' => 1,
])); ]));
} else { } else {
$dbForProject->updateDocument( $dbForProject->updateDocument(
'stats', 'stats',
$document->getId(), $document->getId(),
$document->setAttribute('value', $storageTotal) $document->setAttribute('value', $deploymentsTotal)
); );
} }
$time = (int) (floor(time() / 86400) * 86400); // Time rounded to nearest day $time = (int) (floor(time() / 86400) * 86400); // Time rounded to nearest day
@ -450,14 +450,14 @@ $cli
'period' => '1d', 'period' => '1d',
'time' => $time, 'time' => $time,
'metric' => 'storage.deployments.total', 'metric' => 'storage.deployments.total',
'value' => $storageTotal, 'value' => $deploymentsTotal,
'type' => 1, 'type' => 1,
])); ]));
} else { } else {
$dbForProject->updateDocument( $dbForProject->updateDocument(
'stats', 'stats',
$document->getId(), $document->getId(),
$document->setAttribute('value', $storageTotal) $document->setAttribute('value', $deploymentsTotal)
); );
} }
} catch(\Exception $e) { } 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) { foreach ($subCollectionTotals as $subCollection => $count) {
$dbForProject->setNamespace("_{$projectId}"); $dbForProject->setNamespace("_{$projectId}");
@ -754,6 +754,45 @@ $cli
$dbForProject->updateDocument('stats', $document->getId(), $dbForProject->updateDocument('stats', $document->getId(),
$document->setAttribute('value', $count)); $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) { } catch (\Exception$e) {
Console::warning("Failed to save database counters data for project {$collection}: {$e->getMessage()}"); Console::warning("Failed to save database counters data for project {$collection}: {$e->getMessage()}");