diff --git a/AsbCloudInfrastructure/Services/DataSaubStatService.cs b/AsbCloudInfrastructure/Services/DataSaubStatService.cs index b9bcc251..6f3d48ea 100644 --- a/AsbCloudInfrastructure/Services/DataSaubStatService.cs +++ b/AsbCloudInfrastructure/Services/DataSaubStatService.cs @@ -114,7 +114,7 @@ namespace AsbCloudInfrastructure.Services if (indexEnd == indexStart) continue; - var length = indexEnd - indexStart; + var length = indexEnd - indexStart + 1; var subset = dataSaub.AsSpan(indexStart, length); var stats = CalcStats(operation, subset); @@ -135,7 +135,7 @@ namespace AsbCloudInfrastructure.Services if (IsNewCacheItem(previous, current) || i == dataSaub.Length - 1) { - var length = i - indexStart; + var length = i - indexStart + 1; var span = dataSaub.Slice(indexStart, length); indexStart = i; if (length <= 2 || (span[^1].WellDepth - span[0].WellDepth) < 0.001) diff --git a/AsbCloudWebApi.Tests/Services/DataSaubStatServiceTest.cs b/AsbCloudWebApi.Tests/Services/DataSaubStatServiceTest.cs index 32595c0d..baae9b4f 100644 --- a/AsbCloudWebApi.Tests/Services/DataSaubStatServiceTest.cs +++ b/AsbCloudWebApi.Tests/Services/DataSaubStatServiceTest.cs @@ -1,27 +1,24 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Runtime.CompilerServices; -using System.Threading; -using System.Threading.Tasks; using AsbCloudApp.Data; using AsbCloudApp.Data.DetectedOperation; using AsbCloudApp.Data.SAUB; -using AsbCloudApp.Data.User; using AsbCloudApp.Repositories; using AsbCloudApp.Requests; using AsbCloudApp.Services; -using AsbCloudInfrastructure.Repository; using AsbCloudInfrastructure.Services; -using AsbCloudInfrastructure.Services.SAUB; +using Mapster; using NSubstitute; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; using Xunit; namespace AsbCloudWebApi.Tests.Services; public class DataSaubStatServiceTest { + private readonly int Gap = 5; private readonly IDataSaubStatRepository dataSaubStatRepositoryMock = Substitute.For(); private readonly ITelemetryDataCache telemetryDataCacheMock = Substitute.For>(); private readonly IDetectedOperationRepository detectedOperationRepositoryMock = Substitute.For(); @@ -29,7 +26,7 @@ public class DataSaubStatServiceTest private DataSaubStatService dataSaubStatService; - private int[] idTelemetries = [1, 2, 3]; + private int[] idTelemetries = [1]; private IEnumerable dataSaubStatDtos = new List() { new DataSaubStatDto { @@ -55,7 +52,7 @@ public class DataSaubStatServiceTest RotorTorque = 1, RotorTorqueLimitMax = 1, RotorTorqueSp = 1, - Speed = 1 + Speed = 1 }, new DataSaubStatDto { Id = 2, @@ -109,8 +106,8 @@ public class DataSaubStatServiceTest }, }; - private IEnumerable detectedOperationDtos = new List() { - new DetectedOperationDto { + private List detectedOperationDtos = new List() { + new DetectedOperationDto { Id = 1, DateEnd = DateTimeOffset.UtcNow, DateStart = DateTimeOffset.UtcNow.AddHours(-1), @@ -120,11 +117,11 @@ public class DataSaubStatServiceTest IdTelemetry = 1, Value = 1, IdEditor = 1, - IdUserAtStart = 1 + IdUserAtStart = 1 } }; - private IEnumerable telemetryDataSaubDtos1 = new List { + private List telemetryDataSaubDtos = new List { new TelemetryDataSaubDto() { IdTelemetry = 1, @@ -168,280 +165,7 @@ public class DataSaubStatServiceTest RotorTorqueIdle = 1, RotorTorqueSp = 1, RotorTorqueLimitMax = 1, - WellDepth = 1, - }, - new TelemetryDataSaubDto() - { - IdTelemetry = 1, - DateTime = DateTime.UtcNow.AddMinutes(-20), - AxialLoad = 1, - AxialLoadLimitMax = 1, - AxialLoadSp = 1, - BitDepth = 1, - BlockPosition = 1, - BlockPositionMax = 1, - BlockPositionMin = 1, - BlockSpeed = 1, - BlockSpeedSp = 1, - BlockSpeedSpDevelop = 1, - BlockSpeedSpRotor = 1, - BlockSpeedSpSlide = 1, - Flow = 1, - FlowDeltaLimitMax = 1, - FlowIdle = 1, - HookWeight = 1, - HookWeightIdle = 1, - HookWeightLimitMax = 1, - HookWeightLimitMin = 1, - IdFeedRegulator = 1, - IdUser = 1, - Mode = 1, - Mse = 1, - MseState = 1, - Pressure = 1, - PressureDeltaLimitMax = 1, - PressureIdle = 1, - PressureSp = 1, - PressureSpDevelop = 1, - PressureSpRotor = 1, - PressureSpSlide = 1, - Pump0Flow = 1, - Pump1Flow = 1, - Pump2Flow = 1, - RotorSpeed = 1, - RotorTorque = 1, - RotorTorqueIdle = 1, - RotorTorqueSp = 1, - RotorTorqueLimitMax = 1, - WellDepth = 1, - }, - new TelemetryDataSaubDto() - { - IdTelemetry = 1, - DateTime = DateTime.UtcNow.AddMinutes(-10), - AxialLoad = 1, - AxialLoadLimitMax = 1, - AxialLoadSp = 1, - BitDepth = 1, - BlockPosition = 1, - BlockPositionMax = 1, - BlockPositionMin = 1, - BlockSpeed = 1, - BlockSpeedSp = 1, - BlockSpeedSpDevelop = 1, - BlockSpeedSpRotor = 1, - BlockSpeedSpSlide = 1, - Flow = 1, - FlowDeltaLimitMax = 1, - FlowIdle = 1, - HookWeight = 1, - HookWeightIdle = 1, - HookWeightLimitMax = 1, - HookWeightLimitMin = 1, - IdFeedRegulator = 1, - IdUser = 1, - Mode = 1, - Mse = 1, - MseState = 1, - Pressure = 1, - PressureDeltaLimitMax = 1, - PressureIdle = 1, - PressureSp = 1, - PressureSpDevelop = 1, - PressureSpRotor = 1, - PressureSpSlide = 1, - Pump0Flow = 1, - Pump1Flow = 1, - Pump2Flow = 1, - RotorSpeed = 1, - RotorTorque = 1, - RotorTorqueIdle = 1, - RotorTorqueSp = 1, - RotorTorqueLimitMax = 1, - WellDepth = 2, - }, - new TelemetryDataSaubDto() - { - IdTelemetry = 1, - DateTime = DateTime.UtcNow.AddMinutes(-5), - AxialLoad = 1, - AxialLoadLimitMax = 1, - AxialLoadSp = 1, - BitDepth = 1, - BlockPosition = 1, - BlockPositionMax = 1, - BlockPositionMin = 1, - BlockSpeed = 1, - BlockSpeedSp = 1, - BlockSpeedSpDevelop = 1, - BlockSpeedSpRotor = 1, - BlockSpeedSpSlide = 1, - Flow = 1, - FlowDeltaLimitMax = 1, - FlowIdle = 1, - HookWeight = 1, - HookWeightIdle = 1, - HookWeightLimitMax = 1, - HookWeightLimitMin = 1, - IdFeedRegulator = 1, - IdUser = 1, - Mode = 1, - Mse = 1, - MseState = 1, - Pressure = 1, - PressureDeltaLimitMax = 1, - PressureIdle = 1, - PressureSp = 1, - PressureSpDevelop = 1, - PressureSpRotor = 1, - PressureSpSlide = 1, - Pump0Flow = 1, - Pump1Flow = 1, - Pump2Flow = 1, - RotorSpeed = 1, - RotorTorque = 1, - RotorTorqueIdle = 1, - RotorTorqueSp = 1, - RotorTorqueLimitMax = 1, - WellDepth = 3, - }, - new TelemetryDataSaubDto() - { - IdTelemetry = 1, - DateTime = DateTime.UtcNow.AddMinutes(5), - AxialLoad = 1, - AxialLoadLimitMax = 1, - AxialLoadSp = 1, - BitDepth = 1, - BlockPosition = 1, - BlockPositionMax = 1, - BlockPositionMin = 1, - BlockSpeed = 1, - BlockSpeedSp = 1, - BlockSpeedSpDevelop = 1, - BlockSpeedSpRotor = 1, - BlockSpeedSpSlide = 1, - Flow = 1, - FlowDeltaLimitMax = 1, - FlowIdle = 1, - HookWeight = 1, - HookWeightIdle = 1, - HookWeightLimitMax = 1, - HookWeightLimitMin = 1, - IdFeedRegulator = 1, - IdUser = 1, - Mode = 1, - Mse = 1, - MseState = 1, - Pressure = 1, - PressureDeltaLimitMax = 1, - PressureIdle = 1, - PressureSp = 1, - PressureSpDevelop = 1, - PressureSpRotor = 1, - PressureSpSlide = 1, - Pump0Flow = 1, - Pump1Flow = 1, - Pump2Flow = 1, - RotorSpeed = 1, - RotorTorque = 1, - RotorTorqueIdle = 1, - RotorTorqueSp = 1, - RotorTorqueLimitMax = 1, - WellDepth = 1, - } - }; - - private IEnumerable telemetryDataSaubDtos2 = new List { - new TelemetryDataSaubDto() - { - IdTelemetry = 2, - DateTime = DateTime.Now.AddMinutes(-20), - AxialLoad = 1, - AxialLoadLimitMax = 1, - AxialLoadSp = 1, - BitDepth = 1, - BlockPosition = 1, - BlockPositionMax = 1, - BlockPositionMin = 1, - BlockSpeed = 1, - BlockSpeedSp = 1, - BlockSpeedSpDevelop = 1, - BlockSpeedSpRotor = 1, - BlockSpeedSpSlide = 1, - Flow = 1, - FlowDeltaLimitMax = 1, - FlowIdle = 1, - HookWeight = 1, - HookWeightIdle = 1, - HookWeightLimitMax = 1, - HookWeightLimitMin = 1, - IdFeedRegulator = 1, - IdUser = 1, - Mode = 1, - Mse = 1, - MseState = 1, - Pressure = 1, - PressureDeltaLimitMax = 1, - PressureIdle = 1, - PressureSp = 1, - PressureSpDevelop = 1, - PressureSpRotor = 1, - PressureSpSlide = 1, - Pump0Flow = 1, - Pump1Flow = 1, - Pump2Flow = 1, - RotorSpeed = 1, - RotorTorque = 1, - RotorTorqueIdle = 1, - RotorTorqueSp = 1, - RotorTorqueLimitMax = 1, - WellDepth = 1, - }, - new TelemetryDataSaubDto() - { - IdTelemetry = 2, - DateTime = DateTime.Now.AddMinutes(-20), - AxialLoad = 1, - AxialLoadLimitMax = 1, - AxialLoadSp = 1, - BitDepth = 1, - BlockPosition = 1, - BlockPositionMax = 1, - BlockPositionMin = 1, - BlockSpeed = 1, - BlockSpeedSp = 1, - BlockSpeedSpDevelop = 1, - BlockSpeedSpRotor = 1, - BlockSpeedSpSlide = 1, - Flow = 1, - FlowDeltaLimitMax = 1, - FlowIdle = 1, - HookWeight = 1, - HookWeightIdle = 1, - HookWeightLimitMax = 1, - HookWeightLimitMin = 1, - IdFeedRegulator = 1, - IdUser = 1, - Mode = 1, - Mse = 1, - MseState = 1, - Pressure = 1, - PressureDeltaLimitMax = 1, - PressureIdle = 1, - PressureSp = 1, - PressureSpDevelop = 1, - PressureSpRotor = 1, - PressureSpSlide = 1, - Pump0Flow = 1, - Pump1Flow = 1, - Pump2Flow = 1, - RotorSpeed = 1, - RotorTorque = 1, - RotorTorqueIdle = 1, - RotorTorqueSp = 1, - RotorTorqueLimitMax = 1, - WellDepth = 1, + WellDepth = 10, } }; @@ -455,35 +179,131 @@ public class DataSaubStatServiceTest .GetLastsAsync(Arg.Any(), Arg.Any()) .Returns(dataSaubStatDtos); - detectedOperationRepositoryMock - .Get(Arg.Any(), Arg.Any()) - .Returns(detectedOperationDtos); + var telemetrySaubDto = telemetryDataSaubDtos.FirstOrDefault(); + if (telemetrySaubDto != null) + { + //заполнение списка телеметрий следующим образом: + // - всего в списке 6 элементов: + // - все они попадают в диапазон, определенный датами DateStart и DateEnd соответствующей записи detectedOperation + // - из этих 6-х записей у 2-х записей меняется параметр, + // являющийся признаком начала нового интервала (новой записи dataSaubStat) + // таким образом, в базе данных должно создаться 1 новая запись dataSaubStat (insertedDataSaubStatCount = 1) + var telemetrySaubDto1 = telemetrySaubDto.Adapt(); + telemetrySaubDto1.DateTime = DateTime.UtcNow.AddMinutes(-20); + telemetryDataSaubDtos.Add(telemetrySaubDto1); + + var telemetrySaubDto2 = telemetrySaubDto.Adapt(); + telemetrySaubDto2.DateTime = DateTime.UtcNow.AddMinutes(-10); + telemetrySaubDto2.RotorTorqueLimitMax = 2; + telemetryDataSaubDtos.Add(telemetrySaubDto2); + + var telemetrySaubDto3 = telemetrySaubDto.Adapt(); + telemetrySaubDto3.DateTime = DateTime.UtcNow.AddMinutes(-8); + telemetrySaubDto3.RotorTorqueLimitMax = 2; + telemetryDataSaubDtos.Add(telemetrySaubDto3); + + var telemetrySaubDto4 = telemetrySaubDto.Adapt(); + telemetrySaubDto4.DateTime = DateTime.UtcNow.AddMinutes(-6); + telemetrySaubDto4.RotorTorqueLimitMax = 2; + telemetryDataSaubDtos.Add(telemetrySaubDto4); + + var telemetrySaubDto5 = telemetrySaubDto.Adapt(); + telemetrySaubDto5.DateTime = DateTime.UtcNow.AddMinutes(-4); + telemetrySaubDto5.RotorTorqueLimitMax = 2; + telemetryDataSaubDtos.Add(telemetrySaubDto5); + + var telemetrySaubDto6 = telemetrySaubDto.Adapt(); + telemetrySaubDto6.DateTime = DateTime.UtcNow.AddMinutes(-2); + telemetrySaubDto6.RotorTorqueLimitMax = 3; + telemetrySaubDto6.WellDepth = 11; + telemetryDataSaubDtos.Add(telemetrySaubDto6); + } dataSaubServiceMock - .Get(1, Arg.Any(), Arg.Any(), Arg.Any(), Arg.Any(), Arg.Any()) - .Returns(telemetryDataSaubDtos1); - - dataSaubStatRepositoryMock - .InsertRangeAsync(Arg.Any>(), Arg.Any()) - .Returns(2); + .Get(Arg.Any(), Arg.Any(), Arg.Any(), Arg.Any(), Arg.Any(), Arg.Any()) + .Returns(telemetryDataSaubDtos); dataSaubStatService = new DataSaubStatService( - dataSaubStatRepositoryMock, - telemetryDataCacheMock, - dataSaubServiceMock, + dataSaubStatRepositoryMock, + telemetryDataCacheMock, + dataSaubServiceMock, detectedOperationRepositoryMock); } [Fact] - public async Task GetByMarkId_ShouldReturn_FileInfo() + public async Task Create_1DataSaubStatItems_ShouldReturn__Success() { - //act - //var data = await fileService.GetByMarkId(idMark, CancellationToken.None); - Action action = async (a, b) => { - ; + var insertedDataSaubStatCount = 1; + + detectedOperationRepositoryMock + .Get(Arg.Any(), Arg.Any()) + .Returns(detectedOperationDtos); + + + dataSaubStatRepositoryMock + .InsertRangeAsync(Arg.Any>(), Arg.Any()) + .Returns(insertedDataSaubStatCount); + + Action action = (message, count) => + { + //assert + Assert.Equal(insertedDataSaubStatCount, count); }; - await dataSaubStatService.CreateStatAsync(5, action, CancellationToken.None); - //assert - Assert.NotNull("sd"); + + //act + await dataSaubStatService.CreateStatAsync(Gap, action, CancellationToken.None); + } + + [Fact] + public async Task Create_2DataSaubStatItems_ShouldReturn__Success() + { + var insertedDataSaubStatCount = 2; + + var detectedOperationDto = detectedOperationDtos.FirstOrDefault(); + if (detectedOperationDto != null) + { + var detectedOperationDto1 = detectedOperationDto.Adapt(); + detectedOperationDto1.DateStart = DateTimeOffset.UtcNow.AddMinutes(1); + detectedOperationDto1.DateEnd = DateTimeOffset.UtcNow.AddHours(1); + + detectedOperationDtos.Add(detectedOperationDto1); + } + + var telemetryDataSaubDto = telemetryDataSaubDtos.LastOrDefault(); + if (telemetryDataSaubDto != null) + { + var telemetryDataSaubDto1 = telemetryDataSaubDto.Adapt(); + telemetryDataSaubDto1.DateTime = DateTime.UtcNow.AddMinutes(10); + telemetryDataSaubDto1.WellDepth = telemetryDataSaubDto.WellDepth + 1; + + var telemetryDataSaubDto2 = telemetryDataSaubDto.Adapt(); + telemetryDataSaubDto2.DateTime = DateTime.UtcNow.AddMinutes(20); + telemetryDataSaubDto2.WellDepth = telemetryDataSaubDto1.WellDepth + 1; + + var telemetryDataSaubDto3 = telemetryDataSaubDto.Adapt(); + telemetryDataSaubDto3.DateTime = DateTime.UtcNow.AddMinutes(30); + telemetryDataSaubDto3.WellDepth = telemetryDataSaubDto2.WellDepth + 1; + + telemetryDataSaubDtos.Add(telemetryDataSaubDto1); + telemetryDataSaubDtos.Add(telemetryDataSaubDto2); + telemetryDataSaubDtos.Add(telemetryDataSaubDto3); + } + + detectedOperationRepositoryMock + .Get(Arg.Any(), Arg.Any()) + .Returns(detectedOperationDtos); + + dataSaubStatRepositoryMock + .InsertRangeAsync(Arg.Any>(), Arg.Any()) + .Returns(insertedDataSaubStatCount); + + Action action = (message, count) => + { + //assert + Assert.Equal(insertedDataSaubStatCount, count); + }; + + //act + await dataSaubStatService.CreateStatAsync(Gap, action, CancellationToken.None); } } \ No newline at end of file