forked from ddrilling/AsbCloudServer
Приведение записей из t_data_saub_stat ко времени куста
This commit is contained in:
parent
65f04090bf
commit
8bc66a0d85
@ -1,5 +1,6 @@
|
||||
using AsbCloudApp.Data;
|
||||
using AsbCloudApp.Repositories;
|
||||
using AsbCloudApp.Services;
|
||||
using AsbCloudDb.Model;
|
||||
using Mapster;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
@ -14,27 +15,54 @@ namespace AsbCloudInfrastructure.Repository
|
||||
public class DataSaubStatRepository : IDataSaubStatRepository
|
||||
{
|
||||
private readonly IAsbCloudDbContext db;
|
||||
private readonly ITelemetryService telemetryService;
|
||||
|
||||
public DataSaubStatRepository(IAsbCloudDbContext dbContext)
|
||||
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().Adapt<DataSaubStatDto>())
|
||||
.GroupBy(s => s.IdTelemetry, (key, group) => group.OrderByDescending(el => el.DateEnd).First())
|
||||
.ToArrayAsync(token);
|
||||
|
||||
return stats;
|
||||
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 => data.Adapt<DataSaubStat>());
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -9,13 +9,15 @@ 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 DataSaubStatDto()
|
||||
{
|
||||
IdTelemetry = 1,
|
||||
DateEnd = new DateTimeOffset(2024, 1, 1, 20, 25, 0, TimeSpan.Zero),
|
||||
DateStart = new DateTimeOffset(2024, 1, 1, 20, 15, 0, TimeSpan.Zero),
|
||||
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,
|
||||
@ -40,8 +42,8 @@ namespace AsbCloudWebApi.IntegrationTests.Repository
|
||||
new DataSaubStatDto()
|
||||
{
|
||||
IdTelemetry = 1,
|
||||
DateEnd = new DateTimeOffset(2024, 2, 2, 20, 25, 0, TimeSpan.Zero),
|
||||
DateStart = new DateTimeOffset(2024, 2, 2, 20, 15, 0, TimeSpan.Zero),
|
||||
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,
|
||||
@ -87,7 +89,7 @@ namespace AsbCloudWebApi.IntegrationTests.Repository
|
||||
var dbSetSaubStat = dbContext.Set<DataSaubStat>();
|
||||
var dbSetCategories = dbContext.Set<WellOperationCategory>();
|
||||
|
||||
var entities = statDtos.Select(stat => stat.Adapt<DataSaubStat>());
|
||||
var entities = statDtos.Select(stat => ConvertToEntity(stat));
|
||||
|
||||
dbSetCategories.Add(category);
|
||||
dbSetSaubStat.AddRange(entities);
|
||||
@ -113,6 +115,10 @@ namespace AsbCloudWebApi.IntegrationTests.Repository
|
||||
//act
|
||||
var dbSet = dbContext.Set<DataSaubStat>();
|
||||
dbSet.RemoveRange(dbSet);
|
||||
|
||||
var dbSetCategories = dbContext.Set<WellOperationCategory>();
|
||||
dbSetCategories.Add(category);
|
||||
|
||||
dbContext.SaveChanges();
|
||||
|
||||
var result = await dataSaubStatRepository.InsertRangeAsync(statDtos, CancellationToken.None);
|
||||
@ -120,7 +126,7 @@ namespace AsbCloudWebApi.IntegrationTests.Repository
|
||||
//assert
|
||||
Assert.Equal(statDtos.Length, result);
|
||||
|
||||
var statDtosFromDb = dbSet.Select(stat => stat.Adapt<DataSaubStatDto>()).ToArray();
|
||||
var statDtosFromDb = dbSet.Select(stat => ConvertToDto(stat, timeSpan)).ToArray();
|
||||
|
||||
var excludedProps = new[] {
|
||||
nameof(DataSaubStat.Telemetry),
|
||||
@ -136,7 +142,24 @@ namespace AsbCloudWebApi.IntegrationTests.Repository
|
||||
|
||||
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