MES-ETL/Mesdb.DataGenerator/MockInputService.cs

49 lines
1.7 KiB
C#
Raw Normal View History

using MesETL.App.Const;
using MesETL.App.HostedServices.Abstractions;
using MesETL.App.Services;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
namespace Mesdb.DataGenerator;
public class MockInputService : IInputService
{
private readonly DataRecordQueue _producerQueue;
private readonly ProcessContext _context;
private readonly IOptions<MockInputOptions> _options;
private readonly ILogger _logger;
public MockInputService([FromKeyedServices(ConstVar.Producer)]DataRecordQueue producerQueue, ProcessContext context, IOptions<MockInputOptions> options,
ILogger<MockInputService> logger)
{
_producerQueue = producerQueue;
_context = context;
_options = options;
_logger = logger;
}
public async Task ExecuteAsync(CancellationToken cancellationToken)
{
_logger.LogInformation("***** 开始模拟输入数据 *****");
foreach (var (table, options) in _options.Value.Rules)
{
_logger.LogInformation("模拟表 '{TableName}' 输入,数量: {Amount}", table, options.Amount);
for (int i = 0; i < options.Amount; i++)
{
var ctx = new TableMockContext()
{
Index = i,
};
var record = options.Generate(ctx);
await _producerQueue.EnqueueAsync(record);
_context.AddInput(1);
_context.AddTableInput(table, 1);
}
_logger.LogInformation("表 '{TableName}' 输入完成", table);
}
_context.CompleteInput();
_logger.LogInformation("***** 模拟数据输入完成 *****");
}
}