Добавлен ввод конфигурации для Setpoint
All checks were successful
Unit tests / test (push) Successful in 48s

This commit is contained in:
Шибалкин Александр 2025-01-17 16:39:36 +05:00
parent ee5425cf6d
commit e2b2fed68f
10 changed files with 104 additions and 30 deletions

View File

@ -29,9 +29,9 @@ public class SetpointController : ControllerBase, ISetpointApi
/// <param name="token"></param> /// <param name="token"></param>
/// <returns></returns> /// <returns></returns>
[HttpGet("current")] [HttpGet("current")]
public async Task<ActionResult<IEnumerable<SetpointValueDto>>> GetCurrent([FromQuery] IEnumerable<Guid> setpointKeys, CancellationToken token) public async Task<ActionResult<Dictionary<Guid, object>>> GetCurrent([FromQuery] IEnumerable<Guid> setpointKeys, CancellationToken token)
{ {
var result = await setpointRepository.GetCurrent(setpointKeys, token); var result = await setpointRepository.GetCurrentDictionary(setpointKeys, token);
return Ok(result); return Ok(result);
} }

View File

@ -29,8 +29,8 @@ public interface ISetpointClient : IDisposable
/// </summary> /// </summary>
/// <param name="setpointConfigs"></param> /// <param name="setpointConfigs"></param>
/// <param name="token"></param> /// <param name="token"></param>
/// <returns></returns> /// <returns></returns>s
Task<Dictionary<Guid, object?>> GetCurrentDictionary(Dictionary<Guid, Type> setpointConfigs, CancellationToken token); Task<Dictionary<Guid, object>> GetCurrentDictionary(IEnumerable<Guid> setpointConfigs, CancellationToken token);
/// <summary> /// <summary>

View File

@ -1,5 +1,6 @@
using DD.Persistence.Models; using DD.Persistence.Models;
using Refit; using Refit;
using System.Text.Json;
namespace DD.Persistence.Client.Clients.Interfaces.Refit; namespace DD.Persistence.Client.Clients.Interfaces.Refit;
@ -7,8 +8,11 @@ public interface IRefitSetpointClient : IRefitClient, IDisposable
{ {
private const string BaseRoute = "/api/setpoint"; private const string BaseRoute = "/api/setpoint";
//[Get($"{BaseRoute}/current")]
//Task<IApiResponse<IEnumerable<SetpointValueDto>>> GetCurrent([Query(CollectionFormat.Multi)] IEnumerable<Guid> setpointKeys, CancellationToken token);
[Get($"{BaseRoute}/current")] [Get($"{BaseRoute}/current")]
Task<IApiResponse<IEnumerable<SetpointValueDto>>> GetCurrent([Query(CollectionFormat.Multi)] IEnumerable<Guid> setpointKeys, CancellationToken token); Task<IApiResponse<Dictionary<Guid, JsonElement>>> GetCurrent([Query(CollectionFormat.Multi)] IEnumerable<Guid> setpointKeys, CancellationToken token);
[Get($"{BaseRoute}/history")] [Get($"{BaseRoute}/history")]
Task<IApiResponse<IEnumerable<SetpointValueDto>>> GetHistory([Query(CollectionFormat.Multi)] IEnumerable<Guid> setpointKeys, [Query] DateTimeOffset historyMoment, CancellationToken token); Task<IApiResponse<IEnumerable<SetpointValueDto>>> GetHistory([Query(CollectionFormat.Multi)] IEnumerable<Guid> setpointKeys, [Query] DateTimeOffset historyMoment, CancellationToken token);

View File

@ -12,49 +12,66 @@ namespace DD.Persistence.Client.Clients;
public class SetpointClient : BaseClient, ISetpointClient public class SetpointClient : BaseClient, ISetpointClient
{ {
private readonly IRefitSetpointClient refitSetpointClient; private readonly IRefitSetpointClient refitSetpointClient;
private readonly ISetpointConfigStorage setpointConfigStorage;
public SetpointClient(IRefitClientFactory<IRefitSetpointClient> refitSetpointClientFactory, ILogger<SetpointClient> logger) : base(logger) public SetpointClient(
IRefitClientFactory<IRefitSetpointClient> refitSetpointClientFactory,
ISetpointConfigStorage setpointConfigStorage,
ILogger<SetpointClient> logger) : base(logger)
{ {
this.refitSetpointClient = refitSetpointClientFactory.Create(); this.refitSetpointClient = refitSetpointClientFactory.Create();
} this.setpointConfigStorage = setpointConfigStorage;
}
public async Task<IEnumerable<SetpointValueDto>> GetCurrent(IEnumerable<Guid> setpointKeys, CancellationToken token) public async Task<IEnumerable<SetpointValueDto>> GetCurrent(IEnumerable<Guid> setpointKeys, CancellationToken token)
{ {
var result = await ExecuteGetResponse( var result = await ExecuteGetResponse(
async () => await refitSetpointClient.GetCurrent(setpointKeys, token), token); 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<Dictionary<Guid, object?>> GetCurrentDictionary(Dictionary<Guid, Type> setpointConfigs, CancellationToken token) private object DeserializeValue(Guid key, JsonElement value)
{ {
var data = await GetCurrent(setpointConfigs.Keys, token); if (setpointConfigStorage.TryGetType(key, out var type))
var dict = DeserializeResultToDict(setpointConfigs, data); return value.Deserialize(type)!;
return dict; return value;
} }
public async Task<Dictionary<Guid, object>> GetCurrentDictionary(IEnumerable<Guid> setpointConfigs, CancellationToken token)
private static Dictionary<Guid, object?> DeserializeResultToDict(Dictionary<Guid, Type> setpointConfigs, IEnumerable<SetpointValueDto> data)
{ {
var dict = new Dictionary<Guid, object?>(); var result = await ExecuteGetResponse(
async () => await refitSetpointClient.GetCurrent(setpointConfigs, token), token);
foreach (var valueDto in data) return result!.ToDictionary(x => x.Key,x => DeserializeValue(x.Key,x.Value));
{
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;
} }
//private Dictionary<Guid, object?> DeserializeResultToDict(IEnumerable<SetpointValueDto> data)
//{
// var dict = new Dictionary<Guid, object?>();
// 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<IEnumerable<SetpointValueDto>> GetHistory(IEnumerable<Guid> setpointKeys, DateTimeOffset historyMoment, CancellationToken token) public async Task<IEnumerable<SetpointValueDto>> GetHistory(IEnumerable<Guid> setpointKeys, DateTimeOffset historyMoment, CancellationToken token)
{ {
var result = await ExecuteGetResponse( var result = await ExecuteGetResponse(

View File

@ -15,7 +15,7 @@ public static class DependencyInjection
/// </summary> /// </summary>
/// <param name="services"></param> /// <param name="services"></param>
/// <returns></returns> /// <returns></returns>
public static IServiceCollection AddPersistenceClients(this IServiceCollection services) public static IServiceCollection AddPersistenceClients(this IServiceCollection services, Dictionary<Guid, Type>? setpointTypeConfigs = null)
{ {
services.AddTransient(typeof(IRefitClientFactory<>), typeof(RefitClientFactory<>)); services.AddTransient(typeof(IRefitClientFactory<>), typeof(RefitClientFactory<>));
services.AddTransient<IChangeLogClient, ChangeLogClient>(); services.AddTransient<IChangeLogClient, ChangeLogClient>();
@ -25,6 +25,11 @@ public static class DependencyInjection
services.AddTransient<ITimeSeriesClient<DataSaubDto>, TimeSeriesClient<DataSaubDto>>(); services.AddTransient<ITimeSeriesClient<DataSaubDto>, TimeSeriesClient<DataSaubDto>>();
services.AddTransient<ITimestampedSetClient, TimestampedSetClient>(); services.AddTransient<ITimestampedSetClient, TimestampedSetClient>();
services.AddTransient<IWitsDataClient, WitsDataClient>(); services.AddTransient<IWitsDataClient, WitsDataClient>();
services.AddSingleton<ISetpointConfigStorage, SetpointConfigStorage>(provider =>
{
return new SetpointConfigStorage(setpointTypeConfigs);
});
return services; return services;
} }
} }

View File

@ -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);
}

View File

@ -0,0 +1,15 @@
namespace DD.Persistence.Client;
internal class SetpointConfigStorage : ISetpointConfigStorage
{
private readonly Dictionary<Guid, Type> setpointTypeConfigs;
public SetpointConfigStorage(Dictionary<Guid, Type>? setpointTypeConfigs)
{
this.setpointTypeConfigs = setpointTypeConfigs?? new Dictionary<Guid, Type>();
}
public bool TryGetType(Guid id, out Type type)
{
return setpointTypeConfigs.TryGetValue(id, out type);
}
}

View File

@ -32,6 +32,18 @@ namespace DD.Persistence.Repository.Repositories
var dtos = entities.Select(e => e.Adapt<SetpointValueDto>()); var dtos = entities.Select(e => e.Adapt<SetpointValueDto>());
return dtos; return dtos;
} }
public async Task<Dictionary<Guid, object>> GetCurrentDictionary(IEnumerable<Guid> 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<IEnumerable<SetpointValueDto>> GetHistory(IEnumerable<Guid> setpointKeys, DateTimeOffset historyMoment, CancellationToken token) public async Task<IEnumerable<SetpointValueDto>> GetHistory(IEnumerable<Guid> setpointKeys, DateTimeOffset historyMoment, CancellationToken token)
{ {
@ -107,5 +119,7 @@ namespace DD.Persistence.Repository.Repositories
await db.Set<Setpoint>().AddAsync(entity, token); await db.Set<Setpoint>().AddAsync(entity, token);
await db.SaveChangesAsync(token); await db.SaveChangesAsync(token);
} }
} }
} }

View File

@ -14,7 +14,7 @@ public interface ISetpointApi : ISyncApi<SetpointLogDto>
/// <param name="setpoitKeys">ключи уставок</param> /// <param name="setpoitKeys">ключи уставок</param>
/// <param name="token"></param> /// <param name="token"></param>
/// <returns></returns> /// <returns></returns>
Task<ActionResult<IEnumerable<SetpointValueDto>>> GetCurrent(IEnumerable<Guid> setpoitKeys, CancellationToken token); Task<ActionResult<Dictionary<Guid, object>>> GetCurrent(IEnumerable<Guid> setpoitKeys, CancellationToken token);
/// <summary> /// <summary>
/// Получить значения уставок за определенный момент времени /// Получить значения уставок за определенный момент времени

View File

@ -16,6 +16,14 @@ public interface ISetpointRepository
/// <returns></returns> /// <returns></returns>
Task<IEnumerable<SetpointValueDto>> GetCurrent(IEnumerable<Guid> setpointKeys, CancellationToken token); Task<IEnumerable<SetpointValueDto>> GetCurrent(IEnumerable<Guid> setpointKeys, CancellationToken token);
/// <summary>
/// Получить значения уставок по набору ключей
/// </summary>
/// <param name="setpointKeys"></param>
/// <param name="token"></param>
/// <returns></returns>
Task<Dictionary<Guid, object>> GetCurrentDictionary(IEnumerable<Guid> setpointKeys, CancellationToken token);
/// <summary> /// <summary>
/// Получить значения уставок за определенный момент времени /// Получить значения уставок за определенный момент времени
/// </summary> /// </summary>