persistence/Persistence.Repository/Repositories/TechMessagesRepository.cs

158 lines
4.8 KiB
C#
Raw Normal View History

using Mapster;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Caching.Memory;
using Newtonsoft.Json.Linq;
using Persistence.Database.Entity;
using Persistence.Models;
2024-12-05 11:30:07 +05:00
using Persistence.Models.Requests;
using Persistence.Repositories;
2024-12-10 10:43:12 +05:00
using UuidExtensions;
namespace Persistence.Repository.Repositories
{
public class TechMessagesRepository : ITechMessagesRepository
{
private readonly IDataSourceSystemRepository sourceSystemRepository;
private DbContext db;
public TechMessagesRepository(DbContext db, IDataSourceSystemRepository sourceSystemRepository)
2024-12-10 10:43:12 +05:00
{
this.db = db;
this.sourceSystemRepository = sourceSystemRepository;
2024-12-10 10:43:12 +05:00
}
protected virtual IQueryable<TechMessage> GetQueryReadOnly() => db.Set<TechMessage>()
.Include(e => e.System);
public async Task<PaginationContainer<TechMessageDto>> GetPage(PaginationRequest request, CancellationToken token)
{
var query = GetQueryReadOnly();
var count = await query.CountAsync(token);
var sort = request.SortSettings != string.Empty
? request.SortSettings!
: nameof(TechMessage.Timestamp);
var entities = await query
.SortBy(sort)
.Skip(request.Skip)
.Take(request.Take)
.ToArrayAsync(token);
var dto = new PaginationContainer<TechMessageDto>()
{
Skip = request.Skip,
Take = request.Take,
Count = count,
Items = entities.Select(e => e.Adapt<TechMessageDto>())
};
return dto;
}
public async Task<IEnumerable<MessagesStatisticDto>> GetStatistics(IEnumerable<Guid> systems, IEnumerable<int> categoryIds, CancellationToken token)
{
var query = GetQueryReadOnly();
var result = await query
.Where(e => systems.Count() == 0 || systems.Contains(e.System.SystemId))
.GroupBy(e => e.System.Name, (key, group) => new
{
System = key,
Categories = group
.Where(g => categoryIds.Count() == 0 || categoryIds.Contains(g.CategoryId))
})
.ToArrayAsync(token);
2024-12-10 10:43:12 +05:00
var entities = new List<MessagesStatisticDto>();
foreach (var e in result)
{
var categories = e.Categories
.GroupBy(g => g.CategoryId)
.ToDictionary(c => c.Key, v => v.Count());
var entity = new MessagesStatisticDto()
{
System = e.System,
Categories = categories
};
entities.Add(entity);
}
return entities;
}
public async Task<int> AddRange(Guid systemId, IEnumerable<TechMessageDto> dtos, Guid userId, CancellationToken token)
{
var entities = new List<TechMessage>();
foreach (var dto in dtos)
{
var entity = dto.Adapt<TechMessage>();
await CreateSystemIfNotExist(systemId, token);
entity.SystemId = systemId;
2024-12-10 10:43:12 +05:00
entities.Add(entity);
}
2024-12-10 10:43:12 +05:00
await db.Set<TechMessage>().AddRangeAsync(entities, token);
var result = await db.SaveChangesAsync(token);
2024-12-10 10:43:12 +05:00
return result;
}
2024-12-10 10:43:12 +05:00
public async Task<IEnumerable<TechMessageDto>> GetPart(DateTimeOffset dateBegin, int take, CancellationToken token)
{
var query = GetQueryReadOnly();
var entities = await query
.Where(e => e.Timestamp >= dateBegin)
.Take(take)
.ToArrayAsync(token);
var dtos = entities
.Select(e => e.Adapt<TechMessageDto>());
2024-12-10 10:43:12 +05:00
return dtos;
}
public async Task<IEnumerable<DataSourceSystemDto>> GetSystems(CancellationToken token)
{
var systems = await sourceSystemRepository.Get(token);
return systems ?? [];
}
public async Task<DatesRangeDto> GetDatesRangeAsync(CancellationToken token)
{
var query = GetQueryReadOnly()
.GroupBy(e => 1)
.Select(group => new
{
Min = group.Min(e => e.Timestamp),
Max = group.Max(e => e.Timestamp),
});
var values = await query.FirstOrDefaultAsync(token);
var result = new DatesRangeDto()
{
From = values?.Min ?? DateTimeOffset.MinValue,
To = values?.Max ?? DateTimeOffset.MaxValue
};
2024-12-10 10:43:12 +05:00
return result;
}
private async Task CreateSystemIfNotExist(Guid systemId, CancellationToken token)
{
var systems = await sourceSystemRepository.Get(token);
var system = systems?.FirstOrDefault(e => e.SystemId == systemId);
if (system == null)
{
system = new DataSourceSystemDto()
{
SystemId = systemId,
Name = string.Empty
};
await sourceSystemRepository.Add(system, token);
}
}
}
}