using Microsoft.EntityFrameworkCore;
using Persistence.Database;
using Persistence.Database.Entity;
using Persistence.Models;
using Persistence.Repositories;
namespace Persistence.Repository.Repositories;
///
/// Репозиторий для хранения разных наборов данных временных рядов.
/// idDiscriminator - идентифицирует конкретный набор данных, прим.: циклы измерения АСИБР, или отчет о DrillTest.
/// idDiscriminator формируют клиенты и только им известно что они обозначают.
/// Так как данные приходят редко, то их прореживания для построения графиков не предусмотрено.
///
public class TimestampedSetRepository : ITimestampedSetRepository
{
private readonly IPersistenceDbContext db;
public TimestampedSetRepository(IPersistenceDbContext db)
{
this.db = db;
}
public Task InsertRange(Guid idDiscriminator, IEnumerable sets, CancellationToken token)
{
var entities = sets.Select(set => new TimestampedSet(idDiscriminator, set.Timestamp.ToUniversalTime(), set.Set));
var dbSet = db.Set();
dbSet.AddRange(entities);
return db.SaveChangesAsync(token);
}
public async Task> Get(Guid idDiscriminator, DateTimeOffset? geTimestamp, IEnumerable? props, int skip, int take, CancellationToken token)
{
var dbSet = db.Set();
var query = dbSet.Where(entity => entity.IdDiscriminator == idDiscriminator);
if (geTimestamp.HasValue)
query = ApplyGeTimestamp(query, geTimestamp.Value);
var data = await Materialize(query, token);
if (props is not null && props.Any())
data = ApplyPropsFilter(data, props);
return data;
}
public async Task> GetLast(Guid idDiscriminator, IEnumerable? props, int take, CancellationToken token)
{
var dbSet = db.Set();
var query = dbSet.Where(entity => entity.IdDiscriminator == idDiscriminator);
query = query.OrderByDescending(entity => entity.Timestamp)
.Take(take)
.OrderBy(entity => entity.Timestamp);
var data = await Materialize(query, token);
if (props is not null && props.Any())
data = ApplyPropsFilter(data, props);
return data;
}
public Task Count(Guid idDiscriminator, CancellationToken token)
{
var dbSet = db.Set();
var query = dbSet.Where(entity => entity.IdDiscriminator == idDiscriminator);
return query.CountAsync(token);
}
public async Task GetDatesRange(Guid idDiscriminator, CancellationToken token)
{
var query = db.Set()
.GroupBy(entity => entity.IdDiscriminator)
.Select(group => new
{
Min = group.Min(entity => entity.Timestamp),
Max = group.Max(entity => entity.Timestamp),
});
var item = await query.FirstOrDefaultAsync(token);
if (item is null)
return null;
return new DatesRangeDto
{
From = item.Min,
To = item.Max,
};
}
private static async Task> Materialize(IQueryable query, CancellationToken token)
{
var dtoQuery = query.Select(entity => new TimestampedSetDto(entity.Timestamp, entity.Set));
var dtos = await dtoQuery.ToArrayAsync(token);
return dtos;
}
private static IQueryable ApplyGeTimestamp(IQueryable query, DateTimeOffset geTimestamp)
{
var geTimestampUtc = geTimestamp.ToUniversalTime();
return query.Where(entity => entity.Timestamp >= geTimestampUtc);
}
private static IEnumerable ApplyPropsFilter(IEnumerable query, IEnumerable props)
{
var newQuery = query
.Select(entity => new TimestampedSetDto(
entity.Timestamp,
entity.Set
.Where(prop => props.Contains(prop.Key))
.ToDictionary(prop => prop.Key, prop => prop.Value)
));
return newQuery;
}
}