2024-07-04 11:02:45 +05:00
|
|
|
using AsbCloudApp.Data;
|
2022-04-08 13:10:06 +05:00
|
|
|
using AsbCloudApp.Data.SAUB;
|
2023-02-02 11:08:25 +05:00
|
|
|
using AsbCloudApp.Requests;
|
2021-04-23 10:21:25 +05:00
|
|
|
using AsbCloudApp.Services;
|
2023-02-02 11:08:25 +05:00
|
|
|
using AsbCloudDb;
|
2021-04-23 10:21:25 +05:00
|
|
|
using AsbCloudDb.Model;
|
2021-07-28 09:46:58 +05:00
|
|
|
using Mapster;
|
2021-04-23 10:21:25 +05:00
|
|
|
using Microsoft.EntityFrameworkCore;
|
2023-02-21 18:01:03 +05:00
|
|
|
using Microsoft.Extensions.Caching.Memory;
|
2021-04-23 10:21:25 +05:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
2021-08-11 16:54:42 +05:00
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
2021-04-23 10:21:25 +05:00
|
|
|
|
2023-04-18 16:22:53 +05:00
|
|
|
|
2022-04-11 18:00:34 +05:00
|
|
|
namespace AsbCloudInfrastructure.Services.SAUB
|
2021-04-23 10:21:25 +05:00
|
|
|
{
|
|
|
|
public class MessageService : IMessageService
|
|
|
|
{
|
|
|
|
private readonly IAsbCloudDbContext db;
|
2023-02-21 18:01:03 +05:00
|
|
|
private readonly IMemoryCache memoryCache;
|
2021-04-23 10:21:25 +05:00
|
|
|
private readonly ITelemetryService telemetryService;
|
|
|
|
|
2023-02-21 18:01:03 +05:00
|
|
|
public MessageService(IAsbCloudDbContext db, IMemoryCache memoryCache, ITelemetryService telemetryService)
|
2021-04-23 10:21:25 +05:00
|
|
|
{
|
|
|
|
this.db = db;
|
2023-02-21 18:01:03 +05:00
|
|
|
this.memoryCache = memoryCache;
|
2021-04-23 10:21:25 +05:00
|
|
|
this.telemetryService = telemetryService;
|
|
|
|
}
|
|
|
|
|
2023-02-02 11:08:25 +05:00
|
|
|
public async Task<PaginationContainer<MessageDto>> GetMessagesAsync(MessageRequest request, CancellationToken token)
|
2021-04-23 10:21:25 +05:00
|
|
|
{
|
2023-02-03 11:15:14 +05:00
|
|
|
var result = new PaginationContainer<MessageDto>
|
|
|
|
{
|
|
|
|
Skip = request.Skip ?? 0,
|
|
|
|
Take = request.Take ?? 32,
|
|
|
|
};
|
2023-02-02 16:07:41 +05:00
|
|
|
|
2023-03-07 09:12:49 +05:00
|
|
|
var telemetry = telemetryService.GetOrDefaultTelemetryByIdWell(request.IdWell);
|
|
|
|
if (telemetry is null)
|
2023-02-03 11:15:14 +05:00
|
|
|
return result;
|
2021-04-23 10:21:25 +05:00
|
|
|
|
2023-02-21 18:01:03 +05:00
|
|
|
var allEvents = await memoryCache.GetOrCreateBasicAsync(db.Set<TelemetryEvent>(), token);
|
2023-03-07 09:12:49 +05:00
|
|
|
var events = allEvents.Where(e => e.IdTelemetry == telemetry.Id);
|
2021-04-23 10:21:25 +05:00
|
|
|
|
2021-05-21 11:02:43 +05:00
|
|
|
if (!events.Any())
|
2023-02-03 11:15:14 +05:00
|
|
|
return result;
|
2021-04-23 10:21:25 +05:00
|
|
|
|
2023-03-07 09:12:49 +05:00
|
|
|
var query = db.TelemetryMessages.Where(m => m.IdTelemetry == telemetry.Id)
|
2022-04-01 17:55:44 +05:00
|
|
|
.OrderBy(m => m.DateTime).AsNoTracking();
|
2021-04-23 10:21:25 +05:00
|
|
|
|
2023-02-02 16:07:41 +05:00
|
|
|
if (request.Categoryids?.Any() == true || !string.IsNullOrEmpty(request.SearchString))
|
2021-04-23 10:21:25 +05:00
|
|
|
{
|
2023-02-02 16:07:41 +05:00
|
|
|
if (!string.IsNullOrEmpty(request.SearchString))
|
|
|
|
events = events.Where(e => e.MessageTemplate.Contains(request.SearchString, StringComparison.OrdinalIgnoreCase));
|
2021-08-09 15:09:46 +05:00
|
|
|
|
2023-02-02 16:07:41 +05:00
|
|
|
if (request.Categoryids?.Any() == true)
|
|
|
|
events = events.Where(e => request.Categoryids.ToList().Contains(e.IdCategory));
|
2021-08-09 15:09:46 +05:00
|
|
|
|
2021-08-09 15:41:42 +05:00
|
|
|
var eventIds = events.Select(e => e.IdEvent);
|
2021-04-23 10:21:25 +05:00
|
|
|
|
2021-05-19 15:21:19 +05:00
|
|
|
if (!eventIds.Any())
|
2023-02-03 11:15:14 +05:00
|
|
|
return result;
|
2021-04-23 10:21:25 +05:00
|
|
|
|
2021-08-10 14:36:35 +05:00
|
|
|
query = query.Where(m => eventIds.Contains(m.IdEvent));
|
2021-04-23 10:21:25 +05:00
|
|
|
}
|
|
|
|
|
2022-04-01 17:55:44 +05:00
|
|
|
query = query.OrderByDescending(m => m.DateTime);
|
2021-04-23 10:21:25 +05:00
|
|
|
|
2023-02-03 11:15:14 +05:00
|
|
|
if (request.Begin is not null)
|
2021-12-30 17:05:44 +05:00
|
|
|
{
|
2024-03-21 13:58:28 +05:00
|
|
|
var beginUtc = request.Begin.Value.ToUniversalTime();
|
2022-04-01 17:55:44 +05:00
|
|
|
query = query.Where(m => m.DateTime >= beginUtc);
|
2022-04-11 18:00:34 +05:00
|
|
|
}
|
2021-04-23 10:21:25 +05:00
|
|
|
|
2023-02-03 11:15:14 +05:00
|
|
|
if (request.End is not null)
|
2021-12-30 17:05:44 +05:00
|
|
|
{
|
2024-03-21 13:58:28 +05:00
|
|
|
var endUtc = request.End.Value.ToUniversalTime();
|
2022-04-01 17:55:44 +05:00
|
|
|
query = query.Where(m => m.DateTime <= endUtc);
|
2022-04-11 18:00:34 +05:00
|
|
|
}
|
2021-04-23 10:21:25 +05:00
|
|
|
|
2023-02-03 11:15:14 +05:00
|
|
|
result.Count = query.Count();
|
2021-04-23 10:21:25 +05:00
|
|
|
|
2023-02-02 16:07:41 +05:00
|
|
|
if (request.SortFields?.Any() == true)
|
2023-02-02 11:08:25 +05:00
|
|
|
{
|
2023-02-02 15:58:07 +05:00
|
|
|
query = query.SortBy(request.SortFields);
|
2023-02-02 11:08:25 +05:00
|
|
|
}
|
2023-02-03 11:15:14 +05:00
|
|
|
var messagesList = await query.Skip(result.Skip)
|
|
|
|
.Take(result.Take).AsNoTracking()
|
2023-02-02 15:58:07 +05:00
|
|
|
.ToListAsync(token).ConfigureAwait(false);
|
2021-04-23 10:21:25 +05:00
|
|
|
|
|
|
|
if (messagesList.Count == 0)
|
|
|
|
return result;
|
|
|
|
|
2023-02-21 18:01:03 +05:00
|
|
|
var allUsers = await memoryCache.GetOrCreateBasicAsync(db.Set<TelemetryUser>(), token);
|
2023-03-07 09:12:49 +05:00
|
|
|
var users = allUsers.Where(u => u.IdTelemetry == telemetry.Id);
|
2021-04-23 10:21:25 +05:00
|
|
|
|
2023-02-03 11:15:14 +05:00
|
|
|
var eventsDict = events.ToDictionary(x=>x.IdEvent, x => x);
|
|
|
|
var usersDict = users.ToDictionary(x => x.IdUser, x => x);
|
2023-02-02 11:08:25 +05:00
|
|
|
|
2023-07-18 14:41:20 +05:00
|
|
|
var messagesDtoList = new List<MessageDto>();
|
2024-03-28 11:00:55 +05:00
|
|
|
var timezone = telemetryService.GetTimezone(telemetry.Id);
|
2023-07-18 14:41:20 +05:00
|
|
|
|
2021-04-23 10:21:25 +05:00
|
|
|
foreach (var message in messagesList)
|
|
|
|
{
|
|
|
|
var messageDto = new MessageDto
|
|
|
|
{
|
|
|
|
Id = message.Id,
|
2021-09-24 11:17:31 +05:00
|
|
|
WellDepth = message.WellDepth
|
2021-04-23 10:21:25 +05:00
|
|
|
};
|
|
|
|
|
2024-03-22 11:15:10 +05:00
|
|
|
messageDto.DateTime = message.DateTime.ToOffset(TimeSpan.FromHours(timezone.Hours));
|
2021-12-30 17:05:44 +05:00
|
|
|
|
2021-09-20 10:33:15 +05:00
|
|
|
if (message.IdTelemetryUser is not null)
|
|
|
|
{
|
2023-02-03 11:15:14 +05:00
|
|
|
if (usersDict.TryGetValue((int)message.IdTelemetryUser, out TelemetryUser? user))
|
2021-09-20 10:33:15 +05:00
|
|
|
{
|
|
|
|
messageDto.User = user.MakeDisplayName();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
messageDto.User = message.IdTelemetryUser.ToString();
|
|
|
|
}
|
|
|
|
|
2023-02-03 11:15:14 +05:00
|
|
|
if (eventsDict.TryGetValue(message.IdEvent, out TelemetryEvent? e))
|
2021-04-23 10:21:25 +05:00
|
|
|
{
|
|
|
|
messageDto.CategoryId = e.IdCategory;
|
|
|
|
messageDto.Message = e.MakeMessageText(message);
|
|
|
|
}
|
2023-07-18 14:41:20 +05:00
|
|
|
messagesDtoList.Add(messageDto);
|
2021-04-23 10:21:25 +05:00
|
|
|
}
|
2022-04-11 18:00:34 +05:00
|
|
|
|
2023-07-18 14:41:20 +05:00
|
|
|
result.Items = result.Items.Concat(messagesDtoList);
|
|
|
|
|
2021-04-23 10:21:25 +05:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-08-11 16:54:42 +05:00
|
|
|
public Task InsertAsync(string uid, IEnumerable<TelemetryMessageDto> dtos,
|
|
|
|
CancellationToken token = default)
|
2021-04-23 10:21:25 +05:00
|
|
|
{
|
2021-05-21 11:02:43 +05:00
|
|
|
if (!dtos.Any())
|
2023-02-03 11:15:14 +05:00
|
|
|
return Task.CompletedTask;
|
2021-04-23 10:21:25 +05:00
|
|
|
|
2023-06-20 14:41:19 +05:00
|
|
|
var telemetry = telemetryService.GetOrCreateTelemetryByUid(uid);
|
2024-05-13 09:56:02 +05:00
|
|
|
|
2021-04-23 10:21:25 +05:00
|
|
|
foreach (var dto in dtos)
|
|
|
|
{
|
2022-04-11 18:00:34 +05:00
|
|
|
var entity = dto.Adapt<TelemetryMessage>();
|
2021-04-30 17:35:35 +05:00
|
|
|
entity.Id = 0;
|
2023-06-20 14:41:19 +05:00
|
|
|
entity.IdTelemetry = telemetry.Id;
|
2024-05-13 09:56:02 +05:00
|
|
|
entity.DateTime = dto.Date.ToUniversalTime();
|
2021-08-10 16:37:13 +05:00
|
|
|
db.TelemetryMessages.Add(entity);
|
2021-04-23 10:21:25 +05:00
|
|
|
}
|
|
|
|
|
2021-08-11 16:54:42 +05:00
|
|
|
return db.SaveChangesAsync(token);
|
2021-04-23 10:21:25 +05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-04-18 16:22:53 +05:00
|
|
|
|