-
-
Notifications
You must be signed in to change notification settings - Fork 519
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Initial structure for Data Analytics.
Added basic Card Abandonment Rate calculations. Added Market Basked Model analytics Added Project for hosting Data Analytics and ESDB subscription management Added the separate solution for Data Analytics example Added configuration for CartAbandonmentRateAnalysis Added Product Relationships analytics Refactored CartAbandonmentRate to return summary type instead of event Replaced redundant Shopping Cart class for CartProductItemsMatched event and used Dictionary instead Added ElasticSearch read model to Data Analytics example
- Loading branch information
1 parent
a1f5d91
commit d775bac
Showing
34 changed files
with
1,749 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
Sample/EventStoreDB/DataAnalytics/DataAnalytics.Core/BackgroundWorkers/BackgroundWorker.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.Extensions.Hosting; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace DataAnalytics.Core.BackgroundWorkers | ||
{ | ||
public class BackgroundWorker: IHostedService | ||
{ | ||
private Task? executingTask; | ||
private CancellationTokenSource? cts; | ||
private readonly ILogger<BackgroundWorker> logger; | ||
private readonly Func<CancellationToken, Task> perform; | ||
|
||
public BackgroundWorker( | ||
ILogger<BackgroundWorker> logger, | ||
Func<CancellationToken, Task> perform | ||
) | ||
{ | ||
this.logger = logger; | ||
this.perform = perform; | ||
} | ||
|
||
public Task StartAsync(CancellationToken cancellationToken) | ||
{ | ||
// Create a linked token so we can trigger cancellation outside of this token's cancellation | ||
cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken); | ||
|
||
executingTask = perform(cts.Token); | ||
|
||
return executingTask; | ||
} | ||
|
||
public async Task StopAsync(CancellationToken cancellationToken) | ||
{ | ||
// Stop called without start | ||
if (executingTask == null) | ||
return; | ||
|
||
// Signal cancellation to the executing method | ||
cts?.Cancel(); | ||
|
||
// Wait until the issue completes or the stop token triggers | ||
await Task.WhenAny(executingTask, Task.Delay(-1, cancellationToken)); | ||
|
||
// Throw if cancellation triggered | ||
cancellationToken.ThrowIfCancellationRequested(); | ||
|
||
logger.LogInformation("Background worker stopped"); | ||
} | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
Sample/EventStoreDB/DataAnalytics/DataAnalytics.Core/Configuration.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
using DataAnalytics.Core.ElasticSearch; | ||
using DataAnalytics.Core.Events; | ||
using DataAnalytics.Core.EventStoreDB; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace DataAnalytics.Core | ||
{ | ||
public static class Configuration | ||
{ | ||
public static IServiceCollection AddCoreServices( | ||
this IServiceCollection services, | ||
IConfiguration configuration | ||
) => | ||
services | ||
.AddEventBus() | ||
.AddEventStoreDB(configuration) | ||
.AddElasticsearch(configuration); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
Sample/EventStoreDB/DataAnalytics/DataAnalytics.Core/DataAnalytics.Core.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<Nullable>enable</Nullable> | ||
<TreatWarningsAsErrors>true</TreatWarningsAsErrors> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="6.0.0" /> | ||
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="6.0.0" /> | ||
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="6.0.0" /> | ||
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="6.0.0" /> | ||
<PackageReference Include="Polly" Version="7.2.2" /> | ||
|
||
<PackageReference Include="EventStore.Client.Grpc.Streams" Version="21.2.0" /> | ||
|
||
<PackageReference Include="NEST" Version="7.15.0" /> | ||
</ItemGroup> | ||
|
||
</Project> |
32 changes: 32 additions & 0 deletions
32
Sample/EventStoreDB/DataAnalytics/DataAnalytics.Core/ElasticSearch/Configuration.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
using System; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Nest; | ||
|
||
namespace DataAnalytics.Core.ElasticSearch | ||
{ | ||
public class ElasticSearchConfig | ||
{ | ||
public string Url { get; set; } = default!; | ||
public string DefaultIndex { get; set; } = default!; | ||
} | ||
|
||
public static class ElasticSearchConfigExtensions | ||
{ | ||
private const string DefaultConfigKey = "ElasticSearch"; | ||
public static IServiceCollection AddElasticsearch( | ||
this IServiceCollection services, IConfiguration configuration, Action<ConnectionSettings>? config = null) | ||
{ | ||
var elasticSearchConfig = configuration.GetSection(DefaultConfigKey).Get<ElasticSearchConfig>(); | ||
|
||
var settings = new ConnectionSettings(new Uri(elasticSearchConfig.Url)) | ||
.DefaultIndex(elasticSearchConfig.DefaultIndex); | ||
|
||
config?.Invoke(settings); | ||
|
||
var client = new ElasticClient(settings); | ||
|
||
return services.AddSingleton<IElasticClient>(client); | ||
} | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
...le/EventStoreDB/DataAnalytics/DataAnalytics.Core/ElasticSearch/ElasticSearchRepository.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
using System; | ||
using System.Collections.Concurrent; | ||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Nest; | ||
|
||
namespace DataAnalytics.Core.ElasticSearch | ||
{ | ||
public static class ElasticSearchRepository | ||
{ | ||
|
||
public static async Task<T?> Find<T>(this IElasticClient elasticClient, string id, CancellationToken ct) | ||
where T: class => | ||
(await elasticClient.GetAsync<T>(id, ct: ct))?.Source; | ||
|
||
public static async Task Upsert<T>(this IElasticClient elasticClient, string id, T entity, CancellationToken ct) | ||
where T: class => | ||
await elasticClient.UpdateAsync<T>(id, | ||
u => u.Doc(entity).Upsert(entity).Index(ToIndexName<T>()), | ||
ct | ||
); | ||
|
||
private static readonly ConcurrentDictionary<Type, string> TypeNameMap = new(); | ||
|
||
private static string ToIndexName<TIndex>() | ||
{ | ||
var indexType = typeof(TIndex); | ||
return TypeNameMap.GetOrAdd(indexType, _ => | ||
{ | ||
var modulePrefix = indexType.Namespace!.Split(".").First(); | ||
return $"{modulePrefix}-{indexType.Name}".ToLower(); | ||
}); | ||
} | ||
} | ||
} | ||
|
38 changes: 38 additions & 0 deletions
38
Sample/EventStoreDB/DataAnalytics/DataAnalytics.Core/EventStoreDB/Configuration.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
using DataAnalytics.Core.Subscriptions; | ||
using EventStore.Client; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace DataAnalytics.Core.EventStoreDB | ||
{ | ||
public class EventStoreDBConfig | ||
{ | ||
public string ConnectionString { get; set; } = default!; | ||
} | ||
|
||
public record EventStoreDBOptions( | ||
bool UseInternalCheckpointing = true | ||
); | ||
|
||
public static class EventStoreDBConfigExtensions | ||
{ | ||
private const string DefaultConfigKey = "EventStore"; | ||
|
||
public static IServiceCollection AddEventStoreDB(this IServiceCollection services, IConfiguration config, EventStoreDBOptions? options = null) | ||
{ | ||
var eventStoreDBConfig = config.GetSection(DefaultConfigKey).Get<EventStoreDBConfig>(); | ||
|
||
services.AddSingleton( | ||
new EventStoreClient(EventStoreClientSettings.Create(eventStoreDBConfig.ConnectionString))) | ||
.AddTransient<EventStoreDBSubscriptionToAll, EventStoreDBSubscriptionToAll>(); | ||
|
||
if (options?.UseInternalCheckpointing != false) | ||
{ | ||
services | ||
.AddTransient<ISubscriptionCheckpointRepository, EventStoreDBSubscriptionCheckpointRepository>(); | ||
} | ||
|
||
return services; | ||
} | ||
} | ||
} |
Oops, something went wrong.