コード例 #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 Prepare(string id, [FromBody] ClassifierPrepareSettings classifierPrepareSettings)
        {
            //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.New)
            {
                return(new HttpStatusCodeWithErrorResult(StatusCodes.Status400BadRequest, ServiceResources.InvalidStatusOnlyTheServicesWithNewStatusCanBePrepared));
            }

            //DATASET VALIDATION
            if (!GlobalStore.DataSets.IsExist(classifierPrepareSettings.DataSetName))
            {
                return(new HttpStatusCodeWithErrorResult(StatusCodes.Status400BadRequest,
                                                         string.Format(ServiceResources.DataSet_0_NotFound, classifierPrepareSettings.DataSetName)));
            }

            var globalStoreDataSet = GlobalStore.DataSets.Get(classifierPrepareSettings.DataSetName);
            var dataSet            = globalStoreDataSet.DataSet;

            //NGRAM COUNT LIST VALIDATION
            var nGramResult = CommonValidators.ValidateNGramList(classifierPrepareSettings.NGramList, dataSet.NGramCount);

            if (nGramResult.IsFailure)
            {
                return(HttpErrorResult(StatusCodes.Status400BadRequest, nGramResult.Error));
            }

            //TAGS VALIDATION
            var tagQuery = queryFactory.GetTagQuery(dataSet.Name);
            List <TagElastic> tags;

            if (classifierPrepareSettings?.TagIdList?.Any() == true)
            {
                tags = tagQuery.Get(classifierPrepareSettings.TagIdList).ToList();
                if (tags.Count < classifierPrepareSettings.TagIdList.Count)
                {
                    var missingTagIds = classifierPrepareSettings.TagIdList.Except(tags.Select(t => t.Id)).ToList();
                    return(new HttpStatusCodeWithErrorResult(StatusCodes.Status400BadRequest,
                                                             string.Format(ServiceResources.TheFollowingTagIdsNotExistInTheDataSet_0, string.Join(", ", missingTagIds))));
                }
            }
            else
            {
                tags = tagQuery.GetAll().Items.Where(i => i.IsLeaf).ToList();
            }

            //SAVE SETTINGS TO ELASTIC
            var serviceSettings = new ClassifierSettingsElastic {
                DataSetName      = globalStoreDataSet.IndexName,
                ServiceId        = service.Id,
                NGramList        = classifierPrepareSettings.NGramList,
                Tags             = tags,
                CompressSettings = CompressHelper.ToCompressSettingsElastic(classifierPrepareSettings.CompressSettings, classifierPrepareSettings.CompressLevel)
            };

            serviceQuery.IndexSettings(serviceSettings);

            var process = processHandler.Create(
                ProcessTypeEnum.ClassifierPrepare,
                service.Id, classifierPrepareSettings,
                string.Format(ServiceResources.Preparing_0_Service_1, ServiceTypeEnum.Classifier, service.Name));

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

            processHandler.Start(process, (tokenSource) => classifierHandler.Prepare(process.Id, serviceSettings, tokenSource.Token));

            return(new HttpStatusCodeWithObjectResult(StatusCodes.Status202Accepted, process.ToProcessModel()));
        }
コード例 #3
0
 public async Task <ClientResponseWithObject <Process> > PrepareServiceAsync(string serviceId, ClassifierPrepareSettings classifierPrepareSettings)
 {
     return(await _client.SendAsync <Process>(System.Net.Http.HttpMethod.Post, classifierPrepareSettings, $"{serviceId}/{PrepareEndpointSuffix}", null, null));
 }