This repository has been archived by the owner on Nov 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
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
13 changed files
with
646 additions
and
290 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
from typing import Dict, Optional | ||
from eppo_client.sharding import Sharder | ||
from eppo_client.models import Range, Shard, Variation | ||
from eppo_client.rules import matches_rule | ||
import hashlib | ||
from dataclasses import dataclass | ||
import logging | ||
|
||
|
||
@dataclass | ||
class EvalResult: | ||
flag_key: str | ||
allocation_key: str | ||
variation: Variation | ||
extra_logging: Dict[str, str] | ||
do_log: bool | ||
|
||
|
||
@dataclass | ||
class Evaluator: | ||
sharder: Sharder | ||
|
||
def evaluate_flag( | ||
self, flag, subject_key, subject_attributes | ||
) -> Optional[EvalResult]: | ||
for allocation in flag.allocations: | ||
if not allocation.rules or any( | ||
matches_rule(rule, subject_attributes) for rule in allocation.rules | ||
): | ||
for split in allocation.splits: | ||
if all( | ||
self.matches_shard(shard, subject_key, flag.total_shards) | ||
for shard in split.shards | ||
): | ||
return EvalResult( | ||
flag_key=flag.key, | ||
allocation_key=allocation.key, | ||
variation=flag.variations.get(split.variation_key), | ||
extra_logging=split.extra_logging, | ||
do_log=allocation.do_log, | ||
) | ||
|
||
return EvalResult( | ||
flag_key=flag.key, | ||
allocation_key=None, | ||
variation=None, | ||
extra_logging={}, | ||
do_log=True, | ||
) | ||
|
||
def matches_shard(self, shard: Shard, subject_key: str, total_shards: int) -> bool: | ||
h = self.sharder.get_shard(seed(shard.salt, subject_key), total_shards) | ||
return any(is_in_shard_range(h, r) for r in shard.ranges) | ||
|
||
|
||
def is_in_shard_range(shard: int, range: Range) -> bool: | ||
return range.start <= shard < range.end | ||
|
||
|
||
def seed(salt, subject_key): | ||
return f"{salt}-{subject_key}" |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
from datetime import datetime | ||
|
||
from typing import Dict, List, Optional | ||
|
||
from eppo_client.base_model import SdkBaseModel | ||
from eppo_client.rules import Rule | ||
|
||
|
||
class Variation(SdkBaseModel): | ||
# TODO: generalize | ||
key: str | ||
value: str | ||
|
||
|
||
class Range(SdkBaseModel): | ||
start: int | ||
end: int | ||
|
||
|
||
class Shard(SdkBaseModel): | ||
salt: str | ||
ranges: List[Range] | ||
|
||
|
||
class Split(SdkBaseModel): | ||
shards: List[Shard] | ||
variation_key: str | ||
extra_logging: Dict[str, str] = {} | ||
|
||
|
||
class Allocation(SdkBaseModel): | ||
key: str | ||
rules: List[Rule] | ||
start_at: Optional[datetime] = None | ||
end_at: Optional[datetime] = None | ||
splits: List[Split] | ||
do_log: bool = True | ||
|
||
|
||
class Flag(SdkBaseModel): | ||
key: str | ||
enabled: bool | ||
variations: Dict[str, Variation] | ||
allocations: List[Allocation] | ||
total_shards: int = 10_000 |
Oops, something went wrong.