react-version #1

Merged
memartel_loc merged 290 commits from react-version into main 2023-11-04 09:48:15 -04:00
4 changed files with 280 additions and 198 deletions
Showing only changes of commit 8a5b00624e - Show all commits

View File

@ -1,4 +1,7 @@
using Microsoft.AspNetCore.Mvc; namespace GrossesMitainesAPI.Controllers;
#region Dependencies
using Microsoft.AspNetCore.Mvc;
using GrossesMitainesAPI.Models; using GrossesMitainesAPI.Models;
using System.Linq; using System.Linq;
using GrossesMitainesAPI.Data; using GrossesMitainesAPI.Data;
@ -7,30 +10,41 @@ using Microsoft.AspNetCore.Cors;
using GrossesMitainesAPI.Services; using GrossesMitainesAPI.Services;
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authorization;
namespace GrossesMitainesAPI.Controllers; #endregion
[EnableCors("_myAllowSpecificOrigins"), ApiController, Route("api/[controller]")] [EnableCors("_myAllowSpecificOrigins"), ApiController, Route("api/[controller]")]
public class InventoryController : Controller { public class InventoryController : Controller {
#region Constants
private const int AMOUNT_SCROLL = 5;
#endregion
#region DI Fields
private readonly ILogger<InventoryController> _logger; private readonly ILogger<InventoryController> _logger;
private readonly InventoryContext _context; private readonly InventoryContext _context;
private readonly DatabaseCacheService _cache; private readonly DatabaseCacheService _cache;
private const int AMOUNT_SCROLL = 5;
#endregion
#region Ctor
public InventoryController(ILogger<InventoryController> logger, InventoryContext context, DatabaseCacheService cache) { public InventoryController(ILogger<InventoryController> logger, InventoryContext context, DatabaseCacheService cache) {
_context = context; _context = context;
_logger = logger; _logger = logger;
_cache = cache; _cache = cache;
} }
#endregion
#region API Methods
[EnableCors("_myAllowSpecificOrigins"), HttpGet(Name = "Inventory"), AllowAnonymous] // Pour faire des calls async par paquet de AMOUNT (5) (pour du loading en scrollant) [EnableCors("_myAllowSpecificOrigins"), HttpGet(Name = "Inventory"), AllowAnonymous] // Pour faire des calls async par paquet de AMOUNT (5) (pour du loading en scrollant)
public IEnumerable<ProductViewModel> Get(int? lastId, string? order, string? filterPrice, string? filterState, bool? all) { public IEnumerable<ProductViewModel> Get(int? lastId, string? order, string? filterPrice, string? filterState, bool? all) {
bool iscache = false; bool iscache = false;
IQueryable<Product> ret; IQueryable<ProductViewModel> ret;
if (_cache.isOk()) { if (_cache.isOk()) {
ret = _cache.queryCache(); ret = _cache.queryCache().Select(x => new ProductViewModel(x));
iscache = true; iscache = true;
} }
else ret = _context.Products.AsQueryable(); else ret = _context.Products.AsQueryable().Select(x => new ProductViewModel(x));
switch (filterPrice) { switch (filterPrice) {
case "PriceUnder20": case "PriceUnder20":
ret = ret.Where(x => x.Price < 20); ret = ret.Where(x => x.Price < 20);
@ -63,16 +77,21 @@ public class InventoryController : Controller {
ret = ret.Where(x => x.Status == Product.States.Discontinued); ret = ret.Where(x => x.Status == Product.States.Discontinued);
break; break;
case "isPromoted": case "isPromoted":
ret = ret.Where(x => x.Status == Product.States.Clearance || x.Status == Product.States.Promotion); ret = ret.Where(x => x.Status == Product.States.Clearance ||
x.Status == Product.States.Promotion);
break; break;
default: break; default: break;
} }
switch (order) { switch (order) {
case "Price": case "Price":
ret = ret.OrderBy(x => x.Status == Product.States.Promotion || x.Status == Product.States.Clearance ? x.PromoPrice : x.Price); ret = ret.OrderBy(x => x.Status == Product.States.Promotion ||
x.Status == Product.States.Clearance ?
x.PromoPrice : x.Price);
break; break;
case "PriceDesc": case "PriceDesc":
ret = ret.OrderByDescending(x => x.Status == Product.States.Promotion || x.Status == Product.States.Clearance ? x.PromoPrice : x.Price); ret = ret.OrderByDescending(x => x.Status == Product.States.Promotion ||
x.Status == Product.States.Clearance ?
x.PromoPrice : x.Price);
break; break;
case "Title": case "Title":
ret = ret.OrderBy(x => x.Title); ret = ret.OrderBy(x => x.Title);
@ -95,9 +114,9 @@ public class InventoryController : Controller {
if (!lastId.HasValue || lastId == 0) if (!lastId.HasValue || lastId == 0)
yup = true; yup = true;
foreach (Product prod in ret.ToList()) { foreach (ProductViewModel prod in ret.ToList()) {
if (yup && add < AMOUNT_SCROLL || (all.HasValue && all == true)) { if (yup && add < AMOUNT_SCROLL || (all.HasValue && all == true)) {
lst.Add(new ProductViewModel(prod)); lst.Add(prod);
add++; add++;
} }
if (!yup && prod.Id == lastId) if (!yup && prod.Id == lastId)
@ -143,5 +162,7 @@ public class InventoryController : Controller {
_cache.askForRefresh(); _cache.askForRefresh();
return rid; return rid;
} }
#endregion
} }

View File

@ -1,4 +1,7 @@
using GrossesMitainesAPI.Models; namespace GrossesMitainesAPI.Controllers;
#region Dependencies
using GrossesMitainesAPI.Models;
using System.Linq; using System.Linq;
using GrossesMitainesAPI.Data; using GrossesMitainesAPI.Data;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
@ -8,7 +11,8 @@ using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using GrossesMitainesAPI.Services; using GrossesMitainesAPI.Services;
namespace GrossesMitainesAPI.Controllers; #endregion
/// <summary> /// <summary>
/// Ce contrôleur ne va pas chercher dans la cache, /// Ce contrôleur ne va pas chercher dans la cache,
/// mais les changements dans celui-ci entrainera /// mais les changements dans celui-ci entrainera
@ -18,16 +22,23 @@ namespace GrossesMitainesAPI.Controllers;
/// </summary> /// </summary>
[EnableCors("_myAllowSpecificOrigins"), ApiController, Route("api/[controller]")] [EnableCors("_myAllowSpecificOrigins"), ApiController, Route("api/[controller]")]
public class ProductController : ControllerBase { public class ProductController : ControllerBase {
#region DI Fields
private readonly ILogger<ProductController> _logger; private readonly ILogger<ProductController> _logger;
private readonly InventoryContext _context; private readonly InventoryContext _context;
private readonly DatabaseCacheService _cache; private readonly DatabaseCacheService _cache;
#endregion
#region Ctor
public ProductController(ILogger<ProductController> logger, InventoryContext context, DatabaseCacheService cache) { public ProductController(ILogger<ProductController> logger, InventoryContext context, DatabaseCacheService cache) {
_logger = logger; _logger = logger;
_context = context; _context = context;
_cache = cache; _cache = cache;
} }
#endregion
#region API Methods
[EnableCors("_myAllowSpecificOrigins"), HttpGet(Name = "Product"), AllowAnonymous] [EnableCors("_myAllowSpecificOrigins"), HttpGet(Name = "Product"), AllowAnonymous]
public ActionResult<ProductViewModel> Get(int id) { public ActionResult<ProductViewModel> Get(int id) {
Product prod; Product prod;
@ -85,4 +96,6 @@ public class ProductController : ControllerBase {
_cache.askForRefresh(); _cache.askForRefresh();
return id; return id;
} }
#endregion
} }

View File

@ -1,4 +1,7 @@
using Microsoft.AspNetCore.Mvc; namespace GrossesMitainesAPI.Controllers;
#region Dependencies
using Microsoft.AspNetCore.Mvc;
using GrossesMitainesAPI.Models; using GrossesMitainesAPI.Models;
using System.Linq; using System.Linq;
using GrossesMitainesAPI.Data; using GrossesMitainesAPI.Data;
@ -8,118 +11,153 @@ using Microsoft.AspNetCore.Cors;
using GrossesMitainesAPI.Services; using GrossesMitainesAPI.Services;
using System.Collections.Immutable; using System.Collections.Immutable;
namespace GrossesMitainesAPI.Controllers; #endregion
[EnableCors("_myAllowSpecificOrigins"), ApiController, Route("api/[controller]")] [EnableCors("_myAllowSpecificOrigins"), ApiController, Route("api/[controller]")]
public class SearchController : Controller { public class SearchController : Controller {
#region Constants
private const int PREVIEW = 4;
#endregion
#region DI Fields
private readonly ILogger<SearchController> _logger; private readonly ILogger<SearchController> _logger;
private readonly InventoryContext _context; private readonly InventoryContext _context;
private readonly DatabaseCacheService _cache; private readonly DatabaseCacheService _cache;
private Product[]? _searchCache = null; private IQueryable<Product>? _searchCache = null;
private const int PREVIEW = 4;
#endregion
#region Ctor
public SearchController(ILogger<SearchController> logger, InventoryContext context, DatabaseCacheService cache) { public SearchController(ILogger<SearchController> logger, InventoryContext context, DatabaseCacheService cache) {
_logger = logger; _logger = logger;
_context = context; _context = context;
_cache = cache; _cache = cache;
if (_cache.isOk()) // Se fait une copie de la cache si elle est fonctionnelle. if (_cache.isOk())
_searchCache = _cache.GetCacheCopy(); _searchCache = _cache.queryCache();
} }
#endregion
#region API Methods
[EnableCors("_myAllowSpecificOrigins"), HttpGet(Name = "Search"), AllowAnonymous] [EnableCors("_myAllowSpecificOrigins"), HttpGet(Name = "Search"), AllowAnonymous]
public IEnumerable<Product> Get(string query, bool? preview, bool? deep) { public IEnumerable<ProductViewModel> Get(string query, bool? preview, string? filterPrice, string? filterState, string? order = "Relevance") {
if (_searchCache is not null) if (preview.HasValue && preview == true)
return SearchCached(query, preview, deep); return _searchCache is null ?
else return SearchDirect(query, preview, deep); _context.Products.Where(x => x.Title.Contains(query)).Take(PREVIEW).Select(x => new ProductViewModel(x)).ToList() :
_searchCache.Where(x => x.Title.Contains(query)).Take(PREVIEW).Select(x => new ProductViewModel(x)).ToList();
return Search(query, filterPrice, filterState, order);
} }
private List<Product> SearchDirect(string query, bool? preview, bool? deep) { #endregion
List<Product> products = new();
#region Private Methods
private List<ProductViewModel> Search(string query, string? filterPrice, string? filterState, string? order) {
List<ProductViewModel> products = new();
query = query.Trim(); query = query.Trim();
try { // Pour faire une liste priorisée. try {
if (preview.HasValue && preview == true) query = query.ToLower();
products = _context.Products.Where(x => x.Title.Contains(query)).Take(PREVIEW).ToList(); IQueryable<ProductViewModel> ret;
else { if (_searchCache is null) {
if (deep.HasValue && deep == true) { _logger.LogError(8, "Erreur de cache.");
List<Product> title = new(), desc = new(), cat = new(); ret = _context.Products.AsQueryable().Select(x => new ProductViewModel(x));
query = query.ToLower(); } else ret = _searchCache.Select(x => new ProductViewModel(x));
foreach (Product prod in _context.Products.ToArray()) {
switch (filterPrice) {
case "PriceUnder20":
ret = ret.Where(x => x.Price < 20);
break;
case "Price20to49":
ret = ret.Where(x => x.Price >= 20 && x.Price < 50);
break;
case "Price50to99":
ret = ret.Where(x => x.Price >= 50 && x.Price < 100);
break;
case "PriceOver100":
ret = ret.Where(x => x.Price >= 100);
break;
default: break;
}
switch (filterState) {
case "isAvailable":
ret = ret.Where(x => x.Status == Product.States.Available);
break;
case "isUnavailable":
ret = ret.Where(x => x.Status == Product.States.Unavailable);
break;
case "isBackOrder":
ret = ret.Where(x => x.Status == Product.States.BackOrder);
break; ;
case "isClearance":
ret = ret.Where(x => x.Status == Product.States.Clearance);
break;
case "isDiscontinued":
ret = ret.Where(x => x.Status == Product.States.Discontinued);
break;
case "isPromoted":
ret = ret.Where(x => x.Status == Product.States.Clearance || x.Status == Product.States.Promotion);
break;
default: break;
}
switch (order) {
case "Relevance":
List<ProductViewModel> title = new(), desc = new(), cat = new();
foreach (ProductViewModel prod in ret.ToList()) {
string sTitle = prod.Title.Replace(",", " ").ToLower() + " ", string sTitle = prod.Title.Replace(",", " ").ToLower() + " ",
sCat = prod.Category.ToLower() + " ", sCat = " " + prod.Category.ToLower() + " ",
sDesc = prod.Description.Replace(".", " ").Replace(",", " ").ToLower() + " "; sDesc = prod.Description.Replace(".", " ").Replace(",", " ").ToLower() + " ";
if (sTitle.StartsWith(query)) if (sTitle.StartsWith(query))
products.Add(prod); products.Add(prod);
else if (sTitle.Contains(" " + query + " ")) else if (sTitle.Contains(" " + query + " "))
title.Add(prod); title.Add(prod);
else if (sDesc.StartsWith(query) || sDesc.Contains(" " + query + " ")) else if (sDesc.Contains(" " + query + " "))
desc.Add(prod); desc.Add(prod);
else if (sCat.Contains(query)) else if (sCat.Contains(" " + query + " "))
cat.Add(prod); cat.Add(prod);
} }
products.AddRange(title); products.AddRange(title);
products.AddRange(desc); products.AddRange(desc);
products.AddRange(cat); products.AddRange(cat);
} else { break;
products = _context.Products.Where(x => x.Title.Contains(query)).ToList(); case "Price":
foreach (Product prod in _context.Products.Where(x => x.Description.Contains(query)).ToArray()) ret = ret.OrderBy(x => x.Status == Product.States.Promotion || x.Status == Product.States.Clearance ? x.PromoPrice : x.Price);
if (!products.Contains(prod)) goto default;
case "PriceDesc":
ret = ret.OrderByDescending(x => x.Status == Product.States.Promotion || x.Status == Product.States.Clearance ? x.PromoPrice : x.Price);
goto default;
case "Title":
ret = ret.OrderBy(x => x.Title);
goto default;
case "TitleDesc":
ret = ret.OrderByDescending(x => x.Title);
goto default;
case "Category":
ret = ret.OrderBy(x => x.Category);
goto default;
case "CategoryDesc":
ret = ret.OrderByDescending(x => x.Category);
goto default;
default:
foreach (ProductViewModel prod in ret.ToList()) {
string sTitle = prod.Title.Replace(",", " ").ToLower() + " ",
sCat = " " + prod.Category.ToLower() + " ",
sDesc = prod.Description.Replace(".", " ").Replace(",", " ").ToLower() + " ";
if (sTitle.StartsWith(query)
|| sTitle.Contains(" " + query + " ")
|| sDesc.StartsWith(query) || sDesc.Contains(" " + query + " ")
|| sCat.Contains(" " + query + " "))
products.Add(prod); products.Add(prod);
foreach (Product prod in _context.Products.Where(x => x.Category.Contains(query)).ToArray()) }
if (!products.Contains(prod)) break;
products.Add(prod);
}
} }
} catch (Exception e) { } catch (Exception e) {
_logger.LogError(8, e.Message); _logger.LogError(8, e.Message);
return new List<ProductViewModel>();
} }
return products; return products;
} }
private List<Product> SearchCached(string query, bool? preview, bool? deep) { #endregion
List<Product> products = new();
query = query.Trim();
if (_searchCache is null) {
_logger.LogError(8, "Erreur de cache.");
return SearchDirect(query, preview, deep); // Fallback vers version non-cached en cas d'erreur.
}
try { // Pour faire une liste priorisée.
if (preview.HasValue && preview == true)
products = _searchCache.Where(x => x.Title.Contains(query)).Take(PREVIEW).ToList();
else {
if (deep.HasValue && deep == true) {
List<Product> title = new(), desc = new(), cat = new();
query = query.ToLower();
foreach (Product prod in _searchCache) {
string sTitle = prod.Title.Replace(",", " ").ToLower() + " ",
sCat = prod.Category.ToLower() + " ",
sDesc = prod.Description.Replace(".", " ").Replace(",", " ").ToLower() + " ";
if (sTitle.StartsWith(query))
products.Add(prod);
else if (sTitle.Contains(" " + query + " "))
title.Add(prod);
else if (sDesc.StartsWith(query) || sDesc.Contains(" " + query + " "))
desc.Add(prod);
else if (sCat.Contains(query))
cat.Add(prod);
}
products.AddRange(title);
products.AddRange(desc);
products.AddRange(cat);
} else {
products = _searchCache.Where(x => x.Title.Contains(query)).ToList();
foreach (Product prod in _searchCache.Where(x => x.Description.Contains(query)).ToArray())
if (!products.Contains(prod))
products.Add(prod);
foreach (Product prod in _searchCache.Where(x => x.Category.Contains(query)).ToArray())
if (!products.Contains(prod))
products.Add(prod);
}
}
} catch (Exception e) {
_logger.LogError(8, e.Message);
return SearchDirect(query, preview, deep); // Fallback vers version non-cached en cas d'erreur.
}
return products;
}
} }

View File

@ -1,117 +1,127 @@
using GrossesMitainesAPI.Data; namespace GrossesMitainesAPI.Services;
#region Dependencies
using GrossesMitainesAPI.Data;
using GrossesMitainesAPI.Models; using GrossesMitainesAPI.Models;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
namespace GrossesMitainesAPI.Services { #endregion
public class DatabaseCacheService {
private readonly IServiceScopeFactory _contextFactory; // https://entityframeworkcore.com/knowledge-base/51939451/how-to-use-a-database-context-in-a-singleton-service-
private readonly ILogger<DatabaseCacheService> _logger;
private Product[] _cache = new Product[1]; public class DatabaseCacheService {
private Dictionary<uint, uint> _hits = new(); #region DI
private bool _ok = false, _needUpd = true; private readonly IServiceScopeFactory _contextFactory; // https://entityframeworkcore.com/knowledge-base/51939451/how-to-use-a-database-context-in-a-singleton-service-
private PeriodicTimer _timer = new PeriodicTimer(TimeSpan.FromSeconds(10)); private readonly ILogger<DatabaseCacheService> _logger;
public DatabaseCacheService(ILogger<DatabaseCacheService> logger, IServiceScopeFactory scopeFactory) { #endregion
_contextFactory = scopeFactory;
_logger = logger;
_ok = UpdateCache();
_needUpd = !_ok;
UpdateJob();
}
private async void UpdateJob() { #region Fields
while (await _timer.WaitForNextTickAsync()) { private Product[] _cache = new Product[1];
if (_needUpd) { private Dictionary<uint, uint> _hits = new();
_ok = UpdateCache(); private bool _ok = false, _needUpd = true;
_needUpd = !_ok; private PeriodicTimer _timer = new PeriodicTimer(TimeSpan.FromSeconds(10));
}
if (_hits.Count > 0 && _ok) { #endregion
UpdateMetrics();
//_needUpd = true; #region Ctor
} public DatabaseCacheService(ILogger<DatabaseCacheService> logger, IServiceScopeFactory scopeFactory) {
} _contextFactory = scopeFactory;
} _logger = logger;
private bool UpdateCache() { _ok = UpdateCache();
try { _needUpd = !_ok;
Product[] prods; UpdateJob();
using (var scope = _contextFactory.CreateScope()) { }
var db = scope.ServiceProvider.GetRequiredService<InventoryContext>();
prods = db.Products.ToArray(); #endregion
}
lock (_cache) { #region Internal Methods
_cache = prods; private async void UpdateJob() {
} while (await _timer.WaitForNextTickAsync()) {
} catch (Exception e) { if (_needUpd) {
_logger.LogError(e, "Erreur de mise à jour de cache."); _ok = UpdateCache();
return false; _needUpd = !_ok;
}
return true;
}
private bool UpdateMetrics() {
try {
Dictionary<uint, uint> hits;
lock (_hits) {
hits = new(_hits);
_hits.Clear();
}
List<uint> ids = hits.Keys.ToList();
using (var scope = _contextFactory.CreateScope()) {
var db = scope.ServiceProvider.GetRequiredService<InventoryContext>();
List<Product> lst = db.Products.Where(x => ids.Contains((uint)x.Id)).ToList();
foreach (var x in hits) {
//Product prod = lst.First(x => x.Id == x.Id);
lst.First(x => x.Id == x.Id).Hits += x.Value;
// prod.Hits = prod.Hits + x.Value;
// db.Products.Update(prod);
}
db.UpdateRange(lst);
db.SaveChanges();
}
} catch (Exception e) {
_logger.LogError(e, "Erreur de mise à jour de cache.");
return false;
}
return true;
}
public bool isOk() { return _ok; }
public void askForRefresh() { _needUpd = true; }
public void addHit(uint id) {
lock (_hits) {
if (_hits.ContainsKey(id))
_hits[id] = _hits[id] + 1;
else _hits[id] = 1;
}
}
public Product[]? GetCacheCopy() {
if (!_ok)
return null;
Product[] copy;
try {
lock (_cache) {
copy = new Product[_cache.Length];
_cache.CopyTo(copy, 0);
}
} catch (Exception e) {
_logger.LogError(e, "Erreur de copie de cache.");
return null;
}
return copy;
}
public IQueryable<Product> queryCache() {
if (!_ok)
return null;
try {
return _cache.AsQueryable();
} catch (Exception e) {
_logger.LogError(e, "Erreur de cache.");
return null;
} }
if (_hits.Count > 0 && _ok)
UpdateMetrics(); // les updates de metrics ne déclencheront pas d'update de cache
// puisque les clients ne voient pas les métriques.
} }
} }
private bool UpdateCache() {
try {
Product[] prods;
using (var scope = _contextFactory.CreateScope()) {
var db = scope.ServiceProvider.GetRequiredService<InventoryContext>();
prods = db.Products.ToArray();
}
lock (_cache) {
_cache = prods;
}
} catch (Exception e) {
_logger.LogError(e, "Erreur de mise à jour de cache.");
return false;
}
return true;
}
private bool UpdateMetrics() {
try {
Dictionary<uint, uint> hits;
lock (_hits) {
hits = new(_hits);
_hits.Clear();
}
List<uint> ids = hits.Keys.ToList();
using (var scope = _contextFactory.CreateScope()) {
var db = scope.ServiceProvider.GetRequiredService<InventoryContext>();
List<Product> lst = db.Products.Where(x => ids.Contains((uint)x.Id)).ToList();
foreach (var x in hits)
lst.First(x => x.Id == x.Id).Hits += x.Value;
db.UpdateRange(lst);
db.SaveChanges();
}
} catch (Exception e) {
_logger.LogError(e, "Erreur de mise à jour de cache.");
return false;
}
return true;
}
#endregion
#region Public Methods
public bool isOk() { return _ok; }
public void askForRefresh() { _needUpd = true; }
public void addHit(uint id) {
lock (_hits) {
if (_hits.ContainsKey(id))
_hits[id] = _hits[id] + 1;
else _hits[id] = 1;
}
}
public Product[]? GetCacheCopy() {
if (!_ok)
return null;
Product[] copy;
try {
lock (_cache) {
copy = new Product[_cache.Length];
_cache.CopyTo(copy, 0);
}
} catch (Exception e) {
_logger.LogError(e, "Erreur de copie de cache.");
return null;
}
return copy;
}
public IQueryable<Product> queryCache() {
if (!_ok)
return null;
try {
return _cache.AsQueryable();
} catch (Exception e) {
_logger.LogError(e, "Erreur de cache.");
return null;
}
}
#endregion
} }