Compare commits
7 Commits
a8ec0cee16
...
799b3f40a2
Author | SHA1 | Date | |
---|---|---|---|
799b3f40a2 | |||
019cabaf13 | |||
3317e20358 | |||
5e28f428f8 | |||
f44c1f25b3 | |||
38e5eb0393 | |||
12b557f53d |
1
.gitignore
vendored
1
.gitignore
vendored
@ -452,3 +452,4 @@ $RECYCLE.BIN/
|
||||
!.vscode/tasks.json
|
||||
!.vscode/launch.json
|
||||
!.vscode/extensions.json
|
||||
/RhSolutions.Api/MLModels
|
||||
|
1
README
Normal file
1
README
Normal file
@ -0,0 +1 @@
|
||||
Серверное API для работы формул плагина RhSolutions для Excel
|
40
RhSolutions.Api.Tests/ProductQueryModifierTests.cs
Normal file
40
RhSolutions.Api.Tests/ProductQueryModifierTests.cs
Normal file
@ -0,0 +1,40 @@
|
||||
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));
|
||||
}
|
||||
}
|
23
RhSolutions.Api.Tests/RhSolutions.Api.Tests.csproj
Normal file
23
RhSolutions.Api.Tests/RhSolutions.Api.Tests.csproj
Normal file
@ -0,0 +1,23 @@
|
||||
<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
RhSolutions.Api.Tests/Usings.cs
Normal file
1
RhSolutions.Api.Tests/Usings.cs
Normal file
@ -0,0 +1 @@
|
||||
global using NUnit.Framework;
|
30
RhSolutions.Api/Middleware/QueryModifier.cs
Normal file
30
RhSolutions.Api/Middleware/QueryModifier.cs
Normal file
@ -0,0 +1,30 @@
|
||||
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,6 +1,7 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using RhSolutions.Models;
|
||||
using RhSolutions.Api.Services;
|
||||
using RhSolutions.Api.Middleware;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
@ -21,12 +22,15 @@ builder.Services.AddDbContext<RhSolutionsContext>(opts =>
|
||||
opts.EnableSensitiveDataLogging(true);
|
||||
}
|
||||
});
|
||||
builder.Services.AddScoped<IPricelistParser, ClosedXMLParser>();
|
||||
builder.Services.AddScoped<IPricelistParser, ClosedXMLParser>()
|
||||
.AddScoped<IProductTypePredicter, ProductTypePredicter>()
|
||||
.AddSingleton<ProductQueryModifierFactory>();
|
||||
builder.Services.AddControllers();
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
app.MapControllers();
|
||||
app.UseMiddleware<QueryModifier>();
|
||||
|
||||
var context = app.Services.CreateScope().ServiceProvider
|
||||
.GetRequiredService<RhSolutionsContext>();
|
||||
|
@ -13,9 +13,16 @@
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Microsoft.ML" Version="2.0.1" />
|
||||
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="7.0.4" />
|
||||
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL.Design" Version="1.1.0" />
|
||||
<PackageReference Include="Rhsolutions.ProductSku" Version="1.0.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<None Update="MLModels\model.zip">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</None>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
11
RhSolutions.Api/Services/BypassQueryModifier.cs
Normal file
11
RhSolutions.Api/Services/BypassQueryModifier.cs
Normal file
@ -0,0 +1,11 @@
|
||||
namespace RhSolutions.Api.Services
|
||||
{
|
||||
public class BypassQueryModifier : IProductQueryModifier
|
||||
{
|
||||
public bool TryQueryModify(IQueryCollection collection, out QueryString queryString)
|
||||
{
|
||||
queryString = QueryString.Empty;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
76
RhSolutions.Api/Services/FlexPipeQueryModifier.cs
Normal file
76
RhSolutions.Api/Services/FlexPipeQueryModifier.cs
Normal file
@ -0,0 +1,76 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
7
RhSolutions.Api/Services/IProductQueryModifier.cs
Normal file
7
RhSolutions.Api/Services/IProductQueryModifier.cs
Normal file
@ -0,0 +1,7 @@
|
||||
namespace RhSolutions.Api.Services
|
||||
{
|
||||
public interface IProductQueryModifier
|
||||
{
|
||||
public bool TryQueryModify(IQueryCollection collection, out QueryString queryString);
|
||||
}
|
||||
}
|
6
RhSolutions.Api/Services/IProductTypePredicter.cs
Normal file
6
RhSolutions.Api/Services/IProductTypePredicter.cs
Normal file
@ -0,0 +1,6 @@
|
||||
namespace RhSolutions.Api.Services;
|
||||
|
||||
public interface IProductTypePredicter
|
||||
{
|
||||
public string? GetPredictedProductType(string productName);
|
||||
}
|
21
RhSolutions.Api/Services/ProductQueryModifierFactory.cs
Normal file
21
RhSolutions.Api/Services/ProductQueryModifierFactory.cs
Normal file
@ -0,0 +1,21 @@
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
44
RhSolutions.Api/Services/ProductTypePredicter.cs
Normal file
44
RhSolutions.Api/Services/ProductTypePredicter.cs
Normal file
@ -0,0 +1,44 @@
|
||||
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; }
|
||||
}
|
||||
}
|
||||
|
38
RhSolutions.Api/Services/SleeveQueryModifier.cs
Normal file
38
RhSolutions.Api/Services/SleeveQueryModifier.cs
Normal file
@ -0,0 +1,38 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
16
RhSolutions.Api/Services/StabilPipeQueryModifier.cs
Normal file
16
RhSolutions.Api/Services/StabilPipeQueryModifier.cs
Normal file
@ -0,0 +1,16 @@
|
||||
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"
|
||||
};
|
||||
}
|
||||
}
|
42
RhSolutions.Api/Services/TPieceQueryModifier.cs
Normal file
42
RhSolutions.Api/Services/TPieceQueryModifier.cs
Normal file
@ -0,0 +1,42 @@
|
||||
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,6 +5,8 @@ VisualStudioVersion = 17.0.31903.59
|
||||
MinimumVisualStudioVersion = 10.0.40219.1
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RhSolutions.Api", "RhSolutions.Api\RhSolutions.Api.csproj", "{FD778359-7E92-4B5C-A4F9-7942A28E58F5}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RhSolutions.Api.Tests", "RhSolutions.Api.Tests\RhSolutions.Api.Tests.csproj", "{9A4E9EFA-B854-4C29-AA94-A6CE074EDABA}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
@ -18,5 +20,9 @@ Global
|
||||
{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.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
|
||||
EndGlobal
|
||||
|
Loading…
x
Reference in New Issue
Block a user