Skip to content

Commit

Permalink
fix: variant hashing in playground (#5213)
Browse files Browse the repository at this point in the history
  • Loading branch information
kwasniew committed Oct 30, 2023
1 parent a7f3a89 commit b54d481
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 11 deletions.
@@ -1,6 +1,6 @@
import { Strategy } from './strategy';
import { Context } from '../context';
import normalizedValue from './util';
import { normalizedStrategyValue } from './util';
import { resolveContextValue } from '../helpers';

const STICKINESS = {
Expand Down Expand Up @@ -54,7 +54,7 @@ export default class FlexibleRolloutStrategy extends Strategy {
if (!stickinessId) {
return false;
}
const normalizedUserId = normalizedValue(stickinessId, groupId);
const normalizedUserId = normalizedStrategyValue(stickinessId, groupId);
return percentage > 0 && normalizedUserId <= percentage;
}
}
@@ -1,5 +1,5 @@
import { Strategy } from './strategy';
import normalizedValue from './util';
import { normalizedStrategyValue } from './util';
import { Context } from '../context';

export default class GradualRolloutSessionIdStrategy extends Strategy {
Expand All @@ -19,7 +19,7 @@ export default class GradualRolloutSessionIdStrategy extends Strategy {
const percentage = Number(parameters.percentage);
const groupId = parameters.groupId || '';

const normalizedId = normalizedValue(sessionId, groupId);
const normalizedId = normalizedStrategyValue(sessionId, groupId);

return percentage > 0 && normalizedId <= percentage;
}
Expand Down
@@ -1,6 +1,6 @@
import { Strategy } from './strategy';
import { Context } from '../context';
import normalizedValue from './util';
import { normalizedStrategyValue } from './util';

export default class GradualRolloutUserIdStrategy extends Strategy {
constructor() {
Expand All @@ -19,7 +19,7 @@ export default class GradualRolloutUserIdStrategy extends Strategy {
const percentage = Number(parameters.percentage);
const groupId = parameters.groupId || '';

const normalizedUserId = normalizedValue(userId, groupId);
const normalizedUserId = normalizedStrategyValue(userId, groupId);

return percentage > 0 && normalizedUserId <= percentage;
}
Expand Down
24 changes: 21 additions & 3 deletions src/lib/features/playground/feature-evaluator/strategy/util.ts
@@ -1,9 +1,27 @@
import * as murmurHash3 from 'murmurhash3js';

export default function normalizedValue(
function normalizedValue(
id: string,
groupId: string,
normalizer = 100,
normalizer: number,
seed = 0,
): number {
return (murmurHash3.x86.hash32(`${groupId}:${id}`) % normalizer) + 1;
const hash = murmurHash3.x86.hash32(`${groupId}:${id}`, seed);
return (hash % normalizer) + 1;
}

const STRATEGY_SEED = 0;

export function normalizedStrategyValue(id: string, groupId: string): number {
return normalizedValue(id, groupId, 100, STRATEGY_SEED);
}

const VARIANT_SEED = 86028157;

export function normalizedVariantValue(
id: string,
groupId: string,
normalizer: number,
): number {
return normalizedValue(id, groupId, normalizer, VARIANT_SEED);
}
4 changes: 2 additions & 2 deletions src/lib/features/playground/feature-evaluator/variant.ts
@@ -1,7 +1,7 @@
import { Context } from './context';
// eslint-disable-next-line import/no-cycle
import { FeatureInterface } from './feature';
import normalizedValue from './strategy/util';
import { normalizedVariantValue } from './strategy/util';
import { resolveContextValue } from './helpers';

interface Override {
Expand Down Expand Up @@ -91,7 +91,7 @@ export function selectVariantDefinition(

const { stickiness } = variants[0];

const target = normalizedValue(
const target = normalizedVariantValue(
getSeed(context, stickiness),
featureName,
totalWeight,
Expand Down

0 comments on commit b54d481

Please sign in to comment.