Merge pull request '#15287262 Аналитика по удержанию в клиньях' (#120) from feature/wedges into dev

Reviewed-on: http://test.digitaldrilling.ru:8080/DDrilling/AsbCloudServer/pulls/120
This commit is contained in:
Никита Фролов 2023-10-04 09:16:25 +05:00
commit b656d475b6
6 changed files with 316 additions and 12 deletions

View File

@ -0,0 +1,44 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AsbCloudApp.Data
{
/// <summary>
/// DTO, описывающая аналитику удержания в клиньях
/// </summary>
public class SlipsStatDto
{
/// <summary>
/// ФИО бурильщика
/// </summary>
public string DrillerName { get; set; } = null!;
/// <summary>
/// Количество скважин
/// </summary>
public int WellCount { get; set; }
/// <summary>
/// Название секции
/// </summary>
public string SectionCaption { get; set; } = null!;
/// <summary>
/// Количество удержаний в клиньях, шт.
/// </summary>
public int SlipsCount { get; set; }
/// <summary>
/// Время удержания в клиньях, мин.
/// </summary>
public double SlipsTimeInMinutes { get; set; }
/// <summary>
/// Проходка, м.
/// </summary>
public double SectionDepth { get; set; }
}
}

View File

@ -0,0 +1,30 @@
using System;
namespace AsbCloudApp.Requests
{
/// <summary>
/// Параметры фильтра операции
/// </summary>
public class OperationStatRequest
{
/// <summary>
/// Дата начала операции в UTC
/// </summary>
public DateTime? DateStartUTC { get; set; }
/// <summary>
/// Дата окончания операции в UTC
/// </summary>
public DateTime? DateEndUTC { get; set; }
/// <summary>
/// Минимальная продолжительность операции, мин
/// </summary>
public double? DurationMinutesMin { get; set; }
/// <summary>
/// Максимальная продолжительность операции, мин
/// </summary>
public double? DurationMinutesMax { get; set; }
}
}

View File

@ -0,0 +1,23 @@
using AsbCloudApp.Data;
using AsbCloudApp.Requests;
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
namespace AsbCloudApp.Services
{
/// <summary>
/// Сервис для получения аналитики удержания в клиньях
/// </summary>
public interface ISlipsStatService
{
/// <summary>
/// Получение записей для построения аналитики удержания в клиньях
/// </summary>
/// <param name="request">параметры запроса</param>
/// <param name="token"></param>
/// <returns></returns>
Task<IEnumerable<SlipsStatDto>> GetAllAsync(OperationStatRequest request, CancellationToken token);
}
}

View File

@ -26,12 +26,10 @@ using System;
using AsbCloudApp.Data.Manuals;
using AsbCloudApp.Services.AutoGeneratedDailyReports;
using AsbCloudApp.Services.Notifications;
using AsbCloudApp.Services.WellOperationImport;
using AsbCloudDb.Model.Manuals;
using AsbCloudInfrastructure.Services.AutoGeneratedDailyReports;
using AsbCloudApp.Services.WellOperationImport;
using AsbCloudInfrastructure.Services.WellOperationImport;
using AsbCloudInfrastructure.Services.WellOperationImport.FileParser;
using AsbCloudInfrastructure.Services.ProcessMap.ProcessMapWellboreDevelopment;
namespace AsbCloudInfrastructure
{
@ -123,7 +121,6 @@ namespace AsbCloudInfrastructure
services.AddTransient<IAuthService, AuthService>();
services.AddTransient<IDepositRepository, DepositRepository>();
services.AddTransient<IProcessMapPlanRepository, ProcessMapRepository>();
services.AddTransient<IProcessMapWellboreDevelopmentRepository, ProcessMapWellboreDevelopmentRepository>();
services.AddTransient<IDrillingProgramService, DrillingProgramService>();
services.AddTransient<IEventService, EventService>();
services.AddTransient<FileService>();
@ -136,6 +133,7 @@ namespace AsbCloudInfrastructure
services.AddTransient<ITelemetryUserService, TelemetryUserService>();
services.AddTransient<ITimezoneService, TimezoneService>();
services.AddTransient<IWellService, WellService>();
services.AddTransient<IWellOperationImportService, WellOperationImportService>();
services.AddTransient<IProcessMapPlanImportService, ProcessMapPlanImportService>();
services.AddTransient<IPlannedTrajectoryImportService, PlannedTrajectoryImportService>();
services.AddTransient<IWellOperationRepository, WellOperationRepository>();
@ -151,7 +149,6 @@ namespace AsbCloudInfrastructure
services.AddTransient<ILimitingParameterService, LimitingParameterService>();
services.AddTransient<IProcessMapReportMakerService, ProcessMapReportMakerService>();
services.AddTransient<IProcessMapService, ProcessMapService>();
services.AddTransient<IProcessMapWellboreDevelopmentService, ProcessMapWellboreDevelopmentService>();
services.AddTransient<WellInfoService>();
services.AddTransient<IHelpPageService, HelpPageService>();
@ -204,6 +201,7 @@ namespace AsbCloudInfrastructure
services.AddTransient<ITrajectoryPlanRepository, TrajectoryPlanRepository>();
services.AddTransient<ITrajectoryFactRepository, TrajectoryFactRepository>();
services.AddTransient<IFaqRepository, FaqRepository>();
services.AddTransient<ISlipsStatService, SlipsStatService>();
services.AddTransient<IWellContactService, WellContactService>();
services.AddTransient<ICrudRepository<WellSectionTypeDto>, CrudCacheRepositoryBase<WellSectionTypeDto,
WellSectionType>>();
@ -235,13 +233,6 @@ namespace AsbCloudInfrastructure
services.AddTransient<IWellboreService, WellboreService>();
services.AddTransient<IWellOperationExportService, WellOperationExportService>();
services.AddTransient<IWellOperationImportService, WellOperationImportService>();
services.AddTransient<IWellOperationImportTemplateService, WellOperationImportTemplateService>();
services.AddTransient<IWellOperationExcelParser, WellOperationDefaultExcelParser>();
services.AddTransient<IWellOperationExcelParser, WellOperationGazpromKhantosExcelParser>();
return services;
}

View File

@ -0,0 +1,163 @@
using AsbCloudApp.Data;
using AsbCloudApp.Requests;
using AsbCloudApp.Services;
using AsbCloudDb.Model;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace AsbCloudInfrastructure.Services;
public class SlipsStatService : ISlipsStatService
{
private readonly IAsbCloudDbContext db;
public SlipsStatService(IAsbCloudDbContext db)
{
this.db = 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)
{
var durationMinutesMin = TimeSpan.FromMinutes(request.DurationMinutesMin.Value);
detectedOperationsQuery = detectedOperationsQuery
.Where(o => o.DateEnd - o.DateStart >= durationMinutesMin);
}
if (request.DurationMinutesMax.HasValue)
{
var durationMinutesMax = TimeSpan.FromMinutes(request.DurationMinutesMax.Value);
detectedOperationsQuery = detectedOperationsQuery
.Where(o => o.DateEnd - o.DateStart <= durationMinutesMax);
}
var detectedOperations = await detectedOperationsQuery
.ToArrayAsync(token);
var detectedOperationsGroupedByDrillerAndSection = detectedOperations.Select(o => new
{
Operation = o,
IdWell = telemetries[o.IdTelemetry],
schedules.FirstOrDefault(s =>
s.IdWell == telemetries[o.IdTelemetry]
&& s.DrillStart <= o.DateStart
&& s.DrillEnd >= o.DateStart
&& new TimeDto(s.ShiftStart) <= new TimeDto(o.DateStart.DateTime)
&& new TimeDto(s.ShiftEnd) >= new TimeDto(o.DateStart.DateTime))
?.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.Section != null)
.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
.Select(o => new
{
Operation = o,
schedules.FirstOrDefault(s =>
s.IdWell == o.IdWell
&& s.DrillStart <= o.DateStart
&& s.DrillEnd >= o.DateStart
&& new TimeDto(s.ShiftStart) <= new TimeDto(o.DateStart.DateTime)
&& new TimeDto(s.ShiftEnd) >= new TimeDto(o.DateStart.DateTime))
?.Driller,
})
.Where(o => o.Driller != null)
.GroupBy(o => new { o.Driller!.Id, o.Operation.IdWellSectionType });
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;
}
}

View File

@ -0,0 +1,53 @@
using AsbCloudApp.Data;
using AsbCloudApp.Exceptions;
using AsbCloudApp.Requests;
using AsbCloudApp.Services;
using AsbCloudDb.Model;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
namespace AsbCloudWebApi.Controllers
{
/// <summary>
/// Аналитика по удержанию в клиньях
/// </summary>
[Route("api/slipsStat")]
[ApiController]
[Authorize]
public class SlipsStatController : ControllerBase
{
private readonly ISlipsStatService slipsAnalyticsService;
public SlipsStatController(ISlipsStatService slipsAnalyticsService)
{
this.slipsAnalyticsService = slipsAnalyticsService;
}
/// <summary>
/// Получить аналитику по удержанию в клиньях (по бурильщикам)
/// </summary>
/// <param name="request">Параметры запроса</param>
/// <param name="token"> Токен отмены задачи </param>
/// <returns>Список бурильщиков</returns>
[HttpGet]
[Permission]
[ProducesResponseType(typeof(IEnumerable<SlipsStatDto>), (int)System.Net.HttpStatusCode.OK)]
public async Task<IActionResult> GetAllAsync(
[FromQuery] OperationStatRequest request,
CancellationToken token)
{
var idUser = User.GetUserId();
if (!idUser.HasValue)
throw new ForbidException("Не удается вас опознать");
var data = await slipsAnalyticsService.GetAllAsync(request, token).ConfigureAwait(false);
return Ok(data);
}
}
}