From c751f74b35e05f1aa09b9fdf4f24542ea3b83a75 Mon Sep 17 00:00:00 2001 From: Olga Nemt Date: Thu, 21 Nov 2024 17:02:36 +0500 Subject: [PATCH] =?UTF-8?q?=D0=A3=D0=B1=D1=80=D0=B0=D0=BD=20=D1=81=D1=83?= =?UTF-8?q?=D1=84=D1=84=D0=B8=D0=BA=D1=81=20"Async"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/TimeSeriesController.cs | 18 +++++++---- Persistence.API/Program.cs | 3 ++ .../TimeSeriesDataCachedRepository.cs | 27 ++++++++++++---- .../Repositories/TimeSeriesDataRepository.cs | 30 +++++++++-------- Persistence/API/IChangeLogApi.cs | 12 +++---- Persistence/API/IDictionaryElementApi.cs | 8 ++--- Persistence/API/ISetpointApi.cs | 8 ++--- Persistence/API/ISyncApi.cs | 2 +- Persistence/API/ITableDataApi.cs | 2 +- ...phDataApi.cs => ITimeSeriesBaseDataApi.cs} | 10 +++--- Persistence/API/ITimeSeriesDataApi.cs | 14 ++------ Persistence/Models/ChangeLogDto.cs | 2 +- .../Repositories/ISetpointRepository.cs | 6 ++-- .../Repositories/ITableDataRepository.cs | 2 +- ...sitory.cs => ITimeSeriesBaseRepository.cs} | 6 ++-- .../Repositories/ITimeSeriesDataRepository.cs | 32 +------------------ 16 files changed, 85 insertions(+), 97 deletions(-) rename Persistence/API/{IGraphDataApi.cs => ITimeSeriesBaseDataApi.cs} (61%) rename Persistence/Repositories/{IGraphDataRepository.cs => ITimeSeriesBaseRepository.cs} (76%) diff --git a/Persistence.API/Controllers/TimeSeriesController.cs b/Persistence.API/Controllers/TimeSeriesController.cs index e6c74ef..d4491cc 100644 --- a/Persistence.API/Controllers/TimeSeriesController.cs +++ b/Persistence.API/Controllers/TimeSeriesController.cs @@ -19,24 +19,30 @@ public class TimeSeriesController : ControllerBase, ITimeSeriesDataApi GetAsync(DateTimeOffset dateBegin, DateTimeOffset dateEnd, CancellationToken token) + public async Task Get(DateTimeOffset dateBegin, CancellationToken token) { - var result = await this.timeSeriesDataRepository.GetAsync(dateBegin, dateEnd, token); + var result = await this.timeSeriesDataRepository.GetGtDate(dateBegin, token); return Ok(result); - } [HttpGet("datesRange")] - public async Task GetDatesRangeAsync(CancellationToken token) + public async Task GetDatesRange(CancellationToken token) { - var result = await this.timeSeriesDataRepository.GetDatesRangeAsync(token); + var result = await this.timeSeriesDataRepository.GetDatesRange(token); return Ok(result); } + public Task GetResampledData(DateTimeOffset dateBegin, DateTimeOffset dateEnd, int approxPointsCount = 1024) + { + throw new NotImplementedException(); + } + [HttpPost] - public async Task InsertRangeAsync(IEnumerable dtos, CancellationToken token) + public async Task InsertRange(IEnumerable dtos, CancellationToken token) { var result = await this.timeSeriesDataRepository.InsertRange(dtos, token); return Ok(result); } + + } diff --git a/Persistence.API/Program.cs b/Persistence.API/Program.cs index 93e62a1..bc54d92 100644 --- a/Persistence.API/Program.cs +++ b/Persistence.API/Program.cs @@ -1,10 +1,13 @@ +using Persistence.Models; + namespace Persistence.API; public class Program { public static void Main(string[] args) { + var host = CreateHostBuilder(args).Build(); Startup.BeforeRunHandler(host); host.Run(); diff --git a/Persistence.Repository/Repositories/TimeSeriesDataCachedRepository.cs b/Persistence.Repository/Repositories/TimeSeriesDataCachedRepository.cs index 4a553ac..7c9d06e 100644 --- a/Persistence.Repository/Repositories/TimeSeriesDataCachedRepository.cs +++ b/Persistence.Repository/Repositories/TimeSeriesDataCachedRepository.cs @@ -10,8 +10,8 @@ public class TimeSeriesDataCachedRepository : TimeSeriesDataRepos where TEntity : class, ITimestampedData, new() where TDto : class, ITimeSeriesAbstractDto, new() { - public static TDto FirstByDate { get; set; } = default!; - public static CyclicArray LastData { get; set; } = null!; + public static TDto? FirstByDate { get; private set; } + public static CyclicArray LastData { get; } = new CyclicArray(CacheItemsCount); private const int CacheItemsCount = 3600; @@ -19,8 +19,6 @@ public class TimeSeriesDataCachedRepository : TimeSeriesDataRepos { Task.Run(async () => { - LastData = new CyclicArray(CacheItemsCount); - var firstDateItem = await base.GetFirstAsync(CancellationToken.None); if (firstDateItem == null) { @@ -35,17 +33,17 @@ public class TimeSeriesDataCachedRepository : TimeSeriesDataRepos }).Wait(); } - public override async Task> GetAsync(DateTimeOffset dateBegin, DateTimeOffset dateEnd, CancellationToken token) + public override async Task> GetGtDate(DateTimeOffset dateBegin, CancellationToken token) { if (LastData.Count() == 0 || LastData[0].Date > dateBegin) { - var dtos = await base.GetAsync(dateBegin, dateEnd, token); + var dtos = await base.GetGtDate(dateBegin, token); return dtos; } var items = LastData - .Where(i => i.Date >= dateBegin && i.Date <= dateEnd); + .Where(i => i.Date >= dateBegin); return items; } @@ -64,5 +62,20 @@ public class TimeSeriesDataCachedRepository : TimeSeriesDataRepos return result; } + + public override async Task GetDatesRange(CancellationToken token) + { + if (FirstByDate == null) + return null; + + return await Task.Run(() => + { + return new DatesRangeDto + { + From = FirstByDate.Date, + To = LastData[^1].Date + }; + }); + } } diff --git a/Persistence.Repository/Repositories/TimeSeriesDataRepository.cs b/Persistence.Repository/Repositories/TimeSeriesDataRepository.cs index 9b339a5..ec255cb 100644 --- a/Persistence.Repository/Repositories/TimeSeriesDataRepository.cs +++ b/Persistence.Repository/Repositories/TimeSeriesDataRepository.cs @@ -18,16 +18,7 @@ public class TimeSeriesDataRepository : ITimeSeriesDataRepository protected virtual IQueryable GetQueryReadOnly() => this.db.Set(); - public virtual async Task> GetAsync(DateTimeOffset dateBegin, DateTimeOffset dateEnd, CancellationToken token) - { - var query = GetQueryReadOnly(); - var entities = await query.ToArrayAsync(token); - var dtos = entities.Select(e => e.Adapt()); - - return dtos; - } - - public virtual async Task GetDatesRangeAsync(CancellationToken token) + public virtual async Task GetDatesRange(CancellationToken token) { var query = GetQueryReadOnly(); var minDate = await query.MinAsync(o => o.Date, token); @@ -42,6 +33,13 @@ public class TimeSeriesDataRepository : ITimeSeriesDataRepository public virtual async Task> GetGtDate(DateTimeOffset date, CancellationToken token) { + //var query = GetQueryReadOnly() + // .Where(q => q.Date >= dateBegin); + //var entities = await query.ToArrayAsync(token); + //var dtos = entities.Select(e => e.Adapt()); + + //return dtos; + var query = this.db.Set().Where(e => e.Date > date); var entities = await query.ToArrayAsync(token); @@ -60,7 +58,7 @@ public class TimeSeriesDataRepository : ITimeSeriesDataRepository return result; } - public async Task> GetLastAsync(int takeCount, CancellationToken token) + protected async Task> GetLastAsync(int takeCount, CancellationToken token) { var query = GetQueryReadOnly() .OrderByDescending(e => e.Date) @@ -72,17 +70,23 @@ public class TimeSeriesDataRepository : ITimeSeriesDataRepository return dtos; } - public async Task GetFirstAsync(CancellationToken token) + protected async Task GetFirstAsync(CancellationToken token) { var query = GetQueryReadOnly() .OrderBy(e => e.Date); var entity = await query.FirstOrDefaultAsync(token); - if(entity == null) + if (entity == null) return null; var dto = entity.Adapt(); return dto; } + + public Task> GetResampledData(DateTimeOffset dateBegin, DateTimeOffset dateEnd, int approxPointsCount = 1024) + { + //todo + throw new NotImplementedException(); + } } diff --git a/Persistence/API/IChangeLogApi.cs b/Persistence/API/IChangeLogApi.cs index 716944e..bb4a3c3 100644 --- a/Persistence/API/IChangeLogApi.cs +++ b/Persistence/API/IChangeLogApi.cs @@ -31,7 +31,7 @@ public interface IChangeLogApi /// /// /// - Task> AddAsync(TDto dto, CancellationToken token); + Task> Add(TDto dto, CancellationToken token); /// /// Добавить несколько записей @@ -39,7 +39,7 @@ public interface IChangeLogApi /// /// /// - Task> AddRangeAsync(IEnumerable dtos, CancellationToken token); + Task> AddRange(IEnumerable dtos, CancellationToken token); /// /// Обновить одну запись @@ -47,7 +47,7 @@ public interface IChangeLogApi /// /// /// - Task> UpdateAsync(TDto dto, CancellationToken token); + Task> Update(TDto dto, CancellationToken token); /// /// Обновить несколько записей @@ -55,7 +55,7 @@ public interface IChangeLogApi /// /// /// - Task> UpdateRangeAsync(IEnumerable dtos, CancellationToken token); + Task> UpdateRange(IEnumerable dtos, CancellationToken token); /// /// Удалить одну запись @@ -63,7 +63,7 @@ public interface IChangeLogApi /// /// /// - Task> DeleteAsync(int id, CancellationToken token); + Task> Delete(int id, CancellationToken token); /// /// Удалить несколько записей @@ -71,5 +71,5 @@ public interface IChangeLogApi /// /// /// - Task> DeleteRangeAsync(IEnumerable ids, CancellationToken token); + Task> DeleteRange(IEnumerable ids, CancellationToken token); } diff --git a/Persistence/API/IDictionaryElementApi.cs b/Persistence/API/IDictionaryElementApi.cs index 540d454..fac7870 100644 --- a/Persistence/API/IDictionaryElementApi.cs +++ b/Persistence/API/IDictionaryElementApi.cs @@ -13,7 +13,7 @@ public interface IDictionaryElementApi where TDto : class, new() /// ключ справочника /// /// - Task>> GetAsync(Guid dictionaryKey, CancellationToken token); + Task>> Get(Guid dictionaryKey, CancellationToken token); /// /// Добавить элемент в справочник @@ -22,7 +22,7 @@ public interface IDictionaryElementApi where TDto : class, new() /// /// /// - Task> AddAsync(Guid dictionaryKey, TDto dto, CancellationToken token); + Task> Add(Guid dictionaryKey, TDto dto, CancellationToken token); /// /// Изменить одну запись @@ -32,7 +32,7 @@ public interface IDictionaryElementApi where TDto : class, new() /// /// /// - Task> UpdateAsync(Guid dictionaryKey, Guid dictionaryElementKey, TDto dto, CancellationToken token); + Task> Update(Guid dictionaryKey, Guid dictionaryElementKey, TDto dto, CancellationToken token); /// /// Удалить одну запись @@ -41,5 +41,5 @@ public interface IDictionaryElementApi where TDto : class, new() /// ключ элемента в справочнике /// /// - Task> DeleteAsync(Guid dictionaryKey, Guid dictionaryElementKey, CancellationToken token); + Task> Delete(Guid dictionaryKey, Guid dictionaryElementKey, CancellationToken token); } diff --git a/Persistence/API/ISetpointApi.cs b/Persistence/API/ISetpointApi.cs index d086557..7af0895 100644 --- a/Persistence/API/ISetpointApi.cs +++ b/Persistence/API/ISetpointApi.cs @@ -14,7 +14,7 @@ public interface ISetpointApi /// ключи уставок /// /// - Task>> GetCurrentAsync(IEnumerable setpoitKeys, CancellationToken token); + Task>> GetCurrent(IEnumerable setpoitKeys, CancellationToken token); /// /// Получить значения уставок за определенный момент времени @@ -23,7 +23,7 @@ public interface ISetpointApi /// дата, на которую получаем данные /// /// - Task>> GetHistoryAsync(IEnumerable setpoitKeys, DateTimeOffset historyMoment, CancellationToken token); + Task>> GetHistory(IEnumerable setpoitKeys, DateTimeOffset historyMoment, CancellationToken token); /// /// Получить историю изменений значений уставок @@ -31,7 +31,7 @@ public interface ISetpointApi /// ключи уставок /// /// - Task>>> GetLogAsync(IEnumerable setpoitKeys, CancellationToken token); + Task>>> GetLog(IEnumerable setpoitKeys, CancellationToken token); /// /// Метод сохранения уставки @@ -40,5 +40,5 @@ public interface ISetpointApi /// значение /// /// - Task> SaveAsync(Guid setpointKey, object newValue, CancellationToken token); + Task> Save(Guid setpointKey, object newValue, CancellationToken token); } diff --git a/Persistence/API/ISyncApi.cs b/Persistence/API/ISyncApi.cs index 9df4301..7f72812 100644 --- a/Persistence/API/ISyncApi.cs +++ b/Persistence/API/ISyncApi.cs @@ -15,7 +15,7 @@ public interface ISyncApi where TDto : class, new() /// количество записей /// /// - Task>> GetPartAsync(DateTimeOffset dateBegin, int take = 24 * 60 * 60, CancellationToken token = default); + Task>> GetPart(DateTimeOffset dateBegin, int take = 24 * 60 * 60, CancellationToken token = default); /// /// Получить диапазон дат, для которых есть данные в репозитории diff --git a/Persistence/API/ITableDataApi.cs b/Persistence/API/ITableDataApi.cs index ae7099c..a310799 100644 --- a/Persistence/API/ITableDataApi.cs +++ b/Persistence/API/ITableDataApi.cs @@ -19,5 +19,5 @@ public interface ITableDataApi /// параметры фильтрации /// /// - Task>> GetPageAsync(TRequest request, CancellationToken token); + Task>> GetPage(TRequest request, CancellationToken token); } diff --git a/Persistence/API/IGraphDataApi.cs b/Persistence/API/ITimeSeriesBaseDataApi.cs similarity index 61% rename from Persistence/API/IGraphDataApi.cs rename to Persistence/API/ITimeSeriesBaseDataApi.cs index b609f2f..16b7f2b 100644 --- a/Persistence/API/IGraphDataApi.cs +++ b/Persistence/API/ITimeSeriesBaseDataApi.cs @@ -4,23 +4,23 @@ using Persistence.Models; namespace Persistence.API; /// -/// Интерфейс для работы с API графиков +/// Базовый интерфейс для работы с временными рядами /// -public interface IGraphDataApi +public interface ITimeSeriesBaseDataApi { /// - /// Получить список объектов с прореживанием, удовлетворящий диапазону дат + /// Получить список объектов с прореживанием, удовлетворяющий диапазону дат /// /// дата начала /// дата окончания /// /// - Task>> GetThinnedDataAsync(DateTimeOffset dateBegin, DateTimeOffset dateEnd, int approxPointsCount = 1024); + Task GetResampledData(DateTimeOffset dateBegin, DateTimeOffset dateEnd, int approxPointsCount = 1024); /// /// Получить диапазон дат, для которых есть данные в репозитории /// /// /// - Task> GetDatesRangeAsync(CancellationToken token); + Task GetDatesRange(CancellationToken token); } diff --git a/Persistence/API/ITimeSeriesDataApi.cs b/Persistence/API/ITimeSeriesDataApi.cs index a6a0363..a2406aa 100644 --- a/Persistence/API/ITimeSeriesDataApi.cs +++ b/Persistence/API/ITimeSeriesDataApi.cs @@ -11,24 +11,16 @@ namespace Persistence.API; /// /// Интерфейс для работы с API временных данных /// -public interface ITimeSeriesDataApi +public interface ITimeSeriesDataApi : ITimeSeriesBaseDataApi where TDto : class, ITimeSeriesAbstractDto, new() { /// /// Получить список объектов, удовлетворяющий диапазон дат /// /// дата начала - /// дата окончания /// /// - Task GetAsync(DateTimeOffset dateBegin, DateTimeOffset dateEnd, CancellationToken token); - - /// - /// Получить диапазон дат, для которых есть данные в репозитории - /// - /// - /// - Task GetDatesRangeAsync(CancellationToken token); + Task Get(DateTimeOffset dateBegin, CancellationToken token); /// /// Добавление записей @@ -36,7 +28,7 @@ public interface ITimeSeriesDataApi /// /// /// - Task InsertRangeAsync(IEnumerable dtos, CancellationToken token); + Task InsertRange(IEnumerable dtos, CancellationToken token); } diff --git a/Persistence/Models/ChangeLogDto.cs b/Persistence/Models/ChangeLogDto.cs index 5d5e7f5..6b2a090 100644 --- a/Persistence/Models/ChangeLogDto.cs +++ b/Persistence/Models/ChangeLogDto.cs @@ -26,7 +26,7 @@ public class ChangeLogDto where T: class public DateTimeOffset Creation { get; set; } /// - /// Дата устаревания (например при удалении) + /// Дата устаревания (например, при удалении) /// public DateTimeOffset? Obsolete { get; set; } diff --git a/Persistence/Repositories/ISetpointRepository.cs b/Persistence/Repositories/ISetpointRepository.cs index 4253657..1e1e2c3 100644 --- a/Persistence/Repositories/ISetpointRepository.cs +++ b/Persistence/Repositories/ISetpointRepository.cs @@ -15,7 +15,7 @@ public interface ISetpointRepository /// дата, на которую получаем данные /// /// - Task> GetHistoryAsync(IEnumerable setpoitKeys, DateTimeOffset historyMoment, CancellationToken token); + Task> GetHistory(IEnumerable setpoitKeys, DateTimeOffset historyMoment, CancellationToken token); /// /// Получить историю изменений значений уставок @@ -23,7 +23,7 @@ public interface ISetpointRepository /// /// /// - Task>> GetLogAsync(IEnumerable setpoitKeys, CancellationToken token); + Task>> GetLog(IEnumerable setpoitKeys, CancellationToken token); /// /// Метод сохранения уставки @@ -35,5 +35,5 @@ public interface ISetpointRepository /// /// to do /// id User учесть в соответствующем методе репозитория - Task SaveAsync(Guid setpointKey, int idUser, object newValue, CancellationToken token); + Task Save(Guid setpointKey, int idUser, object newValue, CancellationToken token); } diff --git a/Persistence/Repositories/ITableDataRepository.cs b/Persistence/Repositories/ITableDataRepository.cs index 22ec55c..73d332d 100644 --- a/Persistence/Repositories/ITableDataRepository.cs +++ b/Persistence/Repositories/ITableDataRepository.cs @@ -15,5 +15,5 @@ public interface ITableDataRepository /// параметры фильтрации /// /// - Task> GetAsync(TRequest request, CancellationToken token); + Task> Get(TRequest request, CancellationToken token); } diff --git a/Persistence/Repositories/IGraphDataRepository.cs b/Persistence/Repositories/ITimeSeriesBaseRepository.cs similarity index 76% rename from Persistence/Repositories/IGraphDataRepository.cs rename to Persistence/Repositories/ITimeSeriesBaseRepository.cs index d110ffa..5e99582 100644 --- a/Persistence/Repositories/IGraphDataRepository.cs +++ b/Persistence/Repositories/ITimeSeriesBaseRepository.cs @@ -5,7 +5,7 @@ namespace Persistence.Repositories; /// /// Интерфейс по работе с прореженными данными /// -public interface IGraphDataRepository +public interface ITimeSeriesBaseRepository where TDto : class, new() { /// @@ -15,12 +15,12 @@ public interface IGraphDataRepository /// дата окончания /// /// - Task> GetThinnedDataAsync(DateTimeOffset dateBegin, DateTimeOffset dateEnd, int approxPointsCount = 1024); + Task> GetResampledData(DateTimeOffset dateBegin, DateTimeOffset dateEnd, int approxPointsCount = 1024); /// /// Получить диапазон дат, для которых есть данные в репозитории /// /// /// - Task GetDatesRangeAsync(CancellationToken token); + Task GetDatesRange(CancellationToken token); } diff --git a/Persistence/Repositories/ITimeSeriesDataRepository.cs b/Persistence/Repositories/ITimeSeriesDataRepository.cs index f3e49eb..9de7fc7 100644 --- a/Persistence/Repositories/ITimeSeriesDataRepository.cs +++ b/Persistence/Repositories/ITimeSeriesDataRepository.cs @@ -6,24 +6,9 @@ namespace Persistence.Repositories; /// Интерфейс по работе с временными данными /// /// -public interface ITimeSeriesDataRepository : ISyncRepository +public interface ITimeSeriesDataRepository : ISyncRepository, ITimeSeriesBaseRepository where TDto : class, ITimeSeriesAbstractDto, new() { - /// - /// Получить страницу списка объектов - /// - /// дата начала - /// дата окончания - /// - /// - Task> GetAsync(DateTimeOffset dateBegin, DateTimeOffset dateEnd, CancellationToken token); - - /// - /// Получить диапазон дат, для которых есть данные в репозитории - /// - /// - /// - Task GetDatesRangeAsync(CancellationToken token); /// /// Добавление записей /// @@ -31,19 +16,4 @@ public interface ITimeSeriesDataRepository : ISyncRepository /// /// Task InsertRange(IEnumerable dtos, CancellationToken token); - - /// - /// Получение списка последних записей - /// - /// количество записей - /// - /// - Task> GetLastAsync(int takeCount, CancellationToken token); - - /// - /// Получение первой записи - /// - /// - /// - Task GetFirstAsync(CancellationToken token); }