From fc0beb63197d9ec72c2ffa757ec47b1f37246b9c Mon Sep 17 00:00:00 2001 From: Olga Nemt Date: Mon, 5 Feb 2024 10:47:00 +0500 Subject: [PATCH 01/12] =?UTF-8?q?=D0=A0=D0=B5=D0=BF=D0=BE=D0=B7=D0=B8?= =?UTF-8?q?=D1=82=D0=BE=D1=80=D0=B8=D0=B9=20=D0=B4=D0=BB=D1=8F=20=D1=80?= =?UTF-8?q?=D0=B0=D0=B1=D0=BE=D1=82=D1=8B=20=D1=81=20=D0=B4=D0=B0=D0=BD?= =?UTF-8?q?=D0=BD=D1=8B=D0=BC=D0=B8=20DataSaubStat?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Repositories/IDataSaubStatRepository.cs | 29 ++++++++++++ .../PeriodicWorks/WorkDataSaubStat.cs | 36 +++++++-------- AsbCloudInfrastructure/DependencyInjection.cs | 1 + .../Repository/DataSaubStatRepository.cs | 44 +++++++++++++++++++ 4 files changed, 89 insertions(+), 21 deletions(-) create mode 100644 AsbCloudApp/Repositories/IDataSaubStatRepository.cs create mode 100644 AsbCloudInfrastructure/Repository/DataSaubStatRepository.cs diff --git a/AsbCloudApp/Repositories/IDataSaubStatRepository.cs b/AsbCloudApp/Repositories/IDataSaubStatRepository.cs new file mode 100644 index 00000000..6894fe41 --- /dev/null +++ b/AsbCloudApp/Repositories/IDataSaubStatRepository.cs @@ -0,0 +1,29 @@ +using AsbCloudApp.Data; +using System.Collections.Generic; +using System.Threading; +using System.Threading.Tasks; + +namespace AsbCloudApp.Repositories +{ + /// + /// Репозиторий работы с данными из таблицы t_data_daub_stat + /// + public interface IDataSaubStatRepository + { + /// + /// Получение последних дат по телеметриям + /// + /// ключи телеметрий + /// + /// + Task> GetLastDatesAsync(int[] idTelemetries, CancellationToken token); + + /// + /// Вставка записей статистики + /// + /// + /// + /// + Task InsertRangeAsync(IEnumerable dataSaubStats, CancellationToken token); + } +} diff --git a/AsbCloudInfrastructure/Background/PeriodicWorks/WorkDataSaubStat.cs b/AsbCloudInfrastructure/Background/PeriodicWorks/WorkDataSaubStat.cs index d69b185e..406f7ddf 100644 --- a/AsbCloudInfrastructure/Background/PeriodicWorks/WorkDataSaubStat.cs +++ b/AsbCloudInfrastructure/Background/PeriodicWorks/WorkDataSaubStat.cs @@ -1,4 +1,5 @@ -using AsbCloudApp.Data.SAUB; +using AsbCloudApp.Data; +using AsbCloudApp.Data.SAUB; using AsbCloudApp.Repositories; using AsbCloudApp.Requests; using AsbCloudDb.Model; @@ -29,37 +30,31 @@ namespace AsbCloudInfrastructure.Background.PeriodicWorks protected override async Task Action(string id, IServiceProvider services, Action onProgressCallback, CancellationToken token) { using var db = services.GetRequiredService(); + var telemetryDataCache = services.GetRequiredService>(); var cacheRequest = new TelemetryDataRequest() { - GeDate = DateTime.UtcNow.AddDays(-Gap) + GeDate = DateTime.UtcNow.AddDays(-Gap*100) }; var idTelemetries = telemetryDataCache.GetIds(cacheRequest).ToArray(); if (!idTelemetries.Any()) return; - var stats = await db.Set() - .Where(s => idTelemetries.Contains(s.IdTelemetry)) - .GroupBy(s => s.IdTelemetry) - .Select(g => new - { - IdTelemetry = g.Key, - DateEnd = g.Max(s => s.DateEnd), - }) - .ToArrayAsync(token); + var dataSaubStatRepo = services.GetRequiredService(); + var stats = await dataSaubStatRepo.GetLastDatesAsync(idTelemetries, token); for( var i =0; i < idTelemetries.Length; i++) { var idTelemetry = idTelemetries[i]; var lastDate = stats.FirstOrDefault(s => s.IdTelemetry == idTelemetry)?.DateEnd ?? DateTimeOffset.UnixEpoch; - var statsCount = await CreateStatForTelemetryFromDate(db, idTelemetry, lastDate, token); + var statsCount = await CreateStatForTelemetryFromDate(db, idTelemetry, lastDate, dataSaubStatRepo, token); onProgressCallback($"Calculate stat for telemetry: {idTelemetry}; from {lastDate}; results count: {statsCount};", 100*i / idTelemetries.Length); } } - private async Task CreateStatForTelemetryFromDate(IAsbCloudDbContext db, int idTelemetry, DateTimeOffset begin, CancellationToken token) + private async Task CreateStatForTelemetryFromDate(IAsbCloudDbContext db, int idTelemetry, DateTimeOffset begin, IDataSaubStatRepository dataSaubStatRepo, CancellationToken token) { var detectedOperations = await db.Set() .Where(o => o.IdTelemetry == idTelemetry) @@ -89,15 +84,14 @@ namespace AsbCloudInfrastructure.Background.PeriodicWorks var dataSaubStats = CreateDataSaubStat(detectedOperations, telemetryDataSaub); - db.Set().AddRange(dataSaubStats); - return await db.SaveChangesAsync(token); + return await dataSaubStatRepo.InsertRangeAsync(dataSaubStats, token); } - private static IEnumerable CreateDataSaubStat(IEnumerable detectedOperations, TelemetryDataSaub[] telemetryDataSaub) + private static IEnumerable CreateDataSaubStat(IEnumerable detectedOperations, TelemetryDataSaub[] telemetryDataSaub) { var indexStart = 0; var indexEnd = 0; - var result = new List(); + var result = new List(); if (!telemetryDataSaub.Any()) return result; @@ -125,9 +119,9 @@ namespace AsbCloudInfrastructure.Background.PeriodicWorks return result; } - private static IEnumerable CalcStats(DetectedOperation operation, Span telemetryDataSaub) + private static IEnumerable CalcStats(DetectedOperation operation, Span telemetryDataSaub) { - var result = new List(); + var result = new List(); var indexStart = 0; for (var i = 1; i < telemetryDataSaub.Length; i++) @@ -150,13 +144,13 @@ namespace AsbCloudInfrastructure.Background.PeriodicWorks return result; } - private static DataSaubStat CalcStat(DetectedOperation operation, Span span) + private static DataSaubStatDto CalcStat(DetectedOperation operation, Span span) { var hasOscillation = operation.ExtraData.TryGetValue(DetectorDrilling.ExtraDataKeyHasOscillation, out object? hasOscillationObject) && hasOscillationObject is true; var aggregatedValues = CalcAggregate(span); - var processMapDrillingCacheItem = new DataSaubStat + var processMapDrillingCacheItem = new DataSaubStatDto { DateStart = operation.DateStart, DateEnd = operation.DateEnd, diff --git a/AsbCloudInfrastructure/DependencyInjection.cs b/AsbCloudInfrastructure/DependencyInjection.cs index 28a051ab..5b131299 100644 --- a/AsbCloudInfrastructure/DependencyInjection.cs +++ b/AsbCloudInfrastructure/DependencyInjection.cs @@ -222,6 +222,7 @@ namespace AsbCloudInfrastructure services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); services.AddTransient< IChangeLogRepository, diff --git a/AsbCloudInfrastructure/Repository/DataSaubStatRepository.cs b/AsbCloudInfrastructure/Repository/DataSaubStatRepository.cs new file mode 100644 index 00000000..cadb4d69 --- /dev/null +++ b/AsbCloudInfrastructure/Repository/DataSaubStatRepository.cs @@ -0,0 +1,44 @@ +using AsbCloudApp.Data; +using AsbCloudApp.Repositories; +using AsbCloudDb.Model; +using Mapster; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; + +namespace AsbCloudInfrastructure.Repository +{ + public class DataSaubStatRepository : IDataSaubStatRepository + { + private readonly IAsbCloudDbContext db; + + public DataSaubStatRepository(IAsbCloudDbContext dbContext) + { + db = dbContext; + } + + public async Task> GetLastDatesAsync(int[] idTelemetries, CancellationToken token) + { + var stats = await db.Set() + .Where(s => idTelemetries.Contains(s.IdTelemetry)) + .GroupBy(s => s.IdTelemetry, (key, group) => new DataSaubStatDto() + { + IdTelemetry = key, + DateEnd = group.Max(s => s.DateEnd) + }) + .ToArrayAsync(token); + + return stats; + } + + public async Task InsertRangeAsync(IEnumerable dataSaubStats, CancellationToken token) + { + var entities = dataSaubStats.Select(data => data.Adapt()); + db.Set().AddRange(entities); + return await db.SaveChangesAsync(token); + } + } +} From 4f468ed8ba40cd385b5c27c52b969e7eaeb5b5b1 Mon Sep 17 00:00:00 2001 From: Olga Nemt Date: Mon, 5 Feb 2024 13:23:29 +0500 Subject: [PATCH 02/12] =?UTF-8?q?=D0=90=D0=B2=D1=82=D0=BE=D1=82=D0=B5?= =?UTF-8?q?=D1=81=D1=82=D1=8B=20=D0=B4=D0=BB=D1=8F=20dataSaubStat?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DataSaubStat/DataSaubStatServiceTests.cs | 71 +++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 AsbCloudWebApi.Tests/Services/DataSaubStat/DataSaubStatServiceTests.cs diff --git a/AsbCloudWebApi.Tests/Services/DataSaubStat/DataSaubStatServiceTests.cs b/AsbCloudWebApi.Tests/Services/DataSaubStat/DataSaubStatServiceTests.cs new file mode 100644 index 00000000..18164509 --- /dev/null +++ b/AsbCloudWebApi.Tests/Services/DataSaubStat/DataSaubStatServiceTests.cs @@ -0,0 +1,71 @@ +using AsbCloudApp.Data; +using AsbCloudApp.Data.ProcessMaps; +using AsbCloudApp.Repositories; +using NSubstitute; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; +using Xunit; + +namespace AsbCloudWebApi.Tests.Services.ProcessMaps; + +public class DataSaubStatServiceTests +{ + private readonly IEnumerable stats = new DataSaubStatDto[] + { + new() + { + IdTelemetry = 1, + DateEnd = DateTimeOffset.UtcNow.AddDays(-10), + DateStart = DateTimeOffset.UtcNow.AddDays(-11), + AxialLoad = 10.0, + AxialLoadLimitMax = 10.0, + AxialLoadSp = 10.0, + BlockSpeedSp = 1000, + DepthEnd = 10.0, + DepthStart = 5.0, + EnabledSubsystems = 1, + Flow = 10.0, + HasOscillation = true, + Id = 1, + IdCategory = 2, + IdFeedRegulator = 1, + Pressure = 10.0, + PressureIdle = 10.0, + PressureSp = 10.0, + RotorSpeed = 10.0, + RotorTorque = 10.0, + RotorTorqueSp = 10.0, + RotorTorqueLimitMax = 10.0, + Speed = 10.0, + Telemetry = new TelemetryDto(){ + Id = 1, + }, + OperationCategory = new WellOperationCategoryDto(){ + Id = 2, + } + } + }; + private readonly IDataSaubStatRepository dataSaubStatRepository; + + public DataSaubStatServiceTests() + { + dataSaubStatRepository = Substitute.For(); + } + + [Fact] + public async Task GetAsync_ReturnsDataSaubStat() + { + dataSaubStatRepository.GetLastDatesAsync(Arg.Any(), Arg.Any()).Returns(stats); + Assert.Equal(2, stats.Count()); + } + + [Fact] + public async Task InsertRangeAsync_Returns_statusOk() + { + var result = await dataSaubStatRepository.InsertRangeAsync(stats, Arg.Any()); + Assert.Equal(1, result); + } +} \ No newline at end of file From 0ae4e8ace45a252c016f652cfca4ee8c1c444c8f Mon Sep 17 00:00:00 2001 From: Olga Nemt Date: Mon, 5 Feb 2024 13:26:07 +0500 Subject: [PATCH 03/12] =?UTF-8?q?=D1=84=D0=B8=D0=BA=D1=81=20=D0=BB=D0=B8?= =?UTF-8?q?=D1=88=D0=BD=D0=B5=D0=B3=D0=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Background/PeriodicWorks/WorkDataSaubStat.cs | 2 +- AsbCloudWebApi/appsettings.json | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/AsbCloudInfrastructure/Background/PeriodicWorks/WorkDataSaubStat.cs b/AsbCloudInfrastructure/Background/PeriodicWorks/WorkDataSaubStat.cs index 406f7ddf..93538b82 100644 --- a/AsbCloudInfrastructure/Background/PeriodicWorks/WorkDataSaubStat.cs +++ b/AsbCloudInfrastructure/Background/PeriodicWorks/WorkDataSaubStat.cs @@ -35,7 +35,7 @@ namespace AsbCloudInfrastructure.Background.PeriodicWorks var cacheRequest = new TelemetryDataRequest() { - GeDate = DateTime.UtcNow.AddDays(-Gap*100) + GeDate = DateTime.UtcNow.AddDays(-Gap) }; var idTelemetries = telemetryDataCache.GetIds(cacheRequest).ToArray(); diff --git a/AsbCloudWebApi/appsettings.json b/AsbCloudWebApi/appsettings.json index 72d50f57..5294ebe7 100644 --- a/AsbCloudWebApi/appsettings.json +++ b/AsbCloudWebApi/appsettings.json @@ -7,10 +7,10 @@ } }, "ConnectionStrings": { - "DefaultConnection": "Host=localhost;Database=postgres;Username=postgres;Password=q;Persist Security Info=True", - "DebugConnection": "Host=localhost;Database=postgres;Username=postgres;Password=q;Persist Security Info=True;Include Error Detail=True", + "DefaultConnection": "Host=localhost;Database=postgres2;Username=postgres;Password=q;Persist Security Info=True", + "DebugConnection": "Host=localhost;Database=postgres2;Username=postgres;Password=q;Persist Security Info=True;Include Error Detail=True", "TestConnection": "Host=localhost;Database=test;Username=postgres;Password=q;Persist Security Info=True;Include Error Detail=True", - "LocalConnection": "Host=localhost;Database=postgres;Username=postgres;Password=q;Persist Security Info=True" + "LocalConnection": "Host=localhost;Database=postgres2;Username=postgres;Password=q;Persist Security Info=True" }, "AllowedHosts": "*", "ContentPath": "../data", From 1f80d37da9f2bbd328282801f15b164b2ac61ac0 Mon Sep 17 00:00:00 2001 From: Olga Nemt Date: Mon, 5 Feb 2024 13:27:02 +0500 Subject: [PATCH 04/12] =?UTF-8?q?=D1=84=D0=B8=D0=BA=D1=81=20=D0=BB=D0=B8?= =?UTF-8?q?=D1=88=D0=BD=D0=B5=D0=B3=D0=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AsbCloudWebApi/appsettings.json | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/AsbCloudWebApi/appsettings.json b/AsbCloudWebApi/appsettings.json index 5294ebe7..72d50f57 100644 --- a/AsbCloudWebApi/appsettings.json +++ b/AsbCloudWebApi/appsettings.json @@ -7,10 +7,10 @@ } }, "ConnectionStrings": { - "DefaultConnection": "Host=localhost;Database=postgres2;Username=postgres;Password=q;Persist Security Info=True", - "DebugConnection": "Host=localhost;Database=postgres2;Username=postgres;Password=q;Persist Security Info=True;Include Error Detail=True", + "DefaultConnection": "Host=localhost;Database=postgres;Username=postgres;Password=q;Persist Security Info=True", + "DebugConnection": "Host=localhost;Database=postgres;Username=postgres;Password=q;Persist Security Info=True;Include Error Detail=True", "TestConnection": "Host=localhost;Database=test;Username=postgres;Password=q;Persist Security Info=True;Include Error Detail=True", - "LocalConnection": "Host=localhost;Database=postgres2;Username=postgres;Password=q;Persist Security Info=True" + "LocalConnection": "Host=localhost;Database=postgres;Username=postgres;Password=q;Persist Security Info=True" }, "AllowedHosts": "*", "ContentPath": "../data", From 163b9d55e12bec5efc0f9193dfd84aaacf05c24a Mon Sep 17 00:00:00 2001 From: Olga Nemt Date: Mon, 5 Feb 2024 16:43:56 +0500 Subject: [PATCH 05/12] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D1=8B=20=D1=84=D1=83=D0=BD=D0=BA=D1=86=D0=B8=D0=BE?= =?UTF-8?q?=D0=BD=D0=B0=D0=BB=D1=8C=D0=BD=D1=8B=D0=B5=20=D1=82=D0=B5=D1=81?= =?UTF-8?q?=D1=82=D1=8B,=20=D1=83=D0=B4=D0=B0=D0=BB=D0=B5=D0=BD=D1=8B=20?= =?UTF-8?q?=D0=B8=D0=BD=D1=82=D0=B5=D0=B3=D1=80=D0=B0=D1=86=D0=B8=D0=BE?= =?UTF-8?q?=D0=BD=D0=BD=D1=8B=D0=B5=20(DataSaubStat)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Repository/DataSaubStatRepositoryTest.cs | 120 ++++++++++++++++++ .../DataSaubStat/DataSaubStatServiceTests.cs | 71 ----------- 2 files changed, 120 insertions(+), 71 deletions(-) create mode 100644 AsbCloudWebApi.IntegrationTests/Repository/DataSaubStatRepositoryTest.cs delete mode 100644 AsbCloudWebApi.Tests/Services/DataSaubStat/DataSaubStatServiceTests.cs diff --git a/AsbCloudWebApi.IntegrationTests/Repository/DataSaubStatRepositoryTest.cs b/AsbCloudWebApi.IntegrationTests/Repository/DataSaubStatRepositoryTest.cs new file mode 100644 index 00000000..bf71fcfa --- /dev/null +++ b/AsbCloudWebApi.IntegrationTests/Repository/DataSaubStatRepositoryTest.cs @@ -0,0 +1,120 @@ +using AsbCloudApp.Data; +using AsbCloudApp.Data.ProcessMapPlan; +using AsbCloudApp.Repositories; +using AsbCloudDb.Model; +using Mapster; +using Microsoft.Extensions.DependencyInjection; +using System.ComponentModel; +using Xunit; + +namespace AsbCloudWebApi.IntegrationTests.Repository +{ + public class DataSaubStatRepositoryTest : BaseIntegrationTest + { + private static readonly DataSaubStatDto[] statDtos = new DataSaubStatDto[2] + { + new DataSaubStatDto() + { + IdTelemetry = 1, + DateEnd = DateTimeOffset.UtcNow.AddDays(-10), + DateStart = DateTimeOffset.UtcNow.AddDays(-11), + AxialLoad = 10.0, + AxialLoadLimitMax = 10.0, + AxialLoadSp = 10.0, + BlockSpeedSp = 1000, + DepthEnd = 10.0, + DepthStart = 5.0, + EnabledSubsystems = 1, + Flow = 10.0, + HasOscillation = true, + Id = default, + IdCategory = 2, + IdFeedRegulator = 1, + Pressure = 10.0, + PressureIdle = 10.0, + PressureSp = 10.0, + RotorSpeed = 10.0, + RotorTorque = 10.0, + RotorTorqueSp = 10.0, + RotorTorqueLimitMax = 10.0, + Speed = 10.0 + }, + new DataSaubStatDto() + { + IdTelemetry = 1, + DateEnd = DateTimeOffset.UtcNow.AddDays(-20), + DateStart = DateTimeOffset.UtcNow.AddDays(-21), + AxialLoad = 10.0, + AxialLoadLimitMax = 10.0, + AxialLoadSp = 10.0, + BlockSpeedSp = 1000, + DepthEnd = 10.0, + DepthStart = 5.0, + EnabledSubsystems = 1, + Flow = 10.0, + HasOscillation = true, + Id = default, + IdCategory = 2, + IdFeedRegulator = 1, + Pressure = 10.0, + PressureIdle = 10.0, + PressureSp = 10.0, + RotorSpeed = 10.0, + RotorTorque = 10.0, + RotorTorqueSp = 10.0, + RotorTorqueLimitMax = 10.0, + Speed = 10.0 + } + }; + private static readonly WellOperationCategory category = new() + { + Id = 2, + IdParent = null, + Name = "Категория 2" + }; + + private readonly IDataSaubStatRepository dataSaubStatRepository; + + public DataSaubStatRepositoryTest(WebAppFactoryFixture factory) : base(factory) + { + var stats = dbContext.Set(); + var categories = dbContext.Set(); + + categories.RemoveRange(categories); + stats.RemoveRange(stats); + dbContext.SaveChanges(); + + categories.Add(category); + + var entities = statDtos.Select(stat => stat.Adapt()); + stats.AddRange(entities); + dbContext.SaveChanges(); + + dataSaubStatRepository = scope.ServiceProvider.GetRequiredService(); + } + + [Fact] + public async Task GetLastDatesAsync_returns_success() + { + //act + var telemetryIds = statDtos.Select(stat => stat.IdTelemetry).ToArray(); + var result = await dataSaubStatRepository.GetLastDatesAsync(telemetryIds, CancellationToken.None); + + var expected = statDtos.Max(stat => stat.DateEnd); + var actual = result.Any() ? result.First().DateEnd : DateTimeOffset.UnixEpoch; + + //assert + Assert.Equal(expected.Ticks, actual.Ticks); + } + + [Fact] + public async Task InsertRangeAsync_returns_success() + { + //act + var result = await dataSaubStatRepository.InsertRangeAsync(statDtos, CancellationToken.None); + + //assert + Assert.Equal(statDtos.Length, result); + } + } +} diff --git a/AsbCloudWebApi.Tests/Services/DataSaubStat/DataSaubStatServiceTests.cs b/AsbCloudWebApi.Tests/Services/DataSaubStat/DataSaubStatServiceTests.cs deleted file mode 100644 index 18164509..00000000 --- a/AsbCloudWebApi.Tests/Services/DataSaubStat/DataSaubStatServiceTests.cs +++ /dev/null @@ -1,71 +0,0 @@ -using AsbCloudApp.Data; -using AsbCloudApp.Data.ProcessMaps; -using AsbCloudApp.Repositories; -using NSubstitute; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading; -using System.Threading.Tasks; -using Xunit; - -namespace AsbCloudWebApi.Tests.Services.ProcessMaps; - -public class DataSaubStatServiceTests -{ - private readonly IEnumerable stats = new DataSaubStatDto[] - { - new() - { - IdTelemetry = 1, - DateEnd = DateTimeOffset.UtcNow.AddDays(-10), - DateStart = DateTimeOffset.UtcNow.AddDays(-11), - AxialLoad = 10.0, - AxialLoadLimitMax = 10.0, - AxialLoadSp = 10.0, - BlockSpeedSp = 1000, - DepthEnd = 10.0, - DepthStart = 5.0, - EnabledSubsystems = 1, - Flow = 10.0, - HasOscillation = true, - Id = 1, - IdCategory = 2, - IdFeedRegulator = 1, - Pressure = 10.0, - PressureIdle = 10.0, - PressureSp = 10.0, - RotorSpeed = 10.0, - RotorTorque = 10.0, - RotorTorqueSp = 10.0, - RotorTorqueLimitMax = 10.0, - Speed = 10.0, - Telemetry = new TelemetryDto(){ - Id = 1, - }, - OperationCategory = new WellOperationCategoryDto(){ - Id = 2, - } - } - }; - private readonly IDataSaubStatRepository dataSaubStatRepository; - - public DataSaubStatServiceTests() - { - dataSaubStatRepository = Substitute.For(); - } - - [Fact] - public async Task GetAsync_ReturnsDataSaubStat() - { - dataSaubStatRepository.GetLastDatesAsync(Arg.Any(), Arg.Any()).Returns(stats); - Assert.Equal(2, stats.Count()); - } - - [Fact] - public async Task InsertRangeAsync_Returns_statusOk() - { - var result = await dataSaubStatRepository.InsertRangeAsync(stats, Arg.Any()); - Assert.Equal(1, result); - } -} \ No newline at end of file From a7d7440a07f052c819ef7e950e8b86a5a9c16e73 Mon Sep 17 00:00:00 2001 From: Olga Nemt Date: Tue, 6 Feb 2024 10:29:01 +0500 Subject: [PATCH 06/12] =?UTF-8?q?=D0=94=D0=BE=D1=80=D0=B0=D0=B1=D0=BE?= =?UTF-8?q?=D1=82=D0=B0=D0=BD=D1=8B=20=D0=B8=D0=BD=D1=82=D0=B5=D0=B3=D1=80?= =?UTF-8?q?=D0=B0=D1=86=D0=B8=D0=BE=D0=BD=D0=BD=D1=8B=D0=B5=20=D1=82=D0=B5?= =?UTF-8?q?=D1=81=D1=82=D1=8B=20=D0=B4=D0=BB=D1=8F=20DataSaubStat?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Repository/DataSaubStatRepositoryTest.cs | 54 +++++++++++++------ 1 file changed, 37 insertions(+), 17 deletions(-) diff --git a/AsbCloudWebApi.IntegrationTests/Repository/DataSaubStatRepositoryTest.cs b/AsbCloudWebApi.IntegrationTests/Repository/DataSaubStatRepositoryTest.cs index bf71fcfa..08ff3584 100644 --- a/AsbCloudWebApi.IntegrationTests/Repository/DataSaubStatRepositoryTest.cs +++ b/AsbCloudWebApi.IntegrationTests/Repository/DataSaubStatRepositoryTest.cs @@ -1,10 +1,8 @@ using AsbCloudApp.Data; -using AsbCloudApp.Data.ProcessMapPlan; using AsbCloudApp.Repositories; using AsbCloudDb.Model; using Mapster; using Microsoft.Extensions.DependencyInjection; -using System.ComponentModel; using Xunit; namespace AsbCloudWebApi.IntegrationTests.Repository @@ -16,8 +14,8 @@ namespace AsbCloudWebApi.IntegrationTests.Repository new DataSaubStatDto() { IdTelemetry = 1, - DateEnd = DateTimeOffset.UtcNow.AddDays(-10), - DateStart = DateTimeOffset.UtcNow.AddDays(-11), + DateEnd = new DateTimeOffset(2024, 1, 1, 20, 25, 0, TimeSpan.Zero), + DateStart = new DateTimeOffset(2024, 1, 1, 20, 15, 0, TimeSpan.Zero), AxialLoad = 10.0, AxialLoadLimitMax = 10.0, AxialLoadSp = 10.0, @@ -33,17 +31,17 @@ namespace AsbCloudWebApi.IntegrationTests.Repository Pressure = 10.0, PressureIdle = 10.0, PressureSp = 10.0, - RotorSpeed = 10.0, - RotorTorque = 10.0, - RotorTorqueSp = 10.0, - RotorTorqueLimitMax = 10.0, + RotorSpeed = 9.0, + RotorTorque = 9.0, + RotorTorqueSp = 9.0, + RotorTorqueLimitMax = 9.0, Speed = 10.0 }, new DataSaubStatDto() { - IdTelemetry = 1, - DateEnd = DateTimeOffset.UtcNow.AddDays(-20), - DateStart = DateTimeOffset.UtcNow.AddDays(-21), + IdTelemetry = 1, + DateEnd = new DateTimeOffset(2024, 2, 2, 20, 25, 0, TimeSpan.Zero), + DateStart = new DateTimeOffset(2024, 2, 2, 20, 15, 0, TimeSpan.Zero), AxialLoad = 10.0, AxialLoadLimitMax = 10.0, AxialLoadSp = 10.0, @@ -70,24 +68,24 @@ namespace AsbCloudWebApi.IntegrationTests.Repository { Id = 2, IdParent = null, - Name = "Категория 2" + Name = "Категория 2" }; private readonly IDataSaubStatRepository dataSaubStatRepository; public DataSaubStatRepositoryTest(WebAppFactoryFixture factory) : base(factory) { - var stats = dbContext.Set(); + var dbSet = dbContext.Set(); var categories = dbContext.Set(); categories.RemoveRange(categories); - stats.RemoveRange(stats); + dbSet.RemoveRange(dbSet); dbContext.SaveChanges(); categories.Add(category); var entities = statDtos.Select(stat => stat.Adapt()); - stats.AddRange(entities); + dbSet.AddRange(entities); dbContext.SaveChanges(); dataSaubStatRepository = scope.ServiceProvider.GetRequiredService(); @@ -104,17 +102,39 @@ namespace AsbCloudWebApi.IntegrationTests.Repository var actual = result.Any() ? result.First().DateEnd : DateTimeOffset.UnixEpoch; //assert - Assert.Equal(expected.Ticks, actual.Ticks); + Assert.True((expected - actual).Ticks == 0.0); } [Fact] public async Task InsertRangeAsync_returns_success() { //act - var result = await dataSaubStatRepository.InsertRangeAsync(statDtos, CancellationToken.None); + var dbSet = dbContext.Set(); + dbSet.RemoveRange(dbSet); + dbContext.SaveChanges(); + var result = await dataSaubStatRepository.InsertRangeAsync(statDtos, CancellationToken.None); + //assert Assert.Equal(statDtos.Length, result); + + var statDtosFromDb = dbSet.Select(stat => stat.Adapt()).ToArray(); + + var excludedProps = new[] { + nameof(DataSaubStat.Telemetry), + nameof(DataSaubStat.Id), + nameof(DataSaubStat.OperationCategory) + }; + foreach (var statDtoFromDb in statDtosFromDb) + { + var statDto = statDtos + .Where(stat => stat.DateStart == statDtoFromDb.DateStart) + .Where(stat => stat.DateEnd == statDtoFromDb.DateEnd) + .FirstOrDefault(); + + MatchHelper.Match(statDtoFromDb, statDto, excludedProps); + } + } } } From deca1bf35b6a69e46e0d53924e3a3a5ccc6eabe0 Mon Sep 17 00:00:00 2001 From: Olga Nemt Date: Tue, 6 Feb 2024 11:44:29 +0500 Subject: [PATCH 07/12] =?UTF-8?q?=D0=9F=D1=80=D0=B0=D0=B2=D0=BA=D0=B8=20?= =?UTF-8?q?=D0=BF=D0=BE=20=D1=80=D0=B5=D0=B7=D1=83=D0=BB=D1=8C=D1=82=D0=B0?= =?UTF-8?q?=D1=82=D0=B0=D0=BC=20=D1=80=D0=B5=D0=B2=D1=8C=D1=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Repositories/IDataSaubStatRepository.cs | 4 +-- .../PeriodicWorks/WorkDataSaubStat.cs | 2 +- .../Repository/DataSaubStatRepository.cs | 8 ++--- .../Repository/DataSaubStatRepositoryTest.cs | 36 ++++++++++--------- 4 files changed, 24 insertions(+), 26 deletions(-) diff --git a/AsbCloudApp/Repositories/IDataSaubStatRepository.cs b/AsbCloudApp/Repositories/IDataSaubStatRepository.cs index 6894fe41..efab95b7 100644 --- a/AsbCloudApp/Repositories/IDataSaubStatRepository.cs +++ b/AsbCloudApp/Repositories/IDataSaubStatRepository.cs @@ -11,12 +11,12 @@ namespace AsbCloudApp.Repositories public interface IDataSaubStatRepository { /// - /// Получение последних дат по телеметриям + /// Получение последних по дате окончания бурения записей в разрезе телеметрий /// /// ключи телеметрий /// /// - Task> GetLastDatesAsync(int[] idTelemetries, CancellationToken token); + Task> GetLastsAsync(int[] idTelemetries, CancellationToken token); /// /// Вставка записей статистики diff --git a/AsbCloudInfrastructure/Background/PeriodicWorks/WorkDataSaubStat.cs b/AsbCloudInfrastructure/Background/PeriodicWorks/WorkDataSaubStat.cs index 93538b82..bc942353 100644 --- a/AsbCloudInfrastructure/Background/PeriodicWorks/WorkDataSaubStat.cs +++ b/AsbCloudInfrastructure/Background/PeriodicWorks/WorkDataSaubStat.cs @@ -43,7 +43,7 @@ namespace AsbCloudInfrastructure.Background.PeriodicWorks return; var dataSaubStatRepo = services.GetRequiredService(); - var stats = await dataSaubStatRepo.GetLastDatesAsync(idTelemetries, token); + var stats = await dataSaubStatRepo.GetLastsAsync(idTelemetries, token); for( var i =0; i < idTelemetries.Length; i++) { diff --git a/AsbCloudInfrastructure/Repository/DataSaubStatRepository.cs b/AsbCloudInfrastructure/Repository/DataSaubStatRepository.cs index cadb4d69..59064abf 100644 --- a/AsbCloudInfrastructure/Repository/DataSaubStatRepository.cs +++ b/AsbCloudInfrastructure/Repository/DataSaubStatRepository.cs @@ -20,15 +20,11 @@ namespace AsbCloudInfrastructure.Repository db = dbContext; } - public async Task> GetLastDatesAsync(int[] idTelemetries, CancellationToken token) + public async Task> GetLastsAsync(int[] idTelemetries, CancellationToken token) { var stats = await db.Set() .Where(s => idTelemetries.Contains(s.IdTelemetry)) - .GroupBy(s => s.IdTelemetry, (key, group) => new DataSaubStatDto() - { - IdTelemetry = key, - DateEnd = group.Max(s => s.DateEnd) - }) + .GroupBy(s => s.IdTelemetry, (key, group) => group.OrderByDescending(el => el.DateEnd).First().Adapt()) .ToArrayAsync(token); return stats; diff --git a/AsbCloudWebApi.IntegrationTests/Repository/DataSaubStatRepositoryTest.cs b/AsbCloudWebApi.IntegrationTests/Repository/DataSaubStatRepositoryTest.cs index 08ff3584..b328e8b8 100644 --- a/AsbCloudWebApi.IntegrationTests/Repository/DataSaubStatRepositoryTest.cs +++ b/AsbCloudWebApi.IntegrationTests/Repository/DataSaubStatRepositoryTest.cs @@ -75,31 +75,30 @@ namespace AsbCloudWebApi.IntegrationTests.Repository public DataSaubStatRepositoryTest(WebAppFactoryFixture factory) : base(factory) { - var dbSet = dbContext.Set(); - var categories = dbContext.Set(); - - categories.RemoveRange(categories); - dbSet.RemoveRange(dbSet); - dbContext.SaveChanges(); - - categories.Add(category); - - var entities = statDtos.Select(stat => stat.Adapt()); - dbSet.AddRange(entities); - dbContext.SaveChanges(); - dataSaubStatRepository = scope.ServiceProvider.GetRequiredService(); } [Fact] public async Task GetLastDatesAsync_returns_success() { + //prepare + dbContext.CleanupDbSet(); + + var dbSetSaubStat = dbContext.Set(); + var dbSetCategories = dbContext.Set(); + + var entities = statDtos.Select(stat => stat.Adapt()); + + dbSetCategories.Add(category); + dbSetSaubStat.AddRange(entities); + dbContext.SaveChanges(); + //act var telemetryIds = statDtos.Select(stat => stat.IdTelemetry).ToArray(); - var result = await dataSaubStatRepository.GetLastDatesAsync(telemetryIds, CancellationToken.None); + var result = await dataSaubStatRepository.GetLastsAsync(telemetryIds, CancellationToken.None); var expected = statDtos.Max(stat => stat.DateEnd); - var actual = result.Any() ? result.First().DateEnd : DateTimeOffset.UnixEpoch; + var actual = result.First().DateEnd; //assert Assert.True((expected - actual).Ticks == 0.0); @@ -108,16 +107,19 @@ namespace AsbCloudWebApi.IntegrationTests.Repository [Fact] public async Task InsertRangeAsync_returns_success() { + //prepare + dbContext.CleanupDbSet(); + //act var dbSet = dbContext.Set(); dbSet.RemoveRange(dbSet); dbContext.SaveChanges(); var result = await dataSaubStatRepository.InsertRangeAsync(statDtos, CancellationToken.None); - + //assert Assert.Equal(statDtos.Length, result); - + var statDtosFromDb = dbSet.Select(stat => stat.Adapt()).ToArray(); var excludedProps = new[] { From 8bc66a0d858851681ce3010ed9f72b0fe6ffde2b Mon Sep 17 00:00:00 2001 From: Olga Nemt Date: Wed, 7 Feb 2024 13:28:02 +0500 Subject: [PATCH 08/12] =?UTF-8?q?=D0=9F=D1=80=D0=B8=D0=B2=D0=B5=D0=B4?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D0=B5=20=D0=B7=D0=B0=D0=BF=D0=B8=D1=81=D0=B5?= =?UTF-8?q?=D0=B9=20=D0=B8=D0=B7=20t=5Fdata=5Fsaub=5Fstat=20=D0=BA=D0=BE?= =?UTF-8?q?=20=D0=B2=D1=80=D0=B5=D0=BC=D0=B5=D0=BD=D0=B8=20=D0=BA=D1=83?= =?UTF-8?q?=D1=81=D1=82=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Repository/DataSaubStatRepository.cs | 36 ++++++++++++++++--- .../Repository/DataSaubStatRepositoryTest.cs | 35 ++++++++++++++---- 2 files changed, 61 insertions(+), 10 deletions(-) diff --git a/AsbCloudInfrastructure/Repository/DataSaubStatRepository.cs b/AsbCloudInfrastructure/Repository/DataSaubStatRepository.cs index 59064abf..92a228d8 100644 --- a/AsbCloudInfrastructure/Repository/DataSaubStatRepository.cs +++ b/AsbCloudInfrastructure/Repository/DataSaubStatRepository.cs @@ -1,5 +1,6 @@ using AsbCloudApp.Data; using AsbCloudApp.Repositories; +using AsbCloudApp.Services; using AsbCloudDb.Model; using Mapster; using Microsoft.EntityFrameworkCore; @@ -14,27 +15,54 @@ namespace AsbCloudInfrastructure.Repository public class DataSaubStatRepository : IDataSaubStatRepository { private readonly IAsbCloudDbContext db; + private readonly ITelemetryService telemetryService; - public DataSaubStatRepository(IAsbCloudDbContext dbContext) + public DataSaubStatRepository(IAsbCloudDbContext dbContext, ITelemetryService telemetryService) { db = dbContext; + this.telemetryService = telemetryService; + } public async Task> GetLastsAsync(int[] idTelemetries, CancellationToken token) { + var timeZoneOffsets = idTelemetries + .Distinct() + .ToDictionary(idTelemetry => idTelemetry, idTelemetry => TimeSpan.FromHours(telemetryService.GetTimezone(idTelemetry).Hours)); + var stats = await db.Set() .Where(s => idTelemetries.Contains(s.IdTelemetry)) - .GroupBy(s => s.IdTelemetry, (key, group) => group.OrderByDescending(el => el.DateEnd).First().Adapt()) + .GroupBy(s => s.IdTelemetry, (key, group) => group.OrderByDescending(el => el.DateEnd).First()) .ToArrayAsync(token); - return stats; + var result = stats.Select(s => ConvertToDto(s, timeZoneOffsets[s.IdTelemetry])); + + return result; } public async Task InsertRangeAsync(IEnumerable dataSaubStats, CancellationToken token) { - var entities = dataSaubStats.Select(data => data.Adapt()); + var entities = dataSaubStats.Select(data => ConvertToEntity(data)); db.Set().AddRange(entities); return await db.SaveChangesAsync(token); } + + private static DataSaubStatDto ConvertToDto(DataSaubStat entity, TimeSpan timeSpan) + { + var dto = entity.Adapt(); + dto.DateStart = dto.DateStart.ToOffset(timeSpan); + dto.DateEnd = dto.DateEnd.ToOffset(timeSpan); + + return dto; + } + + private static DataSaubStat ConvertToEntity(DataSaubStatDto dto) + { + var entity = dto.Adapt(); + entity.DateStart = dto.DateStart.ToUniversalTime(); + entity.DateEnd = dto.DateEnd.ToUniversalTime(); + + return entity; + } } } diff --git a/AsbCloudWebApi.IntegrationTests/Repository/DataSaubStatRepositoryTest.cs b/AsbCloudWebApi.IntegrationTests/Repository/DataSaubStatRepositoryTest.cs index b328e8b8..aa6102ba 100644 --- a/AsbCloudWebApi.IntegrationTests/Repository/DataSaubStatRepositoryTest.cs +++ b/AsbCloudWebApi.IntegrationTests/Repository/DataSaubStatRepositoryTest.cs @@ -9,13 +9,15 @@ namespace AsbCloudWebApi.IntegrationTests.Repository { public class DataSaubStatRepositoryTest : BaseIntegrationTest { + private static readonly TimeSpan timeSpan = TimeSpan.FromHours(1); + private static readonly DataSaubStatDto[] statDtos = new DataSaubStatDto[2] { new DataSaubStatDto() { IdTelemetry = 1, - DateEnd = new DateTimeOffset(2024, 1, 1, 20, 25, 0, TimeSpan.Zero), - DateStart = new DateTimeOffset(2024, 1, 1, 20, 15, 0, TimeSpan.Zero), + DateEnd = new DateTimeOffset(2024, 1, 1, 20, 25, 0, timeSpan), + DateStart = new DateTimeOffset(2024, 1, 1, 20, 15, 0, timeSpan), AxialLoad = 10.0, AxialLoadLimitMax = 10.0, AxialLoadSp = 10.0, @@ -40,8 +42,8 @@ namespace AsbCloudWebApi.IntegrationTests.Repository new DataSaubStatDto() { IdTelemetry = 1, - DateEnd = new DateTimeOffset(2024, 2, 2, 20, 25, 0, TimeSpan.Zero), - DateStart = new DateTimeOffset(2024, 2, 2, 20, 15, 0, TimeSpan.Zero), + DateEnd = new DateTimeOffset(2024, 2, 2, 20, 25, 0, timeSpan), + DateStart = new DateTimeOffset(2024, 2, 2, 20, 15, 0, timeSpan), AxialLoad = 10.0, AxialLoadLimitMax = 10.0, AxialLoadSp = 10.0, @@ -87,7 +89,7 @@ namespace AsbCloudWebApi.IntegrationTests.Repository var dbSetSaubStat = dbContext.Set(); var dbSetCategories = dbContext.Set(); - var entities = statDtos.Select(stat => stat.Adapt()); + var entities = statDtos.Select(stat => ConvertToEntity(stat)); dbSetCategories.Add(category); dbSetSaubStat.AddRange(entities); @@ -113,6 +115,10 @@ namespace AsbCloudWebApi.IntegrationTests.Repository //act var dbSet = dbContext.Set(); dbSet.RemoveRange(dbSet); + + var dbSetCategories = dbContext.Set(); + dbSetCategories.Add(category); + dbContext.SaveChanges(); var result = await dataSaubStatRepository.InsertRangeAsync(statDtos, CancellationToken.None); @@ -120,7 +126,7 @@ namespace AsbCloudWebApi.IntegrationTests.Repository //assert Assert.Equal(statDtos.Length, result); - var statDtosFromDb = dbSet.Select(stat => stat.Adapt()).ToArray(); + var statDtosFromDb = dbSet.Select(stat => ConvertToDto(stat, timeSpan)).ToArray(); var excludedProps = new[] { nameof(DataSaubStat.Telemetry), @@ -136,7 +142,24 @@ namespace AsbCloudWebApi.IntegrationTests.Repository MatchHelper.Match(statDtoFromDb, statDto, excludedProps); } + } + private static DataSaubStat ConvertToEntity(DataSaubStatDto stat) + { + var entity = stat.Adapt(); + entity.DateStart = entity.DateStart.ToUniversalTime(); + entity.DateEnd = entity.DateEnd.ToUniversalTime(); + + return entity; + } + + private static DataSaubStatDto ConvertToDto(DataSaubStat stat, TimeSpan timeSpan) + { + var dto = stat.Adapt(); + dto.DateStart = dto.DateStart.ToOffset(timeSpan); + dto.DateEnd = dto.DateEnd.ToOffset(timeSpan); + + return dto; } } } From 6bdf5405998a9f33c9e45775bb2c5576cd737548 Mon Sep 17 00:00:00 2001 From: Olga Nemt Date: Wed, 7 Feb 2024 13:30:08 +0500 Subject: [PATCH 09/12] =?UTF-8?q?=D0=9F=D1=80=D0=B8=D0=B2=D0=B5=D0=B4?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D0=B5=20=D0=B7=D0=B0=D0=BF=D0=B8=D1=81=D0=B5?= =?UTF-8?q?=D0=B9=20=D0=B8=D0=B7=20t=5Fdata=5Fsaub=5Fstat=20=D0=BA=D0=BE?= =?UTF-8?q?=20=D0=B2=D1=80=D0=B5=D0=BC=D0=B5=D0=BD=D0=B8=20=D0=BA=D1=83?= =?UTF-8?q?=D1=81=D1=82=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AsbCloudWebApi/appsettings.json | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/AsbCloudWebApi/appsettings.json b/AsbCloudWebApi/appsettings.json index 72d50f57..5294ebe7 100644 --- a/AsbCloudWebApi/appsettings.json +++ b/AsbCloudWebApi/appsettings.json @@ -7,10 +7,10 @@ } }, "ConnectionStrings": { - "DefaultConnection": "Host=localhost;Database=postgres;Username=postgres;Password=q;Persist Security Info=True", - "DebugConnection": "Host=localhost;Database=postgres;Username=postgres;Password=q;Persist Security Info=True;Include Error Detail=True", + "DefaultConnection": "Host=localhost;Database=postgres2;Username=postgres;Password=q;Persist Security Info=True", + "DebugConnection": "Host=localhost;Database=postgres2;Username=postgres;Password=q;Persist Security Info=True;Include Error Detail=True", "TestConnection": "Host=localhost;Database=test;Username=postgres;Password=q;Persist Security Info=True;Include Error Detail=True", - "LocalConnection": "Host=localhost;Database=postgres;Username=postgres;Password=q;Persist Security Info=True" + "LocalConnection": "Host=localhost;Database=postgres2;Username=postgres;Password=q;Persist Security Info=True" }, "AllowedHosts": "*", "ContentPath": "../data", From a423cf9f6787a73bbd8769e324cedf32fcd93f0c Mon Sep 17 00:00:00 2001 From: Olga Nemt Date: Wed, 7 Feb 2024 13:30:40 +0500 Subject: [PATCH 10/12] =?UTF-8?q?=D0=A3=D0=B1=D1=80=D0=B0=D0=BD=D0=BE=20?= =?UTF-8?q?=D0=BB=D0=B8=D1=88=D0=BD=D0=B5=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AsbCloudWebApi/appsettings.json | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/AsbCloudWebApi/appsettings.json b/AsbCloudWebApi/appsettings.json index 5294ebe7..72d50f57 100644 --- a/AsbCloudWebApi/appsettings.json +++ b/AsbCloudWebApi/appsettings.json @@ -7,10 +7,10 @@ } }, "ConnectionStrings": { - "DefaultConnection": "Host=localhost;Database=postgres2;Username=postgres;Password=q;Persist Security Info=True", - "DebugConnection": "Host=localhost;Database=postgres2;Username=postgres;Password=q;Persist Security Info=True;Include Error Detail=True", + "DefaultConnection": "Host=localhost;Database=postgres;Username=postgres;Password=q;Persist Security Info=True", + "DebugConnection": "Host=localhost;Database=postgres;Username=postgres;Password=q;Persist Security Info=True;Include Error Detail=True", "TestConnection": "Host=localhost;Database=test;Username=postgres;Password=q;Persist Security Info=True;Include Error Detail=True", - "LocalConnection": "Host=localhost;Database=postgres2;Username=postgres;Password=q;Persist Security Info=True" + "LocalConnection": "Host=localhost;Database=postgres;Username=postgres;Password=q;Persist Security Info=True" }, "AllowedHosts": "*", "ContentPath": "../data", From 2b4000ce6b9a5abb19fc55f84abed9add6c72288 Mon Sep 17 00:00:00 2001 From: Olga Nemt Date: Wed, 7 Feb 2024 13:32:15 +0500 Subject: [PATCH 11/12] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B0=20DataSaubStatDto?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AsbCloudApp/Data/DataSaubStatDto.cs | 132 ++++++++++++++++++++++++++++ 1 file changed, 132 insertions(+) create mode 100644 AsbCloudApp/Data/DataSaubStatDto.cs diff --git a/AsbCloudApp/Data/DataSaubStatDto.cs b/AsbCloudApp/Data/DataSaubStatDto.cs new file mode 100644 index 00000000..639154df --- /dev/null +++ b/AsbCloudApp/Data/DataSaubStatDto.cs @@ -0,0 +1,132 @@ +using System; + +namespace AsbCloudApp.Data +{ + public class DataSaubStatDto + { + /// + /// + /// + public int Id { get; set; } + + /// + /// Дата и время начала + /// + public DateTimeOffset DateStart { get; set; } + + /// + /// Дата и время окончания + /// + public DateTimeOffset DateEnd { get; set; } + + /// + /// Глубина забоя по стволу начальная + /// + public double DepthStart { get; set; } + + /// + /// Глубина забоя по стволу конечная + /// + public double DepthEnd { get; set; } + + /// + /// Скорость бурения + /// + public double Speed { get; set; } + + /// + /// Ограничение скорости блока + /// + public double? BlockSpeedSp { get; set; } + + /// + /// Давление + /// + public double Pressure { get; set; } + + /// + /// Давление холостого хода + /// + public double? PressureIdle { get; set; } + + /// + /// Ограничение фактического давления + /// + public double? PressureSp { get; set; } + + /// + /// Фактическая нагрузка + /// + public double AxialLoad { get; set; } + + /// + /// Ограничение факт. нагрузки + /// + public double? AxialLoadSp { get; set; } + + /// + /// Максимально допустимая нагрузка + /// + public double? AxialLoadLimitMax { get; set; } + + /// + /// Фактический момент + /// + public double RotorTorque { get; set; } + + /// + /// Ограничение факт. момента + /// + public double? RotorTorqueSp { get; set; } + + /// + /// Максимально допустимый момент + /// + public double? RotorTorqueLimitMax { get; set; } + + /// + /// Работа при достижении ограничения + /// + public short? IdFeedRegulator { get; set; } + + /// + /// Фактическая скорость оборотов ВСП + /// + public double RotorSpeed { get; set; } + + /// + /// Название автоопределённой операции + /// + public int IdCategory { get; set; } + + /// + /// Флаги подсистем + /// + public int EnabledSubsystems { get; set; } + + /// + /// Наличие или отсутствие осцилляции + /// + public bool HasOscillation { get; set; } + + /// + /// Фактический расход + /// + public double Flow { get; set; } + + /// + /// Ключ телеметрии + /// + public int IdTelemetry { get; set; } + + /// + /// Телеметрия + /// + public TelemetryDto Telemetry { get; set; } = null!; + + /// + /// Категория автоопределенной операции + /// + public WellOperationCategoryDto OperationCategory { get; set; } = null!; + } +} From 1e27b7e5597c735990100fb3dfc2bb95a3201eda Mon Sep 17 00:00:00 2001 From: ngfrolov Date: Wed, 7 Feb 2024 13:53:08 +0500 Subject: [PATCH 12/12] fix tests --- .../Repository/DataSaubStatRepositoryTest.cs | 298 +++++++++--------- 1 file changed, 149 insertions(+), 149 deletions(-) diff --git a/AsbCloudWebApi.IntegrationTests/Repository/DataSaubStatRepositoryTest.cs b/AsbCloudWebApi.IntegrationTests/Repository/DataSaubStatRepositoryTest.cs index aa6102ba..f44d32ed 100644 --- a/AsbCloudWebApi.IntegrationTests/Repository/DataSaubStatRepositoryTest.cs +++ b/AsbCloudWebApi.IntegrationTests/Repository/DataSaubStatRepositoryTest.cs @@ -5,161 +5,161 @@ using Mapster; using Microsoft.Extensions.DependencyInjection; using Xunit; -namespace AsbCloudWebApi.IntegrationTests.Repository +namespace AsbCloudWebApi.IntegrationTests.Repository; + +public class DataSaubStatRepositoryTest : BaseIntegrationTest { - public class DataSaubStatRepositoryTest : BaseIntegrationTest + private static readonly TimeSpan timeSpan = TimeSpan.FromHours(1); + + private static readonly DataSaubStatDto[] statDtos = new DataSaubStatDto[2] { - private static readonly TimeSpan timeSpan = TimeSpan.FromHours(1); - - private static readonly DataSaubStatDto[] statDtos = new DataSaubStatDto[2] + new() { - new DataSaubStatDto() - { - IdTelemetry = 1, - DateEnd = new DateTimeOffset(2024, 1, 1, 20, 25, 0, timeSpan), - DateStart = new DateTimeOffset(2024, 1, 1, 20, 15, 0, timeSpan), - AxialLoad = 10.0, - AxialLoadLimitMax = 10.0, - AxialLoadSp = 10.0, - BlockSpeedSp = 1000, - DepthEnd = 10.0, - DepthStart = 5.0, - EnabledSubsystems = 1, - Flow = 10.0, - HasOscillation = true, - Id = default, - IdCategory = 2, - IdFeedRegulator = 1, - Pressure = 10.0, - PressureIdle = 10.0, - PressureSp = 10.0, - RotorSpeed = 9.0, - RotorTorque = 9.0, - RotorTorqueSp = 9.0, - RotorTorqueLimitMax = 9.0, - Speed = 10.0 - }, - new DataSaubStatDto() - { - IdTelemetry = 1, - DateEnd = new DateTimeOffset(2024, 2, 2, 20, 25, 0, timeSpan), - DateStart = new DateTimeOffset(2024, 2, 2, 20, 15, 0, timeSpan), - AxialLoad = 10.0, - AxialLoadLimitMax = 10.0, - AxialLoadSp = 10.0, - BlockSpeedSp = 1000, - DepthEnd = 10.0, - DepthStart = 5.0, - EnabledSubsystems = 1, - Flow = 10.0, - HasOscillation = true, - Id = default, - IdCategory = 2, - IdFeedRegulator = 1, - Pressure = 10.0, - PressureIdle = 10.0, - PressureSp = 10.0, - RotorSpeed = 10.0, - RotorTorque = 10.0, - RotorTorqueSp = 10.0, - RotorTorqueLimitMax = 10.0, - Speed = 10.0 - } + IdTelemetry = 1, + DateEnd = new DateTimeOffset(2024, 1, 1, 20, 25, 0, timeSpan), + DateStart = new DateTimeOffset(2024, 1, 1, 20, 15, 0, timeSpan), + AxialLoad = 10.0, + AxialLoadLimitMax = 10.0, + AxialLoadSp = 10.0, + BlockSpeedSp = 1000, + DepthEnd = 10.0, + DepthStart = 5.0, + EnabledSubsystems = 1, + Flow = 10.0, + HasOscillation = true, + Id = default, + IdCategory = 2, + IdFeedRegulator = 1, + Pressure = 10.0, + PressureIdle = 10.0, + PressureSp = 10.0, + RotorSpeed = 9.0, + RotorTorque = 9.0, + RotorTorqueSp = 9.0, + RotorTorqueLimitMax = 9.0, + Speed = 10.0 + }, + new() + { + IdTelemetry = 1, + DateEnd = new DateTimeOffset(2024, 2, 2, 20, 25, 0, timeSpan), + DateStart = new DateTimeOffset(2024, 2, 2, 20, 15, 0, timeSpan), + AxialLoad = 10.0, + AxialLoadLimitMax = 10.0, + AxialLoadSp = 10.0, + BlockSpeedSp = 1000, + DepthEnd = 10.0, + DepthStart = 5.0, + EnabledSubsystems = 1, + Flow = 10.0, + HasOscillation = true, + Id = default, + IdCategory = 2, + IdFeedRegulator = 1, + Pressure = 10.0, + PressureIdle = 10.0, + PressureSp = 10.0, + RotorSpeed = 10.0, + RotorTorque = 10.0, + RotorTorqueSp = 10.0, + RotorTorqueLimitMax = 10.0, + Speed = 10.0 + } + }; + private static readonly WellOperationCategory category = new() + { + Id = 2, + IdParent = null, + Name = "Категория 2" + }; + + private readonly IDataSaubStatRepository dataSaubStatRepository; + + public DataSaubStatRepositoryTest(WebAppFactoryFixture factory) : base(factory) + { + dataSaubStatRepository = scope.ServiceProvider.GetRequiredService(); + } + + [Fact] + public async Task GetLastDatesAsync_returns_success() + { + //prepare + dbContext.CleanupDbSet(); + dbContext.CleanupDbSet(); + + var dbSetSaubStat = dbContext.Set(); + var dbSetCategories = dbContext.Set(); + + var entities = statDtos.Select(stat => ConvertToEntity(stat)); + + dbSetCategories.Add(category); + dbContext.SaveChanges(); + + dbSetSaubStat.AddRange(entities); + dbContext.SaveChanges(); + + //act + var telemetryIds = statDtos.Select(stat => stat.IdTelemetry).ToArray(); + var result = await dataSaubStatRepository.GetLastsAsync(telemetryIds, CancellationToken.None); + + var expected = statDtos.Max(stat => stat.DateEnd); + var actual = result.First().DateEnd; + + //assert + Assert.True((expected - actual).Ticks == 0.0); + } + + [Fact] + public async Task InsertRangeAsync_returns_success() + { + //prepare + dbContext.CleanupDbSet(); + var dbSet = dbContext.Set(); + + var dbSetCategories = dbContext.Set(); + dbSetCategories.Add(category); + + dbContext.SaveChanges(); + + //act + var result = await dataSaubStatRepository.InsertRangeAsync(statDtos, CancellationToken.None); + + //assert + Assert.Equal(statDtos.Length, result); + + var statDtosFromDb = dbSet.Select(stat => ConvertToDto(stat, timeSpan)).ToArray(); + + var excludedProps = new[] { + nameof(DataSaubStat.Telemetry), + nameof(DataSaubStat.Id), + nameof(DataSaubStat.OperationCategory) }; - private static readonly WellOperationCategory category = new() + foreach (var statDtoFromDb in statDtosFromDb) { - Id = 2, - IdParent = null, - Name = "Категория 2" - }; + var statDto = statDtos + .Where(stat => stat.DateStart == statDtoFromDb.DateStart) + .Where(stat => stat.DateEnd == statDtoFromDb.DateEnd) + .FirstOrDefault(); - private readonly IDataSaubStatRepository dataSaubStatRepository; - - public DataSaubStatRepositoryTest(WebAppFactoryFixture factory) : base(factory) - { - dataSaubStatRepository = scope.ServiceProvider.GetRequiredService(); - } - - [Fact] - public async Task GetLastDatesAsync_returns_success() - { - //prepare - dbContext.CleanupDbSet(); - - var dbSetSaubStat = dbContext.Set(); - var dbSetCategories = dbContext.Set(); - - var entities = statDtos.Select(stat => ConvertToEntity(stat)); - - dbSetCategories.Add(category); - dbSetSaubStat.AddRange(entities); - dbContext.SaveChanges(); - - //act - var telemetryIds = statDtos.Select(stat => stat.IdTelemetry).ToArray(); - var result = await dataSaubStatRepository.GetLastsAsync(telemetryIds, CancellationToken.None); - - var expected = statDtos.Max(stat => stat.DateEnd); - var actual = result.First().DateEnd; - - //assert - Assert.True((expected - actual).Ticks == 0.0); - } - - [Fact] - public async Task InsertRangeAsync_returns_success() - { - //prepare - dbContext.CleanupDbSet(); - - //act - var dbSet = dbContext.Set(); - dbSet.RemoveRange(dbSet); - - var dbSetCategories = dbContext.Set(); - dbSetCategories.Add(category); - - dbContext.SaveChanges(); - - var result = await dataSaubStatRepository.InsertRangeAsync(statDtos, CancellationToken.None); - - //assert - Assert.Equal(statDtos.Length, result); - - var statDtosFromDb = dbSet.Select(stat => ConvertToDto(stat, timeSpan)).ToArray(); - - var excludedProps = new[] { - nameof(DataSaubStat.Telemetry), - nameof(DataSaubStat.Id), - nameof(DataSaubStat.OperationCategory) - }; - foreach (var statDtoFromDb in statDtosFromDb) - { - var statDto = statDtos - .Where(stat => stat.DateStart == statDtoFromDb.DateStart) - .Where(stat => stat.DateEnd == statDtoFromDb.DateEnd) - .FirstOrDefault(); - - MatchHelper.Match(statDtoFromDb, statDto, excludedProps); - } - } - - private static DataSaubStat ConvertToEntity(DataSaubStatDto stat) - { - var entity = stat.Adapt(); - entity.DateStart = entity.DateStart.ToUniversalTime(); - entity.DateEnd = entity.DateEnd.ToUniversalTime(); - - return entity; - } - - private static DataSaubStatDto ConvertToDto(DataSaubStat stat, TimeSpan timeSpan) - { - var dto = stat.Adapt(); - dto.DateStart = dto.DateStart.ToOffset(timeSpan); - dto.DateEnd = dto.DateEnd.ToOffset(timeSpan); - - return dto; + MatchHelper.Match(statDtoFromDb, statDto, excludedProps); } } + + private static DataSaubStat ConvertToEntity(DataSaubStatDto stat) + { + var entity = stat.Adapt(); + entity.DateStart = entity.DateStart.ToUniversalTime(); + entity.DateEnd = entity.DateEnd.ToUniversalTime(); + + return entity; + } + + private static DataSaubStatDto ConvertToDto(DataSaubStat stat, TimeSpan timeSpan) + { + var dto = stat.Adapt(); + dto.DateStart = dto.DateStart.ToOffset(timeSpan); + dto.DateEnd = dto.DateEnd.ToOffset(timeSpan); + + return dto; + } }