#nullable enable using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Diagnostics; using Microsoft.Extensions.Options; using Yavsc.Abstract.Models.Messaging; using Yavsc.Models; using Yavsc.Settings; namespace Yavsc.Server.Helpers; public sealed class ResourceUsageNotificationInterceptor(IOptions options) : SaveChangesInterceptor { private static readonly int[] Thresholds = [50, 75, 90, 95, 99]; public override InterceptionResult SavingChanges(DbContextEventData eventData, InterceptionResult result) { AddNotifications(eventData.Context); return result; } public override ValueTask> SavingChangesAsync( DbContextEventData eventData, InterceptionResult result, CancellationToken cancellationToken = default) { AddNotifications(eventData.Context); return ValueTask.FromResult(result); } private void AddNotifications(DbContext? context) { if (context is not ApplicationDbContext db) return; db.ChangeTracker.DetectChanges(); var policy = options.Value; foreach (var entry in db.ChangeTracker.Entries() .Where(e => e.State is EntityState.Added or EntityState.Modified).ToArray()) { var record = entry.Entity; var previous = entry.State == EntityState.Added ? 0m : ResourceUsageCredit.NetConsumption(policy.Calculate( entry.OriginalValues.GetValue(nameof(record.ApiCalls)), entry.OriginalValues.GetValue(nameof(record.CpuSeconds)), entry.OriginalValues.GetValue(nameof(record.BandwidthMb)), entry.OriginalValues.GetValue(nameof(record.StorageMb))), entry.OriginalValues.GetValue(nameof(record.CreditPoints))); var current = ResourceUsageCredit.NetConsumption(policy.Calculate( record.ApiCalls, record.CpuSeconds, record.BandwidthMb, record.StorageMb), record.CreditPoints); var limit = policy.GetLimit(record.UserId); var threshold = Thresholds.LastOrDefault(value => previous < limit * value / 100m && current >= limit * value / 100m); if (threshold == 0) continue; var target = $"user/{record.UserId}"; var tag = $"resource-usage/{threshold}"; // A failed save may be retried with the same pending notification. if (db.ChangeTracker.Entries().Any(e => e.State == EntityState.Added && e.Entity.Target == target && e.Entity.tag == tag)) continue; db.Notification.Add(new Notification { Target = target, tag = tag, title = $"Consommation des ressources : seuil de {threshold} % atteint", body = $"Votre consommation globale de ressources, après déduction des crédits, a atteint {threshold} % de votre limite. Consultez votre consommation et vos abonnements pour éviter le blocage des opérations à la limite.", click_action = "Fermer" }); } } }