#15287262 Сделал OperationStatRequest.DurationMinutes* double

This commit is contained in:
Frolov-Nikita 2023-10-04 09:15:28 +05:00
parent 2b400012c6
commit 6eb118e9f6
No known key found for this signature in database
GPG Key ID: 719E3386D12B0760
2 changed files with 130 additions and 133 deletions

View File

@ -5,9 +5,8 @@ namespace AsbCloudApp.Requests
/// <summary> /// <summary>
/// Параметры фильтра операции /// Параметры фильтра операции
/// </summary> /// </summary>
public class OperationStatRequest : RequestBase public class OperationStatRequest
{ {
/// <summary> /// <summary>
/// Дата начала операции в UTC /// Дата начала операции в UTC
/// </summary> /// </summary>
@ -18,17 +17,14 @@ namespace AsbCloudApp.Requests
/// </summary> /// </summary>
public DateTime? DateEndUTC { get; set; } public DateTime? DateEndUTC { get; set; }
/// <summary> /// <summary>
/// Минимальная продолжительность операции, мин /// Минимальная продолжительность операции, мин
/// </summary> /// </summary>
public int? DurationMinutesMin { get; set; } public double? DurationMinutesMin { get; set; }
/// <summary> /// <summary>
/// Максимальная продолжительность операции, мин /// Максимальная продолжительность операции, мин
/// </summary> /// </summary>
public int? DurationMinutesMax { get; set; } public double? DurationMinutesMax { get; set; }
} }
} }

View File

@ -9,154 +9,155 @@ using System.Linq;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace AsbCloudInfrastructure.Services namespace AsbCloudInfrastructure.Services;
public class SlipsStatService : ISlipsStatService
{ {
public class SlipsStatService : ISlipsStatService private readonly IAsbCloudDbContext db;
public SlipsStatService(IAsbCloudDbContext db)
{ {
private readonly IAsbCloudDbContext db; this.db = db;
public SlipsStatService(IAsbCloudDbContext db) }
public async Task<IEnumerable<SlipsStatDto>> GetAllAsync(OperationStatRequest request, CancellationToken token)
{
if (request.DateStartUTC.HasValue)
request.DateStartUTC = DateTime.SpecifyKind(request.DateStartUTC.Value, DateTimeKind.Utc);
if (request.DateEndUTC.HasValue)
request.DateEndUTC = DateTime.SpecifyKind(request.DateEndUTC.Value, DateTimeKind.Utc);
var schedulesQuery = db.Schedule
.Include(s => s.Well)
.Include(s => s.Driller)
.AsNoTracking();
if (request.DateStartUTC.HasValue && request.DateEndUTC.HasValue)
schedulesQuery = schedulesQuery.
Where(s => s.DrillStart >= request.DateStartUTC && s.DrillEnd <= request.DateEndUTC);
var schedules = await schedulesQuery.ToArrayAsync(token);
var wells = schedules
.Select(d => d.Well)
.Where(well => well.IdTelemetry != null)
.GroupBy(w => w.Id)
.ToDictionary(g => g.Key, g => g.First().IdTelemetry!.Value);
var idsWells = wells.Keys;
var idsTelemetries = wells.Values;
var telemetries = wells.ToDictionary(wt => wt.Value, wt => wt.Key);
var factWellOperationsQuery = db.WellOperations
.Where(o => idsWells.Contains(o.IdWell))
.Where(o => o.IdType == 1)
.Where(o => WellOperationCategory.MechanicalDrillingSubIds.Contains(o.IdCategory))
.Include(o => o.WellSectionType)
.AsNoTracking();
if (request.DateStartUTC.HasValue && request.DateEndUTC.HasValue)
factWellOperationsQuery = factWellOperationsQuery
.Where(o => o.DateStart.AddHours(o.DurationHours) > request.DateStartUTC && o.DateStart < request.DateEndUTC);
var factWellOperations = await factWellOperationsQuery.ToArrayAsync(token);
var sections = factWellOperations
.GroupBy(o => new { o.IdWell, o.IdWellSectionType })
.Select(g => new
{
g.Key.IdWell,
g.Key.IdWellSectionType,
DepthStart = g.Min(o => o.DepthStart),
DepthEnd = g.Max(o => o.DepthEnd),
g.First().WellSectionType.Caption
});
var detectedOperationsQuery = db.DetectedOperations
.Where(o => idsTelemetries.Contains(o.IdTelemetry))
.Where(o => o.IdCategory == WellOperationCategory.IdSlipsTime)
.AsNoTracking();
if (request.DateStartUTC.HasValue && request.DateEndUTC.HasValue)
detectedOperationsQuery = detectedOperationsQuery
.Where(o => o.DateStart < request.DateEndUTC)
.Where(o => o.DateEnd > request.DateStartUTC);
if (request.DurationMinutesMin.HasValue)
{ {
this.db = db; var durationMinutesMin = TimeSpan.FromMinutes(request.DurationMinutesMin.Value);
detectedOperationsQuery = detectedOperationsQuery
.Where(o => o.DateEnd - o.DateStart >= durationMinutesMin);
} }
public async Task<IEnumerable<SlipsStatDto>> GetAllAsync(OperationStatRequest request, CancellationToken token) if (request.DurationMinutesMax.HasValue)
{ {
if (request.DateStartUTC.HasValue) var durationMinutesMax = TimeSpan.FromMinutes(request.DurationMinutesMax.Value);
request.DateStartUTC = DateTime.SpecifyKind(request.DateStartUTC.Value, DateTimeKind.Utc); detectedOperationsQuery = detectedOperationsQuery
.Where(o => o.DateEnd - o.DateStart <= durationMinutesMax);
}
if (request.DateEndUTC.HasValue) var detectedOperations = await detectedOperationsQuery
request.DateEndUTC = DateTime.SpecifyKind(request.DateEndUTC.Value, DateTimeKind.Utc); .ToArrayAsync(token);
var schedulesQuery = db.Schedule var detectedOperationsGroupedByDrillerAndSection = detectedOperations.Select(o => new
.Include(s => s.Well) {
.Include(s => s.Driller) Operation = o,
.AsNoTracking(); IdWell = telemetries[o.IdTelemetry],
schedules.FirstOrDefault(s =>
if (request.DateStartUTC.HasValue && request.DateEndUTC.HasValue) s.IdWell == telemetries[o.IdTelemetry]
schedulesQuery = schedulesQuery. && s.DrillStart <= o.DateStart
Where(s => s.DrillStart >= request.DateStartUTC && s.DrillEnd <= request.DateEndUTC); && s.DrillEnd >= o.DateStart
&& new TimeDto(s.ShiftStart) <= new TimeDto(o.DateStart.DateTime)
var schedules = await schedulesQuery.ToArrayAsync(token); && new TimeDto(s.ShiftEnd) >= new TimeDto(o.DateStart.DateTime))
?.Driller,
var wells = schedules Section = sections.FirstOrDefault(s =>
.Select(d => d.Well) s.IdWell == telemetries[o.IdTelemetry]
.Where(well => well.IdTelemetry != null) && s.DepthStart <= o.DepthStart
.GroupBy(w => w.Id) && s.DepthEnd >= o.DepthStart)
.ToDictionary(g => g.Key, g => g.First().IdTelemetry!.Value); })
.Where(o => o.Driller != null)
var idsWells = wells.Keys; .Where(o => o.Section != null)
var idsTelemetries = wells.Values; .Select(o => new
var telemetries = wells.ToDictionary(wt => wt.Value, wt => wt.Key);
var factWellOperationsQuery = db.WellOperations
.Where(o => idsWells.Contains(o.IdWell))
.Where(o => o.IdType == 1)
.Where(o => WellOperationCategory.MechanicalDrillingSubIds.Contains(o.IdCategory))
.Include(o => o.WellSectionType)
.AsNoTracking();
if (request.DateStartUTC.HasValue && request.DateEndUTC.HasValue)
factWellOperationsQuery = factWellOperationsQuery
.Where(o => o.DateStart.AddHours(o.DurationHours) > request.DateStartUTC && o.DateStart < request.DateEndUTC);
var factWellOperations = await factWellOperationsQuery.ToArrayAsync(token);
var sections = factWellOperations
.GroupBy(o => new { o.IdWell, o.IdWellSectionType })
.Select(g => new
{
g.Key.IdWell,
g.Key.IdWellSectionType,
DepthStart = g.Min(o => o.DepthStart),
DepthEnd = g.Max(o => o.DepthEnd),
g.First().WellSectionType.Caption
});
var detectedOperationsQuery = db.DetectedOperations
.Where(o => idsTelemetries.Contains(o.IdTelemetry))
.Where(o => o.IdCategory == WellOperationCategory.IdSlipsTime)
.AsNoTracking();
if (request.DateStartUTC.HasValue && request.DateEndUTC.HasValue)
detectedOperationsQuery = detectedOperationsQuery
.Where(o => o.DateStart < request.DateEndUTC)
.Where(o => o.DateEnd > request.DateStartUTC);
if (request.DurationMinutesMin.HasValue)
{ {
var durationMinutesMin = new TimeSpan(0, request.DurationMinutesMin.Value, 0); o.Operation,
detectedOperationsQuery = detectedOperationsQuery o.IdWell,
.Where(o => o.DateEnd - o.DateStart >= durationMinutesMin); Driller = o.Driller!,
} Section = o.Section!
if (request.DurationMinutesMax.HasValue) })
{ .GroupBy(o => new { o.Driller.Id, o.Section.IdWellSectionType });
var durationMinutesMax = new TimeSpan(0, request.DurationMinutesMax.Value, 0);
detectedOperationsQuery = detectedOperationsQuery
.Where(o => o.DateEnd - o.DateStart <= durationMinutesMax);
}
var detectedOperations = await detectedOperationsQuery
.ToArrayAsync(token);
var detectedOperationsGroupedByDrillerAndSection = detectedOperations.Select(o => new var factWellOperationsGroupedByDrillerAndSection = factWellOperations
.Select(o => new
{ {
Operation = o, Operation = o,
IdWell = telemetries[o.IdTelemetry],
schedules.FirstOrDefault(s => schedules.FirstOrDefault(s =>
s.IdWell == telemetries[o.IdTelemetry] s.IdWell == o.IdWell
&& s.DrillStart <= o.DateStart && s.DrillStart <= o.DateStart
&& s.DrillEnd >= o.DateStart && s.DrillEnd >= o.DateStart
&& new TimeDto(s.ShiftStart) <= new TimeDto(o.DateStart.DateTime) && new TimeDto(s.ShiftStart) <= new TimeDto(o.DateStart.DateTime)
&& new TimeDto(s.ShiftEnd) >= new TimeDto(o.DateStart.DateTime)) && new TimeDto(s.ShiftEnd) >= new TimeDto(o.DateStart.DateTime))
?.Driller, ?.Driller,
Section = sections.FirstOrDefault(s =>
s.IdWell == telemetries[o.IdTelemetry]
&& s.DepthStart <= o.DepthStart
&& s.DepthEnd >= o.DepthStart)
}) })
.Where(o => o.Driller != null) .Where(o => o.Driller != null)
.Where(o => o.Section != null) .GroupBy(o => new { o.Driller!.Id, o.Operation.IdWellSectionType });
.Select(o => new
{
o.Operation,
o.IdWell,
Driller = o.Driller!,
Section = o.Section!
})
.GroupBy(o => new { o.Driller.Id, o.Section.IdWellSectionType });
var factWellOperationsGroupedByDrillerAndSection = factWellOperations var stats = detectedOperationsGroupedByDrillerAndSection.Select(group => new SlipsStatDto
.Select(o => new {
{ DrillerName = $"{group.First().Driller!.Name} {group.First().Driller!.Patronymic} {group.First().Driller!.Surname}",
Operation = o, SlipsCount = group.Count(),
schedules.FirstOrDefault(s => SlipsTimeInMinutes = group
s.IdWell == o.IdWell .Sum(y => (y.Operation.DateEnd - y.Operation.DateStart).TotalMinutes),
&& s.DrillStart <= o.DateStart SectionDepth = factWellOperationsGroupedByDrillerAndSection
&& s.DrillEnd >= o.DateStart .Where(o => o.Key.Id == group.Key.Id)
&& new TimeDto(s.ShiftStart) <= new TimeDto(o.DateStart.DateTime) .Where(o => o.Key.IdWellSectionType == group.Key.IdWellSectionType)
&& new TimeDto(s.ShiftEnd) >= new TimeDto(o.DateStart.DateTime)) .Sum(o => o.Max(op => op.Operation.DepthEnd) - o.Min(op => op.Operation.DepthStart)),
?.Driller, SectionCaption = group.First().Section.Caption,
}) WellCount = group.GroupBy(g => g.IdWell).Count(),
.Where(o => o.Driller != null) });
.GroupBy(o => new { o.Driller!.Id, o.Operation.IdWellSectionType });
return stats;
var stats = detectedOperationsGroupedByDrillerAndSection.Select(group => new SlipsStatDto
{
DrillerName = $"{group.First().Driller!.Name} {group.First().Driller!.Patronymic} {group.First().Driller!.Surname}",
SlipsCount = group.Count(),
SlipsTimeInMinutes = group
.Sum(y => (y.Operation.DateEnd - y.Operation.DateStart).TotalMinutes),
SectionDepth = factWellOperationsGroupedByDrillerAndSection
.Where(o => o.Key.Id == group.Key.Id)
.Where(o => o.Key.IdWellSectionType == group.Key.IdWellSectionType)
.Sum(o => o.Max(op => op.Operation.DepthEnd) - o.Min(op => op.Operation.DepthStart)),
SectionCaption = group.First().Section.Caption,
WellCount = group.GroupBy(g => g.IdWell).Count(),
});
return stats;
}
} }
} }