2022-04-28 15:04:13 +05:00
|
|
|
|
using AsbCloudApp.Data;
|
2022-08-04 15:06:17 +05:00
|
|
|
|
using AsbCloudApp.Data.DetectedOperation;
|
2022-04-28 15:04:13 +05:00
|
|
|
|
using AsbCloudApp.Requests;
|
|
|
|
|
using AsbCloudApp.Services;
|
|
|
|
|
using AsbCloudDb;
|
|
|
|
|
using AsbCloudDb.Model;
|
|
|
|
|
using Mapster;
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace AsbCloudInfrastructure.Services.DetectOperations
|
|
|
|
|
{
|
2023-04-18 16:22:53 +05:00
|
|
|
|
|
2022-06-15 14:57:37 +05:00
|
|
|
|
public class DetectedOperationService : IDetectedOperationService
|
2022-04-28 15:04:13 +05:00
|
|
|
|
{
|
|
|
|
|
private readonly IAsbCloudDbContext db;
|
|
|
|
|
private readonly IWellService wellService;
|
2022-08-11 14:30:20 +05:00
|
|
|
|
private readonly IRepositoryWellRelated<OperationValueDto> operationValueService;
|
2022-08-11 13:55:36 +05:00
|
|
|
|
private readonly IScheduleRepository scheduleService;
|
2022-04-28 15:04:13 +05:00
|
|
|
|
|
2022-06-15 14:57:37 +05:00
|
|
|
|
public DetectedOperationService(IAsbCloudDbContext db, IWellService wellService,
|
2022-08-11 14:30:20 +05:00
|
|
|
|
IRepositoryWellRelated<OperationValueDto> operationValueService, IScheduleRepository scheduleService)
|
2022-04-28 15:04:13 +05:00
|
|
|
|
{
|
|
|
|
|
this.db = db;
|
|
|
|
|
this.wellService = wellService;
|
2022-06-08 14:37:05 +05:00
|
|
|
|
this.operationValueService = operationValueService;
|
|
|
|
|
this.scheduleService = scheduleService;
|
2022-04-28 15:04:13 +05:00
|
|
|
|
}
|
|
|
|
|
|
2022-08-04 15:06:17 +05:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-19 13:39:50 +05:00
|
|
|
|
public async Task<IEnumerable<DetectedOperationDto>> GetOperationsAsync(DetectedOperationRequest request, CancellationToken token)
|
2022-04-28 15:04:13 +05:00
|
|
|
|
{
|
2022-06-17 17:21:14 +05:00
|
|
|
|
var well = await wellService.GetOrDefaultAsync(request.IdWell, token);
|
2023-12-19 13:39:50 +05:00
|
|
|
|
if (well?.IdTelemetry is null)
|
|
|
|
|
return Enumerable.Empty<DetectedOperationDto>();
|
2022-08-04 15:06:17 +05:00
|
|
|
|
|
2023-12-19 13:39:50 +05:00
|
|
|
|
var query = BuildQuery(well, request).AsNoTracking();
|
2022-04-28 15:04:13 +05:00
|
|
|
|
|
|
|
|
|
var data = await query.ToListAsync(token);
|
2022-06-15 14:57:37 +05:00
|
|
|
|
|
2022-06-17 17:21:14 +05:00
|
|
|
|
var operationValues = await operationValueService.GetByIdWellAsync(request.IdWell, token);
|
|
|
|
|
var schedules = await scheduleService.GetByIdWellAsync(request.IdWell, token);
|
2022-06-14 15:35:31 +05:00
|
|
|
|
var dtos = data.Select(o => Convert(o, well, operationValues, schedules));
|
2022-08-04 15:06:17 +05:00
|
|
|
|
return dtos;
|
2022-11-03 15:57:45 +05:00
|
|
|
|
}
|
2022-11-03 13:30:16 +05:00
|
|
|
|
|
2022-08-04 15:06:17 +05:00
|
|
|
|
private static IEnumerable<DetectedOperationDrillersStatDto> GetOperationsDrillersStat(IEnumerable<DetectedOperationDto> operations)
|
|
|
|
|
{
|
|
|
|
|
var groups = operations.GroupBy(o => o.Driller);
|
|
|
|
|
|
|
|
|
|
var stats = new List<DetectedOperationDrillersStatDto>(groups.Count());
|
2022-06-14 15:35:31 +05:00
|
|
|
|
foreach (var group in groups)
|
2022-06-08 14:37:05 +05:00
|
|
|
|
{
|
2022-06-14 15:35:31 +05:00
|
|
|
|
var itemsWithTarget = group.Where(i => i.OperationValue is not null);
|
2022-08-04 15:06:17 +05:00
|
|
|
|
var stat = new DetectedOperationDrillersStatDto
|
2022-06-14 15:35:31 +05:00
|
|
|
|
{
|
|
|
|
|
Driller = group.Key,
|
|
|
|
|
AverageValue = group.Sum(e => e.Value) / group.Count(),
|
|
|
|
|
Count = group.Count(),
|
|
|
|
|
};
|
|
|
|
|
if (itemsWithTarget.Any())
|
|
|
|
|
{
|
|
|
|
|
var itemsOutOfTarget = itemsWithTarget.Where(o => !IsTargetOk(o));
|
2022-08-04 15:06:17 +05:00
|
|
|
|
stat.AverageTargetValue = itemsWithTarget.Average(e => e.OperationValue?.TargetValue);
|
2022-06-14 15:35:31 +05:00
|
|
|
|
stat.Efficiency = 100d * itemsOutOfTarget.Count() / itemsWithTarget.Count();
|
|
|
|
|
stat.Loss = itemsOutOfTarget.Sum(DeltaToTarget);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
stats.Add(stat);
|
2022-06-08 14:37:05 +05:00
|
|
|
|
}
|
2022-08-04 15:06:17 +05:00
|
|
|
|
return stats;
|
|
|
|
|
}
|
2022-06-14 15:35:31 +05:00
|
|
|
|
|
2022-08-04 15:06:17 +05:00
|
|
|
|
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;
|
2022-04-28 15:04:13 +05:00
|
|
|
|
}
|
|
|
|
|
|
2022-06-17 17:21:14 +05:00
|
|
|
|
public async Task<int> DeleteAsync(DetectedOperationRequest request, CancellationToken token)
|
2022-04-28 15:04:13 +05:00
|
|
|
|
{
|
2022-06-17 17:21:14 +05:00
|
|
|
|
var well = await wellService.GetOrDefaultAsync(request.IdWell, token);
|
2022-04-28 15:04:13 +05:00
|
|
|
|
if (well?.IdTelemetry is null || well.Timezone is null)
|
|
|
|
|
return 0;
|
|
|
|
|
|
2023-12-05 10:56:49 +05:00
|
|
|
|
var query = BuildQueryBase(well, request);
|
2022-08-04 15:06:17 +05:00
|
|
|
|
|
|
|
|
|
if (query is null)
|
|
|
|
|
return 0;
|
|
|
|
|
|
2022-04-28 15:04:13 +05:00
|
|
|
|
db.DetectedOperations.RemoveRange(query);
|
|
|
|
|
return await db.SaveChangesAsync(token);
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-14 15:35:31 +05:00
|
|
|
|
private static bool IsTargetOk(DetectedOperationDto op)
|
2022-06-10 18:36:14 +05:00
|
|
|
|
{
|
2022-06-14 15:35:31 +05:00
|
|
|
|
return (op.IdCategory) switch
|
2022-06-10 18:36:14 +05:00
|
|
|
|
{
|
2022-12-09 14:36:45 +05:00
|
|
|
|
WellOperationCategory.IdRotor => op.Value > op.OperationValue?.TargetValue,
|
|
|
|
|
WellOperationCategory.IdSlide => op.Value > op.OperationValue?.TargetValue,
|
|
|
|
|
WellOperationCategory.IdSlipsTime => op.Value > op.OperationValue?.TargetValue,
|
2022-08-04 15:06:17 +05:00
|
|
|
|
_ => op.Value > op.OperationValue?.TargetValue,
|
2022-06-14 15:35:31 +05:00
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static double DeltaToTarget(DetectedOperationDto op)
|
|
|
|
|
{
|
|
|
|
|
return (op.IdCategory) switch
|
|
|
|
|
{
|
2022-12-09 14:36:45 +05:00
|
|
|
|
WellOperationCategory.IdRotor => 0,
|
|
|
|
|
WellOperationCategory.IdSlide => 0,
|
|
|
|
|
WellOperationCategory.IdSlipsTime => op.Value - op.OperationValue?.TargetValue??0,
|
2022-06-14 15:35:31 +05:00
|
|
|
|
_ => 0,
|
2022-06-10 18:36:14 +05:00
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-28 15:04:13 +05:00
|
|
|
|
|
2023-12-05 10:56:49 +05:00
|
|
|
|
private IQueryable<DetectedOperation> BuildQueryBase(WellDto well, DetectedOperationRequest request)
|
|
|
|
|
{
|
2022-04-28 15:04:13 +05:00
|
|
|
|
var query = db.Set<DetectedOperation>()
|
|
|
|
|
.Where(o => o.IdTelemetry == well.IdTelemetry);
|
|
|
|
|
|
|
|
|
|
if (request is not null)
|
|
|
|
|
{
|
2023-12-19 00:40:04 +05:00
|
|
|
|
if (request.IdsTelemetries.Any())
|
|
|
|
|
query = query.Where(o => request.IdsTelemetries.Contains(o.IdTelemetry));
|
|
|
|
|
|
|
|
|
|
if (request.IdsCategories.Any())
|
2022-08-04 15:06:17 +05:00
|
|
|
|
query = query.Where(o => request.IdsCategories.Contains(o.IdCategory));
|
2022-04-28 15:04:13 +05:00
|
|
|
|
|
2023-12-19 00:40:04 +05:00
|
|
|
|
if (request.GeDateStart is not null)
|
|
|
|
|
query = query.Where(o => o.DateStart >= request.GeDateStart.Value.Date.ToUtcDateTimeOffset(well.Timezone.Hours));
|
2022-04-28 15:04:13 +05:00
|
|
|
|
|
2023-12-19 00:40:04 +05:00
|
|
|
|
if (request.LeDateEnd is not null)
|
|
|
|
|
query = query.Where(o => o.DateEnd <= request.LeDateEnd.Value.Date.ToUtcDateTimeOffset(well.Timezone.Hours));
|
2022-04-28 15:04:13 +05:00
|
|
|
|
|
2023-12-19 00:40:04 +05:00
|
|
|
|
if (request.GeDepth is not null)
|
|
|
|
|
query = query.Where(o => o.DepthStart >= request.GeDepth);
|
2022-04-28 15:04:13 +05:00
|
|
|
|
|
2023-12-19 00:40:04 +05:00
|
|
|
|
if (request.LeDepth is not null)
|
|
|
|
|
query = query.Where(o => o.DepthEnd <= request.LeDepth);
|
2022-04-28 15:04:13 +05:00
|
|
|
|
|
|
|
|
|
if (request.EqIdTelemetryUser is not null)
|
|
|
|
|
query = query.Where(o => o.IdUsersAtStart == request.EqIdTelemetryUser);
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-05 10:56:49 +05:00
|
|
|
|
return query;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private IQueryable<DetectedOperation> BuildQuery(WellDto well, DetectedOperationRequest request)
|
|
|
|
|
{
|
|
|
|
|
IQueryable<DetectedOperation> query = BuildQueryBase(well, request)
|
|
|
|
|
.Include(o => o.OperationCategory);
|
|
|
|
|
|
2022-04-28 15:04:13 +05:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-02 17:18:16 +05:00
|
|
|
|
private static DetectedOperationDto Convert(DetectedOperation operation, WellDto well, IEnumerable<OperationValueDto> operationValues, IEnumerable<ScheduleDto> schedules)
|
2022-04-28 15:04:13 +05:00
|
|
|
|
{
|
|
|
|
|
var dto = operation.Adapt<DetectedOperationDto>();
|
|
|
|
|
dto.IdWell = well.Id;
|
2022-06-14 15:35:31 +05:00
|
|
|
|
var dateStart = operation.DateStart.ToRemoteDateTime(well.Timezone.Hours);
|
|
|
|
|
dto.DateStart = dateStart;
|
2022-04-28 15:04:13 +05:00
|
|
|
|
dto.DateEnd = operation.DateEnd.ToRemoteDateTime(well.Timezone.Hours);
|
2022-06-16 14:37:34 +05:00
|
|
|
|
dto.OperationValue = operationValues.FirstOrDefault(v => v.IdOperationCategory == dto.IdCategory
|
|
|
|
|
&& v.DepthStart <= dto.DepthStart
|
|
|
|
|
&& v.DepthEnd > dto.DepthStart);
|
2022-06-14 15:35:31 +05:00
|
|
|
|
|
|
|
|
|
var timeStart = new TimeDto(dateStart);
|
2022-06-15 14:57:37 +05:00
|
|
|
|
var driller = schedules.FirstOrDefault(s =>
|
2022-06-14 15:35:31 +05:00
|
|
|
|
s.DrillStart <= dateStart &&
|
|
|
|
|
s.DrillEnd > dateStart && (
|
2022-06-15 14:57:37 +05:00
|
|
|
|
s.ShiftStart > s.ShiftEnd
|
2022-06-14 15:35:31 +05:00
|
|
|
|
) ^ (s.ShiftStart <= timeStart &&
|
|
|
|
|
s.ShiftEnd > timeStart
|
|
|
|
|
))
|
|
|
|
|
?.Driller;
|
|
|
|
|
dto.Driller = driller;
|
2022-04-28 15:04:13 +05:00
|
|
|
|
return dto;
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-04 15:06:17 +05:00
|
|
|
|
public async Task<IEnumerable<WellOperationCategoryDto>?> GetCategoriesAsync(int? idWell, CancellationToken token)
|
2022-04-28 15:04:13 +05:00
|
|
|
|
{
|
2022-08-04 15:06:17 +05:00
|
|
|
|
IQueryable<WellOperationCategory> query;
|
2022-06-17 17:21:14 +05:00
|
|
|
|
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
|
2022-04-28 15:04:13 +05:00
|
|
|
|
.Where(c => c.Id < 1000)
|
2022-06-17 17:21:14 +05:00
|
|
|
|
.AsNoTracking()
|
2022-04-28 15:04:13 +05:00
|
|
|
|
.Select(c => c.Adapt<WellOperationCategoryDto>())
|
|
|
|
|
.ToArrayAsync(token);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-04-18 16:22:53 +05:00
|
|
|
|
|
2022-04-28 15:04:13 +05:00
|
|
|
|
}
|