From cdb6ed73905aa8e1b7749a2073f93950c0801ef2 Mon Sep 17 00:00:00 2001 From: Olga Nemt Date: Tue, 10 Sep 2024 10:16:31 +0500 Subject: [PATCH] =?UTF-8?q?1.=20=D0=9A=D1=80=D0=B8=D1=82=D0=B8=D1=87=D0=B5?= =?UTF-8?q?=D1=81=D0=BA=D0=B8=D0=B5=20=D1=81=D0=BE=D0=BE=D0=B1=D1=89=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D1=8F=202.=20=D0=A0=D0=B5=D1=84=D0=B0=D0=BA=D1=82?= =?UTF-8?q?=D0=BE=D1=80=D0=B8=D0=BD=D0=B3=20=D0=BA=D0=BE=D0=B4=D0=B0,=20?= =?UTF-8?q?=D1=81=D0=B2=D1=8F=D0=B7=D0=B0=D0=BD=D0=BD=D0=BE=D0=B3=D0=BE=20?= =?UTF-8?q?=D1=81=20=D1=81=D0=BE=D0=BE=D0=B1=D1=89=D0=B5=D0=BD=D0=B8=D1=8F?= =?UTF-8?q?=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(); }