-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create MetricsBucket interface to make MetricsSender usable externally
Signed-off-by: Quentin Devos <4972091+Okhoshi@users.noreply.github.com>
- Loading branch information
Showing
5 changed files
with
97 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
<?php | ||
|
||
namespace Unleash\Client\Metrics; | ||
|
||
use DateTimeInterface; | ||
use JetBrains\PhpStorm\ArrayShape; | ||
use JsonSerializable; | ||
use LogicException; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
final class DefaultMetricsBucket implements MetricsBucket, JsonSerializable | ||
{ | ||
/** | ||
* @var array<MetricsBucketToggle> | ||
*/ | ||
private array $toggles = []; | ||
|
||
public function __construct( | ||
private readonly DateTimeInterface $startDate, | ||
private ?DateTimeInterface $endDate = null, | ||
) { | ||
} | ||
|
||
public function addToggle(MetricsBucketToggle $toggle): self | ||
{ | ||
$this->toggles[] = $toggle; | ||
|
||
return $this; | ||
} | ||
|
||
public function getStartDate(): DateTimeInterface | ||
{ | ||
return $this->startDate; | ||
} | ||
|
||
public function setEndDate(?DateTimeInterface $endDate): self | ||
{ | ||
$this->endDate = $endDate; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @return array<string,mixed> | ||
*/ | ||
#[ArrayShape(['start' => 'string', 'stop' => 'string', 'toggles' => 'array'])] | ||
public function jsonSerialize(): array | ||
{ | ||
$togglesArray = []; | ||
|
||
if ($this->endDate === null) { | ||
throw new LogicException('Cannot serialize incomplete bucket'); | ||
} | ||
|
||
foreach ($this->toggles as $toggle) { | ||
$featureName = $toggle->getFeature()->getName(); | ||
if (!isset($togglesArray[$featureName])) { | ||
$togglesArray[$featureName] = [ | ||
'yes' => 0, | ||
'no' => 0, | ||
]; | ||
} | ||
|
||
$updateField = $toggle->isSuccess() ? 'yes' : 'no'; | ||
++$togglesArray[$featureName][$updateField]; | ||
|
||
if ($toggle->getVariant() !== null) { | ||
$variant = $toggle->getVariant(); | ||
$togglesArray[$featureName]['variants'][$variant->getName()] ??= 0; | ||
++$togglesArray[$featureName]['variants'][$variant->getName()]; | ||
} | ||
} | ||
|
||
return [ | ||
'start' => $this->startDate->format('c'), | ||
'stop' => $this->endDate->format('c'), | ||
'toggles' => $togglesArray, | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters