private List <AllEgsOutputReport> GetExoReport(int Id, string Mode = "P")
        {
            var exchangeOutputReportList = new List <AllEgsOutputReport>();
            var qcRawResult = QcExecutionRawResultHandler.GetQcExecutionRawResultById(Id);

            if (qcRawResult != null)
            {
                // Deserialize the Result
                var qcResults = JsonConvert.DeserializeObject <List <AllEgsOutputModel> >(qcRawResult.Result);

                // Fetch the GDCO tickets
                var projectIds = new List <string>();
                foreach (var qcResult in qcResults)
                {
                    if (!string.IsNullOrEmpty(qcResult.ProjectId))
                    {
                        projectIds.Add(qcResult.ProjectId);
                    }
                    else if (!string.IsNullOrEmpty(qcResult.MdmId))
                    {
                        projectIds.Add(qcResult.MdmId);
                    }
                }
                var gdcoTicketsDictionary = new GdcoTicketHandler().GetTickets(projectIds, "msassetsku");

                foreach (var qcResult in qcResults)
                {
                    var exchangeOutputReport = new AllEgsOutputReport();
                    exchangeOutputReport.ExecutionDate = qcRawResult.ExecutionDate;
                    exchangeOutputReport.DetailReport  = qcRawResult.ReportFilePath;
                    if (qcResult != null && qcResult.AllEgsOutput != null)
                    {
                        if (Mode.Equals("P"))
                        {
                            exchangeOutputReport.ProjectId = qcResult.ProjectId;
                        }
                        else if (Mode.Equals("M") || Mode.Equals("D"))
                        {
                            exchangeOutputReport.MdmId = qcResult.MdmId;
                        }
                        exchangeOutputReport.TotalTests  = qcResult.AllEgsOutput.Count;
                        exchangeOutputReport.PassedTests = qcResult.AllEgsOutput.Where(eo => eo.TestStatus.Equals("Passed")).ToList().Count;
                        exchangeOutputReport.FailedTests = qcResult.AllEgsOutput.Where(eo => eo.TestStatus.Equals("Failed")).ToList().Count;
                        exchangeOutputReport.WorkOrder   = qcResult.WorkOrderName;
                        if ((!string.IsNullOrEmpty(qcResult.ProjectId) && gdcoTicketsDictionary.ContainsKey(qcResult.ProjectId)) || (!string.IsNullOrEmpty(qcResult.MdmId) && gdcoTicketsDictionary.ContainsKey(qcResult.MdmId)))
                        {
                            var pid = string.IsNullOrEmpty(qcResult.ProjectId) ? qcResult.MdmId : qcResult.ProjectId;
                            exchangeOutputReport.gdcoTickets = gdcoTicketsDictionary[pid];
                        }
                    }
                    exchangeOutputReportList.Add(exchangeOutputReport);
                }
            }
            return(exchangeOutputReportList);
        }
        private List <AllEgsOutputReport> GetExoReport(List <AllEgsOutputModel> AllEgsOutput, string Mode)
        {
            var allEgsOutputReportList = new List <AllEgsOutputReport>();

            if (AllEgsOutput != null)
            {
                // Fetch the GDCO tickets
                var projectIds = new List <string>();
                foreach (var qcResult in AllEgsOutput)
                {
                    if (!string.IsNullOrEmpty(qcResult.ProjectId))
                    {
                        projectIds.Add(qcResult.ProjectId);
                    }
                    else if (!string.IsNullOrEmpty(qcResult.MdmId))
                    {
                        projectIds.Add(qcResult.MdmId);
                    }
                }
                var gdcoTicketsDictionary = new GdcoTicketHandler().GetTickets(projectIds, "allegs_msassetsku");

                foreach (var qcResult in AllEgsOutput)
                {
                    var allEgsOutputReport = new AllEgsOutputReport();
                    allEgsOutputReport.PropertyGroupName = qcResult.PropertyGroupName;
                    if (qcResult != null && qcResult.AllEgsOutput != null)
                    {
                        if (Mode.Equals("P"))
                        {
                            allEgsOutputReport.ProjectId = qcResult.ProjectId;
                        }
                        else if (Mode.Equals("M") || Mode.Equals("D"))
                        {
                            allEgsOutputReport.MdmId = qcResult.MdmId;
                        }
                        allEgsOutputReport.TotalTests  = qcResult.AllEgsOutput.Count;
                        allEgsOutputReport.PassedTests = qcResult.AllEgsOutput.Where(eo => eo.TestStatus.Equals("Passed")).ToList().Count;
                        allEgsOutputReport.FailedTests = qcResult.AllEgsOutput.Where(eo => eo.TestStatus.Equals("Failed")).ToList().Count;
                        allEgsOutputReport.WorkOrder   = qcResult.WorkOrderName;
                        if ((!string.IsNullOrEmpty(qcResult.ProjectId) && gdcoTicketsDictionary.ContainsKey(qcResult.ProjectId)) || (!string.IsNullOrEmpty(qcResult.MdmId) && gdcoTicketsDictionary.ContainsKey(qcResult.MdmId)))
                        {
                            var pid = string.IsNullOrEmpty(qcResult.ProjectId) ? qcResult.MdmId : qcResult.ProjectId;
                            allEgsOutputReport.gdcoTickets = gdcoTicketsDictionary[pid];
                        }
                    }
                    allEgsOutputReportList.Add(allEgsOutputReport);
                }
            }
            return(allEgsOutputReportList);
        }
        public AllEgsResult GetDSMsAssetSkuValidationResult(string fulfillmentID = null, string CurrentUser = null, bool Email = false, string EmailList = null)
        {
            var exoResult         = new AllEgsResult();
            var exchangeOutput    = new List <AllEgsOutputModel>();
            var error             = new StringBuilder();
            var emailSubject      = "Exchange MSAsset/SKU Validation Report for FulfillmentIDs from DS - " + DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss");
            var errorEmailSubject = "Error: Exchange MSAsset/SKU Validation Report for FulfillmentIDs from DS - " + DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss");

            ExecutionMode = (fulfillmentID == null) ? "S" : "U";

            if (currentExeName != null)
            {
                emailTrigger = currentExeName.IndexOf("AllEgsMsAssetSkuValidationReporting", StringComparison.OrdinalIgnoreCase) >= 0 ? true : false;
            }

            Dictionary <string, List <DSDataSet> > dsDataSet = null;

            try
            {
                dsDataSet = new DSDataSetAccess().GetInProgressExchangeDeploymentsToRTEG(fulfillmentID);
            }
            catch (Exception ex)
            {
                throw ex;
            }

            AllEgsQc exoQc = new AllEgsQc();

            // Get FulfillmentId to DemandId mapping
            GDCOTicketStoreHandler      GDCOTicketStoreHandler = new GDCOTicketStoreHandler();
            Dictionary <string, string> FidToDemandIdMapping   = GDCOTicketStoreHandler.GetFidToDemandIdMapping(dsDataSet.Keys.ToList());

            // Get CPS FidList, it contains all of the fids which are CPS
            Dictionary <string, string> CpsFidTitleDic = GDCOTicketStoreHandler.GetCpsFidTitleDic(FidToDemandIdMapping);

            // Iterate over each DS project
            foreach (var fID in dsDataSet.Keys)
            {
                var dsTasks   = dsDataSet[fID];
                var GroupType = dsTasks.Last().GroupType;
                if (dsTasks != null && dsTasks.Any())
                {
                    var dcCode = dsTasks.Last().DeploymentDCCode;

                    var exoOpModel = exoQc.PerformAllEgsSkuMsAssetValidation(fID, dsTasks.Last().GDCOFaultDescription, dsTasks.Last().DeploymentPGName, "D", null, null, dcCode, GroupType, CpsFidTitleDic, dsTasks.Last().MDMIdList, null, true);
                    exoOpModel.GroupType = GroupType;
                    exchangeOutput.Add(exoOpModel);
                    error.AppendLine(exoOpModel.Error.ToString());
                }
            }

            // Save the exchangeOutput to QcExecutionRawResult table and to the shared path
            List <string> resultFiles = null;

            try
            {
                resultFiles = SaveResultToFile(exchangeOutput, "D");
            }
            catch (Exception ex)
            {
                error.AppendLine("Exception in saving results to the csv file. " + ex.ToString());
            }

            try
            {
                var gdcoTickets = CreateGdcoTickets(exchangeOutput, "D");

                // Create blocking ICM ticket for EXO fid
                DSDataSetAccess   dsa = new DSDataSetAccess();
                GdcoTicketHandler gdcoTicketHandler = new GdcoTicketHandler();

                foreach (var fID in dsDataSet.Keys)
                {
                    var dsTasks = dsDataSet[fID];
                    if (dsTasks != null && dsTasks.Any() && dsTasks.Last().GroupType.IndexOf("PreRack", StringComparison.OrdinalIgnoreCase) < 0)
                    {
                        var dcCode           = dsTasks.Last().DeploymentDCCode;
                        var clusterName      = dsTasks.Last().ClusterName;
                        var currentWorkOrder = dsTasks.Last().GDCOFaultDescription;
                        var errorDescription = "Please complete manual QC for this cluster " + clusterName + "< EOM >";
                        var errorCode        = "Exo manual block ticket";
                        var errorTitle       = "NetworkProvisioning - General Investigation for Azure Networking - MOR/PRD build (Non PNaaS)";

                        if (dsa.IsInOA(dsTasks))
                        {
                            var tickets = new List <GdcoTicket.Model.GdcoTableTicket>();
                            try
                            {
                                tickets = new GdcoTicketTableHandler().GetTickets(fID, "msassetsku");
                            }
                            catch (Exception ex)
                            {
                                return(null);
                            }

                            bool             blockTicketExist = false;
                            List <DSDataSet> oaTasks          = new List <DSDataSet>();

                            if (dsDataSet.ContainsKey(fID))
                            {
                                oaTasks = dsDataSet[fID].Where(d => (d.GDCOFaultCode.Equals("124110"))).ToList();
                            }
                            foreach (var oaTask in oaTasks)
                            {
                                if (oaTask.TicketState.Equals("Created", StringComparison.OrdinalIgnoreCase) || oaTask.TicketState.Equals("InProgress", StringComparison.OrdinalIgnoreCase) ||
                                    oaTask.TicketState.Equals("Blocked", StringComparison.OrdinalIgnoreCase))
                                {
                                    // Loop through each ticket to see if manual exo blocking ticket exist under active OA task ticket
                                    foreach (var ticket in tickets)
                                    {
                                        if (ticket.GdcoTicket.IndexOf(errorTitle, StringComparison.OrdinalIgnoreCase) >= 0 &&
                                            ticket.GdcoTicket.IndexOf(oaTask.TicketId, StringComparison.OrdinalIgnoreCase) >= 0)
                                        {
                                            blockTicketExist = true;
                                            break;
                                        }
                                    }
                                }
                                if (blockTicketExist)
                                {
                                    break;
                                }
                            }

                            if (!blockTicketExist)
                            {
                                try
                                {
                                    var ExoBlockingTicket = new GdcoTicketHandler().CreateTicket(fID, currentWorkOrder,
                                                                                                 errorCode, errorTitle, errorDescription, dcCode, "oadpm", "3", "", "D", "msassetsku");
                                    gdcoTickets.Add(ExoBlockingTicket);

                                    // Assign blocking tickets' parents
                                    DSDataSet oaTask = null;

                                    if (dsDataSet.ContainsKey(fID))
                                    {
                                        oaTask = dsDataSet[fID].Where(d => ((d.GDCOFaultCode.Equals("124110")) &&
                                                                            (d.TicketState.Equals("Created", StringComparison.OrdinalIgnoreCase) || d.TicketState.Equals("InProgress", StringComparison.OrdinalIgnoreCase)))).FirstOrDefault();
                                    }
                                    if (oaTask != null && oaTask.TicketId != null)
                                    {
                                        new GdcoTicketHandler().AssignParent(Convert.ToInt64(oaTask.TicketId), ExoBlockingTicket.Id);
                                    }
                                }
                                catch (Exception ex)
                                {
                                    SendEmail.SendExoSkuMsAssetReportEmail(Constants.serviceAccount, Constants.automationTeam, "EXO Blocking ticket failure", ex.ToString());
                                }
                            }
                        }
                    }
                }

                foreach (var gdcoTicket in gdcoTickets)
                {
                    if (!string.IsNullOrEmpty(gdcoTicket.Error))
                    {
                        error.AppendLine("Error in GDCO Ticket Creation " + gdcoTicket.Error);
                    }
                }
            }
            catch (Exception ex)
            {
                error.AppendLine("Exception in creating GDCO Tickets. " + ex.ToString());
            }

            // Insert the record and send email
            try
            {
                exoResult.Result = exchangeOutput;
                var insertedResultId = QcExecutionRawResultHandler.InsertQcExecutionRawResult(PropertyGroup, ExecutionMode, JsonConvert.SerializeObject(exchangeOutput), DateTime.Now, resultFiles, CurrentUser);
                exoResult.Report = GetExoReport(insertedResultId, "D");
            }
            catch (Exception ex)
            {
                error.AppendLine("Error in inserting the execution result. " + ex.ToString());
            }
            exoResult.Error = error;

            // send diff email
            if (EmailBody.Length > 440)
            {
                string to      = "*****@*****.**";
                string subject = "OA CIS tickets comparision";
                EmailBody.AppendLine("</table>");
                SendEmail.SendExoSkuMsAssetReportEmail(Constants.serviceAccount, to, subject, EmailBody.ToString());
            }

            if (Email)
            {
                var    body            = new StringBuilder();
                var    dbSummaryReport = new StringBuilder();
                string dbDetailReport  = null;
                body.AppendLine("<style> table {    font-family: arial, sans-serif;    border-collapse: collapse;    width: 100%;}td, th {    border: 1px solid #dddddd;    text-align: left;    padding: 8px;}tr:nth-child(even) {    background-color: #dddddd;} h3 {    font-family: arial, sans-serif; color:#2F4F4F;}</style>");
                body.AppendLine("<p><h3>Summary Report:</h3></p>");
                body.AppendLine("<table>");
                body.AppendLine("<tr><td>MdmId</td><td>Current WO</td><td>Failed Tests</td><td>Ticket(s)/Status</td></tr>");
                foreach (var report in exoResult.Report)
                {
                    var tickets = new StringBuilder(string.Empty);
                    if (report.gdcoTickets != null && report.gdcoTickets.Any())
                    {
                        foreach (var gdcoTicket in report.gdcoTickets)
                        {
                            if (gdcoTicket.Fields.ContainsKey("GDCOTicketing.Custom.UpdateInfo") && gdcoTicket.Fields.ContainsKey("System.State"))
                            {
                                var ticketUrl  = gdcoTicket.Fields["GDCOTicketing.Custom.UpdateInfo"].ToString();
                                var firstIndex = ticketUrl.IndexOf("<a");
                                var lastIndex  = ticketUrl.IndexOf(">", firstIndex);
                                tickets.Append(ticketUrl.Substring(firstIndex, lastIndex - firstIndex + 1) + gdcoTicket.Id + "</a>" + "(" + gdcoTicket.Fields["System.State"] + ") ");
                            }
                        }
                    }
                    body.AppendLine("<tr><td>" + report.MdmId + "</td><td>" + report.WorkOrder + "</td><td>" + report.FailedTests + "</td><td>" + tickets.ToString() + "</td></tr>");
                    dbSummaryReport.Append(report.MdmId + "," + report.WorkOrder + "," + report.FailedTests + "," + tickets.ToString() + ";");
                }
                if (dbSummaryReport.Length > 0)
                {
                    dbSummaryReport.Remove(dbSummaryReport.Length - 1, 1);
                }
                body.AppendLine("</table>");
                if (exoResult.Report != null && exoResult.Report.Any())
                {
                    body.AppendLine("<p><h3>Detail Report: </h3>" + exoResult.Report.First().DetailReport + "</p>");
                    dbDetailReport = exoResult.Report.First().DetailReport;
                }
                // Save the Report to database
                try
                {
                    new ReportingHandler().SaveReport(dbSummaryReport.ToString(), dbDetailReport, "ds", "msassetsku");
                }
                catch (Exception ex)
                {
                    exoResult.Error.AppendLine(ex.ToString());
                }

                if (exoResult.Error != null && exoResult.Error.ToString().Trim().Length > 0)
                {
                    string[] errorLines = exoResult.Error.ToString().Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
                    body.AppendLine("<p><h3>Errors: </h3><ol>");
                    foreach (var erroLine in errorLines)
                    {
                        if (erroLine != null && !string.IsNullOrEmpty(erroLine.Trim()))
                        {
                            body.AppendLine("<li>" + erroLine + "</li>");
                        }
                    }
                    body.AppendLine("</ol></p>");
                }
                SendEmail.SendExoSkuMsAssetReportEmail(CurrentUser, EmailList, emailSubject, body.ToString());
            }

            // Send out email to OA when no pid/mdmid found in MSAsset and no PO found for these Fids
            if (emailTrigger && error.ToString().IndexOf("MSAsset returned no output", StringComparison.OrdinalIgnoreCase) >= 0)
            {
                // Filter the error just contains MsAsset association issues.
                string[]      sep     = { Environment.NewLine, "\n" }; // "\n" added in case you manually appended a newline
                string[]      errList = error.ToString().Split(sep, StringSplitOptions.None);
                StringBuilder sb      = new StringBuilder();
                sb.AppendLine("<p><h3>Below EXO fids don't have both MsAsset and PO Associations, please find investigate. </h3></p>");

                foreach (string line in errList)
                {
                    if (line.IndexOf("MSAsset returned no output", StringComparison.OrdinalIgnoreCase) >= 0)
                    {
                        sb.AppendLine(line);
                    }
                }

                string subject = "Below EXO Fids don't have MsAsset and PO Associations";
                string to      = Constants.msAssetSupportAccount + ";" + Constants.OADPMAccount;
                SendEmail.SendExoSkuMsAssetReportEmail(Constants.serviceAccount, to, subject, sb.ToString());
            }

            return(exoResult);
        }
        private List <GdcoTicket.Model.GdcoTicket> CreateGdcoTickets(List <AllEgsOutputModel> results, string mode)
        {
            var gdcoTickets = new List <GdcoTicket.Model.GdcoTicket>();
            HashSet <String> failedDescriptions = new HashSet <String>();

            if (results == null || !results.Any())
            {
                return(gdcoTickets);
            }

            // Check for failed tests, create tickets
            foreach (var result in results)
            {
                var errorTypeToExchangeResults = new Dictionary <string, List <AllEngineeringGroupsResult> >();
                var exchangeResults            = result.AllEgsOutput.Where(eo => eo.TestStatus.Equals("Failed") && !string.IsNullOrEmpty(eo.QcName));
                var mdmId = string.IsNullOrEmpty(result.MdmId) ? result.ProjectId : result.MdmId;

                KustoAccess ka = new KustoAccess();

                // compare the current ticket with CIS Kusto feed, if different, cut the ticket
                var kustoErrMsg = "";

                if (exchangeResults != null && exchangeResults.Any())
                {
                    kustoErrMsg = ka.GetErrorEsgByMdmid(mdmId);
                }

                foreach (var exchangeResult in exchangeResults)
                {
                    if (!errorTypeToExchangeResults.ContainsKey(exchangeResult.QcName))
                    {
                        errorTypeToExchangeResults[exchangeResult.QcName] = new List <AllEngineeringGroupsResult> {
                            exchangeResult
                        };
                    }
                    else
                    {
                        var exchangeResultList = errorTypeToExchangeResults[exchangeResult.QcName];
                        exchangeResultList.Add(exchangeResult);
                    }
                }

                if (!errorTypeToExchangeResults.Any())
                {
                    continue;
                }

                foreach (var key in errorTypeToExchangeResults.Keys)
                {
                    var    exchangeErrorList = errorTypeToExchangeResults[key];
                    var    projectId         = string.IsNullOrEmpty(result.ProjectId) ? result.MdmId : result.ProjectId;
                    var    currentWorkOrder  = exchangeErrorList.First().WorkOrderName;
                    var    errorCode         = key;
                    string errorTitle        = null;
                    var    errorDescription  = GetExoResultError(exchangeErrorList);
                    var    dcCode            = result.DataCenterCode;
                    var    requestOwner      = "oadpm";
                    string severity          = null;
                    string parentTicketId    = null;

                    // KustoErrMsg is empty means we couldn't found ticket in Kusto feed, we should email
                    if (String.IsNullOrEmpty(kustoErrMsg))
                    {
                        // send email if there is something diff between us and their Kusto feed
                        EmailBody.AppendLine("<tr><td>" + projectId + "</td><td>" + errorTitle + "</td><td>" + errorDescription + "</td></tr>");
                    }

                    if (result.GroupType.IndexOf("PreRack", StringComparison.OrdinalIgnoreCase) >= 0)
                    {
                        continue;
                    }

                    var gdcoTicket = new GdcoTicketHandler().CreateTicket(projectId, currentWorkOrder,
                                                                          errorCode, errorTitle, errorDescription, dcCode, requestOwner, severity, parentTicketId, mode, "msassetsku");
                    gdcoTickets.Add(gdcoTicket);

                    try
                    {
                        if (mode.Equals("P"))
                        {
                            new GdcoTicketHandler().AssignParent(projectId, gdcoTicket.Id);
                        }
                        else if (mode.Equals("M"))
                        {
                            var cisProject = new CisAccess().GetCisProjectsFromCache().CisProjects.Where(c => c.MdmId.Equals(projectId)).FirstOrDefault();
                            if (cisProject != null && cisProject.UpdateAssetDataTicketId != null)
                            {
                                new GdcoTicketHandler().AssignParent(Convert.ToInt64(cisProject.UpdateAssetDataTicketId), gdcoTicket.Id);
                            }
                        }
                        else if (mode.Equals("D"))
                        {
                            var       dsDataSet       = new DSDataSetAccess().GetInProgressExchangeDeploymentsToRTEG();
                            DSDataSet updateAssetTask = null;
                            DSDataSet oaTask          = null;
                            if (dsDataSet.ContainsKey(projectId))
                            {
                                updateAssetTask = dsDataSet[projectId].Where(d => (d.GDCOFaultCode.Equals("124107") || d.GDCOFaultCode.Equals("124246") || d.GDCOFaultCode.Equals("124054"))).FirstOrDefault();
                                oaTask          = dsDataSet[projectId].Where(d => (d.GDCOFaultCode.Equals("124110"))).FirstOrDefault();
                            }
                            if (updateAssetTask != null && updateAssetTask.TicketState.Equals("Created") && updateAssetTask.TicketId != null)
                            {
                                new GdcoTicketHandler().AssignParent(Convert.ToInt64(updateAssetTask.TicketId), gdcoTicket.Id);
                            }
                            else if (oaTask != null && oaTask.TicketId != null)
                            {
                                new GdcoTicketHandler().AssignParent(Convert.ToInt64(oaTask.TicketId), gdcoTicket.Id);
                            }
                        }
                    }
                    catch (Exception)
                    {
                    }
                    errorDescription = errorDescription.Length > 1000 ? errorDescription.Substring(0, 1000) : errorDescription;
                    failedDescriptions.Add(errorDescription);
                }
            }

            // Check for passed cases that have failed earlier and ticket is not updated. Update the tickets.
            var pids       = new List <string>();
            var failedPids = new List <string>();

            foreach (var result in results)
            {
                if (result.GroupType.IndexOf("PreRack", StringComparison.OrdinalIgnoreCase) >= 0)
                {
                    continue;
                }
                var failedExchangeResults = result.AllEgsOutput.Where(eo => eo.TestStatus.Equals("Failed") && !string.IsNullOrEmpty(eo.QcName));
                var pid = string.IsNullOrEmpty(result.ProjectId) ? result.MdmId : result.ProjectId;
                if (!failedExchangeResults.Any())
                {
                    pids.Add(pid);
                }
                else
                {
                    failedPids.Add(pid);
                }
            }
            if (pids.Any())
            {
                foreach (var pid in pids)
                {
                    gdcoTickets.AddRange(new GdcoTicketHandler().UpdateTicket(pid, "msassetsku"));
                }
            }
            if (failedPids.Any())
            {
                foreach (var pid in failedPids)
                {
                    gdcoTickets.AddRange(new GdcoTicketHandler().UpdateFailedTicket(pid, "msassetsku", failedDescriptions));
                }
            }
            return(gdcoTickets);
        }
Exemple #5
0
        public void SyncDsDataSet()
        {
            List <DeploymentServiceStore> dsDataSet = new List <DeploymentServiceStore>();
            var errorSubject = "Error in syncing Deployment Service Data";
            var take         = 100;
            var skip         = 0;

            try
            {
                DSAccess          ds = new DSAccess();
                GdcoTicketHandler gt = new GdcoTicketHandler();
                List <string>     FulfillmentIdList = new List <string>();
                dsDataSet = ds.GetDeploymentServiceStoreList();

                foreach (DeploymentServiceStore dss in dsDataSet)
                {
                    FulfillmentIdList.Add(dss.fulfillmentId);
                }

                if (FulfillmentIdList.Any())
                {
                    Dictionary <string, List <TicketingObject> > ticketingDic = new Dictionary <string, List <TicketingObject> >();
                    // Add paging here and pass in 200 FulfillmentIds per time
                    int total = FulfillmentIdList.Count;
                    take = 200;
                    skip = 0;

                    while (skip < total)
                    {
                        var CurrentFulfillmentIdList = FulfillmentIdList.Skip(skip).Take(take).ToList();
                        Dictionary <string, List <TicketingObject> > tempDic = gt.GetTicketsByFulfillmentId(CurrentFulfillmentIdList);
                        foreach (var row in tempDic)
                        {
                            if (!ticketingDic.ContainsKey(row.Key))
                            {
                                ticketingDic.Add(row.Key, row.Value);
                            }
                        }
                        skip = skip + take;
                    }

                    // Put ticketingObjectList to related DeploymentServiceStore by fullfillmentId
                    foreach (var dss in dsDataSet)
                    {
                        if (ticketingDic.ContainsKey(dss.fulfillmentId))
                        {
                            dss.TicketList = ticketingDic[dss.fulfillmentId];
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                SendEmail.SendExoSkuMsAssetReportEmail(Constants.serviceAccount, Constants.automationTeam, errorSubject, ex.ToString());
            }

            if (dsDataSet == null || !dsDataSet.Any())
            {
                return;
            }

            // Before we write DS data into database, we filter out all of the DSData which has null ticketList
            List <DeploymentServiceStore> dsFinalList = new List <DeploymentServiceStore>();
            Dictionary <string, DeploymentServiceStore> FidDeploymentDic = new Dictionary <string, DeploymentServiceStore>();

            foreach (var dsData in dsDataSet)
            {
                if (dsData.TicketList != null && dsData.TicketList.Any())
                {
                    dsFinalList.Add(dsData);
                    if (!FidDeploymentDic.ContainsKey(dsData.fulfillmentId))
                    {
                        FidDeploymentDic.Add(dsData.fulfillmentId, dsData);
                    }
                }
            }

            // Get FulfillmentId to DemandId mapping
            Dictionary <string, string> CpsFidTitleDic = new Dictionary <string, string>();

            if (FidDeploymentDic.Any())
            {
                GDCOTicketStoreHandler      GDCOTicketStoreHandler = new GDCOTicketStoreHandler();
                Dictionary <string, string> FidToDemandIdMapping   = GDCOTicketStoreHandler.GetFidToDemandIdMapping(FidDeploymentDic.Keys.ToList());

                // Get CPS FidList, it contains all of the fids which are CPS
                CpsFidTitleDic = GDCOTicketStoreHandler.GetCpsFidTitleDic(FidToDemandIdMapping);
                bool sendEmail = false;

                if (CpsFidTitleDic != null && CpsFidTitleDic.Any())
                {
                    StringBuilder body = new StringBuilder();
                    body.AppendLine("<style> table {    font-family: arial, sans-serif;    border-collapse: collapse;    width: 100%;}td, th {    border: 1px solid #dddddd;    text-align: left;    padding: 8px;}tr:nth-child(even) {    background-color: #dddddd;} h3 {    font-family: arial, sans-serif; color:#2F4F4F;}</style>");
                    body.AppendLine("<p><h3>Please QC following CPS Fids: </h3></p>");
                    body.AppendLine("<table>");
                    body.AppendLine("<tr><td>FulfillmentId</td><td>Title</td><td>TicketCreatedDate</td><td>EngineerGroup</td><td>PropertyGroup/td></tr>");

                    foreach (var fid in CpsFidTitleDic.Keys)
                    {
                        var    DsData             = FidDeploymentDic[fid];
                        var    title              = CpsFidTitleDic[fid];
                        var    engineerGroup      = DsData.EngineeringGroup;
                        var    propertyGroup      = DsData.PGName;
                        string ticketCreationDate = "NA";
                        foreach (var ticket in DsData.TicketList)
                        {
                            // check if current fid has OA task, if yes, we should QC it
                            if (ticket.GDCOFaultCode.Equals("124110"))
                            {
                                ticketCreationDate = ticket.CreatedDate.ToString();
                                sendEmail          = true;
                                body.AppendLine("<tr><td>" + fid + "</td><td>" + title + "</td><td>" + ticketCreationDate + "</td><td>" + engineerGroup + "</td><td>" + propertyGroup + "</td></tr>");
                            }
                        }
                    }
                    body.AppendLine("</table>");

                    TimeSpan start = new TimeSpan(8, 0, 0);  //8 o'clock
                    TimeSpan end   = new TimeSpan(10, 0, 0); //10 o'clock
                    TimeSpan now   = DateTime.Now.TimeOfDay;

                    if (sendEmail && (now >= start) && (now <= end))
                    {
                        SendEmail.SendExoSkuMsAssetReportEmail(Constants.serviceAccountAlias, Constants.OADPMAccount, "CPS Fid List", body.ToString());
                    }
                }
            }

            take = 100;
            skip = 0;

            while (skip < dsFinalList.Count)
            {
                var currentdsDataSet = dsFinalList.Skip(skip).Take(take);
                using (var connection = new SqlConnection(ConnectionHandler.ConnectionString))
                {
                    try
                    {
                        var command = new SqlCommand("mcio_oa_db.prc_UpdateDsDataSet", connection)
                        {
                            CommandType = CommandType.StoredProcedure
                        };

                        var dsDataTable = new DataTable("mcio_oa_db.DsDataSetTableType");

                        dsDataTable.Columns.Add("FulfillmentId", typeof(string)).AllowDBNull           = true;
                        dsDataTable.Columns.Add("MDMIdList", typeof(string)).AllowDBNull               = true;
                        dsDataTable.Columns.Add("DeploymentId", typeof(string)).AllowDBNull            = true;
                        dsDataTable.Columns.Add("ResourceTypeList", typeof(string)).AllowDBNull        = true;
                        dsDataTable.Columns.Add("GroupType", typeof(string)).AllowDBNull               = true;
                        dsDataTable.Columns.Add("EngineeringGroup", typeof(string)).AllowDBNull        = true;
                        dsDataTable.Columns.Add("DeploymentDCCode", typeof(string)).AllowDBNull        = true;
                        dsDataTable.Columns.Add("DeploymentPGName", typeof(string)).AllowDBNull        = true;
                        dsDataTable.Columns.Add("PDimmension", typeof(string)).AllowDBNull             = true;
                        dsDataTable.Columns.Add("DeploymentClusterName", typeof(string)).AllowDBNull   = true;
                        dsDataTable.Columns.Add("DeploymentStatus", typeof(string)).AllowDBNull        = true;
                        dsDataTable.Columns.Add("DeploymentCreatedDate", typeof(DateTime)).AllowDBNull = true;
                        dsDataTable.Columns.Add("TicketId", typeof(string)).AllowDBNull             = true;
                        dsDataTable.Columns.Add("TicketState", typeof(string)).AllowDBNull          = true;
                        dsDataTable.Columns.Add("CurrentSeverity", typeof(string)).AllowDBNull      = true;
                        dsDataTable.Columns.Add("GDCOFaultCode", typeof(string)).AllowDBNull        = true;
                        dsDataTable.Columns.Add("GDCOFaultDescription", typeof(string)).AllowDBNull = true;
                        dsDataTable.Columns.Add("TicketTitle", typeof(string)).AllowDBNull          = true;
                        dsDataTable.Columns.Add("ClusterName", typeof(string)).AllowDBNull          = true;
                        dsDataTable.Columns.Add("TicketDCCode", typeof(string)).AllowDBNull         = true;
                        dsDataTable.Columns.Add("TicketPGName", typeof(string)).AllowDBNull         = true;
                        dsDataTable.Columns.Add("DeliveryNumber", typeof(string)).AllowDBNull       = true;
                        dsDataTable.Columns.Add("TemplateType", typeof(string)).AllowDBNull         = true;
                        dsDataTable.Columns.Add("TicketCreatedDate", typeof(DateTime)).AllowDBNull  = true;
                        dsDataTable.Columns.Add("TicketAssignedDate", typeof(DateTime)).AllowDBNull = true;
                        dsDataTable.Columns.Add("TicketResolvedDate", typeof(DateTime)).AllowDBNull = true;
                        dsDataTable.Columns.Add("TicketDueDate", typeof(DateTime)).AllowDBNull      = true;
                        dsDataTable.Columns.Add("WasSLABreached", typeof(string)).AllowDBNull       = true;
                        dsDataTable.Columns.Add("PurchaseOrderNumber", typeof(string)).AllowDBNull  = true;

                        foreach (var dsData in currentdsDataSet)
                        {
                            var FulfillmentId    = dsData.fulfillmentId != null ? dsData.fulfillmentId : "NA";
                            var MDMIdList        = dsData.MDMIDList != null ? dsData.MDMIDList : "NA";
                            var DeploymentId     = dsData.deploymentId != null ? dsData.deploymentId : "NA";
                            var resourceTypeList = dsData.ResourceTypeList != null ? dsData.ResourceTypeList : "NA";
                            var groupType        = dsData.GroupType != null ? dsData.GroupType : "NA";

                            var engineeringGroup = "NA";
                            var DeploymentPGName = "NA";
                            if (dsData != null && dsData.fulfillmentId != null && CpsFidTitleDic != null && CpsFidTitleDic.ContainsKey(dsData.fulfillmentId))
                            {
                                var title = CpsFidTitleDic[dsData.fulfillmentId];
                                if (title.IndexOf("CDDS", StringComparison.OrdinalIgnoreCase) >= 0)
                                {
                                    engineeringGroup = "CDDS";
                                    DeploymentPGName = "CDDS";

                                    if (title.IndexOf("FI", StringComparison.OrdinalIgnoreCase) >= 0)
                                    {
                                        engineeringGroup = "CDDS FI";
                                        DeploymentPGName = "CDDS FI";
                                    }
                                }
                                else if (title.IndexOf("IS ", StringComparison.OrdinalIgnoreCase) >= 0)
                                {
                                    engineeringGroup = "IS";
                                    DeploymentPGName = "IS";

                                    if (title.IndexOf("FI", StringComparison.OrdinalIgnoreCase) >= 0)
                                    {
                                        engineeringGroup = "IS FI";
                                        DeploymentPGName = "IS FI";
                                    }
                                }
                                else if (title.IndexOf("DPS", StringComparison.OrdinalIgnoreCase) >= 0)
                                {
                                    engineeringGroup = "DPS";
                                    DeploymentPGName = "DPS";
                                }
                                else if (title.IndexOf("ADNS", StringComparison.OrdinalIgnoreCase) >= 0)
                                {
                                    engineeringGroup = "ADNS";
                                    DeploymentPGName = "ADNS";
                                }
                            }
                            else
                            {
                                engineeringGroup = dsData.EngineeringGroup;
                                DeploymentPGName = dsData.PGName;
                            }

                            var DeploymentDCCode      = dsData.dcCode != null ? dsData.dcCode : "NA";
                            var PDimmension           = dsData.PDimmension;
                            var DeploymentClusterName = dsData.ClusterName != null ? dsData.ClusterName : "NA";
                            var DeploymentStatus      = dsData.Status != null ? dsData.Status : "NA";
                            var DeploymentCreatedDate = dsData.CreatedDate;
                            var PurchaseOrderNumber   = dsData.PurchaseOrderNumber != null ? dsData.PurchaseOrderNumber : "NA";

                            if (dsData.TicketList != null && dsData.TicketList.Any())
                            {
                                foreach (var dsTicket in dsData.TicketList)
                                {
                                    var TicketId                = dsTicket.TicketId;
                                    var TicketState             = dsTicket.StateName;
                                    var CurrentSeverity         = dsTicket.CurrentSeverity;
                                    var GDCOFaultCode           = dsTicket.GDCOFaultCode;
                                    var GDCOFaultDescription    = dsTicket.GDCOFaultDescription;
                                    var TicketTitle             = dsTicket.TicketTitle;
                                    var TicketClusterName       = dsTicket.ClusterName;
                                    var TicketDatacenterCode    = dsTicket.DatacenterCode;
                                    var TicketPropertyGroupName = dsTicket.PropertyGroupName;
                                    var DeliveryNumber          = dsTicket.DeliveryNumber;
                                    var TemplateType            = dsTicket.TemplateType;
                                    var TicketCreatedDate       = dsTicket.CreatedDate;
                                    var TicketAssignedDate      = dsTicket.AssignedDate;
                                    var TicketResolvedDate      = dsTicket.ResolvedDate;
                                    var TicketDueDate           = dsTicket.DueDate;
                                    var WasSLABreached          = dsTicket.WasSLABreached;

                                    dsDataTable.Rows.Add(
                                        FulfillmentId,
                                        MDMIdList,
                                        DeploymentId,
                                        resourceTypeList,
                                        groupType,
                                        engineeringGroup,
                                        DeploymentDCCode,
                                        DeploymentPGName,
                                        PDimmension,
                                        DeploymentClusterName,
                                        DeploymentStatus,
                                        DeploymentCreatedDate,
                                        TicketId,
                                        TicketState,
                                        CurrentSeverity,
                                        GDCOFaultCode,
                                        GDCOFaultDescription,
                                        TicketTitle,
                                        TicketClusterName,
                                        TicketDatacenterCode,
                                        TicketPropertyGroupName,
                                        DeliveryNumber,
                                        TemplateType,
                                        TicketCreatedDate,
                                        TicketAssignedDate,
                                        TicketResolvedDate,
                                        TicketDueDate,
                                        WasSLABreached,
                                        PurchaseOrderNumber
                                        );
                                }
                            }
                        }

                        command.Parameters.Add(new SqlParameter
                        {
                            ParameterName = "@DsDataSetTvp",
                            SqlDbType     = SqlDbType.Structured,
                            Value         = dsDataTable
                        });

                        connection.Open();
                        command.ExecuteScalar();
                        connection.Close();
                    }
                    catch (Exception ex)
                    {
                        SendEmail.SendExoSkuMsAssetReportEmail(Constants.serviceAccount, Constants.automationTeam, errorSubject, $"Take = {take} Skip = {skip} {ex.ToString()}");
                    }
                }
                skip = skip + take;
            }
        }
        private List <GdcoTicket.Model.GdcoTicket> CreateGdcoTickets(List <AllEgsOutputModel> results, string mode)
        {
            var gdcoTickets = new List <GdcoTicket.Model.GdcoTicket>();
            HashSet <String> failedDescriptions = new HashSet <String>();
            KustoAccess      ka = new KustoAccess();

            if (results == null || !results.Any())
            {
                return(gdcoTickets);
            }

            // Check for failed tests, create tickets
            foreach (var result in results)
            {
                var errorTypeToAllEgsResults = new Dictionary <string, List <AllEngineeringGroupsResult> >();
                var allEgsResults            = result.AllEgsOutput.Where(eo => eo.TestStatus.Equals("Failed") && !string.IsNullOrEmpty(eo.QcName));
                var mdmId = string.IsNullOrEmpty(result.ProjectId) ? result.MdmId : result.ProjectId;
                // compare the current ticket with CIS Kusto feed, if different, cut the ticket
                var kustoErrMsg = "";

                if (allEgsResults != null && allEgsResults.Any())
                {
                    kustoErrMsg = ka.GetErrorEsgByMdmid(mdmId);
                }

                foreach (var allEgResult in allEgsResults)
                {
                    if (!errorTypeToAllEgsResults.ContainsKey(allEgResult.QcName))
                    {
                        errorTypeToAllEgsResults[allEgResult.QcName] = new List <AllEngineeringGroupsResult> {
                            allEgResult
                        };
                    }
                    else
                    {
                        var exchangeResultList = errorTypeToAllEgsResults[allEgResult.QcName];
                        exchangeResultList.Add(allEgResult);
                    }
                }

                if (!errorTypeToAllEgsResults.Any())
                {
                    continue;
                }

                foreach (var key in errorTypeToAllEgsResults.Keys)
                {
                    var    allEgsErrorList  = errorTypeToAllEgsResults[key];
                    var    projectId        = string.IsNullOrEmpty(result.ProjectId) ? result.MdmId : result.ProjectId;
                    var    currentWorkOrder = allEgsErrorList.First().WorkOrderName.Trim();
                    var    errorCode        = key;
                    string errorTitle       = null;
                    var    errorDescription = GetAllEgsResultError(allEgsErrorList);
                    var    dcCode           = result.DataCenterCode.Trim();
                    var    requestOwner     = "oadpm";
                    string severity         = null;
                    string parentTicketId   = null;

                    var gdcoTicket = new GdcoTicket.Model.GdcoTicket();
                    try
                    {
                        if (String.IsNullOrEmpty(kustoErrMsg))
                        {
                            // send email if there is something diff between us and their Kusto feed
                            EmailBody.AppendLine("<tr><td>" + projectId + "</td><td>" + errorTitle + "</td><td>" + errorDescription + "</td></tr>");
                        }


                        if (result.GroupType.IndexOf("PreRack", StringComparison.OrdinalIgnoreCase) >= 0)
                        {
                            continue;
                        }

                        // Leave cutting ticket part here and will move into above if later
                        if (ErrorCodeMapping.LiveErrorChanges.Contains(errorCode))
                        {
                            var internalErrorCode = mode.Equals("P") ? ErrorCodeMapping.InternalGfsdErrorToInternalFc[key] : ErrorCodeMapping.InternalCisErrorToInternalFc[key];
                            errorTitle       = ErrorCodeMapping.InternalFcToGdcoTicketTitle.Keys.Contains(internalErrorCode) ? ErrorCodeMapping.InternalFcToGdcoTicketTitle[internalErrorCode] : "";
                            errorDescription = GetAllEgsNewResultError(allEgsErrorList, internalErrorCode);
                            gdcoTicket       = new GdcoTicketHandler().CreateErrorTicket(projectId, currentWorkOrder,
                                                                                         internalErrorCode, errorTitle, errorDescription, dcCode, requestOwner, severity, parentTicketId, mode, "allegs_msassetsku");
                        }
                        else
                        {
                            gdcoTicket = new GdcoTicketHandler().CreateTicket(projectId, currentWorkOrder,
                                                                              errorCode, errorTitle, errorDescription, dcCode, requestOwner, severity, parentTicketId, mode, "allegs_msassetsku");
                        }
                    }
                    catch (Exception ex)
                    {
                        SendEmail.SendExoSkuMsAssetReportEmail(Constants.serviceAccountAlias, Constants.automationTeam, "Exception in CreateTicket(), Ticket Creation failed for ", "ProjectId: " + projectId + " Exception in CreateTicket(): " + ex);
                        continue;
                    }
                    errorDescription = errorDescription.Length > 1000 ? errorDescription.Substring(0, 1000) : errorDescription;
                    failedDescriptions.Add(errorDescription);
                    gdcoTickets.Add(gdcoTicket);
                    try
                    {
                        if (mode.Equals("P"))
                        {
                            new GdcoTicketHandler().AssignParent(projectId, gdcoTicket.Id);
                        }
                        else if (mode.Equals("M"))
                        {
                            var cisProject = cisProjectModel.CisProjects.Where(c => c.MdmId.Equals(projectId)).FirstOrDefault();
                            if (cisProject != null && cisProject.UpdateAssetDataTicketId != null)
                            {
                                new GdcoTicketHandler().AssignParent(Convert.ToInt64(cisProject.UpdateAssetDataTicketId), gdcoTicket.Id);
                            }
                        }
                        else if (mode.Equals("D"))
                        {
                            var       dsDataSet       = new DSDataSetAccess().GetInProgressAllEGsDeploymentsToRTEG();
                            DSDataSet updateAssetTask = null;
                            DSDataSet oaTask          = null;
                            if (dsDataSet.ContainsKey(projectId))
                            {
                                updateAssetTask = dsDataSet[projectId].Where(d => (d.GDCOFaultCode.Equals("124107") || d.GDCOFaultCode.Equals("124246") || d.GDCOFaultCode.Equals("124054"))).FirstOrDefault();
                                oaTask          = dsDataSet[projectId].Where(d => (d.GDCOFaultCode.Equals("124110"))).FirstOrDefault();
                            }
                            if (updateAssetTask != null && updateAssetTask.TicketState.Equals("Created") && updateAssetTask.TicketId != null)
                            {
                                new GdcoTicketHandler().AssignParent(Convert.ToInt64(updateAssetTask.TicketId), gdcoTicket.Id);
                            }
                            else if (oaTask != null && oaTask.TicketId != null)
                            {
                                new GdcoTicketHandler().AssignParent(Convert.ToInt64(oaTask.TicketId), gdcoTicket.Id);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        SendEmail.SendExoSkuMsAssetReportEmail(Constants.serviceAccountAlias, Constants.automationTeam, "Exception in CreateGdcoTickets()", "ProjectId: " + projectId + " Exception in CreateGdcoTickets(): " + ex);
                        continue;
                    }
                }
            }

            // Check for passed cases that have failed earlier and ticket is not updated. Update the tickets.
            var pids       = new List <string>();
            var failedPids = new List <string>();

            foreach (var result in results)
            {
                if (result.GroupType.IndexOf("PreRack", StringComparison.OrdinalIgnoreCase) >= 0)
                {
                    continue;
                }
                var failedAllEgsResults = result.AllEgsOutput.Where(eo => eo.TestStatus.Equals("Failed") && !string.IsNullOrEmpty(eo.QcName));
                var pid = string.IsNullOrEmpty(result.ProjectId) ? result.MdmId : result.ProjectId;

                if (!failedAllEgsResults.Any())
                {
                    pids.Add(pid);
                }
                else
                {
                    failedPids.Add(pid);
                }
            }
            if (pids.Any())
            {
                foreach (var pid in pids)
                {
                    gdcoTickets.AddRange(new GdcoTicketHandler().UpdateTicket(pid, "allegs_msassetsku"));
                }
            }
            if (failedPids.Any())
            {
                foreach (var pid in failedPids)
                {
                    gdcoTickets.AddRange(new GdcoTicketHandler().UpdateFailedTicket(pid, "allegs_msassetsku", failedDescriptions));
                }
            }
            return(gdcoTickets);
        }