From c3f81e570d91d4c736e039304a9355d35ffb96f9 Mon Sep 17 00:00:00 2001 From: ngfrolov Date: Fri, 17 Jun 2022 17:21:14 +0500 Subject: [PATCH] =?UTF-8?q?DetectedOperationController=20=D0=BF=D0=B5?= =?UTF-8?q?=D1=80=D0=B5=D0=B4=D0=B5=D0=BB=D0=B0=D0=BD=D1=8B=20route.=20?= =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5=D0=BD=D0=BE=202=20?= =?UTF-8?q?=D0=B4=D0=B5=D1=82=D0=B5=D0=BA=D1=82=D0=BE=D1=80=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Requests/DetectedOperationRequest.cs | 13 ++++++-- .../Services/IDetectedOperationService.cs | 4 +-- .../DetectedOperationService.cs | 11 +++---- .../Detectors/DetectorAbstract.cs | 2 +- .../Detectors/DetectorDrillingRotor.cs | 33 +++++++++++-------- .../Detectors/DetectorDrillingSlide.cs | 31 ++++++++++------- .../OperationDetectionBackgroundService.cs | 4 +-- .../SAUB/DetectedOperationController.cs | 33 +++++++++++-------- 8 files changed, 79 insertions(+), 52 deletions(-) diff --git a/AsbCloudApp/Requests/DetectedOperationRequest.cs b/AsbCloudApp/Requests/DetectedOperationRequest.cs index 5ce2b499..6d54a801 100644 --- a/AsbCloudApp/Requests/DetectedOperationRequest.cs +++ b/AsbCloudApp/Requests/DetectedOperationRequest.cs @@ -1,5 +1,5 @@ using System; -using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; namespace AsbCloudApp.Requests { @@ -9,9 +9,16 @@ namespace AsbCloudApp.Requests public class DetectedOperationRequest : RequestBase { /// - /// категории операций + /// категория операций /// - public IEnumerable CategoryIds { get; set; } + [Required] + public int IdWell { get; set; } + + /// + /// категория операций + /// + [Required] + public int IdCategory { get; set; } /// /// Больше или равно дате diff --git a/AsbCloudApp/Services/IDetectedOperationService.cs b/AsbCloudApp/Services/IDetectedOperationService.cs index 3b3bccf5..0443d3eb 100644 --- a/AsbCloudApp/Services/IDetectedOperationService.cs +++ b/AsbCloudApp/Services/IDetectedOperationService.cs @@ -9,7 +9,7 @@ namespace AsbCloudApp.Services public interface IDetectedOperationService { Task> GetCategoriesAsync(CancellationToken token); - Task GetAsync(int idWell, Requests.DetectedOperationRequest request, CancellationToken token); - Task DeleteAsync(int idWell, DetectedOperationRequest request, CancellationToken token); + Task GetAsync(DetectedOperationRequest request, CancellationToken token); + Task DeleteAsync(DetectedOperationRequest request, CancellationToken token); } } diff --git a/AsbCloudInfrastructure/Services/DetectOperations/DetectedOperationService.cs b/AsbCloudInfrastructure/Services/DetectOperations/DetectedOperationService.cs index 5d41d171..8989d550 100644 --- a/AsbCloudInfrastructure/Services/DetectOperations/DetectedOperationService.cs +++ b/AsbCloudInfrastructure/Services/DetectOperations/DetectedOperationService.cs @@ -33,7 +33,7 @@ namespace AsbCloudInfrastructure.Services.DetectOperations this.scheduleService = scheduleService; } - public async Task GetAsync(int idWell, DetectedOperationRequest request, CancellationToken token) + public async Task GetAsync(DetectedOperationRequest request, CancellationToken token) { var well = await wellService.GetOrDefaultAsync(idWell, token); if (well?.IdTelemetry is null || well.Timezone is null) @@ -44,8 +44,8 @@ namespace AsbCloudInfrastructure.Services.DetectOperations var data = await query.ToListAsync(token); - var operationValues = await operationValueService.GetByIdWellAsync(idWell, token); - var schedules = await scheduleService.GetByIdWellAsync(idWell, token); + var operationValues = await operationValueService.GetByIdWellAsync(request.IdWell, token); + var schedules = await scheduleService.GetByIdWellAsync(request.IdWell, token); var dtos = data.Select(o => Convert(o, well, operationValues, schedules)); var groups = dtos.GroupBy(o => o.Driller); @@ -78,7 +78,7 @@ namespace AsbCloudInfrastructure.Services.DetectOperations return result; } - public async Task DeleteAsync(int idWell, DetectedOperationRequest request, CancellationToken token) + public async Task DeleteAsync(DetectedOperationRequest request, CancellationToken token) { var well = await wellService.GetOrDefaultAsync(idWell, token); if (well?.IdTelemetry is null || well.Timezone is null) @@ -122,8 +122,7 @@ namespace AsbCloudInfrastructure.Services.DetectOperations if (request is not null) { - if (request.CategoryIds is not null) - query = query.Where(o => request.CategoryIds.Contains(o.IdCategory)); + query = query.Where(o => request.IdCategory == o.IdCategory); if (request.GtDate is not null) query = query.Where(o => o.DateStart >= request.GtDate.Value.ToUtcDateTimeOffset(well.Timezone.Hours)); diff --git a/AsbCloudInfrastructure/Services/DetectOperations/Detectors/DetectorAbstract.cs b/AsbCloudInfrastructure/Services/DetectOperations/Detectors/DetectorAbstract.cs index 0c812b1a..0924849a 100644 --- a/AsbCloudInfrastructure/Services/DetectOperations/Detectors/DetectorAbstract.cs +++ b/AsbCloudInfrastructure/Services/DetectOperations/Detectors/DetectorAbstract.cs @@ -12,7 +12,7 @@ namespace AsbCloudInfrastructure.Services.DetectOperations public int IdCategory { get; } // TODO: assert MaxDurationSeconds and MinDurationSeconds - public double MaxDurationSeconds { get; } = 31 * 24 * 60 * 60; + public double MaxDurationSeconds { get; } = 24 * 60 * 60; public double MinDurationSeconds { get; } = 3; /// diff --git a/AsbCloudInfrastructure/Services/DetectOperations/Detectors/DetectorDrillingRotor.cs b/AsbCloudInfrastructure/Services/DetectOperations/Detectors/DetectorDrillingRotor.cs index 4027ab42..71e1c119 100644 --- a/AsbCloudInfrastructure/Services/DetectOperations/Detectors/DetectorDrillingRotor.cs +++ b/AsbCloudInfrastructure/Services/DetectOperations/Detectors/DetectorDrillingRotor.cs @@ -6,6 +6,11 @@ namespace AsbCloudInfrastructure.Services.DetectOperations.Detectors #nullable enable class DetectorDrillingRotor : DetectorAbstract { + const double minRop = 5; //м/час + const double minRotorSpeed = 5; //об/мин + const double ticksPerHour = 60 * 60 * 10_000_000d; + const double minPressure = 25; + public DetectorDrillingRotor() : base(1) { FragmentLength = 15; @@ -15,17 +20,19 @@ namespace AsbCloudInfrastructure.Services.DetectOperations.Detectors protected override bool DetectStart(DetectableTelemetry[] telemetry, int position) { var firstItem = telemetry[position]; - var delta = firstItem.WellDepth - firstItem.BitDepth; - if (delta is not null && - System.Math.Abs((float)delta) > 1d) + var deltaDepth = firstItem.WellDepth - firstItem.BitDepth; + if (deltaDepth is not null && + System.Math.Abs((float)deltaDepth) > 1d) + return false; + + if(firstItem.RotorSpeed > minRotorSpeed) + return false; + + if (firstItem.Pressure < minPressure) return false; var fragment = telemetry[position..(position + FragmentLength)]; - const double minRop = 5; //м/час - const double minRotorSpeed = 5; //об/мин - const double ticksPerHour = 60 * 60 * 10_000_000d; - var lineBlockPosition = new InterpolationLine(fragment.Select(d => (d.BlockPosition ?? 0d, d.DateTime.Ticks / ticksPerHour))); if (!lineBlockPosition.IsYDecreases(minRop)) return false; @@ -34,19 +41,19 @@ namespace AsbCloudInfrastructure.Services.DetectOperations.Detectors if (!lineWellDepth.IsYIncreases(minRop)) return false; - var lineRotorSpeed = new InterpolationLine(fragment.Select(d => (d.RotorSpeed ?? 0d, d.DateTime.Ticks / ticksPerHour))); - if (!lineRotorSpeed.IsAverageYLessThanBound(minRotorSpeed)) - return false; - return true; } protected override bool DetectEnd(DetectableTelemetry[] telemetry, int position) => !DetectStart(telemetry, position); + /// + /// Рассчитываем МСП, м/час + /// + /// protected override void CalcValue(ref DetectedOperation result) - { - throw new System.NotImplementedException(); + { + result.Value = (result.DepthEnd - result.DepthStart) / (result.DateEnd - result.DateStart).TotalHours; } } #nullable disable diff --git a/AsbCloudInfrastructure/Services/DetectOperations/Detectors/DetectorDrillingSlide.cs b/AsbCloudInfrastructure/Services/DetectOperations/Detectors/DetectorDrillingSlide.cs index ddf15aa0..8fd1579f 100644 --- a/AsbCloudInfrastructure/Services/DetectOperations/Detectors/DetectorDrillingSlide.cs +++ b/AsbCloudInfrastructure/Services/DetectOperations/Detectors/DetectorDrillingSlide.cs @@ -6,6 +6,11 @@ namespace AsbCloudInfrastructure.Services.DetectOperations.Detectors #nullable enable class DetectorDrillingSlide : DetectorAbstract { + const double minRop = 5; //м/час + const double minRotorSpeed = 5; //об/мин + const double ticksPerHour = 60 * 60 * 10_000_000d; + const double minPressure = 25; + public DetectorDrillingSlide() : base(3) { FragmentLength = 10; @@ -14,17 +19,19 @@ namespace AsbCloudInfrastructure.Services.DetectOperations.Detectors protected override bool DetectStart(DetectableTelemetry[] telemetry, int position) { var firstItem = telemetry[position]; - var delta = firstItem.WellDepth - firstItem.BitDepth; - if (delta is not null && - System.Math.Abs((float)delta) > 1d) + var deltaDepth = firstItem.WellDepth - firstItem.BitDepth; + if (deltaDepth is not null && + System.Math.Abs((float)deltaDepth) > 1d) + return false; + + if(firstItem.RotorSpeed < minRotorSpeed) + return false; + + if (firstItem.Pressure < minPressure) return false; var fragment = telemetry[position..(position + FragmentLength)]; - const double minRop = 5; //м/час - const double minRotorSpeed = 5; //об/мин - const double ticksPerHour = 60 * 60 * 10_000_000d; - var lineBlockPosition = new InterpolationLine(fragment.Select(d => (d.BlockPosition ?? 0d, d.DateTime.Ticks / ticksPerHour))); if (!lineBlockPosition.IsYDecreases(minRop)) return false; @@ -33,19 +40,19 @@ namespace AsbCloudInfrastructure.Services.DetectOperations.Detectors if (!lineWellDepth.IsYIncreases(minRop)) return false; - var lineRotorSpeed = new InterpolationLine(fragment.Select(d => (d.RotorSpeed ?? 0d, d.DateTime.Ticks / ticksPerHour))); - if (!lineRotorSpeed.IsAverageYMoreThanBound(minRotorSpeed)) - return false; - return true; } protected override bool DetectEnd(DetectableTelemetry[] telemetry, int position) => !DetectStart(telemetry, position); + /// + /// Рассчитываем МСП, м/час + /// + /// protected override void CalcValue(ref DetectedOperation result) { - throw new System.NotImplementedException(); + result.Value = (result.DepthEnd - result.DepthStart) / (result.DateEnd - result.DateStart).TotalHours; } } #nullable disable diff --git a/AsbCloudInfrastructure/Services/DetectOperations/OperationDetectionBackgroundService.cs b/AsbCloudInfrastructure/Services/DetectOperations/OperationDetectionBackgroundService.cs index 59cc3f31..16e414ae 100644 --- a/AsbCloudInfrastructure/Services/DetectOperations/OperationDetectionBackgroundService.cs +++ b/AsbCloudInfrastructure/Services/DetectOperations/OperationDetectionBackgroundService.cs @@ -16,8 +16,8 @@ namespace AsbCloudInfrastructure.Services.DetectOperations private readonly IEnumerable detectors = new List { new Detectors.DetectorSlipsTime(), - // new Detectors.DetectorDrillingRotor(), - // new Detectors.DetectorDrillingSlide(), + new Detectors.DetectorDrillingRotor(), + new Detectors.DetectorDrillingSlide(), }; private readonly int minStepLength; diff --git a/AsbCloudWebApi/Controllers/SAUB/DetectedOperationController.cs b/AsbCloudWebApi/Controllers/SAUB/DetectedOperationController.cs index 828208bf..343944fe 100644 --- a/AsbCloudWebApi/Controllers/SAUB/DetectedOperationController.cs +++ b/AsbCloudWebApi/Controllers/SAUB/DetectedOperationController.cs @@ -1,8 +1,10 @@ using AsbCloudApp.Data; using AsbCloudApp.Requests; using AsbCloudApp.Services; +using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; using System.Threading; using System.Threading.Tasks; @@ -11,8 +13,9 @@ namespace AsbCloudWebApi.Controllers.SAUB /// /// Операции определенные по телеметрии САУБ /// - [Route("api/well/{idWell}/[controller]")] + [Route("api/[controller]")] [ApiController] + [Authorize] public class DetectedOperationController : ControllerBase { private readonly IDetectedOperationService detectedOperationService; @@ -47,16 +50,13 @@ namespace AsbCloudWebApi.Controllers.SAUB [HttpGet] [ProducesResponseType(typeof(DetectedOperationListDto), (int)System.Net.HttpStatusCode.OK)] public async Task GetAsync( - int idWell, [FromQuery] DetectedOperationRequest request, CancellationToken token = default) { - int? idCompany = User.GetCompanyId(); - if (idCompany is null || !await wellService.IsCompanyInvolvedInWellAsync((int)idCompany, - idWell, token).ConfigureAwait(false)) + if (!await UserHasAccesToWellAsync(request.IdWell, token)) return Forbid(); - var result = await detectedOperationService.GetAsync(idWell, request, token); + var result = await detectedOperationService.GetAsync(request, token); return Ok(result); } @@ -73,17 +73,24 @@ namespace AsbCloudWebApi.Controllers.SAUB [Permission] [ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)] public async Task DeleteAsync( - int idWell, - [FromQuery] DetectedOperationRequest request, - CancellationToken token = default) + [FromQuery, Required] DetectedOperationRequest request, + CancellationToken token) { - int? idCompany = User.GetCompanyId(); - if (idCompany is null || !await wellService.IsCompanyInvolvedInWellAsync((int)idCompany, - idWell, token).ConfigureAwait(false)) + if (!await UserHasAccesToWellAsync(request.IdWell, token)) return Forbid(); - var result = await detectedOperationService.DeleteAsync(idWell, request, token); + var result = await detectedOperationService.DeleteAsync(request, token); return Ok(result); } + + protected async Task UserHasAccesToWellAsync(int idWell, CancellationToken token) + { + var idCompany = User.GetCompanyId(); + if (idCompany is not null && + await wellService.IsCompanyInvolvedInWellAsync((int)idCompany, idWell, token) + .ConfigureAwait(false)) + return true; + return false; + } } }