Автотест 1 и рефакторинг, нужный для автотеста

This commit is contained in:
Olga Nemt 2024-10-09 13:31:36 +05:00
parent 7872e068ba
commit 0ef3b5a30d
3 changed files with 81 additions and 78 deletions

View File

@ -1,4 +1,6 @@
using AsbCloudApp.Data;
using System; using System;
using System.Collections.Generic;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -10,7 +12,7 @@ namespace AsbCloudApp.Services;
public interface IDataSaubStatDrillingQualityService public interface IDataSaubStatDrillingQualityService
{ {
/// <summary> /// <summary>
/// Расчет статистики DataSaubStat /// Расчет статистики DataSaubStatDrillingQuality и сохранение её в бд
/// </summary> /// </summary>
/// <param name="lastDaysFilter"> /// <param name="lastDaysFilter">
/// Количество дней за которые должны были приходить данные, чтобы телеметрия попала в обработку. /// Количество дней за которые должны были приходить данные, чтобы телеметрия попала в обработку.
@ -20,4 +22,16 @@ public interface IDataSaubStatDrillingQualityService
/// <returns></returns> /// <returns></returns>
Task CreateStatAsync(int lastDaysFilter, Action<string, double?> onProgressCallback, CancellationToken token); Task CreateStatAsync(int lastDaysFilter, Action<string, double?> onProgressCallback, CancellationToken token);
/// <summary>
/// Расчет статистики DataSaubStatDrillingQuality
/// </summary>
/// <param name="idTelemetry"></param>
/// <param name="geDate"></param>
/// <param name="token"></param>
/// <returns></returns>
Task<IEnumerable<DataSaubStatDrillingQualityDto>> CreateStatDrillingQualityForTelemetry(
int idTelemetry,
DateTimeOffset geDate,
CancellationToken token);
} }

View File

@ -117,6 +117,31 @@ public class DataSaubStatDtillingQualityServiceTest
.GetLastsAsync(Arg.Any<int[]>(), Arg.Any<CancellationToken>()) .GetLastsAsync(Arg.Any<int[]>(), Arg.Any<CancellationToken>())
.Returns(dataSaubStatDrillingQualityDtos); .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<TelemetryDataSaubDto>();
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(); var telemetrySaubDto = telemetryDataSaubDtos.FirstOrDefault();
if (telemetrySaubDto != null) if (telemetrySaubDto != null)
{ {
@ -131,7 +156,8 @@ public class DataSaubStatDtillingQualityServiceTest
//1 1500 //1 1500
//2 1600 //2 1600
//Îćčäŕĺěűĺ đĺçóëüňŕňű äë˙ idFeedRegulator: //Îćčäŕĺěűĺ đĺçóëüňŕňű äë˙ idFeedRegulator:
// depthWell = (1000 - 800) + (1300 - 900) + (1600 - 1100) = // êîëè÷åñòâî âîçâðàùàåìûõ çàïèñåé = 3
// îáùàÿ ãëóáèíà = (1000 - 800) + (1300 - 900) + (1600 - 1100) = 1100
var telemetrySaubDto1 = CreateTelemetryDataSaubItem( var telemetrySaubDto1 = CreateTelemetryDataSaubItem(
telemetrySaubDto, telemetrySaubDto,
wellDepth: 900, wellDepth: 900,
@ -141,10 +167,10 @@ public class DataSaubStatDtillingQualityServiceTest
telemetryDataSaubDtos.Add(telemetrySaubDto1); telemetryDataSaubDtos.Add(telemetrySaubDto1);
var telemetrySaubDto2 = CreateTelemetryDataSaubItem( var telemetrySaubDto2 = CreateTelemetryDataSaubItem(
telemetrySaubDto, telemetrySaubDto,
wellDepth: 1000, wellDepth: 1000,
bitDepth: 1000, bitDepth: 1000,
idFeedRegulator: 1, idFeedRegulator: 1,
dateTime: DateTime.UtcNow.AddMinutes(20)); dateTime: DateTime.UtcNow.AddMinutes(20));
telemetryDataSaubDtos.Add(telemetrySaubDto2); telemetryDataSaubDtos.Add(telemetrySaubDto2);
@ -201,54 +227,11 @@ public class DataSaubStatDtillingQualityServiceTest
.Get(Arg.Any<int>(), Arg.Any<bool>(), Arg.Any<DateTimeOffset>(), Arg.Any<DateTimeOffset>(), Arg.Any<int>(), Arg.Any<CancellationToken>()) .Get(Arg.Any<int>(), Arg.Any<bool>(), Arg.Any<DateTimeOffset>(), Arg.Any<DateTimeOffset>(), Arg.Any<int>(), Arg.Any<CancellationToken>())
.Returns(telemetryDataSaubDtos); .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<TelemetryDataSaubDto>();
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<IEnumerable<DataSaubStatDrillingQualityDto>>(), Arg.Any<CancellationToken>())
.Returns(insertedDataSaubStatCount);
Action<string, double?> action = (message, percent) =>
{
//assert
Assert.NotNull(percent);
Assert.InRange(percent.Value, 0.0, 1.0);
};
//act //act
await dataSaubStatService.CreateStatAsync(Gap, action, CancellationToken.None); var result = await dataSaubStatService.CreateStatDrillingQualityForTelemetry(1, DateTimeOffset.UnixEpoch, CancellationToken.None);
//assert //assert
await dataSaubStatRepositoryMock.Received().InsertRangeAsync( Assert.Equal(3, result.Count());
Arg.Is<IEnumerable<DataSaubStatDrillingQualityDto>>(l => l.Count() == insertedDataSaubStatCount),
Arg.Any<CancellationToken>());
} }

View File

@ -70,38 +70,40 @@ public class DataSaubStatDrillingQualityService : IDataSaubStatDrillingQualitySe
{ {
var idTelemetry = idTelemetries[i]; var idTelemetry = idTelemetries[i];
var lastDate = stats.FirstOrDefault(s => s.IdTelemetry == idTelemetry)?.DateEnd.ToUniversalTime() ?? DateTimeOffset.UnixEpoch; 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) if (onProgressCallback != null)
onProgressCallback($"Calculate stat for telemetry: {idTelemetry}; from {lastDate}; results count: {statsCount};", 1d * i / idTelemetries.Length); onProgressCallback($"Calculate stat for telemetry: {idTelemetry}; from {lastDate}; results count: {statsCount};", 1d * i / idTelemetries.Length);
} }
} }
private async Task<int> CreateStatDrillingQualityForTelemetry( public async Task<IEnumerable<DataSaubStatDrillingQualityDto>> CreateStatDrillingQualityForTelemetry(
int idTelemetry, int idTelemetry,
DateTimeOffset geDate, DateTimeOffset geDate,
CancellationToken token) CancellationToken token)
{ {
var leDate = DateTimeOffset.UtcNow; var leDate = DateTimeOffset.UtcNow;
var result = new List<DataSaubStatDrillingQualityDto>();
var dataSaub = await dataSaubService.Get(idTelemetry, true, geDate, leDate, 100_000, token); var dataSaub = await dataSaubService.Get(idTelemetry, true, geDate, leDate, 100_000, token);
if (!dataSaub.Any()) if (!dataSaub.Any())
return 0; return result;
if (dataSaub is not TelemetryDataSaubDto[] dataSaubArray) if (dataSaub is not TelemetryDataSaubDto[] dataSaubArray)
dataSaubArray = dataSaub.ToArray(); dataSaubArray = dataSaub.ToArray();
var result = new List<DataSaubStatDrillingQualityDto>();
foreach (var item in QualitySettingsForFeedRegulators) foreach (var item in QualitySettingsForFeedRegulators)
{ {
var data = CreateDataSaubStatDrillingQuality(item.Key, item.Value, dataSaubArray); var data = CreateDataSaubStatDrillingQuality(item.Key, item.Value, dataSaubArray);
result.AddRange(data); result.AddRange(data);
} }
if (result.Any()) return result;
return await dataSaubStatDrillingQualityRepository.InsertRangeAsync(result, token);
return 0;
} }
private static IEnumerable<DataSaubStatDrillingQualityDto> CreateDataSaubStatDrillingQuality( private static IEnumerable<DataSaubStatDrillingQualityDto> CreateDataSaubStatDrillingQuality(
@ -120,29 +122,16 @@ public class DataSaubStatDrillingQualityService : IDataSaubStatDrillingQualitySe
if (indexStart < 0) if (indexStart < 0)
break; break;
indexEnd = indexStart + 1; indexEnd = FindIndexEnd(indexStart, idFeedRegulator, dataSaub);
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;
var length = indexEnd - indexStart + 1; var length = indexEnd - indexStart + 1;
if (length <= 2)
continue;
var subset = dataSaub.AsSpan(indexStart, length); 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; // мелкие выборки не учитываем. continue; // мелкие выборки не учитываем.
@ -153,6 +142,23 @@ public class DataSaubStatDrillingQualityService : IDataSaubStatDrillingQualitySe
return result; 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( private static DataSaubStatDrillingQualityDto CalcStatsDrillingQuality(
int idFeedRegulator, int idFeedRegulator,
Span<TelemetryDataSaubDto> dataSaub, Span<TelemetryDataSaubDto> dataSaub,