mirror of
				https://github.com/Unleash/unleash.git
				synced 2025-10-27 11:02:16 +01:00 
			
		
		
		
	fix: variant hashing in playground (#5213)
This commit is contained in:
		
							parent
							
								
									a7f3a89387
								
							
						
					
					
						commit
						b54d481fc8
					
				@ -1,6 +1,6 @@
 | 
				
			|||||||
import { Strategy } from './strategy';
 | 
					import { Strategy } from './strategy';
 | 
				
			||||||
import { Context } from '../context';
 | 
					import { Context } from '../context';
 | 
				
			||||||
import normalizedValue from './util';
 | 
					import { normalizedStrategyValue } from './util';
 | 
				
			||||||
import { resolveContextValue } from '../helpers';
 | 
					import { resolveContextValue } from '../helpers';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const STICKINESS = {
 | 
					const STICKINESS = {
 | 
				
			||||||
@ -54,7 +54,7 @@ export default class FlexibleRolloutStrategy extends Strategy {
 | 
				
			|||||||
        if (!stickinessId) {
 | 
					        if (!stickinessId) {
 | 
				
			||||||
            return false;
 | 
					            return false;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        const normalizedUserId = normalizedValue(stickinessId, groupId);
 | 
					        const normalizedUserId = normalizedStrategyValue(stickinessId, groupId);
 | 
				
			||||||
        return percentage > 0 && normalizedUserId <= percentage;
 | 
					        return percentage > 0 && normalizedUserId <= percentage;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
@ -1,5 +1,5 @@
 | 
				
			|||||||
import { Strategy } from './strategy';
 | 
					import { Strategy } from './strategy';
 | 
				
			||||||
import normalizedValue from './util';
 | 
					import { normalizedStrategyValue } from './util';
 | 
				
			||||||
import { Context } from '../context';
 | 
					import { Context } from '../context';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export default class GradualRolloutSessionIdStrategy extends Strategy {
 | 
					export default class GradualRolloutSessionIdStrategy extends Strategy {
 | 
				
			||||||
@ -19,7 +19,7 @@ export default class GradualRolloutSessionIdStrategy extends Strategy {
 | 
				
			|||||||
        const percentage = Number(parameters.percentage);
 | 
					        const percentage = Number(parameters.percentage);
 | 
				
			||||||
        const groupId = parameters.groupId || '';
 | 
					        const groupId = parameters.groupId || '';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        const normalizedId = normalizedValue(sessionId, groupId);
 | 
					        const normalizedId = normalizedStrategyValue(sessionId, groupId);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        return percentage > 0 && normalizedId <= percentage;
 | 
					        return percentage > 0 && normalizedId <= percentage;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
				
			|||||||
@ -1,6 +1,6 @@
 | 
				
			|||||||
import { Strategy } from './strategy';
 | 
					import { Strategy } from './strategy';
 | 
				
			||||||
import { Context } from '../context';
 | 
					import { Context } from '../context';
 | 
				
			||||||
import normalizedValue from './util';
 | 
					import { normalizedStrategyValue } from './util';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export default class GradualRolloutUserIdStrategy extends Strategy {
 | 
					export default class GradualRolloutUserIdStrategy extends Strategy {
 | 
				
			||||||
    constructor() {
 | 
					    constructor() {
 | 
				
			||||||
@ -19,7 +19,7 @@ export default class GradualRolloutUserIdStrategy extends Strategy {
 | 
				
			|||||||
        const percentage = Number(parameters.percentage);
 | 
					        const percentage = Number(parameters.percentage);
 | 
				
			||||||
        const groupId = parameters.groupId || '';
 | 
					        const groupId = parameters.groupId || '';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        const normalizedUserId = normalizedValue(userId, groupId);
 | 
					        const normalizedUserId = normalizedStrategyValue(userId, groupId);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        return percentage > 0 && normalizedUserId <= percentage;
 | 
					        return percentage > 0 && normalizedUserId <= percentage;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
				
			|||||||
@ -1,9 +1,27 @@
 | 
				
			|||||||
import * as murmurHash3 from 'murmurhash3js';
 | 
					import * as murmurHash3 from 'murmurhash3js';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export default function normalizedValue(
 | 
					function normalizedValue(
 | 
				
			||||||
    id: string,
 | 
					    id: string,
 | 
				
			||||||
    groupId: string,
 | 
					    groupId: string,
 | 
				
			||||||
    normalizer = 100,
 | 
					    normalizer: number,
 | 
				
			||||||
 | 
					    seed = 0,
 | 
				
			||||||
): number {
 | 
					): 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);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
@ -1,7 +1,7 @@
 | 
				
			|||||||
import { Context } from './context';
 | 
					import { Context } from './context';
 | 
				
			||||||
// eslint-disable-next-line import/no-cycle
 | 
					// eslint-disable-next-line import/no-cycle
 | 
				
			||||||
import { FeatureInterface } from './feature';
 | 
					import { FeatureInterface } from './feature';
 | 
				
			||||||
import normalizedValue from './strategy/util';
 | 
					import { normalizedVariantValue } from './strategy/util';
 | 
				
			||||||
import { resolveContextValue } from './helpers';
 | 
					import { resolveContextValue } from './helpers';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
interface Override {
 | 
					interface Override {
 | 
				
			||||||
@ -91,7 +91,7 @@ export function selectVariantDefinition(
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    const { stickiness } = variants[0];
 | 
					    const { stickiness } = variants[0];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    const target = normalizedValue(
 | 
					    const target = normalizedVariantValue(
 | 
				
			||||||
        getSeed(context, stickiness),
 | 
					        getSeed(context, stickiness),
 | 
				
			||||||
        featureName,
 | 
					        featureName,
 | 
				
			||||||
        totalWeight,
 | 
					        totalWeight,
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
		Reference in New Issue
	
	Block a user