Merge branch 'dev' into feature/user_settings

This commit is contained in:
ai.astrakhantsev 2022-11-08 11:37:52 +05:00
commit 6ac073d59a
6 changed files with 183 additions and 15 deletions

View File

@ -0,0 +1,32 @@
using System.Collections.Generic;
namespace AsbCloudApp.Data.Subsystems
{
#nullable enable
/// <summary>
/// Статистика наработки подсистем по активным скважинам
/// </summary>
public class SubsystemActiveWellStatDto
{
/// <summary>
/// Активная скважина
/// </summary>
public WellDto Well { get; set; }
/// <summary>
/// Наработки подсистемы АКБ
/// </summary>
public SubsystemStatDto? SubsystemAKB { get; set; }
/// <summary>
/// Наработки подсистемы МСЕ
/// </summary>
public SubsystemStatDto? SubsystemMSE { get; set; }
/// <summary>
/// Наработки подсистемы СПИН
/// </summary>
public SubsystemStatDto? SubsystemSpinMaster { get; set; }
/// <summary>
/// Наработки подсистемы ТОРК
/// </summary>
public SubsystemStatDto? SubsystemTorqueMaster { get; set; }
}
#nullable disable
}

View File

@ -1,6 +1,7 @@
using AsbCloudApp.Data; using AsbCloudApp.Data;
using AsbCloudApp.Data.DetectedOperation; using AsbCloudApp.Data.DetectedOperation;
using AsbCloudApp.Requests; using AsbCloudApp.Requests;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Threading; using System.Threading;
@ -39,6 +40,16 @@ namespace AsbCloudApp.Services
/// <returns></returns> /// <returns></returns>
Task<IEnumerable<DetectedOperationDto>?> GetOperationsAsync(DetectedOperationRequest request, CancellationToken token); Task<IEnumerable<DetectedOperationDto>?> GetOperationsAsync(DetectedOperationRequest request, CancellationToken token);
/// <summary>
/// Получить интервалы глубин по всем скважинам
/// </summary>
/// <param name="telemetryIds">список ИД телеметрий активных скважин</param>
/// <param name="gtDate"></param>
/// <param name="ltDate"></param>
/// <param name="token"></param>
/// <returns>кортеж - ид телеметрии, интервалы глубины забоя (ротор,слайд) </returns>
Task<IEnumerable<(int idTelemetry,double depthIntervalRotor, double depthIntervalSlide)>> GetDepthIntervalAllOperationsAsync(IEnumerable<int?> telemetryIds,DateTimeOffset gtDate, DateTimeOffset ltDate, CancellationToken token);
/// <summary> /// <summary>
/// Удалить операции /// Удалить операции
/// </summary> /// </summary>

View File

@ -47,6 +47,15 @@ namespace AsbCloudApp.Services.Subsystems
/// <param name="token"></param> /// <param name="token"></param>
/// <returns></returns> /// <returns></returns>
Task<DatesRangeDto?> GetDateRangeOperationTimeAsync(SubsystemOperationTimeRequest request, CancellationToken token); Task<DatesRangeDto?> GetDateRangeOperationTimeAsync(SubsystemOperationTimeRequest request, CancellationToken token);
/// <summary>
/// Получение статистики по наработке подсистем по активным скважинам
/// </summary>
/// <param name="idCompany"></param>
/// <param name="gtDate"></param>
/// <param name="ltDate"></param>
/// <param name="token"></param>
/// <returns></returns>
Task<IEnumerable<SubsystemActiveWellStatDto>> GetStatByActiveWells(int idCompany, DateTime? gtDate, DateTime? ltDate, CancellationToken token);
} }
#nullable disable #nullable disable
} }

View File

@ -18,7 +18,7 @@ namespace AsbCloudInfrastructure.Services.DetectOperations
#nullable enable #nullable enable
public class DetectedOperationService : IDetectedOperationService public class DetectedOperationService : IDetectedOperationService
{ {
public const int IdOperationRotor = 1; public const int IdOperationRotor = 2;
public const int IdOperationSlide = 3; public const int IdOperationSlide = 3;
public const int IdOperationSlipsTime = 14; public const int IdOperationSlipsTime = 14;
public const int idOperationFlushing = 22; public const int idOperationFlushing = 22;
@ -84,6 +84,30 @@ namespace AsbCloudInfrastructure.Services.DetectOperations
return dtos; return dtos;
} }
public async Task<IEnumerable<(int idTelemetry, double depthIntervalRotor, double depthIntervalSlide)>> GetDepthIntervalAllOperationsAsync(IEnumerable<int?> telemetryIds, DateTimeOffset gtDate, DateTimeOffset ltDate, CancellationToken token)
{
var query = db.Set<DetectedOperation>()
.Include(o => o.OperationCategory)
.Where(o => o.DateStart >= gtDate)
.Where(o => o.DateEnd <= ltDate)
.Where(o => telemetryIds.Contains(o.IdTelemetry))
.GroupBy(g => g.IdTelemetry)
.Select(g => new
{
IdTelemetry = g.Key,
RotorDepthInterval = g.Where(o => o.IdCategory == IdOperationRotor).Sum(o => o.DepthEnd - o.DepthStart),
SlideDepthInterval = g.Where(o => o.IdCategory == IdOperationSlide).Sum(o => o.DepthEnd - o.DepthStart)
});
var data = await query.ToArrayAsync(token);
var result = data.Select(g =>
(
g.IdTelemetry,
g.RotorDepthInterval,
g.SlideDepthInterval
));
return result;
}
private static IEnumerable<DetectedOperationDrillersStatDto> GetOperationsDrillersStat(IEnumerable<DetectedOperationDto> operations) private static IEnumerable<DetectedOperationDrillersStatDto> GetOperationsDrillersStat(IEnumerable<DetectedOperationDto> operations)
{ {
var groups = operations.GroupBy(o => o.Driller); var groups = operations.GroupBy(o => o.Driller);
@ -301,6 +325,7 @@ namespace AsbCloudInfrastructure.Services.DetectOperations
var exportService = new DetectedOperationExportService(db, wellService); var exportService = new DetectedOperationExportService(db, wellService);
return exportService.ExportAsync(idsWells, token); return exportService.ExportAsync(idsWells, token);
} }
} }
#nullable disable #nullable disable
} }

View File

@ -25,6 +25,10 @@ namespace AsbCloudInfrastructure.Services.Subsystems
private readonly IWellService wellService; private readonly IWellService wellService;
private readonly ICrudService<SubsystemDto> subsystemService; private readonly ICrudService<SubsystemDto> subsystemService;
private readonly IDetectedOperationService detectedOperationService; private readonly IDetectedOperationService detectedOperationService;
public const int IdSubsystemAKB = 1;
public const int IdSubsystemMSE = 2;
public const int IdSubsystemSpin = 65536;
public const int IdSubsystemTorque = 65537;
public SubsystemOperationTimeService(IAsbCloudDbContext db, IWellService wellService, ICrudService<SubsystemDto> subsystemService, IDetectedOperationService detectedOperationService) public SubsystemOperationTimeService(IAsbCloudDbContext db, IWellService wellService, ICrudService<SubsystemDto> subsystemService, IDetectedOperationService detectedOperationService)
{ {
this.db = db; this.db = db;
@ -75,7 +79,7 @@ namespace AsbCloudInfrastructure.Services.Subsystems
data = Trim(data, begin, end); data = Trim(data, begin, end);
} }
var dtos = data.Select(o => Convert(o, well)); var dtos = data.Select(o => Convert(o, well.Timezone.Hours));
return dtos; return dtos;
} }
@ -101,7 +105,7 @@ namespace AsbCloudInfrastructure.Services.Subsystems
return null; return null;
var depthInterval = GetDepthInterval(detectedOperations); var depthInterval = GetDepthInterval(detectedOperations);
var statList = CalcStat(data,depthInterval,request, token); var statList = CalcStat(data,depthInterval);
return statList; return statList;
} }
@ -125,7 +129,7 @@ namespace AsbCloudInfrastructure.Services.Subsystems
return items; return items;
} }
private IEnumerable<SubsystemStatDto> CalcStat(IEnumerable<SubsystemOperationTimeDto> dtos, (double depthIntervalRotor, double depthIntervalSlide) depthInterval, SubsystemOperationTimeRequest request, CancellationToken token) private IEnumerable<SubsystemStatDto> CalcStat(IEnumerable<SubsystemOperationTimeDto> dtos, (double depthIntervalRotor, double depthIntervalSlide) depthInterval)
{ {
var groupedDataSubsystems = dtos var groupedDataSubsystems = dtos
.GroupBy(o => o.IdSubsystem); .GroupBy(o => o.IdSubsystem);
@ -152,9 +156,8 @@ namespace AsbCloudInfrastructure.Services.Subsystems
return result; return result;
} }
private (double depthIntervalRotor, double depthIntervalSlide) GetDepthInterval (IEnumerable<DetectedOperationDto> detectedOperations) private static (double depthIntervalRotor, double depthIntervalSlide) GetDepthInterval (IEnumerable<DetectedOperationDto> detectedOperations)
{ {
var depthIntervalRotor = detectedOperations.Where(o => o.IdCategory == 1) var depthIntervalRotor = detectedOperations.Where(o => o.IdCategory == 1)
.Sum(o => o.DepthEnd - o.DepthStart); .Sum(o => o.DepthEnd - o.DepthStart);
var depthIntervalSlide = detectedOperations.Where(o => o.IdCategory == 3) var depthIntervalSlide = detectedOperations.Where(o => o.IdCategory == 3)
@ -163,21 +166,21 @@ namespace AsbCloudInfrastructure.Services.Subsystems
return depthInterval; return depthInterval;
} }
private double GetDepthIntervalSubsystem(int idSubsystem, (double depthIntervalRotor, double depthIntervalSlide) depthInterval) private static double GetDepthIntervalSubsystem(int idSubsystem, (double depthIntervalRotor, double depthIntervalSlide) depthInterval)
{ {
var depthIntervalSubsystem = 0d; var depthIntervalSubsystem = 0d;
//AKB - MSE //AKB - MSE
if (idSubsystem == 1 | idSubsystem == 2) if (idSubsystem == IdSubsystemAKB | idSubsystem == IdSubsystemMSE)
{ {
depthIntervalSubsystem = depthInterval.depthIntervalRotor + depthInterval.depthIntervalSlide; depthIntervalSubsystem = depthInterval.depthIntervalRotor + depthInterval.depthIntervalSlide;
} }
//Spin //Spin
if (idSubsystem == 65536) if (idSubsystem == IdSubsystemSpin)
{ {
depthIntervalSubsystem = depthInterval.depthIntervalSlide; depthIntervalSubsystem = depthInterval.depthIntervalSlide;
} }
//Torque //Torque
if (idSubsystem == 65537) if (idSubsystem == IdSubsystemTorque)
{ {
depthIntervalSubsystem = depthInterval.depthIntervalRotor; depthIntervalSubsystem = depthInterval.depthIntervalRotor;
} }
@ -185,6 +188,75 @@ namespace AsbCloudInfrastructure.Services.Subsystems
} }
private async Task<IEnumerable<WellDto>> GetActiveWellsByCompany(int idCompany, CancellationToken token)
{
var listWell = await wellService.GetWellsByCompanyAsync(idCompany, token);
var active = listWell.Where(w => w.IdState == 1);
return active;
}
/// <inheritdoc/>
public async Task<IEnumerable<SubsystemActiveWellStatDto>> GetStatByActiveWells(int idCompany, DateTime? gtDate, DateTime? ltDate, CancellationToken token)
{
var wells = await GetActiveWellsByCompany(idCompany, token);
if (!wells.Any())
return Enumerable.Empty<SubsystemActiveWellStatDto>();
var hoursOffset = wells
.FirstOrDefault(well => well.Timezone is not null)
?.Timezone.Hours
?? 5d;
var beginUTC = gtDate.HasValue
? gtDate.Value.ToUtcDateTimeOffset(hoursOffset)
:DateTime.Today.AddDays(-1).ToUtcDateTimeOffset(hoursOffset);
var endUTC = ltDate.HasValue
? ltDate.Value.ToUtcDateTimeOffset(hoursOffset)
: DateTime.Today.ToUtcDateTimeOffset(hoursOffset);
var telemetryIds = wells
.Where(w => w.IdTelemetry is not null)
.Select(w => w.IdTelemetry)
.Distinct();
var query = db.SubsystemOperationTimes
.Where(o => telemetryIds.Contains(o.IdTelemetry) &&
o.DateStart >= beginUTC &&
o.DateEnd <= endUTC)
.AsNoTracking();
var subsystemsOperationTime = await query.ToListAsync(token);
var depthIntervals = await detectedOperationService
.GetDepthIntervalAllOperationsAsync(telemetryIds, beginUTC, endUTC, token);
var result = wells
.Select(well => {
var dtos = subsystemsOperationTime
.Where(s => s.IdTelemetry == well.IdTelemetry)
.Select(s => Convert(s, well.Timezone.Hours));
var (idTelemetry, depthIntervalRotor, depthIntervalSlide) = depthIntervals
.FirstOrDefault(i => i.idTelemetry == well.IdTelemetry);
var subsystemStat = idTelemetry > 0 && dtos.Any()
? CalcStat(dtos, (depthIntervalRotor, depthIntervalSlide))
: Enumerable.Empty<SubsystemStatDto>();
return new SubsystemActiveWellStatDto
{
Well = well,
SubsystemAKB = subsystemStat.FirstOrDefault(s => s.IdSubsystem == IdSubsystemAKB),
SubsystemMSE = subsystemStat.FirstOrDefault(s => s.IdSubsystem == IdSubsystemMSE),
SubsystemSpinMaster = subsystemStat.FirstOrDefault(s => s.IdSubsystem == IdSubsystemSpin),
SubsystemTorqueMaster = subsystemStat.FirstOrDefault(s => s.IdSubsystem == IdSubsystemTorque),
};
});
return result;
}
/// <inheritdoc/> /// <inheritdoc/>
public async Task<DatesRangeDto?> GetDateRangeOperationTimeAsync(SubsystemOperationTimeRequest request, CancellationToken token) public async Task<DatesRangeDto?> GetDateRangeOperationTimeAsync(SubsystemOperationTimeRequest request, CancellationToken token)
{ {
@ -261,11 +333,12 @@ namespace AsbCloudInfrastructure.Services.Subsystems
return query; return query;
} }
private static SubsystemOperationTimeDto Convert(SubsystemOperationTime operationTime, WellDto well) private static SubsystemOperationTimeDto Convert(SubsystemOperationTime operationTime, double? timezoneHours = null)
{ {
var dto = operationTime.Adapt<SubsystemOperationTimeDto>(); var dto = operationTime.Adapt<SubsystemOperationTimeDto>();
dto.DateStart = operationTime.DateStart.ToRemoteDateTime(well.Timezone.Hours); var hours = timezoneHours ?? operationTime.Telemetry.TimeZone.Hours;
dto.DateEnd = operationTime.DateEnd.ToRemoteDateTime(well.Timezone.Hours); dto.DateStart = operationTime.DateStart.ToRemoteDateTime(hours);
dto.DateEnd = operationTime.DateEnd.ToRemoteDateTime(hours);
return dto; return dto;
} }

View File

@ -46,6 +46,24 @@ namespace AsbCloudWebApi.Controllers.Subsystems
return Ok(subsystemResult); return Ok(subsystemResult);
} }
/// <summary>
/// получить статистику по активным скважинам
/// </summary>
/// <param name="GtDate"> Больше или равно дате </param>
/// <param name="LtDate"> Меньше или равно дате </param>
/// <param name="token"> Токен </param>
/// <returns> </returns>
[HttpGet("statByActiveWell")]
[ProducesResponseType(typeof(IEnumerable<SubsystemActiveWellStatDto>), (int)System.Net.HttpStatusCode.OK)]
public async Task<IActionResult> GetStatByWellAsync(DateTime? GtDate, DateTime? LtDate, CancellationToken token = default)
{
var idCompany = User.GetCompanyId();
if (!idCompany.HasValue)
return Forbid();
var subsystemResult = await subsystemOperationTimeService.GetStatByActiveWells(idCompany.Value, GtDate, LtDate, token);
return Ok(subsystemResult);
}
/// <summary> /// <summary>
/// получить список подсистем общий. /// получить список подсистем общий.
/// </summary> /// </summary>