WorkDataSaubStat избавился от зависимости БД

This commit is contained in:
ngfrolov 2024-02-08 17:33:38 +05:00
parent 7282eb1cfa
commit 7dd92be567
Signed by untrusted user who does not match committer: ng.frolov
GPG Key ID: E99907A0357B29A7
5 changed files with 82 additions and 44 deletions

View File

@ -12,6 +12,18 @@ namespace AsbCloudApp.Services
/// </summary>
public interface ITelemetryDataSaubService : ITelemetryDataService<TelemetryDataSaubDto>
{
/// <summary>
/// Получение телеметрии для РТК статистики
/// </summary>
/// <param name="idTelemetry"></param>
/// <param name="isBitOnBottom"></param>
/// <param name="geDate"></param>
/// <param name="leDate"></param>
/// <param name="take"></param>
/// <param name="token"></param>
/// <returns></returns>
Task<IEnumerable<TelemetryDataSaubDto>> Get(int idTelemetry, bool isBitOnBottom, DateTimeOffset geDate, DateTimeOffset leDate, int take, CancellationToken token);
/// <summary>
/// усредненная статистика по 1м за весь период
/// <para>

View File

@ -1,10 +1,11 @@
using AsbCloudApp.Data;
using AsbCloudApp.Data.DetectedOperation;
using AsbCloudApp.Data.SAUB;
using AsbCloudApp.Repositories;
using AsbCloudApp.Requests;
using AsbCloudApp.Services;
using AsbCloudDb.Model;
using AsbCloudInfrastructure.Services.DetectOperations.Detectors;
using Microsoft.EntityFrameworkCore;
using AsbCloudInfrastructure.Services.DetectOperations;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
@ -19,7 +20,6 @@ namespace AsbCloudInfrastructure.Background.PeriodicWorks
/// </summary>
internal class WorkDataSaubStat : Work
{
private int MechanicalDrillingCategoryId = 4001;
private int Gap = 60;
public WorkDataSaubStat() : base("Generate DataSaubStat entries and save them into Db")
@ -29,7 +29,6 @@ namespace AsbCloudInfrastructure.Background.PeriodicWorks
protected override async Task Action(string id, IServiceProvider services, Action<string, double?> onProgressCallback, CancellationToken token)
{
using var db = services.GetRequiredService<IAsbCloudDbContext>();
var telemetryDataCache = services.GetRequiredService<ITelemetryDataCache<TelemetryDataSaubDto>>();
@ -43,96 +42,102 @@ namespace AsbCloudInfrastructure.Background.PeriodicWorks
return;
var dataSaubStatRepo = services.GetRequiredService<IDataSaubStatRepository>();
var dataSaubService = services.GetRequiredService<ITelemetryDataSaubService>();
var detectedOperationRepository = services.GetRequiredService<IDetectedOperationRepository>();
var stats = await dataSaubStatRepo.GetLastsAsync(idTelemetries, token);
for( var i =0; i < idTelemetries.Length; i++)
{
var idTelemetry = idTelemetries[i];
var lastDate = stats.FirstOrDefault(s => s.IdTelemetry == idTelemetry)?.DateEnd.ToUniversalTime() ?? DateTimeOffset.UnixEpoch;
var statsCount = await CreateStatForTelemetryFromDate(db, idTelemetry, lastDate, dataSaubStatRepo, token);
var statsCount = await CreateStatForTelemetryFromDate(idTelemetry, lastDate, dataSaubService, dataSaubStatRepo, detectedOperationRepository, token);
onProgressCallback($"Calculate stat for telemetry: {idTelemetry}; from {lastDate}; results count: {statsCount};", 100*i / idTelemetries.Length);
}
}
private async Task<int> CreateStatForTelemetryFromDate(IAsbCloudDbContext db, int idTelemetry, DateTimeOffset begin, IDataSaubStatRepository dataSaubStatRepo, CancellationToken token)
private static async Task<int> CreateStatForTelemetryFromDate(
int idTelemetry,
DateTimeOffset begin,
ITelemetryDataSaubService dataSaubService,
IDataSaubStatRepository dataSaubStatRepo,
IDetectedOperationRepository detectedOperationRepository,
CancellationToken token)
{
var detectedOperations = await db.Set<DetectedOperation>()
.Where(o => o.IdTelemetry == idTelemetry)
.Where(o => o.DateStart > begin)
.Where(o => o.OperationCategory.IdParent == MechanicalDrillingCategoryId)
.OrderBy(o => o.DateStart)
.Take(250)
.ToArrayAsync(token);
var detectedOperationRequest = new DetectedOperationByTelemetryRequest {
GeDateStart = begin,
IdTelemetry = idTelemetry,
IdsCategories = WellOperationCategory.MechanicalDrillingSubIds,
SortFields = new[] {nameof(DetectedOperation.DateStart) },
Take = 250,
};
var detectedOperations = await detectedOperationRepository.Get(detectedOperationRequest, token);
if (!detectedOperations.Any())
return 0;
var minDate = detectedOperations.First().DateStart;
var maxDate = detectedOperations.OrderByDescending(d => d.DateEnd).First().DateEnd;
var geDate = detectedOperations.First().DateStart;
var leDate = detectedOperations.OrderByDescending(d => d.DateEnd).First().DateEnd;
var telemetryDataSaub = await db.Set<TelemetryDataSaub>()
.Where(t => t.IdTelemetry == idTelemetry)
.Where(t => t.DateTime >= minDate)
.Where(t => t.DateTime <= maxDate)
.Where(t => Math.Abs(t.BitDepth - t.WellDepth) < 0.0001)
.OrderBy(t => t.DateTime)
.Take(100_000)
.ToArrayAsync(token);
var dataSaub = await dataSaubService.Get(idTelemetry, true, geDate, leDate, 100_000, token);
if (!telemetryDataSaub.Any())
if (!dataSaub.Any())
return 0;
var dataSaubStats = CreateDataSaubStat(detectedOperations, telemetryDataSaub);
if(dataSaub is not TelemetryDataSaubDto[] dataSaubArray)
dataSaubArray = dataSaub.ToArray();
var dataSaubStats = CreateDataSaubStat(detectedOperations, dataSaubArray);
return await dataSaubStatRepo.InsertRangeAsync(dataSaubStats, token);
}
private static IEnumerable<DataSaubStatDto> CreateDataSaubStat(IEnumerable<DetectedOperation> detectedOperations, TelemetryDataSaub[] telemetryDataSaub)
private static IEnumerable<DataSaubStatDto> CreateDataSaubStat(IEnumerable<DetectedOperationDto> detectedOperations, TelemetryDataSaubDto[] dataSaub)
{
var indexStart = 0;
var indexEnd = 0;
var result = new List<DataSaubStatDto>();
if (!telemetryDataSaub.Any())
if (!dataSaub.Any())
return result;
foreach (var operation in detectedOperations)
{
indexStart = Array.FindIndex(telemetryDataSaub, indexEnd, t => t.DateTime >= operation.DateStart);
indexStart = Array.FindIndex(dataSaub, indexEnd, t => t.DateTime >= operation.DateStart);
if (indexStart < 0)
break;
indexEnd = Array.FindIndex(telemetryDataSaub, indexStart, t => t.DateTime > operation.DateEnd);
indexEnd = Array.FindIndex(dataSaub, indexStart, t => t.DateTime > operation.DateEnd);
if (indexEnd < 0)
indexEnd = telemetryDataSaub.Length - 1;
indexEnd = dataSaub.Length - 1;
if (indexEnd == indexStart)
continue;
var length = indexEnd - indexStart;
var subset = telemetryDataSaub.AsSpan(indexStart, length);
var subset = dataSaub.AsSpan(indexStart, length);
var stats = CalcStats(operation, subset);
result.AddRange(stats);
}
return result;
}
private static IEnumerable<DataSaubStatDto> CalcStats(DetectedOperation operation, Span<TelemetryDataSaub> telemetryDataSaub)
private static IEnumerable<DataSaubStatDto> CalcStats(DetectedOperationDto operation, Span<TelemetryDataSaubDto> dataSaub)
{
var result = new List<DataSaubStatDto>();
var indexStart = 0;
for (var i = 1; i < telemetryDataSaub.Length; i++)
for (var i = 1; i < dataSaub.Length; i++)
{
var previous = telemetryDataSaub[i - 1];
var current = telemetryDataSaub[i];
var previous = dataSaub[i - 1];
var current = dataSaub[i];
if (IsNewCacheItem(previous, current) || i == telemetryDataSaub.Length - 1)
if (IsNewCacheItem(previous, current) || i == dataSaub.Length - 1)
{
var length = i - indexStart;
var span = telemetryDataSaub.Slice(indexStart, length);
var span = dataSaub.Slice(indexStart, length);
indexStart = i;
if (length <= 2 || (span[^1].WellDepth - span[0].WellDepth) < 0.001)
continue; // мелкие выборки не учитываем.
@ -144,10 +149,9 @@ namespace AsbCloudInfrastructure.Background.PeriodicWorks
return result;
}
private static DataSaubStatDto CalcStat(DetectedOperation operation, Span<TelemetryDataSaub> span)
private static DataSaubStatDto CalcStat(DetectedOperationDto operation, Span<TelemetryDataSaubDto> span)
{
var hasOscillation = operation.ExtraData.TryGetValue(DetectorDrilling.ExtraDataKeyHasOscillation, out object? hasOscillationObject)
&& hasOscillationObject is true;
var hasOscillation = EnabledSubsystemsFlags.AutoOscillation.HasEnabledSubsystems(operation.EnabledSubsystems);
var aggregatedValues = CalcAggregate(span);
var processMapDrillingCacheItem = new DataSaubStatDto
@ -184,7 +188,7 @@ namespace AsbCloudInfrastructure.Background.PeriodicWorks
double RotorTorque,
double RotorSpeed,
double Flow
) CalcAggregate(Span<TelemetryDataSaub> span)
) CalcAggregate(Span<TelemetryDataSaubDto> span)
{
var sumPressure = 0.0;
var sumAxialLoad = 0.0;
@ -210,7 +214,7 @@ namespace AsbCloudInfrastructure.Background.PeriodicWorks
);
}
private static bool IsNewCacheItem(TelemetryDataSaub previous, TelemetryDataSaub current)
private static bool IsNewCacheItem(TelemetryDataSaubDto previous, TelemetryDataSaubDto current)
{
return !(current.Mode == previous.Mode)
|| !(current.WellDepth >= previous.WellDepth)

View File

@ -6,6 +6,7 @@ using AsbCloudApp.Services;
using AsbCloudDb.Model;
using Mapster;
using Microsoft.EntityFrameworkCore;
using Org.BouncyCastle.Asn1.Pkcs;
using System;
using System.Collections.Generic;
using System.IO;
@ -33,6 +34,29 @@ namespace AsbCloudInfrastructure.Services.SAUB
this.telemetryUserService = telemetryUserService;
}
public async Task<IEnumerable<TelemetryDataSaubDto>> Get(int idTelemetry, bool isBitOnBottom, DateTimeOffset geDate, DateTimeOffset leDate, int take, CancellationToken token)
{
var offset = telemetryService.GetTimezone(idTelemetry).Offset;
var geDateUtc = geDate.ToUniversalTime();
var leDateUtc = leDate.ToUniversalTime();
var query = db.Set<TelemetryDataSaub>()
.Where(t => t.IdTelemetry == idTelemetry)
.Where(t => t.DateTime >= geDateUtc)
.Where(t => t.DateTime <= leDateUtc);
if (isBitOnBottom)
query = query.Where(t => Math.Abs(t.BitDepth - t.WellDepth) < 0.0001);
query = query
.OrderBy(t => t.DateTime)
.Take(take);
var entities = await query.ToArrayAsync(token);
var dtos = entities.Select(e => Convert(e, offset.TotalHours));
return dtos;
}
public async Task<IEnumerable<TelemetryDataSaubStatDto>> GetTelemetryDataStatAsync(int idTelemetry, CancellationToken token)
{
var timezone = telemetryService.GetTimezone(idTelemetry);

View File

@ -10,7 +10,6 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNet.WebApi.Core" Version="5.3.0" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="6.0.26" />
<PackageReference Include="Microsoft.Extensions.ApiDescription.Server" Version="8.0.1">
<PrivateAssets>all</PrivateAssets>

View File

@ -5,7 +5,6 @@ using AsbCloudApp.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Threading;
using System.Threading.Tasks;
using AsbCloudInfrastructure.Services.DetectOperations;