Exemple #1
0
        /// <summary>
        /// Deletes file if exists
        /// </summary>
        /// <param name="s3Input"></param>
        /// <returns></returns>
        public void DeleteFile(S3FileComment s3File)
        {
            try
            {
                if (s3File == null ||
                    string.IsNullOrWhiteSpace(s3File.BucketName) ||
                    string.IsNullOrWhiteSpace(s3File.FileNameWithPath))
                {
                    throw new ArgumentNullException();
                }

                //Check if bucket available or not
                if (S3Obj.IsBucketExist(s3File.BucketName))
                {
                    if (S3Obj.IsFileExist(s3File.BucketName, s3File.FileNameWithPath))
                    {
                        _ = S3Obj.DeleteFile(s3File.BucketName, s3File.FileNameWithPath);
                    }
                    else
                    {
                        throw new System.IO.FileNotFoundException(string.Format("File {0} does not exist", s3File.FileNameWithPath));
                    }
                }
            }
            catch
            {
                throw;
            }
        }
Exemple #2
0
        /// <summary>
        /// Determines if file exists
        /// </summary>
        /// <param name="s3Input"></param>
        /// <returns></returns>
        public bool FileExists(S3FileComment s3File)
        {
            bool exists = false;

            try
            {
                if (s3File == null ||
                    string.IsNullOrWhiteSpace(s3File.BucketName) ||
                    string.IsNullOrWhiteSpace(s3File.FileNameWithPath))
                {
                    throw new ArgumentNullException();
                }

                //Check if bucket available or not
                if (S3Obj.IsBucketExist(s3File.BucketName))
                {
                    exists = S3Obj.IsFileExist(s3File.BucketName, s3File.FileNameWithPath);
                }
            }
            catch
            {
                throw;
            }
            return(exists);
        }
Exemple #3
0
        public IHttpActionResult CheckExistenceOfRecommendationsReport([FromUri] Guid scenarioId)
        {
            string filePath;

            try
            {
                filePath = GetRecommendationsReportFilePath(scenarioId);
            }
            catch (ArgumentNullException exception)
            {
                return(this.Error().ResourceNotFound(exception.Message));
            }

            var descriptor = new S3FileComment()
            {
                BucketName       = _awsSettings.S3Bucket,
                FileNameWithPath = filePath
            };

            var engine = _storageClientFactory.GetEngine();

            var output = new ReportExistenceViewModel
            {
                ReportReference = CreateReportReference(scenarioId),
                Exists          = engine.FileExists(descriptor)
            };

            return(Ok(output));
        }
Exemple #4
0
        public IHttpActionResult GetReducedRecommendations(
            [FromUri] Guid id,
            [FromUri] List <string> externalCampaignNumbers = null,
            [FromUri] List <string> salesAreaNames          = null
            )
        {
            var run = _runRepository.Find(id);

            if (run is null)
            {
                run = _runRepository.FindByScenarioId(id);
            }

            if (run is null)
            {
                return(Content(HttpStatusCode.NotFound, "Invalid run ID or scenario ID"));
            }

            bool isScenarioId = run.Id != id;
            var  scenarioId   = isScenarioId ? id : run.Scenarios[0].Id;

            var descriptor = new S3FileComment()
            {
                BucketName       = _awsSettings.S3Bucket,
                FileNameWithPath = $"reports/ReducedRecommendations-{scenarioId.ToString()}-{DateTime.UtcNow.ToString("yyyyMMddHHmmssfff")}.json"
            };

            var engine = _storageClientFactory.GetEngine();

            try
            {
                if (!engine.FileExists(descriptor))
                {
                    var dataSnapshot = GenerateReducedRecommendationsResultsDataShapshot(scenarioId, externalCampaignNumbers, salesAreaNames);
                    if (dataSnapshot.data != null)
                    {
                        var jsonStr = JsonConvert.SerializeObject(dataSnapshot.data);
                        using (Stream mStream = new MemoryStream(Encoding.UTF8.GetBytes(jsonStr)))
                        {
                            engine.Upload(new S3UploadComment()
                            {
                                BucketName          = descriptor.BucketName,
                                DestinationFilePath = descriptor.FileNameWithPath,
                                FileStream          = mStream
                            });
                        }
                    }
                    else
                    {
                        return(Content(HttpStatusCode.NotFound, "No recommendations found"));
                    }
                }
            }
            catch
            {
                return(Content(HttpStatusCode.InternalServerError, "Error creating file"));
            }

            return(Content(HttpStatusCode.OK, engine.GetPreSignedUrl(descriptor)));
        }
Exemple #5
0
        public IHttpActionResult GetExportScenarioCampaignFailuresLink([FromUri] Guid scenarioId)
        {
            var engine = _storageClientFactory.GetEngine();

            var run = _runRepository.FindByScenarioId(scenarioId);

            if (run is null)
            {
                return(this.Error().ResourceNotFound(RunNotFoundMessage));
            }

            var scenario = _scenarioRepository.Get(scenarioId);
            var filePath = ScenarioCampaignFailuresReportCreator.GetFilePath(scenario.Name, run.ExecuteStartedDateTime.Value, scenario.Id);

            var descriptor = new S3FileComment()
            {
                BucketName       = _awsSettings.S3Bucket,
                FileNameWithPath = filePath
            };

            if (!engine.FileExists(descriptor))
            {
                var reportService        = new ScenarioCampaignFailuresReportCreator();
                var reportData           = GenerateScenarioCampaignFailuresDataShapshot(scenarioId);
                var tenantStartDayOfWeek = _tenantSettingsRepository.GetStartDayOfWeek();

                var excelFile = reportService.GenerateReport(reportData.data, reportData.snapshot, tenantStartDayOfWeek);

                engine.Upload(new S3UploadComment()
                {
                    BucketName          = descriptor.BucketName,
                    DestinationFilePath = descriptor.FileNameWithPath,
                    FileStream          = excelFile
                });
            }

            return(Content(HttpStatusCode.OK, engine.GetPreSignedUrl(descriptor)));
        }
Exemple #6
0
        public IHttpActionResult GetExportScenarioCampaignResultsLink([FromUri] Guid scenarioId)
        {
            var engine = _storageClientFactory.GetEngine();

            var run = _runRepository.FindByScenarioId(scenarioId);

            if (run is null)
            {
                return(this.Error().ResourceNotFound(RunNotFoundMessage));
            }

            var scenario = _scenarioRepository.Get(scenarioId);
            var filePath = ReportFileNameHelper.ScenarioCampaignResult(scenario, run.ExecuteStartedDateTime.Value);

            var descriptor = new S3FileComment()
            {
                BucketName       = _awsSettings.S3Bucket,
                FileNameWithPath = filePath
            };

            if (!engine.FileExists(descriptor))
            {
                using (var excelFileStream =
                           _scenarioCampaignResultReportCreator.GenerateReport(
                               ScenarioCampaignResultReportHelper.MapToExtendedResults(
                                   _scenarioCampaignResultRepository.Get(scenarioId)?.Items, scenario.CampaignPassPriorities, _mapper)))
                {
                    engine.Upload(new S3UploadComment()
                    {
                        BucketName          = descriptor.BucketName,
                        DestinationFilePath = descriptor.FileNameWithPath,
                        FileStream          = excelFileStream
                    });
                }
            }

            return(Content(HttpStatusCode.OK, engine.GetPreSignedUrl(descriptor)));
        }
Exemple #7
0
 public string GetPreSignedUrl(S3FileComment s3File) =>
 _s3Obj.GetPreSignedUrl(s3File.BucketName, s3File.FileNameWithPath);