В метод GetResampledData добавлен Cancelation token

This commit is contained in:
Olga Nemt 2024-11-22 16:48:55 +05:00
parent 1fdd199954
commit f6648b812d
5 changed files with 32 additions and 31 deletions

View File

@ -33,9 +33,9 @@ public class TimeSeriesController<TDto> : ControllerBase, ITimeSeriesDataApi<TDt
} }
[HttpGet("resampled")] [HttpGet("resampled")]
public async Task<IActionResult> GetResampledData(DateTimeOffset dateBegin, double intervalSec = 600d, int approxPointsCount = 1024) public async Task<IActionResult> 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); return Ok(result);
} }

View File

@ -78,12 +78,16 @@ public class TimeSeriesDataCachedRepository<TEntity, TDto> : TimeSeriesDataRepos
}); });
} }
public override async Task<IEnumerable<TDto>> GetResampledData(DateTimeOffset dateBegin, double intervalSec = 600d, int approxPointsCount = 1024) public override async Task<IEnumerable<TDto>> GetResampledData(
DateTimeOffset dateBegin,
double intervalSec = 600d,
int approxPointsCount = 1024,
CancellationToken token = default)
{ {
var dtos = LastData.Where(i => i.Date >= dateBegin); var dtos = LastData.Where(i => i.Date >= dateBegin);
if (LastData.Count == 0 || LastData[0].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); var dateEnd = dateBegin.AddSeconds(intervalSec);

View File

@ -33,13 +33,6 @@ public class TimeSeriesDataRepository<TEntity, TDto> : ITimeSeriesDataRepository
public virtual async Task<IEnumerable<TDto>> GetGtDate(DateTimeOffset date, CancellationToken token) public virtual async Task<IEnumerable<TDto>> 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<TDto>());
//return dtos;
var query = this.db.Set<TEntity>().Where(e => e.Date > date); var query = this.db.Set<TEntity>().Where(e => e.Date > date);
var entities = await query.ToArrayAsync(token); var entities = await query.ToArrayAsync(token);
@ -84,26 +77,23 @@ public class TimeSeriesDataRepository<TEntity, TDto> : ITimeSeriesDataRepository
return dto; return dto;
} }
public virtual Task<IEnumerable<TDto>> GetResampledData(DateTimeOffset dateBegin, double intervalSec = 600d, int approxPointsCount = 1024) public async virtual Task<IEnumerable<TDto>> GetResampledData(
DateTimeOffset dateBegin,
double intervalSec = 600d,
int approxPointsCount = 1024,
CancellationToken token = default)
{ {
//if (!caches.TryGetValue(idTelemetry, out TelemetryDataCacheItem? cacheItem)) var dtos = await GetGtDate(dateBegin, token);
// return null;
//var cacheLastData = cacheItem.LastData; var dateEnd = dateBegin.AddSeconds(intervalSec);
dtos = dtos
.Where(i => i.Date <= dateEnd);
//if (cacheLastData.Count == 0 || cacheLastData[0].DateTime > dateBegin) var ratio = dtos.Count() / approxPointsCount;
// return null; if (ratio > 1)
dtos = dtos
.Where((_, index) => index % ratio == 0);
//var dateEnd = dateBegin.AddSeconds(intervalSec); return dtos;
//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;
} }
} }

View File

@ -15,7 +15,11 @@ public interface ITimeSeriesBaseDataApi<TDto>
/// <param name="dateEnd">дата окончания</param> /// <param name="dateEnd">дата окончания</param>
/// <param name="approxPointsCount"></param> /// <param name="approxPointsCount"></param>
/// <returns></returns> /// <returns></returns>
Task<IActionResult> GetResampledData(DateTimeOffset dateBegin, double intervalSec = 600d, int approxPointsCount = 1024); Task<IActionResult> GetResampledData(
DateTimeOffset dateBegin,
double intervalSec = 600d,
int approxPointsCount = 1024,
CancellationToken token = default);
/// <summary> /// <summary>
/// Получить диапазон дат, для которых есть данные в репозитории /// Получить диапазон дат, для которых есть данные в репозитории

View File

@ -12,10 +12,13 @@ public interface ITimeSeriesBaseRepository<TDto>
/// Получить список объектов с прореживанием /// Получить список объектов с прореживанием
/// </summary> /// </summary>
/// <param name="dateBegin">дата начала</param> /// <param name="dateBegin">дата начала</param>
/// <param name="dateEnd">дата окончания</param>
/// <param name="approxPointsCount"></param> /// <param name="approxPointsCount"></param>
/// <returns></returns> /// <returns></returns>
Task<IEnumerable<TDto>> GetResampledData(DateTimeOffset dateBegin, double intervalSec = 600d, int approxPointsCount = 1024); Task<IEnumerable<TDto>> GetResampledData(
DateTimeOffset dateBegin,
double intervalSec = 600d,
int approxPointsCount = 1024,
CancellationToken token = default);
/// <summary> /// <summary>
/// Получить диапазон дат, для которых есть данные в репозитории /// Получить диапазон дат, для которых есть данные в репозитории