2024-12-13 09:39:31 +05:00
|
|
|
using Mapster;
|
2024-11-25 13:49:07 +05:00
|
|
|
using Microsoft.EntityFrameworkCore;
|
2024-11-28 08:55:50 +05:00
|
|
|
using Microsoft.Extensions.Caching.Memory;
|
2024-12-12 11:47:52 +05:00
|
|
|
using Newtonsoft.Json.Linq;
|
2024-11-25 13:49:07 +05:00
|
|
|
using Persistence.Database.Entity;
|
|
|
|
using Persistence.Models;
|
2024-12-05 11:30:07 +05:00
|
|
|
using Persistence.Models.Requests;
|
2024-11-25 13:49:07 +05:00
|
|
|
using Persistence.Repositories;
|
2024-12-10 10:43:12 +05:00
|
|
|
using UuidExtensions;
|
2024-11-25 13:49:07 +05:00
|
|
|
|
|
|
|
namespace Persistence.Repository.Repositories
|
|
|
|
{
|
|
|
|
public class TechMessagesRepository : ITechMessagesRepository
|
|
|
|
{
|
2024-12-13 10:54:47 +05:00
|
|
|
private readonly IDataSourceSystemRepository sourceSystemRepository;
|
2024-11-25 13:49:07 +05:00
|
|
|
private DbContext db;
|
2024-11-28 08:55:50 +05:00
|
|
|
|
2024-12-13 10:54:47 +05:00
|
|
|
public TechMessagesRepository(DbContext db, IDataSourceSystemRepository sourceSystemRepository)
|
2024-12-10 10:43:12 +05:00
|
|
|
{
|
|
|
|
this.db = db;
|
2024-12-13 10:54:47 +05:00
|
|
|
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;
|
|
|
|
}
|
2024-11-26 10:23:48 +05:00
|
|
|
|
2024-12-12 11:47:52 +05:00
|
|
|
public async Task<IEnumerable<MessagesStatisticDto>> GetStatistics(IEnumerable<Guid> systems, IEnumerable<int> categoryIds, CancellationToken token)
|
2024-11-25 13:49:07 +05:00
|
|
|
{
|
|
|
|
var query = GetQueryReadOnly();
|
2024-12-02 15:14:00 +05:00
|
|
|
var result = await query
|
2024-12-13 15:30:11 +05:00
|
|
|
.Where(e => !systems.Any() || systems.Contains(e.System.SystemId))
|
|
|
|
.GroupBy(e => e.System.SystemId, (key, group) => new
|
2024-12-02 15:14:00 +05:00
|
|
|
{
|
2024-12-13 15:30:11 +05:00
|
|
|
System = group.FirstOrDefault()!.System.Name,
|
2024-12-02 15:14:00 +05:00
|
|
|
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;
|
|
|
|
}
|
2024-11-25 13:49:07 +05:00
|
|
|
|
2024-12-12 11:47:52 +05:00
|
|
|
public async Task<int> AddRange(Guid systemId, IEnumerable<TechMessageDto> dtos, Guid userId, CancellationToken token)
|
2024-11-25 13:49:07 +05:00
|
|
|
{
|
2024-12-13 15:30:11 +05:00
|
|
|
await CreateSystemIfNotExist(systemId, token);
|
2024-11-28 08:55:50 +05:00
|
|
|
|
2024-12-13 15:30:11 +05:00
|
|
|
var entities = new List<TechMessage>();
|
2024-11-28 13:13:07 +05:00
|
|
|
foreach (var dto in dtos)
|
|
|
|
{
|
|
|
|
var entity = dto.Adapt<TechMessage>();
|
2024-11-28 08:55:50 +05:00
|
|
|
|
2024-12-13 15:30:11 +05:00
|
|
|
await CreateSystemIfNotExist(systemId, token);
|
|
|
|
|
|
|
|
entity.SystemId = systemId;
|
2024-11-28 08:55:50 +05:00
|
|
|
|
2024-12-10 10:43:12 +05:00
|
|
|
entities.Add(entity);
|
|
|
|
}
|
2024-11-25 13:49:07 +05:00
|
|
|
|
2024-12-10 10:43:12 +05:00
|
|
|
await db.Set<TechMessage>().AddRangeAsync(entities, token);
|
|
|
|
var result = await db.SaveChangesAsync(token);
|
2024-11-25 13:49:07 +05:00
|
|
|
|
2024-12-10 10:43:12 +05:00
|
|
|
return result;
|
|
|
|
}
|
2024-11-28 08:55:50 +05:00
|
|
|
|
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-02 15:14:00 +05:00
|
|
|
|
2024-12-10 10:43:12 +05:00
|
|
|
return dtos;
|
|
|
|
}
|
2024-12-02 15:14:00 +05:00
|
|
|
|
2024-12-12 17:05:47 +05:00
|
|
|
public async Task<IEnumerable<DataSourceSystemDto>> GetSystems(CancellationToken token)
|
2024-12-12 11:47:52 +05:00
|
|
|
{
|
2024-12-13 10:54:47 +05:00
|
|
|
var systems = await sourceSystemRepository.Get(token);
|
2024-12-12 11:47:52 +05:00
|
|
|
|
2024-12-13 15:30:11 +05:00
|
|
|
return systems!;
|
2024-12-12 11:47:52 +05:00
|
|
|
}
|
|
|
|
|
2024-12-02 15:14:00 +05:00
|
|
|
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;
|
|
|
|
}
|
2024-12-02 15:14:00 +05:00
|
|
|
|
2024-12-12 11:47:52 +05:00
|
|
|
private async Task CreateSystemIfNotExist(Guid systemId, CancellationToken token)
|
2024-11-28 13:13:07 +05:00
|
|
|
{
|
2024-12-13 10:54:47 +05:00
|
|
|
var systems = await sourceSystemRepository.Get(token);
|
2024-12-12 11:47:52 +05:00
|
|
|
var system = systems?.FirstOrDefault(e => e.SystemId == systemId);
|
2024-11-28 13:13:07 +05:00
|
|
|
|
2024-12-12 11:47:52 +05:00
|
|
|
if (system == null)
|
2024-11-28 08:55:50 +05:00
|
|
|
{
|
2024-12-12 17:05:47 +05:00
|
|
|
system = new DataSourceSystemDto()
|
2024-12-12 11:47:52 +05:00
|
|
|
{
|
|
|
|
SystemId = systemId,
|
|
|
|
Name = string.Empty
|
|
|
|
};
|
2024-12-13 10:54:47 +05:00
|
|
|
await sourceSystemRepository.Add(system, token);
|
|
|
|
}
|
2024-11-28 08:55:50 +05:00
|
|
|
}
|
2024-11-25 13:49:07 +05:00
|
|
|
}
|
|
|
|
}
|