DD.WellWorkover.Cloud/AsbCloudInfrastructure/Services/AnalyticsService.cs

311 lines
14 KiB
C#
Raw Normal View History

2021-07-21 15:29:19 +05:00
using AsbCloudApp.Data;
using AsbCloudApp.Services;
using AsbCloudDb.Model;
2021-07-21 15:29:19 +05:00
using AsbCloudInfrastructure.Services.Cache;
using System;
using System.Collections.Generic;
2021-07-21 15:29:19 +05:00
using System.Linq;
using Microsoft.EntityFrameworkCore;
namespace AsbCloudInfrastructure.Services
{
public class AnalyticsService : IAnalyticsService
{
private readonly IAsbCloudDbContext db;
private readonly ITelemetryService telemetryService;
2021-07-20 11:24:57 +05:00
private readonly ISaubDataCache saubDataCache;
private readonly CacheTable<Operation> cacheOperations;
private readonly OperationDetectorService operationDetectorService;
private readonly IEnumerable<Operation> operations;
2021-07-21 15:29:19 +05:00
public AnalyticsService(IAsbCloudDbContext db, ITelemetryService telemetryService,
2021-07-20 11:24:57 +05:00
ISaubDataCache saubDataCache, CacheDb cacheDb)
{
this.db = db;
this.telemetryService = telemetryService;
2021-07-20 11:24:57 +05:00
this.saubDataCache = saubDataCache;
cacheOperations = cacheDb.GetCachedTable<Operation>((AsbCloudDbContext)db);
operations = cacheOperations.Select(c => true);
operationDetectorService = new OperationDetectorService(operations);
}
public IEnumerable<WellDepthToDayDto> GetWellDepthToDay(int wellId)
{
var telemetry = telemetryService.GetTelemetryByWellId(wellId);
if (telemetry is null)
return null;
var depthToTimeData = (from d in db.DataSaubBases
where d.IdTelemetry == telemetry.Id
select new
{
d.Id,
d.WellDepth,
d.BitDepth,
d.Date
});
var m = (int)Math.Round(1d * depthToTimeData.Count() / 2048);
if (m > 1)
depthToTimeData = depthToTimeData.Where(d => d.Id % m == 0);
return depthToTimeData.Select(d => new WellDepthToDayDto
{
WellDepth = d.WellDepth ?? 0.0,
BitDepth = d.BitDepth ?? 0.0,
Date = d.Date
}).ToList();
}
public IEnumerable<WellDepthToIntervalDto> GetWellDepthToInterval(int wellId,
int intervalSeconds, int workBeginSeconds)
{
intervalSeconds = intervalSeconds == 0 ? 86400 : intervalSeconds;
var telemetry = telemetryService.GetTelemetryByWellId(wellId);
if (telemetry is null)
return null;
var timezoneOffset = telemetryService.GetTimezoneOffsetByTelemetryId(telemetry.Id);
var drillingPeriodsInfo = db.GetDepthToInterval(telemetry.Id, intervalSeconds,
workBeginSeconds, timezoneOffset);
var wellDepthToIntervalData = drillingPeriodsInfo.Select(d => new WellDepthToIntervalDto
{
IntervalStartDate = d.BeginPeriodDate,
IntervalDepthProgress = (d.MaxDepth - d.MinDepth) ?? 0.0 / intervalSeconds
}).OrderBy(d => d.IntervalStartDate).ToList();
return wellDepthToIntervalData;
}
public PaginationContainer<OperationDto> GetOperationsByWell(int wellId,
IEnumerable<int> categoryIds = default, DateTime begin = default,
DateTime end = default, int skip = 0, int take = 32)
{
var telemetry = telemetryService.GetTelemetryByWellId(wellId);
if (telemetry is null)
return null;
var operations = from a in db.TelemetryAnalysis.Include(t => t.Operation)
where a.IdTelemetry == telemetry.Id
select a;
if ((categoryIds != default) && (categoryIds.Any()))
operations = operations.Where(o => categoryIds.Contains(o.IdOperation));
var result = new PaginationContainer<OperationDto>
{
Skip = skip,
Take = take
};
operations = operations.OrderBy(o => o.UnixDate);
if (begin != default)
{
var unixBegin = (begin - new DateTime(1970, 1, 1)).TotalSeconds;
operations = operations.Where(o => o.UnixDate >= unixBegin);
}
if (end != default)
{
var unixEnd = (end - new DateTime(1970, 1, 1)).TotalSeconds;
operations = operations.Where(m => (m.UnixDate + m.Duration) <= unixEnd);
}
result.Count = operations.Count();
if (skip > 0)
operations = operations.Skip(skip);
var operationsList = operations.Take(take).ToList();
if (operationsList.Count == 0)
return result;
foreach(var operation in operations)
{
var operationDto = new OperationDto
{
Id = operation.Id,
Name = operation.Operation.Name,
BeginDate = DateTimeOffset.FromUnixTimeSeconds(operation.UnixDate).DateTime,
EndDate = DateTimeOffset.FromUnixTimeSeconds(operation.UnixDate + operation.Duration).DateTime,
StartWellDepth = operation.OperationStartDepth ?? 0.0,
EndWellDepth = operation.OperationEndDepth ?? 0.0
};
result.Items.Add(operationDto);
}
return result;
}
public IEnumerable<OperationDurationDto> GetOperationsSummary(int wellId,
DateTime begin = default, DateTime end = default)
{
var telemetry = telemetryService.GetTelemetryByWellId(wellId);
if (telemetry is null)
return null;
var unixBegin = (begin - new DateTime(1970, 1, 1)).TotalSeconds;
var unixEnd = (end - new DateTime(1970, 1, 1)).TotalSeconds;
var operations = (from a in db.TelemetryAnalysis
where a.IdTelemetry == telemetry.Id &&
a.UnixDate > unixBegin && a.UnixDate < unixEnd
join o in db.Operations on a.IdOperation equals o.Id
group a by new { a.IdOperation, o.Name } into g
select new OperationDurationDto
{
OperationName = g.Key.Name,
Duration = g.Where(g => g.Duration > 0)
.Sum(a => a.Duration)
}).ToList();
return operations;
}
public IEnumerable<OperationInfoDto> GetOperationsToInterval(int wellId,
int intervalSeconds, int workBeginSeconds)
{
intervalSeconds = intervalSeconds == 0 ? 86400 : intervalSeconds;
var telemetry = telemetryService.GetTelemetryByWellId(wellId);
if (telemetry is null)
return null;
var timezoneOffset = telemetryService.GetTimezoneOffsetByTelemetryId(telemetry.Id);
var operations = (from a in db.TelemetryAnalysis
where a.IdTelemetry == telemetry.Id
join o in db.Operations on a.IdOperation equals o.Id
2021-07-21 15:29:19 +05:00
group a by new
{
Interval = Math.Floor((a.UnixDate - workBeginSeconds + timezoneOffset) / intervalSeconds),
2021-07-21 15:29:19 +05:00
OperationId = a.IdOperation,
o.Name
} into g
select new
{
IntervalStart = g.Min(d => d.UnixDate),
OperationName = g.Key.Name,
OperationsDuration = g.Sum(an => an.Duration)
}).ToList();
var operationsGroupedByInterval = operations.GroupBy(op => op.IntervalStart)
2021-07-21 15:29:19 +05:00
.Select(o => new OperationInfoDto
{
IntervalBegin = DateTimeOffset.FromUnixTimeSeconds(o.Key),
Operations = o.Select(opr => new OperationDetailsDto
{
OperationName = opr.OperationName,
Duration = opr.OperationsDuration
}).ToList()
})
2021-07-21 15:29:19 +05:00
.OrderBy(ops => ops.IntervalBegin);
return operationsGroupedByInterval;
}
2021-07-20 11:24:57 +05:00
public void SaveAnalytics(DataSaubBase dataSaub)
{
saubDataCache.AddData(dataSaub);
if (saubDataCache.GetOrCreateCache(dataSaub.IdTelemetry).Count() > 1)
{
var dataSaubs = saubDataCache.GetOrCreateCache(dataSaub.IdTelemetry)
.OrderBy(d => d.Date);
var telemetryAnalysis = GetDrillingAnalysis(dataSaubs);
if (saubDataCache.CurrentAnalysis is null)
saubDataCache.CurrentAnalysis = telemetryAnalysis;
if (saubDataCache.CurrentAnalysis.IdOperation == telemetryAnalysis.IdOperation)
{
saubDataCache.CurrentAnalysis.Duration +=
telemetryAnalysis.Duration;
saubDataCache.CurrentAnalysis.OperationEndDepth = dataSaub.WellDepth;
}
else
{
db.TelemetryAnalysis.Add(saubDataCache.CurrentAnalysis);
saubDataCache.CurrentAnalysis = telemetryAnalysis;
saubDataCache.CurrentAnalysis.OperationStartDepth = dataSaub.WellDepth;
}
}
}
private TelemetryAnalysis GetDrillingAnalysis(IEnumerable<DataSaubBase> dataSaubBases)
{
var lastSaubDate = dataSaubBases.Last().Date;
var saubWellDepths = dataSaubBases.Where(sw => sw.WellDepth is not null)
2021-07-21 15:29:19 +05:00
.Select(s => (Value: (double)s.WellDepth,
(s.Date - dataSaubBases.First().Date).TotalSeconds));
var saubBitDepths = dataSaubBases.Where(sw => sw.BitDepth is not null)
2021-07-21 15:29:19 +05:00
.Select(s => (Value: (double)s.BitDepth,
(s.Date - dataSaubBases.First().Date).TotalSeconds));
var saubBlockPositions = dataSaubBases.Where(sw => sw.BlockPosition is not null)
2021-07-21 15:29:19 +05:00
.Select(s => (Value: (double)s.BlockPosition,
(s.Date - dataSaubBases.First().Date).TotalSeconds));
var saubRotorSpeeds = dataSaubBases.Where(sw => sw.RotorSpeed is not null)
2021-07-21 15:29:19 +05:00
.Select(s => (Value: (double)s.RotorSpeed,
(s.Date - dataSaubBases.First().Date).TotalSeconds));
var saubPressures = dataSaubBases.Where(sw => sw.Pressure is not null)
2021-07-21 15:29:19 +05:00
.Select(s => (Value: (double)s.Pressure,
(s.Date - dataSaubBases.First().Date).TotalSeconds));
var saubHookWeights = dataSaubBases.Where(sw => sw.HookWeight is not null)
2021-07-21 15:29:19 +05:00
.Select(s => (Value: (double)s.HookWeight,
(s.Date - dataSaubBases.First().Date).TotalSeconds));
2021-07-20 11:24:57 +05:00
var wellDepthChangingIndex = new InterpolationLine(saubWellDepths).GetAForLinearFormula();
var bitPositionChangingIndex = new InterpolationLine(saubBitDepths).GetAForLinearFormula();
var blockPositionChangingIndex = new InterpolationLine(saubBlockPositions).GetAForLinearFormula();
var rotorSpeedChangingIndex = new InterpolationLine(saubRotorSpeeds).GetAForLinearFormula();
var pressureChangingIndex = new InterpolationLine(saubPressures).GetAForLinearFormula();
var hookWeightChangingIndex = new InterpolationLine(saubHookWeights).GetAForLinearFormula();
2021-07-20 11:24:57 +05:00
var IsBlockRising = InterpolationLine.IsValueDecreases(blockPositionChangingIndex, -0.0001);
var IsBlockGoesDown = InterpolationLine.IsValueIncreases(blockPositionChangingIndex, 0.0001);
var IsBlockStandsStill = InterpolationLine.IsValueNotChanges(blockPositionChangingIndex, (0.0001, -0.0001));
var drillingAnalysis = new TelemetryAnalysis
{
IdTelemetry = dataSaubBases.First().IdTelemetry,
UnixDate = (long)(lastSaubDate - new DateTime(1970, 1, 1, 0, 0, 0)).TotalSeconds,
Duration = (int)(dataSaubBases.Last().Date - dataSaubBases.ElementAt(dataSaubBases.Count() - 2).Date).TotalSeconds,
OperationStartDepth = null,
OperationEndDepth = null,
2021-07-20 11:24:57 +05:00
IsWellDepthDecreasing = InterpolationLine.IsValueDecreases(wellDepthChangingIndex, -0.0001),
IsWellDepthIncreasing = InterpolationLine.IsValueIncreases(wellDepthChangingIndex, 0.0001),
IsBitPositionDecreasing = InterpolationLine.IsValueDecreases(bitPositionChangingIndex, -0.0001),
IsBitPositionIncreasing = InterpolationLine.IsValueIncreases(bitPositionChangingIndex, 0.0001),
IsBitDepthLess20 = InterpolationLine.IsAverageLessThanBound(saubBitDepths, 20),
IsBlockPositionDecreasing = InterpolationLine.IsValueDecreases(blockPositionChangingIndex, -0.0001),
IsBlockPositionIncreasing = InterpolationLine.IsValueIncreases(blockPositionChangingIndex, 0.0001),
IsRotorSpeedLt3 = InterpolationLine.IsAverageLessThanBound(saubRotorSpeeds, 3),
IsRotorSpeedGt3 = InterpolationLine.IsAverageMoreThanBound(saubRotorSpeeds, 3),
IsPressureLt20 = InterpolationLine.IsAverageLessThanBound(saubPressures, 20),
IsPressureGt20 = InterpolationLine.IsAverageMoreThanBound(saubPressures, 20),
IsHookWeightNotChanges = InterpolationLine.IsValueNotChanges(hookWeightChangingIndex, (0.0001, -0.0001)),
IsHookWeightLt3 = InterpolationLine.IsAverageLessThanBound(saubHookWeights, 3),
IdOperation = 1
};
drillingAnalysis.IdOperation = operationDetectorService.DetectOperation(drillingAnalysis).Id;
return drillingAnalysis;
}
}
}