Пример #1
0
 public IList <MeasureTaskInfo> List()
 {
     using var dbContext = new GbmDbContext();
     return(dbContext.Tasks.Select(x => new MeasureTaskInfo
     {
         Id = new Guid(x.Id),
         CreatedAt = x.CreatedAt,
         Status = (MeasureTaskStatus)x.Status,
         OutputMetrics = x.OutputMetrics
     }).OrderBy(x => x.CreatedAt).ToList());
 }
Пример #2
0
        public IActionResult GetOutputImage(Guid id)
        {
            using var dbContext = new GbmDbContext();

            var task = dbContext.Tasks.SingleOrDefault(x => x.Id == id.ToString().ToLower());

            if (task == null)
            {
                return(NotFound());
            }

            if (task.Status != (int)MeasureTaskStatus.Finished)
            {
                return(BadRequest("Task is not finished"));
            }

            var stream = new FileInfo(task.OutputImageFilePath).OpenRead();

            return(File(stream, task.OutputImageMime));
        }
Пример #3
0
        public Task Execute(IJobExecutionContext context)
        {
            Console.WriteLine("Executing MeasureImageJob.");

            using var dbContext = new GbmDbContext();

            foreach (var task in dbContext.Tasks.Where(t => t.Status == (int)MeasureTaskStatus.Segmented))
            {
                var args =
                    $"{ApplicationContext.Current.Configuration["PythonRepository"]}/measure.py {ApplicationContext.Current.Configuration["PythonRepository"]}/settings.json {task.OutputImageFilePath} {ApplicationContext.Current.Configuration["OutputMetricsDirectory"]}/{task.Id}.txt";

                Console.WriteLine($"Processing task {task.Id}");
                Console.WriteLine($"{ApplicationContext.Current.Configuration["PythonRuntime"]} {args}");

                ProcessStartInfo start = new ProcessStartInfo
                {
                    FileName               = ApplicationContext.Current.Configuration["PythonRuntime"],
                    Arguments              = args,
                    UseShellExecute        = false,
                    RedirectStandardOutput = true
                };
                using (Process process = Process.Start(start))
                {
                    using (StreamReader reader = process.StandardOutput)
                    {
                        string result = reader.ReadToEnd();
                        Console.WriteLine(result);
                    }
                }

                using (TextReader tr = new StreamReader($"{ApplicationContext.Current.Configuration["OutputMetricsDirectory"]}/{task.Id}.txt"))
                    task.OutputMetrics = tr.ReadToEnd();

                dbContext.Tasks.Single(t => t.Id == task.Id).Status = (int)MeasureTaskStatus.Finished;
                dbContext.SaveChanges();
            }

            Console.WriteLine("MeasureImageJob finished.");

            return(Task.CompletedTask);
        }
Пример #4
0
        public IActionResult Delete([FromQuery] string redirect_uri, Guid id)
        {
            using var dbContext = new GbmDbContext();

            var task = dbContext.Tasks.SingleOrDefault(x => x.Id == id.ToString().ToLower());

            if (task == null)
            {
                return(NotFound());
            }

            dbContext.Tasks.Remove(task);
            dbContext.SaveChanges();

            if (string.IsNullOrEmpty(redirect_uri))
            {
                return(Ok());
            }

            return(Redirect(redirect_uri));
        }
Пример #5
0
        public async Task <IActionResult> CreateAsync([FromQuery] string redirect_uri, IFormFile file)
        {
            using var dbContext = new GbmDbContext();

            if (file == null)
            {
                return(BadRequest("Requires input image file"));
            }

            var id = Guid.NewGuid().ToString();

            var filePath = $"{ApplicationContext.Current.Configuration["InputImagesDirectory"]}/{id}.jpg";

            using (var stream = System.IO.File.Create(filePath))
            {
                await file.CopyToAsync(stream);
            }

            dbContext.Tasks.Add(new MeasureTask
            {
                Id                  = id.ToLower(),
                CreatedAt           = DateTime.Now,
                InputImageMime      = file.ContentType,
                InputImageFilePath  = filePath,
                Status              = (int)MeasureTaskStatus.Created,
                OutputImageFilePath = $"{ApplicationContext.Current.Configuration["OutputImagesDirectory"]}/{id}.png",
                OutputImageMime     = "image/png",
            });

            dbContext.SaveChanges();

            if (string.IsNullOrEmpty(redirect_uri))
            {
                return(Ok());
            }

            return(Redirect(redirect_uri));
        }