From 787fd478eb5d1a4fa8216d773cd1116dc9d71103 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D1=82=D0=B5=D0=BF=D0=B0=D0=BD=D0=BE=D0=B2=20=D0=94?= =?UTF-8?q?=D0=BC=D0=B8=D1=82=D1=80=D0=B8=D0=B9?= Date: Mon, 2 Sep 2024 18:53:25 +0500 Subject: [PATCH 1/3] =?UTF-8?q?=D0=A4=D0=B8=D0=BA=D1=81=20=D0=BA=D0=BB?= =?UTF-8?q?=D1=8E=D1=87=D0=B0=20=D0=BA=D1=8D=D1=88=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AsbCloudInfrastructure/Repository/WellOperationRepository.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/AsbCloudInfrastructure/Repository/WellOperationRepository.cs b/AsbCloudInfrastructure/Repository/WellOperationRepository.cs index f557d106..d1c47800 100644 --- a/AsbCloudInfrastructure/Repository/WellOperationRepository.cs +++ b/AsbCloudInfrastructure/Repository/WellOperationRepository.cs @@ -109,7 +109,7 @@ public class WellOperationRepository : CrudRepositoryBase> GetSectionsAsync(IEnumerable idsWells, CancellationToken token) { - const string keyCacheSections = "OperationsBySectionSummarties"; + var keyCacheSections = $"OperationsBySectionSummaries_{string.Join('_', idsWells.OrderBy(x => x))}"; var cache = await memoryCache.GetOrCreateAsync(keyCacheSections, async (entry) => { From aa5c67d2e7ee0a46b5dfbcbe11f21a4d12a45099 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D1=82=D0=B5=D0=BF=D0=B0=D0=BD=D0=BE=D0=B2=20=D0=94?= =?UTF-8?q?=D0=BC=D0=B8=D1=82=D1=80=D0=B8=D0=B9?= Date: Wed, 4 Sep 2024 14:06:28 +0500 Subject: [PATCH 2/3] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5=20=D0=B7=D0=B0=D0=BF=D0=B8=D1=81=D0=B5=D0=B9?= =?UTF-8?q?=20=D0=B2=20=D0=BA=D1=8D=D1=88=20=D0=B4=D0=BB=D1=8F=20=D0=BA?= =?UTF-8?q?=D0=B0=D0=B6=D0=B4=D0=BE=D0=B9=20=D1=81=D0=BA=D0=B2=D0=B0=D0=B6?= =?UTF-8?q?=D0=B8=D0=BD=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Repository/WellOperationRepository.cs | 91 +++++++++---------- 1 file changed, 42 insertions(+), 49 deletions(-) diff --git a/AsbCloudInfrastructure/Repository/WellOperationRepository.cs b/AsbCloudInfrastructure/Repository/WellOperationRepository.cs index d1c47800..ba5ffba5 100644 --- a/AsbCloudInfrastructure/Repository/WellOperationRepository.cs +++ b/AsbCloudInfrastructure/Repository/WellOperationRepository.cs @@ -109,71 +109,64 @@ public class WellOperationRepository : CrudRepositoryBase> GetSectionsAsync(IEnumerable idsWells, CancellationToken token) { - var keyCacheSections = $"OperationsBySectionSummaries_{string.Join('_', idsWells.OrderBy(x => x))}"; + const string keyCacheTemplate = "OperationsBySectionSummaries_{0}"; - var cache = await memoryCache.GetOrCreateAsync(keyCacheSections, async (entry) => + var result = new List(); + + foreach (var idWell in idsWells) { - entry.AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(30); + var sections = await memoryCache.GetOrCreateAsync(string.Format(keyCacheTemplate, idWell), async (entry) => + { + entry.AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(30); - var query = dbContext.Set() - .GroupBy(operation => new - { - operation.IdWell, - operation.IdType, - operation.IdWellSectionType, - operation.WellSectionType.Caption, - }) - .Select(group => new - { - group.Key.IdWell, - group.Key.IdType, - group.Key.IdWellSectionType, - group.Key.Caption, + var query = dbContext.Set() + .Where(x => x.IdWell == idWell) + .GroupBy(operation => new + { + operation.IdType, operation.IdWellSectionType, operation.WellSectionType.Caption, + }) + .Select(group => new + { + group.Key.IdType, + group.Key.IdWellSectionType, + group.Key.Caption, + First = group + .OrderBy(operation => operation.DateStart) + .Select(operation => new { operation.DateStart, operation.DepthStart, }) + .First(), + Last = group + .OrderByDescending(operation => operation.DateStart) + .Select(operation => new + { + operation.DateStart, operation.DurationHours, operation.DepthEnd, + }) + .First(), + }); - First = group - .OrderBy(operation => operation.DateStart) - .Select(operation => new - { - operation.DateStart, - operation.DepthStart, - }) - .First(), + var dbData = await query.ToArrayAsync(token); - Last = group - .OrderByDescending(operation => operation.DateStart) - .Select(operation => new - { - operation.DateStart, - operation.DurationHours, - operation.DepthEnd, - }) - .First(), - }) - .Where(s => idsWells.Contains(s.IdWell)); - var dbData = await query.ToArrayAsync(token); - var sections = dbData.Select( + var sections = dbData.Select( item => new SectionByOperationsDto { - IdWell = item.IdWell, + 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, - }) - .ToArray() - .AsEnumerable(); + }); - entry.Value = sections; - return sections; - }); - return cache!; + entry.Value = sections; + return sections; + }); + + result.AddRange(sections!); + } + + return result; } public async Task GetDatesRangeAsync(int idWell, int idType, CancellationToken cancellationToken) From 9dd530a9fbe67ca39b6b9d47a5e7e3c3f2e86f38 Mon Sep 17 00:00:00 2001 From: ngfrolov Date: Wed, 4 Sep 2024 15:19:09 +0500 Subject: [PATCH 3/3] =?UTF-8?q?fix=20WellOperationRepository.GetSectionsAs?= =?UTF-8?q?ync=20=D0=B4=D0=B0=D0=BD=D0=BD=D1=8B=D0=B5=20=D0=B8=D0=B7=20?= =?UTF-8?q?=D0=91=D0=94=20=D0=B7=D0=B0=D0=BF=D1=80=D0=B0=D1=88=D0=B8=D0=B2?= =?UTF-8?q?=D0=B0=D1=8E=D1=82=D1=81=D1=8F=20=D0=BE=D0=B4=D0=BD=D0=B8=D0=BC?= =?UTF-8?q?=20=D0=B7=D0=B0=D0=BF=D1=80=D0=BE=D1=81=D0=BE=D0=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Repository/WellOperationRepository.cs | 91 +++++++++++-------- 1 file changed, 55 insertions(+), 36 deletions(-) diff --git a/AsbCloudInfrastructure/Repository/WellOperationRepository.cs b/AsbCloudInfrastructure/Repository/WellOperationRepository.cs index 3d485db9..92f5844c 100644 --- a/AsbCloudInfrastructure/Repository/WellOperationRepository.cs +++ b/AsbCloudInfrastructure/Repository/WellOperationRepository.cs @@ -20,6 +20,7 @@ namespace AsbCloudInfrastructure.Repository; public class WellOperationRepository : CrudRepositoryBase, IWellOperationRepository { + private const string keyCacheTemplate = "OperationsBySectionSummaries_{0}"; private const string cacheKeyWellOperations = "FirstAndLastFactWellsOperations"; private readonly IMemoryCache memoryCache; private readonly IWellOperationCategoryRepository wellOperationCategoryRepository; @@ -109,61 +110,79 @@ public class WellOperationRepository : CrudRepositoryBase> GetSectionsAsync(IEnumerable idsWells, CancellationToken token) { - const string keyCacheTemplate = "OperationsBySectionSummaries_{0}"; - var result = new List(); + var notFoundIds = new List(); 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>(cacheKey, out var section)) { - entry.AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(30); + result.AddRange(section!); + } + else + { + notFoundIds.Add(idWell); + } + } - var query = dbContext.Set() - .Where(x => x.IdWell == idWell) - .GroupBy(operation => new - { - operation.IdType, operation.IdWellSectionType, operation.WellSectionType.Caption, - }) - .Select(group => new - { - group.Key.IdType, - group.Key.IdWellSectionType, - group.Key.Caption, - First = group + if (notFoundIds.Count != 0) + { + var query = dbContext.Set() + .Where(operation => notFoundIds.Contains( operation.IdWell)) + .GroupBy(operation => new + { + operation.IdWell, + operation.IdType, + operation.IdWellSectionType, + operation.WellSectionType.Caption, + }) + .Select(group => new + { + group.Key.IdWell, + group.Key.IdType, + group.Key.IdWellSectionType, + group.Key.Caption, + First = group .OrderBy(operation => operation.DateStart) .Select(operation => new { operation.DateStart, operation.DepthStart, }) .First(), - Last = group + Last = group .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;