This commit is contained in:
ngfrolov 2024-02-01 17:17:26 +05:00
parent 2832a73b6c
commit e39ac38129
Signed by untrusted user who does not match committer: ng.frolov
GPG Key ID: E99907A0357B29A7
3 changed files with 65 additions and 43 deletions

View File

@ -78,6 +78,6 @@ namespace AsbCloudApp.Repositories
/// </summary> /// </summary>
/// <param name="request"></param> /// <param name="request"></param>
/// <returns></returns> /// <returns></returns>
IEnumerable<int>? GetIds(TelemetryDataRequest request); IEnumerable<int> GetIds(TelemetryDataRequest request);
} }
} }

View File

@ -31,23 +31,37 @@ namespace AsbCloudInfrastructure.Background.PeriodicWorks
using var db = services.GetRequiredService<IAsbCloudDbContext>(); using var db = services.GetRequiredService<IAsbCloudDbContext>();
var telemetryDataCache = services.GetRequiredService<ITelemetryDataCache<TelemetryDataSaubDto>>(); var telemetryDataCache = services.GetRequiredService<ITelemetryDataCache<TelemetryDataSaubDto>>();
var telemetryIds = telemetryDataCache.GetIds(new TelemetryDataRequest() var cacheRequest = new TelemetryDataRequest()
{ {
GeDate = DateTime.UtcNow.AddDays(-Gap) GeDate = DateTime.UtcNow.AddDays(-Gap)
}); };
var idTelemetries = telemetryDataCache.GetIds(cacheRequest);
if (telemetryIds == null || !telemetryIds.Any()) if (!idTelemetries.Any())
return; return;
var dateEnd = db.Set<DataSaubStat>() var stats = await db.Set<DataSaubStat>()
.OrderByDescending(c => c.DateEnd) .Where(s => idTelemetries.Contains(s.IdTelemetry))
.FirstOrDefault()?.DateEnd .GroupBy(s => s.IdTelemetry)
?? DateTimeOffset.MinValue; .ToDictionaryAsync(
g => g.Key,
g => g.OrderByDescending(s => s.DateEnd).First(),
token
);
foreach ( var idTelemetry in idTelemetries)
{
var lastDate = stats.GetValueOrDefault(idTelemetry)?.DateEnd ?? DateTimeOffset.UnixEpoch;
await CreateStatForTelemetryFromDate(db, idTelemetry, lastDate, token);
}
}
private async Task CreateStatForTelemetryFromDate(IAsbCloudDbContext db, int idTelemetry, DateTimeOffset begin, CancellationToken token)
{
var detectedOperations = await db.Set<DetectedOperation>() var detectedOperations = await db.Set<DetectedOperation>()
.Where(o => o.DateStart > dateEnd) .Where(o => o.IdTelemetry == idTelemetry)
.Where(o => o.DateStart > begin)
.Where(o => o.OperationCategory.IdParent == MechanicalDrillingCategoryId) .Where(o => o.OperationCategory.IdParent == MechanicalDrillingCategoryId)
.Where(o => telemetryIds.Contains(o.IdTelemetry))
.OrderBy(o => o.DateStart) .OrderBy(o => o.DateStart)
.Take(500) .Take(500)
.ToArrayAsync(token); .ToArrayAsync(token);
@ -55,45 +69,47 @@ namespace AsbCloudInfrastructure.Background.PeriodicWorks
if (!detectedOperations.Any()) if (!detectedOperations.Any())
return; return;
var minDate = detectedOperations.FirstOrDefault()?.DateStart; var minDate = detectedOperations.First().DateStart;
var maxDate = detectedOperations.OrderByDescending(d => d.DateEnd).FirstOrDefault()?.DateEnd; var maxDate = detectedOperations.OrderByDescending(d => d.DateEnd).First().DateEnd;
var telemetryDataSaub = (await db.Set<TelemetryDataSaub>() var telemetryDataSaub = await db.Set<TelemetryDataSaub>()
.Where(t => t.IdTelemetry == idTelemetry)
.Where(t => t.DateTime >= minDate) .Where(t => t.DateTime >= minDate)
.Where(t => t.DateTime <= maxDate) .Where(t => t.DateTime <= maxDate)
.Where(t => Math.Abs(t.BitDepth - t.WellDepth) < 0.0001) .Where(t => Math.Abs(t.BitDepth - t.WellDepth) < 0.0001)
.Where(t => telemetryIds.Contains(t.IdTelemetry)) .ToArrayAsync(token);
.ToArrayAsync(token))
.GroupBy(t => t.IdTelemetry)
.ToDictionary(g => g.Key, g => g.OrderBy(t => t.DateTime).ToArray());
CreateDataSaubStat(db, detectedOperations, telemetryDataSaub); if (!telemetryDataSaub.Any())
return;
var dataSaubStats = CreateDataSaubStat(detectedOperations, telemetryDataSaub);
db.Set<DataSaubStat>().AddRange(dataSaubStats);
await db.SaveChangesAsync(token);
} }
private static void CreateDataSaubStat(IAsbCloudDbContext db, DetectedOperation[] detectedOperations, Dictionary<int, TelemetryDataSaub[]> telemetryDataSaub) private static IEnumerable<DataSaubStat> CreateDataSaubStat(IEnumerable<DetectedOperation> detectedOperations, TelemetryDataSaub[] telemetryDataSaub)
{ {
var indexStart = 0; var indexStart = 0;
var indexEnd = 0; var indexEnd = 0;
foreach (var operation in detectedOperations) foreach (var operation in detectedOperations)
{ {
if (!telemetryDataSaub.TryGetValue(operation.IdTelemetry, out TelemetryDataSaub[]? telemetryDataSaubPart)) indexStart = Array.FindIndex(telemetryDataSaub, indexEnd, t => t.DateTime >= operation.DateStart);
break; indexEnd = Array.FindIndex(telemetryDataSaub, indexStart, t => t.DateTime > operation.DateEnd) - 1;
indexStart = Array.FindIndex(telemetryDataSaubPart, indexEnd, t => t.DateTime >= operation.DateStart);
indexEnd = Array.FindIndex(telemetryDataSaubPart, indexStart, t => t.DateTime > operation.DateEnd) - 1;
if (indexStart >= 0 && indexEnd >= indexStart) if (indexStart >= 0 && indexEnd >= indexStart)
{ {
var length = indexEnd - indexStart; var length = indexEnd - indexStart;
var subset = telemetryDataSaubPart.AsSpan(indexStart, length + 1); var subset = telemetryDataSaub.AsSpan(indexStart, length + 1);
CalcStats(operation, subset, db); var stats = CalcStats(operation, subset);
db.SaveChanges(); foreach (var stat in stats)
yield return stat;
} }
} }
} }
private static void CalcStats(DetectedOperation operation, Span<TelemetryDataSaub> telemetryDataSaub, IAsbCloudDbContext db) private static IEnumerable<DataSaubStat> CalcStats(DetectedOperation operation, Span<TelemetryDataSaub> telemetryDataSaub)
{ {
var indexStart = 0; var indexStart = 0;
for (var i = 1; i < telemetryDataSaub.Length; i++) for (var i = 1; i < telemetryDataSaub.Length; i++)
@ -106,8 +122,8 @@ namespace AsbCloudInfrastructure.Background.PeriodicWorks
var length = i - indexStart; var length = i - indexStart;
var span = telemetryDataSaub.Slice(indexStart, length); var span = telemetryDataSaub.Slice(indexStart, length);
indexStart = i; indexStart = i;
var dataSaubStatItem = CalcStat(operation, span); var stat = CalcStat(operation, span);
db.Set<DataSaubStat>().Add(dataSaubStatItem); yield return stat;
} }
} }
} }
@ -162,11 +178,11 @@ namespace AsbCloudInfrastructure.Background.PeriodicWorks
var diffDepthTotal = span[^1].WellDepth - span[0].WellDepth; var diffDepthTotal = span[^1].WellDepth - span[0].WellDepth;
for (var i = 0; i < span.Length - 1; i++) for (var i = 0; i < span.Length - 1; i++)
{ {
var weigth = span[i + 1].WellDepth - span[i].WellDepth; var diffDepth = span[i + 1].WellDepth - span[i].WellDepth;
sumPressure += weigth * span[i].Pressure; sumPressure += diffDepth * span[i].Pressure;
sumAxialLoad += weigth * span[i].AxialLoad; sumAxialLoad += diffDepth * span[i].AxialLoad;
sumRotorTorque += weigth * span[i].RotorTorque; sumRotorTorque += diffDepth * span[i].RotorTorque;
sumRotorSpeed += weigth * span[i].RotorSpeed; sumRotorSpeed += diffDepth * span[i].RotorSpeed;
flow = span[i + 1].Flow > flow ? span[i + 1].Flow ?? 0.0 : flow; flow = span[i + 1].Flow > flow ? span[i + 1].Flow ?? 0.0 : flow;
} }
return ( return (

View File

@ -325,22 +325,28 @@ namespace AsbCloudInfrastructure.Services.SAUB
return data; return data;
} }
public IEnumerable<int>? GetIds(TelemetryDataRequest request) public IEnumerable<int> GetIds(TelemetryDataRequest request)
{ {
var data = new Dictionary<int, TelemetryDataCacheItem>(); var data = caches.Where(i => i.Value.LastData.Count > 0);
if (request.GeDate.HasValue) if (request.GeDate.HasValue)
{ {
data = caches data = data
.Where(item => item.Value.LastData.Any(d => d.DateTime >= request.GeDate.Value.ToRemoteDateTime(item.Value.TimezoneHours))) .Where(item => {
.ToDictionary(item => item.Key, item => item.Value); var lastItem = item.Value.LastData.Last();
var geDate = request.GeDate.Value.ToOffset(TimeSpan.FromHours(item.Value.TimezoneHours));
return lastItem.DateTime >= geDate;
});
} }
if (request.LeDate.HasValue) if (request.LeDate.HasValue)
{ {
data = caches data = data
.Where(item => item.Value.LastData.Any(d => d.DateTime <= request.LeDate.Value.ToRemoteDateTime(item.Value.TimezoneHours))) .Where(item => {
.ToDictionary(item => item.Key, item => item.Value); var firstItem = item.Value.LastData.First();
var leDate = request.LeDate.Value.ToOffset(TimeSpan.FromHours(item.Value.TimezoneHours));
return firstItem.DateTime <= leDate;
});
} }
var telemetryIds = data.Select(item => item.Key); var telemetryIds = data.Select(item => item.Key);