forked from ddrilling/AsbCloudServer
Merge pull request '#28518041 - РТК отчет. Тестирование. Реп. статистики' (#212) from feature/28518041-data-saub-stat-repository into dev
Reviewed-on: http://test.digitaldrilling.ru:8080/DDrilling/AsbCloudServer/pulls/212
This commit is contained in:
commit
91edd50bf6
132
AsbCloudApp/Data/DataSaubStatDto.cs
Normal file
132
AsbCloudApp/Data/DataSaubStatDto.cs
Normal file
@ -0,0 +1,132 @@
|
||||
using System;
|
||||
|
||||
namespace AsbCloudApp.Data
|
||||
{
|
||||
public class DataSaubStatDto
|
||||
{
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public int Id { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Дата и время начала
|
||||
/// </summary>
|
||||
public DateTimeOffset DateStart { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Дата и время окончания
|
||||
/// </summary>
|
||||
public DateTimeOffset DateEnd { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Глубина забоя по стволу начальная
|
||||
/// </summary>
|
||||
public double DepthStart { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Глубина забоя по стволу конечная
|
||||
/// </summary>
|
||||
public double DepthEnd { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Скорость бурения
|
||||
/// </summary>
|
||||
public double Speed { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Ограничение скорости блока
|
||||
/// </summary>
|
||||
public double? BlockSpeedSp { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Давление
|
||||
/// </summary>
|
||||
public double Pressure { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Давление холостого хода
|
||||
/// </summary>
|
||||
public double? PressureIdle { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Ограничение фактического давления
|
||||
/// </summary>
|
||||
public double? PressureSp { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Фактическая нагрузка
|
||||
/// </summary>
|
||||
public double AxialLoad { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Ограничение факт. нагрузки
|
||||
/// </summary>
|
||||
public double? AxialLoadSp { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Максимально допустимая нагрузка
|
||||
/// </summary>
|
||||
public double? AxialLoadLimitMax { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Фактический момент
|
||||
/// </summary>
|
||||
public double RotorTorque { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Ограничение факт. момента
|
||||
/// </summary>
|
||||
public double? RotorTorqueSp { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Максимально допустимый момент
|
||||
/// </summary>
|
||||
public double? RotorTorqueLimitMax { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Работа при достижении ограничения
|
||||
/// </summary>
|
||||
public short? IdFeedRegulator { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Фактическая скорость оборотов ВСП
|
||||
/// </summary>
|
||||
public double RotorSpeed { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Название автоопределённой операции
|
||||
/// </summary>
|
||||
public int IdCategory { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Флаги подсистем
|
||||
/// </summary>
|
||||
public int EnabledSubsystems { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Наличие или отсутствие осцилляции
|
||||
/// </summary>
|
||||
public bool HasOscillation { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Фактический расход
|
||||
/// </summary>
|
||||
public double Flow { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Ключ телеметрии
|
||||
/// </summary>
|
||||
public int IdTelemetry { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Телеметрия
|
||||
/// </summary>
|
||||
public TelemetryDto Telemetry { get; set; } = null!;
|
||||
|
||||
/// <summary>
|
||||
/// Категория автоопределенной операции
|
||||
/// </summary>
|
||||
public WellOperationCategoryDto OperationCategory { get; set; } = null!;
|
||||
}
|
||||
}
|
29
AsbCloudApp/Repositories/IDataSaubStatRepository.cs
Normal file
29
AsbCloudApp/Repositories/IDataSaubStatRepository.cs
Normal 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>> GetLastsAsync(int[] idTelemetries, CancellationToken token);
|
||||
|
||||
/// <summary>
|
||||
/// Вставка записей статистики
|
||||
/// </summary>
|
||||
/// <param name="dataSaubStats"></param>
|
||||
/// <param name="token"></param>
|
||||
/// <returns></returns>
|
||||
Task<int> InsertRangeAsync(IEnumerable<DataSaubStatDto> dataSaubStats, CancellationToken token);
|
||||
}
|
||||
}
|
@ -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,6 +30,7 @@ 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()
|
||||
@ -40,26 +42,19 @@ namespace AsbCloudInfrastructure.Background.PeriodicWorks
|
||||
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.GetLastsAsync(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,
|
||||
|
@ -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>,
|
||||
|
68
AsbCloudInfrastructure/Repository/DataSaubStatRepository.cs
Normal file
68
AsbCloudInfrastructure/Repository/DataSaubStatRepository.cs
Normal file
@ -0,0 +1,68 @@
|
||||
using AsbCloudApp.Data;
|
||||
using AsbCloudApp.Repositories;
|
||||
using AsbCloudApp.Services;
|
||||
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;
|
||||
private readonly ITelemetryService telemetryService;
|
||||
|
||||
public DataSaubStatRepository(IAsbCloudDbContext dbContext, ITelemetryService telemetryService)
|
||||
{
|
||||
db = dbContext;
|
||||
this.telemetryService = telemetryService;
|
||||
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<DataSaubStatDto>> GetLastsAsync(int[] idTelemetries, CancellationToken token)
|
||||
{
|
||||
var timeZoneOffsets = idTelemetries
|
||||
.Distinct()
|
||||
.ToDictionary(idTelemetry => idTelemetry, idTelemetry => TimeSpan.FromHours(telemetryService.GetTimezone(idTelemetry).Hours));
|
||||
|
||||
var stats = await db.Set<DataSaubStat>()
|
||||
.Where(s => idTelemetries.Contains(s.IdTelemetry))
|
||||
.GroupBy(s => s.IdTelemetry, (key, group) => group.OrderByDescending(el => el.DateEnd).First())
|
||||
.ToArrayAsync(token);
|
||||
|
||||
var result = stats.Select(s => ConvertToDto(s, timeZoneOffsets[s.IdTelemetry]));
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public async Task<int> InsertRangeAsync(IEnumerable<DataSaubStatDto> dataSaubStats, CancellationToken token)
|
||||
{
|
||||
var entities = dataSaubStats.Select(data => ConvertToEntity(data));
|
||||
db.Set<DataSaubStat>().AddRange(entities);
|
||||
return await db.SaveChangesAsync(token);
|
||||
}
|
||||
|
||||
private static DataSaubStatDto ConvertToDto(DataSaubStat entity, TimeSpan timeSpan)
|
||||
{
|
||||
var dto = entity.Adapt<DataSaubStatDto>();
|
||||
dto.DateStart = dto.DateStart.ToOffset(timeSpan);
|
||||
dto.DateEnd = dto.DateEnd.ToOffset(timeSpan);
|
||||
|
||||
return dto;
|
||||
}
|
||||
|
||||
private static DataSaubStat ConvertToEntity(DataSaubStatDto dto)
|
||||
{
|
||||
var entity = dto.Adapt<DataSaubStat>();
|
||||
entity.DateStart = dto.DateStart.ToUniversalTime();
|
||||
entity.DateEnd = dto.DateEnd.ToUniversalTime();
|
||||
|
||||
return entity;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,165 @@
|
||||
using AsbCloudApp.Data;
|
||||
using AsbCloudApp.Repositories;
|
||||
using AsbCloudDb.Model;
|
||||
using Mapster;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Xunit;
|
||||
|
||||
namespace AsbCloudWebApi.IntegrationTests.Repository;
|
||||
|
||||
public class DataSaubStatRepositoryTest : BaseIntegrationTest
|
||||
{
|
||||
private static readonly TimeSpan timeSpan = TimeSpan.FromHours(1);
|
||||
|
||||
private static readonly DataSaubStatDto[] statDtos = new DataSaubStatDto[2]
|
||||
{
|
||||
new()
|
||||
{
|
||||
IdTelemetry = 1,
|
||||
DateEnd = new DateTimeOffset(2024, 1, 1, 20, 25, 0, timeSpan),
|
||||
DateStart = new DateTimeOffset(2024, 1, 1, 20, 15, 0, timeSpan),
|
||||
AxialLoad = 10.0,
|
||||
AxialLoadLimitMax = 10.0,
|
||||
AxialLoadSp = 10.0,
|
||||
BlockSpeedSp = 1000,
|
||||
DepthEnd = 10.0,
|
||||
DepthStart = 5.0,
|
||||
EnabledSubsystems = 1,
|
||||
Flow = 10.0,
|
||||
HasOscillation = true,
|
||||
Id = default,
|
||||
IdCategory = 2,
|
||||
IdFeedRegulator = 1,
|
||||
Pressure = 10.0,
|
||||
PressureIdle = 10.0,
|
||||
PressureSp = 10.0,
|
||||
RotorSpeed = 9.0,
|
||||
RotorTorque = 9.0,
|
||||
RotorTorqueSp = 9.0,
|
||||
RotorTorqueLimitMax = 9.0,
|
||||
Speed = 10.0
|
||||
},
|
||||
new()
|
||||
{
|
||||
IdTelemetry = 1,
|
||||
DateEnd = new DateTimeOffset(2024, 2, 2, 20, 25, 0, timeSpan),
|
||||
DateStart = new DateTimeOffset(2024, 2, 2, 20, 15, 0, timeSpan),
|
||||
AxialLoad = 10.0,
|
||||
AxialLoadLimitMax = 10.0,
|
||||
AxialLoadSp = 10.0,
|
||||
BlockSpeedSp = 1000,
|
||||
DepthEnd = 10.0,
|
||||
DepthStart = 5.0,
|
||||
EnabledSubsystems = 1,
|
||||
Flow = 10.0,
|
||||
HasOscillation = true,
|
||||
Id = default,
|
||||
IdCategory = 2,
|
||||
IdFeedRegulator = 1,
|
||||
Pressure = 10.0,
|
||||
PressureIdle = 10.0,
|
||||
PressureSp = 10.0,
|
||||
RotorSpeed = 10.0,
|
||||
RotorTorque = 10.0,
|
||||
RotorTorqueSp = 10.0,
|
||||
RotorTorqueLimitMax = 10.0,
|
||||
Speed = 10.0
|
||||
}
|
||||
};
|
||||
private static readonly WellOperationCategory category = new()
|
||||
{
|
||||
Id = 2,
|
||||
IdParent = null,
|
||||
Name = "Категория 2"
|
||||
};
|
||||
|
||||
private readonly IDataSaubStatRepository dataSaubStatRepository;
|
||||
|
||||
public DataSaubStatRepositoryTest(WebAppFactoryFixture factory) : base(factory)
|
||||
{
|
||||
dataSaubStatRepository = scope.ServiceProvider.GetRequiredService<IDataSaubStatRepository>();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetLastDatesAsync_returns_success()
|
||||
{
|
||||
//prepare
|
||||
dbContext.CleanupDbSet<DataSaubStat>();
|
||||
dbContext.CleanupDbSet<WellOperationCategory>();
|
||||
|
||||
var dbSetSaubStat = dbContext.Set<DataSaubStat>();
|
||||
var dbSetCategories = dbContext.Set<WellOperationCategory>();
|
||||
|
||||
var entities = statDtos.Select(stat => ConvertToEntity(stat));
|
||||
|
||||
dbSetCategories.Add(category);
|
||||
dbContext.SaveChanges();
|
||||
|
||||
dbSetSaubStat.AddRange(entities);
|
||||
dbContext.SaveChanges();
|
||||
|
||||
//act
|
||||
var telemetryIds = statDtos.Select(stat => stat.IdTelemetry).ToArray();
|
||||
var result = await dataSaubStatRepository.GetLastsAsync(telemetryIds, CancellationToken.None);
|
||||
|
||||
var expected = statDtos.Max(stat => stat.DateEnd);
|
||||
var actual = result.First().DateEnd;
|
||||
|
||||
//assert
|
||||
Assert.True((expected - actual).Ticks == 0.0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task InsertRangeAsync_returns_success()
|
||||
{
|
||||
//prepare
|
||||
dbContext.CleanupDbSet<DataSaubStat>();
|
||||
var dbSet = dbContext.Set<DataSaubStat>();
|
||||
|
||||
var dbSetCategories = dbContext.Set<WellOperationCategory>();
|
||||
dbSetCategories.Add(category);
|
||||
|
||||
dbContext.SaveChanges();
|
||||
|
||||
//act
|
||||
var result = await dataSaubStatRepository.InsertRangeAsync(statDtos, CancellationToken.None);
|
||||
|
||||
//assert
|
||||
Assert.Equal(statDtos.Length, result);
|
||||
|
||||
var statDtosFromDb = dbSet.Select(stat => ConvertToDto(stat, timeSpan)).ToArray();
|
||||
|
||||
var excludedProps = new[] {
|
||||
nameof(DataSaubStat.Telemetry),
|
||||
nameof(DataSaubStat.Id),
|
||||
nameof(DataSaubStat.OperationCategory)
|
||||
};
|
||||
foreach (var statDtoFromDb in statDtosFromDb)
|
||||
{
|
||||
var statDto = statDtos
|
||||
.Where(stat => stat.DateStart == statDtoFromDb.DateStart)
|
||||
.Where(stat => stat.DateEnd == statDtoFromDb.DateEnd)
|
||||
.FirstOrDefault();
|
||||
|
||||
MatchHelper.Match(statDtoFromDb, statDto, excludedProps);
|
||||
}
|
||||
}
|
||||
|
||||
private static DataSaubStat ConvertToEntity(DataSaubStatDto stat)
|
||||
{
|
||||
var entity = stat.Adapt<DataSaubStat>();
|
||||
entity.DateStart = entity.DateStart.ToUniversalTime();
|
||||
entity.DateEnd = entity.DateEnd.ToUniversalTime();
|
||||
|
||||
return entity;
|
||||
}
|
||||
|
||||
private static DataSaubStatDto ConvertToDto(DataSaubStat stat, TimeSpan timeSpan)
|
||||
{
|
||||
var dto = stat.Adapt<DataSaubStatDto>();
|
||||
dto.DateStart = dto.DateStart.ToOffset(timeSpan);
|
||||
dto.DateEnd = dto.DateEnd.ToOffset(timeSpan);
|
||||
|
||||
return dto;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user