Added logging to module framework

Implemented logging to Adafruit and changed database loading behavior.
This commit is contained in:
2021-08-07 17:20:46 -05:00
parent c94ea4a624
commit 38ffb3c7e1
36 changed files with 304 additions and 240 deletions

View File

@@ -1,4 +1,5 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Threading;
@@ -10,58 +11,65 @@ namespace Props.Shop.Adafruit.Tests.Api
{
public class FakeProductListingManager : IProductListingManager
{
private ProductListingsParser parser;
private Timer updateTimer;
private Timer refreshTimer;
private bool disposedValue;
private volatile Task<IDictionary<string, IList<ProductListing>>> activeListings;
private DateTime? lastDownload;
private ProductListingsParser parser = new ProductListingsParser();
private readonly ConcurrentDictionary<string, ProductListing> activeProductListingUrls = new ConcurrentDictionary<string, ProductListing>();
private volatile Dictionary<string, IList<ProductListing>> activeListings;
public IDictionary<string, IList<ProductListing>> ActiveListings => activeListings;
public Task<IDictionary<string, IList<ProductListing>>> ProductListings => activeListings;
public event EventHandler DataUpdateEvent;
public FakeProductListingManager()
public async Task<ProductListing> GetProductListingFromUrl(string url)
{
parser = new ProductListingsParser();
if (disposedValue) throw new ObjectDisposedException("ProductListingManager");
await activeListings;
return activeProductListingUrls[url];
}
public Task DownloadListings()
public void RefreshProductListings()
{
if (disposedValue) throw new ObjectDisposedException("FakeProductListingManager");
using (Stream stream = File.OpenRead("./Assets/products.json"))
{
parser.BuildProductListings(stream);
}
if (disposedValue) throw new ObjectDisposedException("ProductListingManager");
if ((lastDownload != null && DateTime.UtcNow - lastDownload <= TimeSpan.FromMilliseconds(5 * 60 * 1000)) || (activeListings != null && !activeListings.IsCompleted)) return;
activeListings = DownloadListings();
}
Dictionary<string, IList<ProductListing>> results = new Dictionary<string, IList<ProductListing>>();
private Task<IDictionary<string, IList<ProductListing>>> DownloadListings() {
if (disposedValue) throw new ObjectDisposedException("ProductListingManager");
lastDownload = DateTime.UtcNow;
parser.BuildProductListings(File.OpenRead("./Assets/products.json"));
Dictionary<string, IList<ProductListing>> listingNames = new Dictionary<string, IList<ProductListing>>();
activeProductListingUrls.Clear();
foreach (ProductListing product in parser.ProductListings)
{
IList<ProductListing> sameProducts = results.GetValueOrDefault(product.Name);
activeProductListingUrls.TryAdd(product.URL, product);
IList<ProductListing> sameProducts = listingNames.GetValueOrDefault(product.Name);
if (sameProducts == null) {
sameProducts = new List<ProductListing>();
results.Add(product.Name, sameProducts);
listingNames.Add(product.Name, sameProducts);
}
sameProducts.Add(product);
}
activeListings = results;
DataUpdateEvent?.Invoke(this, null);
return Task.CompletedTask;
return Task.FromResult<IDictionary<string, IList<ProductListing>>>(listingNames);
}
public void StartUpdateTimer(int delay = 300000, int period = 300000)
{
if (disposedValue) throw new ObjectDisposedException("FakeProductListingManager");
if (updateTimer != null) throw new InvalidOperationException("Update timer already started.");
updateTimer = new Timer((state) => DownloadListings(), null, delay, period);
if (disposedValue) throw new ObjectDisposedException("ProductListingManager");
if (refreshTimer != null) throw new InvalidOperationException("Refresh timer already running.");
refreshTimer = new Timer((state) => {
RefreshProductListings();
}, null, delay, period);
}
public void StopUpdateTimer()
{
if (disposedValue) throw new ObjectDisposedException("FakeProductListingManager");
if (updateTimer == null) throw new InvalidOperationException("Update timer not started.");
updateTimer.Dispose();
updateTimer = null;
if (disposedValue) throw new ObjectDisposedException("ProductListingManager");
if (refreshTimer == null) throw new InvalidOperationException("Refresh timer not running.");
refreshTimer.Dispose();
refreshTimer = null;
}
protected virtual void Dispose(bool disposing)
@@ -70,8 +78,8 @@ namespace Props.Shop.Adafruit.Tests.Api
{
if (disposing)
{
updateTimer?.Dispose();
updateTimer = null;
refreshTimer?.Dispose();
refreshTimer = null;
}
disposedValue = true;

View File

@@ -1,3 +1,4 @@
using System.Linq;
using System.Threading.Tasks;
using Props.Shop.Adafruit.Api;
using Xunit;
@@ -7,13 +8,13 @@ namespace Props.Shop.Adafruit.Tests.Api
public class SearchManagerTest
{
[Fact]
public async Task SearchTest()
public void SearchTest()
{
FakeProductListingManager stubProductListingManager = new FakeProductListingManager();
SearchManager searchManager = new SearchManager(stubProductListingManager);
await stubProductListingManager.DownloadListings();
stubProductListingManager.RefreshProductListings();
searchManager.Similarity = 0.8f;
Assert.NotEmpty(searchManager.Search("Raspberry Pi"));
Assert.NotEmpty(searchManager.Search("Raspberry Pi").ToEnumerable());
searchManager.Dispose();
}
}