Refactored repo organization. Added Jenkinsfile.
This commit is contained in:
19
Props.Shop/Adafruit/Api/IProductListingManager.cs
Normal file
19
Props.Shop/Adafruit/Api/IProductListingManager.cs
Normal file
@@ -0,0 +1,19 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using Props.Shop.Framework;
|
||||
|
||||
namespace Props.Shop.Adafruit.Api
|
||||
{
|
||||
public interface IProductListingManager : IDisposable
|
||||
{
|
||||
public Task<IReadOnlyDictionary<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 DateTime? LastDownload { get; }
|
||||
|
||||
public Task<ProductListing> GetProductListingFromIdentifier(string url);
|
||||
}
|
||||
}
|
52
Props.Shop/Adafruit/Api/ListingsParser.cs
Normal file
52
Props.Shop/Adafruit/Api/ListingsParser.cs
Normal file
@@ -0,0 +1,52 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Net.Http;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using Props.Shop.Framework;
|
||||
|
||||
namespace Props.Shop.Adafruit.Api
|
||||
{
|
||||
public class ProductListingsParser
|
||||
{
|
||||
public IEnumerable<ProductListing> ProductListings { get; private set; }
|
||||
public void BuildProductListings(Stream stream)
|
||||
{
|
||||
using (StreamReader streamReader = new StreamReader(stream))
|
||||
{
|
||||
DateTime startTime = DateTime.UtcNow;
|
||||
dynamic data = JArray.Load(new JsonTextReader(streamReader));
|
||||
List<ProductListing> parsed = new List<ProductListing>();
|
||||
foreach (dynamic item in data)
|
||||
{
|
||||
if (item.products_discontinued == 0)
|
||||
{
|
||||
ProductListing res = new ProductListing();
|
||||
res.TimeFetchedUtc = startTime;
|
||||
res.Name = item.product_name;
|
||||
res.LowerPrice = item.product_price;
|
||||
res.UpperPrice = res.LowerPrice;
|
||||
foreach (dynamic discount in item.discount_pricing)
|
||||
{
|
||||
if (discount.discounted_price < res.LowerPrice)
|
||||
{
|
||||
res.LowerPrice = discount.discounted_price;
|
||||
}
|
||||
if (discount.discounted_price > res.UpperPrice)
|
||||
{
|
||||
res.UpperPrice = discount.discounted_price;
|
||||
}
|
||||
}
|
||||
res.URL = item.product_url;
|
||||
res.InStock = item.product_stock > 0;
|
||||
parsed.Add(res);
|
||||
res.Identifier = res.URL;
|
||||
}
|
||||
}
|
||||
ProductListings = parsed;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
125
Props.Shop/Adafruit/Api/LiveProductListingManager.cs
Normal file
125
Props.Shop/Adafruit/Api/LiveProductListingManager.cs
Normal file
@@ -0,0 +1,125 @@
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.Net.Http;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Props.Shop.Adafruit.Persistence;
|
||||
using Props.Shop.Framework;
|
||||
|
||||
namespace Props.Shop.Adafruit.Api
|
||||
{
|
||||
public class LiveProductListingManager : IProductListingManager
|
||||
{
|
||||
private ILogger<LiveProductListingManager> logger;
|
||||
private bool disposedValue;
|
||||
private int minDownloadInterval;
|
||||
public DateTime? LastDownload { get; private set; }
|
||||
private object refreshLock = new object();
|
||||
private volatile Task<IReadOnlyDictionary<string, IList<ProductListing>>> productListingsTask;
|
||||
|
||||
public Task<IReadOnlyDictionary<string, IList<ProductListing>>> ProductListings => productListingsTask;
|
||||
private readonly ConcurrentDictionary<string, ProductListing> identifierMap = new ConcurrentDictionary<string, ProductListing>();
|
||||
|
||||
private ProductListingsParser parser = new ProductListingsParser();
|
||||
private HttpClient httpClient;
|
||||
private Timer updateTimer;
|
||||
|
||||
public LiveProductListingManager(HttpClient httpClient, ILogger<LiveProductListingManager> logger, ProductListingCacheData productListingCacheData = null, int minDownloadInterval = 5 * 60 * 1000)
|
||||
{
|
||||
this.logger = logger;
|
||||
this.minDownloadInterval = minDownloadInterval;
|
||||
this.httpClient = httpClient;
|
||||
if (productListingCacheData != null)
|
||||
{
|
||||
productListingsTask = Task.FromResult(productListingCacheData.ProductListings);
|
||||
LastDownload = productListingCacheData.LastUpdatedUtc;
|
||||
logger.LogInformation("{0} Cached product listings loaded. Listing saved at {1}", productListingCacheData.ProductListings.Count, productListingCacheData.LastUpdatedUtc);
|
||||
}
|
||||
}
|
||||
|
||||
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> GetProductListingFromIdentifier(string identifier)
|
||||
{
|
||||
if (disposedValue) throw new ObjectDisposedException("ProductListingManager");
|
||||
await productListingsTask;
|
||||
return identifierMap[identifier];
|
||||
}
|
||||
|
||||
private async Task<IReadOnlyDictionary<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());
|
||||
logger.LogDebug("Listing database parsed.");
|
||||
Dictionary<string, IList<ProductListing>> listingNames = new Dictionary<string, IList<ProductListing>>();
|
||||
identifierMap.Clear();
|
||||
foreach (ProductListing product in parser.ProductListings)
|
||||
{
|
||||
identifierMap.TryAdd(product.Identifier, product);
|
||||
IList<ProductListing> sameProducts = listingNames.GetValueOrDefault(product.Name);
|
||||
if (sameProducts == null)
|
||||
{
|
||||
sameProducts = new List<ProductListing>();
|
||||
listingNames.Add(product.Name, sameProducts);
|
||||
}
|
||||
|
||||
sameProducts.Add(product);
|
||||
}
|
||||
logger.LogDebug("Downloaded listings organized.");
|
||||
return listingNames;
|
||||
}
|
||||
|
||||
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.");
|
||||
logger.LogInformation("Starting update timer.");
|
||||
updateTimer = new Timer((state) =>
|
||||
{
|
||||
RefreshProductListings();
|
||||
}, null, delay, period);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
protected virtual void Dispose(bool disposing)
|
||||
{
|
||||
if (!disposedValue)
|
||||
{
|
||||
if (disposing)
|
||||
{
|
||||
updateTimer?.Dispose();
|
||||
updateTimer = null;
|
||||
}
|
||||
disposedValue = true;
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Dispose(disposing: true);
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
}
|
||||
}
|
61
Props.Shop/Adafruit/Api/SearchManager.cs
Normal file
61
Props.Shop/Adafruit/Api/SearchManager.cs
Normal file
@@ -0,0 +1,61 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using FuzzySharp;
|
||||
using FuzzySharp.Extractor;
|
||||
using Props.Shop.Framework;
|
||||
|
||||
namespace Props.Shop.Adafruit.Api
|
||||
{
|
||||
public class SearchManager : IDisposable
|
||||
{
|
||||
public float Similarity { get; set; }
|
||||
public IProductListingManager ProductListingManager { get; private set; }
|
||||
private bool disposedValue;
|
||||
|
||||
public SearchManager(IProductListingManager productListingManager, float similarity = 0.8f)
|
||||
{
|
||||
this.ProductListingManager = productListingManager ?? throw new ArgumentNullException("productListingManager");
|
||||
this.Similarity = similarity;
|
||||
}
|
||||
|
||||
public async IAsyncEnumerable<ProductListing> Search(string query)
|
||||
{
|
||||
// TODO: Implement indexed search.
|
||||
if (ProductListingManager.ProductListings == null) {
|
||||
ProductListingManager.RefreshProductListings();
|
||||
}
|
||||
IReadOnlyDictionary<string, IList<ProductListing>> productListings = await ProductListingManager.ProductListings;
|
||||
if (productListings == null) throw new InvalidAsynchronousStateException("productListings can't be null");
|
||||
foreach (ExtractedResult<string> listingNames in Process.ExtractAll(query, productListings.Keys, cutoff: (int)(Similarity * 100)))
|
||||
{
|
||||
foreach (ProductListing same in productListings[listingNames.Value])
|
||||
{
|
||||
yield return same;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected virtual void Dispose(bool disposing)
|
||||
{
|
||||
if (!disposedValue)
|
||||
{
|
||||
if (disposing)
|
||||
{
|
||||
ProductListingManager.Dispose();
|
||||
}
|
||||
|
||||
disposedValue = true;
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Dispose(disposing: true);
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user