forked from ddrilling/AsbCloudServer
336 lines
13 KiB
C#
336 lines
13 KiB
C#
using AsbCloudApp.Data;
|
|
using AsbCloudApp.Data.DetectedOperation;
|
|
using AsbCloudApp.Requests;
|
|
using AsbCloudApp.Services;
|
|
using AsbCloudDb;
|
|
using AsbCloudDb.Model;
|
|
using Mapster;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace AsbCloudInfrastructure.Services.DetectOperations
|
|
{
|
|
#nullable enable
|
|
public class DetectedOperationService : IDetectedOperationService
|
|
{
|
|
public const int IdOperationRotor = 2;
|
|
public const int IdOperationSlide = 3;
|
|
public const int IdOperationSlipsTime = 14;
|
|
public const int idOperationFlushing = 22;
|
|
public const int IdOperationElaboration = 1022;
|
|
|
|
public const int idOperationRepair = 1031;
|
|
public const int IdOperationGis = 1001;
|
|
public const int IdOperationOzc = 1008;
|
|
public const int IdOperationCementing = 1040;
|
|
public const int IdOperationNpv = 1043;
|
|
|
|
public const int IdOperationElaborationBeforeBuilding = 18;
|
|
public const int IdOperationTemplatingBeforeBuilding = 19;
|
|
public const int IdOperationFlushingBeforeBuilding = 20;
|
|
public const int IdOperationStaticSurveying = 21;
|
|
|
|
private readonly IAsbCloudDbContext db;
|
|
private readonly IWellService wellService;
|
|
private readonly IRepositoryWellRelated<OperationValueDto> operationValueService;
|
|
private readonly IScheduleRepository scheduleService;
|
|
|
|
public DetectedOperationService(IAsbCloudDbContext db, IWellService wellService,
|
|
IRepositoryWellRelated<OperationValueDto> operationValueService, IScheduleRepository scheduleService)
|
|
{
|
|
this.db = db;
|
|
this.wellService = wellService;
|
|
this.operationValueService = operationValueService;
|
|
this.scheduleService = scheduleService;
|
|
}
|
|
|
|
public async Task<DetectedOperationListDto?> GetAsync(DetectedOperationRequest request, CancellationToken token)
|
|
{
|
|
var dtos = await GetOperationsAsync(request, token);
|
|
if (dtos?.Any() != true)
|
|
return null;
|
|
|
|
var stats = GetOperationsDrillersStat(dtos);
|
|
var result = new DetectedOperationListDto
|
|
{
|
|
Operations = dtos,
|
|
Stats = stats
|
|
};
|
|
return result;
|
|
}
|
|
|
|
public async Task<IEnumerable<DetectedOperationDto>?> GetOperationsAsync(DetectedOperationRequest request, CancellationToken token)
|
|
{
|
|
var well = await wellService.GetOrDefaultAsync(request.IdWell, token);
|
|
if (well?.IdTelemetry is null || well.Timezone is null)
|
|
return null;
|
|
|
|
var query = BuildQuery(well, request)
|
|
?.AsNoTracking();
|
|
|
|
if (query is null)
|
|
return null;
|
|
|
|
var data = await query.ToListAsync(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));
|
|
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.ToListAsync(token);
|
|
if (data.Any())
|
|
{
|
|
var result = data.Select(g =>
|
|
(
|
|
g.IdTelemetry,
|
|
g.RotorDepthInterval,
|
|
g.SlideDepthInterval
|
|
));
|
|
return result;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
private static IEnumerable<DetectedOperationDrillersStatDto> GetOperationsDrillersStat(IEnumerable<DetectedOperationDto> operations)
|
|
{
|
|
var groups = operations.GroupBy(o => o.Driller);
|
|
|
|
var stats = new List<DetectedOperationDrillersStatDto>(groups.Count());
|
|
foreach (var group in groups)
|
|
{
|
|
var itemsWithTarget = group.Where(i => i.OperationValue is not null);
|
|
var stat = new DetectedOperationDrillersStatDto
|
|
{
|
|
Driller = group.Key,
|
|
AverageValue = group.Sum(e => e.Value) / group.Count(),
|
|
Count = group.Count(),
|
|
};
|
|
if (itemsWithTarget.Any())
|
|
{
|
|
var itemsOutOfTarget = itemsWithTarget.Where(o => !IsTargetOk(o));
|
|
stat.AverageTargetValue = itemsWithTarget.Average(e => e.OperationValue?.TargetValue);
|
|
stat.Efficiency = 100d * itemsOutOfTarget.Count() / itemsWithTarget.Count();
|
|
stat.Loss = itemsOutOfTarget.Sum(DeltaToTarget);
|
|
}
|
|
|
|
stats.Add(stat);
|
|
}
|
|
return stats;
|
|
}
|
|
|
|
public async Task<IEnumerable<DetectedOperationStatDto>?> GetOperationsStatAsync(DetectedOperationRequest request, CancellationToken token)
|
|
{
|
|
var well = await wellService.GetOrDefaultAsync(request.IdWell, token);
|
|
if (well?.IdTelemetry is null || well.Timezone is null)
|
|
return null;
|
|
|
|
var query = BuildQuery(well, request)
|
|
?.AsNoTracking();
|
|
|
|
if (query is null)
|
|
return null;
|
|
|
|
var entities = await query
|
|
.Select(o => new {
|
|
o.IdCategory,
|
|
DurationMinutes = (o.DateEnd - o.DateStart).TotalMinutes,
|
|
o.Value,
|
|
})
|
|
.ToListAsync(token);
|
|
|
|
if (!entities.Any())
|
|
return null;
|
|
|
|
var operationValues = await operationValueService.GetByIdWellAsync(request.IdWell, token);
|
|
var categories = await query
|
|
.Select(o => new {o.IdCategory, o.OperationCategory.Name })
|
|
.Distinct()
|
|
.ToDictionaryAsync(c=>c.IdCategory, c=>c.Name, token);
|
|
|
|
var dtos = entities
|
|
.GroupBy(o => o.IdCategory)
|
|
.OrderBy(g => g.Key)
|
|
.Select(g => new DetectedOperationStatDto{
|
|
IdCategory = g.Key,
|
|
Category = categories[g.Key],
|
|
Count = g.Count(),
|
|
MinutesAverage = g.Average(o => o.DurationMinutes),
|
|
MinutesMin = g.Min(o => o.DurationMinutes),
|
|
MinutesMax = g.Max(o => o.DurationMinutes),
|
|
MinutesTotal = g.Sum(o => o.DurationMinutes),
|
|
ValueAverage = g.Average(o => o.Value),
|
|
ValueMax = g.Max(o => o.Value),
|
|
ValueMin = g.Min(o => o.Value),
|
|
});
|
|
|
|
return dtos;
|
|
}
|
|
|
|
public async Task<int> DeleteAsync(DetectedOperationRequest request, CancellationToken token)
|
|
{
|
|
var well = await wellService.GetOrDefaultAsync(request.IdWell, token);
|
|
if (well?.IdTelemetry is null || well.Timezone is null)
|
|
return 0;
|
|
|
|
var query = BuildQuery(well, request);
|
|
|
|
if (query is null)
|
|
return 0;
|
|
|
|
db.DetectedOperations.RemoveRange(query);
|
|
return await db.SaveChangesAsync(token);
|
|
}
|
|
|
|
private static bool IsTargetOk(DetectedOperationDto op)
|
|
{
|
|
return (op.IdCategory) switch
|
|
{
|
|
IdOperationRotor => op.Value > op.OperationValue?.TargetValue,
|
|
IdOperationSlide => op.Value > op.OperationValue?.TargetValue,
|
|
IdOperationSlipsTime => op.Value > op.OperationValue?.TargetValue,
|
|
_ => op.Value > op.OperationValue?.TargetValue,
|
|
};
|
|
}
|
|
|
|
private static double DeltaToTarget(DetectedOperationDto op)
|
|
{
|
|
return (op.IdCategory) switch
|
|
{
|
|
IdOperationRotor => 0,
|
|
IdOperationSlide => 0,
|
|
IdOperationSlipsTime => op.Value - op.OperationValue?.TargetValue??0,
|
|
_ => 0,
|
|
};
|
|
}
|
|
|
|
private IQueryable<DetectedOperation>? BuildQuery(WellDto well, DetectedOperationRequest request)
|
|
{
|
|
if (well?.IdTelemetry is null || well.Timezone is null)
|
|
return null;
|
|
|
|
var query = db.Set<DetectedOperation>()
|
|
.Include(o => o.OperationCategory)
|
|
.Where(o => o.IdTelemetry == well.IdTelemetry);
|
|
|
|
if (request is not null)
|
|
{
|
|
if (request.IdsCategories?.Any() == true)
|
|
query = query.Where(o => request.IdsCategories.Contains(o.IdCategory));
|
|
|
|
if (request.GtDate is not null)
|
|
query = query.Where(o => o.DateStart >= request.GtDate.Value.ToUtcDateTimeOffset(well.Timezone.Hours));
|
|
|
|
if (request.LtDate is not null)
|
|
query = query.Where(o => o.DateEnd <= request.LtDate.Value.ToUtcDateTimeOffset(well.Timezone.Hours));
|
|
|
|
if (request.GtDepth is not null)
|
|
query = query.Where(o => o.DepthStart >= request.GtDepth);
|
|
|
|
if (request.LtDepth is not null)
|
|
query = query.Where(o => o.DepthEnd <= request.LtDepth);
|
|
|
|
if (request.EqIdTelemetryUser is not null)
|
|
query = query.Where(o => o.IdUsersAtStart == request.EqIdTelemetryUser);
|
|
}
|
|
|
|
if (request?.SortFields?.Any() == true)
|
|
{
|
|
query = query.SortBy(request.SortFields);
|
|
}
|
|
else
|
|
query = query
|
|
.OrderBy(o => o.DateStart)
|
|
.ThenBy(o => o.DepthStart);
|
|
|
|
if (request?.Skip > 0)
|
|
query = query.Skip((int)request.Skip);
|
|
|
|
if (request?.Take > 0)
|
|
query = query.Take((int)request.Take);
|
|
|
|
return query;
|
|
}
|
|
|
|
private DetectedOperationDto Convert(DetectedOperation operation, WellDto well, IEnumerable<OperationValueDto> operationValues, IEnumerable<ScheduleDto> schedules)
|
|
{
|
|
var dto = operation.Adapt<DetectedOperationDto>();
|
|
dto.IdWell = well.Id;
|
|
var dateStart = operation.DateStart.ToRemoteDateTime(well.Timezone.Hours);
|
|
dto.DateStart = dateStart;
|
|
dto.DateEnd = operation.DateEnd.ToRemoteDateTime(well.Timezone.Hours);
|
|
dto.OperationValue = operationValues.FirstOrDefault(v => v.IdOperationCategory == dto.IdCategory
|
|
&& v.DepthStart <= dto.DepthStart
|
|
&& v.DepthEnd > dto.DepthStart);
|
|
|
|
var timeStart = new TimeDto(dateStart);
|
|
var driller = schedules.FirstOrDefault(s =>
|
|
s.DrillStart <= dateStart &&
|
|
s.DrillEnd > dateStart && (
|
|
s.ShiftStart > s.ShiftEnd
|
|
) ^ (s.ShiftStart <= timeStart &&
|
|
s.ShiftEnd > timeStart
|
|
))
|
|
?.Driller;
|
|
dto.Driller = driller;
|
|
return dto;
|
|
}
|
|
|
|
public async Task<IEnumerable<WellOperationCategoryDto>?> GetCategoriesAsync(int? idWell, CancellationToken token)
|
|
{
|
|
IQueryable<WellOperationCategory> query;
|
|
if(idWell is null)
|
|
{
|
|
query = db.WellOperationCategories;
|
|
}
|
|
else
|
|
{
|
|
var well = await wellService.GetOrDefaultAsync((int )idWell, token);
|
|
if (well?.IdTelemetry is null)
|
|
return null;
|
|
var idTelemetry = (int)well.IdTelemetry;
|
|
query = db.DetectedOperations
|
|
.Include(o => o.OperationCategory)
|
|
.Where(o => o.IdTelemetry == idTelemetry)
|
|
.Select(o => o.OperationCategory)
|
|
.Distinct();
|
|
}
|
|
|
|
var result = await query
|
|
.Where(c => c.Id < 1000)
|
|
.AsNoTracking()
|
|
.Select(c => c.Adapt<WellOperationCategoryDto>())
|
|
.ToArrayAsync(token);
|
|
return result;
|
|
}
|
|
|
|
public Task<Stream> ExportAsync(IEnumerable<int> idsWells, CancellationToken token)
|
|
{
|
|
var exportService = new DetectedOperationExportService(db, wellService);
|
|
return exportService.ExportAsync(idsWells, token);
|
|
}
|
|
|
|
}
|
|
#nullable disable
|
|
}
|