Skip to content

Commit

Permalink
Fixed statistics calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
Aragas committed May 27, 2024
1 parent ec19326 commit aec912f
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public interface IVogen<TVogen, TValueObject>
static abstract bool Equals(TVogen left, TVogen right, IEqualityComparer<TVogen> comparer);

static abstract int CompareTo(TVogen left, TVogen right);

TValueObject Value { get; }

bool IsInitialized();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.OpenApi.Models;

using Swashbuckle.AspNetCore.SwaggerGen;

namespace BUTR.Site.NexusMods.Server.ValueObjects.Utils;
Expand Down Expand Up @@ -26,7 +27,7 @@ private static T CopyPublicProperties<T>(T oldObject, T newObject) where T : cla

return newObject;
}

public void Apply(OpenApiSchema schema, SchemaFilterContext context)
{
if (context.Type.GetInterfaces().FirstOrDefault(x => x.IsGenericType && x.GetGenericTypeDefinition() == typeof(IVogen<,>)) is not { } vogen)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,20 +46,9 @@ public async Task Execute(IJobExecutionContext context)
private async Task HandleTenantAsync(AsyncServiceScope scope, TenantId tenant, CancellationToken ct)
{
var unitOfWorkFactory = scope.ServiceProvider.GetRequiredService<IUnitOfWorkFactory>();
await using var unitOfRead = unitOfWorkFactory.CreateUnitOfRead();
await using var unitOfWrite = unitOfWorkFactory.CreateUnitOfWrite();

var exceptionTypes = await unitOfRead.ExceptionTypes.GetAllAsync(null, null, ct);
var statistics = exceptionTypes.Select(x => new StatisticsTopExceptionsTypeEntity
{
TenantId = tenant,
ExceptionTypeId = x.ExceptionTypeId,
ExceptionType = unitOfWrite.UpsertEntityFactory.GetOrCreateExceptionType(x.ExceptionTypeId),
ExceptionCount = x.ToCrashReports.Count
}).ToList();

unitOfWrite.StatisticsTopExceptionsTypes.Remove(x => true);
unitOfWrite.StatisticsTopExceptionsTypes.UpsertRange(statistics);
await unitOfWrite.StatisticsTopExceptionsTypes.CalculateAsync(ct);

await unitOfWrite.SaveChangesAsync(CancellationToken.None);
}
Expand Down
2 changes: 1 addition & 1 deletion src/BUTR.Site.NexusMods.Server/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ public static IHostBuilder CreateHostBuilder(string[] args) => Host
var loggingEndpoint = oltpSection.GetValue<string>("LoggingEndpoint");
if (loggingEndpoint is null) return;
var loggingProtocol = oltpSection.GetValue<OtlpExportProtocol>("LoggingProtocol");

builder.AddOpenTelemetry(o =>
{
o.IncludeScopes = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,51 @@
using BUTR.Site.NexusMods.Server.Contexts;
using BUTR.Site.NexusMods.Server.Models.Database;

using EFCore.BulkExtensions;

using Microsoft.EntityFrameworkCore;

using System.Linq;
using System.Threading;
using System.Threading.Tasks;

namespace BUTR.Site.NexusMods.Server.Repositories;

public interface IStatisticsTopExceptionsTypeEntityRepositoryRead : IRepositoryRead<StatisticsTopExceptionsTypeEntity>;
public interface IStatisticsTopExceptionsTypeEntityRepositoryWrite : IRepositoryWrite<StatisticsTopExceptionsTypeEntity>, IStatisticsTopExceptionsTypeEntityRepositoryRead;
public interface IStatisticsTopExceptionsTypeEntityRepositoryWrite : IRepositoryWrite<StatisticsTopExceptionsTypeEntity>, IStatisticsTopExceptionsTypeEntityRepositoryRead
{
Task CalculateAsync(CancellationToken ct);
}

[ScopedService<IStatisticsTopExceptionsTypeEntityRepositoryWrite, IStatisticsTopExceptionsTypeEntityRepositoryRead>]
internal class StatisticsTopExceptionsTypeEntityRepository : Repository<StatisticsTopExceptionsTypeEntity>, IStatisticsTopExceptionsTypeEntityRepositoryWrite
{
private readonly ITenantContextAccessor _tenantContextAccessor;

public StatisticsTopExceptionsTypeEntityRepository(IAppDbContextProvider appDbContextProvider, ITenantContextAccessor tenantContextAccessor) : base(appDbContextProvider.Get())
{
_tenantContextAccessor = tenantContextAccessor;
}

protected override IQueryable<StatisticsTopExceptionsTypeEntity> InternalQuery => base.InternalQuery
.Include(x => x.ExceptionType);

public StatisticsTopExceptionsTypeEntityRepository(IAppDbContextProvider appDbContextProvider) : base(appDbContextProvider.Get()) { }
public async Task CalculateAsync(CancellationToken ct)
{
if (_dbContext is not AppDbContextWrite dbContextWrite)
throw AppDbContextRead.WriteNotSupported();

var tenant = _tenantContextAccessor.Current;
var upsertEntityFactory = dbContextWrite.GetEntityFactory();
var statistics = dbContextWrite.ExceptionTypes.Select(x => new StatisticsTopExceptionsTypeEntity
{
TenantId = tenant,
ExceptionTypeId = x.ExceptionTypeId,
ExceptionType = upsertEntityFactory.GetOrCreateExceptionType(x.ExceptionTypeId),
ExceptionCount = x.ToCrashReports.Count
}).ToList();

await dbContextWrite.StatisticsTopExceptionsTypes.Where(x => true).ExecuteDeleteAsync(cancellationToken: ct);
await dbContextWrite.BulkInsertOrUpdateAsync(statistics, cancellationToken: ct);
}
}

0 comments on commit aec912f

Please sign in to comment.