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 class StatsUsage extends Event
{ {
protected array $metrics = []; protected array $metrics = [];
protected array $reduce = []; protected array $reduce = [];
protected array $disabled = []; protected array $disabled = [];
public function __construct(protected Publisher $publisher) public function __construct(protected Publisher $publisher)
@ -42,10 +42,6 @@ class StatsUsage extends Event
*/ */
public function addMetric(string $key, int $value): self public function addMetric(string $key, int $value): self
{ {
if ($this->disabled[$key]) {
return $this;
}
$this->metrics[] = [ $this->metrics[] = [
'key' => $key, 'key' => $key,
'value' => $value, 'value' => $value,
@ -62,7 +58,7 @@ class StatsUsage extends Event
*/ */
public function disableMetric(string $key): self public function disableMetric(string $key): self
{ {
$this->disabled[$key] = true; $this->disabled[] = $key;
return $this; return $this;
} }
@ -76,8 +72,15 @@ class StatsUsage extends Event
{ {
return [ return [
'project' => $this->getProject(), 'project' => $this->getProject(),
'reduce' => $this->reduce, 'reduce' => $this->reduce,
'metrics' => $this->metrics, '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;
}),
]; ];
} }
} }