Skip to content

Commit cd86ca2

Browse files
committed
feat: Change fractional custom op from percentage-based to relative weighting.
Signed-off-by: Simon Schrottner <[email protected]>
1 parent 6af48e9 commit cd86ca2

File tree

1 file changed

+21
-8
lines changed
  • providers/openfeature-provider-flagd/src/openfeature/contrib/provider/flagd/resolvers/process

1 file changed

+21
-8
lines changed

providers/openfeature-provider-flagd/src/openfeature/contrib/provider/flagd/resolvers/process/custom_ops.py

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,23 +31,36 @@ def fractional(data: dict, *args: JsonLogicArg) -> typing.Optional[str]:
3131
logger.error("No hashKey value resolved")
3232
return None
3333

34-
hash_ratio = abs(mmh3.hash(bucket_by)) / (2**31 - 1)
35-
bucket = int(hash_ratio * 100)
34+
hash_ratio = abs(mmh3.hash(bucket_by)) / (2 ** 31 - 1)
35+
bucket = hash_ratio * 100
3636

37+
totalWeight = 0
3738
for arg in args:
3839
if (
39-
not isinstance(arg, (tuple, list))
40-
or len(arg) != 2
41-
or not isinstance(arg[0], str)
42-
or not isinstance(arg[1], int)
40+
not isinstance(arg, (tuple, list))
41+
or len(arg) == 0
4342
):
4443
logger.error("Fractional variant weights must be (str, int) tuple")
4544
return None
45+
46+
if (not isinstance(arg[0], str)):
47+
logger.error("Fractional variant's first element isn't string")
48+
return None
49+
if (len(arg) >= 2):
50+
if (not isinstance(arg[1], int)):
51+
logger.error("Fractional variant's second element isn't int")
52+
return None
53+
else:
54+
arg.append(1)
55+
56+
totalWeight += arg[1]
57+
58+
4659
variant_weights: typing.Tuple[typing.Tuple[str, int]] = args # type: ignore[assignment]
4760

4861
range_end = 0
4962
for variant, weight in variant_weights:
50-
range_end += weight
63+
range_end += weight * 100 / totalWeight
5164
if bucket < range_end:
5265
return variant
5366

@@ -69,7 +82,7 @@ def f(s1: str, s2: str) -> bool:
6982

7083

7184
def string_comp(
72-
comparator: typing.Callable[[str, str], bool], data: dict, *args: JsonLogicArg
85+
comparator: typing.Callable[[str, str], bool], data: dict, *args: JsonLogicArg
7386
) -> typing.Optional[bool]:
7487
if not args:
7588
logger.error("No arguments provided to string_comp operator.")

0 commit comments

Comments
 (0)