Пример #1
0
        public IActionResult Get(string id)
        {
            var service = serviceQuery.Get(id);

            if (service == null)
            {
                return(new StatusCodeResult(StatusCodes.Status404NotFound));
            }
            if (service.Type != (int)ServiceTypeEnum.Classifier)
            {
                return(new HttpStatusCodeWithErrorResult(StatusCodes.Status400BadRequest, string.Format(ServiceResources.InvalidServiceTypeOnly_0_ServicesAreValidForThisRequest, "Classifier")));
            }

            ClassifierActivateSettings activateSettings = null;
            ClassifierPrepareSettings  prepareSettings  = null;

            var classifierSettingsElastic = serviceQuery.GetSettings <ClassifierSettingsElastic>(service.Id);

            if (classifierSettingsElastic != null)
            {
                if (service.Status == (int)ServiceStatusEnum.Prepared || service.Status == (int)ServiceStatusEnum.Active)
                {
                    prepareSettings = new ClassifierPrepareSettings {
                        DataSetName      = GlobalStore.DataSets.Get(classifierSettingsElastic.DataSetName).AliasName,
                        NGramList        = classifierSettingsElastic.NGramList,
                        TagIdList        = classifierSettingsElastic.Tags.Select(t => t.Id).ToList(),
                        CompressSettings = CompressHelper.ToCompressSettings(classifierSettingsElastic.CompressSettings),
                        CompressLevel    = CompressHelper.ToCompressLevel(classifierSettingsElastic.CompressSettings)
                    };
                    if (service.Status == (int)ServiceStatusEnum.Active)
                    {
                        activateSettings = new ClassifierActivateSettings
                        {
                            TagIdList           = classifierSettingsElastic.ActivatedTagIdList,
                            EmphasizedTagIdList = classifierSettingsElastic.EmphasizedTagIdList,
                            NGramList           = classifierSettingsElastic.ActivatedNGramList
                        };
                    }
                }
            }

            var respService = service.ToServiceModel <ClassifierService>();

            respService.ActualProcessId  = service.ProcessIdList.FirstOrDefault(pid => GlobalStore.Processes.IsExist(pid));
            respService.ActivateSettings = activateSettings;
            respService.PrepareSettings  = prepareSettings;

            return(new OkObjectResult(respService));
        }
Пример #2
0
        public IActionResult Activate(string id, [FromBody] ClassifierActivateSettings classifierActivateSettings)
        {
            //SERVICE VALIDATION
            var service = serviceQuery.Get(id);

            if (service == null)
            {
                return(new HttpStatusCodeWithErrorResult(StatusCodes.Status404NotFound, ServiceResources.InvalidIdNotExistingService));
            }
            if (service.Type != (int)ServiceTypeEnum.Classifier)
            {
                return(new HttpStatusCodeWithErrorResult(StatusCodes.Status400BadRequest, string.Format(ServiceResources.InvalidServiceTypeOnly_0_ServicesAreValidForThisRequest, "Classifier")));
            }
            if (service.Status != (int)ServiceStatusEnum.Prepared)
            {
                return(new HttpStatusCodeWithErrorResult(StatusCodes.Status400BadRequest, ServiceResources.InvalidStatusOnlyTheServicesWithPreparedStatusCanBeActivated));
            }
            var classifierSettings = serviceQuery.GetSettings <ClassifierSettingsElastic>(service.Id);

            service.Status = (int)ServiceStatusEnum.Active;

            if (classifierActivateSettings == null)
            {
                if (classifierSettings.ActivatedTagIdList == null ||
                    classifierActivateSettings.NGramList == null)
                {
                    return(new HttpStatusCodeWithErrorResult(StatusCodes.Status400BadRequest, ServiceResources.EmptyActivateSettings));
                }
            }
            else
            {
                //NGRAM LIST VALIDATION
                if (classifierActivateSettings.NGramList.Except(classifierSettings.NGramList).Count() > 0)
                {
                    return(new HttpStatusCodeWithErrorResult(StatusCodes.Status400BadRequest, ServiceResources.NGramCountWasntPrepared));
                }
                //TAGS VALIDATION
                var           preparedTagIds = classifierSettings.Tags.Select(t => t.Id).ToList();
                List <string> tagIds;
                if (classifierActivateSettings?.TagIdList?.Any() == true)
                {
                    tagIds = preparedTagIds.Intersect(classifierActivateSettings.TagIdList).ToList();
                    if (tagIds.Count < classifierActivateSettings.TagIdList.Count)
                    {
                        var missingTagIds = classifierActivateSettings.TagIdList.Except(preparedTagIds).ToList();
                        return(new HttpStatusCodeWithErrorResult(StatusCodes.Status400BadRequest,
                                                                 string.Format(ServiceResources.TheFollowingTagIdsWereNotPrepared_0, string.Join(", ", missingTagIds))));
                    }
                }
                else
                {
                    tagIds = preparedTagIds;
                }

                var emphasizedTagIds = new List <string>();
                if (classifierActivateSettings?.EmphasizedTagIdList?.Any() == true)
                {
                    emphasizedTagIds = preparedTagIds.Intersect(classifierActivateSettings.EmphasizedTagIdList).ToList();
                    if (emphasizedTagIds.Count < classifierActivateSettings.EmphasizedTagIdList.Count)
                    {
                        var missingTagIds = classifierActivateSettings.EmphasizedTagIdList.Except(preparedTagIds).ToList();
                        return(new HttpStatusCodeWithErrorResult(StatusCodes.Status400BadRequest,
                                                                 string.Format(ServiceResources.TheFollowingEmphasizedTagIdsWereNotPrepared_0, string.Join(", ", missingTagIds))));
                    }
                }

                classifierSettings.ActivatedNGramList  = classifierActivateSettings.NGramList;
                classifierSettings.ActivatedTagIdList  = tagIds;
                classifierSettings.EmphasizedTagIdList = emphasizedTagIds;
            }

            var process = processHandler.Create(
                ProcessTypeEnum.ClassifierActivate,
                service.Id,
                classifierSettings,
                string.Format(ServiceResources.Activating_0_Service_1, ServiceTypeEnum.Classifier, service.Name));

            service.ProcessIdList.Add(process.Id);
            serviceQuery.Update(service.Id, service);
            serviceQuery.IndexSettings(classifierSettings);

            processHandler.Start(process, (tokenSource) => classifierHandler.Activate(process.Id, classifierSettings, tokenSource.Token));

            return(new HttpStatusCodeWithObjectResult(StatusCodes.Status202Accepted, process.ToProcessModel()));
        }
 public async Task <ClientResponseWithObject <Process> > ActivateServiceAsync(string serviceId, ClassifierActivateSettings classifierActivateSettings)
 {
     return(await _client.SendAsync <Process>(System.Net.Http.HttpMethod.Post, classifierActivateSettings, $"{serviceId}/{ActivateEndpointSuffix}", null, null));
 }