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 System.Linq;
using GrossesMitainesAPI.Data;
@ -7,30 +10,41 @@ using Microsoft.AspNetCore.Cors;
using GrossesMitainesAPI.Services;
using Microsoft.AspNetCore.Authorization;
namespace GrossesMitainesAPI.Controllers;
#endregion
[EnableCors("_myAllowSpecificOrigins"), ApiController, Route("api/[controller]")]
public class InventoryController : Controller {
#region Constants
private const int AMOUNT_SCROLL = 5;
#endregion
#region DI Fields
private readonly ILogger<InventoryController> _logger;
private readonly InventoryContext _context;
private readonly DatabaseCacheService _cache;
private const int AMOUNT_SCROLL = 5;
#endregion
#region Ctor
public InventoryController(ILogger<InventoryController> logger, InventoryContext context, DatabaseCacheService cache) {
_context = context;
_logger = logger;
_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)
public IEnumerable<ProductViewModel> Get(int? lastId, string? order, string? filterPrice, string? filterState, bool? all) {
bool iscache = false;
IQueryable<Product> ret;
IQueryable<ProductViewModel> ret;
if (_cache.isOk()) {
ret = _cache.queryCache();
ret = _cache.queryCache().Select(x => new ProductViewModel(x));
iscache = true;
}
else ret = _context.Products.AsQueryable();
else ret = _context.Products.AsQueryable().Select(x => new ProductViewModel(x));
switch (filterPrice) {
case "PriceUnder20":
ret = ret.Where(x => x.Price < 20);
@ -63,16 +77,21 @@ public class InventoryController : Controller {
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);
ret = ret.Where(x => x.Status == Product.States.Clearance ||
x.Status == Product.States.Promotion);
break;
default: break;
}
switch (order) {
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;
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;
case "Title":
ret = ret.OrderBy(x => x.Title);
@ -95,9 +114,9 @@ public class InventoryController : Controller {
if (!lastId.HasValue || lastId == 0)
yup = true;
foreach (Product prod in ret.ToList()) {
foreach (ProductViewModel prod in ret.ToList()) {
if (yup && add < AMOUNT_SCROLL || (all.HasValue && all == true)) {
lst.Add(new ProductViewModel(prod));
lst.Add(prod);
add++;
}
if (!yup && prod.Id == lastId)
@ -143,5 +162,7 @@ public class InventoryController : Controller {
_cache.askForRefresh();
return rid;
}
#endregion
}

View File

@ -1,4 +1,7 @@
using GrossesMitainesAPI.Models;
namespace GrossesMitainesAPI.Controllers;
#region Dependencies
using GrossesMitainesAPI.Models;
using System.Linq;
using GrossesMitainesAPI.Data;
using Microsoft.Extensions.Logging;
@ -8,7 +11,8 @@ using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;
using GrossesMitainesAPI.Services;
namespace GrossesMitainesAPI.Controllers;
#endregion
/// <summary>
/// Ce contrôleur ne va pas chercher dans la cache,
/// mais les changements dans celui-ci entrainera
@ -18,16 +22,23 @@ namespace GrossesMitainesAPI.Controllers;
/// </summary>
[EnableCors("_myAllowSpecificOrigins"), ApiController, Route("api/[controller]")]
public class ProductController : ControllerBase {
#region DI Fields
private readonly ILogger<ProductController> _logger;
private readonly InventoryContext _context;
private readonly DatabaseCacheService _cache;
#endregion
#region Ctor
public ProductController(ILogger<ProductController> logger, InventoryContext context, DatabaseCacheService cache) {
_logger = logger;
_context = context;
_cache = cache;
}
#endregion
#region API Methods
[EnableCors("_myAllowSpecificOrigins"), HttpGet(Name = "Product"), AllowAnonymous]
public ActionResult<ProductViewModel> Get(int id) {
Product prod;
@ -85,4 +96,6 @@ public class ProductController : ControllerBase {
_cache.askForRefresh();
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 System.Linq;
using GrossesMitainesAPI.Data;
@ -8,118 +11,153 @@ using Microsoft.AspNetCore.Cors;
using GrossesMitainesAPI.Services;
using System.Collections.Immutable;
namespace GrossesMitainesAPI.Controllers;
#endregion
[EnableCors("_myAllowSpecificOrigins"), ApiController, Route("api/[controller]")]
public class SearchController : Controller {
#region Constants
private const int PREVIEW = 4;
#endregion
#region DI Fields
private readonly ILogger<SearchController> _logger;
private readonly InventoryContext _context;
private readonly DatabaseCacheService _cache;
private Product[]? _searchCache = null;
private const int PREVIEW = 4;
private IQueryable<Product>? _searchCache = null;
#endregion
#region Ctor
public SearchController(ILogger<SearchController> logger, InventoryContext context, DatabaseCacheService cache) {
_logger = logger;
_context = context;
_cache = cache;
if (_cache.isOk()) // Se fait une copie de la cache si elle est fonctionnelle.
_searchCache = _cache.GetCacheCopy();
if (_cache.isOk())
_searchCache = _cache.queryCache();
}
#endregion
#region API Methods
[EnableCors("_myAllowSpecificOrigins"), HttpGet(Name = "Search"), AllowAnonymous]
public IEnumerable<Product> Get(string query, bool? preview, bool? deep) {
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.
public IEnumerable<ProductViewModel> Get(string query, bool? preview, string? filterPrice, string? filterState, string? order = "Relevance") {
if (preview.HasValue && preview == true)
products = _context.Products.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 _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;
return _searchCache is null ?
_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> SearchCached(string query, bool? preview, bool? deep) {
List<Product> products = new();
#endregion
#region Private Methods
private List<ProductViewModel> Search(string query, string? filterPrice, string? filterState, string? order) {
List<ProductViewModel> products = new();
query = query.Trim();
try {
query = query.ToLower();
IQueryable<ProductViewModel> ret;
if (_searchCache is null) {
_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.
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) {
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() + " ",
sCat = prod.Category.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 + " "))
else if (sDesc.Contains(" " + query + " "))
desc.Add(prod);
else if (sCat.Contains(query))
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))
break;
case "Price":
ret = ret.OrderBy(x => x.Status == Product.States.Promotion || x.Status == Product.States.Clearance ? x.PromoPrice : x.Price);
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);
}
break;
}
} catch (Exception e) {
_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;
}
#endregion
}

View File

@ -1,17 +1,28 @@
using GrossesMitainesAPI.Data;
namespace GrossesMitainesAPI.Services;
#region Dependencies
using GrossesMitainesAPI.Data;
using GrossesMitainesAPI.Models;
using Microsoft.EntityFrameworkCore;
namespace GrossesMitainesAPI.Services {
#endregion
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 ILogger<DatabaseCacheService> _logger;
#endregion
#region Fields
private Product[] _cache = new Product[1];
private Dictionary<uint, uint> _hits = new();
private bool _ok = false, _needUpd = true;
private PeriodicTimer _timer = new PeriodicTimer(TimeSpan.FromSeconds(10));
#endregion
#region Ctor
public DatabaseCacheService(ILogger<DatabaseCacheService> logger, IServiceScopeFactory scopeFactory) {
_contextFactory = scopeFactory;
_logger = logger;
@ -20,16 +31,18 @@ namespace GrossesMitainesAPI.Services {
UpdateJob();
}
#endregion
#region Internal Methods
private async void UpdateJob() {
while (await _timer.WaitForNextTickAsync()) {
if (_needUpd) {
_ok = UpdateCache();
_needUpd = !_ok;
}
if (_hits.Count > 0 && _ok) {
UpdateMetrics();
//_needUpd = true;
}
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() {
@ -48,7 +61,6 @@ namespace GrossesMitainesAPI.Services {
}
return true;
}
private bool UpdateMetrics() {
try {
Dictionary<uint, uint> hits;
@ -60,12 +72,8 @@ namespace GrossesMitainesAPI.Services {
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);
foreach (var x in hits)
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();
}
@ -76,6 +84,9 @@ namespace GrossesMitainesAPI.Services {
return true;
}
#endregion
#region Public Methods
public bool isOk() { return _ok; }
public void askForRefresh() { _needUpd = true; }
public void addHit(uint id) {
@ -89,7 +100,6 @@ namespace GrossesMitainesAPI.Services {
public Product[]? GetCacheCopy() {
if (!_ok)
return null;
Product[] copy;
try {
lock (_cache) {
@ -102,7 +112,6 @@ namespace GrossesMitainesAPI.Services {
}
return copy;
}
public IQueryable<Product> queryCache() {
if (!_ok)
return null;
@ -113,5 +122,6 @@ namespace GrossesMitainesAPI.Services {
return null;
}
}
}
#endregion
}