Skip to content

Commit

Permalink
Added HostRecorder (part 1)
Browse files Browse the repository at this point in the history
  • Loading branch information
jezzsantos committed Dec 25, 2023
1 parent 4e459a9 commit 31c7ce8
Show file tree
Hide file tree
Showing 173 changed files with 2,428 additions and 573 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.2" />
<ProjectReference Include="..\AncillaryApplication\AncillaryApplication.csproj" />
<ProjectReference Include="..\UnitTesting.Common\UnitTesting.Common.csproj" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\AncillaryApplication\AncillaryApplication.csproj" />
<ProjectReference Include="..\Application.Interfaces\Application.Interfaces.csproj" />
<ProjectReference Include="..\UnitTesting.Common\UnitTesting.Common.csproj" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.2" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using AncillaryDomain;
using Application.Interfaces;
using Application.Persistence.Shared;
using Application.Services.Shared;
using Common;
using Common.Extensions;
using Domain.Common.Identity;
Expand Down Expand Up @@ -98,7 +97,7 @@ public async Task WhenDeliverUsageAsync_ThenDelivers()
{
ForId = "aforid",
EventName = "aneventname",
Context = new Dictionary<string, string>
Additional = new Dictionary<string, string>
{
{ "aname", "avalue" }
}
Expand Down
3 changes: 1 addition & 2 deletions src/AncillaryApplication/AncillaryApplication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using Application.Persistence.Interfaces;
using Application.Persistence.Shared;
using Application.Resources.Shared;
using Application.Services.Shared;
using Common;
using Common.Extensions;
using Domain.Common.Identity;
Expand Down Expand Up @@ -126,7 +125,7 @@ private async Task<Result<bool, Error>> DeliverUsageAsync(ICallerContext context
return Error.RuleViolation(Resources.AncillaryApplication_MissingUsageEventName);
}

await _usageReportingService.TrackAsync(context, message.ForId!, message.EventName!, message.Context,
await _usageReportingService.TrackAsync(context, message.ForId!, message.EventName!, message.Additional,
cancellationToken);

_recorder.TraceInformation(context.ToCall(), "Delivered usage for {For}", message.ForId!);
Expand Down
1 change: 1 addition & 0 deletions src/AncillaryApplication/AncillaryApplication.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<ProjectReference Include="..\AncillaryDomain\AncillaryDomain.csproj" />
<ProjectReference Include="..\Application.Common\Application.Common.csproj" />
<ProjectReference Include="..\Application.Persistence.Common\Application.Persistence.Common.csproj" />
<ProjectReference Include="..\Application.Persistence.Shared\Application.Persistence.Shared.csproj" />
<ProjectReference Include="..\Application.Services.Shared\Application.Services.Shared.csproj" />
</ItemGroup>

Expand Down
2 changes: 1 addition & 1 deletion src/AncillaryApplication/IAncillaryApplication.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Application.Interfaces;
using Application.Resources.Shared;
using Common;
using Audit = Application.Resources.Shared.Audit;

namespace AncillaryApplication;

Expand Down
15 changes: 15 additions & 0 deletions src/AncillaryApplication/IRecordingApplication.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Application.Interfaces;
using Common;

namespace AncillaryApplication;

public interface IRecordingApplication
{
Task<Result<Error>> RecordMeasurementAsync(ICallerContext context, string eventName,
Dictionary<string, object?>? additional,
CancellationToken cancellationToken);

Task<Result<Error>> RecordUsageAsync(ICallerContext context, string eventName,
Dictionary<string, object?>? additional,
CancellationToken cancellationToken);
}
43 changes: 43 additions & 0 deletions src/AncillaryApplication/RecordingApplication.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using Application.Common;
using Application.Interfaces;
using Common;
using Common.Extensions;
using Task = System.Threading.Tasks.Task;

namespace AncillaryApplication;

public class RecordingApplication : IRecordingApplication
{
private readonly IRecorder _recorder;

public RecordingApplication(IRecorder recorder)
{
_recorder = recorder;
}

public Task<Result<Error>> RecordMeasurementAsync(ICallerContext context, string eventName,
Dictionary<string, object?>? additional,
CancellationToken cancellationToken)
{
_recorder.Measure(context.ToCall(), eventName, (additional.Exists()
? additional
.Where(pair => pair.Value.Exists())
.ToDictionary(pair => pair.Key, pair => pair.Value)
: null)!);

return Task.FromResult(Result.Ok);
}

public Task<Result<Error>> RecordUsageAsync(ICallerContext context, string eventName,
Dictionary<string, object?>? additional,
CancellationToken cancellationToken)
{
_recorder.TrackUsage(context.ToCall(), eventName, (additional.Exists()
? additional
.Where(pair => pair.Value.Exists())
.ToDictionary(pair => pair.Key, pair => pair.Value)
: null)!);

return Task.FromResult(Result.Ok);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.2" />
<ProjectReference Include="..\AncillaryDomain\AncillaryDomain.csproj" />
<ProjectReference Include="..\UnitTesting.Common\UnitTesting.Common.csproj" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\AncillaryDomain\AncillaryDomain.csproj" />
<ProjectReference Include="..\Application.Interfaces\Application.Interfaces.csproj" />
<ProjectReference Include="..\UnitTesting.Common\UnitTesting.Common.csproj" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.2" />
</ItemGroup>

</Project>
11 changes: 11 additions & 0 deletions src/AncillaryDomain/Validations.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using Domain.Interfaces.Validations;

namespace AncillaryDomain;

public static class Validations
{
public static class Recording
{
public static readonly Validation AdditionalStringValue = CommonValidations.DescriptiveName(1, 300);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.2" />
<ProjectReference Include="..\ApiHost1\ApiHost1.csproj" />
<ProjectReference Include="..\IntegrationTesting.WebApi.Common\IntegrationTesting.WebApi.Common.csproj" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\ApiHost1\ApiHost1.csproj" />
<ProjectReference Include="..\IntegrationTesting.WebApi.Common\IntegrationTesting.WebApi.Common.csproj" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.2" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using AncillaryInfrastructure.IntegrationTests.Stubs;
using ApiHost1;
using Application.Persistence.Shared;
using Application.Services.Shared;
using Common;
using Common.Extensions;
using FluentAssertions;
Expand Down
89 changes: 89 additions & 0 deletions src/AncillaryInfrastructure.IntegrationTests/RecordingApiSpec.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
using System.Text.Json;
using AncillaryInfrastructure.IntegrationTests.Stubs;
using ApiHost1;
using Common;
using FluentAssertions;
using Infrastructure.Web.Api.Common.Extensions;
using Infrastructure.Web.Api.Operations.Shared.Ancillary;
using IntegrationTesting.WebApi.Common;
using Microsoft.Extensions.DependencyInjection;
using Xunit;
using Task = System.Threading.Tasks.Task;

namespace AncillaryInfrastructure.IntegrationTests;

[Trait("Category", "Integration.Web")]
public class RecordingApiSpec : WebApiSpec<Program>
{
private readonly StubRecorder _recorder;

public RecordingApiSpec(WebApiSetup<Program> setup) : base(setup, OverrideDependencies)
{
EmptyAllRepositories(setup);
_recorder = setup.GetRequiredService<IRecorder>().As<StubRecorder>();
_recorder.Reset();
}

[Fact]
public async Task WhenRecordUseWithNoAdditional_ThenRecords()
{
var request = new RecordUseRequest
{
EventName = "aneventname",
Additional = null
};
await Api.PostAsync(request, req => req.SetHmacAuth(request, "asecret"));

_recorder.LastUsageEventName.Should().Be("aneventname");
_recorder.LastUsageAdditional.Should().BeNull();
}

[Fact]
public async Task WhenRecordUse_ThenRecords()
{
var request = new RecordUseRequest
{
EventName = "aneventname",
Additional = new Dictionary<string, object?>
{
{ "aname1", "avalue" },
{ "aname2", 25 },
{ "aname3", true }
}
};
await Api.PostAsync(request, req => req.SetHmacAuth(request, "asecret"));

_recorder.LastUsageEventName.Should().Be("aneventname");
_recorder.LastUsageAdditional!.Count.Should().Be(3);
_recorder.LastUsageAdditional!["aname1"].As<JsonElement>().GetString().Should().Be("avalue");
_recorder.LastUsageAdditional!["aname2"].As<JsonElement>().GetInt32().Should().Be(25);
_recorder.LastUsageAdditional!["aname3"].As<JsonElement>().GetBoolean().Should().Be(true);
}

[Fact]
public async Task WhenRecordMeasure_ThenRecords()
{
var request = new RecordMeasureRequest
{
EventName = "aneventname",
Additional = new Dictionary<string, object?>
{
{ "aname1", "avalue" },
{ "aname2", 25 },
{ "aname3", true }
}
};
await Api.PostAsync(request, req => req.SetHmacAuth(request, "asecret"));

_recorder.LastMeasureEventName.Should().Be("aneventname");
_recorder.LastMeasureAdditional!.Count.Should().Be(3);
_recorder.LastMeasureAdditional!["aname1"].As<JsonElement>().GetString().Should().Be("avalue");
_recorder.LastMeasureAdditional!["aname2"].As<JsonElement>().GetInt32().Should().Be(25);
_recorder.LastMeasureAdditional!["aname3"].As<JsonElement>().GetBoolean().Should().Be(true);
}

private static void OverrideDependencies(IServiceCollection services)
{
services.AddSingleton<IRecorder, StubRecorder>();
}
}
Loading

0 comments on commit 31c7ce8

Please sign in to comment.