예제 #1
0
        public async Task <bool> IsInVariantAsync(int experimentId, int userId, bool recordParticipation = true)
        {
            var experiment = await _experimentRepository.GetExperimentAsync(experimentId);

            if (!experiment.IsActive)
            {
                return(false);
            }

            var user = await _userRepository.GetUserAsync(userId);

            // Do some bit fiddling to make sure we always return the same value
            // Use experiment.SplitType to calculate it based on the userId or the teamId
            int differentiator;

            switch (experiment.SplitType)
            {
            case GroupSplitType.User:
                differentiator = user.UserId;
                break;

            case GroupSplitType.Team:
                differentiator = user.TeamId;
                break;

            case GroupSplitType.None:
            default:
                throw new ArgumentOutOfRangeException();
            }

            var hash    = GetParticipationHash(experiment.ExperimentId, differentiator);
            var variant = (int)(hash % experiment.Variants.Length);

            if (recordParticipation)
            {
                await _experimentRepository.RecordParticipationAsync(experimentId, userId, variant);
            }

            // 0 is always control
            return(variant > 0);
        }