Merge branch 'dev' into j3/feat/pagination
This commit is contained in:
@@ -1,8 +1,11 @@
|
||||
namespace Webzine.WebApplication.Areas.Administration.Controllers;
|
||||
|
||||
using Business.Contracts;
|
||||
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Mvc.Rendering;
|
||||
|
||||
using Webzine.Business.Contracts.Dto;
|
||||
using Webzine.Entity;
|
||||
using Webzine.Repository.Contracts;
|
||||
using Webzine.WebApplication.Areas.Administration.ViewModels.Titre;
|
||||
@@ -17,6 +20,7 @@ public class TitreController : Controller
|
||||
private readonly ITitreRepository titreRepository;
|
||||
private readonly IArtisteRepository artisteRepository;
|
||||
private readonly IStyleRepository styleRepository;
|
||||
private readonly ITitreAdminService titreAdminService;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="TitreController"/> class.
|
||||
@@ -26,12 +30,14 @@ public class TitreController : Controller
|
||||
/// <param name="titreRepository">Repository des titres injecté pour accéder aux données des titres.</param>
|
||||
/// <param name="artisteRepository">Repository des artistes injecté pour accéder aux données des artistes, nécessaires pour les associations avec les titres.</param>
|
||||
/// <param name="styleRepository">Repository des styles injecté pour accéder aux données des styles, nécessaires pour les associations avec les titres.</param>
|
||||
public TitreController(ILogger<TitreController> logger, ITitreRepository titreRepository, IArtisteRepository artisteRepository, IStyleRepository styleRepository)
|
||||
/// <param name="titreAdminService">Service Titre Administration injecté gérant Edit et Crée.</param>
|
||||
public TitreController(ILogger<TitreController> logger, ITitreRepository titreRepository, IArtisteRepository artisteRepository, IStyleRepository styleRepository, ITitreAdminService titreAdminService)
|
||||
{
|
||||
this.logger = logger;
|
||||
this.titreRepository = titreRepository;
|
||||
this.artisteRepository = artisteRepository;
|
||||
this.styleRepository = styleRepository;
|
||||
this.titreAdminService = titreAdminService;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -81,6 +87,23 @@ public class TitreController : Controller
|
||||
return this.View(model);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Traite la soumission du formulaire de création d'un titre.
|
||||
/// </summary>
|
||||
/// <param name="model">Données saisies dans le formulaire.</param>
|
||||
/// <returns>Redirection vers Index en cas de succès, réaffichage du formulaire sinon.</returns>
|
||||
[HttpPost]
|
||||
public IActionResult Create(TitreAdminDTO model)
|
||||
{
|
||||
if (this.ModelState.IsValid)
|
||||
{
|
||||
this.titreAdminService.CreerTitre(model);
|
||||
return this.RedirectToAction("Index");
|
||||
}
|
||||
|
||||
return this.View(model);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Affiche le formulaire de modification d'un titre existant dans la vue Edit, en préremplissant les champs avec les données du titre sélectionné. Les listes déroulantes pour les artistes et les styles sont également remplies pour permettre à l'utilisateur de modifier ces associations.
|
||||
/// </summary>
|
||||
@@ -121,6 +144,23 @@ public class TitreController : Controller
|
||||
return this.View(model);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Traite la soumission du formulaire de modification d'un titre.
|
||||
/// </summary>
|
||||
/// <param name="model">Données saisies dans le formulaire.</param>
|
||||
/// <returns>Redirection vers Index en cas de succès, réaffichage du formulaire sinon.</returns>
|
||||
[HttpPost]
|
||||
public IActionResult Edit(TitreAdminDTO model)
|
||||
{
|
||||
if (this.ModelState.IsValid)
|
||||
{
|
||||
this.titreAdminService.ModifierTitre(model);
|
||||
return this.RedirectToAction("Index");
|
||||
}
|
||||
|
||||
return this.View(model);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Affiche la vue de confirmation de suppression d'un titre, en récupérant les détails du titre à supprimer à partir de l'identifiant fourni. Le ViewModel contient les informations essentielles du titre, telles que le libellé et le nom de l'artiste, pour permettre à l'utilisateur de confirmer la suppression.
|
||||
/// </summary>
|
||||
@@ -163,8 +203,9 @@ public class TitreController : Controller
|
||||
if (titre != null)
|
||||
{
|
||||
this.titreRepository.Delete(titre);
|
||||
return this.RedirectToAction("Index");
|
||||
}
|
||||
|
||||
return this.RedirectToAction("Index");
|
||||
return this.View(model);
|
||||
}
|
||||
}
|
||||
13
Webzine.WebApplication/Configuration/Middlewares.cs
Normal file
13
Webzine.WebApplication/Configuration/Middlewares.cs
Normal file
@@ -0,0 +1,13 @@
|
||||
namespace Webzine.WebApplication.Configuration;
|
||||
|
||||
/// <summary>
|
||||
/// Options de seuil pour la détection des opérations EF Core lentes.
|
||||
/// </summary>
|
||||
public class EfPerformanceOptions
|
||||
{
|
||||
/// <summary>
|
||||
/// Obtient ou définit le seuil en millisecondes au-delà duquel une commande SQL est journalisée.
|
||||
/// Valeur par défaut : 200 ms.
|
||||
/// </summary>
|
||||
public int SeuilMs { get; set; } = 200;
|
||||
}
|
||||
@@ -51,6 +51,8 @@ namespace Webzine.WebApplication.Controllers
|
||||
return this.RedirectToAction("Index");
|
||||
}
|
||||
|
||||
this.titreRepository.IncrementNbLectures(titre);
|
||||
|
||||
var vm = new TitreDetail
|
||||
{
|
||||
Details = new TitreContent
|
||||
@@ -110,10 +112,11 @@ namespace Webzine.WebApplication.Controllers
|
||||
if (titre == null)
|
||||
{
|
||||
this.logger.LogWarning("Impossible d'ajouter un like. Titre ID {Id} introuvable.", model.IdTitre);
|
||||
return this.RedirectToAction("Index");
|
||||
}
|
||||
|
||||
titre.NbLikes++;
|
||||
else
|
||||
{
|
||||
this.titreRepository.IncrementNbLikes(titre);
|
||||
}
|
||||
|
||||
return this.RedirectToAction("Details", new { id = model.IdTitre });
|
||||
}
|
||||
|
||||
146
Webzine.WebApplication/Interceptors/EfSlowQueryInterceptor.cs
Normal file
146
Webzine.WebApplication/Interceptors/EfSlowQueryInterceptor.cs
Normal file
@@ -0,0 +1,146 @@
|
||||
namespace Webzine.WebApplication.Interceptors;
|
||||
|
||||
using System.Data.Common;
|
||||
using System.Diagnostics;
|
||||
|
||||
using Configuration;
|
||||
|
||||
using Microsoft.EntityFrameworkCore.Diagnostics;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
/// <summary>
|
||||
/// Intercepteur EF Core qui journalise uniquement les commandes SQL dépassant le seuil configuré.
|
||||
/// Remonte la pile d'appels pour identifier la méthode repository (<c>Webzine.Repository.*</c>) à l'origine de la requête.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// <para>
|
||||
/// <b>Références :</b>
|
||||
/// <list type="bullet">
|
||||
/// <item>
|
||||
/// EF Core interceptors (doc officielle) :
|
||||
/// <see href="https://learn.microsoft.com/en-us/ef/core/logging-events-diagnostics/interceptors"/>
|
||||
/// </item>
|
||||
/// <item>
|
||||
/// <see cref="DbCommandInterceptor"/> API :
|
||||
/// <see href="https://learn.microsoft.com/en-us/dotnet/api/microsoft.entityframeworkcore.diagnostics.dbcommandinterceptor"/>
|
||||
/// </item>
|
||||
/// <item>
|
||||
/// Exemple de slow-query interceptor (SO) :
|
||||
/// <see href="https://medium.com/@sudipdevdev/how-to-detect-and-log-slow-queries-in-entity-framework-core-e2ab71024849"/>
|
||||
/// </item>
|
||||
/// <item>
|
||||
/// <see cref="System.Diagnostics.StackTrace"/> pour remonter l'appelant :
|
||||
/// <see href="https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.stacktrace"/>
|
||||
/// </item>
|
||||
/// <item>
|
||||
/// Enregistrement via <c>AddInterceptors</c> :
|
||||
/// <see href="https://learn.microsoft.com/en-us/ef/core/logging-events-diagnostics/interceptors#registering-interceptors"/>
|
||||
/// </item>
|
||||
/// </list>
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
public class EfSlowQueryInterceptor : DbCommandInterceptor
|
||||
{
|
||||
private readonly ILogger<EfSlowQueryInterceptor> logger;
|
||||
private readonly int seuilMs;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="EfSlowQueryInterceptor"/> class.
|
||||
/// </summary>
|
||||
/// <param name="logger">Le service de journalisation injecté pour suivre les opérations de l'intercepteur.</param>
|
||||
/// <param name="options">Les options de performance EF injectées pour récupérer le seuil de lenteur configuré.</param>
|
||||
public EfSlowQueryInterceptor(ILogger<EfSlowQueryInterceptor> logger, IOptions<EfPerformanceOptions> options)
|
||||
{
|
||||
this.logger = logger;
|
||||
this.seuilMs = options.Value.SeuilMs;
|
||||
|
||||
this.logger.LogDebug("[EfSlowQueryInterceptor] Constructeur appelé — seuil : {SeuilMs} ms.", this.seuilMs);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override DbDataReader ReaderExecuted(DbCommand command, CommandExecutedEventData eventData, DbDataReader result)
|
||||
{
|
||||
this.JournaliserSiLent(eventData.Duration);
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override ValueTask<DbDataReader> ReaderExecutedAsync(DbCommand command, CommandExecutedEventData eventData, DbDataReader result, CancellationToken cancellationToken = default)
|
||||
{
|
||||
this.JournaliserSiLent(eventData.Duration);
|
||||
return ValueTask.FromResult(result);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override int NonQueryExecuted(DbCommand command, CommandExecutedEventData eventData, int result)
|
||||
{
|
||||
this.JournaliserSiLent(eventData.Duration);
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override ValueTask<int> NonQueryExecutedAsync(DbCommand command, CommandExecutedEventData eventData, int result, CancellationToken cancellationToken = default)
|
||||
{
|
||||
this.JournaliserSiLent(eventData.Duration);
|
||||
return ValueTask.FromResult(result);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override object? ScalarExecuted(DbCommand command, CommandExecutedEventData eventData, object? result)
|
||||
{
|
||||
this.JournaliserSiLent(eventData.Duration);
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override ValueTask<object?> ScalarExecutedAsync(DbCommand command, CommandExecutedEventData eventData, object? result, CancellationToken cancellationToken = default)
|
||||
{
|
||||
this.JournaliserSiLent(eventData.Duration);
|
||||
return ValueTask.FromResult(result);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remonte la pile d'appels pour trouver la première méthode dans <c>Webzine.Repository</c>.
|
||||
/// Toutes les requêtes EF Core du projet transitent par ce namespace, ce qui garantit
|
||||
/// un résultat pertinent sans parcourir l'intégralité de la stack.
|
||||
/// </summary>
|
||||
/// <returns>Chaîne <c>Classe.Méthode</c> ou <c>"inconnu"</c> si rien trouvé.</returns>
|
||||
/// <remarks>
|
||||
/// <see cref="StackTrace"/> est instancié uniquement quand le seuil est dépassé,
|
||||
/// ce qui évite tout impact sur le chemin nominal.
|
||||
/// Ref : <see href="https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.stacktrace"/>.
|
||||
/// </remarks>
|
||||
private static string TrouverAppelantRepository()
|
||||
{
|
||||
// skipFrames: 1 pour sauter TrouverAppelantRepository elle-même
|
||||
// fNeedFileInfo: false — on ne veut pas les numéros de ligne (coût supplémentaire inutile)
|
||||
var frames = new StackTrace(skipFrames: 1, fNeedFileInfo: false).GetFrames();
|
||||
|
||||
foreach (var frame in frames)
|
||||
{
|
||||
var methode = frame.GetMethod();
|
||||
if (methode?.DeclaringType?.Namespace?.StartsWith("Webzine.Repository", StringComparison.Ordinal) == true)
|
||||
{
|
||||
return $"{methode.DeclaringType.Name}.{methode.Name}";
|
||||
}
|
||||
}
|
||||
|
||||
return "inconnu";
|
||||
}
|
||||
|
||||
private void JournaliserSiLent(TimeSpan duree)
|
||||
{
|
||||
if (duree.TotalMilliseconds > this.seuilMs)
|
||||
{
|
||||
var appelant = TrouverAppelantRepository();
|
||||
|
||||
this.logger.LogWarning(
|
||||
"[EfSlowQueryInterceptor] Opération EF Core lente détectée — durée réelle : {DureeMs} ms — seuil : {SeuilMs} ms — dépassement : +{Depassement} ms.{NewLine}Appelant : {Appelant}",
|
||||
duree.TotalMilliseconds.ToString("F2"),
|
||||
this.seuilMs,
|
||||
(duree.TotalMilliseconds - this.seuilMs).ToString("F2"),
|
||||
Environment.NewLine,
|
||||
appelant);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
namespace Webzine.WebApplication.Middlewares
|
||||
{
|
||||
using System.Diagnostics;
|
||||
|
||||
public class LogTempsExecutionMiddleware
|
||||
{
|
||||
/// <summary>
|
||||
/// log à chaque requete http.
|
||||
/// </summary>
|
||||
// _next représente le maillon suivant dans la chaîne (le prochain middleware ou le contrôleur)
|
||||
private readonly RequestDelegate next;
|
||||
private readonly ILogger<LogTempsExecutionMiddleware> logger;
|
||||
|
||||
// Le constructeur récupère "_next" et le Logger
|
||||
public LogTempsExecutionMiddleware(RequestDelegate next, ILogger<LogTempsExecutionMiddleware> logger)
|
||||
{
|
||||
this.next = next;
|
||||
this.logger = logger;
|
||||
}
|
||||
|
||||
// méthode appelée à chaque requête HTTP
|
||||
|
||||
/// <summary>
|
||||
/// Middleware chargé de journaliser le cycle de vie d'une requête HTTP (entrée, exécution, sortie et temps de réponse).
|
||||
/// </summary>
|
||||
/// <param name="context">Le contexte HTTP encapsulant toutes les informations de la requête et de la réponse.</param>
|
||||
/// <returns>Une tâche (<see cref="Task"/>) représentant l'opération asynchrone.</returns>
|
||||
public async Task InvokeAsync(HttpContext context)
|
||||
{
|
||||
// (Avant le contrôleur)
|
||||
var chronometre = Stopwatch.StartNew(); // lance le chrono
|
||||
|
||||
// --- IN ---
|
||||
var methode = context.Request.Method;
|
||||
var endpoint = context.Request.Path;
|
||||
var traceId = context.TraceIdentifier; // Identifiant unique généré par .NET
|
||||
|
||||
this.logger.LogInformation("[IN] TraceId: {traceId} | Méthode: {methode} | Endpoint: {endpoint}", traceId, methode, endpoint);
|
||||
|
||||
await this.next(context);
|
||||
|
||||
// (Après le contrôleur)
|
||||
chronometre.Stop(); // arrête le chrono
|
||||
var tempsEcoule = chronometre.ElapsedMilliseconds;
|
||||
|
||||
var httpCode = context.Response.StatusCode; // exemple: 200, 404, 500
|
||||
|
||||
// --- OUT ---
|
||||
if (httpCode >= 500)
|
||||
{
|
||||
this.logger.LogError("[OUT] TraceId: {traceId} | HTTP {httpCode} | Temps: {tempsEcoule} ms | Endpoint: {endpoint}", traceId, httpCode, tempsEcoule, endpoint);
|
||||
}
|
||||
else if (httpCode >= 400)
|
||||
{
|
||||
this.logger.LogWarning("[OUT] TraceId: {traceId} | HTTP {httpCode} | Temps: {tempsEcoule} ms | Endpoint: {endpoint}", traceId, httpCode, tempsEcoule, endpoint);
|
||||
}
|
||||
else
|
||||
{
|
||||
this.logger.LogInformation("[OUT] TraceId: {traceId} | HTTP {httpCode} | Temps: {tempsEcoule} ms | Endpoint: {endpoint}", traceId, httpCode, tempsEcoule, endpoint);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -15,6 +15,7 @@ using Webzine.Repository;
|
||||
using Webzine.Repository.Contracts;
|
||||
using Webzine.WebApplication.Configuration;
|
||||
using Webzine.WebApplication.Extensions;
|
||||
using Webzine.WebApplication.Interceptors;
|
||||
|
||||
// Initiation du logger NLog pour la classe courante afin de pouvoir l'utiliser pour logger des messages d'information, d'erreur, etc avant la construction de l'application.
|
||||
var logger = LogManager.Setup().LoadConfigurationFromAppSettings().GetCurrentClassLogger();
|
||||
@@ -37,6 +38,12 @@ try
|
||||
builder.Logging.ClearProviders();
|
||||
builder.Host.UseNLog();
|
||||
|
||||
builder.Services.Configure<EfPerformanceOptions>(options =>
|
||||
{
|
||||
options.SeuilMs = builder.Configuration.GetValue<int>("EfPerformance:SeuilMs");
|
||||
});
|
||||
builder.Services.AddSingleton<EfSlowQueryInterceptor>();
|
||||
|
||||
// En fonction de la configuration, utilise soit les repositories basés sur une base de données, soit les repositories basés sur des listes locales.
|
||||
var repositoryType = builder.Configuration.GetValue<RepositoryType>("Repository");
|
||||
var seederType = builder.Configuration.GetValue<SeederType>("Seeder");
|
||||
@@ -45,13 +52,21 @@ try
|
||||
{
|
||||
if (builder.Environment.IsProduction())
|
||||
{
|
||||
builder.Services.AddDbContext<WebzineDbContext>(options =>
|
||||
options.UseNpgsql(builder.Configuration.GetConnectionString("PostGreSQLConnection")));
|
||||
builder.Services.AddDbContext<WebzineDbContext>((serviceProvider, options) =>
|
||||
{
|
||||
options
|
||||
.UseNpgsql(builder.Configuration.GetConnectionString("PostGreSQLConnection"))
|
||||
.AddInterceptors(serviceProvider.GetRequiredService<EfSlowQueryInterceptor>());
|
||||
});
|
||||
}
|
||||
else
|
||||
{
|
||||
builder.Services.AddDbContext<WebzineDbContext>(options =>
|
||||
options.UseSqlite(builder.Configuration.GetConnectionString("SqliteConnection")));
|
||||
builder.Services.AddDbContext<WebzineDbContext>((serviceProvider, options) =>
|
||||
{
|
||||
options
|
||||
.UseSqlite(builder.Configuration.GetConnectionString("SqliteConnection"))
|
||||
.AddInterceptors(serviceProvider.GetRequiredService<EfSlowQueryInterceptor>());
|
||||
});
|
||||
}
|
||||
|
||||
builder.Services.AddScoped<DbEntityRepository>();
|
||||
@@ -70,6 +85,7 @@ try
|
||||
}
|
||||
|
||||
builder.Services.AddScoped<IDashboardService, DashboardService>();
|
||||
builder.Services.AddScoped<ITitreAdminService, TitreAdminService>();
|
||||
|
||||
// https://learn.microsoft.com/fr-fr/aspnet/core/performance/response-compression?view=aspnetcore-10.0#configuration
|
||||
// Ajoute le service de compression des réponses HTTP pour réduire la taille des données envoyées au client et améliorer les performances de l'application.
|
||||
@@ -77,6 +93,8 @@ try
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
app.UseMiddleware<Webzine.WebApplication.Middlewares.LogTempsExecutionMiddleware>();
|
||||
|
||||
if (repositoryType == RepositoryType.Db)
|
||||
{
|
||||
using (var scope = app.Services.CreateScope())
|
||||
|
||||
@@ -13,5 +13,8 @@
|
||||
"SqliteConnection": "Data Source=Data/webzine.sqlite",
|
||||
"PostGreSQLConnection": "Host=localhost;Port=5432;Username=admin;Password=admin123;Database=webzine_db"
|
||||
},
|
||||
"AllowedHosts": "*"
|
||||
"AllowedHosts": "*",
|
||||
"EfPerformance": {
|
||||
"SeuilMs": 10
|
||||
}
|
||||
}
|
||||
@@ -28,7 +28,7 @@
|
||||
|
||||
<rules>
|
||||
<!-- Vos logs d'application en Debug+ -->
|
||||
<logger name="Webzine.WebApplication.*" minlevel="Debug" writeTo="allfile,ownfile-web,console" />
|
||||
<logger name="Webzine.WebApplication.*" minlevel="Info" writeTo="allfile,ownfile-web,console" />
|
||||
|
||||
<!-- Logs Microsoft en Warning+ sauf Hosting.Lifetime -->
|
||||
<logger name="Microsoft.*" minlevel="Warn" writeTo="allfile" final="true" />
|
||||
|
||||
Reference in New Issue
Block a user