Exemple #1
0
        protected void AddVariableToSegment(Experiment experiment, IDataReader reader)
        {
            var segmentID = Convert.ToInt32(reader["ExperimentSegmentId"]);
            var segment = experiment.Segments.FirstOrDefault(a => a.Id == segmentID);
            if (segment != null)
            {
                var variableName = reader["Name"].ToString();
                segment.Variables.Add(variableName, reader["Value"].ToString());

                if (experiment.Variables.IndexOf(variableName) == -1)
                {
                    experiment.Variables.Add(variableName);
                }
            }
        }
        public Experiment Add(Experiment experiment)
        {
            Experiment rtnExperiment = null;

            using (DbCommand cmd = _db.GetSqlStringCommand(ExperimentQueries.AddExperiment))
            {
                _db.AddInParameter(cmd, "@Name", DbType.String, experiment.Name);
                _db.AddInParameter(cmd, "@SystemName", DbType.String, experiment.SystemName);
                _db.AddInParameter(cmd, "@TargetCohortId", DbType.Int32, experiment.TargetCohortId);
                _db.AddInParameter(cmd, "@CreatedBy", DbType.String, experiment.CreatedBy);

                using (IDataReader reader = _db.ExecuteReader(cmd))
                {
                    while (reader.Read())
                    {
                        rtnExperiment = ReaderToExperiment(reader);
                    }
                }
            }

            foreach (string variable in experiment.Variables)
            {
                using (DbCommand cmd = _db.GetSqlStringCommand(ExperimentQueries.AddExperimentVariable))
                {
                    _db.AddInParameter(cmd, "@ExperimentId", DbType.Int32, rtnExperiment.Id);
                    _db.AddInParameter(cmd, "@Name", DbType.String, variable);

                    rtnExperiment.Variables.Add(variable);

                    _db.ExecuteNonQuery(cmd);
                }
            }

            foreach (int goalId in experiment.GoalIds)
            {
                using (DbCommand cmd = _db.GetSqlStringCommand(ExperimentQueries.AddExperimentGoal))
                {
                    _db.AddInParameter(cmd, "@ExperimentId", DbType.Int32, rtnExperiment.Id);
                    _db.AddInParameter(cmd, "@GoalId", DbType.Int32, goalId);

                    rtnExperiment.GoalIds.Add(goalId);

                    _db.ExecuteNonQuery(cmd);
                }
            }

            return rtnExperiment;
        }
Exemple #3
0
        protected Experiment ReaderToExperiment(IDataReader reader)
        {
            var experiment = new Experiment();

            if (reader["Id"] != DBNull.Value)
            {
                experiment.Id = Convert.ToInt32(reader["Id"]);
            }

            if (reader["UID"] != DBNull.Value)
            {
                experiment.UID = reader.GetGuid(reader.GetOrdinal("UID"));
            }

            if (reader["Name"] != DBNull.Value)
            {
                experiment.Name = reader["Name"].ToString();
            }

            if (reader["SystemName"] != DBNull.Value)
            {
                experiment.SystemName = reader["SystemName"].ToString();
            }

            if (reader["TargetCohortId"] != DBNull.Value)
            {
                experiment.TargetCohortId = Convert.ToInt32(reader["TargetCohortId"]);
            }

            if (reader.HasColumn("Segments") && reader["Segments"] != DBNull.Value)
            {
                experiment.SegmentCount = Convert.ToInt32(reader["Segments"]);
            }

            if (reader.HasColumn("TargetCohortName") && reader["TargetCohortName"] != DBNull.Value)
            {
                experiment.TargetCohortName = reader["TargetCohortName"].ToString();
            }

            if (reader["CreatedBy"] != DBNull.Value)
            {
                experiment.CreatedBy = reader["CreatedBy"].ToString();
            }

            if (reader["UpdatedBy"] != DBNull.Value)
            {
                experiment.UpdatedBy = reader["UpdatedBy"].ToString();
            }

            if (reader["Created"] != DBNull.Value)
            {
                experiment.Created = Convert.ToDateTime(reader["Created"]);
            }

            if (reader["Updated"] != DBNull.Value)
            {
                experiment.Updated = Convert.ToDateTime(reader["Updated"]);
            }

            experiment.Segments = new List<ExperimentSegment>();
            experiment.Variables = new List<string>();

            return experiment;
        }
        public void TestGetDefault()
        {
            Experiment exp = new Experiment()
            {
                Id = 100,
                Name = "TestExp",
                SystemName = "TestExp",
                TargetCohort = new AuthenticatedUsersCohort()
                {
                    Id = 200,
                    Name = "AllUsers",
                    SystemName = "AllUsers"
                },
                Segments = new List<ExperimentSegment>()
            };

            var segment1 = new ExperimentSegment()
            {
                Id = 300,
                ExperimentId = 100,
                TargetPercentage = 50,
                Name = "Option1",
                SystemName = "Option1"
            };

            exp.Segments.Add(segment1);

            var segment2 = new ExperimentSegment()
            {
                Id = 301,
                ExperimentId = 100,
                TargetPercentage = 50,
                Name = "Option2",
                SystemName = "Option2"
            };

            exp.Segments.Add(segment2);

            var defSeg = new ExperimentSegment()
            {
                Id = 302,
                ExperimentId = 100,
                TargetPercentage = 0,
                Name = "Default",
                SystemName = "Default",
                IsDefault=true
            };

            exp.Segments.Add(defSeg);

            var visitor = new Visitor()
            {
                ExperimentSegmentsLoaded = true,
                CohortsLoaded = true,
                AttributesLoaded = true,
                ConverstionsLoaded = true,
                DetailsLoaded = true,
                LandingUrlsLoaded = true,
                ReferrersLoaded = true,
                ExperimentSegments = new List<ExperimentSegment>(),
                Cohorts = new List<Cohort>(),
                Attributes = new Dictionary<string, string>(),
                Conversions = new List<Goal>(),
                FirstVisit = DateTime.Now,
                IsAuthenticated=false,
            };

            var hyp = ExperimentManager.Current.GetHypothesis(exp, visitor);

            Assert.IsNotNull(hyp);
            Assert.IsTrue(hyp == defSeg);
            Assert.AreEqual<int>(0, visitor.ExperimentSegments.Count);
            Assert.AreEqual<int>(0, visitor.Request.ExperimentSegments.Count);
        }
        /// <summary>
        /// Mostly for testing, allows you to specify experiment and visitor properties.
        /// </summary>
        /// <param name="experiment"></param>
        /// <param name="visitor"></param>
        /// <returns></returns>
        public ExperimentSegment GetHypothesis(Experiment experiment, Visitor visitor)
        {
            //See if user has current segments loaded.
            if (!visitor.IsNew && !visitor.ExperimentSegmentsLoaded)
            {
                visitor = _visitProvider.GetSegments(visitor);
                visitor.ExperimentSegmentsLoaded = true;
            }

            var existingSegment = visitor.ExperimentSegments.FirstOrDefault(a => a.ExperimentId == experiment.Id);
            if (existingSegment != null)
            {
                return existingSegment;
            }

            //check if user is in cohort.
            if (userIsInCohort(visitor, experiment.TargetCohort))
            {
                //Randomly select a segment
                int randNum = _random.Next(1, 100);
                double counter = 0;
                foreach (var segment in experiment.Segments)
                {
                    if (segment.TargetPercentage != 0)
                    {
                        if (counter + segment.TargetPercentage >= randNum)
                        {
                            visitor.Request.ExperimentSegments.Add(segment);
                            visitor.ExperimentSegments.Add(segment);
                            return segment;
                        }
                        counter += segment.TargetPercentage;
                    }
                }
            }
            else
            {
                //Get Default Segment
                return experiment.Segments.FirstOrDefault(a => a.IsDefault);
            }

            return null;
        }
        public Experiment Update(Experiment experiment)
        {
            using (DbCommand cmd = _db.GetSqlStringCommand(ExperimentQueries.UpdateExperiment))
            {
                _db.AddInParameter(cmd, "@Name", DbType.String, experiment.Name);
                _db.AddInParameter(cmd, "@UpdatedBy", DbType.String, experiment.UpdatedBy);
                _db.AddInParameter(cmd, "@TargetCohortId", DbType.Int32, experiment.TargetCohortId);
                _db.AddInParameter(cmd, "@ExperimentId", DbType.Int32, experiment.Id);

                _db.ExecuteNonQuery(cmd);
            }

            var updated = Get(experiment.Id);

            //only add new variables as they shouldn't be deleted/updated
            if (experiment.Variables.Count != updated.Variables.Count)
            {
                foreach (string variable in experiment.Variables)
                {
                    if (updated.Variables.IndexOf(variable) == -1)
                    {
                        using (DbCommand cmd = _db.GetSqlStringCommand(ExperimentQueries.AddExperimentVariable))
                        {
                            _db.AddInParameter(cmd, "@ExperimentId", DbType.Int32, experiment.Id);
                            _db.AddInParameter(cmd, "@Name", DbType.String, variable);

                            _db.ExecuteNonQuery(cmd);
                        }
                    }
                }
            }

            //delete any existing goals
            using (DbCommand cmd = _db.GetSqlStringCommand(ExperimentQueries.DeleteExperimentGoals))
            {
                _db.AddInParameter(cmd, "@ExperimentId", DbType.Int32, experiment.Id);
                _db.ExecuteNonQuery(cmd);
            }

            //add back goals
            foreach (int goalId in experiment.GoalIds)
            {
                using (DbCommand cmd = _db.GetSqlStringCommand(ExperimentQueries.AddExperimentGoal))
                {
                    _db.AddInParameter(cmd, "@ExperimentId", DbType.Int32, experiment.Id);
                    _db.AddInParameter(cmd, "@GoalId", DbType.Int32, goalId);

                    _db.ExecuteNonQuery(cmd);
                }
            }

            return experiment;
        }
        public void Delete(Experiment experiment)
        {
            using (DbCommand cmd = _db.GetSqlStringCommand(ExperimentQueries.DeleteExperiment))
            {
                _db.AddInParameter(cmd, "@ExperimentId", DbType.Int32, experiment.Id);
                _db.AddInParameter(cmd, "@UpdatedBy", DbType.String, experiment.UpdatedBy);

                _db.ExecuteNonQuery(cmd);
            }
        }