From e2b2fed68f558952a1aa7e459990975b78efac46 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A8=D0=B8=D0=B1=D0=B0=D0=BB=D0=BA=D0=B8=D0=BD=20=D0=90?= =?UTF-8?q?=D0=BB=D0=B5=D0=BA=D1=81=D0=B0=D0=BD=D0=B4=D1=80?= Date: Fri, 17 Jan 2025 16:39:36 +0500 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=20=D0=B2=D0=B2=D0=BE=D0=B4=20=D0=BA=D0=BE=D0=BD=D1=84?= =?UTF-8?q?=D0=B8=D0=B3=D1=83=D1=80=D0=B0=D1=86=D0=B8=D0=B8=20=D0=B4=D0=BB?= =?UTF-8?q?=D1=8F=20Setpoint?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/SetpointController.cs | 4 +- .../Clients/Interfaces/ISetpointClient.cs | 4 +- .../Interfaces/Refit/IRefitSetpointClient.cs | 6 +- .../Clients/SetpointClient.cs | 63 ++++++++++++------- DD.Persistence.Client/DependencyInjection.cs | 7 ++- .../ISetpointConfigStorage.cs | 11 ++++ .../SetpointConfigStorage.cs | 15 +++++ .../Repositories/SetpointRepository.cs | 14 +++++ DD.Persistence/API/ISetpointApi.cs | 2 +- .../Repositories/ISetpointRepository.cs | 8 +++ 10 files changed, 104 insertions(+), 30 deletions(-) create mode 100644 DD.Persistence.Client/ISetpointConfigStorage.cs create mode 100644 DD.Persistence.Client/SetpointConfigStorage.cs diff --git a/DD.Persistence.API/Controllers/SetpointController.cs b/DD.Persistence.API/Controllers/SetpointController.cs index 1d4c285..0850438 100644 --- a/DD.Persistence.API/Controllers/SetpointController.cs +++ b/DD.Persistence.API/Controllers/SetpointController.cs @@ -29,9 +29,9 @@ public class SetpointController : ControllerBase, ISetpointApi /// /// [HttpGet("current")] - public async Task>> GetCurrent([FromQuery] IEnumerable setpointKeys, CancellationToken token) + public async Task>> GetCurrent([FromQuery] IEnumerable setpointKeys, CancellationToken token) { - var result = await setpointRepository.GetCurrent(setpointKeys, token); + var result = await setpointRepository.GetCurrentDictionary(setpointKeys, token); return Ok(result); } diff --git a/DD.Persistence.Client/Clients/Interfaces/ISetpointClient.cs b/DD.Persistence.Client/Clients/Interfaces/ISetpointClient.cs index 36ede35..f197fe8 100644 --- a/DD.Persistence.Client/Clients/Interfaces/ISetpointClient.cs +++ b/DD.Persistence.Client/Clients/Interfaces/ISetpointClient.cs @@ -29,8 +29,8 @@ public interface ISetpointClient : IDisposable /// /// /// - /// - Task> GetCurrentDictionary(Dictionary setpointConfigs, CancellationToken token); + /// s + Task> GetCurrentDictionary(IEnumerable setpointConfigs, CancellationToken token); /// diff --git a/DD.Persistence.Client/Clients/Interfaces/Refit/IRefitSetpointClient.cs b/DD.Persistence.Client/Clients/Interfaces/Refit/IRefitSetpointClient.cs index 1acb398..1cd2742 100644 --- a/DD.Persistence.Client/Clients/Interfaces/Refit/IRefitSetpointClient.cs +++ b/DD.Persistence.Client/Clients/Interfaces/Refit/IRefitSetpointClient.cs @@ -1,5 +1,6 @@ using DD.Persistence.Models; using Refit; +using System.Text.Json; namespace DD.Persistence.Client.Clients.Interfaces.Refit; @@ -7,8 +8,11 @@ public interface IRefitSetpointClient : IRefitClient, IDisposable { private const string BaseRoute = "/api/setpoint"; + //[Get($"{BaseRoute}/current")] + //Task>> GetCurrent([Query(CollectionFormat.Multi)] IEnumerable setpointKeys, CancellationToken token); + [Get($"{BaseRoute}/current")] - Task>> GetCurrent([Query(CollectionFormat.Multi)] IEnumerable setpointKeys, CancellationToken token); + Task>> GetCurrent([Query(CollectionFormat.Multi)] IEnumerable setpointKeys, CancellationToken token); [Get($"{BaseRoute}/history")] Task>> GetHistory([Query(CollectionFormat.Multi)] IEnumerable setpointKeys, [Query] DateTimeOffset historyMoment, CancellationToken token); diff --git a/DD.Persistence.Client/Clients/SetpointClient.cs b/DD.Persistence.Client/Clients/SetpointClient.cs index 0432949..3078d9d 100644 --- a/DD.Persistence.Client/Clients/SetpointClient.cs +++ b/DD.Persistence.Client/Clients/SetpointClient.cs @@ -12,49 +12,66 @@ namespace DD.Persistence.Client.Clients; public class SetpointClient : BaseClient, ISetpointClient { private readonly IRefitSetpointClient refitSetpointClient; + private readonly ISetpointConfigStorage setpointConfigStorage; - public SetpointClient(IRefitClientFactory refitSetpointClientFactory, ILogger logger) : base(logger) + public SetpointClient( + IRefitClientFactory refitSetpointClientFactory, + ISetpointConfigStorage setpointConfigStorage, + ILogger logger) : base(logger) { this.refitSetpointClient = refitSetpointClientFactory.Create(); - } + this.setpointConfigStorage = setpointConfigStorage; + } public async Task> GetCurrent(IEnumerable setpointKeys, CancellationToken token) { var result = await ExecuteGetResponse( async () => await refitSetpointClient.GetCurrent(setpointKeys, token), token); - return result!; + return result!.Select(x => new SetpointValueDto { + Key = x.Key, + Value = DeserializeValue(x.Key, x.Value) + }); } - public async Task> GetCurrentDictionary(Dictionary setpointConfigs, CancellationToken token) + private object DeserializeValue(Guid key, JsonElement value) { - var data = await GetCurrent(setpointConfigs.Keys, token); - var dict = DeserializeResultToDict(setpointConfigs, data); + if (setpointConfigStorage.TryGetType(key, out var type)) + return value.Deserialize(type)!; - return dict; + return value; } - - private static Dictionary DeserializeResultToDict(Dictionary setpointConfigs, IEnumerable data) + public async Task> GetCurrentDictionary(IEnumerable setpointConfigs, CancellationToken token) { - var dict = new Dictionary(); + var result = await ExecuteGetResponse( + async () => await refitSetpointClient.GetCurrent(setpointConfigs, token), token); - foreach (var valueDto in data) - { - if (valueDto.Value is not null && - valueDto.Value is JsonElement element && - setpointConfigs.TryGetValue(valueDto.Key, out var type) && - type is not null) - - dict[valueDto.Key] = element.Deserialize(type) ?? valueDto.Value; - else - dict[valueDto.Key] = valueDto.Value; - } - - return dict; + return result!.ToDictionary(x => x.Key,x => DeserializeValue(x.Key,x.Value)); } + + //private Dictionary DeserializeResultToDict(IEnumerable data) + //{ + // var dict = new Dictionary(); + + + // foreach (var valueDto in data) + // { + // if (valueDto.Value is not null && + // valueDto.Value is JsonElement element && + // setpointConfigStorage.TryGetType(valueDto.Key, out var type) && + // type is not null) + + // dict[valueDto.Key] = element.Deserialize(type) ?? valueDto.Value; + // else + // dict[valueDto.Key] = valueDto.Value; + // } + + // return dict; + //} + public async Task> GetHistory(IEnumerable setpointKeys, DateTimeOffset historyMoment, CancellationToken token) { var result = await ExecuteGetResponse( diff --git a/DD.Persistence.Client/DependencyInjection.cs b/DD.Persistence.Client/DependencyInjection.cs index eced892..e5c7c29 100644 --- a/DD.Persistence.Client/DependencyInjection.cs +++ b/DD.Persistence.Client/DependencyInjection.cs @@ -15,7 +15,7 @@ public static class DependencyInjection /// /// /// - public static IServiceCollection AddPersistenceClients(this IServiceCollection services) + public static IServiceCollection AddPersistenceClients(this IServiceCollection services, Dictionary? setpointTypeConfigs = null) { services.AddTransient(typeof(IRefitClientFactory<>), typeof(RefitClientFactory<>)); services.AddTransient(); @@ -25,6 +25,11 @@ public static class DependencyInjection services.AddTransient, TimeSeriesClient>(); services.AddTransient(); services.AddTransient(); + + services.AddSingleton(provider => + { + return new SetpointConfigStorage(setpointTypeConfigs); + }); return services; } } diff --git a/DD.Persistence.Client/ISetpointConfigStorage.cs b/DD.Persistence.Client/ISetpointConfigStorage.cs new file mode 100644 index 0000000..2c73783 --- /dev/null +++ b/DD.Persistence.Client/ISetpointConfigStorage.cs @@ -0,0 +1,11 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DD.Persistence.Client; +public interface ISetpointConfigStorage +{ + bool TryGetType(Guid id, out Type type); +} diff --git a/DD.Persistence.Client/SetpointConfigStorage.cs b/DD.Persistence.Client/SetpointConfigStorage.cs new file mode 100644 index 0000000..a0b3a17 --- /dev/null +++ b/DD.Persistence.Client/SetpointConfigStorage.cs @@ -0,0 +1,15 @@ +namespace DD.Persistence.Client; +internal class SetpointConfigStorage : ISetpointConfigStorage +{ + private readonly Dictionary setpointTypeConfigs; + + public SetpointConfigStorage(Dictionary? setpointTypeConfigs) + { + this.setpointTypeConfigs = setpointTypeConfigs?? new Dictionary(); + } + + public bool TryGetType(Guid id, out Type type) + { + return setpointTypeConfigs.TryGetValue(id, out type); + } +} diff --git a/DD.Persistence.Repository/Repositories/SetpointRepository.cs b/DD.Persistence.Repository/Repositories/SetpointRepository.cs index 321d881..30e67d5 100644 --- a/DD.Persistence.Repository/Repositories/SetpointRepository.cs +++ b/DD.Persistence.Repository/Repositories/SetpointRepository.cs @@ -32,6 +32,18 @@ namespace DD.Persistence.Repository.Repositories var dtos = entities.Select(e => e.Adapt()); return dtos; } + public async Task> GetCurrentDictionary(IEnumerable setpointKeys, CancellationToken token) + { + var query = GetQueryReadOnly(); + + var entities = await query + .Where(e => setpointKeys.Contains(e.Key)) + .GroupBy(e => e.Key) + .Select(g => g.OrderByDescending(x => x.Created).FirstOrDefault()) + .ToDictionaryAsync(x=> x.Key, x => (object)x.Value, token); + + return entities; + } public async Task> GetHistory(IEnumerable setpointKeys, DateTimeOffset historyMoment, CancellationToken token) { @@ -107,5 +119,7 @@ namespace DD.Persistence.Repository.Repositories await db.Set().AddAsync(entity, token); await db.SaveChangesAsync(token); } + + } } diff --git a/DD.Persistence/API/ISetpointApi.cs b/DD.Persistence/API/ISetpointApi.cs index f600f99..138e336 100644 --- a/DD.Persistence/API/ISetpointApi.cs +++ b/DD.Persistence/API/ISetpointApi.cs @@ -14,7 +14,7 @@ public interface ISetpointApi : ISyncApi /// ключи уставок /// /// - Task>> GetCurrent(IEnumerable setpoitKeys, CancellationToken token); + Task>> GetCurrent(IEnumerable setpoitKeys, CancellationToken token); /// /// Получить значения уставок за определенный момент времени diff --git a/DD.Persistence/Repositories/ISetpointRepository.cs b/DD.Persistence/Repositories/ISetpointRepository.cs index 016a6c5..165ce29 100644 --- a/DD.Persistence/Repositories/ISetpointRepository.cs +++ b/DD.Persistence/Repositories/ISetpointRepository.cs @@ -16,6 +16,14 @@ public interface ISetpointRepository /// Task> GetCurrent(IEnumerable setpointKeys, CancellationToken token); + /// + /// Получить значения уставок по набору ключей + /// + /// + /// + /// + Task> GetCurrentDictionary(IEnumerable setpointKeys, CancellationToken token); + /// /// Получить значения уставок за определенный момент времени ///