示例#1
0
        public void ConfigureTest(string name, TestConfiguration config)
        {
            _logger.LogDebug(name);
            var db          = _dbConnector.GetDatabase();
            var currentTest = _testContext.GetTest(name, dbContext: db);

            if (currentTest == null)
            {
                _logger.LogDebug("test {0} does not exist, creating".FormatWith(name));
                config.Version = 1;

                _testContext.SaveTest(config);
            }
            else
            {
                _logger.LogDebug("test {0} exists, updating".FormatWith(name));
                currentTest.Variants = config.Variants;
                currentTest.Version += 1;

                _testContext.SaveTest(currentTest);
            }
        }
示例#2
0
        public ActionResult GetTest(string test)
        {
            var testConfig = _testContext.GetTest(test);

            return(Ok(testConfig));
        }
示例#3
0
        public string EnrollParticipantInTest(string clientId, string test)
        {
            var db = _dbConnector.GetDatabase();

            var currentTest = _testContext.GetTest(test, dbContext: db);

            // check to see if the test is valid
            if (currentTest == null)
            {
                throw new Exception(
                          "Test {0} has not been configured, cannot enroll".FormatWith(test));
            }

            var participant     = _participantContext.GetParticipant(clientId);
            var selectedVariant = _variantPicker.SelectVariant(currentTest);

            // check to see if the user has already been setup
            if (participant == null)
            {
                _logger.LogDebug("User was not setup with any tests, creating key");
                participant = new Participant {
                    ClientId      = clientId,
                    EnrolledTests = new List <Test> ()
                };
            }

            var existingTest    = participant.EnrolledTests.Where(x => x.Name == test).FirstOrDefault();
            var pipelineBuilder = new PipelineBuilder(db);

            if (existingTest == null)
            {
                _logger.LogDebug("User did not have test, setting up");
                participant.EnrolledTests = participant.EnrolledTests.Append(new Test {
                    Name               = test,
                    SelectedVariant    = selectedVariant,
                    VersionAtSelection = currentTest.Version
                });
                pipelineBuilder.StringIncrement(DatabaseKeys.ExperimentCurrentCountKeyFormat.FormatWith(test));
                pipelineBuilder.StringIncrement(DatabaseKeys.ExperimentVariantVersionKeyFormat.FormatWith(test, currentTest.Version, selectedVariant));
            }
            else
            {
                if (existingTest.VersionAtSelection == currentTest.Version)
                {
                    _logger.LogDebug("User already had variant");
                    return(existingTest.SelectedVariant);
                }
                else
                {
                    _logger.LogDebug("User variant version differs, updating");
                    existingTest.SelectedVariant    = selectedVariant;
                    existingTest.VersionAtSelection = currentTest.Version;
                    pipelineBuilder.StringIncrement(DatabaseKeys.ExperimentCurrentCountKeyFormat.FormatWith(test));

                    // clean existing recorded goals for the new test
                    var goalsAsSet      = _participantContext.GetParticipantsGoals(participant.ClientId, db);
                    var experimentGoals = goalsAsSet.Keys.Where(x => x.StartsWith(test + ":")).ToList();

                    pipelineBuilder.StringIncrement(DatabaseKeys.ExperimentVariantVersionKeyFormat.FormatWith(test, currentTest.Version, selectedVariant));
                    pipelineBuilder.SetRemoveRange(DatabaseKeys.ParticipantGoalsFormat.FormatWith(clientId), experimentGoals);
                }
            }

            var participantBytes = participant.SerializeAsJson();

            pipelineBuilder.StringSet(DatabaseKeys.ParticipantKeyFormat.FormatWith(clientId), participantBytes);

            pipelineBuilder.Execute();

            return(selectedVariant);
        }