public void SaveTest(TestConfiguration config, IDatabase dbContext = null) { var db = dbContext ?? _dbConnector.GetDatabase(); var dataStream = config.SerializeAsJson(); db.StringSet(DatabaseKeys.ExperimentKeyFormat.FormatWith(config.Name), dataStream); }
public Participant GetParticipant(string clientId, IDatabase dbContext = null) { var db = dbContext ?? _dbConnector.GetDatabase(); var participant = db.StringGet(DatabaseKeys.ParticipantKeyFormat.FormatWith(clientId)); if (participant.IsNullOrEmpty) { return(null); } return(participant.SerializeFromJson <Participant>()); }
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); } }
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); }