Added primitive search mechanism in backend.
Began implementing search mechanism for frontend.
This commit is contained in:
@@ -0,0 +1,16 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using Props.Shop.Framework;
|
||||
|
||||
namespace Props.Shop.Adafruit.Api
|
||||
{
|
||||
public interface IProductListingManager : IDisposable
|
||||
{
|
||||
public event EventHandler DataUpdateEvent;
|
||||
public IDictionary<string, IList<ProductListing>> ActiveListings { get; }
|
||||
public Task DownloadListings();
|
||||
public void StartUpdateTimer(int delay = 1000 * 60 * 5, int period = 1000 * 60 * 5);
|
||||
public void StopUpdateTimer();
|
||||
}
|
||||
}
|
@@ -1,44 +1,49 @@
|
||||
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 ListingsParser
|
||||
public class ProductListingsParser
|
||||
{
|
||||
public IEnumerable<ProductListing> ProductListings { get; private set; }
|
||||
public ListingsParser(string json)
|
||||
public void BuildProductListings(Stream stream)
|
||||
{
|
||||
dynamic data = JArray.Parse(json);
|
||||
List<ProductListing> parsed = new List<ProductListing>();
|
||||
foreach (dynamic item in data)
|
||||
using (StreamReader streamReader = new StreamReader(stream))
|
||||
{
|
||||
if (item.products_discontinued == 0)
|
||||
dynamic data = JArray.Load(new JsonTextReader(streamReader));
|
||||
List<ProductListing> parsed = new List<ProductListing>();
|
||||
foreach (dynamic item in data)
|
||||
{
|
||||
ProductListing res = new ProductListing();
|
||||
res.Name = item.product_name;
|
||||
res.LowerPrice = item.product_price;
|
||||
res.UpperPrice = res.LowerPrice;
|
||||
foreach (dynamic discount in item.discount_pricing)
|
||||
if (item.products_discontinued == 0)
|
||||
{
|
||||
if (discount.discounted_price < res.LowerPrice)
|
||||
ProductListing res = new ProductListing();
|
||||
res.Name = item.product_name;
|
||||
res.LowerPrice = item.product_price;
|
||||
res.UpperPrice = res.LowerPrice;
|
||||
foreach (dynamic discount in item.discount_pricing)
|
||||
{
|
||||
res.LowerPrice = discount.discounted_price;
|
||||
}
|
||||
if (discount.discounted_price > res.UpperPrice)
|
||||
{
|
||||
res.UpperPrice = discount.discounted_price;
|
||||
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.URL = item.product_url;
|
||||
res.InStock = item.product_stock > 0;
|
||||
parsed.Add(res);
|
||||
}
|
||||
ProductListings = parsed;
|
||||
}
|
||||
ProductListings = parsed;
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,77 @@
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.Net.Http;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Props.Shop.Framework;
|
||||
|
||||
namespace Props.Shop.Adafruit.Api
|
||||
{
|
||||
public class ProductListingManager : IProductListingManager
|
||||
{
|
||||
public event EventHandler DataUpdateEvent;
|
||||
private bool disposedValue;
|
||||
private volatile Dictionary<string, IList<ProductListing>> activeListings;
|
||||
public IDictionary<string, IList<ProductListing>> ActiveListings => activeListings;
|
||||
private ProductListingsParser parser = new ProductListingsParser();
|
||||
private HttpClient httpClient;
|
||||
private Timer updateTimer;
|
||||
|
||||
public ProductListingManager(HttpClient httpClient)
|
||||
{
|
||||
this.httpClient = httpClient;
|
||||
}
|
||||
|
||||
public async Task DownloadListings() {
|
||||
if (disposedValue) throw new ObjectDisposedException("ProductListingManager");
|
||||
HttpResponseMessage responseMessage = await httpClient.GetAsync("products");
|
||||
parser.BuildProductListings(responseMessage.Content.ReadAsStream());
|
||||
Dictionary<string, IList<ProductListing>> listings = new Dictionary<string, IList<ProductListing>>();
|
||||
foreach (ProductListing product in parser.ProductListings)
|
||||
{
|
||||
IList<ProductListing> sameProducts = listings.GetValueOrDefault(product.Name);
|
||||
if (sameProducts == null) {
|
||||
sameProducts = new List<ProductListing>();
|
||||
listings.Add(product.Name, sameProducts);
|
||||
}
|
||||
|
||||
sameProducts.Add(product);
|
||||
}
|
||||
activeListings = listings;
|
||||
DataUpdateEvent?.Invoke(this, null);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
public void StopUpdateTimer() {
|
||||
if (disposedValue) throw new ObjectDisposedException("ProductListingManager");
|
||||
if (updateTimer != null) throw new InvalidOperationException("Update timer not started.");
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,58 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net.Http;
|
||||
using System.Threading.Tasks;
|
||||
using FuzzySharp;
|
||||
using FuzzySharp.Extractor;
|
||||
using Props.Shop.Framework;
|
||||
|
||||
namespace Props.Shop.Adafruit.Api
|
||||
{
|
||||
public class ProductListingManager
|
||||
{
|
||||
private double minutesPerRequest;
|
||||
private Dictionary<string, List<ProductListing>> listings = new Dictionary<string, List<ProductListing>>();
|
||||
private bool requested = false;
|
||||
public DateTime TimeOfLastRequest { get; private set; }
|
||||
public bool RequestReady => !requested || DateTime.Now - TimeOfLastRequest > TimeSpan.FromMinutes(minutesPerRequest);
|
||||
|
||||
public ProductListingManager(int requestsPerMinute = 5)
|
||||
{
|
||||
this.minutesPerRequest = 1 / requestsPerMinute;
|
||||
}
|
||||
|
||||
public async Task RefreshListings(HttpClient http)
|
||||
{
|
||||
requested = true;
|
||||
TimeOfLastRequest = DateTime.Now;
|
||||
HttpResponseMessage response = await http.GetAsync("/products");
|
||||
SetListingsData(await response.Content.ReadAsStringAsync());
|
||||
}
|
||||
|
||||
public void SetListingsData(string data)
|
||||
{
|
||||
ListingsParser listingsParser = new ListingsParser(data);
|
||||
foreach (ProductListing listing in listingsParser.ProductListings)
|
||||
{
|
||||
List<ProductListing> similar = listings.GetValueOrDefault(listing.Name, new List<ProductListing>());
|
||||
similar.Add(listing);
|
||||
listings[listing.Name] = similar;
|
||||
}
|
||||
}
|
||||
|
||||
public async IAsyncEnumerable<ProductListing> Search(string query, float similarity, HttpClient httpClient = null)
|
||||
{
|
||||
if (RequestReady && httpClient != null) await RefreshListings(httpClient);
|
||||
IEnumerable<ExtractedResult<string>> resultNames = Process.ExtractAll(query, listings.Keys, cutoff: (int)similarity * 100);
|
||||
foreach (ExtractedResult<string> resultName in resultNames)
|
||||
{
|
||||
foreach (ProductListing product in listings[resultName.Value])
|
||||
{
|
||||
yield return product;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
72
Props-Modules/Props.Shop/Adafruit/Api/SearchManager.cs
Normal file
72
Props-Modules/Props.Shop/Adafruit/Api/SearchManager.cs
Normal file
@@ -0,0 +1,72 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using FuzzySharp;
|
||||
using FuzzySharp.Extractor;
|
||||
using Props.Shop.Framework;
|
||||
|
||||
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;
|
||||
private bool disposedValue;
|
||||
|
||||
public SearchManager(IProductListingManager productListingManager, float similarity = 0.8f)
|
||||
{
|
||||
this.listingManager = productListingManager ?? throw new ArgumentNullException("productListingManager");
|
||||
this.Similarity = similarity;
|
||||
listingManager.DataUpdateEvent += OnDataUpdate;
|
||||
}
|
||||
|
||||
private void OnDataUpdate(object sender, EventArgs eventArgs)
|
||||
{
|
||||
BuildSearchIndex();
|
||||
}
|
||||
|
||||
private void BuildSearchIndex()
|
||||
{
|
||||
lock (searchLock)
|
||||
{
|
||||
searched = new Dictionary<string, IList<ProductListing>>(listingManager.ActiveListings);
|
||||
}
|
||||
}
|
||||
|
||||
public IEnumerable<ProductListing> Search(string query)
|
||||
{
|
||||
lock (searchLock)
|
||||
{
|
||||
foreach (ExtractedResult<string> listingNames in Process.ExtractAll(query, searched.Keys, cutoff: (int)(Similarity * 100)))
|
||||
{
|
||||
foreach (ProductListing same in searched[listingNames.Value])
|
||||
{
|
||||
yield return same;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected virtual void Dispose(bool disposing)
|
||||
{
|
||||
if (!disposedValue)
|
||||
{
|
||||
if (disposing)
|
||||
{
|
||||
listingManager.Dispose();
|
||||
}
|
||||
|
||||
disposedValue = true;
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Dispose(disposing: true);
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user