From cdb6ed73905aa8e1b7749a2073f93950c0801ef2 Mon Sep 17 00:00:00 2001 From: Olga Nemt Date: Tue, 10 Sep 2024 10:16:31 +0500 Subject: [PATCH 1/6] =?UTF-8?q?1.=20=D0=9A=D1=80=D0=B8=D1=82=D0=B8=D1=87?= =?UTF-8?q?=D0=B5=D1=81=D0=BA=D0=B8=D0=B5=20=D1=81=D0=BE=D0=BE=D0=B1=D1=89?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D1=8F=202.=20=D0=A0=D0=B5=D1=84=D0=B0=D0=BA?= =?UTF-8?q?=D1=82=D0=BE=D1=80=D0=B8=D0=BD=D0=B3=20=D0=BA=D0=BE=D0=B4=D0=B0?= =?UTF-8?q?,=20=D1=81=D0=B2=D1=8F=D0=B7=D0=B0=D0=BD=D0=BD=D0=BE=D0=B3?= =?UTF-8?q?=D0=BE=20=D1=81=20=D1=81=D0=BE=D0=BE=D0=B1=D1=89=D0=B5=D0=BD?= =?UTF-8?q?=D0=B8=D1=8F=D0=BC=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AsbCloudApp/Data/SAUB/TelemetryMessageDto.cs | 12 ++ AsbCloudApp/Repositories/IEventRepository.cs | 31 +++ .../Repositories/IMessageRepository.cs | 41 ++++ AsbCloudApp/Requests/MessageRequest.cs | 72 +++++-- AsbCloudApp/Requests/TelemetryEventRequest.cs | 28 +++ AsbCloudApp/Services/IEventService.cs | 22 --- AsbCloudApp/Services/IMessageService.cs | 18 +- AsbCloudApp/Services/ITelemetryService.cs | 6 + AsbCloudInfrastructure/DependencyInjection.cs | 3 +- .../Repository/EventRepository.cs | 71 +++++++ .../Services/MessageService.cs | 87 +++++++++ .../Services/SAUB/EventService.cs | 45 ----- ...MessageService.cs => MessageRepository.cs} | 176 ++++++++++-------- .../SAUB/TelemetryDataEditorService.cs | 6 +- .../Services/SAUB/TelemetryService.cs | 28 ++- .../Controllers/SAUB/MessageController.cs | 31 ++- .../Controllers/SAUB/TelemetryController.cs | 20 +- 17 files changed, 489 insertions(+), 208 deletions(-) create mode 100644 AsbCloudApp/Repositories/IEventRepository.cs create mode 100644 AsbCloudApp/Repositories/IMessageRepository.cs create mode 100644 AsbCloudApp/Requests/TelemetryEventRequest.cs delete mode 100644 AsbCloudApp/Services/IEventService.cs create mode 100644 AsbCloudInfrastructure/Repository/EventRepository.cs create mode 100644 AsbCloudInfrastructure/Services/MessageService.cs delete mode 100644 AsbCloudInfrastructure/Services/SAUB/EventService.cs rename AsbCloudInfrastructure/Services/SAUB/{MessageService.cs => MessageRepository.cs} (63%) diff --git a/AsbCloudApp/Data/SAUB/TelemetryMessageDto.cs b/AsbCloudApp/Data/SAUB/TelemetryMessageDto.cs index 05562034..572ab650 100644 --- a/AsbCloudApp/Data/SAUB/TelemetryMessageDto.cs +++ b/AsbCloudApp/Data/SAUB/TelemetryMessageDto.cs @@ -7,6 +7,13 @@ namespace AsbCloudApp.Data.SAUB; /// public class TelemetryMessageDto : IId { + /// + /// + /// + public TelemetryMessageDto() + { + + } /// /// Id сообщения в базе панели оператора /// @@ -51,4 +58,9 @@ public class TelemetryMessageDto : IId /// аргумент №3 для подстановки в шаблон сообщения /// public string? Arg3 { get; set; } + + /// + /// ключ телеметрии + /// + public int IdTelemetry { get; set; } } diff --git a/AsbCloudApp/Repositories/IEventRepository.cs b/AsbCloudApp/Repositories/IEventRepository.cs new file mode 100644 index 00000000..ebe5e500 --- /dev/null +++ b/AsbCloudApp/Repositories/IEventRepository.cs @@ -0,0 +1,31 @@ +using AsbCloudApp.Data.SAUB; +using AsbCloudApp.Requests; +using System.Collections.Generic; +using System.Threading; +using System.Threading.Tasks; + +namespace AsbCloudApp.Repositories; + +/// +/// Репозиторий по работе с событиями +/// +public interface IEventRepository +{ + /// + /// получение списка событий по параметрам запроса + /// + /// параметры запроса + /// + /// + Task> Get(TelemetryEventRequest request, CancellationToken token); + + /// + /// Сохранить. Добавить или заменить. + /// + /// + /// + /// + /// + Task UpsertAsync(string uid, IEnumerable dtos, + CancellationToken token = default); +} diff --git a/AsbCloudApp/Repositories/IMessageRepository.cs b/AsbCloudApp/Repositories/IMessageRepository.cs new file mode 100644 index 00000000..d08eeefc --- /dev/null +++ b/AsbCloudApp/Repositories/IMessageRepository.cs @@ -0,0 +1,41 @@ +using AsbCloudApp.Data; +using AsbCloudApp.Data.SAUB; +using AsbCloudApp.Requests; +using AsbCloudApp.Services; +using System.Collections.Generic; +using System.Threading; +using System.Threading.Tasks; + +namespace AsbCloudApp.Repositories; + +/// +/// Репозиторий по работе с сообщениями панели оператора +/// +public interface IMessageRepository : ITelemetryDataEditorService +{ + /// + /// Получить PaginationContainer с сообщениями по параметрам + /// + /// + /// + /// + Task> GetPaginatedMessagesAsync(MessageTelemetryRequest request, CancellationToken token); + + /// + /// Получить сообщения по параметрам + /// + /// + /// + /// + Task> GetMessagesAsync(MessageTelemetryRequest request, CancellationToken token); + + /// + /// Метод для сохранения сообщения от панели + /// + /// + /// + /// + /// + Task InsertAsync(string uid, IEnumerable dtos, + CancellationToken token); +} \ No newline at end of file diff --git a/AsbCloudApp/Requests/MessageRequest.cs b/AsbCloudApp/Requests/MessageRequest.cs index 1e755231..0d05138a 100644 --- a/AsbCloudApp/Requests/MessageRequest.cs +++ b/AsbCloudApp/Requests/MessageRequest.cs @@ -1,3 +1,5 @@ +using AsbCloudApp.Data; +using AsbCloudApp.Data.SAUB; using System; using System.Collections.Generic; @@ -11,7 +13,7 @@ public class MessageRequestBase : RequestBase /// /// категория /// - public IEnumerable? Categoryids { get; set; } + public IEnumerable? IdsCategories { get; set; } /// @@ -29,29 +31,13 @@ public class MessageRequestBase : RequestBase /// public string? SearchString { get; set; } -} - -/// -/// параметры для запроса списка сообщений (с id скважины) -/// -public class MessageRequest : MessageRequestBase -{ - /// - /// id скважины - /// - public int IdWell { get; set; } - - /// - /// параметры для запроса списка сообщений (с id скважины) + /// копирующий конструктор /// /// - /// - public MessageRequest(MessageRequestBase request, int idWell) + public MessageRequestBase(MessageRequestBase request) { - this.IdWell = idWell; - - this.Categoryids = request.Categoryids; + this.IdsCategories = request.IdsCategories; this.Begin = request.Begin; this.End = request.End; this.SearchString = request.SearchString; @@ -62,3 +48,49 @@ public class MessageRequest : MessageRequestBase } } + +/// +/// параметры для запроса списка сообщений (с ids скважин) +/// +public class MessageRequest : MessageRequestBase +{ + /// + /// ids скважин + /// + public IEnumerable IdsWell { get; set; } = null!; + + /// + /// параметры для запроса списка сообщений (с ids скважин) + /// + /// + /// + public MessageRequest(MessageRequestBase request, IEnumerable idsWell) : base(request) + { + this.IdsWell = idsWell; + } +} + +/// +/// параметры запроса для получения списка сообщений телеметрии +/// +public class MessageTelemetryRequest : MessageRequestBase +{ + /// + /// события + /// + public IEnumerable Events { get; set; } = null!; + + /// + /// телеметрии + /// + public IEnumerable Telemetries { get; set; } = null!; + + /// + /// + /// + /// + public MessageTelemetryRequest(MessageRequestBase request) : base(request) + { + } +} + diff --git a/AsbCloudApp/Requests/TelemetryEventRequest.cs b/AsbCloudApp/Requests/TelemetryEventRequest.cs new file mode 100644 index 00000000..a9018f9b --- /dev/null +++ b/AsbCloudApp/Requests/TelemetryEventRequest.cs @@ -0,0 +1,28 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AsbCloudApp.Requests; + +/// +/// параметры запроса для получения событий телеметрии +/// +public class TelemetryEventRequest +{ + /// + /// ключи телеметрии + /// + public IEnumerable IdsTelemetries { get; set; } = null!; + + /// + /// ключи категорий + /// + public IEnumerable? IdsCategories { get; set; } + + /// + /// строка поиска + /// + public string? SearchString { get; set; } +} diff --git a/AsbCloudApp/Services/IEventService.cs b/AsbCloudApp/Services/IEventService.cs deleted file mode 100644 index a376b649..00000000 --- a/AsbCloudApp/Services/IEventService.cs +++ /dev/null @@ -1,22 +0,0 @@ -using AsbCloudApp.Data.SAUB; -using System.Collections.Generic; -using System.Threading; -using System.Threading.Tasks; - -namespace AsbCloudApp.Services; - -/// -/// Сервис сохранения списка сообщений от панелей -/// -public interface IEventService -{ - /// - /// Сохранить. Добавить или заменить. - /// - /// - /// - /// - /// - Task UpsertAsync(string uid, IEnumerable dtos, - CancellationToken token = default); -} diff --git a/AsbCloudApp/Services/IMessageService.cs b/AsbCloudApp/Services/IMessageService.cs index 5a5f4d17..92fd7864 100644 --- a/AsbCloudApp/Services/IMessageService.cs +++ b/AsbCloudApp/Services/IMessageService.cs @@ -1,5 +1,4 @@ using AsbCloudApp.Data; -using AsbCloudApp.Data.SAUB; using AsbCloudApp.Requests; using System.Collections.Generic; using System.Threading; @@ -8,25 +7,24 @@ using System.Threading.Tasks; namespace AsbCloudApp.Services; /// -/// Сервис сообщений панели оператора +/// Сервис сообщений /// -public interface IMessageService : ITelemetryDataEditorService +public interface IMessageService { /// - /// Получить сообщения по параметрам + /// Получить статистику сообщений по параметрам запроса /// /// /// /// - Task> GetMessagesAsync(MessageRequest request, CancellationToken token); + Task> GetStat(MessageRequest request, CancellationToken token); /// - /// Метод для сохранения сообщения от панели + /// Получить PaginationContainer с сообщениями по параметрам /// - /// - /// + /// /// /// - Task InsertAsync(string uid, IEnumerable dtos, - CancellationToken token); + Task> GetPaginatedMessagesAsync(MessageRequest request, CancellationToken token); + } \ No newline at end of file diff --git a/AsbCloudApp/Services/ITelemetryService.cs b/AsbCloudApp/Services/ITelemetryService.cs index c1b4ba99..02f93461 100644 --- a/AsbCloudApp/Services/ITelemetryService.cs +++ b/AsbCloudApp/Services/ITelemetryService.cs @@ -47,6 +47,12 @@ public interface ITelemetryService /// TelemetryBaseDto? GetOrDefaultTelemetryByIdWell(int idWell); + /// + /// получить список телеметрии по ключам скважин + /// + /// ключи скважин + /// + IEnumerable GetOrDefaultTelemetriesByIdsWells(IEnumerable idsWells); /// /// получить диапазон дат за которые есть данные /// diff --git a/AsbCloudInfrastructure/DependencyInjection.cs b/AsbCloudInfrastructure/DependencyInjection.cs index 7e8d5867..b3d06d6d 100644 --- a/AsbCloudInfrastructure/DependencyInjection.cs +++ b/AsbCloudInfrastructure/DependencyInjection.cs @@ -291,9 +291,10 @@ public static class DependencyInjection services.AddTransient(); services.AddTransient(); services.AddTransient(); - services.AddTransient(); + services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); diff --git a/AsbCloudInfrastructure/Repository/EventRepository.cs b/AsbCloudInfrastructure/Repository/EventRepository.cs new file mode 100644 index 00000000..8d3665a4 --- /dev/null +++ b/AsbCloudInfrastructure/Repository/EventRepository.cs @@ -0,0 +1,71 @@ +using AsbCloudApp.Data.SAUB; +using AsbCloudApp.Repositories; +using AsbCloudApp.Requests; +using AsbCloudApp.Services; +using AsbCloudDb; +using AsbCloudDb.Model; +using Microsoft.Extensions.Caching.Memory; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; + +namespace AsbCloudInfrastructure.Repository; + + +public class EventRepository : IEventRepository +{ + private readonly IAsbCloudDbContext db; + private readonly IMemoryCache memoryCache; + private readonly ITelemetryService telemetryService; + + public EventRepository(IAsbCloudDbContext db, IMemoryCache memoryCache, ITelemetryService telemetryService) + { + this.db = db; + this.memoryCache = memoryCache; + this.telemetryService = telemetryService; + } + + public async Task> Get(TelemetryEventRequest request, CancellationToken token) + { + var allEvents = await memoryCache.GetOrCreateBasicAsync(db.Set(), token); + var events = allEvents.Where(e => request.IdsTelemetries.Contains(e.IdTelemetry)); + + if (request.IdsCategories?.Any() == true) + events = events.Where(e => request.IdsCategories.Contains(e.IdCategory)); + + if (!string.IsNullOrEmpty(request.SearchString)) + events = events.Where(e => e.MessageTemplate.Contains(request.SearchString, StringComparison.OrdinalIgnoreCase)); + + var dtos = events.Select(e => + { + var dto = new TelemetryEventDto(); + dto.Id = e.IdEvent; + dto.IdCategory = e.IdCategory; + dto.MessageTemplate = e.MessageTemplate; + return dto; + }); + + return dtos; + } + + public async Task UpsertAsync(string uid, IEnumerable dtos, + CancellationToken token = default) + { + if (!dtos.Any()) + return; + + var telemetry = telemetryService.GetOrCreateTelemetryByUid(uid); + + var entities = dtos.Select(dto => new TelemetryEvent + { + IdEvent = dto.Id, + IdTelemetry = telemetry.Id, + IdCategory = dto.IdCategory, + MessageTemplate = dto.Message + }); + var result = await db.Database.ExecInsertOrUpdateAsync(db.TelemetryEvents, entities, token); + memoryCache.DropBasic(); + } +} diff --git a/AsbCloudInfrastructure/Services/MessageService.cs b/AsbCloudInfrastructure/Services/MessageService.cs new file mode 100644 index 00000000..f71b5c4a --- /dev/null +++ b/AsbCloudInfrastructure/Services/MessageService.cs @@ -0,0 +1,87 @@ +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> GetPaginatedMessagesAsync(MessageRequest request, CancellationToken token) + { + var messageTelemetryRequest = await CreateMessageTelemetryRequest(request, token); + + var result = await messageRepository.GetPaginatedMessagesAsync(messageTelemetryRequest, token); + return result; + } + + public async Task> 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 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; + } +} diff --git a/AsbCloudInfrastructure/Services/SAUB/EventService.cs b/AsbCloudInfrastructure/Services/SAUB/EventService.cs deleted file mode 100644 index d33dc502..00000000 --- a/AsbCloudInfrastructure/Services/SAUB/EventService.cs +++ /dev/null @@ -1,45 +0,0 @@ -using AsbCloudApp.Data.SAUB; -using AsbCloudApp.Services; -using AsbCloudDb; -using AsbCloudDb.Model; -using Microsoft.Extensions.Caching.Memory; -using System.Collections.Generic; -using System.Linq; -using System.Threading; -using System.Threading.Tasks; - -namespace AsbCloudInfrastructure.Services.SAUB; - - -public class EventService : IEventService -{ - private readonly IAsbCloudDbContext db; - private readonly IMemoryCache memoryCache; - private readonly ITelemetryService telemetryService; - - public EventService(IAsbCloudDbContext db, IMemoryCache memoryCache, ITelemetryService telemetryService) - { - this.db = db; - this.memoryCache = memoryCache; - this.telemetryService = telemetryService; - } - - public async Task UpsertAsync(string uid, IEnumerable dtos, - CancellationToken token = default) - { - if (!dtos.Any()) - return; - - var telemetry = telemetryService.GetOrCreateTelemetryByUid(uid); - - var entities = dtos.Select(dto => new TelemetryEvent - { - IdEvent = dto.Id, - IdTelemetry = telemetry.Id, - IdCategory = dto.IdCategory, - MessageTemplate = dto.Message - }); - var result = await db.Database.ExecInsertOrUpdateAsync(db.TelemetryEvents, entities, token); - memoryCache.DropBasic(); - } -} diff --git a/AsbCloudInfrastructure/Services/SAUB/MessageService.cs b/AsbCloudInfrastructure/Services/SAUB/MessageRepository.cs similarity index 63% rename from AsbCloudInfrastructure/Services/SAUB/MessageService.cs rename to AsbCloudInfrastructure/Services/SAUB/MessageRepository.cs index 5d6555bc..4b9d36c4 100644 --- a/AsbCloudInfrastructure/Services/SAUB/MessageService.cs +++ b/AsbCloudInfrastructure/Services/SAUB/MessageRepository.cs @@ -1,12 +1,17 @@ using AsbCloudApp.Data; using AsbCloudApp.Data.SAUB; +using AsbCloudApp.Repositories; using AsbCloudApp.Requests; using AsbCloudApp.Services; using AsbCloudDb; using AsbCloudDb.Model; +using DocumentFormat.OpenXml.Bibliography; using Mapster; using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Internal; using Microsoft.Extensions.Caching.Memory; +using Microsoft.Extensions.Logging; +using Microsoft.IdentityModel.Tokens; using System; using System.Collections.Generic; using System.Linq; @@ -16,20 +21,31 @@ using System.Threading.Tasks; namespace AsbCloudInfrastructure.Services.SAUB; -public class MessageService : IMessageService +public class MessageRepository : IMessageRepository { private readonly IAsbCloudDbContext db; private readonly IMemoryCache memoryCache; private readonly ITelemetryService telemetryService; - public MessageService(IAsbCloudDbContext db, IMemoryCache memoryCache, ITelemetryService telemetryService) + public MessageRepository(IAsbCloudDbContext db, IMemoryCache memoryCache, ITelemetryService telemetryService) { this.db = db; this.memoryCache = memoryCache; this.telemetryService = telemetryService; } - public async Task> GetMessagesAsync(MessageRequest request, CancellationToken token) + public async Task> GetMessagesAsync(MessageTelemetryRequest request, CancellationToken token) + { + var query = BuildQuery(request, token); + + var entities = await query.ToArrayAsync(token); + + var dtos = entities.Select(m => m.Adapt()); + + return dtos; + } + + public async Task> GetPaginatedMessagesAsync(MessageTelemetryRequest request, CancellationToken token) { var result = new PaginationContainer { @@ -37,36 +53,86 @@ public class MessageService : IMessageService Take = request.Take ?? 32, }; - var telemetry = telemetryService.GetOrDefaultTelemetryByIdWell(request.IdWell); - if (telemetry is null) + if (request.Telemetries.IsNullOrEmpty() || request.Events.IsNullOrEmpty()) return result; - var allEvents = await memoryCache.GetOrCreateBasicAsync(db.Set(), token); - var events = allEvents.Where(e => e.IdTelemetry == telemetry.Id); + var query = BuildQuery(request, token); - if (!events.Any()) - return result; - - var query = db.TelemetryMessages.Where(m => m.IdTelemetry == telemetry.Id) - .OrderBy(m => m.DateTime).AsNoTracking(); - - if (request.Categoryids?.Any() == true || !string.IsNullOrEmpty(request.SearchString)) + if (request.SortFields?.Any() == true) { - if (!string.IsNullOrEmpty(request.SearchString)) - events = events.Where(e => e.MessageTemplate.Contains(request.SearchString, StringComparison.OrdinalIgnoreCase)); - - if (request.Categoryids?.Any() == true) - events = events.Where(e => request.Categoryids.ToList().Contains(e.IdCategory)); - - var eventIds = events.Select(e => e.IdEvent); - - if (!eventIds.Any()) - return result; - - query = query.Where(m => eventIds.Contains(m.IdEvent)); + query = query.SortBy(request.SortFields); } - query = query.OrderByDescending(m => m.DateTime); + result.Count = query.Count(); + + var messagesList = await query + .Skip(result.Skip) + .Take(result.Take) + .AsNoTracking() + .ToArrayAsync(token); + + if (messagesList.Count() == 0) + return result; + + var allUsers = await memoryCache.GetOrCreateBasicAsync(db.Set(), token); + var users = allUsers.Where(u => request.Telemetries!.Select(t => t.Id).Contains(u.IdTelemetry)); + + if (!request.Events.Any()) + return result; + + var eventsDict = request.Events.ToDictionary(x => x.Id); + var usersDict = users.ToDictionary(x => x.IdUser, x => x); + + var messagesDtoList = new List(); + + foreach (var message in messagesList) + { + var messageDto = new MessageDto + { + Id = message.Id, + WellDepth = message.WellDepth, + }; + + var telemetry = request.Telemetries.Where(t => t.Id == message.IdTelemetry).FirstOrDefault(); + + if(telemetry != null && telemetry.TimeZone != null) + messageDto.DateTime = message.DateTime.ToOffset(TimeSpan.FromHours(telemetry.TimeZone.Hours)); + + if (message.IdTelemetryUser is not null) + { + if (usersDict.TryGetValue((int)message.IdTelemetryUser, out TelemetryUser? user)) + { + messageDto.User = user.MakeDisplayName(); + } + else + messageDto.User = message.IdTelemetryUser.ToString(); + } + + if (eventsDict.TryGetValue(message.IdEvent, out TelemetryEventDto? e)) + { + messageDto.CategoryId = e.IdCategory; + messageDto.Message = e.MakeMessageText([ + message.Arg0, + message.Arg1, + message.Arg2, + message.Arg3 + ]); + } + messagesDtoList.Add(messageDto); + } + + result.Items = result.Items.Concat(messagesDtoList); + + return result; + } + public IQueryable BuildQuery(MessageTelemetryRequest request, CancellationToken token) + { + var idsTelemetries = request.Telemetries.Select(t => t.Id); + var eventIds = request.Events.Select(e => e.Id); + + var query = db.TelemetryMessages + .Where(m => idsTelemetries.Contains(m.IdTelemetry)) + .Where(m => eventIds.Contains(m.IdEvent)); if (request.Begin is not null) { @@ -80,63 +146,13 @@ public class MessageService : IMessageService query = query.Where(m => m.DateTime <= endUtc); } - result.Count = query.Count(); + query = query.OrderByDescending(m => m.DateTime); - if (request.SortFields?.Any() == true) - { - query = query.SortBy(request.SortFields); - } - var messagesList = await query.Skip(result.Skip) - .Take(result.Take).AsNoTracking() - .ToListAsync(token).ConfigureAwait(false); - - if (messagesList.Count == 0) - return result; - - var allUsers = await memoryCache.GetOrCreateBasicAsync(db.Set(), token); - var users = allUsers.Where(u => u.IdTelemetry == telemetry.Id); - - var eventsDict = events.ToDictionary(x => x.IdEvent, x => x); - var usersDict = users.ToDictionary(x => x.IdUser, x => x); - - var messagesDtoList = new List(); - var timezone = telemetryService.GetTimezone(telemetry.Id); - - foreach (var message in messagesList) - { - var messageDto = new MessageDto - { - Id = message.Id, - WellDepth = message.WellDepth - }; - - messageDto.DateTime = message.DateTime.ToOffset(TimeSpan.FromHours(timezone.Hours)); - - if (message.IdTelemetryUser is not null) - { - if (usersDict.TryGetValue((int)message.IdTelemetryUser, out TelemetryUser? user)) - { - messageDto.User = user.MakeDisplayName(); - } - else - messageDto.User = message.IdTelemetryUser.ToString(); - } - - if (eventsDict.TryGetValue(message.IdEvent, out TelemetryEvent? e)) - { - messageDto.CategoryId = e.IdCategory; - messageDto.Message = e.MakeMessageText(message); - } - messagesDtoList.Add(messageDto); - } - - result.Items = result.Items.Concat(messagesDtoList); - - return result; + return query; } public Task InsertAsync(string uid, IEnumerable dtos, - CancellationToken token = default) + CancellationToken token = default) { if (!dtos.Any()) return Task.CompletedTask; diff --git a/AsbCloudInfrastructure/Services/SAUB/TelemetryDataEditorService.cs b/AsbCloudInfrastructure/Services/SAUB/TelemetryDataEditorService.cs index 207dc744..442a68f6 100644 --- a/AsbCloudInfrastructure/Services/SAUB/TelemetryDataEditorService.cs +++ b/AsbCloudInfrastructure/Services/SAUB/TelemetryDataEditorService.cs @@ -21,7 +21,7 @@ public class TelemetryDataEditorService : ITelemetryDataEditorService /// /// /// - /// + /// /// /// /// @@ -36,7 +36,7 @@ public class TelemetryDataEditorService : ITelemetryDataEditorService ITelemetryDataSaubService dataSaubService, ITelemetryDataService dataSpinService, IDataSaubStatRepository dataSaubStatRepository, - IMessageService messageService, + IMessageRepository messageRepository, IDrillTestRepository drillTestRepository, ILimitingParameterRepository limitingParameterRepository, IDetectedOperationRepository detectedOperationRepository, @@ -54,7 +54,7 @@ public class TelemetryDataEditorService : ITelemetryDataEditorService dataSaubService, dataSpinService, dataSaubStatRepository, - messageService, + messageRepository, drillTestRepository, limitingParameterRepository, detectedOperationRepository, diff --git a/AsbCloudInfrastructure/Services/SAUB/TelemetryService.cs b/AsbCloudInfrastructure/Services/SAUB/TelemetryService.cs index 6de56bdc..521fd8dd 100644 --- a/AsbCloudInfrastructure/Services/SAUB/TelemetryService.cs +++ b/AsbCloudInfrastructure/Services/SAUB/TelemetryService.cs @@ -144,15 +144,27 @@ public class TelemetryService : ITelemetryService public TelemetryBaseDto? GetOrDefaultTelemetryByIdWell(int idWell) { - var entity = GetTelemetryCache() - .FirstOrDefault(t => t.Well?.Id == idWell); + var dto = GetOrDefaultTelemetriesByIdsWells([idWell]) + .FirstOrDefault(); - if (entity?.Well?.Timezone is not null && entity.TimeZone.Hours != entity.Well.Timezone.Hours) - { - entity.TimeZone = entity.Well.Timezone; - //TODO: выдаем предупреждение! - } - return entity?.Adapt(); + return dto; + } + + public IEnumerable GetOrDefaultTelemetriesByIdsWells(IEnumerable idsWells) + { + var entities = GetTelemetryCache() + .Where(t => t.Well != null) + .Where(t => idsWells.Contains(t.Well!.Id)) + .Select(t => { + t.TimeZone = t.TimeZone.Hours != t.Well!.Timezone.Hours ? t.Well.Timezone : t.TimeZone; + + return t; + }); + + + var dtos = entities.Select(t => t.Adapt()); + + return dtos; } public TelemetryDto GetOrCreateTelemetryByUid(string uid) diff --git a/AsbCloudWebApi/Controllers/SAUB/MessageController.cs b/AsbCloudWebApi/Controllers/SAUB/MessageController.cs index bc89b861..1867be94 100644 --- a/AsbCloudWebApi/Controllers/SAUB/MessageController.cs +++ b/AsbCloudWebApi/Controllers/SAUB/MessageController.cs @@ -1,7 +1,10 @@ using AsbCloudApp.Data; +using AsbCloudApp.Repositories; using AsbCloudApp.Requests; using AsbCloudApp.Services; +using AsbCloudDb.Model; using Microsoft.AspNetCore.Mvc; +using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; @@ -14,13 +17,18 @@ namespace AsbCloudWebApi.Controllers.SAUB; [ApiController] public class MessageController : ControllerBase { - private readonly IMessageService messageService; + private readonly IMessageRepository messageRepository; private readonly IWellService wellService; + private readonly IMessageService messageService; - public MessageController(IMessageService messageService, IWellService wellService) + public MessageController( + IMessageRepository messageRepository, + IWellService wellService, + IMessageService messageService) { - this.messageService = messageService; + this.messageRepository = messageRepository; this.wellService = wellService; + this.messageService = messageService; } /// @@ -46,17 +54,22 @@ public class MessageController : ControllerBase if (request.Take > 1024) return this.ValidationBadRequest(nameof(request.Take), "limit mast be less then 1024"); - var requestToService = new MessageRequest(request, idWell); - - var result = await messageService.GetMessagesAsync( - requestToService, - token) - .ConfigureAwait(false); + var messageRequest = new MessageRequest(request, new int[] { idWell }); + var result = await messageService.GetPaginatedMessagesAsync(messageRequest, token); if (result is null || result.Count == 0) return NoContent(); return Ok(result); + + } + + [HttpGet("/api/serviceOperation/[controller]")] + [ProducesResponseType(typeof(IEnumerable), (int)System.Net.HttpStatusCode.OK)] + public async Task Get([FromQuery] MessageRequest request, CancellationToken token) + { + var result = await messageService.GetStat(request, token); + return Ok(result); } /// diff --git a/AsbCloudWebApi/Controllers/SAUB/TelemetryController.cs b/AsbCloudWebApi/Controllers/SAUB/TelemetryController.cs index 0adbba81..c04b230c 100644 --- a/AsbCloudWebApi/Controllers/SAUB/TelemetryController.cs +++ b/AsbCloudWebApi/Controllers/SAUB/TelemetryController.cs @@ -1,11 +1,11 @@ using AsbCloudApp.Data.SAUB; +using AsbCloudApp.Repositories; using AsbCloudApp.Services; using AsbCloudWebApi.SignalR; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.SignalR; using System; using System.Collections.Generic; -using System.IO; using System.Linq; using System.Threading; using System.Threading.Tasks; @@ -20,21 +20,21 @@ namespace AsbCloudWebApi.Controllers.SAUB; public class TelemetryController : ControllerBase { private readonly ITelemetryService telemetryService; - private readonly IMessageService messageService; - private readonly IEventService eventService; + private readonly IMessageRepository messageRepository; + private readonly IEventRepository eventRepository; private readonly ITelemetryUserService telemetryUserService; private readonly IHubContext telemetryHubContext; public TelemetryController( ITelemetryService telemetryService, - IMessageService messageService, - IEventService eventService, + IMessageRepository messageRepository, + IEventRepository eventRepository, ITelemetryUserService telemetryUserService, IHubContext telemetryHubContext) { this.telemetryService = telemetryService; - this.messageService = messageService; - this.eventService = eventService; + this.messageRepository = messageRepository; + this.eventRepository = eventRepository; this.telemetryUserService = telemetryUserService; this.telemetryHubContext = telemetryHubContext; } @@ -49,7 +49,7 @@ public class TelemetryController : ControllerBase { var from = DateTimeOffset.UtcNow.AddDays(-1); var stream = await telemetryService.GetTelemetriesInfoByLastData(from, token); - return File(stream, "text/csv", $"Software versions by active telemetries from {from :yy-MM-dd hh-mm}.csv"); + return File(stream, "text/csv", $"Software versions by active telemetries from {from:yy-MM-dd hh-mm}.csv"); } /// @@ -79,7 +79,7 @@ public class TelemetryController : ControllerBase CancellationToken token) { var idWell = telemetryService.GetIdWellByTelemetryUid(uid); - await messageService.InsertAsync(uid, dtos, token).ConfigureAwait(false); + await messageRepository.InsertAsync(uid, dtos, token).ConfigureAwait(false); if (dtos.Any()) await Task.Run(() => telemetryHubContext.Clients.Group($"well_{idWell}") @@ -99,7 +99,7 @@ public class TelemetryController : ControllerBase public async Task PostEventsAsync(string uid, [FromBody] List events, CancellationToken token) { - await eventService.UpsertAsync(uid, events, token) + await eventRepository.UpsertAsync(uid, events, token) .ConfigureAwait(false); return Ok(); } From a6496a1af29ba013ed24a83fc1417cc765d38e93 Mon Sep 17 00:00:00 2001 From: Olga Nemt Date: Tue, 10 Sep 2024 12:33:24 +0500 Subject: [PATCH 2/6] =?UTF-8?q?=D0=90=D0=B2=D1=82=D0=BE=D1=82=D0=B5=D1=81?= =?UTF-8?q?=D1=82=20=D0=B4=D0=BB=D1=8F=20=D0=BF=D1=80=D0=BE=D0=B2=D0=B5?= =?UTF-8?q?=D1=80=D0=BA=D0=B8=20=D1=80=D0=B0=D1=81=D1=87=D0=B5=D1=82=D0=B0?= =?UTF-8?q?=20=D1=81=D1=82=D0=B0=D1=82=D0=B8=D1=81=D1=82=D0=B8=D0=BA=D0=B8?= =?UTF-8?q?=20=D1=81=D0=BE=D0=BE=D0=B1=D1=89=D0=B5=D0=BD=D0=B8=D0=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AsbCloudApp/Requests/MessageRequest.cs | 8 + .../Services/MessageServiceTest.cs | 320 ++++++++++++++++++ 2 files changed, 328 insertions(+) create mode 100644 AsbCloudInfrastructure.Tests/Services/MessageServiceTest.cs diff --git a/AsbCloudApp/Requests/MessageRequest.cs b/AsbCloudApp/Requests/MessageRequest.cs index 0d05138a..861d091a 100644 --- a/AsbCloudApp/Requests/MessageRequest.cs +++ b/AsbCloudApp/Requests/MessageRequest.cs @@ -31,6 +31,14 @@ public class MessageRequestBase : RequestBase /// public string? SearchString { get; set; } + /// + /// + /// + public MessageRequestBase() + { + + } + /// /// копирующий конструктор /// diff --git a/AsbCloudInfrastructure.Tests/Services/MessageServiceTest.cs b/AsbCloudInfrastructure.Tests/Services/MessageServiceTest.cs new file mode 100644 index 00000000..955c2101 --- /dev/null +++ b/AsbCloudInfrastructure.Tests/Services/MessageServiceTest.cs @@ -0,0 +1,320 @@ +using AsbCloudApp.Data; +using AsbCloudApp.Data.SAUB; +using AsbCloudApp.Repositories; +using AsbCloudApp.Requests; +using AsbCloudApp.Services; +using AsbCloudDb.Model; +using AsbCloudInfrastructure.Repository; +using AsbCloudInfrastructure.Services; +using AsbCloudInfrastructure.Services.SAUB; +using DocumentFormat.OpenXml.Office2010.ExcelAc; +using NSubstitute; +using Org.BouncyCastle.Asn1.Ocsp; +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; +using Xunit; + +namespace AsbCloudInfrastructure.Tests.Services; + +public class MessageServiceTest +{ + private static readonly List telemetries = new List() + { + new TelemetryBaseDto() + { + Id = 1, + TimeZone = new SimpleTimezoneDto() + { + Hours = 5 + } + }, + new TelemetryBaseDto() + { + Id = 2, + TimeZone = new SimpleTimezoneDto() + { + Hours = 5 + } + } + }; + + private static readonly List events = new List() + { + new TelemetryEventDto() + { + Id = 1, + IdCategory = 1, + MessageTemplate = " 1 1" + }, + new TelemetryEventDto() + { + Id = 2, + IdCategory = 1, + MessageTemplate = " 2 1" + }, + new TelemetryEventDto() + { + Id = 3, + IdCategory = 1, + MessageTemplate = " 3 1" + }, + new TelemetryEventDto() + { + Id = 4, + IdCategory = 2, + MessageTemplate = " 1 2" + }, + new TelemetryEventDto() + { + Id = 5, + IdCategory = 2, + MessageTemplate = " 2 2" + }, + new TelemetryEventDto() + { + Id = 6, + IdCategory = 3, + MessageTemplate = " 1 3" + } + }; + + /// + /// : + /// 1: + /// 1: + /// 1: + /// 1 + /// 2 + /// 3 + /// 2: + /// 1 + /// 2 + /// 3 + /// 3: + /// 1 + /// 2 + /// 3 + /// 2: + /// 2: + /// 1 + /// 2 + /// 3: + /// 1 + /// 2 + /// 2 + /// 3: + /// 3: + /// 1 + /// 2 + /// 3 + /// + /// + private static readonly List messages = new List() + { + new TelemetryMessageDto() + { + Id = 1, + IdEvent = 1, + IdTelemetry = 1 + }, + new TelemetryMessageDto() + { + Id = 2, + IdEvent = 1, + IdTelemetry = 1 + }, + new TelemetryMessageDto() + { + Id = 3, + IdEvent = 1, + IdTelemetry = 1 + }, + new TelemetryMessageDto() + { + Id = 4, + IdEvent = 2, + IdTelemetry = 1 + }, + new TelemetryMessageDto() + { + Id = 5, + IdEvent = 2, + IdTelemetry = 1 + }, + new TelemetryMessageDto() + { + Id = 6, + IdEvent = 2, + IdTelemetry = 1 + }, + new TelemetryMessageDto() + { + Id = 7, + IdEvent = 3, + IdTelemetry = 1 + }, + new TelemetryMessageDto() + { + Id = 8, + IdEvent = 3, + IdTelemetry = 1 + }, + new TelemetryMessageDto() + { + Id = 9, + IdEvent = 3, + IdTelemetry = 1 + }, + new TelemetryMessageDto() + { + Id = 10, + IdEvent = 4, + IdTelemetry = 1 + }, + new TelemetryMessageDto() + { + Id = 11, + IdEvent = 4, + IdTelemetry = 1 + }, + new TelemetryMessageDto() + { + Id = 12, + IdEvent = 5, + IdTelemetry = 1 + }, + new TelemetryMessageDto() + { + Id = 13, + IdEvent = 5, + IdTelemetry = 1 + }, + new TelemetryMessageDto() + { + Id = 14, + IdEvent = 6, + IdTelemetry = 2 + }, + new TelemetryMessageDto() + { + Id = 15, + IdEvent = 6, + IdTelemetry = 2 + }, + new TelemetryMessageDto() + { + Id = 16, + IdEvent = 6, + IdTelemetry = 2 + }, + }; + + + + private readonly IMessageService messageServiceMock = Substitute.For(); + private readonly IMessageRepository messageRepositoryMock = Substitute.For(); + private readonly IEventRepository eventRepositoryMock = Substitute.For(); + private readonly ITelemetryService telemetryServiceMock = Substitute.For(); + private readonly IAsbCloudDbContext db = Substitute.For(); + + public MessageServiceTest() + { + messageServiceMock = new MessageService(messageRepositoryMock, telemetryServiceMock, eventRepositoryMock); + + telemetryServiceMock + .GetOrDefaultTelemetriesByIdsWells(Arg.Any>()) + .Returns(telemetries); + + eventRepositoryMock + .Get(Arg.Any(), Arg.Any()) + .Returns(events); + + messageRepositoryMock + .GetMessagesAsync(Arg.Any(), Arg.Any()) + .Returns(messages); + } + + + [Fact] + public async Task GetStatMessages_ShouldReturn_Success() + { + //act + var baseRequest = new MessageRequestBase() { }; + var messageRequest = new MessageRequest(baseRequest, [1]); + var result = await messageServiceMock.GetStat(messageRequest, CancellationToken.None); + + Assert.Equal(6, result.Count()); + + var mCount_Telemetry_1_Category_1_Template_1_Expected = messages + .Where(x => x.IdTelemetry == 1) + .Where(x => x.IdEvent == 1) + .Count(); + var mCount_Telemetry_1_Category_1_Template_1_Actual = result + .Where(x => x.IdTelemetry == 1) + .Where(x => x.IdCategory == 1) + .Where(x => x.MessageTemplate == " 1 1") + .Sum(x => x.MessagesCount); + Assert.Equal(mCount_Telemetry_1_Category_1_Template_1_Expected, mCount_Telemetry_1_Category_1_Template_1_Actual); + + var mCount_Telemetry_1_Category_1_Template_2_Expected = messages + .Where(x => x.IdTelemetry == 1) + .Where(x => x.IdEvent == 2) + .Count(); + var mCount_Telemetry_1_Category_1_Template_2_Actual = result + .Where(x => x.IdTelemetry == 1) + .Where(x => x.IdCategory == 1) + .Where(x => x.MessageTemplate == " 2 1") + .Sum(x => x.MessagesCount); + Assert.Equal(mCount_Telemetry_1_Category_1_Template_2_Expected, mCount_Telemetry_1_Category_1_Template_2_Actual); + + var mCount_Telemetry_1_Category_1_Template_3_Expected = messages + .Where(x => x.IdTelemetry == 1) + .Where(x => x.IdEvent == 3) + .Count(); + var mCount_Telemetry_1_Category_1_Template_3_Actual = result + .Where(x => x.IdTelemetry == 1) + .Where(x => x.IdCategory == 1) + .Where(x => x.MessageTemplate == " 3 1") + .Sum(x => x.MessagesCount); + Assert.Equal(mCount_Telemetry_1_Category_1_Template_3_Expected, mCount_Telemetry_1_Category_1_Template_3_Actual); + + var mCount_Telemetry_1_Category_2_Template_2_Expected = messages + .Where(x => x.IdTelemetry == 1) + .Where(x => x.IdEvent == 5) + .Count(); + var mCount_Telemetry_1_Category_2_Template_2_Actual = result + .Where(x => x.IdTelemetry == 1) + .Where(x => x.IdCategory == 2) + .Where(x => x.MessageTemplate == " 2 2") + .Sum(x => x.MessagesCount); + Assert.Equal(mCount_Telemetry_1_Category_2_Template_2_Expected, mCount_Telemetry_1_Category_2_Template_2_Actual); + + var mCount_Telemetry_1_Category_2_Template_3_Expected = messages + .Where(x => x.IdTelemetry == 1) + .Where(x => x.IdEvent == 6) + .Count(); + var mCount_Telemetry_1_Category_2_Template_3_Actual = result + .Where(x => x.IdTelemetry == 1) + .Where(x => x.IdCategory == 2) + .Where(x => x.MessageTemplate == " 3 2") + .Sum(x => x.MessagesCount); + Assert.Equal(mCount_Telemetry_1_Category_2_Template_3_Expected, mCount_Telemetry_1_Category_2_Template_3_Actual); + + var mCount_Telemetry_2_Category_3_Template_1_Expected = messages + .Where(x => x.IdTelemetry == 2) + .Where(x => x.IdEvent == 6) + .Count(); + var mCount_Telemetry_2_Category_3_Template_1_Actual = result + .Where(x => x.IdTelemetry == 2) + .Where(x => x.IdCategory == 3) + .Where(x => x.MessageTemplate == " 1 3") + .Sum(x => x.MessagesCount); + Assert.Equal(mCount_Telemetry_2_Category_3_Template_1_Expected, mCount_Telemetry_2_Category_3_Template_1_Actual); + } + + +} \ No newline at end of file From 57d97057da558194270df8563e8a53036b7c0e92 Mon Sep 17 00:00:00 2001 From: Olga Nemt Date: Tue, 10 Sep 2024 16:54:35 +0500 Subject: [PATCH 3/6] =?UTF-8?q?1.=20=D0=97=D0=B0=D0=BB=D0=B8=D1=82=D1=8B?= =?UTF-8?q?=20=D0=BD=D0=B5=D0=B4=D0=BE=D1=81=D1=82=D0=B0=D1=8E=D1=89=D0=B8?= =?UTF-8?q?=D0=B5=20=D1=84=D0=B0=D0=B9=D0=BB=D1=8B=202.=20=D0=9F=D0=BE?= =?UTF-8?q?=D0=BF=D1=80=D0=B0=D0=B2=D0=BB=D0=B5=D0=BD=D0=B0=20=D0=BA=D0=BE?= =?UTF-8?q?=D0=B4=D0=B8=D1=80=D0=BE=D0=B2=D0=BA=D0=B0=20=D1=84=D0=B0=D0=B9?= =?UTF-8?q?=D0=BB=D0=B0=20=D1=81=20=D0=B0=D0=B2=D1=82=D0=BE=D1=82=D0=B5?= =?UTF-8?q?=D1=81=D1=82=D0=BE=D0=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AsbCloudApp/Data/SAUB/TelemetryEventDto.cs | 29 +++++++ AsbCloudApp/Data/SAUB/TelemetryMessageDto.cs | 7 -- AsbCloudApp/Data/StatCriticalMessageDto.cs | 28 ++++++ .../Services/MessageServiceTest.cs | 86 +++++++++---------- 4 files changed, 97 insertions(+), 53 deletions(-) create mode 100644 AsbCloudApp/Data/SAUB/TelemetryEventDto.cs create mode 100644 AsbCloudApp/Data/StatCriticalMessageDto.cs diff --git a/AsbCloudApp/Data/SAUB/TelemetryEventDto.cs b/AsbCloudApp/Data/SAUB/TelemetryEventDto.cs new file mode 100644 index 00000000..e9a8a90e --- /dev/null +++ b/AsbCloudApp/Data/SAUB/TelemetryEventDto.cs @@ -0,0 +1,29 @@ +namespace AsbCloudApp.Data.SAUB; + +/// +/// dto для события телеметрии +/// +public class TelemetryEventDto +{ + /// + /// ключ события + /// + public object Id { get; set; } + + /// + /// ключ категории + /// + public int IdCategory { get; set; } + + /// + /// шаблон сообщения + /// + public string MessageTemplate { get; set; } = null!; + + /// + /// метод формирования текста сообщения по шаблону + /// + /// аргументы, которые подставляются в шаблон сообщения + /// + public string MakeMessageText(string?[] args) => string.Format(MessageTemplate, args); +} diff --git a/AsbCloudApp/Data/SAUB/TelemetryMessageDto.cs b/AsbCloudApp/Data/SAUB/TelemetryMessageDto.cs index 572ab650..b34d1289 100644 --- a/AsbCloudApp/Data/SAUB/TelemetryMessageDto.cs +++ b/AsbCloudApp/Data/SAUB/TelemetryMessageDto.cs @@ -7,13 +7,6 @@ namespace AsbCloudApp.Data.SAUB; /// public class TelemetryMessageDto : IId { - /// - /// - /// - public TelemetryMessageDto() - { - - } /// /// Id сообщения в базе панели оператора /// diff --git a/AsbCloudApp/Data/StatCriticalMessageDto.cs b/AsbCloudApp/Data/StatCriticalMessageDto.cs new file mode 100644 index 00000000..e8640ab6 --- /dev/null +++ b/AsbCloudApp/Data/StatCriticalMessageDto.cs @@ -0,0 +1,28 @@ + +namespace AsbCloudApp.Data; + +/// +/// dto для отображения статистики сообщений +/// +public class StatCriticalMessageDto +{ + /// + /// ключ телеметрии + /// + public int IdTelemetry { get; set; } + + /// + /// ключ категории + /// + public int? IdCategory { get; set; } + + /// + /// шаблон сообщения + /// + public string? MessageTemplate { get; set; } + + /// + /// количество сообщений + /// + public int MessagesCount { get; set; } +} diff --git a/AsbCloudInfrastructure.Tests/Services/MessageServiceTest.cs b/AsbCloudInfrastructure.Tests/Services/MessageServiceTest.cs index 955c2101..c6a0627d 100644 --- a/AsbCloudInfrastructure.Tests/Services/MessageServiceTest.cs +++ b/AsbCloudInfrastructure.Tests/Services/MessageServiceTest.cs @@ -4,15 +4,10 @@ using AsbCloudApp.Repositories; using AsbCloudApp.Requests; using AsbCloudApp.Services; using AsbCloudDb.Model; -using AsbCloudInfrastructure.Repository; using AsbCloudInfrastructure.Services; -using AsbCloudInfrastructure.Services.SAUB; -using DocumentFormat.OpenXml.Office2010.ExcelAc; using NSubstitute; -using Org.BouncyCastle.Asn1.Ocsp; using System; using System.Collections.Generic; -using System.IO; using System.Linq; using System.Threading; using System.Threading.Tasks; @@ -48,69 +43,69 @@ public class MessageServiceTest { Id = 1, IdCategory = 1, - MessageTemplate = " 1 1" + MessageTemplate = "Шаблон сообщения 1 категории 1" }, new TelemetryEventDto() { Id = 2, IdCategory = 1, - MessageTemplate = " 2 1" + MessageTemplate = "Шаблон сообщения 2 категории 1" }, new TelemetryEventDto() { Id = 3, IdCategory = 1, - MessageTemplate = " 3 1" + MessageTemplate = "Шаблон сообщения 3 категории 1" }, new TelemetryEventDto() { Id = 4, IdCategory = 2, - MessageTemplate = " 1 2" + MessageTemplate = "Шаблон сообщения 1 категории 2" }, new TelemetryEventDto() { Id = 5, IdCategory = 2, - MessageTemplate = " 2 2" + MessageTemplate = "Шаблон сообщения 2 категории 2" }, new TelemetryEventDto() { Id = 6, IdCategory = 3, - MessageTemplate = " 1 3" + MessageTemplate = "Шаблон сообщения 1 категории 3" } }; /// - /// : - /// 1: - /// 1: - /// 1: - /// 1 - /// 2 - /// 3 - /// 2: - /// 1 - /// 2 - /// 3 - /// 3: - /// 1 - /// 2 - /// 3 - /// 2: - /// 2: - /// 1 - /// 2 - /// 3: - /// 1 - /// 2 - /// 2 - /// 3: - /// 3: - /// 1 - /// 2 - /// 3 + /// Структура: + /// Телеметрия 1: + /// Категория 1: + /// Событие 1: + /// Сообщение 1 + /// Сообщение 2 + /// Сообщение 3 + /// Событие 2: + /// Сообщение 1 + /// Сообщение 2 + /// Сообщение 3 + /// Событие 3: + /// Сообщение 1 + /// Сообщение 2 + /// Сообщение 3 + /// Категория 2: + /// Событие 2: + /// Сообщение 1 + /// Сообщение 2 + /// Событие 3: + /// Сообщение 1 + /// Сообщение 2 + /// Телеметрия 2 + /// Категория 3: + /// Событие 3: + /// Сообщение 1 + /// Сообщение 2 + /// Сообщение 3 /// /// private static readonly List messages = new List() @@ -219,7 +214,6 @@ public class MessageServiceTest private readonly IMessageRepository messageRepositoryMock = Substitute.For(); private readonly IEventRepository eventRepositoryMock = Substitute.For(); private readonly ITelemetryService telemetryServiceMock = Substitute.For(); - private readonly IAsbCloudDbContext db = Substitute.For(); public MessageServiceTest() { @@ -256,7 +250,7 @@ public class MessageServiceTest var mCount_Telemetry_1_Category_1_Template_1_Actual = result .Where(x => x.IdTelemetry == 1) .Where(x => x.IdCategory == 1) - .Where(x => x.MessageTemplate == " 1 1") + .Where(x => x.MessageTemplate == "Шаблон сообщения 1 категории 1") .Sum(x => x.MessagesCount); Assert.Equal(mCount_Telemetry_1_Category_1_Template_1_Expected, mCount_Telemetry_1_Category_1_Template_1_Actual); @@ -267,7 +261,7 @@ public class MessageServiceTest var mCount_Telemetry_1_Category_1_Template_2_Actual = result .Where(x => x.IdTelemetry == 1) .Where(x => x.IdCategory == 1) - .Where(x => x.MessageTemplate == " 2 1") + .Where(x => x.MessageTemplate == "Шаблон сообщения 2 категории 1") .Sum(x => x.MessagesCount); Assert.Equal(mCount_Telemetry_1_Category_1_Template_2_Expected, mCount_Telemetry_1_Category_1_Template_2_Actual); @@ -278,7 +272,7 @@ public class MessageServiceTest var mCount_Telemetry_1_Category_1_Template_3_Actual = result .Where(x => x.IdTelemetry == 1) .Where(x => x.IdCategory == 1) - .Where(x => x.MessageTemplate == " 3 1") + .Where(x => x.MessageTemplate == "Шаблон сообщения 3 категории 1") .Sum(x => x.MessagesCount); Assert.Equal(mCount_Telemetry_1_Category_1_Template_3_Expected, mCount_Telemetry_1_Category_1_Template_3_Actual); @@ -289,7 +283,7 @@ public class MessageServiceTest var mCount_Telemetry_1_Category_2_Template_2_Actual = result .Where(x => x.IdTelemetry == 1) .Where(x => x.IdCategory == 2) - .Where(x => x.MessageTemplate == " 2 2") + .Where(x => x.MessageTemplate == "Шаблон сообщения 2 категории 2") .Sum(x => x.MessagesCount); Assert.Equal(mCount_Telemetry_1_Category_2_Template_2_Expected, mCount_Telemetry_1_Category_2_Template_2_Actual); @@ -300,7 +294,7 @@ public class MessageServiceTest var mCount_Telemetry_1_Category_2_Template_3_Actual = result .Where(x => x.IdTelemetry == 1) .Where(x => x.IdCategory == 2) - .Where(x => x.MessageTemplate == " 3 2") + .Where(x => x.MessageTemplate == "Шаблон сообщения 3 категории 2") .Sum(x => x.MessagesCount); Assert.Equal(mCount_Telemetry_1_Category_2_Template_3_Expected, mCount_Telemetry_1_Category_2_Template_3_Actual); @@ -311,7 +305,7 @@ public class MessageServiceTest var mCount_Telemetry_2_Category_3_Template_1_Actual = result .Where(x => x.IdTelemetry == 2) .Where(x => x.IdCategory == 3) - .Where(x => x.MessageTemplate == " 1 3") + .Where(x => x.MessageTemplate == "Шаблон сообщения 1 категории 3") .Sum(x => x.MessagesCount); Assert.Equal(mCount_Telemetry_2_Category_3_Template_1_Expected, mCount_Telemetry_2_Category_3_Template_1_Actual); } From 31efdbf406796a9480cb643cbd5b1ae6235358d0 Mon Sep 17 00:00:00 2001 From: Olga Nemt Date: Wed, 11 Sep 2024 10:56:26 +0500 Subject: [PATCH 4/6] =?UTF-8?q?=D0=BF=D1=80=D0=B0=D0=B2=D0=BA=D0=B8=20?= =?UTF-8?q?=D0=BF=D0=BE=20=D1=80=D0=B5=D0=B7=D1=83=D0=BB=D1=8C=D1=82=D0=B0?= =?UTF-8?q?=D1=82=D0=B0=D0=BC=20=D1=80=D0=B5=D0=B2=D1=8C=D1=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AsbCloudApp/Repositories/IEventRepository.cs | 5 +- .../Repositories/IMessageRepository.cs | 2 +- AsbCloudApp/Requests/MessageRequest.cs | 21 ++++-- AsbCloudApp/Services/IMessageService.cs | 2 +- .../Services/MessageServiceTest.cs | 66 ++++++------------- .../Repository/EventRepository.cs | 4 +- .../Services/MessageService.cs | 11 ++-- .../Controllers/SAUB/MessageController.cs | 2 +- 8 files changed, 47 insertions(+), 66 deletions(-) diff --git a/AsbCloudApp/Repositories/IEventRepository.cs b/AsbCloudApp/Repositories/IEventRepository.cs index ebe5e500..90aa9687 100644 --- a/AsbCloudApp/Repositories/IEventRepository.cs +++ b/AsbCloudApp/Repositories/IEventRepository.cs @@ -17,7 +17,7 @@ public interface IEventRepository /// параметры запроса /// /// - Task> Get(TelemetryEventRequest request, CancellationToken token); + Task> GetAsync(TelemetryEventRequest request, CancellationToken token); /// /// Сохранить. Добавить или заменить. @@ -26,6 +26,5 @@ public interface IEventRepository /// /// /// - Task UpsertAsync(string uid, IEnumerable dtos, - CancellationToken token = default); + Task UpsertAsync(string uid, IEnumerable dtos, CancellationToken token); } diff --git a/AsbCloudApp/Repositories/IMessageRepository.cs b/AsbCloudApp/Repositories/IMessageRepository.cs index d08eeefc..ac794a46 100644 --- a/AsbCloudApp/Repositories/IMessageRepository.cs +++ b/AsbCloudApp/Repositories/IMessageRepository.cs @@ -14,7 +14,7 @@ namespace AsbCloudApp.Repositories; public interface IMessageRepository : ITelemetryDataEditorService { /// - /// Получить PaginationContainer с сообщениями по параметрам + /// Получить сообщения по параметрам /// /// /// diff --git a/AsbCloudApp/Requests/MessageRequest.cs b/AsbCloudApp/Requests/MessageRequest.cs index 861d091a..413b9091 100644 --- a/AsbCloudApp/Requests/MessageRequest.cs +++ b/AsbCloudApp/Requests/MessageRequest.cs @@ -2,6 +2,7 @@ using AsbCloudApp.Data; using AsbCloudApp.Data.SAUB; using System; using System.Collections.Generic; +using System.Linq; namespace AsbCloudApp.Requests; @@ -86,19 +87,25 @@ public class MessageTelemetryRequest : MessageRequestBase /// /// события /// - public IEnumerable Events { get; set; } = null!; + public IEnumerable Events { get; set; }= Enumerable.Empty(); /// /// телеметрии /// - public IEnumerable Telemetries { get; set; } = null!; + public IEnumerable Telemetries { get; set; } = Enumerable.Empty(); - /// - /// - /// - /// - public MessageTelemetryRequest(MessageRequestBase request) : base(request) + /// + public MessageTelemetryRequest( + MessageRequestBase request, + IEnumerable events, + IEnumerable telemetries) : base(request) { + Events = events; + Telemetries = telemetries; } + + /// + public MessageTelemetryRequest(MessageRequestBase request) + : base(request) { } } diff --git a/AsbCloudApp/Services/IMessageService.cs b/AsbCloudApp/Services/IMessageService.cs index 92fd7864..319d2faa 100644 --- a/AsbCloudApp/Services/IMessageService.cs +++ b/AsbCloudApp/Services/IMessageService.cs @@ -17,7 +17,7 @@ public interface IMessageService /// /// /// - Task> GetStat(MessageRequest request, CancellationToken token); + Task> GetStatAsync(MessageRequest request, CancellationToken token); /// /// Получить PaginationContainer с сообщениями по параметрам diff --git a/AsbCloudInfrastructure.Tests/Services/MessageServiceTest.cs b/AsbCloudInfrastructure.Tests/Services/MessageServiceTest.cs index c6a0627d..8cb94ca5 100644 --- a/AsbCloudInfrastructure.Tests/Services/MessageServiceTest.cs +++ b/AsbCloudInfrastructure.Tests/Services/MessageServiceTest.cs @@ -224,7 +224,7 @@ public class MessageServiceTest .Returns(telemetries); eventRepositoryMock - .Get(Arg.Any(), Arg.Any()) + .GetAsync(Arg.Any(), Arg.Any()) .Returns(events); messageRepositoryMock @@ -239,75 +239,51 @@ public class MessageServiceTest //act var baseRequest = new MessageRequestBase() { }; var messageRequest = new MessageRequest(baseRequest, [1]); - var result = await messageServiceMock.GetStat(messageRequest, CancellationToken.None); + var result = await messageServiceMock.GetStatAsync(messageRequest, CancellationToken.None); Assert.Equal(6, result.Count()); - var mCount_Telemetry_1_Category_1_Template_1_Expected = messages - .Where(x => x.IdTelemetry == 1) - .Where(x => x.IdEvent == 1) - .Count(); - var mCount_Telemetry_1_Category_1_Template_1_Actual = result + var сообщение_1_1 = result .Where(x => x.IdTelemetry == 1) .Where(x => x.IdCategory == 1) .Where(x => x.MessageTemplate == "Шаблон сообщения 1 категории 1") - .Sum(x => x.MessagesCount); - Assert.Equal(mCount_Telemetry_1_Category_1_Template_1_Expected, mCount_Telemetry_1_Category_1_Template_1_Actual); + .Single(); + Assert.Equal(3, сообщение_1_1.MessagesCount); - var mCount_Telemetry_1_Category_1_Template_2_Expected = messages - .Where(x => x.IdTelemetry == 1) - .Where(x => x.IdEvent == 2) - .Count(); - var mCount_Telemetry_1_Category_1_Template_2_Actual = result + var сообщение_1_2 = result .Where(x => x.IdTelemetry == 1) .Where(x => x.IdCategory == 1) .Where(x => x.MessageTemplate == "Шаблон сообщения 2 категории 1") - .Sum(x => x.MessagesCount); - Assert.Equal(mCount_Telemetry_1_Category_1_Template_2_Expected, mCount_Telemetry_1_Category_1_Template_2_Actual); + .Single(); + Assert.Equal(3, сообщение_1_2.MessagesCount); - var mCount_Telemetry_1_Category_1_Template_3_Expected = messages - .Where(x => x.IdTelemetry == 1) - .Where(x => x.IdEvent == 3) - .Count(); - var mCount_Telemetry_1_Category_1_Template_3_Actual = result + var сообщение_1_3 = result .Where(x => x.IdTelemetry == 1) .Where(x => x.IdCategory == 1) .Where(x => x.MessageTemplate == "Шаблон сообщения 3 категории 1") - .Sum(x => x.MessagesCount); - Assert.Equal(mCount_Telemetry_1_Category_1_Template_3_Expected, mCount_Telemetry_1_Category_1_Template_3_Actual); + .Single(); + Assert.Equal(3, сообщение_1_3.MessagesCount); - var mCount_Telemetry_1_Category_2_Template_2_Expected = messages - .Where(x => x.IdTelemetry == 1) - .Where(x => x.IdEvent == 5) - .Count(); - var mCount_Telemetry_1_Category_2_Template_2_Actual = result + var сообщение_2_2 = result .Where(x => x.IdTelemetry == 1) .Where(x => x.IdCategory == 2) .Where(x => x.MessageTemplate == "Шаблон сообщения 2 категории 2") - .Sum(x => x.MessagesCount); - Assert.Equal(mCount_Telemetry_1_Category_2_Template_2_Expected, mCount_Telemetry_1_Category_2_Template_2_Actual); + .Single(); + Assert.Equal(2, сообщение_2_2.MessagesCount); - var mCount_Telemetry_1_Category_2_Template_3_Expected = messages - .Where(x => x.IdTelemetry == 1) - .Where(x => x.IdEvent == 6) - .Count(); - var mCount_Telemetry_1_Category_2_Template_3_Actual = result + var сообщение_2_1 = result .Where(x => x.IdTelemetry == 1) .Where(x => x.IdCategory == 2) - .Where(x => x.MessageTemplate == "Шаблон сообщения 3 категории 2") - .Sum(x => x.MessagesCount); - Assert.Equal(mCount_Telemetry_1_Category_2_Template_3_Expected, mCount_Telemetry_1_Category_2_Template_3_Actual); + .Where(x => x.MessageTemplate == "Шаблон сообщения 1 категории 2") + .Single(); + Assert.Equal(2, сообщение_2_1.MessagesCount); - var mCount_Telemetry_2_Category_3_Template_1_Expected = messages - .Where(x => x.IdTelemetry == 2) - .Where(x => x.IdEvent == 6) - .Count(); - var mCount_Telemetry_2_Category_3_Template_1_Actual = result + var сообщение_3_1 = result .Where(x => x.IdTelemetry == 2) .Where(x => x.IdCategory == 3) .Where(x => x.MessageTemplate == "Шаблон сообщения 1 категории 3") - .Sum(x => x.MessagesCount); - Assert.Equal(mCount_Telemetry_2_Category_3_Template_1_Expected, mCount_Telemetry_2_Category_3_Template_1_Actual); + .Single(); + Assert.Equal(3, сообщение_3_1.MessagesCount); } diff --git a/AsbCloudInfrastructure/Repository/EventRepository.cs b/AsbCloudInfrastructure/Repository/EventRepository.cs index 8d3665a4..680746f9 100644 --- a/AsbCloudInfrastructure/Repository/EventRepository.cs +++ b/AsbCloudInfrastructure/Repository/EventRepository.cs @@ -27,7 +27,7 @@ public class EventRepository : IEventRepository this.telemetryService = telemetryService; } - public async Task> Get(TelemetryEventRequest request, CancellationToken token) + public async Task> GetAsync(TelemetryEventRequest request, CancellationToken token) { var allEvents = await memoryCache.GetOrCreateBasicAsync(db.Set(), token); var events = allEvents.Where(e => request.IdsTelemetries.Contains(e.IdTelemetry)); @@ -65,7 +65,7 @@ public class EventRepository : IEventRepository IdCategory = dto.IdCategory, MessageTemplate = dto.Message }); - var result = await db.Database.ExecInsertOrUpdateAsync(db.TelemetryEvents, entities, token); + await db.Database.ExecInsertOrUpdateAsync(db.TelemetryEvents, entities, token); memoryCache.DropBasic(); } } diff --git a/AsbCloudInfrastructure/Services/MessageService.cs b/AsbCloudInfrastructure/Services/MessageService.cs index f71b5c4a..24d703c1 100644 --- a/AsbCloudInfrastructure/Services/MessageService.cs +++ b/AsbCloudInfrastructure/Services/MessageService.cs @@ -36,7 +36,7 @@ public class MessageService : IMessageService return result; } - public async Task> GetStat(MessageRequest request, CancellationToken token) + public async Task> GetStatAsync(MessageRequest request, CancellationToken token) { var messageTelemetryRequest = await CreateMessageTelemetryRequest(request, token); @@ -70,17 +70,16 @@ public class MessageService : IMessageService public async Task 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); + var events = await eventRepository.GetAsync(telemetryEventRequest, token); + + var messageTelemetryRequest = new MessageTelemetryRequest(request, events, telemetries); return messageTelemetryRequest; } diff --git a/AsbCloudWebApi/Controllers/SAUB/MessageController.cs b/AsbCloudWebApi/Controllers/SAUB/MessageController.cs index 1867be94..31bd4345 100644 --- a/AsbCloudWebApi/Controllers/SAUB/MessageController.cs +++ b/AsbCloudWebApi/Controllers/SAUB/MessageController.cs @@ -68,7 +68,7 @@ public class MessageController : ControllerBase [ProducesResponseType(typeof(IEnumerable), (int)System.Net.HttpStatusCode.OK)] public async Task Get([FromQuery] MessageRequest request, CancellationToken token) { - var result = await messageService.GetStat(request, token); + var result = await messageService.GetStatAsync(request, token); return Ok(result); } From 6007fb181c8da8727b6c18ccb72d380fff9d60a3 Mon Sep 17 00:00:00 2001 From: Olga Nemt Date: Thu, 12 Sep 2024 12:46:46 +0500 Subject: [PATCH 5/6] =?UTF-8?q?=D0=9F=D1=80=D0=B0=D0=B2=D0=BA=D0=B0=20?= =?UTF-8?q?=D0=BF=D0=BE=20=D1=80=D0=B5=D0=B7=D1=83=D0=BB=D1=8C=D1=82=D0=B0?= =?UTF-8?q?=D1=82=D0=B0=D0=BC=20=D1=80=D0=B5=D0=B2=D1=8C=D1=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AsbCloudApp/Data/StatCriticalMessageDto.cs | 4 +- AsbCloudApp/Requests/MessageRequest.cs | 4 +- AsbCloudApp/Services/ITelemetryService.cs | 2 +- .../Services/MessageServiceTest.cs | 44 ++++++++++--------- .../Services/MessageService.cs | 11 +++-- .../Services/SAUB/TelemetryService.cs | 9 +++- 6 files changed, 42 insertions(+), 32 deletions(-) diff --git a/AsbCloudApp/Data/StatCriticalMessageDto.cs b/AsbCloudApp/Data/StatCriticalMessageDto.cs index e8640ab6..8595934a 100644 --- a/AsbCloudApp/Data/StatCriticalMessageDto.cs +++ b/AsbCloudApp/Data/StatCriticalMessageDto.cs @@ -7,9 +7,9 @@ namespace AsbCloudApp.Data; public class StatCriticalMessageDto { /// - /// ключ телеметрии + /// ключ скважины /// - public int IdTelemetry { get; set; } + public int IdWell { get; set; } /// /// ключ категории diff --git a/AsbCloudApp/Requests/MessageRequest.cs b/AsbCloudApp/Requests/MessageRequest.cs index 413b9091..f11f6290 100644 --- a/AsbCloudApp/Requests/MessageRequest.cs +++ b/AsbCloudApp/Requests/MessageRequest.cs @@ -92,13 +92,13 @@ public class MessageTelemetryRequest : MessageRequestBase /// /// телеметрии /// - public IEnumerable Telemetries { get; set; } = Enumerable.Empty(); + public IEnumerable Telemetries { get; set; } = Enumerable.Empty(); /// public MessageTelemetryRequest( MessageRequestBase request, IEnumerable events, - IEnumerable telemetries) : base(request) + IEnumerable telemetries) : base(request) { Events = events; Telemetries = telemetries; diff --git a/AsbCloudApp/Services/ITelemetryService.cs b/AsbCloudApp/Services/ITelemetryService.cs index 02f93461..fa6c88f4 100644 --- a/AsbCloudApp/Services/ITelemetryService.cs +++ b/AsbCloudApp/Services/ITelemetryService.cs @@ -52,7 +52,7 @@ public interface ITelemetryService /// /// ключи скважин /// - IEnumerable GetOrDefaultTelemetriesByIdsWells(IEnumerable idsWells); + IEnumerable GetOrDefaultTelemetriesByIdsWells(IEnumerable idsWells); /// /// получить диапазон дат за которые есть данные /// diff --git a/AsbCloudInfrastructure.Tests/Services/MessageServiceTest.cs b/AsbCloudInfrastructure.Tests/Services/MessageServiceTest.cs index 8cb94ca5..f3bc4bf3 100644 --- a/AsbCloudInfrastructure.Tests/Services/MessageServiceTest.cs +++ b/AsbCloudInfrastructure.Tests/Services/MessageServiceTest.cs @@ -17,19 +17,21 @@ namespace AsbCloudInfrastructure.Tests.Services; public class MessageServiceTest { - private static readonly List telemetries = new List() + private static readonly List telemetries = new List() { - new TelemetryBaseDto() + new TelemetryDto() { Id = 1, + IdWell = 1, TimeZone = new SimpleTimezoneDto() { Hours = 5 } }, - new TelemetryBaseDto() + new TelemetryDto() { Id = 2, + IdWell = 2, TimeZone = new SimpleTimezoneDto() { Hours = 5 @@ -243,47 +245,47 @@ public class MessageServiceTest Assert.Equal(6, result.Count()); - var сообщение_1_1 = result - .Where(x => x.IdTelemetry == 1) + var messageStat_1_1 = result + .Where(x => x.IdWell == 1) .Where(x => x.IdCategory == 1) .Where(x => x.MessageTemplate == "Шаблон сообщения 1 категории 1") .Single(); - Assert.Equal(3, сообщение_1_1.MessagesCount); + Assert.Equal(3, messageStat_1_1.MessagesCount); - var сообщение_1_2 = result - .Where(x => x.IdTelemetry == 1) + var messageStat_1_2 = result + .Where(x => x.IdWell == 1) .Where(x => x.IdCategory == 1) .Where(x => x.MessageTemplate == "Шаблон сообщения 2 категории 1") .Single(); - Assert.Equal(3, сообщение_1_2.MessagesCount); + Assert.Equal(3, messageStat_1_2.MessagesCount); - var сообщение_1_3 = result - .Where(x => x.IdTelemetry == 1) + var messageStat_1_3 = result + .Where(x => x.IdWell == 1) .Where(x => x.IdCategory == 1) .Where(x => x.MessageTemplate == "Шаблон сообщения 3 категории 1") .Single(); - Assert.Equal(3, сообщение_1_3.MessagesCount); + Assert.Equal(3, messageStat_1_3.MessagesCount); - var сообщение_2_2 = result - .Where(x => x.IdTelemetry == 1) + var messageStat_2_2 = result + .Where(x => x.IdWell == 1) .Where(x => x.IdCategory == 2) .Where(x => x.MessageTemplate == "Шаблон сообщения 2 категории 2") .Single(); - Assert.Equal(2, сообщение_2_2.MessagesCount); + Assert.Equal(2, messageStat_2_2.MessagesCount); - var сообщение_2_1 = result - .Where(x => x.IdTelemetry == 1) + var messageStat_2_1 = result + .Where(x => x.IdWell == 1) .Where(x => x.IdCategory == 2) .Where(x => x.MessageTemplate == "Шаблон сообщения 1 категории 2") .Single(); - Assert.Equal(2, сообщение_2_1.MessagesCount); + Assert.Equal(2, messageStat_2_1.MessagesCount); - var сообщение_3_1 = result - .Where(x => x.IdTelemetry == 2) + var messageStat_3_1 = result + .Where(x => x.IdWell == 2) .Where(x => x.IdCategory == 3) .Where(x => x.MessageTemplate == "Шаблон сообщения 1 категории 3") .Single(); - Assert.Equal(3, сообщение_3_1.MessagesCount); + Assert.Equal(3, messageStat_3_1.MessagesCount); } diff --git a/AsbCloudInfrastructure/Services/MessageService.cs b/AsbCloudInfrastructure/Services/MessageService.cs index 24d703c1..e4a5b333 100644 --- a/AsbCloudInfrastructure/Services/MessageService.cs +++ b/AsbCloudInfrastructure/Services/MessageService.cs @@ -45,20 +45,23 @@ public class MessageService : IMessageService var groupedMessages = telemetryMessages.GroupBy(x => new { x.IdTelemetry, x.IdEvent }); var events = messageTelemetryRequest.Events.ToDictionary(e => e.Id); + var idsWellsDict = messageTelemetryRequest.Telemetries.ToDictionary(e => e.Id, e => e.IdWell); + var result = groupedMessages .Select(m => new { - m.Key.IdTelemetry, + IdWell = idsWellsDict.GetValueOrDefault(m.Key.IdTelemetry), events.GetValueOrDefault(m.Key.IdEvent)?.IdCategory, events.GetValueOrDefault(m.Key.IdEvent)?.MessageTemplate, - Count = m.Count() + Count = m.Count(), }) .Where(m => m.IdCategory != null) + .Where(m => m.IdWell != null) .Where(m => m.MessageTemplate != null) - .GroupBy(m => new { m.IdTelemetry, m.IdCategory, m.MessageTemplate }) + .GroupBy(m => new { m.IdWell, m.IdCategory, m.MessageTemplate }) .Select(m => new StatCriticalMessageDto() { - IdTelemetry = m.Key.IdTelemetry, + IdWell = m.First().IdWell!.Value, IdCategory = m.Key.IdCategory, MessageTemplate = m.Key.MessageTemplate, MessagesCount = m.Select(z => z.Count).Sum() diff --git a/AsbCloudInfrastructure/Services/SAUB/TelemetryService.cs b/AsbCloudInfrastructure/Services/SAUB/TelemetryService.cs index 521fd8dd..a501f37c 100644 --- a/AsbCloudInfrastructure/Services/SAUB/TelemetryService.cs +++ b/AsbCloudInfrastructure/Services/SAUB/TelemetryService.cs @@ -150,7 +150,7 @@ public class TelemetryService : ITelemetryService return dto; } - public IEnumerable GetOrDefaultTelemetriesByIdsWells(IEnumerable idsWells) + public IEnumerable GetOrDefaultTelemetriesByIdsWells(IEnumerable idsWells) { var entities = GetTelemetryCache() .Where(t => t.Well != null) @@ -162,7 +162,12 @@ public class TelemetryService : ITelemetryService }); - var dtos = entities.Select(t => t.Adapt()); + var dtos = entities.Select(t => { + var dto = t.Adapt(); + dto.IdWell = t.Well?.Id; + + return dto; + }); return dtos; } From 95dfef62196fe1a0c03686f26c5d33172eda9388 Mon Sep 17 00:00:00 2001 From: Olga Nemt Date: Thu, 12 Sep 2024 13:05:58 +0500 Subject: [PATCH 6/6] =?UTF-8?q?=D0=9F=D1=80=D0=B0=D0=B2=D0=BA=D0=B0=20?= =?UTF-8?q?=D0=B0=D0=B2=D1=82=D0=BE=D1=82=D0=B5=D1=81=D1=82=D0=B0=20=D0=BF?= =?UTF-8?q?=D0=BE=20=D1=80=D0=B5=D0=B7=D1=83=D0=BB=D1=8C=D1=82=D0=B0=D1=82?= =?UTF-8?q?=D0=B0=D0=BC=20=D1=80=D0=B5=D0=B2=D1=8C=D1=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Services/MessageServiceTest.cs | 56 ++++++------------- 1 file changed, 17 insertions(+), 39 deletions(-) diff --git a/AsbCloudInfrastructure.Tests/Services/MessageServiceTest.cs b/AsbCloudInfrastructure.Tests/Services/MessageServiceTest.cs index f3bc4bf3..ae7b6691 100644 --- a/AsbCloudInfrastructure.Tests/Services/MessageServiceTest.cs +++ b/AsbCloudInfrastructure.Tests/Services/MessageServiceTest.cs @@ -245,47 +245,25 @@ public class MessageServiceTest Assert.Equal(6, result.Count()); - var messageStat_1_1 = result - .Where(x => x.IdWell == 1) - .Where(x => x.IdCategory == 1) - .Where(x => x.MessageTemplate == "Шаблон сообщения 1 категории 1") - .Single(); - Assert.Equal(3, messageStat_1_1.MessagesCount); + AssertStatByEvent(result, 1, events[0], 3); + AssertStatByEvent(result, 1, events[1], 3); + AssertStatByEvent(result, 1, events[2], 3); + AssertStatByEvent(result, 1, events[3], 2); + AssertStatByEvent(result, 1, events[4], 2); + AssertStatByEvent(result, 2, events[5], 3); + } - var messageStat_1_2 = result - .Where(x => x.IdWell == 1) - .Where(x => x.IdCategory == 1) - .Where(x => x.MessageTemplate == "Шаблон сообщения 2 категории 1") - .Single(); - Assert.Equal(3, messageStat_1_2.MessagesCount); + private void AssertStatByEvent(IEnumerable result, int idWell, TelemetryEventDto eventDto, int count) + { + var eventStats = result + .Where(x => x.IdWell == idWell) + .Where(x => x.IdCategory == eventDto.IdCategory) + .Where(x => x.MessageTemplate == eventDto.MessageTemplate) + .ToArray(); - var messageStat_1_3 = result - .Where(x => x.IdWell == 1) - .Where(x => x.IdCategory == 1) - .Where(x => x.MessageTemplate == "Шаблон сообщения 3 категории 1") - .Single(); - Assert.Equal(3, messageStat_1_3.MessagesCount); - - var messageStat_2_2 = result - .Where(x => x.IdWell == 1) - .Where(x => x.IdCategory == 2) - .Where(x => x.MessageTemplate == "Шаблон сообщения 2 категории 2") - .Single(); - Assert.Equal(2, messageStat_2_2.MessagesCount); - - var messageStat_2_1 = result - .Where(x => x.IdWell == 1) - .Where(x => x.IdCategory == 2) - .Where(x => x.MessageTemplate == "Шаблон сообщения 1 категории 2") - .Single(); - Assert.Equal(2, messageStat_2_1.MessagesCount); - - var messageStat_3_1 = result - .Where(x => x.IdWell == 2) - .Where(x => x.IdCategory == 3) - .Where(x => x.MessageTemplate == "Шаблон сообщения 1 категории 3") - .Single(); - Assert.Equal(3, messageStat_3_1.MessagesCount); + Assert.Single(eventStats); + var eventStat = eventStats.First(); + Assert.Equal(count, eventStat.MessagesCount); }