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); }