diff --git a/AsbCloudApp/Services/DataSaubStat/IDataSaubStatDrillingQualityService.cs b/AsbCloudApp/Services/DataSaubStat/IDataSaubStatDrillingQualityService.cs index 5db1ff71..8adbfc39 100644 --- a/AsbCloudApp/Services/DataSaubStat/IDataSaubStatDrillingQualityService.cs +++ b/AsbCloudApp/Services/DataSaubStat/IDataSaubStatDrillingQualityService.cs @@ -1,4 +1,6 @@ +using AsbCloudApp.Data; using System; +using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; @@ -10,7 +12,7 @@ namespace AsbCloudApp.Services; public interface IDataSaubStatDrillingQualityService { /// - /// Расчет статистики DataSaubStat + /// Расчет статистики DataSaubStatDrillingQuality и сохранение её в бд /// /// /// Количество дней за которые должны были приходить данные, чтобы телеметрия попала в обработку. @@ -20,4 +22,16 @@ public interface IDataSaubStatDrillingQualityService /// Task CreateStatAsync(int lastDaysFilter, Action onProgressCallback, CancellationToken token); + /// + /// Расчет статистики DataSaubStatDrillingQuality + /// + /// + /// + /// + /// + Task> CreateStatDrillingQualityForTelemetry( + int idTelemetry, + DateTimeOffset geDate, + CancellationToken token); + } diff --git a/AsbCloudInfrastructure.Tests/Services/DataSaubStat/DataSaubStatDtillingQualityServiceTest.cs b/AsbCloudInfrastructure.Tests/Services/DataSaubStat/DataSaubStatDtillingQualityServiceTest.cs index ed2b07d0..17700f54 100644 --- a/AsbCloudInfrastructure.Tests/Services/DataSaubStat/DataSaubStatDtillingQualityServiceTest.cs +++ b/AsbCloudInfrastructure.Tests/Services/DataSaubStat/DataSaubStatDtillingQualityServiceTest.cs @@ -117,6 +117,31 @@ public class DataSaubStatDtillingQualityServiceTest .GetLastsAsync(Arg.Any(), Arg.Any()) .Returns(dataSaubStatDrillingQualityDtos); + dataSaubStatService = new DataSaubStatDrillingQualityService( + dataSaubStatRepositoryMock, + telemetryDataCacheMock, + dataSaubServiceMock); + } + + private TelemetryDataSaubDto CreateTelemetryDataSaubItem( + TelemetryDataSaubDto telemetrySaubDto, + float wellDepth, + float bitDepth, + short idFeedRegulator, + DateTime dateTime) + { + var telemetrySaubDto1 = telemetrySaubDto.Adapt(); + telemetrySaubDto1.WellDepth = wellDepth; + telemetrySaubDto1.BitDepth = bitDepth; + telemetrySaubDto1.IdFeedRegulator = idFeedRegulator; + telemetrySaubDto1.DateTime = dateTime; + + return telemetrySaubDto1; + } + + [Fact] + public async Task Create_1DataSaubStatItems_ShouldReturn__Success() + { var telemetrySaubDto = telemetryDataSaubDtos.FirstOrDefault(); if (telemetrySaubDto != null) { @@ -131,7 +156,8 @@ public class DataSaubStatDtillingQualityServiceTest //1 1500 //2 1600 // idFeedRegulator: - // depthWell = (1000 - 800) + (1300 - 900) + (1600 - 1100) = + // = 3 + // = (1000 - 800) + (1300 - 900) + (1600 - 1100) = 1100 var telemetrySaubDto1 = CreateTelemetryDataSaubItem( telemetrySaubDto, wellDepth: 900, @@ -141,10 +167,10 @@ public class DataSaubStatDtillingQualityServiceTest telemetryDataSaubDtos.Add(telemetrySaubDto1); var telemetrySaubDto2 = CreateTelemetryDataSaubItem( - telemetrySaubDto, - wellDepth: 1000, - bitDepth: 1000, - idFeedRegulator: 1, + telemetrySaubDto, + wellDepth: 1000, + bitDepth: 1000, + idFeedRegulator: 1, dateTime: DateTime.UtcNow.AddMinutes(20)); telemetryDataSaubDtos.Add(telemetrySaubDto2); @@ -201,54 +227,11 @@ public class DataSaubStatDtillingQualityServiceTest .Get(Arg.Any(), Arg.Any(), Arg.Any(), Arg.Any(), Arg.Any(), Arg.Any()) .Returns(telemetryDataSaubDtos); - dataSaubStatService = new DataSaubStatDrillingQualityService( - dataSaubStatRepositoryMock, - telemetryDataCacheMock, - dataSaubServiceMock); - } - - private TelemetryDataSaubDto CreateTelemetryDataSaubItem( - TelemetryDataSaubDto telemetrySaubDto, - float wellDepth, - float bitDepth, - short idFeedRegulator, - DateTime dateTime) - { - var telemetrySaubDto1 = telemetrySaubDto.Adapt(); - telemetrySaubDto1.WellDepth = wellDepth; - telemetrySaubDto1.BitDepth = bitDepth; - telemetrySaubDto1.IdFeedRegulator = idFeedRegulator; - telemetrySaubDto1.DateTime = dateTime; - - return telemetrySaubDto1; - } - - [Fact] - public async Task Create_1DataSaubStatItems_ShouldReturn__Success() - { - var insertedDataSaubStatCount = 2; - - - - - dataSaubStatRepositoryMock - .InsertRangeAsync(Arg.Any>(), Arg.Any()) - .Returns(insertedDataSaubStatCount); - - Action action = (message, percent) => - { - //assert - Assert.NotNull(percent); - Assert.InRange(percent.Value, 0.0, 1.0); - }; - //act - await dataSaubStatService.CreateStatAsync(Gap, action, CancellationToken.None); + var result = await dataSaubStatService.CreateStatDrillingQualityForTelemetry(1, DateTimeOffset.UnixEpoch, CancellationToken.None); //assert - await dataSaubStatRepositoryMock.Received().InsertRangeAsync( - Arg.Is>(l => l.Count() == insertedDataSaubStatCount), - Arg.Any()); + Assert.Equal(3, result.Count()); } diff --git a/AsbCloudInfrastructure/Services/DataSaubStatDrillingQualityService.cs b/AsbCloudInfrastructure/Services/DataSaubStatDrillingQualityService.cs index 41c4adee..a058a9c4 100644 --- a/AsbCloudInfrastructure/Services/DataSaubStatDrillingQualityService.cs +++ b/AsbCloudInfrastructure/Services/DataSaubStatDrillingQualityService.cs @@ -70,38 +70,40 @@ public class DataSaubStatDrillingQualityService : IDataSaubStatDrillingQualitySe { var idTelemetry = idTelemetries[i]; var lastDate = stats.FirstOrDefault(s => s.IdTelemetry == idTelemetry)?.DateEnd.ToUniversalTime() ?? DateTimeOffset.UnixEpoch; - var statsCount = await CreateStatDrillingQualityForTelemetry(idTelemetry, lastDate, token); + var result = await CreateStatDrillingQualityForTelemetry(idTelemetry, lastDate, token); + + var statsCount = result.Count(); + if (result.Any()) + statsCount = await dataSaubStatDrillingQualityRepository.InsertRangeAsync(result, token); + if (onProgressCallback != null) onProgressCallback($"Calculate stat for telemetry: {idTelemetry}; from {lastDate}; results count: {statsCount};", 1d * i / idTelemetries.Length); } } - private async Task CreateStatDrillingQualityForTelemetry( + public async Task> CreateStatDrillingQualityForTelemetry( int idTelemetry, DateTimeOffset geDate, CancellationToken token) { var leDate = DateTimeOffset.UtcNow; + var result = new List(); var dataSaub = await dataSaubService.Get(idTelemetry, true, geDate, leDate, 100_000, token); if (!dataSaub.Any()) - return 0; + return result; if (dataSaub is not TelemetryDataSaubDto[] dataSaubArray) dataSaubArray = dataSaub.ToArray(); - - var result = new List(); + foreach (var item in QualitySettingsForFeedRegulators) { var data = CreateDataSaubStatDrillingQuality(item.Key, item.Value, dataSaubArray); result.AddRange(data); } - if (result.Any()) - return await dataSaubStatDrillingQualityRepository.InsertRangeAsync(result, token); - - return 0; + return result; } private static IEnumerable CreateDataSaubStatDrillingQuality( @@ -120,29 +122,16 @@ public class DataSaubStatDrillingQualityService : IDataSaubStatDrillingQualitySe if (indexStart < 0) break; - indexEnd = indexStart + 1; - for (var i = indexStart + 1; i < dataSaub.Count(); i++) - { - if (dataSaub[i].WellDepth >= dataSaub[i - 1].WellDepth) - indexEnd = i; - else if (dataSaub[i].IdFeedRegulator != idFeedRegulator) - break; - else - break; - - } - - if (indexEnd < 0) - indexEnd = dataSaub.Length - 1; - - if (indexEnd == indexStart) - continue; + indexEnd = FindIndexEnd(indexStart, idFeedRegulator, dataSaub); var length = indexEnd - indexStart + 1; + if (length <= 2) + continue; + var subset = dataSaub.AsSpan(indexStart, length); - if (length <= 2 || (subset[^1].WellDepth - subset[0].WellDepth) < 0.15) + if ((subset[^1].WellDepth - subset[0].WellDepth) < 0.15) continue; // мелкие выборки не учитываем. @@ -153,6 +142,23 @@ public class DataSaubStatDrillingQualityService : IDataSaubStatDrillingQualitySe return result; } + private static int FindIndexEnd(int indexStart, int idFeedRegulator, TelemetryDataSaubDto[] dataSaub) + { + var indexEnd = indexStart + 1; + for (var i = indexStart + 1; i < dataSaub.Count(); i++) + { + if (dataSaub[i].WellDepth >= dataSaub[i - 1].WellDepth) + { + indexEnd = i; + } + else if (dataSaub[i].IdFeedRegulator != idFeedRegulator) + break; + else + break; + } + return indexEnd; + } + private static DataSaubStatDrillingQualityDto CalcStatsDrillingQuality( int idFeedRegulator, Span dataSaub,