Доработки суточного рапорта

Сделана оптимизация получения фиктивного рапорта. Получение диапозона дат операций по скважине вынес в репозиторий. Избавился от получения всего списка фактический операций.
This commit is contained in:
Степанов Дмитрий 2023-12-11 10:58:03 +05:00
parent 9a7649d765
commit 24232d4f36
4 changed files with 89 additions and 46 deletions

View File

@ -105,5 +105,14 @@ namespace AsbCloudApp.Repositories
/// <param name="token"></param>
/// <returns></returns>
Task<IEnumerable<SectionByOperationsDto>> GetSectionsAsync(IEnumerable<int> idsWells, CancellationToken token);
}
/// <summary>
/// Получить диапазон дат выполнения операций
/// </summary>
/// <param name="idWell"></param>
/// <param name="idType"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
Task<DatesRangeDto?> GetDatesRangeAsync(int idWell, int idType, CancellationToken cancellationToken);
}
}

View File

@ -184,6 +184,20 @@ public class WellOperationRepository : IWellOperationRepository
return sections;
}
public async Task<DatesRangeDto?> GetDatesRangeAsync(int idWell, int idType, CancellationToken cancellationToken)
{
var query = db.WellOperations.Where(o => o.IdWell == idWell && o.IdType == idType);
if (!await query.AnyAsync(cancellationToken))
return null;
return new DatesRangeDto
{
From = (await query.MinAsync(o => o.DateStart, cancellationToken)).Date,
To = (await query.MaxAsync(o => o.DateStart, cancellationToken)).Date
};
}
/// <inheritdoc/>
public DateTimeOffset? FirstOperationDate(int idWell)
{

View File

@ -19,7 +19,6 @@ using AsbCloudApp.Services.ProcessMaps.WellDrilling;
using AsbCloudApp.Services.Subsystems;
using AsbCloudDb.Model;
using Mapster;
using AsbCloudApp.Data.Trajectory;
namespace AsbCloudInfrastructure.Services.DailyReport;
@ -108,7 +107,15 @@ public class DailyReportService : IDailyReportService
IdWell = well.Id
};
var factWellOperations = (await GetFactWellOperationsAsync(idWell, dailyReport.Date, cancellationToken))
var factOperationRequest = new WellOperationRequest
{
IdWell = idWell,
OperationType = WellOperation.IdOperationTypeFact,
GeDate = dateDailyReport,
LtDate = dateDailyReport.AddHours(24)
};
var factWellOperations = (await wellOperationRepository.GetAsync(factOperationRequest, cancellationToken))
.OrderBy(o => o.DateStart)
.ThenBy(o => o.DepthStart);
@ -149,10 +156,7 @@ public class DailyReportService : IDailyReportService
return result;
var dailyReports = new List<DailyReportDto>();
var existingDailyReports = await dailyReportRepository.GetAsync(idWell, request, cancellationToken);
var factWellOperations = await GetFactWellOperationsAsync(idWell, null, cancellationToken);
if (request.GeDate.HasValue)
{
var startDate = new DateTime(request.GeDate.Value.Year, request.GeDate.Value.Month,
@ -173,8 +177,22 @@ public class DailyReportService : IDailyReportService
if (datesRange.From.AddDays(result.Skip) <= datesRange.To)
result.Count = (int)(Math.Ceiling((datesRange.To - DateTime.UnixEpoch).TotalDays) -
Math.Floor((datesRange.From - DateTime.UnixEpoch).TotalDays));
Math.Floor((datesRange.From - DateTime.UnixEpoch).TotalDays)) + 1;
var existingDailyReports = await dailyReportRepository.GetAsync(idWell, request, cancellationToken);
var geDateFactWellOperation = datesRange.From.AddDays(result.Skip);
var ltDateFactWellOperation = geDateFactWellOperation.AddDays(result.Take);
var factWellOperationRequest = new WellOperationRequest
{
IdWell = idWell,
OperationType = WellOperation.IdOperationTypeFact,
GeDate = geDateFactWellOperation,
LtDate = ltDateFactWellOperation
};
var factWellOperations = await wellOperationRepository.GetAsync(factWellOperationRequest, cancellationToken);
if (request.SortFields?.Contains("DateStart desc") == true)
{
@ -208,7 +226,9 @@ public class DailyReportService : IDailyReportService
IdWell = idWell
};
AddFactWellOperationBlock(dailyReport, factWellOperations.Where(o => o.DateStart >= date && o.DateStart <= date.AddDays(1)));
var factWellOperationPerDay = factWellOperations.Where(o => o.DateStart.Date >= date && o.DateStart.Date <= date.AddDays(1));
AddFactWellOperationBlock(dailyReport, factWellOperationPerDay);
dailyReports.Add(dailyReport);
}
@ -216,18 +236,16 @@ public class DailyReportService : IDailyReportService
public async Task<DatesRangeDto?> GetDatesRangeAsync(int idWell, CancellationToken cancellationToken)
{
var factOperations = await GetFactWellOperationsAsync(idWell, null, cancellationToken);
var factOperationDatesRange = await wellOperationRepository.GetDatesRangeAsync(idWell, WellOperation.IdOperationTypeFact,
cancellationToken);
if (!factOperations.Any())
if (factOperationDatesRange is null)
return null;
var minDateStart = factOperations.Min(o => o.DateStart).Date;
var maxDateStart = factOperations.Max(o => o.DateStart).Date;
return new DatesRangeDto
{
From = minDateStart.AddDays(1) <= DateTime.UtcNow ? minDateStart : DateTime.UtcNow.Date.AddDays(-1),
To = maxDateStart.AddDays(1) <= DateTime.UtcNow ? maxDateStart : DateTime.UtcNow.Date.AddDays(-1)
From = factOperationDatesRange.From.AddDays(1) <= DateTime.UtcNow ? factOperationDatesRange.From : DateTime.UtcNow.Date.AddDays(-1),
To = factOperationDatesRange.To.AddDays(1) <= DateTime.UtcNow ? factOperationDatesRange.To : DateTime.UtcNow.Date.AddDays(-1)
};
}
@ -358,15 +376,6 @@ public class DailyReportService : IDailyReportService
};
}
private Task<IEnumerable<WellOperationDto>> GetFactWellOperationsAsync(int idWell, DateTime? dateDailyReport, CancellationToken cancellationToken) =>
wellOperationRepository.GetAsync(new WellOperationRequest
{
IdWell = idWell,
OperationType = WellOperation.IdOperationTypeFact,
GeDate = dateDailyReport,
LtDate = dateDailyReport?.AddHours(24)
}, cancellationToken);
private async Task<bool> IsDateDailyReportInRangeAsync(int idWell, DateTime dateDailyReport, CancellationToken cancellationToken)
{
var datesRange = await GetDatesRangeAsync(idWell, cancellationToken);

View File

@ -212,6 +212,7 @@ public class DailyReportServiceTest
private readonly DailyReportDto fakeDailyReport;
private readonly WellDto fakeWell;
private readonly DatesRangeDto fakeDatesRange;
public DailyReportServiceTest()
{
@ -233,6 +234,12 @@ public class DailyReportServiceTest
Companies = new[] { fakeCustomer, fakeContractor }
};
fakeDatesRange = new DatesRangeDto
{
From = fakeFirstFactWellOperation.DateStart,
To = fakeLastFactWellOperation.DateStart
};
dailyReportService = new DailyReportService(wellServiceMock,
trajectoryFactNnbRepositoryMock,
dailyReportRepositoryMock,
@ -260,6 +267,9 @@ public class DailyReportServiceTest
wellOperationRepositoryMock.GetAsync(Arg.Any<WellOperationRequest>(), Arg.Any<CancellationToken>())
.ReturnsForAnyArgs(new[] { fakeFirstFactWellOperation, fakeLastFactWellOperation });
wellOperationRepositoryMock.GetDatesRangeAsync(Arg.Any<int>(), Arg.Any<int>(), Arg.Any<CancellationToken>())
.ReturnsForAnyArgs(fakeDatesRange);
wellOperationRepositoryMock.GetSectionTypes()
.ReturnsForAnyArgs(new[] { fakeSectionType });
@ -490,20 +500,23 @@ public class DailyReportServiceTest
[Fact]
public async Task GetAsync_ShouldReturn_FictiveDailyReport()
{
//arrange
var expectedCount = (fakeLastFactWellOperation.DateStart - fakeFirstFactWellOperation.DateStart).TotalDays + 1;
//act
var result = await dailyReportService.GetAsync(idWell, new FileReportRequest(), CancellationToken.None);
//assert
Assert.True((fakeLastFactWellOperation.DateStart - fakeFirstFactWellOperation.DateStart).Days == result.Count);
Assert.Equal(expectedCount, result.Count);
}
[Theory]
[MemberData(nameof(FactWellOperationDates))]
public async Task GetDatesRangeAsync_ShouldReturn_DateRangeByFactWellOperations(IEnumerable<DateTime> factWellOperationDates)
[MemberData(nameof(FactWellOperationDatesRange))]
public async Task GetDatesRangeAsync_ShouldReturn_DateRangeByFactWellOperations(DatesRangeDto datesRange)
{
//arrange
wellOperationRepositoryMock.GetAsync(Arg.Any<WellOperationRequest>(), CancellationToken.None)
.ReturnsForAnyArgs(factWellOperationDates.Select(dateStart => new WellOperationDto { DateStart = dateStart }));
wellOperationRepositoryMock.GetDatesRangeAsync(Arg.Any<int>(), Arg.Any<int>(), Arg.Any<CancellationToken>())
.Returns(datesRange);
//act
var result = await dailyReportService.GetDatesRangeAsync(idWell, CancellationToken.None);
@ -514,43 +527,41 @@ public class DailyReportServiceTest
Assert.True(result.To < DateTime.UtcNow.Date);
}
public static IEnumerable<object[]> FactWellOperationDates()
public static IEnumerable<object[]> FactWellOperationDatesRange()
{
yield return new object[]
{
new[]
new DatesRangeDto
{
new DateTime(2023, 11, 1),
new DateTime(2023, 11, 9),
DateTime.UtcNow
From = new DateTime(2023, 11, 1),
To = new DateTime(2023, 11, 9)
}
};
yield return new object[]
{
new[]
new DatesRangeDto
{
new DateTime(2023, 11, 1),
new DateTime(2023, 11, 1)
From = new DateTime(2023, 11, 1),
To = new DateTime(2023, 11, 1)
}
};
yield return new object[]
{
new[]
new DatesRangeDto
{
DateTime.UtcNow,
DateTime.UtcNow
From = DateTime.UtcNow,
To = DateTime.UtcNow
}
};
yield return new object[]
{
new[]
new DatesRangeDto
{
new DateTime(2023, 11, 1),
new DateTime(2023, 11, 9),
new DateTime(2023, 11, 11)
From = new DateTime(2023, 11, 1),
To = new DateTime(2023, 11, 11)
}
};
}