2024-01-17 21:11:03 +00:00
|
|
|
using Bogus;
|
|
|
|
|
using Elsa.Extensions;
|
|
|
|
|
using Elsa.Samples.AspNet.BatchProcessing.Models;
|
|
|
|
|
using Elsa.Workflows;
|
|
|
|
|
using Elsa.Workflows.Attributes;
|
|
|
|
|
using Elsa.Workflows.Models;
|
|
|
|
|
|
|
|
|
|
namespace Elsa.Samples.AspNet.BatchProcessing.Activities;
|
|
|
|
|
|
2024-03-13 11:07:39 +00:00
|
|
|
/// <summary>
|
|
|
|
|
/// Fetches orders from the data source.
|
|
|
|
|
/// </summary>
|
2024-01-17 21:11:03 +00:00
|
|
|
[Activity("Demo", "Warehousing", "Fetch orders from the data source.")]
|
|
|
|
|
[Output(IsSerializable = false)]
|
|
|
|
|
public class FetchOrders : CodeActivity<IAsyncEnumerable<ICollection<Order>>>
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The total number of orders to fetch.
|
|
|
|
|
/// </summary>
|
2024-03-13 11:07:39 +00:00
|
|
|
[Input(
|
|
|
|
|
Description = "The total number of orders to fetch.",
|
|
|
|
|
DefaultValue = 1000
|
|
|
|
|
)]
|
|
|
|
|
public Input<int> Count { get; set; } = new(1000);
|
|
|
|
|
|
2024-01-17 21:11:03 +00:00
|
|
|
/// <summary>
|
|
|
|
|
/// The number of orders to fetch per batch.
|
|
|
|
|
/// </summary>
|
2024-03-13 11:07:39 +00:00
|
|
|
[Input(
|
|
|
|
|
Description = "The number of orders to fetch per batch.",
|
|
|
|
|
DefaultValue = 100
|
|
|
|
|
)]
|
2024-01-17 21:11:03 +00:00
|
|
|
public Input<int> BatchSize { get; set; } = new(100);
|
2024-03-13 11:07:39 +00:00
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2024-01-17 21:11:03 +00:00
|
|
|
protected override void Execute(ActivityExecutionContext context)
|
|
|
|
|
{
|
|
|
|
|
var count = Count.Get(context);
|
|
|
|
|
var batchSize = BatchSize.Get(context);
|
|
|
|
|
var orders = GenerateOrders(count).Chunk(batchSize).ToAsyncEnumerable();
|
2024-03-13 11:07:39 +00:00
|
|
|
|
2024-01-17 21:11:03 +00:00
|
|
|
Result.Set(context, orders);
|
|
|
|
|
}
|
2024-03-13 11:07:39 +00:00
|
|
|
|
2024-01-17 21:11:03 +00:00
|
|
|
private IEnumerable<Order> GenerateOrders(int count)
|
|
|
|
|
{
|
|
|
|
|
var orderFaker = new Faker<Order>()
|
|
|
|
|
.RuleFor(o => o.Id, f => Guid.NewGuid().ToString())
|
|
|
|
|
.RuleFor(o => o.CustomerId, f => Guid.NewGuid().ToString())
|
|
|
|
|
.RuleFor(o => o.ProductId, f => Guid.NewGuid().ToString())
|
|
|
|
|
.RuleFor(o => o.Quantity, f => f.Random.Int(1, 100))
|
|
|
|
|
.RuleFor(o => o.Price, f => f.Random.Decimal(0.01m, 1000.00m));
|
|
|
|
|
|
|
|
|
|
return orderFaker.Generate(count);
|
|
|
|
|
}
|
|
|
|
|
}
|