From 4d24eb944512318a9f97e5af49cbd5c86b5a4f7b Mon Sep 17 00:00:00 2001 From: Olga Nemt Date: Tue, 19 Nov 2024 17:51:51 +0500 Subject: [PATCH] =?UTF-8?q?=D0=A0=D0=B5=D0=BF=D0=BE=D0=B7=D0=B8=D1=82?= =?UTF-8?q?=D0=BE=D1=80=D0=B8=D0=B9=20=D0=B4=D0=BB=D1=8F=20=D0=BA=D0=B5?= =?UTF-8?q?=D1=88=D0=B8=D1=80=D0=BE=D0=B2=D0=B0=D0=BD=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../TimeSeriesDataCachedRepository.cs | 40 ++++++++++--------- .../Repositories/TimeSeriesDataRepository.cs | 26 ++++++++++++ .../Repositories/ITimeSeriesDataRepository.cs | 15 +++++++ 3 files changed, 62 insertions(+), 19 deletions(-) diff --git a/Persistence.Repository/Repositories/TimeSeriesDataCachedRepository.cs b/Persistence.Repository/Repositories/TimeSeriesDataCachedRepository.cs index 765c3ce..4a553ac 100644 --- a/Persistence.Repository/Repositories/TimeSeriesDataCachedRepository.cs +++ b/Persistence.Repository/Repositories/TimeSeriesDataCachedRepository.cs @@ -6,43 +6,45 @@ using Persistence.Models; namespace Persistence.Repository.Repositories; -public class Test { - -} - public class TimeSeriesDataCachedRepository : TimeSeriesDataRepository where TEntity : class, ITimestampedData, new() where TDto : class, ITimeSeriesAbstractDto, new() { - public static TDto FirstByDate { get; set; } = null!; + public static TDto FirstByDate { get; set; } = default!; public static CyclicArray LastData { get; set; } = null!; + private const int CacheItemsCount = 3600; + public TimeSeriesDataCachedRepository(DbContext db) : base(db) { - //Task.Run(async () => { - // //FirstByDate = await base.GetFirstAsync(1, CancellationToken.None); - // //LastData = await base.GetLastAsync(3600, CancellationToken.None); + Task.Run(async () => + { + LastData = new CyclicArray(CacheItemsCount); + + var firstDateItem = await base.GetFirstAsync(CancellationToken.None); + if (firstDateItem == null) + { + return; + } + + FirstByDate = firstDateItem; - //}).wait(); + var dtos = await base.GetLastAsync(CacheItemsCount, CancellationToken.None); + dtos = dtos.OrderBy(d => d.Date); + LastData.AddRange(dtos); + }).Wait(); } public override async Task> GetAsync(DateTimeOffset dateBegin, DateTimeOffset dateEnd, CancellationToken token) { - var cacheLastData = LastData; - if (cacheLastData[0].Date > dateBegin) + if (LastData.Count() == 0 || LastData[0].Date > dateBegin) { var dtos = await base.GetAsync(dateBegin, dateEnd, token); - - dtos = dtos.OrderBy(x => x.Date); - - FirstByDate = dtos.ElementAt(0); - LastData.AddRange(dtos); - return dtos; } - var items = cacheLastData + var items = LastData .Where(i => i.Date >= dateBegin && i.Date <= dateEnd); return items; @@ -56,7 +58,7 @@ public class TimeSeriesDataCachedRepository : TimeSeriesDataRepos dtos = dtos.OrderBy(x => x.Date); - FirstByDate = dtos.ElementAt(0); + FirstByDate = dtos.First(); LastData.AddRange(dtos); } diff --git a/Persistence.Repository/Repositories/TimeSeriesDataRepository.cs b/Persistence.Repository/Repositories/TimeSeriesDataRepository.cs index 6ad643c..9b339a5 100644 --- a/Persistence.Repository/Repositories/TimeSeriesDataRepository.cs +++ b/Persistence.Repository/Repositories/TimeSeriesDataRepository.cs @@ -59,4 +59,30 @@ public class TimeSeriesDataRepository : ITimeSeriesDataRepository return result; } + + public async Task> GetLastAsync(int takeCount, CancellationToken token) + { + var query = GetQueryReadOnly() + .OrderByDescending(e => e.Date) + .Take(takeCount); + + var entities = await query.ToArrayAsync(token); + var dtos = entities.Select(e => e.Adapt()); + + return dtos; + } + + public async Task GetFirstAsync(CancellationToken token) + { + var query = GetQueryReadOnly() + .OrderBy(e => e.Date); + + var entity = await query.FirstOrDefaultAsync(token); + + if(entity == null) + return null; + + var dto = entity.Adapt(); + return dto; + } } diff --git a/Persistence/Repositories/ITimeSeriesDataRepository.cs b/Persistence/Repositories/ITimeSeriesDataRepository.cs index 16aae46..f3e49eb 100644 --- a/Persistence/Repositories/ITimeSeriesDataRepository.cs +++ b/Persistence/Repositories/ITimeSeriesDataRepository.cs @@ -31,4 +31,19 @@ public interface ITimeSeriesDataRepository : ISyncRepository /// /// Task InsertRange(IEnumerable dtos, CancellationToken token); + + /// + /// Получение списка последних записей + /// + /// количество записей + /// + /// + Task> GetLastAsync(int takeCount, CancellationToken token); + + /// + /// Получение первой записи + /// + /// + /// + Task GetFirstAsync(CancellationToken token); }