2024-11-14 15:17:43 +05:00
|
|
|
|
using Mapster;
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2024-11-18 14:22:09 +05:00
|
|
|
|
using Persistence.Database.Model;
|
2024-11-14 15:17:43 +05:00
|
|
|
|
using Persistence.Models;
|
|
|
|
|
using Persistence.Repositories;
|
|
|
|
|
|
|
|
|
|
namespace Persistence.Repository.Repositories;
|
2024-11-15 16:29:15 +05:00
|
|
|
|
public class TimeSeriesDataRepository<TEntity, TDto> : ITimeSeriesDataRepository<TDto>
|
2024-11-18 14:22:09 +05:00
|
|
|
|
where TEntity : class, ITimestampedData, new()
|
2024-11-14 15:17:43 +05:00
|
|
|
|
where TDto : class, ITimeSeriesAbstractDto, new()
|
|
|
|
|
{
|
|
|
|
|
private DbContext db;
|
|
|
|
|
|
|
|
|
|
public TimeSeriesDataRepository(DbContext db)
|
|
|
|
|
{
|
|
|
|
|
this.db = db;
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-15 16:29:15 +05:00
|
|
|
|
protected virtual IQueryable<TEntity> GetQueryReadOnly() => this.db.Set<TEntity>();
|
2024-11-14 15:17:43 +05:00
|
|
|
|
|
2024-11-21 17:02:36 +05:00
|
|
|
|
public virtual async Task<DatesRangeDto?> GetDatesRange(CancellationToken token)
|
2024-11-14 15:17:43 +05:00
|
|
|
|
{
|
2024-11-18 14:22:09 +05:00
|
|
|
|
var query = GetQueryReadOnly();
|
|
|
|
|
var minDate = await query.MinAsync(o => o.Date, token);
|
|
|
|
|
var maxDate = await query.MaxAsync(o => o.Date, token);
|
|
|
|
|
|
|
|
|
|
return new DatesRangeDto
|
|
|
|
|
{
|
|
|
|
|
From = minDate,
|
|
|
|
|
To = maxDate
|
|
|
|
|
};
|
2024-11-14 15:17:43 +05:00
|
|
|
|
}
|
|
|
|
|
|
2024-11-19 11:32:56 +05:00
|
|
|
|
public virtual async Task<IEnumerable<TDto>> GetGtDate(DateTimeOffset date, CancellationToken token)
|
2024-11-14 15:17:43 +05:00
|
|
|
|
{
|
2024-11-21 17:02:36 +05:00
|
|
|
|
//var query = GetQueryReadOnly()
|
|
|
|
|
// .Where(q => q.Date >= dateBegin);
|
|
|
|
|
//var entities = await query.ToArrayAsync(token);
|
|
|
|
|
//var dtos = entities.Select(e => e.Adapt<TDto>());
|
|
|
|
|
|
|
|
|
|
//return dtos;
|
|
|
|
|
|
2024-11-18 14:22:09 +05:00
|
|
|
|
var query = this.db.Set<TEntity>().Where(e => e.Date > date);
|
|
|
|
|
var entities = await query.ToArrayAsync(token);
|
|
|
|
|
|
|
|
|
|
var dtos = entities.Select(e => e.Adapt<TDto>());
|
|
|
|
|
|
|
|
|
|
return dtos;
|
2024-11-14 15:17:43 +05:00
|
|
|
|
}
|
|
|
|
|
|
2024-11-19 11:32:56 +05:00
|
|
|
|
public virtual async Task<int> InsertRange(IEnumerable<TDto> dtos, CancellationToken token)
|
2024-11-14 15:17:43 +05:00
|
|
|
|
{
|
|
|
|
|
var entities = dtos.Select(d => d.Adapt<TEntity>());
|
|
|
|
|
|
|
|
|
|
await db.Set<TEntity>().AddRangeAsync(entities, token);
|
|
|
|
|
var result = await db.SaveChangesAsync(token);
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
2024-11-19 17:51:51 +05:00
|
|
|
|
|
2024-11-21 17:02:36 +05:00
|
|
|
|
protected async Task<IEnumerable<TDto>> GetLastAsync(int takeCount, CancellationToken token)
|
2024-11-19 17:51:51 +05:00
|
|
|
|
{
|
|
|
|
|
var query = GetQueryReadOnly()
|
|
|
|
|
.OrderByDescending(e => e.Date)
|
|
|
|
|
.Take(takeCount);
|
|
|
|
|
|
|
|
|
|
var entities = await query.ToArrayAsync(token);
|
|
|
|
|
var dtos = entities.Select(e => e.Adapt<TDto>());
|
|
|
|
|
|
|
|
|
|
return dtos;
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-21 17:02:36 +05:00
|
|
|
|
protected async Task<TDto?> GetFirstAsync(CancellationToken token)
|
2024-11-19 17:51:51 +05:00
|
|
|
|
{
|
|
|
|
|
var query = GetQueryReadOnly()
|
|
|
|
|
.OrderBy(e => e.Date);
|
|
|
|
|
|
|
|
|
|
var entity = await query.FirstOrDefaultAsync(token);
|
|
|
|
|
|
2024-11-21 17:02:36 +05:00
|
|
|
|
if (entity == null)
|
2024-11-19 17:51:51 +05:00
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
var dto = entity.Adapt<TDto>();
|
|
|
|
|
return dto;
|
|
|
|
|
}
|
2024-11-21 17:02:36 +05:00
|
|
|
|
|
2024-11-22 15:47:00 +05:00
|
|
|
|
public virtual Task<IEnumerable<TDto>> GetResampledData(DateTimeOffset dateBegin, double intervalSec = 600d, int approxPointsCount = 1024)
|
2024-11-21 17:02:36 +05:00
|
|
|
|
{
|
2024-11-22 15:47:00 +05:00
|
|
|
|
//if (!caches.TryGetValue(idTelemetry, out TelemetryDataCacheItem? cacheItem))
|
|
|
|
|
// return null;
|
|
|
|
|
|
|
|
|
|
//var cacheLastData = cacheItem.LastData;
|
|
|
|
|
|
|
|
|
|
//if (cacheLastData.Count == 0 || cacheLastData[0].DateTime > dateBegin)
|
|
|
|
|
// return null;
|
|
|
|
|
|
|
|
|
|
//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;
|
2024-11-21 17:02:36 +05:00
|
|
|
|
}
|
2024-11-14 15:17:43 +05:00
|
|
|
|
}
|