builds metric reduce

This commit is contained in:
shimon committed 2024-10-09 16:13:19 +03:00
1 parent a6a67301a4
commit e76d787e39
2 files changed
+78 -5

No files matched your search

+6
View File
@@ -144,6 +144,12 @@ $databaseListener = function (string $event, Document $document, Document $proje
->addMetric(METRIC_DEPLOYMENTS_STORAGE, $document->getAttribute('size') * $value) // per project
->addMetric(str_replace(['{resourceType}', '{resourceInternalId}'], [$document->getAttribute('resourceType'), $document->getAttribute('resourceInternalId')], METRIC_FUNCTION_ID_DEPLOYMENTS), $value) // per function
->addMetric(str_replace(['{resourceType}', '{resourceInternalId}'], [$document->getAttribute('resourceType'), $document->getAttribute('resourceInternalId')], METRIC_FUNCTION_ID_DEPLOYMENTS_STORAGE), $document->getAttribute('size') * $value);
if ($event === Database::EVENT_DOCUMENT_DELETE && $document->getAttribute('resourceType') === 'functions') {
var_dump($document);
$queueForUsage
->addReduce($document);
}
break;
default:
break;
+72 -5
View File
@@ -56,7 +56,6 @@ class Usage extends Action
if (empty($payload)) {
throw new Exception('Missing payload');
}
//Todo Figure out way to preserve keys when the container is being recreated @shimonewman
$aggregationInterval = (int) System::getEnv('_APP_USAGE_AGGREGATION_INTERVAL', '20');
$project = new Document($payload['project'] ?? []);
@@ -86,7 +85,7 @@ class Usage extends Action
$this->stats[$projectId]['keys'][$metric['key']] += $metric['value'];
}
// If keys crossed threshold or X time passed since the last send and there are some keys in the array ($this->stats)
//If keys crossed threshold or X time passed since the last send and there are some keys in the array ($this->stats)
if (
$this->keys >= self::KEYS_THRESHOLD ||
(time() - $this->lastTriggeredTime > $aggregationInterval && $this->keys > 0)
@@ -118,7 +117,7 @@ class Usage extends Action
try {
switch (true) {
case $document->getCollection() === 'users': // users
case $document->getCollection() === 'users': //users
$sessions = count($document->getAttribute(METRIC_SESSIONS, 0));
if (!empty($sessions)) {
$metrics[] = [
@@ -127,7 +126,7 @@ class Usage extends Action
];
}
break;
case $document->getCollection() === 'databases': // databases
case $document->getCollection() === 'databases': //databases
$collections = $dbForProject->getDocument('stats', md5(self::INFINITY_PERIOD . str_replace('{databaseInternalId}', $document->getInternalId(), METRIC_DATABASE_ID_COLLECTIONS)));
$documents = $dbForProject->getDocument('stats', md5(self::INFINITY_PERIOD . str_replace('{databaseInternalId}', $document->getInternalId(), METRIC_DATABASE_ID_DOCUMENTS)));
if (!empty($collections['value'])) {
@@ -270,11 +269,79 @@ class Usage extends Action
];
}
break;
case $document->getCollection() === 'deployments':
$build = $dbForProject->getDocument('builds', $document->getAttribute('buildId'));
if (! $build->isEmpty()) {
// Project scope
$metrics[] = [
'key' => METRIC_BUILDS,
'value' => -1,
];
$metrics[] = [
'key' => $build->getAttribute('status') === 'ready' ? METRIC_BUILDS_SUCCESS : METRIC_BUILDS_FAILED,
'value' => -1
];
$metrics[] = [
'key' => METRIC_BUILDS_STORAGE,
'value' => ($build->getAttribute('size') * -1)
];
$metrics[] = [
'key' => METRIC_BUILDS_COMPUTE,
'value' => (($build->getAttribute('duration', 0) * 1000) * -1),
];
$metrics[] = [
'key' => $build->getAttribute('status') === 'ready' ? METRIC_BUILDS_COMPUTE_SUCCESS : METRIC_BUILDS_COMPUTE_FAILED,
'value' => (($build->getAttribute('duration') * 1000) * -1),
];
//Function scope
$metrics[] = [
'key' => str_replace('{functionInternalId}', $document->getAttribute('resourceInternalId'), METRIC_FUNCTION_ID_BUILDS),
'value' => -1,
];
$statusMetric = $build->getAttribute('status') === 'ready' ? METRIC_FUNCTION_ID_BUILDS_SUCCESS : METRIC_FUNCTION_ID_BUILDS_FAILED;
$metrics[] = [
'key' => str_replace('{functionInternalId}', $document->getAttribute('resourceInternalId'), $statusMetric),
'value' => -1,
];
$metrics[] = [
'key' => str_replace('{functionInternalId}', $document->getAttribute('resourceInternalId'), METRIC_FUNCTION_ID_BUILDS_STORAGE),
'value' => ($build->getAttribute('size') * -1),
];
$metrics[] = [
'key' => str_replace('{functionInternalId}', $document->getAttribute('resourceInternalId'), METRIC_FUNCTION_ID_BUILDS_COMPUTE),
'value' => (($build->getAttribute('duration') * 1000) * -1),
];
$statusMetric = $build->getAttribute('status') === 'ready' ? METRIC_FUNCTION_ID_BUILDS_COMPUTE_SUCCESS : METRIC_FUNCTION_ID_BUILDS_COMPUTE_FAILED;
$metrics[] = [
'key' => str_replace('{functionInternalId}', $document->getAttribute('resourceInternalId'), $statusMetric),
'value' => (($build->getAttribute('duration') * 1000) * -1),
];
}
break;
default:
break;
}
} catch (\Throwable $e) {
console::error("[reducer] " . " {DateTime::now()} " . " {$project->getInternalId()} " . " {$e->getMessage()}");
Console::error('[' . DateTime::now() . '] ' . $project->getInternalId() . ' '. $e->getMessage());;
}
}
}