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 sets #18

Merged
merged 3 commits into from
Jan 12, 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
37 changes: 37 additions & 0 deletions examples/treatmentsByFlagSets.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->getTreatmentsByFlagSets("key", null, ["server_side", "backend"], ['age' => 22]));
print_r($client->getTreatmentsWithConfigByFlagSets("key", null, ["server_side", "backend"], ['age' => 22]));
62 changes: 62 additions & 0 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,68 @@ public function getTreatmentsWithConfigByFlagSet(
}
}

public function getTreatmentsByFlagSets(
string $key,
?string $bucketingKey,
array $flagSets,
?array $attributes
): array {
try {
$id = $this->tracer->makeId();
$method = Tracer::METHOD_GET_TREATMENTS_BY_FLAG_SETS;
$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->getTreatmentsByFlagSets($key, $bucketingKey, $flagSets, $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 getTreatmentsWithConfigByFlagSets(
string $key,
?string $bucketingKey,
array $flagSets,
?array $attributes = null
): array {
try {
$id = $this->tracer->makeId();
$method = Tracer::METHOD_GET_TREATMENTS_WITH_CONFIG_BY_FLAG_SETS;
$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->getTreatmentsWithConfigByFlagSets($key, $bucketingKey, $flagSets, $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 @@ -15,5 +15,12 @@ function getTreatmentsWithConfigByFlagSet(
string $flagSet,
?array $attributes
): array;
function getTreatmentsByFlagSets(string $key, ?string $bucketingKey, array $flagSets, ?array $attributes): array;
function getTreatmentsWithConfigByFlagSets(
string $key,
?string $bucketingKey,
array $flagSets,
?array $attributes
): array;
function track(string $key, string $trafficType, string $eventType, ?float $value = null, ?array $properties = null): bool;
}
18 changes: 14 additions & 4 deletions src/Fallback/AlwaysControlClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,38 @@ public function getTreatmentWithConfig(string $key, ?string $bucketingKey, strin
return ['treatment' => 'control', 'config' => null];
}

public function getTreatments(string $key, ?string $bucketingKey, array $features, ?array $attributes): array
public function getTreatments(string $key, ?string $bucketingKey, array $features, ?array $attributes): array
{
return array_reduce($features, function ($carry, $item) {
$carry[$item] = "control";
return $carry;
}, []);
}

public function getTreatmentsWithConfig(string $key, ?string $bucketingKey, array $features, ?array $attributes): array
public function getTreatmentsWithConfig(string $key, ?string $bucketingKey, array $features, ?array $attributes): array
{
return array_reduce($features, function ($carry, $item) {
$carry[$item] = ['treatment' => 'control', 'config' => null];
return $carry;
}, []);
}

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

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

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

public function getTreatmentsWithConfigByFlagSets(string $key, $bucketingKey, array $flagSets, $attributes): array
{
return array();
}
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 @@ -17,6 +17,13 @@ function getTreatmentsWithConfigByFlagSet(
string $flagSet,
?array $attributes
): array;
function getTreatmentsByFlagSets(string $key, ?string $bucketingKey, array $flagSets, ?array $attributes): array;
function getTreatmentsWithConfigByFlagSets(
string $key,
?string $bucketingKey,
array $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
44 changes: 43 additions & 1 deletion src/Link/Consumer/V1Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,6 @@ public function getTreatmentsByFlagSet(
return $results;
}


public function getTreatmentsWithConfigByFlagSet(
string $key,
?string $bucketingKey,
Expand All @@ -140,6 +139,49 @@ public function getTreatmentsWithConfigByFlagSet(
return $results;
}

public function getTreatmentsByFlagSets(
string $key,
?string $bucketingKey,
array $flagSets,
?array $attributes): array
{
$response = Protocol\V1\TreatmentsByFlagSetResponse::fromRaw(
$this->rpcWithReconnect(RPC::forTreatmentsByFlagSets($key, $bucketingKey, $flagSets, $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 getTreatmentsWithConfigByFlagSets(
string $key,
?string $bucketingKey,
array $flagSets,
?array $attributes
): array {
$response = Protocol\V1\TreatmentsByFlagSetResponse::fromRaw(
$this->rpcWithReconnect(RPC::forTreatmentsWithConfigByFlagSets($key, $bucketingKey, $flagSets, $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
{
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 @@ -28,6 +28,8 @@ class OpCode extends Enum
private const TreatmentsWithConfig = 0x14;
private const TreatmentsByFlagSet = 0x15;
private const TreatmentsWithConfigByFlagSet = 0x16;
private const TreatmentsByFlagSets = 0x17;
private const TreatmentsWithConfigByFlagSets = 0x18;

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 @@ -85,6 +85,24 @@ public static function forTreatmentsWithConfigByFlagSet(
return self::_forTreatmentsByFlagSet($key, $bucketingKey, $flagSet, $attributes, true);
}

public static function forTreatmentsByFlagSets(
string $key,
?string $bucketingKey,
array $flagSets,
?array $attributes): RPC
{
return self::_forTreatmentsByFlagSets($key, $bucketingKey, $flagSets, $attributes, false);
}

public static function forTreatmentsWithConfigByFlagSets(
string $key,
?string $bucketingKey,
array $flagSets,
?array $attributes): RPC
{
return self::_forTreatmentsByFlagSets($key, $bucketingKey, $flagSets, $attributes, true);
}

public static function forTrack(
string $key,
string $trafficType,
Expand Down Expand Up @@ -175,4 +193,23 @@ public static function _forTreatmentsByFlagSet(
]
);
}

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

namespace SplitIO\ThinSdk\Link\Protocol\V1;

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

use MyCLabs\Enum\Enum;

class TreatmentsByFlagSetsArgs extends Enum
{
private const KEY = 0;
private const BUCKETING_KEY = 1;
private const FLAG_SETS = 2;
private const ATTRIBUTES = 3;
}
2 changes: 2 additions & 0 deletions src/Utils/Tracing/Tracer.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ class Tracer
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 METHOD_GET_TREATMENTS_BY_FLAG_SETS = 17;
public const METHOD_GET_TREATMENTS_WITH_CONFIG_BY_FLAG_SETS = 18;

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