コード例 #1
0
        public bool RestoreOle(bool overwrite)
        {
            var jsonFile = Context.Server.MapPath("~/Areas/SitecoreCognitiveServices/Assets/json/SitecoreCognitiveServices.Feature.OleChat.json");

            if (!File.Exists(jsonFile))
            {
                return(false);
            }

            var jsonText      = File.ReadAllText(jsonFile);
            var appDefinition = JsonConvert.DeserializeObject <ApplicationDefinition>(jsonText);

            var  infoResponse    = LuisService.GetApplicationInfo(OleSettings.OleApplicationId);
            bool shouldOverwrite = infoResponse != null && overwrite;
            bool isNoApp         = infoResponse == null;

            if (shouldOverwrite)
            {
                LuisService.DeleteApplication(new Guid(infoResponse.Id));
            }

            Guid appId;

            if (shouldOverwrite || isNoApp)
            {
                var importResponse = LuisService.ImportApplication(appDefinition, appDefinition.Name);
                if (!Guid.TryParse(importResponse, out appId))
                {
                    return(false);
                }

                OleSettings.OleApplicationId = Guid.Parse(importResponse);
            }
            else
            {
                appId = OleSettings.OleApplicationId;
            }

            LuisService.TrainApplicationVersion(appId, appDefinition.VersionId);
            int trainCount  = 1;
            int loopCount   = 0;
            var hasResponse = false;

            do
            {
                System.Threading.Thread.Sleep(1000);

                var trainResponse = LuisService.GetApplicationVersionTrainingStatus(appId, appDefinition.VersionId);
                var statusList    = trainResponse.Select(a => a.Details.Status).ToList();
                var anyFailed     = statusList.Any(a => a.Equals("Fail"));
                var anyInProgress = statusList.Any(b => b.Equals("InProgress"));
                if (anyFailed)
                {
                    if (trainCount > 3)
                    {
                        return(false);
                    }

                    LuisService.TrainApplicationVersion(appId, appDefinition.VersionId);
                    trainCount++;
                }
                else if (!anyInProgress)
                {
                    hasResponse = true;
                }

                if (loopCount > 100)
                {
                    return(false);
                }

                loopCount++;
            }while (!hasResponse);

            PublishRequest pr = new PublishRequest()
            {
                VersionId      = appDefinition.VersionId,
                IsStaging      = false,
                EndpointRegion = OleSettings.LuisPublishResource
            };
            var publishResponse = LuisService.PublishApplication(appId, pr);

            return(true);
        }
コード例 #2
0
        public bool RestoreOle(bool overwrite)
        {
            var jsonText      = OleSettings.ApplicationBackup;
            var appDefinition = JsonConvert.DeserializeObject <ApplicationDefinition>(jsonText);

            var  apps            = LuisService.GetUserApplications();
            var  infoResponse    = (apps != null && apps.Any()) ?  apps.FirstOrDefault(a => a.Name.Equals(appDefinition.Name)) : null;
            bool shouldOverwrite = infoResponse != null && overwrite;
            bool isNoApp         = infoResponse == null;
            bool hasAppId        = !string.IsNullOrWhiteSpace(infoResponse?.Id);

            if (shouldOverwrite)
            {
                LuisService.DeleteApplication(new Guid(infoResponse.Id));
            }

            Guid appId;

            if (shouldOverwrite || isNoApp)
            {
                var importResponse = LuisService.ImportApplication(appDefinition, appDefinition.Name);
                if (!Guid.TryParse(importResponse, out appId))
                {
                    return(false);
                }

                OleSettings.OleApplicationId = appId;
            }
            else if (OleSettings.OleApplicationId == Guid.Empty && hasAppId)
            {
                appId = Guid.Parse(infoResponse.Id);
                OleSettings.OleApplicationId = appId;
            }
            else
            {
                appId = OleSettings.OleApplicationId;
            }

            LuisService.TrainApplicationVersion(appId, appDefinition.VersionId);
            int trainCount  = 1;
            int loopCount   = 0;
            var hasResponse = false;

            do
            {
                System.Threading.Thread.Sleep(1000);

                var trainResponse = LuisService.GetApplicationVersionTrainingStatus(appId, appDefinition.VersionId);
                var statusList    = trainResponse.Select(a => a.Details.Status).ToList();
                var anyFailed     = statusList.Any(a => a.Equals("Fail"));
                var anyInProgress = statusList.Any(b => b.Equals("InProgress"));
                if (anyFailed)
                {
                    if (trainCount > 3)
                    {
                        return(false);
                    }

                    LuisService.TrainApplicationVersion(appId, appDefinition.VersionId);
                    trainCount++;
                }
                else if (!anyInProgress)
                {
                    hasResponse = true;
                }

                if (loopCount > 100)
                {
                    return(false);
                }

                loopCount++;
            }while (!hasResponse);

            PublishRequest pr = new PublishRequest()
            {
                VersionId      = appDefinition.VersionId,
                IsStaging      = false,
                EndpointRegion = OleSettings.LuisPublishResource
            };
            var publishResponse = LuisService.PublishApplication(appId, pr);

            return(true);
        }