В метод 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")]
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);
}

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

View File

@ -33,13 +33,6 @@ public class TimeSeriesDataRepository<TEntity, TDto> : ITimeSeriesDataRepository
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 entities = await query.ToArrayAsync(token);
@ -84,26 +77,23 @@ public class TimeSeriesDataRepository<TEntity, TDto> : ITimeSeriesDataRepository
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))
// 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;
}
}

View File

@ -15,7 +15,11 @@ public interface ITimeSeriesBaseDataApi<TDto>
/// <param name="dateEnd">дата окончания</param>
/// <param name="approxPointsCount"></param>
/// <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>
/// Получить диапазон дат, для которых есть данные в репозитории

View File

@ -12,10 +12,13 @@ public interface ITimeSeriesBaseRepository<TDto>
/// Получить список объектов с прореживанием
/// </summary>
/// <param name="dateBegin">дата начала</param>
/// <param name="dateEnd">дата окончания</param>
/// <param name="approxPointsCount"></param>
/// <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>
/// Получить диапазон дат, для которых есть данные в репозитории