From d361c9a9b432af143cc4480ed6c81fb617b931c5 Mon Sep 17 00:00:00 2001 From: KharchenkoVladimir Date: Mon, 22 Nov 2021 17:29:19 +0500 Subject: [PATCH] CS2-107: Added calculation of max and average Rop for cluster wells --- AsbCloudApp/Data/WellDto.cs | 2 + AsbCloudApp/Services/IWellService.cs | 1 + AsbCloudInfrastructure/DependencyInjection.cs | 2 +- ...tatService.cs => OperationsStatService.cs} | 67 ++++++++++++++++++- .../Services/WellService.cs | 16 +++++ .../ClusterOperationStatController.cs | 67 ------------------- ...ntroller.cs => OperationStatController.cs} | 48 +++++++++++-- 7 files changed, 129 insertions(+), 74 deletions(-) rename AsbCloudInfrastructure/Services/WellOperationService/{WellOperationsStatService.cs => OperationsStatService.cs} (88%) delete mode 100644 AsbCloudWebApi/Controllers/ClusterOperationStatController.cs rename AsbCloudWebApi/Controllers/{WellOperationStatController.cs => OperationStatController.cs} (65%) diff --git a/AsbCloudApp/Data/WellDto.cs b/AsbCloudApp/Data/WellDto.cs index 38bf79ce..d55e7549 100644 --- a/AsbCloudApp/Data/WellDto.cs +++ b/AsbCloudApp/Data/WellDto.cs @@ -10,6 +10,8 @@ namespace AsbCloudApp.Data public double? Longitude { get; set; } public string WellType { get; set; } public int IdWellType { get; set; } + + public int? IdCluster { get; set; } /// /// 0 - незвестно, diff --git a/AsbCloudApp/Services/IWellService.cs b/AsbCloudApp/Services/IWellService.cs index d6f08988..10c9512e 100644 --- a/AsbCloudApp/Services/IWellService.cs +++ b/AsbCloudApp/Services/IWellService.cs @@ -18,5 +18,6 @@ namespace AsbCloudApp.Services bool IsCompanyInvolvedInWell(int idCompany, int idWell); string GetStateText(int state); DateTime GetLastTelemetryDate(int idWell); + Task> GetClusterWellsIdsAsync(int idWell, CancellationToken token); } } diff --git a/AsbCloudInfrastructure/DependencyInjection.cs b/AsbCloudInfrastructure/DependencyInjection.cs index 92805779..10dc2982 100644 --- a/AsbCloudInfrastructure/DependencyInjection.cs +++ b/AsbCloudInfrastructure/DependencyInjection.cs @@ -40,7 +40,7 @@ namespace AsbCloudInfrastructure services.AddTransient(); services.AddTransient(); services.AddTransient(); - services.AddTransient(); + services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); diff --git a/AsbCloudInfrastructure/Services/WellOperationService/WellOperationsStatService.cs b/AsbCloudInfrastructure/Services/WellOperationService/OperationsStatService.cs similarity index 88% rename from AsbCloudInfrastructure/Services/WellOperationService/WellOperationsStatService.cs rename to AsbCloudInfrastructure/Services/WellOperationService/OperationsStatService.cs index d819d7c1..45cc8212 100644 --- a/AsbCloudInfrastructure/Services/WellOperationService/WellOperationsStatService.cs +++ b/AsbCloudInfrastructure/Services/WellOperationService/OperationsStatService.cs @@ -12,10 +12,11 @@ using System.Threading.Tasks; namespace AsbCloudInfrastructure.Services.WellOperationService { - public class WellOperationsStatService : IWellOperationsStatService + public class OperationsStatService : IOperationsStatService { private readonly IAsbCloudDbContext db; private readonly IWellService wellService; + private readonly ITelemetryService telemetryService; private readonly CacheTable cacheSectionsTypes; private readonly CacheTable cacheWellType; private readonly CacheTable cacheCluster; @@ -29,14 +30,33 @@ namespace AsbCloudInfrastructure.Services.WellOperationService private const int idOperationTypePlan = 0; private const int idOperationTypeFact = 1; - public WellOperationsStatService(IAsbCloudDbContext db, CacheDb cache, IWellService wellService) + public OperationsStatService(IAsbCloudDbContext db, CacheDb cache, IWellService wellService, + ITelemetryService telemetryService) { this.db = db; this.wellService = wellService; + this.telemetryService = telemetryService; cacheSectionsTypes = cache.GetCachedTable((DbContext)db); cacheWellType = cache.GetCachedTable((DbContext)db); cacheCluster = cache.GetCachedTable((DbContext)db); } + + public async Task GetRopStatByIdWellAsync(int idWell, + CancellationToken token) + { + return await GetRopStatAsync(idWell, token).ConfigureAwait(false); + } + + public async Task GetRopStatByUidAsync(string uid, + CancellationToken token) + { + var idWell = telemetryService.GetidWellByTelemetryUid(uid); + + if (idWell is null) + return null; + + return await GetRopStatAsync((int)idWell, token).ConfigureAwait(false); + } public async Task GetStatClusterAsync(int idCluster, int idCompany, CancellationToken token = default) { @@ -93,6 +113,49 @@ namespace AsbCloudInfrastructure.Services.WellOperationService return statWellDto; } + private async Task GetRopStatAsync(int idWell, CancellationToken token) + { + var clusterWellsIds = await wellService.GetClusterWellsIdsAsync(idWell, token) + .ConfigureAwait(false); + + if (clusterWellsIds is null) + return null; + + var idLastSectionType = await (from o in db.WellOperations + where o.IdWell == idWell && + o.IdType == 1 + orderby o.DepthStart + select o.IdWellSectionType) + .LastOrDefaultAsync(token) + .ConfigureAwait(false); + + var operations = await (from o in db.WellOperations + where clusterWellsIds.Contains(o.IdWell) && + o.IdType == 1 && + o.IdWellSectionType == idLastSectionType + select o) + .ToListAsync(token) + .ConfigureAwait(false); + + var statsList = clusterWellsIds.Select(clusterWellId => + { + var currentWellOps = operations.Where(o => o.IdWell == clusterWellId); + var stat = CalcStat(currentWellOps); + return stat; + }).Where(c => c is not null); + + if (!statsList.Any()) + return null; + + var clusterRops = new ClusterRopStatDto() + { + RopMax = statsList.Max(s => s.Rop), + RopAverage = statsList.Average(s => s.Rop) + }; + + return clusterRops; + } + private async Task CalcStatWellAsync(Well well, CancellationToken token = default) { var wellType = await cacheWellType.FirstOrDefaultAsync(t => t.Id == well.IdWellType, token); diff --git a/AsbCloudInfrastructure/Services/WellService.cs b/AsbCloudInfrastructure/Services/WellService.cs index 679da469..3e546705 100644 --- a/AsbCloudInfrastructure/Services/WellService.cs +++ b/AsbCloudInfrastructure/Services/WellService.cs @@ -138,12 +138,28 @@ namespace AsbCloudInfrastructure.Services }; } + public async Task> GetClusterWellsIdsAsync(int idWell, CancellationToken token) + { + var well = await cacheWells.FirstOrDefaultAsync(w => w.Id == idWell) + .ConfigureAwait(false); + + if (well is null) + return null; + + var clusterWells = await cacheWells.WhereAsync(w => w.IdCluster == well.IdCluster) + .ConfigureAwait(false); + + return clusterWells.Select(w => w.Id); + + } + private WellDto Convert(Well well) { return new WellDto { Id = well.Id, Caption = well.Caption, + IdCluster = well.IdCluster, Cluster = well.Cluster.Caption, Deposit = well.Cluster.Deposit.Caption, LastTelemetryDate = GetLastTelemetryDate(well.Id), diff --git a/AsbCloudWebApi/Controllers/ClusterOperationStatController.cs b/AsbCloudWebApi/Controllers/ClusterOperationStatController.cs deleted file mode 100644 index 60b652e0..00000000 --- a/AsbCloudWebApi/Controllers/ClusterOperationStatController.cs +++ /dev/null @@ -1,67 +0,0 @@ -using AsbCloudApp.Data; -using AsbCloudApp.Services; -using Microsoft.AspNetCore.Authorization; -using Microsoft.AspNetCore.Http; -using Microsoft.AspNetCore.Mvc; -using System; -using System.Collections.Generic; -using System.IO; -using System.Threading; -using System.Threading.Tasks; - -namespace AsbCloudWebApi.Controllers -{ - /// - /// Контроллер статистики вручную внесенных операций на кусту - /// - [Route("api/cluster")] - [ApiController] - [Authorize] - public class ClusterOperationStatController : ControllerBase - { - private readonly IWellOperationService operationService; - private readonly IWellService wellService; - private readonly IWellOperationImportService wellOperationImportService; - - public ClusterOperationStatController(IWellOperationService operationService, - IWellService wellService, IWellOperationImportService wellOperationImportService) - { - this.operationService = operationService; - this.wellService = wellService; - this.wellOperationImportService = wellOperationImportService; - } - - /// - /// Формирует данные по среднему и максимальному МСП на кусту - /// - /// id скважины с данного куста (через нее будут полуены данные) - /// - /// Возвращает данные по среднему и максимальному МСП на кусту - [HttpGet("{idWell}/ropStat")] - [ProducesResponseType(typeof(ClusterRopStatDto), (int)System.Net.HttpStatusCode.OK)] - public async Task GetClusterRopStatAsync([FromRoute] int idWell, - CancellationToken token = default) - { - if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false)) - return Forbid(); - - // var result = await operationService.GetOperationsAsync( - // idWell, token).ConfigureAwait(false); - - var result = new ClusterRopStatDto() - { - RopMax = 3.2, - RopAverage = 1.1 - }; - - return Ok(result); - } - - private async Task CanUserAccessToWellAsync(int idWell, CancellationToken token = default) - { - int? idCompany = User.GetCompanyId(); - return idCompany is not null && await wellService.IsCompanyInvolvedInWellAsync((int)idCompany, - idWell, token).ConfigureAwait(false); - } - } -} \ No newline at end of file diff --git a/AsbCloudWebApi/Controllers/WellOperationStatController.cs b/AsbCloudWebApi/Controllers/OperationStatController.cs similarity index 65% rename from AsbCloudWebApi/Controllers/WellOperationStatController.cs rename to AsbCloudWebApi/Controllers/OperationStatController.cs index f6235f8d..8f3fc356 100644 --- a/AsbCloudWebApi/Controllers/WellOperationStatController.cs +++ b/AsbCloudWebApi/Controllers/OperationStatController.cs @@ -10,21 +10,61 @@ using System.Threading.Tasks; namespace AsbCloudWebApi.Controllers { /// - /// Контроллер секций скважины + /// Контроллер статистики по операциям на скважине /// [ApiController] [Authorize] [Route("api")] - public class WellOperationStatController : ControllerBase + public class OperationStatController : ControllerBase { - private readonly IWellOperationsStatService operationsStatService; + private readonly IOperationsStatService operationsStatService; private readonly IWellService wellService; - public WellOperationStatController(IWellOperationsStatService sectionsService, IWellService wellService) + public OperationStatController(IOperationsStatService sectionsService, IWellService wellService) { this.operationsStatService = sectionsService; this.wellService = wellService; } + + /// + /// Формирует данные по среднему и максимальному МСП на кусту по id скважины + /// + /// id скважины с данного куста (через нее будут полуены данные) + /// + /// Возвращает данные по среднему и максимальному МСП на кусту + [HttpGet("cluster/{idWell}/idWellRopStat")] + [ProducesResponseType(typeof(ClusterRopStatDto), (int)System.Net.HttpStatusCode.OK)] + public async Task GetClusterRopStatByIdWellAsync([FromRoute] int idWell, + CancellationToken token = default) + { + if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false)) + return Forbid(); + + var result = await operationsStatService.GetRopStatByIdWellAsync( + idWell, token).ConfigureAwait(false); + + return Ok(result); + } + + /// + /// Формирует данные по среднему и максимальному МСП на кусту по uid панели + /// + /// id передающей данные панели + /// + /// Возвращает данные по среднему и максимальному МСП на кусту + [HttpGet("cluster/{uid}/uidRopStat")] + [ProducesResponseType(typeof(ClusterRopStatDto), (int)System.Net.HttpStatusCode.OK)] + public async Task GetClusterRopStatByUidAsync([FromRoute] string uid, + CancellationToken token = default) + { + // if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false)) + // return Forbid(); + + var result = await operationsStatService.GetRopStatByUidAsync( + uid, token).ConfigureAwait(false); + + return Ok(result); + } /// /// Получает статстику по скважинам куста