CS2-95 В WellOperationStatController добавить метод получения статистики по скважинам и секциям по массиву id скважин

This commit is contained in:
Фролов 2021-10-12 16:07:08 +05:00
parent e34bb4b554
commit b800759a09
6 changed files with 111 additions and 57 deletions

View File

@ -10,5 +10,6 @@ namespace AsbCloudApp.Services
Task<StatClusterDto> GetStatClusterAsync(int idCluster, CancellationToken token = default);
Task<StatWellDto> GetStatWellAsync(int idWell, CancellationToken token = default);
Task<IEnumerable<PlanFactPredictBase<WellOperationDto>>> GetTvdAsync(int idWell, CancellationToken token);
Task<IEnumerable<StatWellDto>> GetWellsStatAsync(IEnumerable<int> idWells, CancellationToken token);
}
}

View File

@ -13,5 +13,6 @@ namespace AsbCloudApp.Services
Task<string> GetWellCaptionByIdAsync(int idWell, CancellationToken token);
Task<IEnumerable<CompanyDto>> GetCompaniesAsync(int idWell, CancellationToken token);
Task<WellDto> GetAsync(int idWell, CancellationToken token);
bool IsCompanyInvolvedInWell(int idCompany, int idWell);
}
}

View File

@ -0,0 +1,21 @@
using AsbCloudDb.Model;
using System;
using System.Collections.Generic;
namespace AsbCloudInfrastructure.Services.WellOperationService
{
class Race
{
public DateTime StartDate { get; set; }
public double StartWellDepth { get; set; }
public DateTime EndDate { get; set; }
public double EndWellDepth { get; set; }
public double DrillingTime { get; set; }
public double NonProductiveHours { get; set; }
public double DeltaDepth => EndWellDepth - StartWellDepth;
public double DeltaHoursTimeNoNpt => (EndDate - StartDate).TotalHours - NonProductiveHours;
public double Speed => DeltaDepth / (DeltaHoursTimeNoNpt + double.Epsilon);
public List<WellOperation> Operations { get; internal set; }
}
}

View File

@ -12,21 +12,6 @@ using System.Threading.Tasks;
namespace AsbCloudInfrastructure.Services.WellOperationService
{
class Race
{
public DateTime StartDate { get; set; }
public double StartWellDepth { get; set; }
public DateTime EndDate { get; set; }
public double EndWellDepth { get; set; }
public double DrillingTime { get; set; }
public double NonProductiveHours { get; set; }
public double DeltaDepth => EndWellDepth - StartWellDepth;
public double DeltaHoursTimeNoNpt => (EndDate - StartDate).TotalHours - NonProductiveHours;
public double Speed => DeltaDepth / (DeltaHoursTimeNoNpt + double.Epsilon);
public List<WellOperation> Operations { get; internal set; }
}
public class WellOperationsStatService : IWellOperationsStatService
{
private readonly IAsbCloudDbContext db;
@ -63,23 +48,15 @@ namespace AsbCloudInfrastructure.Services.WellOperationService
.AsNoTracking()
.ToListAsync(token);
var operations = wells
.SelectMany(w => w.WellOperations)
.OrderBy(o => o.DateStart)
.ThenBy(o => o.DepthEnd);
var statsWells = new List<StatWellDto>(wells.Count());
var cluster = await cacheCluster.FirstOrDefaultAsync(c => c.Id == idCluster, token);
var wellsIds = wells.Select(o => o.Id).Distinct();
var statsWells = new List<StatWellDto>(wellsIds.Count());
foreach (var idWell in wellsIds)
foreach (var well in wells)
{
var statWellDto = await CalcStatWell(operations, idWell, token);
var statWellDto = await CalcStatWellAsync(well, token);
statsWells.Add(statWellDto);
}
var cluster = await cacheCluster.FirstOrDefaultAsync(c => c.Id == idCluster, token);
var statClusterDto = new StatClusterDto
{
Id = idCluster,
@ -89,49 +66,59 @@ namespace AsbCloudInfrastructure.Services.WellOperationService
return statClusterDto;
}
public async Task<StatWellDto> GetStatWellAsync(int idWell,
CancellationToken token = default)
public async Task<IEnumerable<StatWellDto>> GetWellsStatAsync(IEnumerable<int> idWells, CancellationToken token)
{
var operations = await db.WellOperations
.Where(o => o.IdWell == idWell)
.OrderBy(o => o.DateStart) // ускорит дальнейшие сортировки
.ThenBy(o => o.DepthEnd)
var wells = await db.Wells
.Include(w => w.WellOperations)
.Where(w => idWells.Contains(w.Id))
.Where(w => w.WellOperations.Any())
.AsNoTracking()
.ToListAsync(token);
if (!operations.Any())
return null;
var statsWells = new List<StatWellDto>(wells.Count);
var statWellDto = await CalcStatWell(operations, idWell, token);
foreach (var well in wells)
{
var statWellDto = await CalcStatWellAsync(well, token);
statsWells.Add(statWellDto);
}
return statsWells;
}
public async Task<StatWellDto> GetStatWellAsync(int idWell,
CancellationToken token = default)
{
var well = await db.Wells
.Include(w => w.WellOperations)
.FirstOrDefaultAsync(w => w.Id == idWell, token)
.ConfigureAwait(false);
var statWellDto = await CalcStatWellAsync(well, token);
return statWellDto;
}
private async Task<StatWellDto> CalcStatWell(IEnumerable<WellOperation> operations, int idWell,
CancellationToken token = default)
private async Task<StatWellDto> CalcStatWellAsync(Well well, CancellationToken token = default)
{
var wellOperations = operations
.Where(o => o.IdWell == idWell);
var wellOperations = well.WellOperations
.OrderBy(o => o.DateStart)
.ThenBy(o => o.DepthEnd);
var well = await cacheWell.FirstOrDefaultAsync(w => w.Id == idWell, token);
var wellType = await cacheWellType.FirstOrDefaultAsync(t => t.Id == well.IdWellType, token);
if (!wellOperations.Any())
return new StatWellDto()
{
Id = idWell,
Caption = well.Caption,
WellType = wellType.Caption
};
var statWellDto = new StatWellDto
var statWellDto = new StatWellDto()
{
Id = idWell,
Id = well.Id,
Caption = well.Caption,
WellType = wellType?.Caption,
Companies = await wellService.GetCompaniesAsync(idWell, token),
Sections = CalcSectionsStats(wellOperations),
Total = GetStat(wellOperations),
WellType = wellType.Caption
};
if (!wellOperations.Any())
return statWellDto;
statWellDto.Companies = await wellService.GetCompaniesAsync(well.Id, token);
statWellDto.Sections = CalcSectionsStats(wellOperations);
statWellDto.Total = GetStat(wellOperations);
return statWellDto;
}
@ -412,7 +399,7 @@ namespace AsbCloudInfrastructure.Services.WellOperationService
return merged;
}
public static List<Tuple<WellOperation, WellOperation>> MergeArrays(IEnumerable<WellOperation> array1, IEnumerable<WellOperation> array2)
private static List<Tuple<WellOperation, WellOperation>> MergeArrays(IEnumerable<WellOperation> array1, IEnumerable<WellOperation> array2)
{
var a1 = array1.ToArray();
var a2 = array2.ToArray();

View File

@ -48,6 +48,9 @@ namespace AsbCloudInfrastructure.Services
return wells.Select(w => From(w));
}
public bool IsCompanyInvolvedInWell(int idCompany, int idWell)
=> cacheRelationCompaniesWells.Contains(r => r.IdWell == idWell && r.IdCompany == idCompany);
public async Task<bool> IsCompanyInvolvedInWellAsync(int idCompany, int idWell, CancellationToken token)
=> await cacheRelationCompaniesWells.ContainsAsync(r => r.IdWell == idWell &&
r.IdCompany == idCompany, token).ConfigureAwait(false);

View File

@ -3,6 +3,7 @@ using AsbCloudApp.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
@ -25,6 +26,12 @@ namespace AsbCloudWebApi.Controllers
this.wellService = wellService;
}
/// <summary>
/// Получает статстику по скважинам куста
/// </summary>
/// <param name="idCluster">id куста</param>
/// <param name="token"></param>
/// <returns></returns>
[HttpGet]
[Route("cluster/{idCluster}/stat")]
[ProducesResponseType(typeof(StatClusterDto), (int)System.Net.HttpStatusCode.OK)]
@ -40,6 +47,29 @@ namespace AsbCloudWebApi.Controllers
return Ok(result);
}
/// <summary>
/// Получает статстику по списку скважин
/// </summary>
/// <param name="idWells">список скважин</param>
/// <param name="token"></param>
/// <returns></returns>
[HttpGet]
[Route("wellsStats")]
[ProducesResponseType(typeof(IEnumerable<StatWellDto>), (int)System.Net.HttpStatusCode.OK)]
public async Task<IActionResult> GetWellsStatAsync([FromQuery]IEnumerable<int> idWells, CancellationToken token = default)
{
var protectedIdWells = idWells.Where(CanUserAccessToWell);
var result = await operationsStatService.GetWellsStatAsync(protectedIdWells, token)
.ConfigureAwait(false);
return Ok(result);
}
/// <summary>
/// Получает статистику по скважине
/// </summary>
/// <param name="idWell">id скважины</param>
/// <param name="token"></param>
/// <returns></returns>
[HttpGet]
[Route("well/{idWell}/stat")]
[ProducesResponseType(typeof(StatWellDto), (int)System.Net.HttpStatusCode.OK)]
@ -54,7 +84,12 @@ namespace AsbCloudWebApi.Controllers
return Ok(result);
}
/// <summary>
/// Получает данные для графика глубина-днь
/// </summary>
/// <param name="idWell"></param>
/// <param name="token"></param>
/// <returns></returns>
[HttpGet]
[Route("well/{idWell}/tvd")]
[ProducesResponseType(typeof(IEnumerable<PlanFactPredictBase<WellOperationDto>>), (int)System.Net.HttpStatusCode.OK)]
@ -69,6 +104,12 @@ namespace AsbCloudWebApi.Controllers
return Ok(result);
}
private bool CanUserAccessToWell(int idWell)
{
int? idCompany = User.GetCompanyId();
return idCompany is not null && wellService.IsCompanyInvolvedInWell((int)idCompany, idWell);
}
private async Task<bool> CanUserAccessToWellAsync(int idWell, CancellationToken token = default)
{
int? idCompany = User.GetCompanyId();