DD.WellWorkover.Cloud/AsbCloudInfrastructure/Services/MessageService.cs
Olga Nemt cdb6ed7390 1. Критические сообщения
2. Рефакторинг кода, связанного с сообщениями
2024-09-10 10:16:31 +05:00

88 lines
3.2 KiB
C#

using AsbCloudApp.Data;
using AsbCloudApp.Repositories;
using AsbCloudApp.Requests;
using AsbCloudApp.Services;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace AsbCloudInfrastructure.Services;
public class MessageService : IMessageService
{
private readonly IMessageRepository messageRepository;
private readonly ITelemetryService telemetryService;
private readonly IEventRepository eventRepository;
public MessageService(
IMessageRepository messageRepository,
ITelemetryService telemetryService,
IEventRepository eventRepository)
{
this.messageRepository = messageRepository;
this.telemetryService = telemetryService;
this.eventRepository = eventRepository;
}
public async Task<PaginationContainer<MessageDto>> GetPaginatedMessagesAsync(MessageRequest request, CancellationToken token)
{
var messageTelemetryRequest = await CreateMessageTelemetryRequest(request, token);
var result = await messageRepository.GetPaginatedMessagesAsync(messageTelemetryRequest, token);
return result;
}
public async Task<IEnumerable<StatCriticalMessageDto>> GetStat(MessageRequest request, CancellationToken token)
{
var messageTelemetryRequest = await CreateMessageTelemetryRequest(request, token);
var telemetryMessages = await messageRepository.GetMessagesAsync(messageTelemetryRequest, token);
var groupedMessages = telemetryMessages.GroupBy(x => new { x.IdTelemetry, x.IdEvent });
var events = messageTelemetryRequest.Events.ToDictionary(e => e.Id);
var result = groupedMessages
.Select(m => new
{
m.Key.IdTelemetry,
events.GetValueOrDefault(m.Key.IdEvent)?.IdCategory,
events.GetValueOrDefault(m.Key.IdEvent)?.MessageTemplate,
Count = m.Count()
})
.Where(m => m.IdCategory != null)
.Where(m => m.MessageTemplate != null)
.GroupBy(m => new { m.IdTelemetry, m.IdCategory, m.MessageTemplate })
.Select(m => new StatCriticalMessageDto()
{
IdTelemetry = m.Key.IdTelemetry,
IdCategory = m.Key.IdCategory,
MessageTemplate = m.Key.MessageTemplate,
MessagesCount = m.Select(z => z.Count).Sum()
});
return result;
}
public async Task<MessageTelemetryRequest> CreateMessageTelemetryRequest(MessageRequest request, CancellationToken token)
{
var telemetries = telemetryService.GetOrDefaultTelemetriesByIdsWells(request.IdsWell);
var telemetryEventRequest = new TelemetryEventRequest()
{
IdsTelemetries = telemetries.Select(t => t.Id),
IdsCategories = request.IdsCategories,
SearchString = request.SearchString,
};
var messageTelemetryRequest = new MessageTelemetryRequest(request);
messageTelemetryRequest.Telemetries = telemetries;
messageTelemetryRequest.Events = await eventRepository.Get(telemetryEventRequest, token);
return messageTelemetryRequest;
}
}