Exemplo n.º 1
0
        public Task Consume(ConsumeContext <PredictedResultReady> context)
        {
            var prediction      = _keyValueRepository.LoadObject <dynamic>(context.Message.Id);
            var softwareInfo    = $@"{{
                              'software': '{_sspSettings.Software}',
                              'version': '{Assembly.GetEntryAssembly().GetVersion()}'
                        }}";
            var softwareInfoObj = JsonConvert.DeserializeObject <dynamic>(softwareInfo);
            var response        = context.Message.Data;

            response.provider   = softwareInfoObj;
            prediction.response = response;

            prediction.status = "COMPLETE";
            _keyValueRepository.SaveObject(context.Message.Id, prediction);
            _keyValueRepository.SetExpiration(context.Message.Id, TimeSpan.Parse(_sspSettings.RedisExpirationTime));
            return(Task.CompletedTask);
        }
Exemplo n.º 2
0
        public void ProcessImage(SourceFileUploadedMessage message)
        {
            string extension = Path.GetExtension(message.SourceFileName);

            if (!FileRasterizer.Supports(extension))
            {
                var error = $"Unsupported file type {extension}";

                new ProcessingInfo
                {
                    FileDescriptorId = message.ImageDescriptorId,
                    ProcessingError  = error
                }.SaveTo(_repository);

                Log.Information(error);
                return;
            }

            var format        = _options.ImageFormat.ParseImageFormat();
            var imageBlobId   = Guid.NewGuid().Encode();
            var imageFileName = $"{imageBlobId}.{format}";
            var rasterizer    = new FileRasterizer();

            var sourceTempFilePath = Path.Combine(Environment.CurrentDirectory, Guid.NewGuid().Encode());

            try
            {
                using (var sourceStream = File.Create(sourceTempFilePath))
                {
                    Log.Information($"Loading source {message.SourceDescriptorId}");
                    _repository.LoadStream(message.SourceBlobId, sourceStream);

                    Log.Information($"Rasterizing source {message.SourceDescriptorId}");
                    sourceStream.Position = 0;
                    var image = rasterizer.Rasterize(sourceStream, extension);
                    image = image.Scale(message.ImageSize.Width, message.ImageSize.Height);

                    Log.Information($"Saving image file {message.ImageDescriptorId} as {format}");

                    using (var imageStream = new MemoryStream())
                    {
                        var imageData = image.Convert(format);
                        imageStream.Write(imageData, 0, imageData.Length);

                        _repository.SaveStream(imageBlobId, imageStream);

                        new FileDescriptor
                        {
                            DescriptorId = message.ImageDescriptorId,
                            FileName     = imageFileName,
                            BlobId       = imageBlobId
                        }.SaveTo(_repository);

                        _repository.SetExpiration(message.ImageDescriptorId, message.SourceExpiry);
                        _repository.SetStreamExpiration(imageBlobId, message.SourceExpiry);

                        _repository.Delete(message.ProcessingInfoId);
                    }

                    Log.Information($"Image {message.ImageDescriptorId} processed");
                }
            }
            catch (Exception ex)
            {
                var error = $"Processing exception for source {message.SourceDescriptorId}: {ex.ToString()}";

                new ProcessingInfo
                {
                    FileDescriptorId = message.ImageDescriptorId,
                    ProcessingError  = error
                }.SaveTo(_repository);

                Log.Information(error);
            }
            finally
            {
                if (File.Exists(sourceTempFilePath))
                {
                    File.Delete(sourceTempFilePath);
                }
            }
        }
Exemplo n.º 3
0
        private UploadedFileInfo SaveFile(MultipartFileData file)
        {
            var fileName = file.Headers.ContentDisposition.FileName ?? file.Headers.ContentDisposition.Name;

            fileName = fileName?.Trim('"');
            var fileInfo = new UploadedFileInfo {
                FileName = fileName
            };

            var sourceDescriptorId = Guid.NewGuid().Encode();
            var imageDescriptorId  = Guid.NewGuid().Encode();
            var processingInfoId   = Guid.NewGuid().Encode();

            try
            {
                Log.Information($"Uploading file {sourceDescriptorId}");
                var sourceBlobId = UploadFileData(file.LocalFileName);
                var fileExpiry   = GetFileExpiry(file);

                // save source descriptor
                Log.Information($"Saving source desctiptor {sourceDescriptorId}");
                new FileDescriptor
                {
                    DescriptorId = sourceDescriptorId,
                    FileName     = fileName,
                    BlobId       = sourceBlobId,
                }.SaveTo(_repository);
                _repository.SetExpiration(sourceDescriptorId, fileExpiry);
                _repository.SetStreamExpiration(sourceBlobId, fileExpiry);

                // save image descriptor
                Log.Information($"Saving image desctiptor {imageDescriptorId}");
                new FileDescriptor
                {
                    DescriptorId     = imageDescriptorId,
                    ProcessingInfoId = processingInfoId,
                }.SaveTo(_repository);
                _repository.SetExpiration(imageDescriptorId, fileExpiry);

                // save processing info
                Log.Information($"Saving processing info {processingInfoId}");
                new ProcessingInfo()
                {
                    ProcessingInfoId = processingInfoId,
                    FileDescriptorId = imageDescriptorId,
                }.SaveTo(_repository);
                _repository.SetExpiration(processingInfoId, fileExpiry);

                // publish message for processing
                Log.Information($"Publishing processing message for source {sourceDescriptorId}");
                _bus.Publish(new SourceFileUploadedMessage
                {
                    SourceDescriptorId = sourceDescriptorId,
                    SourceBlobId       = sourceBlobId,
                    SourceFileName     = fileName,
                    ImageDescriptorId  = imageDescriptorId,
                    ProcessingInfoId   = processingInfoId,
                    SourceExpiry       = fileExpiry,
                    ImageSize          = GetImageSize(file)
                });

                fileInfo.SourceId = sourceDescriptorId;
                fileInfo.ImageId  = imageDescriptorId;
            }
            catch (Exception ex)
            {
                Log.Warning(ex, $"Saving file {sourceDescriptorId} with error.");
                fileInfo.Error = ex.Message;
            }

            File.Delete(file.LocalFileName);
            return(fileInfo);
        }
Exemplo n.º 4
0
        public async Task <IActionResult> CreateSingleStructurePrediction([FromBody] RunSingleStructurePrediction request)
        {
            Guid predictionId  = NewId.NextGuid();
            Guid correlationId = NewId.NextGuid();

            if (!(new string[] { "MOL", "SMILES" }.Contains(request.Format)))
            {
                return(BadRequest("Provided structure representation type not supported."));
            }

            var document = new Dictionary <string, object>();

            document.Add("predictionId", predictionId);
            document.Add("status", "CALCULATING");
            document.Add("request", new Dictionary <string, object>()
            {
                { "receivedTime", DateTimeOffset.UtcNow },
                { "query", request.Structure },
                { "type", request.Format },
                { "propertyName", request.PropertyName },
                { "models", request.ModelIds }
            });

            var models = new List <Dictionary <string, object> >();

            foreach (var modelId in request.ModelIds)
            {
                BsonDocument entityFilter = new BsonDocument("IsDeleted", new BsonDocument("$ne", true))
                                            .Add("_id", modelId);
                var modelViewResult = await _models.Aggregate().Match(entityFilter)
                                      .Project <BsonDocument>((new List <string> {
                    "Blob", "Property"
                }).ToMongoDBProjection())
                                      .FirstOrDefaultAsync();

                BsonDocument permissionFilter = new BsonDocument("IsPublic", new BsonDocument("$eq", true))
                                                .Add("_id", modelId);
                var modelPermissionResult = await _accessPermissions.Aggregate().Match(permissionFilter).FirstOrDefaultAsync();

                if (modelViewResult != null && modelPermissionResult != null)
                {
                    if (modelViewResult.GetValue("Property").ToBsonDocument().GetValue("Name").ToString() == request.PropertyName)
                    {
                        var blobId = (Guid)modelViewResult.GetValue("Blob").ToBsonDocument().GetValue("_id");
                        var bucket = modelViewResult.GetValue("Blob").ToBsonDocument().GetValue("Bucket").ToString();
                        models.Add(new Dictionary <string, object>()
                        {
                            { "Id", modelId },
                            { "Blob", new Blob {
                                  Id = blobId, Bucket = bucket
                              } }
                        }
                                   );
                    }
                    else
                    {
                        return(BadRequest($"Prediction can not be created for model with id {modelId} using parameter '{request.PropertyName}'"));
                    }
                }
                else
                {
                    return(BadRequest($"Model with id {modelId} does not exist or permission denied."));
                }
            }

            _keyValueRepository.SaveObject(predictionId, document);
            _keyValueRepository.SetExpiration(predictionId, TimeSpan.Parse(_sspSettings.RedisExpirationTime));

            await _bus.Publish(new PredictStructure(predictionId, correlationId, request.Structure, request.Format, request.PropertyName, models));

            return(Ok(new { predictionId }));
            //AcceptedAtRoute("GetPrediction", new { id = predictionId });
        }