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)
return SearchCached(query, preview, deep);
else return SearchDirect(query, preview, deep);
}
private List<Product> SearchDirect(string query, bool? preview, bool? deep) {
List<Product> products = new();
query = query.Trim();
try { // Pour faire une liste priorisée.
if (preview.HasValue && preview == true) if (preview.HasValue && preview == true)
products = _context.Products.Where(x => x.Title.Contains(query)).Take(PREVIEW).ToList(); return _searchCache is null ?
else { _context.Products.Where(x => x.Title.Contains(query)).Take(PREVIEW).Select(x => new ProductViewModel(x)).ToList() :
if (deep.HasValue && deep == true) { _searchCache.Where(x => x.Title.Contains(query)).Take(PREVIEW).Select(x => new ProductViewModel(x)).ToList();
List<Product> title = new(), desc = new(), cat = new(); return Search(query, filterPrice, filterState, order);
query = query.ToLower();
foreach (Product prod in _context.Products.ToArray()) {
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 = _context.Products.Where(x => x.Title.Contains(query)).ToList();
foreach (Product prod in _context.Products.Where(x => x.Description.Contains(query)).ToArray())
if (!products.Contains(prod))
products.Add(prod);
foreach (Product prod in _context.Products.Where(x => x.Category.Contains(query)).ToArray())
if (!products.Contains(prod))
products.Add(prod);
}
}
} catch (Exception e) {
_logger.LogError(8, e.Message);
}
return products;
} }
private List<Product> SearchCached(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 {
query = query.ToLower();
IQueryable<ProductViewModel> ret;
if (_searchCache is null) { if (_searchCache is null) {
_logger.LogError(8, "Erreur de cache."); _logger.LogError(8, "Erreur de cache.");
return SearchDirect(query, preview, deep); // Fallback vers version non-cached en cas d'erreur. ret = _context.Products.AsQueryable().Select(x => new ProductViewModel(x));
} else ret = _searchCache.Select(x => new ProductViewModel(x));
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;
} }
try { // Pour faire une liste priorisée. switch (filterState) {
if (preview.HasValue && preview == true) case "isAvailable":
products = _searchCache.Where(x => x.Title.Contains(query)).Take(PREVIEW).ToList(); ret = ret.Where(x => x.Status == Product.States.Available);
else { break;
if (deep.HasValue && deep == true) { case "isUnavailable":
List<Product> title = new(), desc = new(), cat = new(); ret = ret.Where(x => x.Status == Product.States.Unavailable);
query = query.ToLower(); break;
foreach (Product prod in _searchCache) { 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 = _searchCache.Where(x => x.Title.Contains(query)).ToList(); case "Price":
foreach (Product prod in _searchCache.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;
products.Add(prod); case "PriceDesc":
foreach (Product prod in _searchCache.Where(x => x.Category.Contains(query)).ToArray()) ret = ret.OrderByDescending(x => x.Status == Product.States.Promotion || x.Status == Product.States.Clearance ? x.PromoPrice : x.Price);
if (!products.Contains(prod)) 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);
} }
break;
} }
} catch (Exception e) { } catch (Exception e) {
_logger.LogError(8, e.Message); _logger.LogError(8, e.Message);
return SearchDirect(query, preview, deep); // Fallback vers version non-cached en cas d'erreur. return new List<ProductViewModel>();
} }
return products; return products;
} }
#endregion
} }

View File

@ -1,17 +1,28 @@
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 {
public class DatabaseCacheService {
#region DI
private readonly IServiceScopeFactory _contextFactory; // https://entityframeworkcore.com/knowledge-base/51939451/how-to-use-a-database-context-in-a-singleton-service- 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 readonly ILogger<DatabaseCacheService> _logger;
#endregion
#region Fields
private Product[] _cache = new Product[1]; private Product[] _cache = new Product[1];
private Dictionary<uint, uint> _hits = new(); private Dictionary<uint, uint> _hits = new();
private bool _ok = false, _needUpd = true; private bool _ok = false, _needUpd = true;
private PeriodicTimer _timer = new PeriodicTimer(TimeSpan.FromSeconds(10)); private PeriodicTimer _timer = new PeriodicTimer(TimeSpan.FromSeconds(10));
#endregion
#region Ctor
public DatabaseCacheService(ILogger<DatabaseCacheService> logger, IServiceScopeFactory scopeFactory) { public DatabaseCacheService(ILogger<DatabaseCacheService> logger, IServiceScopeFactory scopeFactory) {
_contextFactory = scopeFactory; _contextFactory = scopeFactory;
_logger = logger; _logger = logger;
@ -20,16 +31,18 @@ namespace GrossesMitainesAPI.Services {
UpdateJob(); UpdateJob();
} }
#endregion
#region Internal Methods
private async void UpdateJob() { private async void UpdateJob() {
while (await _timer.WaitForNextTickAsync()) { while (await _timer.WaitForNextTickAsync()) {
if (_needUpd) { if (_needUpd) {
_ok = UpdateCache(); _ok = UpdateCache();
_needUpd = !_ok; _needUpd = !_ok;
} }
if (_hits.Count > 0 && _ok) { if (_hits.Count > 0 && _ok)
UpdateMetrics(); UpdateMetrics(); // les updates de metrics ne déclencheront pas d'update de cache
//_needUpd = true; // puisque les clients ne voient pas les métriques.
}
} }
} }
private bool UpdateCache() { private bool UpdateCache() {
@ -48,7 +61,6 @@ namespace GrossesMitainesAPI.Services {
} }
return true; return true;
} }
private bool UpdateMetrics() { private bool UpdateMetrics() {
try { try {
Dictionary<uint, uint> hits; Dictionary<uint, uint> hits;
@ -60,12 +72,8 @@ namespace GrossesMitainesAPI.Services {
using (var scope = _contextFactory.CreateScope()) { using (var scope = _contextFactory.CreateScope()) {
var db = scope.ServiceProvider.GetRequiredService<InventoryContext>(); var db = scope.ServiceProvider.GetRequiredService<InventoryContext>();
List<Product> lst = db.Products.Where(x => ids.Contains((uint)x.Id)).ToList(); List<Product> lst = db.Products.Where(x => ids.Contains((uint)x.Id)).ToList();
foreach (var x in hits) { foreach (var x in hits)
//Product prod = lst.First(x => x.Id == x.Id);
lst.First(x => x.Id == x.Id).Hits += x.Value; lst.First(x => x.Id == x.Id).Hits += x.Value;
// prod.Hits = prod.Hits + x.Value;
// db.Products.Update(prod);
}
db.UpdateRange(lst); db.UpdateRange(lst);
db.SaveChanges(); db.SaveChanges();
} }
@ -76,6 +84,9 @@ namespace GrossesMitainesAPI.Services {
return true; return true;
} }
#endregion
#region Public Methods
public bool isOk() { return _ok; } public bool isOk() { return _ok; }
public void askForRefresh() { _needUpd = true; } public void askForRefresh() { _needUpd = true; }
public void addHit(uint id) { public void addHit(uint id) {
@ -89,7 +100,6 @@ namespace GrossesMitainesAPI.Services {
public Product[]? GetCacheCopy() { public Product[]? GetCacheCopy() {
if (!_ok) if (!_ok)
return null; return null;
Product[] copy; Product[] copy;
try { try {
lock (_cache) { lock (_cache) {
@ -102,7 +112,6 @@ namespace GrossesMitainesAPI.Services {
} }
return copy; return copy;
} }
public IQueryable<Product> queryCache() { public IQueryable<Product> queryCache() {
if (!_ok) if (!_ok)
return null; return null;
@ -113,5 +122,6 @@ namespace GrossesMitainesAPI.Services {
return null; return null;
} }
} }
}
#endregion
} }