-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
153 additions
and
110 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,91 @@ | ||
<?php | ||
|
||
namespace Unleash\Client\Metrics; | ||
|
||
use DateTimeInterface; | ||
use JetBrains\PhpStorm\ArrayShape; | ||
use LogicException; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
final class DefaultMetricsBucket implements MetricsBucket | ||
{ | ||
/** | ||
* @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 setStartDate(DateTimeInterface $date): self | ||
{ | ||
throw new LogicException('Start date cannot be modified'); | ||
} | ||
|
||
public function getStartDate(): DateTimeInterface | ||
{ | ||
return $this->startDate; | ||
} | ||
|
||
public function setEndDate(?DateTimeInterface $endDate): self | ||
{ | ||
$this->endDate = $endDate; | ||
|
||
return $this; | ||
} | ||
|
||
public function getEndDate(): ?DateTimeInterface | ||
{ | ||
return $this->endDate; | ||
} | ||
|
||
/** | ||
* @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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
namespace Unleash\Client\Metrics; | ||
|
||
use Unleash\Client\DTO\Feature; | ||
use Unleash\Client\DTO\Variant; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
final class DefaultMetricsBucketToggle implements MetricsBucketToggle | ||
{ | ||
public function __construct( | ||
private readonly Feature $feature, | ||
private readonly bool $success, | ||
private readonly ?Variant $variant = null, | ||
) { | ||
} | ||
|
||
public function getFeature(): Feature | ||
{ | ||
return $this->feature; | ||
} | ||
|
||
public function isSuccess(): bool | ||
{ | ||
return $this->success; | ||
} | ||
|
||
public function getVariant(): ?Variant | ||
{ | ||
return $this->variant; | ||
} | ||
} |
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
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