Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Task/treatments by flag set #17

Merged
merged 5 commits into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ vendor
.phpunit.cache
coverage.xml
coverage.html/
*.local.php
37 changes: 37 additions & 0 deletions examples/treatmentsByFlagSet.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

require_once '../vendor/autoload.php';

use \SplitIO\ThinSdk\Factory;
use \SplitIO\ThinSdk\Utils\ImpressionListener;
use \SplitIO\ThinSdk\Models\Impression;

class CustomListener implements ImpressionListener
{
public function accept(Impression $i, ?array $a)
{
echo "got an impression for: key=".$i->getKey()
." feat=".$i->getFeature()
." treatment=".$i->getTreatment()
." label=".$i->getLabel()
." cn=".$i->getChangeNumber()
." #attrs=".(($a == null) ? 0 : count($a))."\n";
}
}

$factory = Factory::withConfig([
'transfer' => [
'address' => '../../splitd/splitd.sock',
'type' => 'unix-stream',
],
'logging' => [
'level' => \Psr\Log\LogLevel::INFO,
],
'utils' => [
'impressionListener' => new CustomListener(),
],
]);

$client = $factory->client();
print_r($client->getTreatmentsByFlagSet("key", null, "server_side", ['age' => 22]));
print_r($client->getTreatmentsWithConfigByFlagSet("key", null, "server_side", ['age' => 22]));
62 changes: 62 additions & 0 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,68 @@ public function getTreatmentsWithConfig(string $key, ?string $bucketingKey, arra
}
}

public function getTreatmentsByFlagSet(
string $key,
?string $bucketingKey,
string $flagSet,
?array $attributes
): array {
try {
$id = $this->tracer->makeId();
$method = Tracer::METHOD_GET_TREATMENTS_BY_FLAG_SET;
$this->tracer->trace(TEF::forStart($method, $id, $this->tracer->includeArgs() ? func_get_args() : []));
// @TODO implement cache for this method

$this->tracer->trace(TEF::forRPCStart($method, $id));
$results = $this->lm->getTreatmentsByFlagSet($key, $bucketingKey, $flagSet, $attributes);
$this->tracer->trace(TEF::forRPCEnd($method, $id));
foreach ($results as $feature => $result) {
list($treatment, $ilData) = $result;
$toReturn[$feature] = $treatment;
$this->handleListener($key, $bucketingKey, $feature, $attributes, $treatment, $ilData);
}
$this->cache->setMany($key, $attributes, $toReturn);
return $toReturn;
} catch (\Exception $exc) {
$this->tracer->trace(TEF::forException($method, $id, $exc));
$this->logger->error($exc);
return array();
} finally {
$this->tracer->trace(TEF::forEnd($method, $id));
}
}

public function getTreatmentsWithConfigByFlagSet(
string $key,
?string $bucketingKey,
string $flagSet,
?array $attributes = null
): array {
try {
$id = $this->tracer->makeId();
$method = Tracer::METHOD_GET_TREATMENTS_WITH_CONFIG_BY_FLAG_SET;
$this->tracer->trace(TEF::forStart($method, $id, $this->tracer->includeArgs() ? func_get_args() : []));
// @TODO implement cache for this method

$this->tracer->trace(TEF::forRPCStart($method, $id));
$results = $this->lm->getTreatmentsWithConfigByFlagSet($key, $bucketingKey, $flagSet, $attributes);
$this->tracer->trace(TEF::forRPCEnd($method, $id));
foreach ($results as $feature => $result) {
list($treatment, $ilData, $config) = $result;
$toReturn[$feature] = ['treatment' => $treatment, 'config' => $config];
$this->handleListener($key, $bucketingKey, $feature, $attributes, $treatment, $ilData);
}
$this->cache->setMany($key, $attributes, $toReturn);
return $toReturn;
} catch (\Exception $exc) {
$this->tracer->trace(TEF::forException($method, $id, $exc));
$this->logger->error($exc);
return array();
} finally {
$this->tracer->trace(TEF::forEnd($method, $id));
}
}

public function track(string $key, string $trafficType, string $eventType, ?float $value = null, ?array $properties = null): bool
{
try {
Expand Down
7 changes: 7 additions & 0 deletions src/ClientInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,12 @@ function getTreatment(string $key, ?string $bucketingKey, string $feature, ?arra
function getTreatmentWithConfig(string $key, ?string $bucketingKey, string $feature, ?array $attributes): array;
function getTreatments(string $key, ?string $bucketingKey, array $features, ?array $attributes): array;
function getTreatmentsWithConfig(string $key, ?string $bucketingKey, array $features, ?array $attributes): array;
function getTreatmentsByFlagSet(string $key, ?string $bucketingKey, string $flagSet, ?array $attributes): array;
function getTreatmentsWithConfigByFlagSet(
string $key,
?string $bucketingKey,
string $flagSet,
?array $attributes
): array;
function track(string $key, string $trafficType, string $eventType, ?float $value = null, ?array $properties = null): bool;
}
10 changes: 10 additions & 0 deletions src/Fallback/AlwaysControlClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@ public function getTreatmentsWithConfig(string $key, ?string $bucketingKey, arr
}, []);
}

public function getTreatmentsByFlagSet(string $key, $bucketingKey, string $flagSet, $attributes): array
{
return array();
}

public function getTreatmentsWithConfigByFlagSet(string $key, $bucketingKey, string $flagSet, $attributes): array
{
return array();
}

public function track(string $key, string $trafficType, string $eventType, ?float $value = null, ?array $properties = null): bool
{
return false;
Expand Down
7 changes: 7 additions & 0 deletions src/Link/Consumer/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ function getTreatment(string $key, ?string $bucketingKey, string $feature, ?arra
function getTreatmentWithConfig(string $key, ?string $bucketingKey, string $feature, ?array $attributes): array;
function getTreatments(string $key, ?string $bucketingKey, array $features, ?array $attributes): array;
function getTreatmentsWithConfig(string $key, ?string $bucketingKey, array $features, ?array $attributes): array;
function getTreatmentsByFlagSet(string $key, ?string $bucketingKey, string $flagSet, ?array $attributes): array;
function getTreatmentsWithConfigByFlagSet(
string $key,
?string $bucketingKey,
string $flagSet,
?array $attributes
): array;
function track(string $key, string $trafficType, string $eventType, ?float $value, ?array $properties): bool;
function splitNames(): array;
function split(string $splitName): ?SplitView;
Expand Down
46 changes: 46 additions & 0 deletions src/Link/Consumer/V1Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,52 @@ public function getTreatmentsWithConfig(string $key, ?string $bucketingKey, arra
return $results;
}

public function getTreatmentsByFlagSet(
string $key,
?string $bucketingKey,
string $flagSet,
?array $attributes): array
{
$response = Protocol\V1\TreatmentsByFlagSetResponse::fromRaw(
$this->rpcWithReconnect(RPC::forTreatmentsByFlagSet($key, $bucketingKey, $flagSet, $attributes))
);
$response->ensureSuccess();

$results = [];

foreach ($response->getEvaluationResults() as $feature => $evalResult) {
$results[$feature] = $evalResult == null
? ["control", null, null]
: [$evalResult->getTreatment(), $evalResult->getImpressionListenerdata(), $evalResult->getConfig()];
}

return $results;
}


public function getTreatmentsWithConfigByFlagSet(
string $key,
?string $bucketingKey,
string $flagSet,
?array $attributes
): array {
$response = Protocol\V1\TreatmentsByFlagSetResponse::fromRaw(
$this->rpcWithReconnect(RPC::forTreatmentsWithConfigByFlagSet($key, $bucketingKey, $flagSet, $attributes))
);
$response->ensureSuccess();

$results = [];

foreach ($response->getEvaluationResults() as $feature => $evalResult) {
$results[$feature] = $evalResult == null
? ["control", null, null]
: [$evalResult->getTreatment(), $evalResult->getImpressionListenerdata(), $evalResult->getConfig()];
}

return $results;
}


public function track(string $key, string $trafficType, string $eventType, ?float $value, ?array $properties): bool
{
$response = Protocol\V1\TrackResponse::fromRaw(
Expand Down
2 changes: 2 additions & 0 deletions src/Link/Protocol/V1/OpCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ class OpCode extends Enum
private const Treatments = 0x12;
private const TreatmentWithConfig = 0x13;
private const TreatmentsWithConfig = 0x14;
private const TreatmentsByFlagSet = 0x15;
private const TreatmentsWithConfigByFlagSet = 0x16;

private const Track = 0x80;

Expand Down
37 changes: 37 additions & 0 deletions src/Link/Protocol/V1/RPC.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,24 @@ public static function forTreatmentsWithConfig(string $key, ?string $bucketingKe
return self::_forTreatments($key, $bucketingKey, $features, $attributes, true);
}

public static function forTreatmentsByFlagSet(
string $key,
?string $bucketingKey,
string $flagSet,
?array $attributes): RPC
{
return self::_forTreatmentsByFlagSet($key, $bucketingKey, $flagSet, $attributes, false);
}

public static function forTreatmentsWithConfigByFlagSet(
string $key,
?string $bucketingKey,
string $flagSet,
?array $attributes): RPC
{
return self::_forTreatmentsByFlagSet($key, $bucketingKey, $flagSet, $attributes, true);
}

public static function forTrack(
string $key,
string $trafficType,
Expand Down Expand Up @@ -138,4 +156,23 @@ public static function _forTreatments(string $k, ?string $bk, array $f, ?array $
]
);
}

public static function _forTreatmentsByFlagSet(
string $k,
?string $bk,
string $f,
?array $a,
bool $includeConfig): RPC
{
return new RPC(
Version::V1(),
$includeConfig ? OpCode::TreatmentsWithConfigByFlagSet() : OpCode::TreatmentsByFlagSet(),
[
TreatmentsByFlagSetArgs::KEY()->getValue() => $k,
TreatmentsByFlagSetArgs::BUCKETING_KEY()->getValue() => $bk,
TreatmentsByFlagSetArgs::FLAG_SET()->getValue() => $f,
TreatmentsByFlagSetArgs::ATTRIBUTES()->getValue() => ($a != null && count($a) > 0) ? $a : null,
]
);
}
}
23 changes: 23 additions & 0 deletions src/Link/Protocol/V1/TreatmentsByFlagSetArgs.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace SplitIO\ThinSdk\Link\Protocol\V1;

/*
enum TreatmentsByFlagSetArgs: int
{
case KEY = 0;
case BUCKETING_KEY = 1;
case FLAGSET = 2;
case ATTRIBUTES = 3;
}
*/

use MyCLabs\Enum\Enum;

class TreatmentsByFlagSetArgs extends Enum
{
private const KEY = 0;
private const BUCKETING_KEY = 1;
private const FLAG_SET = 2;
private const ATTRIBUTES = 3;
}
44 changes: 44 additions & 0 deletions src/Link/Protocol/V1/TreatmentsByFlagSetResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace SplitIO\ThinSdk\Link\Protocol\V1;

use SplitIO\ThinSdk\Link\Protocol\V1\Result;
use SplitIO\ThinSdk\Foundation\Lang\Enforce;

class TreatmentsByFlagSetResponse extends Response
{

private /*array*/ $evaluationResults;

public function __construct(Result $status, array $results)
{
parent::__construct($status);
$this->evaluationResults = $results;
}

public function getEvaluationResults(): array
{
return $this->evaluationResults;
}

public function getEvaluationResult(int $index): ?EvaluationResult
{
return count($this->evaluationResults) > $index ? $this->evaluationResults[$index] : null;
}

public static function fromRaw(/*mixed*/$raw)/*: mixed*/
{
Enforce::isArray($raw);
$status = Result::from(Enforce::isInt($raw['s']));

$results = [];
foreach (Enforce::isArray($raw['p']['r']) as $feature => $evalResult) {
$results[$feature] = EvaluationResult::fromRaw(Enforce::isArray($evalResult));
};

return new TreatmentsByFlagSetResponse(
$status,
Enforce::isArray($results)
);
}
}
2 changes: 2 additions & 0 deletions src/Utils/Tracing/Tracer.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ class Tracer
public const METHOD_GET_TREATMENT_WITH_CONFIG = 12;
public const METHOD_GET_TREATMENTS_WITH_CONFIG = 13;
public const METHOD_TRACK = 14;
public const METHOD_GET_TREATMENTS_BY_FLAG_SET = 15;
public const METHOD_GET_TREATMENTS_WITH_CONFIG_BY_FLAG_SET = 16;

public const EVENT_START = 30;
public const EVENT_RPC_START = 31;
Expand Down
Loading