76 lines
2.6 KiB
C#
76 lines
2.6 KiB
C#
using System.Diagnostics;
|
|
using ConsoleApp2.Helpers;
|
|
using ConsoleApp2.Services;
|
|
using Microsoft.Extensions.Hosting;
|
|
using Microsoft.Extensions.Logging;
|
|
using Microsoft.Extensions.Options;
|
|
|
|
namespace ConsoleApp2;
|
|
|
|
public class CsvConversion : BackgroundService
|
|
{
|
|
private readonly ILogger _logger;
|
|
private readonly IOptions<CsvOptions> _csvOptions;
|
|
private readonly DataTransformService _transform;
|
|
private readonly TaskManager _taskManager;
|
|
|
|
|
|
public CsvConversion(ILogger<CsvConversion> logger,
|
|
IOptions<CsvOptions> csvOptions,
|
|
DataTransformService transform,
|
|
TaskManager taskManager)
|
|
{
|
|
_logger = logger;
|
|
_csvOptions = csvOptions;
|
|
_transform = transform;
|
|
_taskManager = taskManager;
|
|
}
|
|
|
|
protected override async Task ExecuteAsync(CancellationToken cancellationToken)
|
|
{
|
|
var sw = Stopwatch.StartNew();
|
|
var inputDir = _csvOptions.Value.InputDir;
|
|
_logger.LogInformation("Working dir: {InputDir}", inputDir);
|
|
var files = Directory.GetFiles(inputDir).Where(s => s.EndsWith(".sql") && !s.Contains("schema")).ToArray();
|
|
if (files.Length == 0)
|
|
{
|
|
_logger.LogInformation("No sql files found in {InputDir}", inputDir);
|
|
return;
|
|
}
|
|
|
|
foreach(var sqlPath in files)
|
|
{
|
|
_logger.LogInformation("Working sql file: {SqlPath}", sqlPath);
|
|
var headers = await DumpDataHelper.GetCsvHeadersFromSqlFileAsync(sqlPath);
|
|
var csvFiles = await DumpDataHelper.GetCsvFileNamesFromSqlFileAsync(sqlPath);
|
|
|
|
var queue = new DataRecordQueue();
|
|
foreach (var csvFile in csvFiles)
|
|
{
|
|
var csvPath = Path.Combine(inputDir, csvFile);
|
|
var source = new JsvSource(csvPath, headers, _logger);
|
|
|
|
while (await source.ReadAsync())
|
|
{
|
|
queue.Enqueue(source.Current);
|
|
}
|
|
|
|
if (queue.Count > 200)
|
|
{
|
|
var queue1 = queue;
|
|
await _taskManager.CreateTask(async () => await _transform.ExecuteAsync(queue1, cancellationToken));
|
|
queue = new DataRecordQueue();
|
|
}
|
|
|
|
if (cancellationToken.IsCancellationRequested)
|
|
return;
|
|
}
|
|
|
|
_logger.LogInformation("File '{File}' input completed", Path.GetFileName(sqlPath));
|
|
}
|
|
|
|
_logger.LogInformation("***** Csv input service completed *****");
|
|
_logger.LogInformation("Elapsed: {Elapsed}", sw.Elapsed);
|
|
_taskManager.MainTaskCompleted = true;
|
|
}
|
|
} |