Репозиторий для работы с данными DataSaubStat

This commit is contained in:
Olga Nemt 2024-02-05 10:47:00 +05:00
parent fa28fdfefe
commit fc0beb6319
4 changed files with 89 additions and 21 deletions

View File

@ -0,0 +1,29 @@
using AsbCloudApp.Data;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
namespace AsbCloudApp.Repositories
{
/// <summary>
/// Репозиторий работы с данными из таблицы t_data_daub_stat
/// </summary>
public interface IDataSaubStatRepository
{
/// <summary>
/// Получение последних дат по телеметриям
/// </summary>
/// <param name="idTelemetries">ключи телеметрий</param>
/// <param name="token"></param>
/// <returns></returns>
Task<IEnumerable<DataSaubStatDto>> GetLastDatesAsync(int[] idTelemetries, CancellationToken token);
/// <summary>
/// Вставка записей статистики
/// </summary>
/// <param name="dataSaubStats"></param>
/// <param name="token"></param>
/// <returns></returns>
Task<int> InsertRangeAsync(IEnumerable<DataSaubStatDto> dataSaubStats, CancellationToken token);
}
}

View File

@ -1,4 +1,5 @@
using AsbCloudApp.Data.SAUB;
using AsbCloudApp.Data;
using AsbCloudApp.Data.SAUB;
using AsbCloudApp.Repositories;
using AsbCloudApp.Requests;
using AsbCloudDb.Model;
@ -29,37 +30,31 @@ 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>>();
var cacheRequest = new TelemetryDataRequest()
{
GeDate = DateTime.UtcNow.AddDays(-Gap)
GeDate = DateTime.UtcNow.AddDays(-Gap*100)
};
var idTelemetries = telemetryDataCache.GetIds(cacheRequest).ToArray();
if (!idTelemetries.Any())
return;
var stats = await db.Set<DataSaubStat>()
.Where(s => idTelemetries.Contains(s.IdTelemetry))
.GroupBy(s => s.IdTelemetry)
.Select(g => new
{
IdTelemetry = g.Key,
DateEnd = g.Max(s => s.DateEnd),
})
.ToArrayAsync(token);
var dataSaubStatRepo = services.GetRequiredService<IDataSaubStatRepository>();
var stats = await dataSaubStatRepo.GetLastDatesAsync(idTelemetries, token);
for( var i =0; i < idTelemetries.Length; i++)
{
var idTelemetry = idTelemetries[i];
var lastDate = stats.FirstOrDefault(s => s.IdTelemetry == idTelemetry)?.DateEnd ?? DateTimeOffset.UnixEpoch;
var statsCount = await CreateStatForTelemetryFromDate(db, idTelemetry, lastDate, token);
var statsCount = await CreateStatForTelemetryFromDate(db, idTelemetry, lastDate, dataSaubStatRepo, 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, CancellationToken token)
private async Task<int> CreateStatForTelemetryFromDate(IAsbCloudDbContext db, int idTelemetry, DateTimeOffset begin, IDataSaubStatRepository dataSaubStatRepo, CancellationToken token)
{
var detectedOperations = await db.Set<DetectedOperation>()
.Where(o => o.IdTelemetry == idTelemetry)
@ -89,15 +84,14 @@ namespace AsbCloudInfrastructure.Background.PeriodicWorks
var dataSaubStats = CreateDataSaubStat(detectedOperations, telemetryDataSaub);
db.Set<DataSaubStat>().AddRange(dataSaubStats);
return await db.SaveChangesAsync(token);
return await dataSaubStatRepo.InsertRangeAsync(dataSaubStats, token);
}
private static IEnumerable<DataSaubStat> CreateDataSaubStat(IEnumerable<DetectedOperation> detectedOperations, TelemetryDataSaub[] telemetryDataSaub)
private static IEnumerable<DataSaubStatDto> CreateDataSaubStat(IEnumerable<DetectedOperation> detectedOperations, TelemetryDataSaub[] telemetryDataSaub)
{
var indexStart = 0;
var indexEnd = 0;
var result = new List<DataSaubStat>();
var result = new List<DataSaubStatDto>();
if (!telemetryDataSaub.Any())
return result;
@ -125,9 +119,9 @@ namespace AsbCloudInfrastructure.Background.PeriodicWorks
return result;
}
private static IEnumerable<DataSaubStat> CalcStats(DetectedOperation operation, Span<TelemetryDataSaub> telemetryDataSaub)
private static IEnumerable<DataSaubStatDto> CalcStats(DetectedOperation operation, Span<TelemetryDataSaub> telemetryDataSaub)
{
var result = new List<DataSaubStat>();
var result = new List<DataSaubStatDto>();
var indexStart = 0;
for (var i = 1; i < telemetryDataSaub.Length; i++)
@ -150,13 +144,13 @@ namespace AsbCloudInfrastructure.Background.PeriodicWorks
return result;
}
private static DataSaubStat CalcStat(DetectedOperation operation, Span<TelemetryDataSaub> span)
private static DataSaubStatDto CalcStat(DetectedOperation operation, Span<TelemetryDataSaub> span)
{
var hasOscillation = operation.ExtraData.TryGetValue(DetectorDrilling.ExtraDataKeyHasOscillation, out object? hasOscillationObject)
&& hasOscillationObject is true;
var aggregatedValues = CalcAggregate(span);
var processMapDrillingCacheItem = new DataSaubStat
var processMapDrillingCacheItem = new DataSaubStatDto
{
DateStart = operation.DateStart,
DateEnd = operation.DateEnd,

View File

@ -222,6 +222,7 @@ namespace AsbCloudInfrastructure
services.AddTransient<WellInfoService>();
services.AddTransient<IHelpPageService, HelpPageService>();
services.AddTransient<IScheduleReportService, ScheduleReportService>();
services.AddTransient<IDataSaubStatRepository, DataSaubStatRepository>();
services.AddTransient<
IChangeLogRepository<ProcessMapPlanDrillingDto, ProcessMapPlanBaseRequestWithWell>,

View File

@ -0,0 +1,44 @@
using AsbCloudApp.Data;
using AsbCloudApp.Repositories;
using AsbCloudDb.Model;
using Mapster;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace AsbCloudInfrastructure.Repository
{
public class DataSaubStatRepository : IDataSaubStatRepository
{
private readonly IAsbCloudDbContext db;
public DataSaubStatRepository(IAsbCloudDbContext dbContext)
{
db = dbContext;
}
public async Task<IEnumerable<DataSaubStatDto>> GetLastDatesAsync(int[] idTelemetries, CancellationToken token)
{
var stats = await db.Set<DataSaubStat>()
.Where(s => idTelemetries.Contains(s.IdTelemetry))
.GroupBy(s => s.IdTelemetry, (key, group) => new DataSaubStatDto()
{
IdTelemetry = key,
DateEnd = group.Max(s => s.DateEnd)
})
.ToArrayAsync(token);
return stats;
}
public async Task<int> InsertRangeAsync(IEnumerable<DataSaubStatDto> dataSaubStats, CancellationToken token)
{
var entities = dataSaubStats.Select(data => data.Adapt<DataSaubStat>());
db.Set<DataSaubStat>().AddRange(entities);
return await db.SaveChangesAsync(token);
}
}
}