forked from ddrilling/AsbCloudServer
#20644813 ркфакторинг SubsystemOperationTimeService
#20369896 Добавлены новые свойства статистики по наработке подсистем
This commit is contained in:
parent
d2f6d468ab
commit
ee22408225
32
AsbCloudApp/Data/DetectedOperation/OperationsSummaryDto.cs
Normal file
32
AsbCloudApp/Data/DetectedOperation/OperationsSummaryDto.cs
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
namespace AsbCloudApp.Data.DetectedOperation;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Статистика по операциям
|
||||||
|
/// </summary>
|
||||||
|
public class OperationsSummaryDto
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Id телеметрии
|
||||||
|
/// </summary>
|
||||||
|
public int IdTelemetry { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Id названия/описания операции
|
||||||
|
/// </summary>
|
||||||
|
public int IdCategory { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Количество операций
|
||||||
|
/// </summary>
|
||||||
|
public int Count { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Cумма проходок операций
|
||||||
|
/// </summary>
|
||||||
|
public double SumDepthIntervals { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Cумма продолжительностей операций
|
||||||
|
/// </summary>
|
||||||
|
public double SumDurationHours { get; set; }
|
||||||
|
}
|
@ -23,13 +23,20 @@ namespace AsbCloudApp.Data.Subsystems
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public double KUsage { get; set; }
|
public double KUsage { get; set; }
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// сумма изменения глубин
|
/// сумма изменения глубин при включеной подсистеме
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public double SumDepthInterval { get; set; }
|
public double SumDepthInterval { get; set; }
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// количество операций
|
/// сумма проходок автоопределенных операций выполняемых подсистемой
|
||||||
|
/// </summary>
|
||||||
|
public double SumOperationDepthInterval { get; set; }
|
||||||
|
/// <summary>
|
||||||
|
/// сумма продолжительности автоопределенных операций выполняемых подсистемой
|
||||||
|
/// </summary>
|
||||||
|
public double SumOperationDurationHours { get; set; }
|
||||||
|
/// <summary>
|
||||||
|
/// количество включений подсистемы
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public int OperationCount { get; set; }
|
public int OperationCount { get; set; }
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
53
AsbCloudApp/Requests/DetectedOperationSummaryRequest.cs
Normal file
53
AsbCloudApp/Requests/DetectedOperationSummaryRequest.cs
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
|
||||||
|
namespace AsbCloudApp.Requests;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Запрос на получение обобщенных данных по операцим
|
||||||
|
/// </summary>
|
||||||
|
public class DetectedOperationSummaryRequest
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Список id телеметрий
|
||||||
|
/// пустой список - нет фильтрации
|
||||||
|
/// </summary>
|
||||||
|
public IEnumerable<int> IdsTelemetries { get;set;} = Enumerable.Empty<int>();
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Список id категорий операций
|
||||||
|
/// пустой список - нет фильтрации
|
||||||
|
/// </summary>
|
||||||
|
public IEnumerable<int> IdsOperationCategories { get; set; } = Enumerable.Empty<int>();
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Больше или равно даты начала операции
|
||||||
|
/// </summary>
|
||||||
|
public DateTimeOffset? GeDateStart {get;set;}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Меньше или равно даты начала операции
|
||||||
|
/// </summary>
|
||||||
|
public DateTimeOffset? LeDateStart { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Меньше или равно даты окончания операции
|
||||||
|
/// </summary>
|
||||||
|
public DateTimeOffset? LeDateEnd { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Больше или равно глубины начала операции
|
||||||
|
/// </summary>
|
||||||
|
public double? GeDepthStart { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Меньше или равно глубины начала операции
|
||||||
|
/// </summary>
|
||||||
|
public double? LeDepthStart { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Меньше или равно глубины окончания операции
|
||||||
|
/// </summary>
|
||||||
|
public double? LeDepthEnd { get; set; }
|
||||||
|
}
|
@ -26,7 +26,7 @@ namespace AsbCloudApp.Requests
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Больше или равно дате
|
/// Больше или равно дате
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public DateTime? GtDate { get; set; }
|
public DateTime? GtDate { get; set; }//TODO: its Ge*
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Меньше или равно дате
|
/// Меньше или равно дате
|
||||||
@ -43,6 +43,7 @@ namespace AsbCloudApp.Requests
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public double? LtDepth { get; set; }
|
public double? LtDepth { get; set; }
|
||||||
|
|
||||||
|
//TODO: Replace modes by DateTimeOffset LeDateStart, LeDateEnd
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// информация попадает в выборку, если интервал выборки частично или полностью пересекается с запрашиваемым интервалом
|
/// информация попадает в выборку, если интервал выборки частично или полностью пересекается с запрашиваемым интервалом
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -42,12 +42,10 @@ namespace AsbCloudApp.Services
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Получить интервалы глубин по всем скважинам
|
/// Получить интервалы глубин по всем скважинам
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="telemetryIds">список ИД телеметрий активных скважин</param>
|
/// <param name="request"></param>
|
||||||
/// <param name="gtDate"></param>
|
|
||||||
/// <param name="ltDate"></param>
|
|
||||||
/// <param name="token"></param>
|
/// <param name="token"></param>
|
||||||
/// <returns>кортеж - ид телеметрии, интервалы глубины забоя (ротор,слайд) </returns>
|
/// <returns>кортеж - ид телеметрии, интервалы глубины забоя (ротор,слайд) </returns>
|
||||||
Task<IEnumerable<(int idTelemetry,double depthIntervalRotor, double depthIntervalSlide)>> GetDepthIntervalAllOperationsAsync(IEnumerable<int?> telemetryIds,DateTimeOffset gtDate, DateTimeOffset ltDate, CancellationToken token);
|
Task<IEnumerable<OperationsSummaryDto>> GetOperationSummaryAsync(DetectedOperationSummaryRequest request, CancellationToken token);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Удалить операции
|
/// Удалить операции
|
||||||
|
@ -19,7 +19,7 @@ namespace AsbCloudApp.Services.Subsystems
|
|||||||
/// <param name="request"></param>
|
/// <param name="request"></param>
|
||||||
/// <param name="token"></param>
|
/// <param name="token"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
Task<IEnumerable<SubsystemStatDto>?> GetStatAsync(SubsystemOperationTimeRequest request, CancellationToken token);
|
Task<IEnumerable<SubsystemStatDto>> GetStatAsync(SubsystemOperationTimeRequest request, CancellationToken token);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Удаление наработки по подсистемам.
|
/// Удаление наработки по подсистемам.
|
||||||
@ -37,7 +37,7 @@ namespace AsbCloudApp.Services.Subsystems
|
|||||||
/// <param name="request"></param>
|
/// <param name="request"></param>
|
||||||
/// <param name="token"></param>
|
/// <param name="token"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
Task<IEnumerable<SubsystemOperationTimeDto>?> GetOperationTimeAsync(SubsystemOperationTimeRequest request, CancellationToken token);
|
Task<IEnumerable<SubsystemOperationTimeDto>> GetOperationTimeAsync(SubsystemOperationTimeRequest request, CancellationToken token);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Временной диапазон за который есть статистика работы подсистем
|
/// Временной диапазон за который есть статистика работы подсистем
|
||||||
|
@ -67,27 +67,58 @@ namespace AsbCloudInfrastructure.Services.DetectOperations
|
|||||||
return dtos;
|
return dtos;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<IEnumerable<(int idTelemetry, double depthIntervalRotor, double depthIntervalSlide)>> GetDepthIntervalAllOperationsAsync(IEnumerable<int?> telemetryIds, DateTimeOffset gtDate, DateTimeOffset ltDate, CancellationToken token)
|
public async Task<IEnumerable<OperationsSummaryDto>> GetOperationSummaryAsync(DetectedOperationSummaryRequest request, CancellationToken token)
|
||||||
{
|
{
|
||||||
var query = db.Set<DetectedOperation>()
|
var query = db.Set<DetectedOperation>()
|
||||||
.Include(o => o.OperationCategory)
|
.AsNoTracking();
|
||||||
.Where(o => o.DateStart >= gtDate)
|
|
||||||
.Where(o => o.DateEnd <= ltDate)
|
if (request.IdsTelemetries.Any())
|
||||||
.Where(o => telemetryIds.Contains(o.IdTelemetry))
|
query = query.Where(operation => request.IdsTelemetries.Contains(operation.IdTelemetry));
|
||||||
.GroupBy(g => g.IdTelemetry)
|
|
||||||
.Select(g => new
|
if (request.IdsOperationCategories.Any())
|
||||||
{
|
query = query.Where(operation => request.IdsOperationCategories.Contains(operation.IdCategory));
|
||||||
IdTelemetry = g.Key,
|
|
||||||
RotorDepthInterval = g.Where(o => o.IdCategory == WellOperationCategory.IdRotor).Sum(o => o.DepthEnd - o.DepthStart),
|
if (request.GeDateStart.HasValue)
|
||||||
SlideDepthInterval = g.Where(o => o.IdCategory == WellOperationCategory.IdSlide).Sum(o => o.DepthEnd - o.DepthStart)
|
{
|
||||||
});
|
var geDateStart = request.GeDateStart.Value.ToUniversalTime();
|
||||||
var data = await query.ToArrayAsync(token);
|
query = query.Where(operation => operation.DateStart >= geDateStart);
|
||||||
var result = data.Select(g =>
|
}
|
||||||
(
|
|
||||||
g.IdTelemetry,
|
if (request.LeDateStart.HasValue)
|
||||||
g.RotorDepthInterval,
|
{
|
||||||
g.SlideDepthInterval
|
var leDateStart = request.LeDateStart.Value.ToUniversalTime();
|
||||||
));
|
query = query.Where(operation => operation.DateStart <= leDateStart);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (request.LeDateEnd.HasValue)
|
||||||
|
{
|
||||||
|
var leDateEnd = request.LeDateEnd.Value.ToUniversalTime();
|
||||||
|
query = query.Where(operation => operation.DateEnd <= leDateEnd);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (request.GeDepthStart.HasValue)
|
||||||
|
query = query.Where(operation => operation.DepthStart >= request.GeDepthStart.Value);
|
||||||
|
|
||||||
|
if (request.LeDepthStart.HasValue)
|
||||||
|
query = query.Where(operation => operation.DepthStart <= request.LeDepthStart.Value);
|
||||||
|
|
||||||
|
if (request.LeDepthEnd.HasValue)
|
||||||
|
query = query.Where(operation => operation.DepthEnd <= request.LeDepthEnd.Value);
|
||||||
|
|
||||||
|
var queryGroup = query
|
||||||
|
.GroupBy(operation => new { operation.IdTelemetry, operation.IdCategory })
|
||||||
|
.Select(group => new OperationsSummaryDto
|
||||||
|
{
|
||||||
|
IdTelemetry = group.Key.IdTelemetry,
|
||||||
|
IdCategory = group.Key.IdCategory,
|
||||||
|
Count = group.Count(),
|
||||||
|
SumDepthIntervals = group.Sum(operation => operation.DepthEnd - operation.DepthStart),
|
||||||
|
SumDurationHours = group.Sum(operation => (operation.DateEnd - operation.DateStart).TotalHours)
|
||||||
|
})
|
||||||
|
.OrderBy(summ => summ.IdTelemetry)
|
||||||
|
.ThenBy(summ => summ.IdCategory);
|
||||||
|
|
||||||
|
var result = await queryGroup.ToArrayAsync(token);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -16,385 +16,379 @@ using System.Linq;
|
|||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace AsbCloudInfrastructure.Services.Subsystems
|
namespace AsbCloudInfrastructure.Services.Subsystems;
|
||||||
|
|
||||||
|
internal class SubsystemOperationTimeService : ISubsystemOperationTimeService
|
||||||
{
|
{
|
||||||
internal class SubsystemOperationTimeService : ISubsystemOperationTimeService
|
private readonly IAsbCloudDbContext db;
|
||||||
|
private readonly IWellService wellService;
|
||||||
|
private readonly ICrudRepository<SubsystemDto> subsystemService;
|
||||||
|
private readonly IDetectedOperationService detectedOperationService;
|
||||||
|
public const int IdSubsystemAKB = 1;
|
||||||
|
public const int IdSubsystemAKBRotor = 11;
|
||||||
|
public const int IdSubsystemAKBSlide = 12;
|
||||||
|
public const int IdSubsystemMSE = 2;
|
||||||
|
public const int IdSubsystemSpin = 65536;
|
||||||
|
public const int IdSubsystemTorque = 65537;
|
||||||
|
|
||||||
|
public SubsystemOperationTimeService(IAsbCloudDbContext db, IWellService wellService, ICrudRepository<SubsystemDto> subsystemService, IDetectedOperationService detectedOperationService)
|
||||||
{
|
{
|
||||||
private readonly IAsbCloudDbContext db;
|
this.db = db;
|
||||||
private readonly IWellService wellService;
|
this.wellService = wellService;
|
||||||
private readonly ICrudRepository<SubsystemDto> subsystemService;
|
this.subsystemService = subsystemService;
|
||||||
private readonly IDetectedOperationService detectedOperationService;
|
this.detectedOperationService = detectedOperationService;
|
||||||
public const int IdSubsystemAKB = 1;
|
}
|
||||||
public const int IdSubsystemAKBRotor = 11;
|
|
||||||
public const int IdSubsystemAKBSlide = 12;
|
/// <inheritdoc/>
|
||||||
public const int IdSubsystemMSE = 2;
|
public async Task<int> DeleteAsync(SubsystemOperationTimeRequest request, CancellationToken token)
|
||||||
public const int IdSubsystemSpin = 65536;
|
{
|
||||||
public const int IdSubsystemTorque = 65537;
|
var well = await wellService.GetOrDefaultAsync(request.IdWell, token)
|
||||||
public SubsystemOperationTimeService(IAsbCloudDbContext db, IWellService wellService, ICrudRepository<SubsystemDto> subsystemService, IDetectedOperationService detectedOperationService)
|
?? throw new ArgumentInvalidException(nameof(request.IdWell), $"Well Id: {request.IdWell} does not exist");
|
||||||
|
|
||||||
|
var query = BuildQuery(request, well);
|
||||||
|
db.SubsystemOperationTimes.RemoveRange(query);
|
||||||
|
return await db.SaveChangesAsync(token);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc/>
|
||||||
|
public async Task<IEnumerable<SubsystemOperationTimeDto>> GetOperationTimeAsync(SubsystemOperationTimeRequest request, CancellationToken token)
|
||||||
|
{
|
||||||
|
var well = await wellService.GetOrDefaultAsync(request.IdWell, token)
|
||||||
|
?? throw new ArgumentInvalidException(nameof(request.IdWell), $"Well Id: {request.IdWell} does not exist");
|
||||||
|
|
||||||
|
var dtos = await GetOperationTimeAsync(request, well, token);
|
||||||
|
return dtos;
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task<IEnumerable<SubsystemOperationTimeDto>> GetOperationTimeAsync(SubsystemOperationTimeRequest request, WellDto well, CancellationToken token)
|
||||||
|
{
|
||||||
|
var query = BuildQuery(request, well);
|
||||||
|
IEnumerable<SubsystemOperationTime> data = await query.ToListAsync(token);
|
||||||
|
|
||||||
|
if (request.SelectMode == SubsystemOperationTimeRequest.SelectModeInner)
|
||||||
{
|
{
|
||||||
this.db = db;
|
if (request.GtDate is not null)
|
||||||
this.wellService = wellService;
|
data = data.Where(o => o.DateStart >= request.GtDate.Value);
|
||||||
this.subsystemService = subsystemService;
|
|
||||||
this.detectedOperationService = detectedOperationService;
|
if (request.LtDate is not null)
|
||||||
|
data = data.Where(o => o.DateEnd <= request.LtDate.Value);
|
||||||
|
}
|
||||||
|
else if (request.SelectMode == SubsystemOperationTimeRequest.SelectModeTrim)
|
||||||
|
{
|
||||||
|
var begin = request.GtDate?.ToUtcDateTimeOffset(well.Timezone.Hours);
|
||||||
|
var end = request.LtDate?.ToUtcDateTimeOffset(well.Timezone.Hours);
|
||||||
|
data = TrimOperation(data, begin, end);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc/>
|
var dtos = data.Select(o => Convert(o, well.Timezone.Hours));
|
||||||
public async Task<int> DeleteAsync(SubsystemOperationTimeRequest request, CancellationToken token)
|
return dtos;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc/>
|
||||||
|
public async Task<IEnumerable<SubsystemStatDto>> GetStatAsync(SubsystemOperationTimeRequest request, CancellationToken token)
|
||||||
|
{
|
||||||
|
var well = await wellService.GetOrDefaultAsync(request.IdWell, token)
|
||||||
|
?? throw new ArgumentInvalidException(nameof(request.IdWell), $"Well Id: {request.IdWell} does not exist");
|
||||||
|
|
||||||
|
request.SelectMode = SubsystemOperationTimeRequest.SelectModeTrim;
|
||||||
|
var subsystemsTimes = await GetOperationTimeAsync(request, well, token);
|
||||||
|
if (subsystemsTimes is null)
|
||||||
|
return Enumerable.Empty<SubsystemStatDto>();
|
||||||
|
|
||||||
|
var detectedOperationSummaryRequest = new DetectedOperationSummaryRequest()
|
||||||
{
|
{
|
||||||
var well = await wellService.GetOrDefaultAsync(request.IdWell, token);
|
IdsTelemetries = new[] {well.IdTelemetry!.Value},
|
||||||
if (well?.IdTelemetry is null || well.Timezone is null)
|
IdsOperationCategories = WellOperationCategory.MechanicalDrillingSubIds,
|
||||||
return 0;
|
|
||||||
var query = BuildQuery(request);
|
|
||||||
if (query is null)
|
|
||||||
return 0;
|
|
||||||
db.SubsystemOperationTimes.RemoveRange(query);
|
|
||||||
return await db.SaveChangesAsync(token);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <inheritdoc/>
|
GeDateStart = request.GtDate,
|
||||||
public async Task<IEnumerable<SubsystemOperationTimeDto>?> GetOperationTimeAsync(SubsystemOperationTimeRequest request, CancellationToken token)
|
LeDateStart = request.LtDate,
|
||||||
|
|
||||||
|
GeDepthStart = request.GtDepth,
|
||||||
|
LeDepthStart = request.LtDepth,
|
||||||
|
};
|
||||||
|
var operationsSummaries = await detectedOperationService.GetOperationSummaryAsync(detectedOperationSummaryRequest, token);
|
||||||
|
if(!operationsSummaries.Any())
|
||||||
|
return Enumerable.Empty<SubsystemStatDto>();
|
||||||
|
|
||||||
|
var statList = CalcStat(subsystemsTimes, operationsSummaries);
|
||||||
|
return statList;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static IEnumerable<SubsystemOperationTime> TrimOperation(IEnumerable<SubsystemOperationTime> data, DateTimeOffset? gtDate, DateTimeOffset? ltDate)
|
||||||
|
{
|
||||||
|
if (!ltDate.HasValue && !gtDate.HasValue)
|
||||||
|
return data.Select(d => d.Adapt<SubsystemOperationTime>());
|
||||||
|
|
||||||
|
var items = data.Select((item) =>
|
||||||
{
|
{
|
||||||
var well = await wellService.GetOrDefaultAsync(request.IdWell, token);
|
var operationTime = item.Adapt<SubsystemOperationTime>();
|
||||||
if (well?.IdTelemetry is null || well.Timezone is null)
|
if (!(item.DepthStart.HasValue && item.DepthEnd.HasValue))
|
||||||
return null;
|
|
||||||
|
|
||||||
var query = BuildQuery(request);
|
|
||||||
|
|
||||||
if (query is null)
|
|
||||||
return null;
|
|
||||||
|
|
||||||
IEnumerable<SubsystemOperationTime> data = await query.ToListAsync(token);
|
|
||||||
|
|
||||||
if (request.SelectMode == SubsystemOperationTimeRequest.SelectModeInner)
|
|
||||||
{
|
|
||||||
if (request.GtDate is not null)
|
|
||||||
data = data.Where(o => o.DateStart >= request.GtDate.Value);
|
|
||||||
|
|
||||||
if (request.LtDate is not null)
|
|
||||||
data = data.Where(o => o.DateEnd <= request.LtDate.Value);
|
|
||||||
}
|
|
||||||
else if (request.SelectMode == SubsystemOperationTimeRequest.SelectModeTrim)
|
|
||||||
{
|
|
||||||
var begin = request.GtDate?.ToUtcDateTimeOffset(well.Timezone.Hours);
|
|
||||||
var end = request.LtDate?.ToUtcDateTimeOffset(well.Timezone.Hours);
|
|
||||||
data = TrimOperation(data, begin, end);
|
|
||||||
}
|
|
||||||
|
|
||||||
var dtos = data.Select(o => Convert(o, well.Timezone.Hours));
|
|
||||||
return dtos;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <inheritdoc/>
|
|
||||||
public async Task<IEnumerable<SubsystemStatDto>?> GetStatAsync(SubsystemOperationTimeRequest request, CancellationToken token)
|
|
||||||
{
|
|
||||||
request.SelectMode = SubsystemOperationTimeRequest.SelectModeTrim;
|
|
||||||
var data = await GetOperationTimeAsync(request, token);
|
|
||||||
if (data is null)
|
|
||||||
return null;
|
|
||||||
|
|
||||||
var detectedOperationsRequest = new DetectedOperationRequest()
|
|
||||||
{
|
|
||||||
IdWell = request.IdWell,
|
|
||||||
IdsCategories = new int[] {
|
|
||||||
WellOperationCategory.IdRotor, WellOperationCategory.IdSlide,
|
|
||||||
},
|
|
||||||
LtDate = request.LtDate,
|
|
||||||
GtDate = request.GtDate,
|
|
||||||
};
|
|
||||||
var detectedOperations = await detectedOperationService.GetOperationsAsync(detectedOperationsRequest, token);
|
|
||||||
if(detectedOperations?.Any() != true)
|
|
||||||
return null;
|
|
||||||
var depthInterval = GetDepthInterval(detectedOperations);
|
|
||||||
|
|
||||||
var statList = CalcStat(data,depthInterval);
|
|
||||||
return statList;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static IEnumerable<SubsystemOperationTime> TrimOperation(IEnumerable<SubsystemOperationTime> data, DateTimeOffset? gtDate, DateTimeOffset? ltDate)
|
|
||||||
{
|
|
||||||
if (!ltDate.HasValue && !gtDate.HasValue)
|
|
||||||
return data.Select(d => d.Adapt<SubsystemOperationTime>());
|
|
||||||
|
|
||||||
var items = data.Select((item) =>
|
|
||||||
{
|
|
||||||
var operationTime = item.Adapt<SubsystemOperationTime>();
|
|
||||||
if (!(item.DepthStart.HasValue && item.DepthEnd.HasValue))
|
|
||||||
return operationTime;
|
|
||||||
|
|
||||||
var dateDiff = (item.DateEnd - item.DateStart).TotalSeconds;
|
|
||||||
var depthDiff = item.DepthEnd.Value - item.DepthStart.Value;
|
|
||||||
var a = depthDiff / dateDiff;
|
|
||||||
var b = item.DepthStart.Value;
|
|
||||||
|
|
||||||
if (gtDate.HasValue && item.DateStart < gtDate.Value)
|
|
||||||
{
|
|
||||||
operationTime.DateStart = gtDate.Value;
|
|
||||||
var x = (gtDate.Value - item.DateStart).TotalSeconds;
|
|
||||||
operationTime.DepthStart = (float)(a * x + b);
|
|
||||||
}
|
|
||||||
if (ltDate.HasValue && item.DateEnd > ltDate.Value)
|
|
||||||
{
|
|
||||||
operationTime.DateEnd = ltDate.Value;
|
|
||||||
var x = (ltDate.Value - item.DateStart).TotalSeconds;
|
|
||||||
operationTime.DepthEnd = (float)(a * x + b);
|
|
||||||
}
|
|
||||||
return operationTime;
|
return operationTime;
|
||||||
});
|
|
||||||
|
|
||||||
return items;
|
var dateDiff = (item.DateEnd - item.DateStart).TotalSeconds;
|
||||||
|
var depthDiff = item.DepthEnd.Value - item.DepthStart.Value;
|
||||||
|
var a = depthDiff / dateDiff;
|
||||||
|
var b = item.DepthStart.Value;
|
||||||
|
|
||||||
|
if (gtDate.HasValue && item.DateStart < gtDate.Value)
|
||||||
|
{
|
||||||
|
operationTime.DateStart = gtDate.Value;
|
||||||
|
var x = (gtDate.Value - item.DateStart).TotalSeconds;
|
||||||
|
operationTime.DepthStart = (float)(a * x + b);
|
||||||
|
}
|
||||||
|
if (ltDate.HasValue && item.DateEnd > ltDate.Value)
|
||||||
|
{
|
||||||
|
operationTime.DateEnd = ltDate.Value;
|
||||||
|
var x = (ltDate.Value - item.DateStart).TotalSeconds;
|
||||||
|
operationTime.DepthEnd = (float)(a * x + b);
|
||||||
|
}
|
||||||
|
return operationTime;
|
||||||
|
});
|
||||||
|
|
||||||
|
return items;
|
||||||
|
}
|
||||||
|
|
||||||
|
private IEnumerable<SubsystemStatDto> CalcStat(
|
||||||
|
IEnumerable<SubsystemOperationTimeDto> subsystemsTimes,
|
||||||
|
IEnumerable<OperationsSummaryDto> operationsSummaries)
|
||||||
|
{
|
||||||
|
var groupedSubsystemsTimes = subsystemsTimes
|
||||||
|
.OrderBy(o => o.Id)
|
||||||
|
.GroupBy(o => o.IdSubsystem);
|
||||||
|
|
||||||
|
var periodGroupTotal = subsystemsTimes.Sum(o => (o.DateEnd - o.DateStart).TotalHours);
|
||||||
|
|
||||||
|
var result = groupedSubsystemsTimes.Select(g =>
|
||||||
|
{
|
||||||
|
var periodGroup = g.Sum(o => (o.DateEnd - o.DateStart).TotalHours);
|
||||||
|
var periodGroupDepth = g.Sum(o => o.DepthEnd - o.DepthStart);
|
||||||
|
var (sumOprationsDepth, sumOprationsDurationHours) = AggregateOperationsSummaries(g.Key, operationsSummaries);
|
||||||
|
var subsystemStat = new SubsystemStatDto()
|
||||||
|
{
|
||||||
|
IdSubsystem = g.Key,
|
||||||
|
SubsystemName = subsystemService.GetOrDefault(g.Key)?.Name ?? "unknown",
|
||||||
|
UsedTimeHours = periodGroup,
|
||||||
|
SumOperationDepthInterval = sumOprationsDepth,
|
||||||
|
SumOperationDurationHours = sumOprationsDurationHours,
|
||||||
|
SumDepthInterval = periodGroupDepth,
|
||||||
|
KUsage = periodGroupDepth / sumOprationsDepth,
|
||||||
|
OperationCount = g.Count(),
|
||||||
|
};
|
||||||
|
if (subsystemStat.KUsage > 1)
|
||||||
|
subsystemStat.KUsage = 1;
|
||||||
|
return subsystemStat;
|
||||||
|
});
|
||||||
|
|
||||||
|
var apdParts = result.Where(x => x.IdSubsystem == 11 || x.IdSubsystem == 12);
|
||||||
|
if (apdParts.Any())
|
||||||
|
{
|
||||||
|
var apdSum = new SubsystemStatDto()
|
||||||
|
{
|
||||||
|
IdSubsystem = IdSubsystemAKB,
|
||||||
|
SubsystemName = "АПД",
|
||||||
|
UsedTimeHours = apdParts.Sum(part => part.UsedTimeHours),
|
||||||
|
SumOperationDepthInterval = apdParts.Sum(part => part.SumOperationDepthInterval),
|
||||||
|
SumOperationDurationHours = apdParts.Sum(part => part.SumOperationDurationHours),
|
||||||
|
SumDepthInterval = apdParts.Sum(part => part.SumDepthInterval),
|
||||||
|
OperationCount = apdParts.Sum(part => part.OperationCount),
|
||||||
|
};
|
||||||
|
apdSum.KUsage = apdSum.SumDepthInterval / apdSum.SumOperationDepthInterval;
|
||||||
|
if (apdSum.KUsage > 1)
|
||||||
|
apdSum.KUsage = 1;
|
||||||
|
result = result.Append(apdSum).OrderBy(m => m.IdSubsystem);
|
||||||
}
|
}
|
||||||
|
|
||||||
private IEnumerable<SubsystemStatDto> CalcStat(
|
return result;
|
||||||
IEnumerable<SubsystemOperationTimeDto> dtos,
|
}
|
||||||
(double depthIntervalRotor, double depthIntervalSlide) depthInterval)
|
|
||||||
{
|
|
||||||
var groupedDataSubsystems = dtos
|
|
||||||
.OrderBy(o => o.Id)
|
|
||||||
.GroupBy(o => o.IdSubsystem);
|
|
||||||
var periodGroupTotal = dtos.Sum(o => (o.DateEnd - o.DateStart).TotalHours);
|
|
||||||
|
|
||||||
var result = groupedDataSubsystems.Select(g =>
|
private static (double SumDepth, double SumDurationHours) AggregateOperationsSummaries(int idSubsystem, IEnumerable<OperationsSummaryDto> operationsSummaries)
|
||||||
|
=> idSubsystem switch
|
||||||
|
{
|
||||||
|
IdSubsystemAKBRotor or IdSubsystemTorque => CalcOperationSummariesByCategories(operationsSummaries, WellOperationCategory.IdRotor),
|
||||||
|
IdSubsystemAKBSlide or IdSubsystemSpin => CalcOperationSummariesByCategories(operationsSummaries, WellOperationCategory.IdSlide),
|
||||||
|
IdSubsystemAKB or IdSubsystemMSE => CalcOperationSummariesByCategories(operationsSummaries, WellOperationCategory.IdRotor, WellOperationCategory.IdSlide),
|
||||||
|
_ => throw new ArgumentException($"idSubsystem: {idSubsystem} does not supported in this method", nameof(idSubsystem)),
|
||||||
|
};
|
||||||
|
|
||||||
|
private static (double SumDepth, double SumDurationHours) CalcOperationSummariesByCategories(
|
||||||
|
IEnumerable<OperationsSummaryDto> operationsSummaries,
|
||||||
|
params int[] idsOperationCategories)
|
||||||
|
{
|
||||||
|
var filtered = operationsSummaries.Where(sum => idsOperationCategories.Contains(sum.IdCategory));
|
||||||
|
var sumDepth = filtered.Sum(summ => summ.SumDepthIntervals);
|
||||||
|
var sumDurationHours = filtered.Sum(summ => summ.SumDurationHours);
|
||||||
|
return (sumDepth, sumDurationHours);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc/>
|
||||||
|
public async Task<IEnumerable<SubsystemActiveWellStatDto>> GetStatByActiveWells(int idCompany, DateTime? gtDate, DateTime? ltDate, CancellationToken token)
|
||||||
|
{
|
||||||
|
var activeWells = await wellService.GetAsync(new() { IdCompany = idCompany, IdState = 1 }, token);
|
||||||
|
var result = await GetStatAsync(activeWells, gtDate, ltDate, token);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc/>
|
||||||
|
public async Task<IEnumerable<SubsystemActiveWellStatDto>> GetStatByActiveWells(IEnumerable<int> wellIds, CancellationToken token)
|
||||||
|
{
|
||||||
|
var activeWells = await wellService.GetAsync(new() { Ids = wellIds, IdState = 1 }, token);
|
||||||
|
var result = await GetStatAsync(activeWells, null, null, token);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task<IEnumerable<SubsystemActiveWellStatDto>> GetStatAsync(IEnumerable<WellDto> wells, DateTime? gtDate, DateTime? ltDate, CancellationToken token)
|
||||||
|
{
|
||||||
|
if (!wells.Any())
|
||||||
|
return Enumerable.Empty<SubsystemActiveWellStatDto>();
|
||||||
|
|
||||||
|
var hoursOffset = wells
|
||||||
|
.FirstOrDefault(well => well.Timezone is not null)
|
||||||
|
?.Timezone.Hours
|
||||||
|
?? 5d;
|
||||||
|
|
||||||
|
var beginUTC = gtDate.HasValue
|
||||||
|
? gtDate.Value.ToUtcDateTimeOffset(hoursOffset)
|
||||||
|
: db.SubsystemOperationTimes.Min(s => s.DateStart)
|
||||||
|
.DateTime
|
||||||
|
.ToUtcDateTimeOffset(hoursOffset);
|
||||||
|
|
||||||
|
var endUTC = ltDate.HasValue
|
||||||
|
? ltDate.Value.ToUtcDateTimeOffset(hoursOffset)
|
||||||
|
: db.SubsystemOperationTimes.Max(s => s.DateEnd)
|
||||||
|
.DateTime
|
||||||
|
.ToUtcDateTimeOffset(hoursOffset);
|
||||||
|
|
||||||
|
IEnumerable<int> idsTelemetries = wells
|
||||||
|
.Where(w => w.IdTelemetry is not null)
|
||||||
|
.Select(w => w.IdTelemetry!.Value)
|
||||||
|
.Distinct();
|
||||||
|
|
||||||
|
var query = db.SubsystemOperationTimes
|
||||||
|
.Where(o => idsTelemetries.Contains(o.IdTelemetry) &&
|
||||||
|
o.DateStart >= beginUTC &&
|
||||||
|
o.DateEnd <= endUTC)
|
||||||
|
.AsNoTracking();
|
||||||
|
|
||||||
|
var subsystemsOperationTime = await query.ToArrayAsync(token);
|
||||||
|
|
||||||
|
var operationSummaries = await detectedOperationService
|
||||||
|
.GetOperationSummaryAsync(new ()
|
||||||
{
|
{
|
||||||
var depthIntervalSubsystem = GetDepthIntervalSubsystem(g.Key, depthInterval);
|
IdsTelemetries = idsTelemetries,
|
||||||
var periodGroup = g.Sum(o => (o.DateEnd - o.DateStart).TotalHours);
|
IdsOperationCategories = WellOperationCategory.MechanicalDrillingSubIds,
|
||||||
var periodGroupDepth = g.Sum(o => o.DepthEnd - o.DepthStart);
|
GeDateStart = beginUTC,
|
||||||
var subsystemStat = new SubsystemStatDto()
|
LeDateEnd = endUTC,
|
||||||
|
}, token);
|
||||||
|
|
||||||
|
var result = wells
|
||||||
|
.Select(well => {
|
||||||
|
var dtos = subsystemsOperationTime
|
||||||
|
.Where(s => s.IdTelemetry == well.IdTelemetry)
|
||||||
|
.Select(s => Convert(s, well.Timezone.Hours));
|
||||||
|
|
||||||
|
var wellStat = new SubsystemActiveWellStatDto{ Well = well };
|
||||||
|
|
||||||
|
var telemetryOperationSummaries = operationSummaries.Where(summ => summ.IdTelemetry == well.IdTelemetry);
|
||||||
|
if (telemetryOperationSummaries.Any())
|
||||||
{
|
{
|
||||||
IdSubsystem = g.Key,
|
var subsystemStat = CalcStat(dtos, telemetryOperationSummaries);
|
||||||
SubsystemName = subsystemService.GetOrDefault(g.Key)?.Name ?? "unknown",
|
if (subsystemStat.Any())
|
||||||
UsedTimeHours = periodGroup,
|
|
||||||
//% использования = суммарная проходка АПД в слайде
|
|
||||||
/// суммарную проходку автоопределенных операций в слайде.
|
|
||||||
KUsage = periodGroupDepth / depthIntervalSubsystem,
|
|
||||||
SumDepthInterval = periodGroupDepth,
|
|
||||||
OperationCount = g.Count(),
|
|
||||||
};
|
|
||||||
if (subsystemStat.KUsage > 1)
|
|
||||||
subsystemStat.KUsage = 1;
|
|
||||||
return subsystemStat;
|
|
||||||
});
|
|
||||||
|
|
||||||
var apdParts = result.Where(x => x.IdSubsystem == 11 || x.IdSubsystem == 12);
|
|
||||||
if (apdParts.Any())
|
|
||||||
{
|
|
||||||
var apdSum = new SubsystemStatDto()
|
|
||||||
{
|
|
||||||
IdSubsystem = IdSubsystemAKB,
|
|
||||||
SubsystemName = "АПД",
|
|
||||||
UsedTimeHours = apdParts.Sum(part => part.UsedTimeHours),
|
|
||||||
KUsage = apdParts.Sum(part => part.SumDepthInterval) / GetDepthIntervalSubsystem(IdSubsystemAKB, depthInterval),
|
|
||||||
SumDepthInterval = apdParts.Sum(part => part.SumDepthInterval),
|
|
||||||
OperationCount = apdParts.Sum(part => part.OperationCount),
|
|
||||||
};
|
|
||||||
result = result.Append(apdSum).OrderBy(m => m.IdSubsystem);
|
|
||||||
}
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static (double depthIntervalRotor, double depthIntervalSlide) GetDepthInterval (IEnumerable<DetectedOperationDto> detectedOperations)
|
|
||||||
{
|
|
||||||
var depthIntervalRotor = detectedOperations.Where(o => o.IdCategory == WellOperationCategory.IdRotor)
|
|
||||||
.Sum(o => o.DepthEnd - o.DepthStart);
|
|
||||||
var depthIntervalSlide = detectedOperations.Where(o => o.IdCategory == WellOperationCategory.IdSlide)
|
|
||||||
.Sum(o => o.DepthEnd - o.DepthStart);
|
|
||||||
var depthInterval = (depthIntervalRotor, depthIntervalSlide);
|
|
||||||
|
|
||||||
return depthInterval;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static double GetDepthIntervalSubsystem(int idSubsystem, (double depthIntervalRotor, double depthIntervalSlide) depthInterval)
|
|
||||||
{
|
|
||||||
var depthIntervalSubsystem = 0d;
|
|
||||||
//AKB - MSE
|
|
||||||
if (idSubsystem == IdSubsystemAKB || idSubsystem == IdSubsystemMSE)
|
|
||||||
{
|
|
||||||
depthIntervalSubsystem = depthInterval.depthIntervalRotor + depthInterval.depthIntervalSlide;
|
|
||||||
}
|
|
||||||
//AKB - Rotor
|
|
||||||
if (idSubsystem == IdSubsystemAKBRotor)
|
|
||||||
{
|
|
||||||
depthIntervalSubsystem = depthInterval.depthIntervalRotor;
|
|
||||||
}
|
|
||||||
//AKB - Slide
|
|
||||||
if (idSubsystem == IdSubsystemAKBSlide)
|
|
||||||
{
|
|
||||||
depthIntervalSubsystem = depthInterval.depthIntervalSlide;
|
|
||||||
}
|
|
||||||
//Spin
|
|
||||||
if (idSubsystem == IdSubsystemSpin)
|
|
||||||
{
|
|
||||||
depthIntervalSubsystem = depthInterval.depthIntervalSlide;
|
|
||||||
}
|
|
||||||
//Torque
|
|
||||||
if (idSubsystem == IdSubsystemTorque)
|
|
||||||
{
|
|
||||||
depthIntervalSubsystem = depthInterval.depthIntervalRotor;
|
|
||||||
}
|
|
||||||
return depthIntervalSubsystem;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <inheritdoc/>
|
|
||||||
public async Task<IEnumerable<SubsystemActiveWellStatDto>> GetStatByActiveWells(int idCompany, DateTime? gtDate, DateTime? ltDate, CancellationToken token)
|
|
||||||
{
|
|
||||||
var activeWells = await wellService.GetAsync(new() { IdCompany = idCompany, IdState = 1 }, token);
|
|
||||||
var result = await GetStatAsync(activeWells, gtDate, ltDate, token);
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <inheritdoc/>
|
|
||||||
public async Task<IEnumerable<SubsystemActiveWellStatDto>> GetStatByActiveWells(IEnumerable<int> wellIds, CancellationToken token)
|
|
||||||
{
|
|
||||||
var activeWells = await wellService.GetAsync(new() { Ids = wellIds, IdState = 1 }, token);
|
|
||||||
var result = await GetStatAsync(activeWells, null, null, token);
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
private async Task<IEnumerable<SubsystemActiveWellStatDto>> GetStatAsync(IEnumerable<WellDto> wells, DateTime? gtDate, DateTime? ltDate, CancellationToken token)
|
|
||||||
{
|
|
||||||
if (!wells.Any())
|
|
||||||
return Enumerable.Empty<SubsystemActiveWellStatDto>();
|
|
||||||
|
|
||||||
var hoursOffset = wells
|
|
||||||
.FirstOrDefault(well => well.Timezone is not null)
|
|
||||||
?.Timezone.Hours
|
|
||||||
?? 5d;
|
|
||||||
|
|
||||||
var beginUTC = gtDate.HasValue
|
|
||||||
? gtDate.Value.ToUtcDateTimeOffset(hoursOffset)
|
|
||||||
: db.SubsystemOperationTimes.Min(s => s.DateStart)
|
|
||||||
.DateTime
|
|
||||||
.ToUtcDateTimeOffset(hoursOffset);
|
|
||||||
|
|
||||||
var endUTC = ltDate.HasValue
|
|
||||||
? ltDate.Value.ToUtcDateTimeOffset(hoursOffset)
|
|
||||||
: db.SubsystemOperationTimes.Max(s => s.DateEnd)
|
|
||||||
.DateTime
|
|
||||||
.ToUtcDateTimeOffset(hoursOffset);
|
|
||||||
|
|
||||||
var telemetryIds = wells
|
|
||||||
.Where(w => w.IdTelemetry is not null)
|
|
||||||
.Select(w => w.IdTelemetry)
|
|
||||||
.Distinct();
|
|
||||||
|
|
||||||
var query = db.SubsystemOperationTimes
|
|
||||||
.Where(o => telemetryIds.Contains(o.IdTelemetry) &&
|
|
||||||
o.DateStart >= beginUTC &&
|
|
||||||
o.DateEnd <= endUTC)
|
|
||||||
.AsNoTracking();
|
|
||||||
|
|
||||||
var subsystemsOperationTime = await query.ToListAsync(token);
|
|
||||||
|
|
||||||
var depthIntervals = await detectedOperationService
|
|
||||||
.GetDepthIntervalAllOperationsAsync(telemetryIds, beginUTC, endUTC, token);
|
|
||||||
|
|
||||||
var result = wells
|
|
||||||
.Select(well => {
|
|
||||||
var dtos = subsystemsOperationTime
|
|
||||||
.Where(s => s.IdTelemetry == well.IdTelemetry)
|
|
||||||
.Select(s => Convert(s, well.Timezone.Hours));
|
|
||||||
|
|
||||||
var (idTelemetry, depthIntervalRotor, depthIntervalSlide) = depthIntervals
|
|
||||||
.FirstOrDefault(i => i.idTelemetry == well.IdTelemetry);
|
|
||||||
|
|
||||||
var subsystemStat = idTelemetry > 0 && dtos.Any()
|
|
||||||
? CalcStat(dtos, (depthIntervalRotor, depthIntervalSlide))
|
|
||||||
: Enumerable.Empty<SubsystemStatDto>();
|
|
||||||
|
|
||||||
return new SubsystemActiveWellStatDto
|
|
||||||
{
|
{
|
||||||
Well = well,
|
wellStat.SubsystemAKB = subsystemStat.FirstOrDefault(s => s.IdSubsystem == IdSubsystemAKB);
|
||||||
SubsystemAKB = subsystemStat.FirstOrDefault(s => s.IdSubsystem == IdSubsystemAKB),
|
wellStat.SubsystemMSE = subsystemStat.FirstOrDefault(s => s.IdSubsystem == IdSubsystemMSE);
|
||||||
SubsystemMSE = subsystemStat.FirstOrDefault(s => s.IdSubsystem == IdSubsystemMSE),
|
wellStat.SubsystemSpinMaster = subsystemStat.FirstOrDefault(s => s.IdSubsystem == IdSubsystemSpin);
|
||||||
SubsystemSpinMaster = subsystemStat.FirstOrDefault(s => s.IdSubsystem == IdSubsystemSpin),
|
wellStat.SubsystemTorqueMaster = subsystemStat.FirstOrDefault(s => s.IdSubsystem == IdSubsystemTorque);
|
||||||
SubsystemTorqueMaster = subsystemStat.FirstOrDefault(s => s.IdSubsystem == IdSubsystemTorque),
|
}
|
||||||
};
|
}
|
||||||
});
|
|
||||||
|
|
||||||
return result;
|
return wellStat;
|
||||||
}
|
});
|
||||||
|
|
||||||
/// <inheritdoc/>
|
return result;
|
||||||
public async Task<DatesRangeDto?> GetDateRangeOperationTimeAsync(SubsystemOperationTimeRequest request, CancellationToken token)
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc/>
|
||||||
|
public async Task<DatesRangeDto?> GetDateRangeOperationTimeAsync(SubsystemOperationTimeRequest request, CancellationToken token)
|
||||||
|
{
|
||||||
|
var well = await wellService.GetOrDefaultAsync(request.IdWell, token)
|
||||||
|
?? throw new ArgumentInvalidException(nameof(request.IdWell), $"Well Id: {request.IdWell} does not exist");
|
||||||
|
|
||||||
|
var query = BuildQuery(request, well);
|
||||||
|
if (query is null)
|
||||||
{
|
{
|
||||||
var query = BuildQuery(request);
|
return null;
|
||||||
if (query is null)
|
|
||||||
{
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
var result = await query
|
|
||||||
.GroupBy(o => o.IdTelemetry)
|
|
||||||
.Select(g => new DatesRangeDto
|
|
||||||
{
|
|
||||||
From = g.Min(o => o.DateStart).DateTime,
|
|
||||||
To = g.Max(o => o.DateEnd).DateTime
|
|
||||||
})
|
|
||||||
.FirstOrDefaultAsync(token);
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
var result = await query
|
||||||
|
.GroupBy(o => o.IdTelemetry)
|
||||||
|
.Select(g => new DatesRangeDto
|
||||||
|
{
|
||||||
|
From = g.Min(o => o.DateStart).DateTime,
|
||||||
|
To = g.Max(o => o.DateEnd).DateTime
|
||||||
|
})
|
||||||
|
.FirstOrDefaultAsync(token);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
private IQueryable<SubsystemOperationTime> BuildQuery(SubsystemOperationTimeRequest request)
|
private IQueryable<SubsystemOperationTime> BuildQuery(SubsystemOperationTimeRequest request, WellDto well)
|
||||||
|
{
|
||||||
|
var idTelemetry = well.IdTelemetry
|
||||||
|
?? throw new ArgumentInvalidException(nameof(request.IdWell), $"Well Id: {request.IdWell} has no telemetry");
|
||||||
|
|
||||||
|
var query = db.SubsystemOperationTimes
|
||||||
|
.Include(o => o.Subsystem)
|
||||||
|
.Where(o => o.IdTelemetry == idTelemetry)
|
||||||
|
.AsNoTracking();
|
||||||
|
|
||||||
|
if (request.IdsSubsystems.Any())
|
||||||
|
query = query.Where(o => request.IdsSubsystems.Contains(o.IdSubsystem));
|
||||||
|
|
||||||
|
// # Dates range condition
|
||||||
|
// [GtDate LtDate]
|
||||||
|
// [DateStart DateEnd] [DateStart DateEnd]
|
||||||
|
if (request.GtDate.HasValue)
|
||||||
{
|
{
|
||||||
var well = wellService.GetOrDefault(request.IdWell)
|
DateTimeOffset gtDate = request.GtDate.Value.ToUtcDateTimeOffset(well.Timezone.Hours);
|
||||||
?? throw new ArgumentInvalidException(nameof(request.IdWell), $"Not valid IdWell = {request.IdWell}");
|
query = query.Where(o => o.DateEnd >= gtDate);
|
||||||
|
|
||||||
var query = db.SubsystemOperationTimes
|
|
||||||
.Include(o => o.Subsystem)
|
|
||||||
.Where(o => o.IdTelemetry == well.IdTelemetry)
|
|
||||||
.AsNoTracking();
|
|
||||||
|
|
||||||
if (request.IdsSubsystems.Any())
|
|
||||||
query = query.Where(o => request.IdsSubsystems.Contains(o.IdSubsystem));
|
|
||||||
|
|
||||||
// # Dates range condition
|
|
||||||
// [GtDate LtDate]
|
|
||||||
// [DateStart DateEnd] [DateStart DateEnd]
|
|
||||||
if (request.GtDate.HasValue)
|
|
||||||
{
|
|
||||||
DateTimeOffset gtDate = request.GtDate.Value.ToUtcDateTimeOffset(well.Timezone.Hours);
|
|
||||||
query = query.Where(o => o.DateEnd >= gtDate);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (request.LtDate.HasValue)
|
|
||||||
{
|
|
||||||
DateTimeOffset ltDate = request.LtDate.Value.ToUtcDateTimeOffset(well.Timezone.Hours);
|
|
||||||
query = query.Where(o => o.DateStart <= ltDate);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (request.GtDepth.HasValue)
|
|
||||||
query = query.Where(o => o.DepthEnd >= request.GtDepth.Value);
|
|
||||||
|
|
||||||
if (request.LtDepth.HasValue)
|
|
||||||
query = query.Where(o => o.DepthStart <= request.LtDepth.Value);
|
|
||||||
|
|
||||||
if (request?.SortFields?.Any() == true)
|
|
||||||
{
|
|
||||||
query = query.SortBy(request.SortFields);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
query = query
|
|
||||||
.OrderBy(o => o.DateStart)
|
|
||||||
.ThenBy(o => o.DepthStart);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (request?.Skip > 0)
|
|
||||||
query = query.Skip((int)request.Skip);
|
|
||||||
|
|
||||||
if (request?.Take > 0)
|
|
||||||
query = query.Take((int)request.Take);
|
|
||||||
|
|
||||||
return query;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static SubsystemOperationTimeDto Convert(SubsystemOperationTime operationTime, double? timezoneHours = null)
|
if (request.LtDate.HasValue)
|
||||||
{
|
{
|
||||||
var dto = operationTime.Adapt<SubsystemOperationTimeDto>();
|
DateTimeOffset ltDate = request.LtDate.Value.ToUtcDateTimeOffset(well.Timezone.Hours);
|
||||||
var hours = timezoneHours ?? operationTime.Telemetry.TimeZone.Hours;
|
query = query.Where(o => o.DateStart <= ltDate);
|
||||||
dto.DateStart = operationTime.DateStart.ToRemoteDateTime(hours);
|
|
||||||
dto.DateEnd = operationTime.DateEnd.ToRemoteDateTime(hours);
|
|
||||||
return dto;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (request.GtDepth.HasValue)
|
||||||
|
query = query.Where(o => o.DepthEnd >= request.GtDepth.Value);
|
||||||
|
|
||||||
|
if (request.LtDepth.HasValue)
|
||||||
|
query = query.Where(o => o.DepthStart <= request.LtDepth.Value);
|
||||||
|
|
||||||
|
if (request?.SortFields?.Any() == true)
|
||||||
|
{
|
||||||
|
query = query.SortBy(request.SortFields);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
query = query
|
||||||
|
.OrderBy(o => o.DateStart)
|
||||||
|
.ThenBy(o => o.DepthStart);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (request?.Skip > 0)
|
||||||
|
query = query.Skip((int)request.Skip);
|
||||||
|
|
||||||
|
if (request?.Take > 0)
|
||||||
|
query = query.Take((int)request.Take);
|
||||||
|
|
||||||
|
return query;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static SubsystemOperationTimeDto Convert(SubsystemOperationTime operationTime, double? timezoneHours = null)
|
||||||
|
{
|
||||||
|
var dto = operationTime.Adapt<SubsystemOperationTimeDto>();
|
||||||
|
var hours = timezoneHours ?? operationTime.Telemetry.TimeZone.Hours;
|
||||||
|
dto.DateStart = operationTime.DateStart.ToRemoteDateTime(hours);
|
||||||
|
dto.DateEnd = operationTime.DateEnd.ToRemoteDateTime(hours);
|
||||||
|
return dto;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user