fix WellOperationRepository.GetSectionsAsync данные из БД запрашиваются одним запросом

This commit is contained in:
ngfrolov 2024-09-04 15:19:09 +05:00
parent 93b3c47053
commit 9dd530a9fb
Signed by untrusted user who does not match committer: ng.frolov
GPG Key ID: E99907A0357B29A7

View File

@ -20,6 +20,7 @@ namespace AsbCloudInfrastructure.Repository;
public class WellOperationRepository : CrudRepositoryBase<WellOperationBaseDto, WellOperation>,
IWellOperationRepository
{
private const string keyCacheTemplate = "OperationsBySectionSummaries_{0}";
private const string cacheKeyWellOperations = "FirstAndLastFactWellsOperations";
private readonly IMemoryCache memoryCache;
private readonly IWellOperationCategoryRepository wellOperationCategoryRepository;
@ -109,24 +110,36 @@ public class WellOperationRepository : CrudRepositoryBase<WellOperationBaseDto,
public async Task<IEnumerable<SectionByOperationsDto>> GetSectionsAsync(IEnumerable<int> idsWells, CancellationToken token)
{
const string keyCacheTemplate = "OperationsBySectionSummaries_{0}";
var result = new List<SectionByOperationsDto>();
var notFoundIds = new List<int>();
foreach (var idWell in idsWells)
{
var sections = await memoryCache.GetOrCreateAsync(string.Format(keyCacheTemplate, idWell), async (entry) =>
var cacheKey = string.Format(keyCacheTemplate, idWell);
if (memoryCache.TryGetValue<IEnumerable<SectionByOperationsDto>>(cacheKey, out var section))
{
entry.AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(30);
result.AddRange(section!);
}
else
{
notFoundIds.Add(idWell);
}
}
if (notFoundIds.Count != 0)
{
var query = dbContext.Set<WellOperation>()
.Where(x => x.IdWell == idWell)
.Where(operation => notFoundIds.Contains( operation.IdWell))
.GroupBy(operation => new
{
operation.IdType, operation.IdWellSectionType, operation.WellSectionType.Caption,
operation.IdWell,
operation.IdType,
operation.IdWellSectionType,
operation.WellSectionType.Caption,
})
.Select(group => new
{
group.Key.IdWell,
group.Key.IdType,
group.Key.IdWellSectionType,
group.Key.Caption,
@ -138,32 +151,38 @@ public class WellOperationRepository : CrudRepositoryBase<WellOperationBaseDto,
.OrderByDescending(operation => operation.DateStart)
.Select(operation => new
{
operation.DateStart, operation.DurationHours, operation.DepthEnd,
operation.DateStart,
operation.DurationHours,
operation.DepthEnd,
})
.First(),
});
var dbData = await query.ToArrayAsync(token);
var sections = dbData.Select(
item => new SectionByOperationsDto
var entities = await query.ToArrayAsync(token);
var dtos = entities.Select(
entity => new SectionByOperationsDto
{
IdWell = idWell,
IdType = item.IdType,
IdWellSectionType = item.IdWellSectionType,
Caption = item.Caption,
DateStart = item.First.DateStart,
DepthStart = item.First.DepthStart,
DateEnd = item.Last.DateStart.AddHours(item.Last.DurationHours),
DepthEnd = item.Last.DepthEnd,
});
IdWell = entity.IdWell,
IdType = entity.IdType,
IdWellSectionType = entity.IdWellSectionType,
Caption = entity.Caption,
DateStart = entity.First.DateStart,
DepthStart = entity.First.DepthStart,
DateEnd = entity.Last.DateStart.AddHours(entity.Last.DurationHours),
DepthEnd = entity.Last.DepthEnd,
})
.ToList();
result.AddRange(dtos);
entry.Value = sections;
return sections;
});
var groupedByWellDtos = dtos
.GroupBy(dto => dto.IdWell);
result.AddRange(sections!);
foreach (var group in groupedByWellDtos)
{
var cacheKey = string.Format(keyCacheTemplate, group.Key);
memoryCache.Set(cacheKey, group.AsEnumerable(), TimeSpan.FromMinutes(30));
}
}
return result;