コード例 #1
0
        public async Task <ActionResult <ModelTable> > PostModel([FromBody] ModelTable model, string option = "save")
        {
            option = option.Replace("\"", "");
            if (!_modelOptions.Contains(option))
            {
                return(BadRequest("Invalid option: valid values are: \"save\", \"saveandrun\", \"saveandtrain\""));
            }
            //Check if Tenant and User Id exists
            if (await _context.Users.FindAsync(model.UserId) == null)
            {
                return(NotFound());
            }

            //Create new Model object
            ModelTable newModel = new ModelTable
            {
                ModelName     = model.ModelName,
                Configuration = model.Configuration,
                ModelId       = new Guid(),
                File          = null,
                Created       = DateTime.Now,
                LastUpdated   = DateTime.Now,
                UserId        = model.UserId
            };

            _context.Models.Add(newModel);
            await _context.SaveChangesAsync();

            //Run the model if the option is selected
            if (option.Equals("saveandrun"))
            {
                try
                {
                    Task.Factory.StartNew((() =>
                    {
                        backgroundTaskQueue.QueueModelRunWorkItem(model.ModelId);
                    }));
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                }
            }
            //Train the model if the option is selected
            if (option.Equals("saveandtrain"))
            {
                try
                {
                    Task.Factory.StartNew((() =>
                    {
                        backgroundTaskQueue.QueueModelUpdateWorkItem(model.ModelId);
                    }));
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                }
            }

            return(CreatedAtAction("GetModel", new { id = newModel.ModelId }, newModel));
        }