Esempio n. 1
0
        private async Task <List <RecordProcessingResult> > Test(string header, params string[] records)
        {
            var builder = new StringBuilder();

            var withHeader = !string.IsNullOrEmpty(header);

            if (withHeader)
            {
                builder.AppendLine(header);
            }

            foreach (var record in records)
            {
                builder.AppendLine(record);
            }

            var stream    = builder.ToString().ToStream();
            var processed = _csvProcessor.Process(stream, withHeader).ConfigureAwait(false);

            var result = new List <RecordProcessingResult>();

            await foreach (var record in processed)
            {
                result.Add(record);
            }
            return(result);
        }
Esempio n. 2
0
        public async Task <UploadResult> Upload(Stream stream, UploadConfiguration uploadConfiguration)
        {
            var result = new UploadResult();
            var buffer = new List <RecordProcessingResult>();

            DateTime start = DateTime.Now;

            try
            {
                var records = _csvProcessor.Process(stream, uploadConfiguration.ContainsHeader).ConfigureAwait(false);

                await foreach (var record in records)
                {
                    await ValidateRecord(record).ConfigureAwait(false);

                    buffer.Add(record);
                    if (buffer.Count >= _settings.BufferSize)
                    {
                        await Store(buffer).ConfigureAwait(false);

                        MoveBufferToResult(result, buffer, uploadConfiguration);

                        if (result.Records.Count >= uploadConfiguration.MaxRecordsInResponse)
                        {
                            break;
                        }
                    }
                }

                await Store(buffer).ConfigureAwait(false);

                MoveBufferToResult(result, buffer, uploadConfiguration);
            }
            // For handling file validation exceptions etc., which we want to report to end user.
            // There are currently no business expections thrown, so this is just for show
            catch (BusinessException e)
            {
                _logger.LogWarning(e, "Upload failed with a business exception");
                result.Issue = new Error(e.Message);
            }
            // For handling other exceptions, i.e. storage failures. We don't want to propagate the exception message to user in this case
            catch (Exception e)
            {
                _logger.LogError(e, "Upload failed with an unexpected exception");
                result.Issue = new Error("Upload failed with an unexpected exception");
            }


            _logger.LogInformation($"Returning {result.Records.Count} records. Last processed line: {result.LastProcessedLine}. Time ellapsed: {(DateTime.Now - start).TotalSeconds} seconds");
            return(result);
        }