Added logging to module framework
Implemented logging to Adafruit and changed database loading behavior.
This commit is contained in:
@@ -7,10 +7,11 @@ namespace Props.Shop.Adafruit.Api
|
||||
{
|
||||
public interface IProductListingManager : IDisposable
|
||||
{
|
||||
public event EventHandler DataUpdateEvent;
|
||||
public IDictionary<string, IList<ProductListing>> ActiveListings { get; }
|
||||
public Task DownloadListings();
|
||||
public Task<IDictionary<string, IList<ProductListing>>> ProductListings { get; }
|
||||
public void RefreshProductListings();
|
||||
public void StartUpdateTimer(int delay = 1000 * 60 * 5, int period = 1000 * 60 * 5);
|
||||
public void StopUpdateTimer();
|
||||
|
||||
public Task<ProductListing> GetProductListingFromUrl(string url);
|
||||
}
|
||||
}
|
@@ -4,53 +4,94 @@ using System.Collections.Generic;
|
||||
using System.Net.Http;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Props.Shop.Framework;
|
||||
|
||||
namespace Props.Shop.Adafruit.Api
|
||||
{
|
||||
public class ProductListingManager : IProductListingManager
|
||||
public class LiveProductListingManager : IProductListingManager
|
||||
{
|
||||
public event EventHandler DataUpdateEvent;
|
||||
private ILogger<LiveProductListingManager> logger;
|
||||
private bool disposedValue;
|
||||
private volatile Dictionary<string, IList<ProductListing>> activeListings;
|
||||
public IDictionary<string, IList<ProductListing>> ActiveListings => activeListings;
|
||||
private int minDownloadInterval;
|
||||
private DateTime? lastDownload;
|
||||
private object refreshLock = new object();
|
||||
private volatile Task<IDictionary<string, IList<ProductListing>>> productListingsTask;
|
||||
|
||||
public Task<IDictionary<string, IList<ProductListing>>> ProductListings => productListingsTask;
|
||||
private readonly ConcurrentDictionary<string, ProductListing> activeProductListingUrls = new ConcurrentDictionary<string, ProductListing>();
|
||||
|
||||
private ProductListingsParser parser = new ProductListingsParser();
|
||||
private HttpClient httpClient;
|
||||
private Timer updateTimer;
|
||||
|
||||
public ProductListingManager(HttpClient httpClient)
|
||||
public LiveProductListingManager(HttpClient httpClient, ILogger<LiveProductListingManager> logger, int minDownloadInterval = 5 * 60 * 1000)
|
||||
{
|
||||
this.logger = logger;
|
||||
this.minDownloadInterval = minDownloadInterval;
|
||||
this.httpClient = httpClient;
|
||||
}
|
||||
|
||||
public async Task DownloadListings() {
|
||||
public void RefreshProductListings()
|
||||
{
|
||||
lock (refreshLock)
|
||||
{
|
||||
if (disposedValue) throw new ObjectDisposedException("ProductListingManager");
|
||||
if ((lastDownload != null && DateTime.UtcNow - lastDownload <= TimeSpan.FromMilliseconds(minDownloadInterval)) || (productListingsTask != null && !productListingsTask.IsCompleted)) return;
|
||||
lastDownload = DateTime.UtcNow;
|
||||
logger.LogDebug("Refreshing listings ({0}).", lastDownload);
|
||||
productListingsTask = DownloadListings();
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<ProductListing> GetProductListingFromUrl(string url)
|
||||
{
|
||||
if (disposedValue) throw new ObjectDisposedException("ProductListingManager");
|
||||
await productListingsTask;
|
||||
return activeProductListingUrls[url];
|
||||
}
|
||||
|
||||
private async Task<IDictionary<string, IList<ProductListing>>> DownloadListings()
|
||||
{
|
||||
if (disposedValue) throw new ObjectDisposedException("ProductListingManager");
|
||||
logger.LogDebug("Beginning listing database download.");
|
||||
HttpResponseMessage responseMessage = await httpClient.GetAsync("products");
|
||||
parser.BuildProductListings(responseMessage.Content.ReadAsStream());
|
||||
Dictionary<string, IList<ProductListing>> listings = new Dictionary<string, IList<ProductListing>>();
|
||||
logger.LogDebug("Listing database parsed.");
|
||||
Dictionary<string, IList<ProductListing>> listingNames = new Dictionary<string, IList<ProductListing>>();
|
||||
activeProductListingUrls.Clear();
|
||||
foreach (ProductListing product in parser.ProductListings)
|
||||
{
|
||||
IList<ProductListing> sameProducts = listings.GetValueOrDefault(product.Name);
|
||||
if (sameProducts == null) {
|
||||
activeProductListingUrls.TryAdd(product.URL, product);
|
||||
IList<ProductListing> sameProducts = listingNames.GetValueOrDefault(product.Name);
|
||||
if (sameProducts == null)
|
||||
{
|
||||
sameProducts = new List<ProductListing>();
|
||||
listings.Add(product.Name, sameProducts);
|
||||
listingNames.Add(product.Name, sameProducts);
|
||||
}
|
||||
|
||||
sameProducts.Add(product);
|
||||
}
|
||||
activeListings = listings;
|
||||
DataUpdateEvent?.Invoke(this, null);
|
||||
logger.LogDebug("Downloaded listings organized.");
|
||||
return listingNames;
|
||||
}
|
||||
|
||||
public void StartUpdateTimer(int delay = 1000 * 60 * 5, int period = 1000 * 60 * 5) {
|
||||
public void StartUpdateTimer(int delay = 1000 * 60 * 5, int period = 1000 * 60 * 5)
|
||||
{
|
||||
if (disposedValue) throw new ObjectDisposedException("ProductListingManager");
|
||||
if (updateTimer != null) throw new InvalidOperationException("Update timer already started.");
|
||||
updateTimer = new Timer(async (state) => await DownloadListings(), null, delay, period);
|
||||
logger.LogInformation("Starting update timer.");
|
||||
updateTimer = new Timer((state) =>
|
||||
{
|
||||
RefreshProductListings();
|
||||
}, null, delay, period);
|
||||
}
|
||||
|
||||
public void StopUpdateTimer() {
|
||||
public void StopUpdateTimer()
|
||||
{
|
||||
if (disposedValue) throw new ObjectDisposedException("ProductListingManager");
|
||||
if (updateTimer != null) throw new InvalidOperationException("Update timer not started.");
|
||||
logger.LogInformation("Stopping update timer.");
|
||||
updateTimer.Dispose();
|
||||
updateTimer = null;
|
||||
}
|
||||
|
@@ -2,6 +2,7 @@ using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using FuzzySharp;
|
||||
using FuzzySharp.Extractor;
|
||||
using Props.Shop.Framework;
|
||||
@@ -11,41 +12,26 @@ namespace Props.Shop.Adafruit.Api
|
||||
public class SearchManager : IDisposable
|
||||
{
|
||||
public float Similarity { get; set; }
|
||||
private readonly object searchLock = new object();
|
||||
private IDictionary<string, IList<ProductListing>> searched;
|
||||
private IProductListingManager listingManager;
|
||||
public IProductListingManager ProductListingManager { get; private set; }
|
||||
private bool disposedValue;
|
||||
|
||||
public SearchManager(IProductListingManager productListingManager, float similarity = 0.8f)
|
||||
{
|
||||
this.listingManager = productListingManager ?? throw new ArgumentNullException("productListingManager");
|
||||
this.ProductListingManager = productListingManager ?? throw new ArgumentNullException("productListingManager");
|
||||
this.Similarity = similarity;
|
||||
listingManager.DataUpdateEvent += OnDataUpdate;
|
||||
}
|
||||
|
||||
private void OnDataUpdate(object sender, EventArgs eventArgs)
|
||||
public async IAsyncEnumerable<ProductListing> Search(string query)
|
||||
{
|
||||
BuildSearchIndex();
|
||||
}
|
||||
|
||||
private void BuildSearchIndex()
|
||||
{
|
||||
lock (searchLock)
|
||||
{
|
||||
searched = new Dictionary<string, IList<ProductListing>>(listingManager.ActiveListings);
|
||||
if (ProductListingManager.ProductListings == null) {
|
||||
ProductListingManager.RefreshProductListings();
|
||||
}
|
||||
}
|
||||
|
||||
public IEnumerable<ProductListing> Search(string query)
|
||||
{
|
||||
lock (searchLock)
|
||||
IDictionary<string, IList<ProductListing>> productListings = await ProductListingManager.ProductListings;
|
||||
foreach (ExtractedResult<string> listingNames in Process.ExtractAll(query, productListings.Keys, cutoff: (int)(Similarity * 100)))
|
||||
{
|
||||
foreach (ExtractedResult<string> listingNames in Process.ExtractAll(query, searched.Keys, cutoff: (int)(Similarity * 100)))
|
||||
foreach (ProductListing same in productListings[listingNames.Value])
|
||||
{
|
||||
foreach (ProductListing same in searched[listingNames.Value])
|
||||
{
|
||||
yield return same;
|
||||
}
|
||||
yield return same;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -56,7 +42,7 @@ namespace Props.Shop.Adafruit.Api
|
||||
{
|
||||
if (disposing)
|
||||
{
|
||||
listingManager.Dispose();
|
||||
ProductListingManager.Dispose();
|
||||
}
|
||||
|
||||
disposedValue = true;
|
||||
|
Reference in New Issue
Block a user