Skip to content

Commit

Permalink
got executable up and running
Browse files Browse the repository at this point in the history
  • Loading branch information
Aaronontheweb committed Mar 25, 2024
1 parent 74b5a35 commit d564800
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 3 deletions.
16 changes: 14 additions & 2 deletions src/cqrs/cqrs-sqlserver/CqrsSqlServer.Backend/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@
using Akka.Persistence.SqlServer.Hosting;
using CqrsSqlServer.Backend.Actors;
using CqrsSqlServer.DataModel;
using CqrsSqlServer.Shared;
using CqrsSqlServer.Shared.Serialization;
using CqrsSqlServer.Shared.Sharding;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Petabridge.Cmd.Cluster;
using Petabridge.Cmd.Cluster.Sharding;
using Petabridge.Cmd.Host;

var environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Development";

Expand Down Expand Up @@ -44,8 +48,16 @@
Props.Create(() => new GenericChildPerEntityParent(new ProductMessageRouter(),
ProductTotalsActor.GetProps)), "productTotals");

registry.Register<ProductTotalsActor>(parentActor);
});
registry.Register<ProductMarker>(parentActor);
})
.AddPetabridgeCmd(cmd =>
{

})
.AddStartup((system, registry) =>
{
new FakeDataGenerator().Generate(system, registry, 100);
});;
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
</ItemGroup>

<ItemGroup>
<None Remove="appsettings.json"/>
<Content Include="appsettings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<ItemGroup>
<PackageReference Include="Akka.Cluster.Sharding" Version="$(AkkaVersion)"/>
<PackageReference Include="Akka.Cluster.Hosting" Version="$(AkkaHostingVersion)"/>
<PackageReference Include="Bogus" Version="35.5.0" />
<PackageReference Include="Grpc.Tools" Version="2.62.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down
91 changes: 91 additions & 0 deletions src/cqrs/cqrs-sqlserver/CqrsSqlServer.Shared/FakeDataGenerator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
using Akka.Actor;
using Akka.Event;
using Akka.Hosting;
using Bogus;
using CqrsSqlServer.Shared.Commands;
using CqrsSqlServer.Shared.Sharding;

namespace CqrsSqlServer.Shared;

public class FakeDataGenerator
{
private readonly Faker<CreateProduct> _productGenerator;

public FakeDataGenerator()
{
Randomizer.Seed = new Random(123456);
_productGenerator = new Faker<CreateProduct>()
.CustomInstantiator(f =>
{
var count = f.Random.Int(0, 3);
return new CreateProduct(
f.Random.Guid().ToString(),
f.Commerce.ProductName(),
f.Finance.Amount(min: 1, max: 1000),
f.Random.Int(1, 1000),
count == 0 ? Array.Empty<string>() : f.Commerce.Categories(count)
);
});
}

public void Generate(ActorSystem system, IActorRegistry registry, int count)
{
var log = Logging.GetLogger(system, nameof(FakeDataGenerator));
var shardRegion = registry.Get<ProductMarker>();

GenerateAsync(count, shardRegion, log)
.ContinueWith(t =>
{
if (!t.IsCompletedSuccessfully)
{
log.Error(t.Exception, "Failed to generate fake data");
}
})
.ConfigureAwait(false);
}

private async Task GenerateAsync(int count, IActorRef shardRegion, ILoggingAdapter log)
{
log.Info($"Generating {count} fake data");
var items = new List<CreateProduct>();
var total = 0;
foreach (var _ in Enumerable.Range(0, count))
{
var item = _productGenerator.Generate();
items.Add(item);
var response = await shardRegion.Ask<ProductCommandResponse>(item, TimeSpan.FromSeconds(3));
if (!response.Success)
{
log.Error(response.Message);
}
else
{
total++;
}
}

var purchaseCount = count * 5;
var rnd = Randomizer.Seed;
var purchaseTotal = 0;
var twoMinutes = TimeSpan.FromMinutes(2);
var rollBackMinutes = twoMinutes * (purchaseCount + 1);
var now = DateTime.UtcNow - rollBackMinutes;
foreach (var _ in Enumerable.Range(0, purchaseCount))
{
var item = items[rnd.Next(0, items.Count)];
var newOrder = new ProductOrder(Guid.NewGuid().ToString(), item.ProductId, rnd.Next(1, 100), now);
now += twoMinutes;
var createOrderCommand = new PurchaseProduct(newOrder);
var response = await shardRegion.Ask<ProductCommandResponse>(createOrderCommand, TimeSpan.FromSeconds(3));
if (!response.Success)
{
log.Error(response.Message);
}
else
{
purchaseTotal++;
}
}
log.Info($"Generated {total} fake data and {purchaseTotal} fake purchases");
}
}

0 comments on commit d564800

Please sign in to comment.