From 9736b41f1a3ea9fe5cf4e687aee75c7b64f8f575 Mon Sep 17 00:00:00 2001 From: Roman Efremov Date: Wed, 15 Jan 2025 17:37:48 +0500 Subject: [PATCH] =?UTF-8?q?=D0=98=D0=B7=D0=BC=D0=B5=D0=BD=D0=B8=D1=82?= =?UTF-8?q?=D1=8C=20=D0=BA=D0=BB=D0=B8=D0=B5=D0=BD=D1=82=D0=B0,=20=D1=82?= =?UTF-8?q?=D0=B5=D1=81=D1=82=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...ller.cs => TimestampedValuesController.cs} | 4 +- DD.Persistence.App/appsettings.Tests.json | 2 +- DD.Persistence.App/appsettings.json | 2 +- .../Clients/Interfaces/ITimeSeriesClient.cs | 45 ------- .../Interfaces/ITimestampedSetClient.cs | 60 --------- .../Interfaces/ITimestampedValuesClient.cs | 70 ++++++++++ .../Refit/IRefitTimeSeriesClient.cs | 22 ---- .../Refit/IRefitTimestampedValuesClient.cs | 8 +- .../Clients/TimeSeriesClient.cs | 56 -------- .../Clients/TimestampedSetClient.cs | 64 --------- .../Clients/TimestampedValuesClient.cs | 72 +++++++++++ DD.Persistence.Client/DependencyInjection.cs | 2 +- .../Controllers/DataSaubControllerTest.cs | 85 ------------ .../TimeSeriesBaseControllerTest.cs | 122 ------------------ .../TimestampedSetControllerTest.cs | 41 ++++-- .../TimestampedValuesRepository.cs | 6 +- 16 files changed, 184 insertions(+), 477 deletions(-) rename DD.Persistence.API/Controllers/{TimestampedSetController.cs => TimestampedValuesController.cs} (97%) delete mode 100644 DD.Persistence.Client/Clients/Interfaces/ITimeSeriesClient.cs delete mode 100644 DD.Persistence.Client/Clients/Interfaces/ITimestampedSetClient.cs create mode 100644 DD.Persistence.Client/Clients/Interfaces/ITimestampedValuesClient.cs delete mode 100644 DD.Persistence.Client/Clients/Interfaces/Refit/IRefitTimeSeriesClient.cs delete mode 100644 DD.Persistence.Client/Clients/TimeSeriesClient.cs delete mode 100644 DD.Persistence.Client/Clients/TimestampedSetClient.cs create mode 100644 DD.Persistence.Client/Clients/TimestampedValuesClient.cs delete mode 100644 DD.Persistence.IntegrationTests/Controllers/DataSaubControllerTest.cs delete mode 100644 DD.Persistence.IntegrationTests/Controllers/TimeSeriesBaseControllerTest.cs diff --git a/DD.Persistence.API/Controllers/TimestampedSetController.cs b/DD.Persistence.API/Controllers/TimestampedValuesController.cs similarity index 97% rename from DD.Persistence.API/Controllers/TimestampedSetController.cs rename to DD.Persistence.API/Controllers/TimestampedValuesController.cs index 878ee3d..a059243 100644 --- a/DD.Persistence.API/Controllers/TimestampedSetController.cs +++ b/DD.Persistence.API/Controllers/TimestampedValuesController.cs @@ -13,11 +13,11 @@ namespace DD.Persistence.API.Controllers; [ApiController] //[Authorize] [Route("api/[controller]/{discriminatorId}")] -public class TimestampedSetController : ControllerBase +public class TimestampedValuesController : ControllerBase { private readonly ITimestampedValuesRepository repository; - public TimestampedSetController(ITimestampedValuesRepository repository) + public TimestampedValuesController(ITimestampedValuesRepository repository) { this.repository = repository; } diff --git a/DD.Persistence.App/appsettings.Tests.json b/DD.Persistence.App/appsettings.Tests.json index 9934757..e8d3cc4 100644 --- a/DD.Persistence.App/appsettings.Tests.json +++ b/DD.Persistence.App/appsettings.Tests.json @@ -4,7 +4,7 @@ "Port": 5432, "Database": "persistence", "Username": "postgres", - "Password": "postgres" + "Password": "q" }, "NeedUseKeyCloak": false, "AuthUser": { diff --git a/DD.Persistence.App/appsettings.json b/DD.Persistence.App/appsettings.json index 7ad8c67..0af1529 100644 --- a/DD.Persistence.App/appsettings.json +++ b/DD.Persistence.App/appsettings.json @@ -6,7 +6,7 @@ } }, "ConnectionStrings": { - "DefaultConnection": "Host=localhost;Database=persistence;Username=postgres;Password=postgres;Persist Security Info=True" + "DefaultConnection": "Host=localhost;Database=persistence;Username=postgres;Password=q;Persist Security Info=True" }, "AllowedHosts": "*", "NeedUseKeyCloak": false, diff --git a/DD.Persistence.Client/Clients/Interfaces/ITimeSeriesClient.cs b/DD.Persistence.Client/Clients/Interfaces/ITimeSeriesClient.cs deleted file mode 100644 index c41564c..0000000 --- a/DD.Persistence.Client/Clients/Interfaces/ITimeSeriesClient.cs +++ /dev/null @@ -1,45 +0,0 @@ -using DD.Persistence.Models.Common; -using DD.Persistence.ModelsAbstractions; - -namespace DD.Persistence.Client.Clients.Interfaces; - -/// -/// Клиент для работы с временными данными -/// -/// -public interface ITimeSeriesClient : IDisposable where TDto : class, ITimestampAbstractDto -{ - /// - /// Добавление записей - /// - /// - /// - /// - Task AddRange(IEnumerable dtos, CancellationToken token); - - /// - /// Получить список объектов, удовлетворяющий диапазону дат - /// - /// - /// - /// - /// - Task> Get(DateTimeOffset dateBegin, DateTimeOffset dateEnd, CancellationToken token); - - /// - /// Получить диапазон дат, для которых есть данные в репозитории - /// - /// - /// - Task GetDatesRange(CancellationToken token); - - /// - /// Получить список объектов с прореживанием, удовлетворяющий диапазону дат - /// - /// - /// - /// - /// - /// - Task> GetResampledData(DateTimeOffset dateBegin, double intervalSec = 600, int approxPointsCount = 1024, CancellationToken token = default); -} \ No newline at end of file diff --git a/DD.Persistence.Client/Clients/Interfaces/ITimestampedSetClient.cs b/DD.Persistence.Client/Clients/Interfaces/ITimestampedSetClient.cs deleted file mode 100644 index 701beb9..0000000 --- a/DD.Persistence.Client/Clients/Interfaces/ITimestampedSetClient.cs +++ /dev/null @@ -1,60 +0,0 @@ -using DD.Persistence.Models; -using DD.Persistence.Models.Common; - -namespace DD.Persistence.Client.Clients.Interfaces; - -/// -/// Клиент для работы с репозиторием для хранения разных наборов данных рядов. -/// idDiscriminator - идентифицирует конкретный набор данных, прим.: циклы измерения АСИБР, или отчет о DrillTest. -/// idDiscriminator формируют клиенты и только им известно что они обозначают. -/// Так как данные приходят редко, то их прореживания для построения графиков не предусмотрено. -/// -public interface ITimestampedSetClient : IDisposable -{ - /// - /// Записать новые данные - /// - /// - /// - /// - /// - Task AddRange(Guid idDiscriminator, IEnumerable sets, CancellationToken token); - - /// - /// Количество записей по указанному набору в БД. Для пагинации - /// - /// - /// - /// - Task Count(Guid idDiscriminator, CancellationToken token); - - /// - /// Получение данных с фильтрацией. Значение фильтра null - отключен - /// - /// - /// - /// - /// - /// - /// - /// - Task> Get(Guid idDiscriminator, DateTimeOffset? geTimestamp, IEnumerable? columnNames, int skip, int take, CancellationToken token); - - /// - /// Диапазон дат за которые есть данные - /// - /// - /// - /// - Task GetDatesRange(Guid idDiscriminator, CancellationToken token); - - /// - /// - /// - /// - /// - /// - /// - /// - Task> GetLast(Guid idDiscriminator, IEnumerable? columnNames, int take, CancellationToken token); -} \ No newline at end of file diff --git a/DD.Persistence.Client/Clients/Interfaces/ITimestampedValuesClient.cs b/DD.Persistence.Client/Clients/Interfaces/ITimestampedValuesClient.cs new file mode 100644 index 0000000..ea1f576 --- /dev/null +++ b/DD.Persistence.Client/Clients/Interfaces/ITimestampedValuesClient.cs @@ -0,0 +1,70 @@ +using DD.Persistence.Models; +using DD.Persistence.Models.Common; + +namespace DD.Persistence.Client.Clients.Interfaces; + +/// +/// Клиент для работы с репозиторием для хранения разных наборов данных рядов. +/// discriminatorId - идентифицирует конкретный набор данных, прим.: циклы измерения АСИБР, или отчет о DrillTest. +/// discriminatorId формируют клиенты и только им известно что они обозначают. +/// +public interface ITimestampedValuesClient : IDisposable +{ + /// + /// Записать новые данные + /// + /// + /// + /// + /// + Task AddRange(Guid discriminatorId, IEnumerable sets, CancellationToken token); + + /// + /// Количество записей по указанному набору в БД. Для пагинации + /// + /// + /// + /// + Task Count(Guid discriminatorId, CancellationToken token); + + /// + /// Получение данных с фильтрацией. Значение фильтра null - отключен + /// + /// + /// + /// + /// + /// + /// + /// + Task> Get(Guid discriminatorId, DateTimeOffset? geTimestamp, IEnumerable? columnNames, int skip, int take, CancellationToken token); + + /// + /// Диапазон дат за которые есть данные + /// + /// + /// + /// + Task GetDatesRange(Guid discriminatorId, CancellationToken token); + + /// + /// Получить последние данные + /// + /// + /// + /// + /// + /// + Task> GetLast(Guid discriminatorId, IEnumerable? columnNames, int take, CancellationToken token); + + /// + /// Получить список объектов с прореживанием, удовлетворяющий диапазону дат + /// + /// + /// + /// + /// + /// + /// + Task> GetResampledData(Guid discriminatorId, DateTimeOffset dateBegin, double intervalSec = 600d, int approxPointsCount = 1024, CancellationToken token = default); +} \ No newline at end of file diff --git a/DD.Persistence.Client/Clients/Interfaces/Refit/IRefitTimeSeriesClient.cs b/DD.Persistence.Client/Clients/Interfaces/Refit/IRefitTimeSeriesClient.cs deleted file mode 100644 index 0dc4994..0000000 --- a/DD.Persistence.Client/Clients/Interfaces/Refit/IRefitTimeSeriesClient.cs +++ /dev/null @@ -1,22 +0,0 @@ -using DD.Persistence.Models.Common; -using DD.Persistence.ModelsAbstractions; -using Refit; - -namespace DD.Persistence.Client.Clients.Interfaces.Refit; -public interface IRefitTimeSeriesClient : IRefitClient, IDisposable - where TDto : class, ITimestampAbstractDto -{ - private const string BaseRoute = "/api/dataSaub"; - - [Post($"{BaseRoute}")] - Task> AddRange(IEnumerable dtos, CancellationToken token); - - [Get($"{BaseRoute}")] - Task>> Get(DateTimeOffset dateBegin, DateTimeOffset dateEnd, CancellationToken token); - - [Get($"{BaseRoute}/resampled")] - Task>> GetResampledData(DateTimeOffset dateBegin, double intervalSec = 600d, int approxPointsCount = 1024, CancellationToken token = default); - - [Get($"{BaseRoute}/datesRange")] - Task> GetDatesRange(CancellationToken token); -} diff --git a/DD.Persistence.Client/Clients/Interfaces/Refit/IRefitTimestampedValuesClient.cs b/DD.Persistence.Client/Clients/Interfaces/Refit/IRefitTimestampedValuesClient.cs index bf7a386..2515a57 100644 --- a/DD.Persistence.Client/Clients/Interfaces/Refit/IRefitTimestampedValuesClient.cs +++ b/DD.Persistence.Client/Clients/Interfaces/Refit/IRefitTimestampedValuesClient.cs @@ -6,13 +6,13 @@ namespace DD.Persistence.Client.Clients.Interfaces.Refit; public interface IRefitTimestampedValuesClient : IRefitClient, IDisposable { - private const string baseUrl = "/api/TimestampedSet/{idDiscriminator}"; + private const string baseUrl = "/api/TimestampedValues/{discriminatorId}"; [Post(baseUrl)] - Task> AddRange(Guid idDiscriminator, IEnumerable sets, CancellationToken token); + Task> AddRange(Guid discriminatorId, IEnumerable sets, CancellationToken token); [Get(baseUrl)] - Task>> Get(Guid idDiscriminator, [Query] DateTimeOffset? geTimestamp, [Query] IEnumerable? columnNames, int skip, int take, CancellationToken token); + Task>> Get(Guid discriminatorId, [Query] DateTimeOffset? geTimestamp, [Query] IEnumerable? columnNames, int skip, int take, CancellationToken token); [Get($"{baseUrl}/last")] Task>> GetLast(Guid discriminatorId, [Query] IEnumerable? columnNames, int take, CancellationToken token); @@ -24,5 +24,5 @@ public interface IRefitTimestampedValuesClient : IRefitClient, IDisposable Task> GetDatesRange(Guid discriminatorId, CancellationToken token); [Get($"{baseUrl}/resampled")] - Task>> GetResampledData(DateTimeOffset dateBegin, double intervalSec = 600d, int approxPointsCount = 1024, CancellationToken token = default); + Task>> GetResampledData(Guid discriminatorId, DateTimeOffset dateBegin, double intervalSec = 600d, int approxPointsCount = 1024, CancellationToken token = default); } diff --git a/DD.Persistence.Client/Clients/TimeSeriesClient.cs b/DD.Persistence.Client/Clients/TimeSeriesClient.cs deleted file mode 100644 index 10ff2e4..0000000 --- a/DD.Persistence.Client/Clients/TimeSeriesClient.cs +++ /dev/null @@ -1,56 +0,0 @@ -using Microsoft.Extensions.Logging; -using DD.Persistence.Client.Clients.Base; -using DD.Persistence.Client.Clients.Interfaces; -using DD.Persistence.Client.Clients.Interfaces.Refit; -using DD.Persistence.Models.Common; -using DD.Persistence.ModelsAbstractions; - -namespace DD.Persistence.Client.Clients; -public class TimeSeriesClient : BaseClient, ITimeSeriesClient where TDto : class, ITimestampAbstractDto -{ - private readonly IRefitTimeSeriesClient timeSeriesClient; - - public TimeSeriesClient(IRefitClientFactory> refitTechMessagesClientFactory, ILogger> logger) : base(logger) - { - this.timeSeriesClient = refitTechMessagesClientFactory.Create(); - } - - public async Task AddRange(IEnumerable dtos, CancellationToken token) - { - var result = await ExecutePostResponse( - async () => await timeSeriesClient.AddRange(dtos, token), token); - - return result; - } - - public async Task> Get(DateTimeOffset dateBegin, DateTimeOffset dateEnd, CancellationToken token) - { - var result = await ExecuteGetResponse( - async () => await timeSeriesClient.Get(dateBegin, dateEnd, token), token); - - return result!; - } - - public async Task> GetResampledData(DateTimeOffset dateBegin, double intervalSec = 600d, int approxPointsCount = 1024, CancellationToken token = default) - { - var result = await ExecuteGetResponse( - async () => await timeSeriesClient.GetResampledData(dateBegin, intervalSec, approxPointsCount, token), token); - - return result!; - } - - public async Task GetDatesRange(CancellationToken token) - { - var result = await ExecuteGetResponse( - async () => await timeSeriesClient.GetDatesRange(token), token); - - return result; - } - - public void Dispose() - { - timeSeriesClient.Dispose(); - - GC.SuppressFinalize(this); - } -} diff --git a/DD.Persistence.Client/Clients/TimestampedSetClient.cs b/DD.Persistence.Client/Clients/TimestampedSetClient.cs deleted file mode 100644 index 14fdee9..0000000 --- a/DD.Persistence.Client/Clients/TimestampedSetClient.cs +++ /dev/null @@ -1,64 +0,0 @@ -using Microsoft.Extensions.Logging; -using DD.Persistence.Client.Clients.Base; -using DD.Persistence.Client.Clients.Interfaces; -using DD.Persistence.Client.Clients.Interfaces.Refit; -using DD.Persistence.Models; -using DD.Persistence.Models.Common; - -namespace DD.Persistence.Client.Clients; -public class TimestampedSetClient : BaseClient, ITimestampedSetClient -{ - private readonly IRefitTimestampedValuesClient refitTimestampedSetClient; - - public TimestampedSetClient(IRefitClientFactory refitTimestampedSetClientFactory, ILogger logger) : base(logger) - { - this.refitTimestampedSetClient = refitTimestampedSetClientFactory.Create(); - } - - public async Task AddRange(Guid idDiscriminator, IEnumerable sets, CancellationToken token) - { - var result = await ExecutePostResponse( - async () => await refitTimestampedSetClient.AddRange(idDiscriminator, sets, token), token); - - return result; - } - - public async Task> Get(Guid idDiscriminator, DateTimeOffset? geTimestamp, IEnumerable? columnNames, int skip, int take, CancellationToken token) - { - var result = await ExecuteGetResponse( - async () => await refitTimestampedSetClient.Get(idDiscriminator, geTimestamp, columnNames, skip, take, token), token); - - return result!; - } - - public async Task> GetLast(Guid idDiscriminator, IEnumerable? columnNames, int take, CancellationToken token) - { - var result = await ExecuteGetResponse( - async () => await refitTimestampedSetClient.GetLast(idDiscriminator, columnNames, take, token), token); - - return result!; - } - - public async Task Count(Guid idDiscriminator, CancellationToken token) - { - var result = await ExecuteGetResponse( - async () => await refitTimestampedSetClient.Count(idDiscriminator, token), token); - - return result; - } - - public async Task GetDatesRange(Guid idDiscriminator, CancellationToken token) - { - var result = await ExecuteGetResponse( - async () => await refitTimestampedSetClient.GetDatesRange(idDiscriminator, token), token); - - return result; - } - - public void Dispose() - { - refitTimestampedSetClient.Dispose(); - - GC.SuppressFinalize(this); - } -} diff --git a/DD.Persistence.Client/Clients/TimestampedValuesClient.cs b/DD.Persistence.Client/Clients/TimestampedValuesClient.cs new file mode 100644 index 0000000..d32e9b9 --- /dev/null +++ b/DD.Persistence.Client/Clients/TimestampedValuesClient.cs @@ -0,0 +1,72 @@ +using Microsoft.Extensions.Logging; +using DD.Persistence.Client.Clients.Base; +using DD.Persistence.Client.Clients.Interfaces; +using DD.Persistence.Client.Clients.Interfaces.Refit; +using DD.Persistence.Models; +using DD.Persistence.Models.Common; + +namespace DD.Persistence.Client.Clients; +public class TimestampedValuesClient : BaseClient, ITimestampedValuesClient +{ + private readonly IRefitTimestampedValuesClient refitTimestampedSetClient; + + public TimestampedValuesClient(IRefitClientFactory refitTimestampedSetClientFactory, ILogger logger) : base(logger) + { + this.refitTimestampedSetClient = refitTimestampedSetClientFactory.Create(); + } + + public async Task AddRange(Guid discriminatorId, IEnumerable sets, CancellationToken token) + { + var result = await ExecutePostResponse( + async () => await refitTimestampedSetClient.AddRange(discriminatorId, sets, token), token); + + return result; + } + + public async Task> Get(Guid discriminatorId, DateTimeOffset? geTimestamp, IEnumerable? columnNames, int skip, int take, CancellationToken token) + { + var result = await ExecuteGetResponse( + async () => await refitTimestampedSetClient.Get(discriminatorId, geTimestamp, columnNames, skip, take, token), token); + + return result!; + } + + public async Task> GetLast(Guid discriminatorId, IEnumerable? columnNames, int take, CancellationToken token) + { + var result = await ExecuteGetResponse( + async () => await refitTimestampedSetClient.GetLast(discriminatorId, columnNames, take, token), token); + + return result!; + } + + public async Task Count(Guid discriminatorId, CancellationToken token) + { + var result = await ExecuteGetResponse( + async () => await refitTimestampedSetClient.Count(discriminatorId, token), token); + + return result; + } + + public async Task GetDatesRange(Guid discriminatorId, CancellationToken token) + { + var result = await ExecuteGetResponse( + async () => await refitTimestampedSetClient.GetDatesRange(discriminatorId, token), token); + + return result; + } + + public async Task> GetResampledData(Guid discriminatorId, DateTimeOffset dateBegin, double intervalSec = 600, int approxPointsCount = 1024, CancellationToken token = default) + { + var result = await ExecuteGetResponse( + async () => await refitTimestampedSetClient.GetResampledData(discriminatorId, dateBegin, intervalSec, approxPointsCount, token), token); + + return result; + } + + public void Dispose() + { + refitTimestampedSetClient.Dispose(); + + GC.SuppressFinalize(this); + } +} diff --git a/DD.Persistence.Client/DependencyInjection.cs b/DD.Persistence.Client/DependencyInjection.cs index 43a8733..422cf8f 100644 --- a/DD.Persistence.Client/DependencyInjection.cs +++ b/DD.Persistence.Client/DependencyInjection.cs @@ -22,7 +22,7 @@ public static class DependencyInjection services.AddTransient(); services.AddTransient(); services.AddTransient(); - services.AddTransient(); + services.AddTransient(); services.AddTransient(); return services; } diff --git a/DD.Persistence.IntegrationTests/Controllers/DataSaubControllerTest.cs b/DD.Persistence.IntegrationTests/Controllers/DataSaubControllerTest.cs deleted file mode 100644 index bb82675..0000000 --- a/DD.Persistence.IntegrationTests/Controllers/DataSaubControllerTest.cs +++ /dev/null @@ -1,85 +0,0 @@ -//using DD.Persistence.Database.Model; -//using DD.Persistence.Models; -//using Xunit; - -//namespace DD.Persistence.IntegrationTests.Controllers; -//public class DataSaubControllerTest : TimeSeriesBaseControllerTest -//{ -// private readonly DataSaubDto dto = new() -// { -// AxialLoad = 1, -// BitDepth = 2, -// BlockPosition = 3, -// BlockSpeed = 4, -// Date = DateTimeOffset.UtcNow, -// Flow = 5, -// HookWeight = 6, -// IdFeedRegulator = 8, -// Mode = 9, -// Mse = 10, -// MseState = 11, -// Pressure = 12, -// Pump0Flow = 13, -// Pump1Flow = 14, -// Pump2Flow = 15, -// RotorSpeed = 16, -// RotorTorque = 17, -// User = string.Empty, -// WellDepth = 18, -// }; - -// private readonly DataSaub entity = new() -// { -// AxialLoad = 1, -// BitDepth = 2, -// BlockPosition = 3, -// BlockSpeed = 4, -// Timestamp = DateTimeOffset.UtcNow, -// Flow = 5, -// HookWeight = 6, -// IdFeedRegulator = 8, -// Mode = 9, -// Mse = 10, -// MseState = 11, -// Pressure = 12, -// Pump0Flow = 13, -// Pump1Flow = 14, -// Pump2Flow = 15, -// RotorSpeed = 16, -// RotorTorque = 17, -// User = string.Empty, -// WellDepth = 18, -// }; - -// public DataSaubControllerTest(WebAppFactoryFixture factory) : base(factory) -// { -// } - -// [Fact] -// public async Task InsertRange_returns_success() -// { -// await InsertRangeSuccess(dto); -// } - -// [Fact] -// public async Task Get_returns_success() -// { -// var beginDate = DateTimeOffset.UtcNow.AddDays(-1); -// var endDate = DateTimeOffset.UtcNow; -// await GetSuccess(beginDate, endDate, entity); -// } - -// [Fact] -// public async Task GetDatesRange_returns_success() -// { -// await GetDatesRangeSuccess(entity); -// } - - - -// [Fact] -// public async Task GetResampledData_returns_success() -// { -// await GetResampledDataSuccess(entity); -// } -//} diff --git a/DD.Persistence.IntegrationTests/Controllers/TimeSeriesBaseControllerTest.cs b/DD.Persistence.IntegrationTests/Controllers/TimeSeriesBaseControllerTest.cs deleted file mode 100644 index 481bf57..0000000 --- a/DD.Persistence.IntegrationTests/Controllers/TimeSeriesBaseControllerTest.cs +++ /dev/null @@ -1,122 +0,0 @@ -using DD.Persistence.Client; -using DD.Persistence.Client.Clients; -using DD.Persistence.Client.Clients.Interfaces; -using DD.Persistence.Client.Clients.Interfaces.Refit; -using DD.Persistence.Database.EntityAbstractions; -using DD.Persistence.ModelsAbstractions; -using Mapster; -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Logging; -using Xunit; - -namespace DD.Persistence.IntegrationTests.Controllers; - -public abstract class TimeSeriesBaseControllerTest : BaseIntegrationTest - where TEntity : class, ITimestampedItem, new() - where TDto : class, ITimestampAbstractDto, new() -{ - private readonly ITimeSeriesClient timeSeriesClient; - - public TimeSeriesBaseControllerTest(WebAppFactoryFixture factory) : base(factory) - { - dbContext.CleanupDbSet(); - - var refitClientFactory = scope.ServiceProvider - .GetRequiredService>>(); - var logger = scope.ServiceProvider.GetRequiredService>>(); - - timeSeriesClient = scope.ServiceProvider - .GetRequiredService>(); - } - - public async Task InsertRangeSuccess(TDto dto) - { - //arrange - var expected = dto.Adapt(); - - //act - var response = await timeSeriesClient.AddRange(new TDto[] { expected }, new CancellationToken()); - - //assert - Assert.Equal(1, response); - } - - public async Task GetSuccess(DateTimeOffset beginDate, DateTimeOffset endDate, TEntity entity) - { - //arrange - var dbset = dbContext.Set(); - - dbset.Add(entity); - - dbContext.SaveChanges(); - - var response = await timeSeriesClient.Get(beginDate, endDate, new CancellationToken()); - - //assert - Assert.NotNull(response); - Assert.Single(response); - } - - public async Task GetDatesRangeSuccess(TEntity entity) - { - //arrange - var datesRangeExpected = 30; - - var entity2 = entity.Adapt(); - entity2.Timestamp = entity.Timestamp.AddDays(datesRangeExpected); - - var dbset = dbContext.Set(); - dbset.Add(entity); - dbset.Add(entity2); - - dbContext.SaveChanges(); - - var response = await timeSeriesClient.GetDatesRange(new CancellationToken()); - - //assert - Assert.NotNull(response); - - var datesRangeActual = (response.To - response.From).Days; - Assert.Equal(datesRangeExpected, datesRangeActual); - } - - public async Task GetResampledDataSuccess(TEntity entity) - { - //arrange - var approxPointsCount = 10; - var differenceBetweenStartAndEndDays = 50; - - var entities = new List(); - for (var i = 1; i <= differenceBetweenStartAndEndDays; i++) - { - var entity2 = entity.Adapt(); - entity2.Timestamp = entity.Timestamp.AddDays(i - 1); - - entities.Add(entity2); - } - - var dbset = dbContext.Set(); - dbset.AddRange(entities); - - dbContext.SaveChanges(); - - var response = await timeSeriesClient.GetResampledData(entity.Timestamp.AddMinutes(-1), differenceBetweenStartAndEndDays * 24 * 60 * 60 + 60, approxPointsCount, new CancellationToken()); - - //assert - Assert.NotNull(response); - - var ratio = entities.Count / approxPointsCount; - if (ratio > 1) - { - var expectedResampledCount = entities - .Where((_, index) => index % ratio == 0) - .Count(); - - Assert.Equal(expectedResampledCount, response.Count()); - } - else - { - Assert.Equal(entities.Count(), response.Count()); - } - } -} diff --git a/DD.Persistence.IntegrationTests/Controllers/TimestampedSetControllerTest.cs b/DD.Persistence.IntegrationTests/Controllers/TimestampedSetControllerTest.cs index c929230..a8135c6 100644 --- a/DD.Persistence.IntegrationTests/Controllers/TimestampedSetControllerTest.cs +++ b/DD.Persistence.IntegrationTests/Controllers/TimestampedSetControllerTest.cs @@ -6,20 +6,25 @@ using Xunit; using DD.Persistence.Client.Clients.Interfaces.Refit; using DD.Persistence.Client.Clients; using Microsoft.Extensions.Logging; +using System.Text.Json; +using System.Text; +using Microsoft.Extensions.Primitives; +using System.Dynamic; +using Newtonsoft.Json.Linq; namespace DD.Persistence.IntegrationTests.Controllers; public class TimestampedSetControllerTest : BaseIntegrationTest { - private readonly ITimestampedSetClient client; + private readonly ITimestampedValuesClient client; public TimestampedSetControllerTest(WebAppFactoryFixture factory) : base(factory) { var refitClientFactory = scope.ServiceProvider .GetRequiredService>(); - var logger = scope.ServiceProvider.GetRequiredService>(); + var logger = scope.ServiceProvider.GetRequiredService>(); client = scope.ServiceProvider - .GetRequiredService(); + .GetRequiredService(); } [Fact] @@ -71,8 +76,8 @@ public class TimestampedSetControllerTest : BaseIntegrationTest Assert.Equal(count, response.Count()); foreach (var item in response) { - Assert.Single(item.Values); - var kv = item.Values.First(); + Assert.Single(item.Values!); + var kv = item.Values!.First(); Assert.Equal("A", ((KeyValuePair) kv).Key); } } @@ -196,18 +201,28 @@ public class TimestampedSetControllerTest : BaseIntegrationTest private static IEnumerable Generate(int n, DateTimeOffset from) { + var result = new List(); for (int i = 0; i < n; i++) + { + var t = new object[] { + new { A = i }, + new { B = i * 1.1 }, + new { C = $"Any{i}" }, + new { D = DateTimeOffset.Now } + }; + string jsonString = JsonSerializer.Serialize(t); + var values = JsonSerializer.Deserialize(jsonString); + //// + //dynamic obj = new ExpandoObject(); + + //// "FieldName" 123 + //obj.FieldName = 123; + yield return new TimestampedValuesDto() { Timestamp = from.AddSeconds(i), - Values = new Dictionary { - {"A", i }, - {"B", i * 1.1 }, - {"C", $"Any{i}" }, - {"D", DateTimeOffset.Now} - } - .Select(e => (object) e) - .ToArray() + Values = values }; + } } } diff --git a/DD.Persistence.Repository/Repositories/TimestampedValuesRepository.cs b/DD.Persistence.Repository/Repositories/TimestampedValuesRepository.cs index 9ab1877..cc892ad 100644 --- a/DD.Persistence.Repository/Repositories/TimestampedValuesRepository.cs +++ b/DD.Persistence.Repository/Repositories/TimestampedValuesRepository.cs @@ -57,7 +57,11 @@ public class TimestampedValuesRepository : ITimestampedValuesRepository d.Adapt()) .ToList(); - entities.ForEach(d => d.DiscriminatorId = discriminatorId); + entities.ForEach(d => + { + d.DiscriminatorId = discriminatorId; + d.Timestamp = d.Timestamp.ToUniversalTime(); + }); await db.Set().AddRangeAsync(entities, token); var result = await db.SaveChangesAsync(token);