Merge branch 'dev' of http://gitlab.dvgd.oao.rzd/devptk/parr/parr_api into dev
This commit is contained in:
@@ -4,6 +4,8 @@ namespace PARR.Core.Services.RobotSnapshotServices
|
||||
{
|
||||
public interface IRobotSnapshotService
|
||||
{
|
||||
Task<RobotSnapshotItem> CreateAsync(CreateRobotSnapshot robotSnapshot);
|
||||
Task<List<RobotSnapshotItemDto>> GetAsync(RobotSnapshotQuery queryDto);
|
||||
|
||||
Task<RobotSnapshotItemDto> CreateAsync(CreateRobotSnapshot robotSnapshot);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
using AutoMapper;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using PARR.Core.Repositories.Interfaces;
|
||||
using PARR.Core.Repositories.Interfaces.RobotRepositories;
|
||||
using PARR.Domain.DTOs.RobotSnapshotDTO;
|
||||
using PARR.Domain.DTOs.User;
|
||||
using PARR.Domain.Entities.RobotEntities;
|
||||
using PARR.Domain.Exceptions;
|
||||
|
||||
@@ -9,19 +12,63 @@ namespace PARR.Core.Services.RobotSnapshotServices
|
||||
internal class RobotSnapshotService : IRobotSnapshotService
|
||||
{
|
||||
private readonly IRobotSnapshotRepository robotSnapshotRepository;
|
||||
private readonly IUserRepository userRepository;
|
||||
private readonly IMapper mapper;
|
||||
|
||||
public RobotSnapshotService(
|
||||
IRobotSnapshotRepository robotSnapshotRepository,
|
||||
IUserRepository userRepository,
|
||||
IMapper mapper
|
||||
)
|
||||
{
|
||||
this.robotSnapshotRepository = robotSnapshotRepository;
|
||||
this.userRepository = userRepository;
|
||||
this.mapper = mapper;
|
||||
}
|
||||
|
||||
|
||||
public async Task<RobotSnapshotItem> CreateAsync(CreateRobotSnapshot robotSnapshot)
|
||||
public async Task<List<RobotSnapshotItemDto>> GetAsync(RobotSnapshotQuery queryDto)
|
||||
{
|
||||
// Просто возвращаем данные из БД, не проверяем что есть промежутки между которыми не было данных
|
||||
// Т.е это просто чтоб посмотреть что есть в БД
|
||||
|
||||
var minutes = queryDto.Minutes > 0 ? queryDto.Minutes : 60 * 12;
|
||||
|
||||
var dateEnd = DateTimeOffset.UtcNow;
|
||||
var dateStart = dateEnd.AddMinutes(-minutes);
|
||||
|
||||
var query = robotSnapshotRepository.Get().AsNoTracking()
|
||||
.Where(t => dateStart <= t.DateCreated && t.DateCreated <= dateEnd);
|
||||
|
||||
if (!string.IsNullOrEmpty(queryDto.Ip))
|
||||
query = query.Where(t => t.Ip == queryDto.Ip);
|
||||
|
||||
var groupingSnapshots = (await query.ToListAsync()).GroupBy(t => t.Ip);
|
||||
|
||||
|
||||
var robotIps = groupingSnapshots.Select(t => t.Key).ToHashSet();
|
||||
var robotList = await userRepository.Get()
|
||||
.AsNoTracking()
|
||||
.Where(t => robotIps.Contains(t.Ip))
|
||||
.ToDictionaryAsync(t => t.Ip);
|
||||
|
||||
var result = new List<RobotSnapshotItemDto>();
|
||||
|
||||
foreach (var item in groupingSnapshots)
|
||||
{
|
||||
robotList.TryGetValue(item.Key, out var robot);
|
||||
|
||||
var userDto = new UserBaseDto { Ip = item.Key, Description = robot?.Description ?? string.Empty, Name = robot?.Name ?? string.Empty };
|
||||
var snapshots = mapper.Map<List<RobotSnapshotDto>>(item).OrderBy(t => t.DateCreated).ToList();
|
||||
|
||||
result.Add(new RobotSnapshotItemDto { Robot = userDto, Snapshots = snapshots });
|
||||
}
|
||||
|
||||
return result.OrderBy(t => t.Robot.Ip).ThenBy(t => t.Robot.Name).ToList();
|
||||
}
|
||||
|
||||
|
||||
public async Task<RobotSnapshotItemDto> CreateAsync(CreateRobotSnapshot robotSnapshot)
|
||||
{
|
||||
var snapshot = new RobotSnapshot
|
||||
{
|
||||
@@ -38,7 +85,16 @@ namespace PARR.Core.Services.RobotSnapshotServices
|
||||
if (!createdResult || !commitResult)
|
||||
throw new DbErrorException("Ошибка сохранения в БД");
|
||||
|
||||
return mapper.Map<RobotSnapshotItem>(snapshot);
|
||||
var user = await userRepository.Get().AsNoTracking().FirstOrDefaultAsync(t => t.Ip == robotSnapshot.Ip);
|
||||
var userDto = new UserBaseDto { Ip = robotSnapshot.Ip, Description = user?.Description ?? string.Empty, Name = user?.Name ?? string.Empty };
|
||||
|
||||
var result = new RobotSnapshotItemDto
|
||||
{
|
||||
Robot = userDto,
|
||||
Snapshots = mapper.Map<List<RobotSnapshotDto>>(new List<RobotSnapshot> { snapshot })
|
||||
};
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user