Filter in prepare

This commit is contained in:
Jake Barnby 2025-02-13 01:07:10 +13:00
parent 6dbc8c5464
commit 3fe1147194
No known key found for this signature in database
GPG key ID: C437A8CC85B96E9C

View file

@ -8,7 +8,7 @@ use Utopia\Queue\Publisher;
class StatsUsage extends Event
{
protected array $metrics = [];
protected array $reduce = [];
protected array $reduce = [];
protected array $disabled = [];
public function __construct(protected Publisher $publisher)
@ -42,10 +42,6 @@ class StatsUsage extends Event
*/
public function addMetric(string $key, int $value): self
{
if ($this->disabled[$key]) {
return $this;
}
$this->metrics[] = [
'key' => $key,
'value' => $value,
@ -62,7 +58,7 @@ class StatsUsage extends Event
*/
public function disableMetric(string $key): self
{
$this->disabled[$key] = true;
$this->disabled[] = $key;
return $this;
}
@ -76,8 +72,15 @@ class StatsUsage extends Event
{
return [
'project' => $this->getProject(),
'reduce' => $this->reduce,
'metrics' => $this->metrics,
'reduce' => $this->reduce,
'metrics' => \array_filter($this->metrics, function ($metric) {
foreach (array_keys($this->disabled) as $disabledMetric) {
if (\str_ends_with($metric['key'], $disabledMetric)) {
return false;
}
}
return true;
}),
];
}
}