Harrison Deng
0b507b90a1
Implemented in AdafruitShop. AdafruitShop Product listing data now persisted. AdafruitShop configuration now persisted.
84 lines
3.2 KiB
C#
84 lines
3.2 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using Microsoft.Extensions.Logging;
|
|
using Props.Data;
|
|
using Props.Models.Search;
|
|
using Props.Shop.Framework;
|
|
|
|
namespace Props.Services.Modules
|
|
{
|
|
public class LiveMetricsManager : IMetricsManager
|
|
{
|
|
private ILogger<LiveMetricsManager> logger;
|
|
ApplicationDbContext dbContext;
|
|
public LiveMetricsManager(ApplicationDbContext dbContext, ILogger<LiveMetricsManager> logger)
|
|
{
|
|
this.logger = logger;
|
|
this.dbContext = dbContext;
|
|
}
|
|
public IEnumerable<ProductListingInfo> RetrieveTopListings(int max)
|
|
{
|
|
if (dbContext.ProductListingInfos == null) return null;
|
|
return (from l in dbContext.ProductListingInfos
|
|
orderby l.Hits descending
|
|
select l).Take(max);
|
|
}
|
|
|
|
public IEnumerable<string> RetrieveCommonKeywords(int max)
|
|
{
|
|
if (dbContext.Keywords == null) return null;
|
|
return (from k in dbContext.Keywords
|
|
orderby k.Hits descending
|
|
select k.Word).Take(max);
|
|
}
|
|
|
|
public void RegisterSearchQuery(string query)
|
|
{
|
|
query = query.ToLower();
|
|
string[] tokens = query.Split(' ');
|
|
QueryWordInfo[] wordInfos = new QueryWordInfo[tokens.Length];
|
|
for (int wordIndex = 0; wordIndex < tokens.Length; wordIndex++)
|
|
{
|
|
QueryWordInfo queryWordInfo = dbContext.Keywords.Where((k) => k.Word.ToLower().Equals(tokens[wordIndex])).SingleOrDefault() ?? new QueryWordInfo();
|
|
if (queryWordInfo.Hits == 0)
|
|
{
|
|
queryWordInfo.Word = tokens[wordIndex];
|
|
dbContext.Keywords.Add(queryWordInfo);
|
|
}
|
|
queryWordInfo.Hits += 1;
|
|
wordInfos[wordIndex] = queryWordInfo;
|
|
}
|
|
for (int wordIndex = 0; wordIndex < tokens.Length; wordIndex++)
|
|
{
|
|
for (int beforeIndex = 0; beforeIndex < wordIndex; beforeIndex++)
|
|
{
|
|
wordInfos[wordIndex].Preceding.Add(wordInfos[beforeIndex]);
|
|
}
|
|
for (int afterIndex = wordIndex; afterIndex < tokens.Length; afterIndex++)
|
|
{
|
|
wordInfos[wordIndex].Following.Add(wordInfos[afterIndex]);
|
|
}
|
|
}
|
|
dbContext.SaveChanges();
|
|
|
|
}
|
|
|
|
public void RegisterListing(ProductListing productListing, string shopName)
|
|
{
|
|
ProductListingInfo productListingInfo =
|
|
(from info in dbContext.ProductListingInfos
|
|
where info.ProductListingIdentifier.Equals(productListing.Identifier)
|
|
select info).SingleOrDefault() ?? new ProductListingInfo();
|
|
if (productListingInfo.Hits == 0)
|
|
{
|
|
dbContext.Add(productListingInfo);
|
|
}
|
|
productListingInfo.ShopName = shopName;
|
|
productListingInfo.ProductListing = productListing;
|
|
productListingInfo.ProductListingIdentifier = productListing.Identifier;
|
|
productListingInfo.Hits += 1;
|
|
dbContext.SaveChanges();
|
|
}
|
|
}
|
|
} |