private void SetSplitTestStatus(Guid splitTestId, Guid statusId)
        {
            var bulkEmailSplit = new BulkEmailSplit(UserConnection);

            if (bulkEmailSplit.FetchFromDB(splitTestId))
            {
                bulkEmailSplit.SetColumnValue("StatusId", statusId);
                bulkEmailSplit.Save();
            }
        }
Пример #2
0
        virtual protected void ExecuteUpdateSplitTestAudience(Guid id, UserConnection userConnection)
        {
            UserConnection = userConnection;
            //Check current segments state and set it to "RequiresUpdating" when need
            bool updateInProcess = IsSchemaAudienceUpdateInProcess("BulkEmailSplit", id);

            if (updateInProcess)
            {
                SetSchemaSegmentStatus("BulkEmailSplit", id);
                return;
            }
processStart:
            //Read split test entity
            var bulkEmailSplit = new BulkEmailSplit(UserConnection);

            if (!bulkEmailSplit.FetchFromDB(id))
            {
                MailingUtilities.Log.ErrorFormat(
                    "[AudienceHelper.ExecuteUpdateSplitTestAudience]: Failed to fetch BulkEmailSplit from DB." +
                    "Id: {0}", id);
                return;
            }
            //Get split test targets count

            List <int> targets = null;

            try {
                targets = GetSplitTestTargetRIdList(bulkEmailSplit, id);
            } catch (Exception e) {
                MailingUtilities.Log.ErrorFormat(
                    "[AudienceHelper.ExecuteUpdateSplitTestAudience]: Failed to read email from BulkEmailSplit." +
                    "Id: {0}", id, e);
                return;
            }
            //Check targets count
            int targetsCount = targets.Count;

            if (targetsCount <= 1)
            {
                return;
            }

            long initialLowBound = 0;
            //CreateTempTable
            string transferTableName = string.Empty;

            try {
                transferTableName = GetTransferTableName(bulkEmailSplit, id);
            } catch (Exception e) {
                MailingUtilities.Log.ErrorFormat(
                    "[AudienceHelper.ExecuteUpdateSplitTestAudience]: UpdateTargetAudienceProcess " +
                    "Creation of temp table failed. BulkEmailSplit.Id: {0}", id, e);
                return;
            }

            var select = new Select(userConnection)
                         .Column(Func.Max("Id"))
                         .From(transferTableName);

            using (DBExecutor dbExecutor = UserConnection.EnsureDBConnection()) {
                using (IDataReader reader = select.ExecuteReader(dbExecutor)) {
                    if (reader.Read() && reader[0] != DBNull.Value)
                    {
                        initialLowBound = reader.GetInt64(0);
                    }
                }
            }

            //Check percent
            double percent = Convert.ToDouble(bulkEmailSplit.RecipientPercent);

            if (percent <= 0)
            {
                return;
            }
            if (percent > 100)
            {
                percent = 100;
            }
            int initialRecipientCount = bulkEmailSplit.RecipientCount;

            //Fill temp table with audience
            try {
                StartUpdateTargetAudience(id, "", false, transferTableName);
            } catch (Exception e) {
                MailingUtilities.Log.ErrorFormat(
                    "[AudienceHelper.ExecuteUpdateSplitTestAudience]: UpdateTargetAudienceProcess failed." +
                    "BulkEmailSplit.Id: {0}", id, e);
                return;
            }
            //Read split test entity
            bulkEmailSplit = new BulkEmailSplit(UserConnection);
            if (!bulkEmailSplit.FetchFromDB(id))
            {
                MailingUtilities.Log.ErrorFormat(
                    "[AudienceHelper.ExecuteUpdateSplitTestAudience]: Failed to fetch BulkEmailSplit from DB." +
                    "Id: {0}", id);
                return;
            }
            int newRecipientsCount = bulkEmailSplit.RecipientCount - initialRecipientCount;

            //Check added recipients count
            if (newRecipientsCount <= 0)
            {
                CreateReminding(bulkEmailSplit, "UpdateSplitTestAudienceSuccessMessage",
                                bulkEmailSplit.Schema.Caption.ToString(), 0, 0);
                return;
            }
            //calc recipients per email count
            int recipientsPerEmail = Convert.ToInt32(Math.Floor(
                                                         Convert.ToDouble(newRecipientsCount) / Convert.ToDouble(targetsCount) / 100.0 * percent
                                                         ));

            //Check recipients count
            if (recipientsPerEmail <= 0)
            {
                //restore recipients list
                new Delete(UserConnection).From(transferTableName)
                .Where("Id").IsGreater(Column.Parameter(initialRecipientCount))
                .Execute();
                //restore SpltTest state
                new Update(UserConnection, "BulkEmailSplit")
                .Set("ModifiedOn", Column.Parameter(DateTime.UtcNow))
                .Set("SegmentsStatusId", Column.Parameter(MarketingConsts.SegmentStatusUpdatedId))
                .Set("RecipientCount", Column.Parameter(initialRecipientCount))
                .Where("Id").IsEqual(Column.Parameter(bulkEmailSplit.Id))
                .Execute();
                CreateReminding(bulkEmailSplit, "UpdateSplitTestAudienceSuccessMessage",
                                bulkEmailSplit.Schema.Caption.ToString(), 0, 0);
                return;
            }
            try {
                //Insert recipients to target BulkEmail
                InsertRecipientsIntoBulkEmails(newRecipientsCount, recipientsPerEmail,
                                               initialRecipientCount, targetsCount, targets, transferTableName);
            } catch (Exception e) {
                MailingUtilities.Log.ErrorFormat(
                    "[AudienceHelper.ExecuteUpdateSplitTestAudience]: Failed to spread test audience betwean email." +
                    "Id: {0}", id, e);
                return;
            }
            if (IsSchemaSegmentsStatusEquals("BulkEmailSplit", id, MarketingConsts.SegmentStatusRequiresUpdatingId))
            {
                goto processStart;
            }
            using (var dbExecutor = UserConnection.EnsureDBConnection()) {
                new Update(UserConnection, "BulkEmailSplit")
                .Set("ModifiedOn", Column.Parameter(DateTime.UtcNow))
                .Set("SegmentsStatusId", Column.Parameter(MarketingConsts.SegmentStatusUpdatedId))
                .Set("TestRecipientCount", new Select(UserConnection)
                     .Column(Func.Sum("RecipientCount"))
                     .From("BulkEmail")
                     .Where("RId").In(Column.Parameters(targets))
                     )
                .Where("Id").IsEqual(Column.Parameter(bulkEmailSplit.Id))
                .Execute(dbExecutor);
            }
            int contactsAdded = recipientsPerEmail * targetsCount;

            CreateReminding(bulkEmailSplit, "UpdateSplitTestAudienceSuccessMessage",
                            bulkEmailSplit.Schema.Caption.ToString(), contactsAdded, newRecipientsCount);
        }