Автотест 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.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
@ -10,7 +12,7 @@ namespace AsbCloudApp.Services;
public interface IDataSaubStatDrillingQualityService
{
/// <summary>
/// Расчет статистики DataSaubStat
/// Расчет статистики DataSaubStatDrillingQuality и сохранение её в бд
/// </summary>
/// <param name="lastDaysFilter">
/// Количество дней за которые должны были приходить данные, чтобы телеметрия попала в обработку.
@ -20,4 +22,16 @@ public interface IDataSaubStatDrillingQualityService
/// <returns></returns>
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>())
.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();
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,
@ -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>())
.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
await dataSaubStatService.CreateStatAsync(Gap, action, CancellationToken.None);
var result = await dataSaubStatService.CreateStatDrillingQualityForTelemetry(1, DateTimeOffset.UnixEpoch, CancellationToken.None);
//assert
await dataSaubStatRepositoryMock.Received().InsertRangeAsync(
Arg.Is<IEnumerable<DataSaubStatDrillingQualityDto>>(l => l.Count() == insertedDataSaubStatCount),
Arg.Any<CancellationToken>());
Assert.Equal(3, result.Count());
}

View File

@ -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<int> CreateStatDrillingQualityForTelemetry(
public async Task<IEnumerable<DataSaubStatDrillingQualityDto>> CreateStatDrillingQualityForTelemetry(
int idTelemetry,
DateTimeOffset geDate,
CancellationToken token)
{
var leDate = DateTimeOffset.UtcNow;
var result = new List<DataSaubStatDrillingQualityDto>();
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<DataSaubStatDrillingQualityDto>();
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<DataSaubStatDrillingQualityDto> 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<TelemetryDataSaubDto> dataSaub,