using Mapster; using Microsoft.EntityFrameworkCore; using Persistence.Database.Model; using Persistence.Models; using Persistence.Repositories; namespace Persistence.Repository.Repositories; public class TimeSeriesDataRepository : ITimeSeriesDataRepository where TEntity : class, ITimestampedData, new() where TDto : class, ITimeSeriesAbstractDto, new() { private DbContext db; public TimeSeriesDataRepository(DbContext db) { this.db = db; } protected virtual IQueryable GetQueryReadOnly() => this.db.Set(); public virtual async Task GetDatesRange(CancellationToken token) { 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 }; } 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); var dtos = entities.Select(e => e.Adapt()); return dtos; } public virtual async Task InsertRange(IEnumerable dtos, CancellationToken token) { var entities = dtos.Select(d => d.Adapt()); await db.Set().AddRangeAsync(entities, token); var result = await db.SaveChangesAsync(token); return result; } protected async Task> GetLastAsync(int takeCount, CancellationToken token) { var query = GetQueryReadOnly() .OrderByDescending(e => e.Date) .Take(takeCount); var entities = await query.ToArrayAsync(token); var dtos = entities.Select(e => e.Adapt()); return dtos; } protected async Task GetFirstAsync(CancellationToken token) { var query = GetQueryReadOnly() .OrderBy(e => e.Date); var entity = await query.FirstOrDefaultAsync(token); if (entity == null) return null; var dto = entity.Adapt(); return dto; } public virtual Task> GetResampledData(DateTimeOffset dateBegin, double intervalSec = 600d, int approxPointsCount = 1024) { //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; } }