From f6648b812d4c2c695000e8377dd82f2b61382113 Mon Sep 17 00:00:00 2001 From: Olga Nemt Date: Fri, 22 Nov 2024 16:48:55 +0500 Subject: [PATCH] =?UTF-8?q?=D0=92=20=D0=BC=D0=B5=D1=82=D0=BE=D0=B4=20GetRe?= =?UTF-8?q?sampledData=20=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5=D0=BD?= =?UTF-8?q?=20Cancelation=20token?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/TimeSeriesController.cs | 4 +- .../TimeSeriesDataCachedRepository.cs | 8 +++- .../Repositories/TimeSeriesDataRepository.cs | 38 +++++++------------ Persistence/API/ITimeSeriesBaseDataApi.cs | 6 ++- .../Repositories/ITimeSeriesBaseRepository.cs | 7 +++- 5 files changed, 32 insertions(+), 31 deletions(-) diff --git a/Persistence.API/Controllers/TimeSeriesController.cs b/Persistence.API/Controllers/TimeSeriesController.cs index 99e2d8c..a4a860a 100644 --- a/Persistence.API/Controllers/TimeSeriesController.cs +++ b/Persistence.API/Controllers/TimeSeriesController.cs @@ -33,9 +33,9 @@ public class TimeSeriesController : ControllerBase, ITimeSeriesDataApi GetResampledData(DateTimeOffset dateBegin, double intervalSec = 600d, int approxPointsCount = 1024) + public async Task GetResampledData(DateTimeOffset dateBegin, double intervalSec = 600d, int approxPointsCount = 1024, CancellationToken token = default) { - var result = await this.timeSeriesDataRepository.GetResampledData(dateBegin, intervalSec, approxPointsCount); + var result = await this.timeSeriesDataRepository.GetResampledData(dateBegin, intervalSec, approxPointsCount, token); return Ok(result); } diff --git a/Persistence.Repository/Repositories/TimeSeriesDataCachedRepository.cs b/Persistence.Repository/Repositories/TimeSeriesDataCachedRepository.cs index 52a580c..20dbe00 100644 --- a/Persistence.Repository/Repositories/TimeSeriesDataCachedRepository.cs +++ b/Persistence.Repository/Repositories/TimeSeriesDataCachedRepository.cs @@ -78,12 +78,16 @@ public class TimeSeriesDataCachedRepository : TimeSeriesDataRepos }); } - public override async Task> GetResampledData(DateTimeOffset dateBegin, double intervalSec = 600d, int approxPointsCount = 1024) + public override async Task> GetResampledData( + DateTimeOffset dateBegin, + double intervalSec = 600d, + int approxPointsCount = 1024, + CancellationToken token = default) { var dtos = LastData.Where(i => i.Date >= dateBegin); if (LastData.Count == 0 || LastData[0].Date > dateBegin) { - dtos = await base.GetGtDate(dateBegin, CancellationToken.None); + dtos = await base.GetGtDate(dateBegin, token); } var dateEnd = dateBegin.AddSeconds(intervalSec); diff --git a/Persistence.Repository/Repositories/TimeSeriesDataRepository.cs b/Persistence.Repository/Repositories/TimeSeriesDataRepository.cs index 7db1193..d73caf7 100644 --- a/Persistence.Repository/Repositories/TimeSeriesDataRepository.cs +++ b/Persistence.Repository/Repositories/TimeSeriesDataRepository.cs @@ -33,13 +33,6 @@ 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); @@ -84,26 +77,23 @@ public class TimeSeriesDataRepository : ITimeSeriesDataRepository return dto; } - public virtual Task> GetResampledData(DateTimeOffset dateBegin, double intervalSec = 600d, int approxPointsCount = 1024) + public async virtual Task> GetResampledData( + DateTimeOffset dateBegin, + double intervalSec = 600d, + int approxPointsCount = 1024, + CancellationToken token = default) { - //if (!caches.TryGetValue(idTelemetry, out TelemetryDataCacheItem? cacheItem)) - // return null; + var dtos = await GetGtDate(dateBegin, token); - //var cacheLastData = cacheItem.LastData; + var dateEnd = dateBegin.AddSeconds(intervalSec); + dtos = dtos + .Where(i => i.Date <= dateEnd); - //if (cacheLastData.Count == 0 || cacheLastData[0].DateTime > dateBegin) - // return null; + var ratio = dtos.Count() / approxPointsCount; + if (ratio > 1) + dtos = dtos + .Where((_, index) => index % ratio == 0); - //var dateEnd = dateBegin.AddSeconds(intervalSec); - //var items = cacheLastData - // .Where(i => i.DateTime >= dateBegin && i.DateTime <= dateEnd); - - //var ratio = items.Count() / approxPointsCount; - //if (ratio > 1) - // items = items - // .Where((_, index) => index % ratio == 0); - - //return items; - return null; + return dtos; } } diff --git a/Persistence/API/ITimeSeriesBaseDataApi.cs b/Persistence/API/ITimeSeriesBaseDataApi.cs index dcce959..480f145 100644 --- a/Persistence/API/ITimeSeriesBaseDataApi.cs +++ b/Persistence/API/ITimeSeriesBaseDataApi.cs @@ -15,7 +15,11 @@ public interface ITimeSeriesBaseDataApi /// дата окончания /// /// - Task GetResampledData(DateTimeOffset dateBegin, double intervalSec = 600d, int approxPointsCount = 1024); + Task GetResampledData( + DateTimeOffset dateBegin, + double intervalSec = 600d, + int approxPointsCount = 1024, + CancellationToken token = default); /// /// Получить диапазон дат, для которых есть данные в репозитории diff --git a/Persistence/Repositories/ITimeSeriesBaseRepository.cs b/Persistence/Repositories/ITimeSeriesBaseRepository.cs index b9cf358..7102d97 100644 --- a/Persistence/Repositories/ITimeSeriesBaseRepository.cs +++ b/Persistence/Repositories/ITimeSeriesBaseRepository.cs @@ -12,10 +12,13 @@ public interface ITimeSeriesBaseRepository /// Получить список объектов с прореживанием /// /// дата начала - /// дата окончания /// /// - Task> GetResampledData(DateTimeOffset dateBegin, double intervalSec = 600d, int approxPointsCount = 1024); + Task> GetResampledData( + DateTimeOffset dateBegin, + double intervalSec = 600d, + int approxPointsCount = 1024, + CancellationToken token = default); /// /// Получить диапазон дат, для которых есть данные в репозитории