Compare commits
No commits in common. "799b3f40a239b8af547de41222b5565fe2bad954" and "a8ec0cee16b8022ebe0d030c422935a4f33cc1ec" have entirely different histories.
799b3f40a2
...
a8ec0cee16
1
.gitignore
vendored
1
.gitignore
vendored
@ -452,4 +452,3 @@ $RECYCLE.BIN/
|
|||||||
!.vscode/tasks.json
|
!.vscode/tasks.json
|
||||||
!.vscode/launch.json
|
!.vscode/launch.json
|
||||||
!.vscode/extensions.json
|
!.vscode/extensions.json
|
||||||
/RhSolutions.Api/MLModels
|
|
||||||
|
@ -1,40 +0,0 @@
|
|||||||
using Microsoft.AspNetCore.Http;
|
|
||||||
using Microsoft.Extensions.Primitives;
|
|
||||||
using RhSolutions.Api.Services;
|
|
||||||
|
|
||||||
namespace RhSolutions.Api.Tests;
|
|
||||||
|
|
||||||
public class ProductQueryModifierTests
|
|
||||||
{
|
|
||||||
private ProductQueryModifierFactory _factory;
|
|
||||||
|
|
||||||
[SetUp]
|
|
||||||
public void Setup()
|
|
||||||
{
|
|
||||||
_factory = new ProductQueryModifierFactory();
|
|
||||||
}
|
|
||||||
|
|
||||||
[TestCase("Монтажная гильза", "Гильза 16", "Монтажная гильза 16")]
|
|
||||||
[TestCase("Монтажная гильза", "Пресс-втулка 20", "Монтажная гильза 20")]
|
|
||||||
public void SleeveTest(string productType, string query, string modified) =>
|
|
||||||
Test(productType, query, modified);
|
|
||||||
|
|
||||||
[TestCase("Тройник RAUTITAN", "Тройник 20-16-16", "Тройник RAUTITAN -PLATINUM 20-16-16")]
|
|
||||||
[TestCase("Тройник RAUTITAN", "Тройник 20x16x16", "Тройник RAUTITAN -PLATINUM 20-16-16")]
|
|
||||||
public void TPieceTest(string productType, string query, string modified) =>
|
|
||||||
Test(productType, query, modified);
|
|
||||||
|
|
||||||
public void Test(string productType, string query, string modified)
|
|
||||||
{
|
|
||||||
Dictionary<string, StringValues> queryPair = new()
|
|
||||||
{
|
|
||||||
["query"] = new StringValues(query)
|
|
||||||
};
|
|
||||||
QueryCollection collection = new(queryPair);
|
|
||||||
QueryString expected = new($"?query={Uri.EscapeDataString(modified)}");
|
|
||||||
var modifier = _factory.GetModifier(productType);
|
|
||||||
bool result = modifier.TryQueryModify(collection, out var actual);
|
|
||||||
Assert.True(result);
|
|
||||||
Assert.That(actual, Is.EqualTo(expected));
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,23 +0,0 @@
|
|||||||
<Project Sdk="Microsoft.NET.Sdk">
|
|
||||||
|
|
||||||
<PropertyGroup>
|
|
||||||
<TargetFramework>net6.0</TargetFramework>
|
|
||||||
<ImplicitUsings>enable</ImplicitUsings>
|
|
||||||
<Nullable>enable</Nullable>
|
|
||||||
|
|
||||||
<IsPackable>false</IsPackable>
|
|
||||||
</PropertyGroup>
|
|
||||||
|
|
||||||
<ItemGroup>
|
|
||||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
|
|
||||||
<PackageReference Include="NUnit" Version="3.13.3" />
|
|
||||||
<PackageReference Include="NUnit3TestAdapter" Version="4.2.1" />
|
|
||||||
<PackageReference Include="NUnit.Analyzers" Version="3.3.0" />
|
|
||||||
<PackageReference Include="coverlet.collector" Version="3.1.2" />
|
|
||||||
</ItemGroup>
|
|
||||||
|
|
||||||
<ItemGroup>
|
|
||||||
<ProjectReference Include="..\RhSolutions.Api\RhSolutions.Api.csproj" />
|
|
||||||
</ItemGroup>
|
|
||||||
|
|
||||||
</Project>
|
|
@ -1 +0,0 @@
|
|||||||
global using NUnit.Framework;
|
|
@ -1,30 +0,0 @@
|
|||||||
using Microsoft.AspNetCore.Http.Extensions;
|
|
||||||
using RhSolutions.Api.Services;
|
|
||||||
|
|
||||||
namespace RhSolutions.Api.Middleware;
|
|
||||||
|
|
||||||
public class QueryModifier
|
|
||||||
{
|
|
||||||
private RequestDelegate _next;
|
|
||||||
|
|
||||||
public QueryModifier(RequestDelegate nextDelegate)
|
|
||||||
{
|
|
||||||
_next = nextDelegate;
|
|
||||||
}
|
|
||||||
|
|
||||||
public async Task Invoke(HttpContext context, IProductTypePredicter typePredicter, ProductQueryModifierFactory productQueryModifierFactory)
|
|
||||||
{
|
|
||||||
if (context.Request.Method == HttpMethods.Get
|
|
||||||
&& context.Request.Path == "/api/search")
|
|
||||||
{
|
|
||||||
string query = context.Request.Query["query"].ToString();
|
|
||||||
var productType = typePredicter.GetPredictedProductType(query);
|
|
||||||
var modifier = productQueryModifierFactory.GetModifier(productType!);
|
|
||||||
if (modifier.TryQueryModify(context.Request.Query, out var newQuery))
|
|
||||||
{
|
|
||||||
context.Request.QueryString = newQuery;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
await _next(context);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,7 +1,6 @@
|
|||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
using RhSolutions.Models;
|
using RhSolutions.Models;
|
||||||
using RhSolutions.Api.Services;
|
using RhSolutions.Api.Services;
|
||||||
using RhSolutions.Api.Middleware;
|
|
||||||
|
|
||||||
var builder = WebApplication.CreateBuilder(args);
|
var builder = WebApplication.CreateBuilder(args);
|
||||||
|
|
||||||
@ -22,15 +21,12 @@ builder.Services.AddDbContext<RhSolutionsContext>(opts =>
|
|||||||
opts.EnableSensitiveDataLogging(true);
|
opts.EnableSensitiveDataLogging(true);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
builder.Services.AddScoped<IPricelistParser, ClosedXMLParser>()
|
builder.Services.AddScoped<IPricelistParser, ClosedXMLParser>();
|
||||||
.AddScoped<IProductTypePredicter, ProductTypePredicter>()
|
|
||||||
.AddSingleton<ProductQueryModifierFactory>();
|
|
||||||
builder.Services.AddControllers();
|
builder.Services.AddControllers();
|
||||||
|
|
||||||
var app = builder.Build();
|
var app = builder.Build();
|
||||||
|
|
||||||
app.MapControllers();
|
app.MapControllers();
|
||||||
app.UseMiddleware<QueryModifier>();
|
|
||||||
|
|
||||||
var context = app.Services.CreateScope().ServiceProvider
|
var context = app.Services.CreateScope().ServiceProvider
|
||||||
.GetRequiredService<RhSolutionsContext>();
|
.GetRequiredService<RhSolutionsContext>();
|
||||||
|
@ -13,16 +13,9 @@
|
|||||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||||
<PrivateAssets>all</PrivateAssets>
|
<PrivateAssets>all</PrivateAssets>
|
||||||
</PackageReference>
|
</PackageReference>
|
||||||
<PackageReference Include="Microsoft.ML" Version="2.0.1" />
|
|
||||||
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="7.0.4" />
|
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="7.0.4" />
|
||||||
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL.Design" Version="1.1.0" />
|
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL.Design" Version="1.1.0" />
|
||||||
<PackageReference Include="Rhsolutions.ProductSku" Version="1.0.0" />
|
<PackageReference Include="Rhsolutions.ProductSku" Version="1.0.0" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
|
||||||
<None Update="MLModels\model.zip">
|
|
||||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
|
||||||
</None>
|
|
||||||
</ItemGroup>
|
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
@ -1,11 +0,0 @@
|
|||||||
namespace RhSolutions.Api.Services
|
|
||||||
{
|
|
||||||
public class BypassQueryModifier : IProductQueryModifier
|
|
||||||
{
|
|
||||||
public bool TryQueryModify(IQueryCollection collection, out QueryString queryString)
|
|
||||||
{
|
|
||||||
queryString = QueryString.Empty;
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,76 +0,0 @@
|
|||||||
using System.Text;
|
|
||||||
using System.Text.RegularExpressions;
|
|
||||||
using Microsoft.AspNetCore.Http.Extensions;
|
|
||||||
|
|
||||||
namespace RhSolutions.Api.Services
|
|
||||||
{
|
|
||||||
public class FlexPipeQueryModifier : IProductQueryModifier
|
|
||||||
{
|
|
||||||
protected virtual string diameterPattern { get; } = @"16|20|25|32|40|50|63";
|
|
||||||
protected virtual string typePattern { get; } = @"(бухт)|(отр)";
|
|
||||||
protected virtual string pipeName { get; } = "Flex";
|
|
||||||
protected virtual Dictionary<string, string> diameterNames { get; } = new()
|
|
||||||
{
|
|
||||||
["16"] = "16x2,2",
|
|
||||||
["20"] = "20x2,8",
|
|
||||||
["25"] = "25x3,5",
|
|
||||||
["32"] = "32x4,4",
|
|
||||||
["40"] = "40x5,5",
|
|
||||||
["50"] = "50x6,9",
|
|
||||||
["63"] = "63x8,6"
|
|
||||||
};
|
|
||||||
|
|
||||||
public bool TryQueryModify(IQueryCollection collection, out QueryString queryString)
|
|
||||||
{
|
|
||||||
queryString = QueryString.Empty;
|
|
||||||
StringBuilder sb = new();
|
|
||||||
|
|
||||||
string query = collection["query"].ToString();
|
|
||||||
if (string.IsNullOrEmpty(query))
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
sb.Append($"Труба {pipeName} ");
|
|
||||||
var diameterMatches = Regex.Matches(query, diameterPattern);
|
|
||||||
string diameter;
|
|
||||||
if (diameterMatches.Count > 0)
|
|
||||||
{
|
|
||||||
diameter = diameterMatches.First().Value;
|
|
||||||
sb.Append($"{diameterNames[diameter]} " );
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
var typeMatches = Regex.Matches(query, typePattern);
|
|
||||||
if (typeMatches.Count > 0)
|
|
||||||
{
|
|
||||||
var type = typeMatches.First().Value;
|
|
||||||
if (type.StartsWith("бухт"))
|
|
||||||
{
|
|
||||||
sb.Append("бухта");
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
sb.Append("прям.отрезки");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (int.Parse(diameter) < 32)
|
|
||||||
{
|
|
||||||
sb.Append("бухта");
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
sb.Append("прям.отрезки");
|
|
||||||
}
|
|
||||||
|
|
||||||
QueryBuilder qb = new()
|
|
||||||
{
|
|
||||||
{ "query", sb.ToString() }
|
|
||||||
};
|
|
||||||
queryString = qb.ToQueryString();
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,7 +0,0 @@
|
|||||||
namespace RhSolutions.Api.Services
|
|
||||||
{
|
|
||||||
public interface IProductQueryModifier
|
|
||||||
{
|
|
||||||
public bool TryQueryModify(IQueryCollection collection, out QueryString queryString);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,6 +0,0 @@
|
|||||||
namespace RhSolutions.Api.Services;
|
|
||||||
|
|
||||||
public interface IProductTypePredicter
|
|
||||||
{
|
|
||||||
public string? GetPredictedProductType(string productName);
|
|
||||||
}
|
|
@ -1,21 +0,0 @@
|
|||||||
namespace RhSolutions.Api.Services;
|
|
||||||
|
|
||||||
public class ProductQueryModifierFactory
|
|
||||||
{
|
|
||||||
public IProductQueryModifier GetModifier(string productTypeName)
|
|
||||||
{
|
|
||||||
switch (productTypeName)
|
|
||||||
{
|
|
||||||
case "Монтажная гильза":
|
|
||||||
return new SleeveQueryModifier();
|
|
||||||
case "Тройник RAUTITAN":
|
|
||||||
return new TPieceQueryModifier();
|
|
||||||
case "Flex":
|
|
||||||
return new FlexPipeQueryModifier();
|
|
||||||
case "Stabil":
|
|
||||||
return new StabilPipeQueryModifier();
|
|
||||||
default:
|
|
||||||
return new BypassQueryModifier();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,44 +0,0 @@
|
|||||||
using Microsoft.ML;
|
|
||||||
using Microsoft.ML.Data;
|
|
||||||
|
|
||||||
namespace RhSolutions.Api.Services;
|
|
||||||
|
|
||||||
public class ProductTypePredicter : IProductTypePredicter
|
|
||||||
{
|
|
||||||
private readonly string _modelPath = @"./MLModels/model.zip";
|
|
||||||
private MLContext _mlContext;
|
|
||||||
private ITransformer _loadedModel;
|
|
||||||
private PredictionEngine<Product, TypePrediction> _predEngine;
|
|
||||||
|
|
||||||
public ProductTypePredicter()
|
|
||||||
{
|
|
||||||
_mlContext = new MLContext(seed: 0);
|
|
||||||
_loadedModel = _mlContext.Model.Load(_modelPath, out var _);
|
|
||||||
_predEngine = _mlContext.Model.CreatePredictionEngine<Product, TypePrediction>(_loadedModel);
|
|
||||||
}
|
|
||||||
|
|
||||||
public string? GetPredictedProductType(string productName)
|
|
||||||
{
|
|
||||||
Product p = new()
|
|
||||||
{
|
|
||||||
Name = productName
|
|
||||||
};
|
|
||||||
var prediction = _predEngine.Predict(p);
|
|
||||||
return prediction.Type;
|
|
||||||
}
|
|
||||||
|
|
||||||
public class Product
|
|
||||||
{
|
|
||||||
[LoadColumn(0)]
|
|
||||||
public string? Name { get; set; }
|
|
||||||
[LoadColumn(1)]
|
|
||||||
public string? Type { get; set; }
|
|
||||||
}
|
|
||||||
|
|
||||||
public class TypePrediction
|
|
||||||
{
|
|
||||||
[ColumnName("PredictedLabel")]
|
|
||||||
public string? Type { get; set; }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,38 +0,0 @@
|
|||||||
using System.Text.RegularExpressions;
|
|
||||||
using System.Text;
|
|
||||||
using Microsoft.AspNetCore.Http.Extensions;
|
|
||||||
|
|
||||||
namespace RhSolutions.Api.Services
|
|
||||||
{
|
|
||||||
public class SleeveQueryModifier : IProductQueryModifier
|
|
||||||
{
|
|
||||||
private readonly string pattern = @"\b(16|20|25|32|40|50|63)\b";
|
|
||||||
|
|
||||||
public bool TryQueryModify(IQueryCollection collection, out QueryString queryString)
|
|
||||||
{
|
|
||||||
queryString = QueryString.Empty;
|
|
||||||
var query = collection["query"].ToString();
|
|
||||||
if (string.IsNullOrEmpty(query))
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
var matches = Regex.Matches(query, pattern);
|
|
||||||
StringBuilder sb = new();
|
|
||||||
sb.Append("Монтажная гильза ");
|
|
||||||
if (matches.Count > 0)
|
|
||||||
{
|
|
||||||
sb.Append(matches.First());
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
QueryBuilder qb = new()
|
|
||||||
{
|
|
||||||
{"query", sb.ToString() }
|
|
||||||
};
|
|
||||||
queryString = qb.ToQueryString();
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,16 +0,0 @@
|
|||||||
namespace RhSolutions.Api.Services
|
|
||||||
{
|
|
||||||
public class StabilPipeQueryModifier : FlexPipeQueryModifier
|
|
||||||
{
|
|
||||||
protected override string diameterPattern => @"16|20|25|32|40";
|
|
||||||
protected override string pipeName => "Stabil -PLATINUM";
|
|
||||||
protected override Dictionary<string, string> diameterNames => new()
|
|
||||||
{
|
|
||||||
["16"] = "16,2х2,6",
|
|
||||||
["20"] = "20х2,9",
|
|
||||||
["25"] = "25х3,7",
|
|
||||||
["32"] = "32х4,7",
|
|
||||||
["40"] = "40х6,0"
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,42 +0,0 @@
|
|||||||
using Microsoft.AspNetCore.Http.Extensions;
|
|
||||||
using System.Text;
|
|
||||||
using System.Text.RegularExpressions;
|
|
||||||
|
|
||||||
namespace RhSolutions.Api.Services
|
|
||||||
{
|
|
||||||
public class TPieceQueryModifier : IProductQueryModifier
|
|
||||||
{
|
|
||||||
private readonly string pattern = @"16|20|25|32|40|50|63";
|
|
||||||
|
|
||||||
public bool TryQueryModify(IQueryCollection collection, out QueryString queryString)
|
|
||||||
{
|
|
||||||
queryString = QueryString.Empty;
|
|
||||||
var query = collection["query"].ToString();
|
|
||||||
if (string.IsNullOrEmpty(query))
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
var matches = Regex.Matches(query, pattern);
|
|
||||||
StringBuilder sb = new();
|
|
||||||
sb.Append("Тройник RAUTITAN -PLATINUM");
|
|
||||||
if (matches.Count == 1)
|
|
||||||
{
|
|
||||||
sb.Append($" {matches.First().Value}-{matches.First().Value}-{matches.First().Value}");
|
|
||||||
}
|
|
||||||
else if (matches.Count >= 3)
|
|
||||||
{
|
|
||||||
sb.Append($" {matches[0].Value}-{matches[1].Value}-{matches[2].Value}");
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
QueryBuilder qb = new()
|
|
||||||
{
|
|
||||||
{ "query", sb.ToString() }
|
|
||||||
};
|
|
||||||
queryString = qb.ToQueryString();
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -5,8 +5,6 @@ VisualStudioVersion = 17.0.31903.59
|
|||||||
MinimumVisualStudioVersion = 10.0.40219.1
|
MinimumVisualStudioVersion = 10.0.40219.1
|
||||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RhSolutions.Api", "RhSolutions.Api\RhSolutions.Api.csproj", "{FD778359-7E92-4B5C-A4F9-7942A28E58F5}"
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RhSolutions.Api", "RhSolutions.Api\RhSolutions.Api.csproj", "{FD778359-7E92-4B5C-A4F9-7942A28E58F5}"
|
||||||
EndProject
|
EndProject
|
||||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RhSolutions.Api.Tests", "RhSolutions.Api.Tests\RhSolutions.Api.Tests.csproj", "{9A4E9EFA-B854-4C29-AA94-A6CE074EDABA}"
|
|
||||||
EndProject
|
|
||||||
Global
|
Global
|
||||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
Debug|Any CPU = Debug|Any CPU
|
Debug|Any CPU = Debug|Any CPU
|
||||||
@ -20,9 +18,5 @@ Global
|
|||||||
{FD778359-7E92-4B5C-A4F9-7942A28E58F5}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
{FD778359-7E92-4B5C-A4F9-7942A28E58F5}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
{FD778359-7E92-4B5C-A4F9-7942A28E58F5}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
{FD778359-7E92-4B5C-A4F9-7942A28E58F5}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
{FD778359-7E92-4B5C-A4F9-7942A28E58F5}.Release|Any CPU.Build.0 = Release|Any CPU
|
{FD778359-7E92-4B5C-A4F9-7942A28E58F5}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
{9A4E9EFA-B854-4C29-AA94-A6CE074EDABA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
|
||||||
{9A4E9EFA-B854-4C29-AA94-A6CE074EDABA}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
|
||||||
{9A4E9EFA-B854-4C29-AA94-A6CE074EDABA}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
|
||||||
{9A4E9EFA-B854-4C29-AA94-A6CE074EDABA}.Release|Any CPU.Build.0 = Release|Any CPU
|
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
EndGlobal
|
EndGlobal
|
||||||
|
Loading…
x
Reference in New Issue
Block a user