Актуализировать работу с технологическими сообщениями под Event Service

This commit is contained in:
Roman Efremov 2024-12-12 11:47:52 +05:00
parent 21f7158fe5
commit 5ad342a044
13 changed files with 218 additions and 169 deletions

View File

@ -52,7 +52,7 @@ public class TechMessagesController : ControllerBase
/// <param name="token"></param>
/// <returns></returns>
[HttpGet("statistics")]
public async Task<ActionResult<IEnumerable<MessagesStatisticDto>>> GetStatistics([FromQuery] IEnumerable<string> autoDrillingSystem, [FromQuery] IEnumerable<int> categoryIds, CancellationToken token)
public async Task<ActionResult<IEnumerable<MessagesStatisticDto>>> GetStatistics([FromQuery] IEnumerable<Guid> autoDrillingSystem, [FromQuery] IEnumerable<int> categoryIds, CancellationToken token)
{
var result = await techMessagesRepository.GetStatistics(autoDrillingSystem, categoryIds, token);
@ -103,16 +103,17 @@ public class TechMessagesController : ControllerBase
/// <summary>
/// Добавить новые технологические сообщения
/// </summary>
/// <param name="systemId"></param>
/// <param name="dtos"></param>
/// <param name="token"></param>
/// <returns></returns>
[HttpPost]
[HttpPost("{systemId}")]
[ProducesResponseType(typeof(int), (int)HttpStatusCode.Created)]
public async Task<IActionResult> AddRange([FromBody] IEnumerable<TechMessageDto> dtos, CancellationToken token)
public async Task<IActionResult> AddRange([FromRoute] Guid systemId, [FromBody] IEnumerable<TechMessageDto> dtos, CancellationToken token)
{
var userId = User.GetUserId<Guid>();
var result = await techMessagesRepository.AddRange(dtos, userId, token);
var result = await techMessagesRepository.AddRange(systemId, dtos, userId, token);
return CreatedAtAction(nameof(AddRange), result);
}

View File

@ -14,7 +14,7 @@ public interface ITechMessagesClient : IDisposable
/// <param name="dtos"></param>
/// <param name="token"></param>
/// <returns></returns>
Task<int> AddRange(IEnumerable<TechMessageDto> dtos, CancellationToken token);
Task<int> AddRange(Guid systemId, IEnumerable<TechMessageDto> dtos, CancellationToken token);
/// <summary>
/// Получить диапазон дат, для которых есть данные в репозитории
@ -43,16 +43,16 @@ public interface ITechMessagesClient : IDisposable
/// <summary>
/// Получить статистику по системам
/// </summary>
/// <param name="autoDrillingSystem"></param>
/// <param name="systemIds"></param>
/// <param name="categoryId"></param>
/// <param name="token"></param>
/// <returns></returns>
Task<IEnumerable<MessagesStatisticDto>> GetStatistics(string autoDrillingSystem, int categoryId, CancellationToken token);
Task<IEnumerable<MessagesStatisticDto>> GetStatistics(IEnumerable<Guid> systemIds, IEnumerable<int> categoryIds, CancellationToken token);
/// <summary>
/// Получить список всех систем
/// </summary>
/// <param name="token"></param>
/// <returns></returns>
Task<IEnumerable<string>> GetSystems(CancellationToken token);
Task<IEnumerable<DrillingSystemDto>> GetSystems(CancellationToken token);
}

View File

@ -1,4 +1,5 @@
using Persistence.Models;
using Microsoft.AspNetCore.Mvc;
using Persistence.Models;
using Persistence.Models.Requests;
using Refit;
@ -11,11 +12,11 @@ namespace Persistence.Client.Clients.Interfaces.Refit
[Get($"{BaseRoute}")]
Task<IApiResponse<PaginationContainer<TechMessageDto>>> GetPage([Query] PaginationRequest request, CancellationToken token);
[Post($"{BaseRoute}")]
Task<IApiResponse<int>> AddRange([Body] IEnumerable<TechMessageDto> dtos, CancellationToken token);
[Post($"{BaseRoute}/{{systemId}}")]
Task<IApiResponse<int>> AddRange(Guid systemId, [Body] IEnumerable<TechMessageDto> dtos, CancellationToken token);
[Get($"{BaseRoute}/systems")]
Task<IApiResponse<IEnumerable<string>>> GetSystems(CancellationToken token);
Task<IApiResponse<IEnumerable<DrillingSystemDto>>> GetSystems(CancellationToken token);
[Get($"{BaseRoute}/range")]
Task<IApiResponse<DatesRangeDto>> GetDatesRangeAsync(CancellationToken token);
@ -24,6 +25,6 @@ namespace Persistence.Client.Clients.Interfaces.Refit
Task<IApiResponse<IEnumerable<TechMessageDto>>> GetPart(DateTimeOffset dateBegin, int take, CancellationToken token);
[Get($"{BaseRoute}/statistics")]
Task<IApiResponse<IEnumerable<MessagesStatisticDto>>> GetStatistics([Query] string autoDrillingSystem, [Query] int categoryId, CancellationToken token);
Task<IApiResponse<IEnumerable<MessagesStatisticDto>>> GetStatistics([Query] IEnumerable<Guid> systemIds, [Query] IEnumerable<int> categoryIds, CancellationToken token);
}
}

View File

@ -24,17 +24,17 @@ public class TechMessagesClient : BaseClient, ITechMessagesClient
return result;
}
public async Task<int> AddRange(IEnumerable<TechMessageDto> dtos, CancellationToken token)
public async Task<int> AddRange(Guid systemId, IEnumerable<TechMessageDto> dtos, CancellationToken token)
{
var result = await ExecutePostResponse(
async () => await refitTechMessagesClient.AddRange(dtos, token), token);
async () => await refitTechMessagesClient.AddRange(systemId, dtos, token), token);
return result;
}
public async Task<IEnumerable<string>> GetSystems(CancellationToken token)
public async Task<IEnumerable<DrillingSystemDto>> GetSystems(CancellationToken token)
{
var result = await ExecuteGetResponse<IEnumerable<string>>(
var result = await ExecuteGetResponse<IEnumerable<DrillingSystemDto>>(
async () => await refitTechMessagesClient.GetSystems(token), token);
return result;
@ -56,10 +56,10 @@ public class TechMessagesClient : BaseClient, ITechMessagesClient
return result;
}
public async Task<IEnumerable<MessagesStatisticDto>> GetStatistics(string autoDrillingSystem, int categoryId, CancellationToken token)
public async Task<IEnumerable<MessagesStatisticDto>> GetStatistics(IEnumerable<Guid> systemIds, IEnumerable<int> categoryIds, CancellationToken token)
{
var result = await ExecuteGetResponse<IEnumerable<MessagesStatisticDto>>(
async () => await refitTechMessagesClient.GetStatistics(autoDrillingSystem, categoryId, token), token);
async () => await refitTechMessagesClient.GetStatistics(systemIds, categoryIds, token), token);
return result;
}

View File

@ -9,10 +9,10 @@ using Persistence.Database.Model;
#nullable disable
namespace Persistence.Database.Postgres.Migrations
namespace Persistence.Database.Postgres.Migrations.PersistencePostgres
{
[DbContext(typeof(PersistenceDbContext))]
[Migration("20241202072250_TechMessageMigration")]
[DbContext(typeof(PersistencePostgresContext))]
[Migration("20241212041758_TechMessageMigration")]
partial class TechMessageMigration
{
/// <inheritdoc />
@ -48,6 +48,30 @@ namespace Persistence.Database.Postgres.Migrations
b.ToTable("DrillingSystem");
});
modelBuilder.Entity("Persistence.Database.Entity.ParameterData", b =>
{
b.Property<Guid>("DiscriminatorId")
.HasColumnType("uuid")
.HasComment("Дискриминатор системы");
b.Property<int>("ParameterId")
.HasColumnType("integer")
.HasComment("Id параметра");
b.Property<DateTimeOffset>("Timestamp")
.HasColumnType("timestamp with time zone")
.HasComment("Временная отметка");
b.Property<string>("Value")
.IsRequired()
.HasColumnType("varchar(256)")
.HasComment("Значение параметра в виде строки");
b.HasKey("DiscriminatorId", "ParameterId", "Timestamp");
b.ToTable("ParameterData");
});
modelBuilder.Entity("Persistence.Database.Entity.TechMessage", b =>
{
b.Property<Guid>("EventId")
@ -59,27 +83,23 @@ namespace Persistence.Database.Postgres.Migrations
.HasColumnType("integer")
.HasComment("Id Категории важности");
b.Property<double?>("Depth")
.HasColumnType("double precision")
.HasComment("Глубина забоя");
b.Property<int>("EventState")
.HasColumnType("integer")
.HasComment("Статус события");
b.Property<string>("MessageText")
b.Property<Guid>("SystemId")
.HasColumnType("uuid")
.HasComment("Id системы, к которой относится сообщение");
b.Property<string>("Text")
.IsRequired()
.HasColumnType("varchar(512)")
.HasComment("Текст сообщения");
b.Property<Guid>("SystemId")
.HasColumnType("uuid")
.HasComment("Id системы автобурения, к которой относится сообщение");
b.Property<DateTimeOffset>("Timestamp")
.HasColumnType("timestamp with time zone")
.HasComment("Дата возникновения");
b.Property<Guid>("UserId")
.HasColumnType("uuid")
.HasComment("Id пользователя за пультом бурильщика");
b.HasKey("EventId");
b.HasIndex("SystemId");
@ -110,6 +130,59 @@ namespace Persistence.Database.Postgres.Migrations
});
});
modelBuilder.Entity("Persistence.Database.Model.ChangeLog", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("uuid")
.HasComment("Ключ записи");
b.Property<DateTimeOffset>("Creation")
.HasColumnType("timestamp with time zone")
.HasComment("Дата создания записи");
b.Property<double>("DepthEnd")
.HasColumnType("double precision")
.HasComment("Глубина забоя на дату окончания интервала");
b.Property<double>("DepthStart")
.HasColumnType("double precision")
.HasComment("Глубина забоя на дату начала интервала");
b.Property<Guid>("IdAuthor")
.HasColumnType("uuid")
.HasComment("Автор изменения");
b.Property<Guid>("IdDiscriminator")
.HasColumnType("uuid")
.HasComment("Дискриминатор таблицы");
b.Property<Guid?>("IdEditor")
.HasColumnType("uuid")
.HasComment("Редактор");
b.Property<Guid?>("IdNext")
.HasColumnType("uuid")
.HasComment("Id заменяющей записи");
b.Property<Guid>("IdSection")
.HasColumnType("uuid")
.HasComment("Ключ секции");
b.Property<DateTimeOffset?>("Obsolete")
.HasColumnType("timestamp with time zone")
.HasComment("Дата устаревания (например при удалении)");
b.Property<string>("Value")
.IsRequired()
.HasColumnType("jsonb")
.HasComment("Значение");
b.HasKey("Id");
b.ToTable("ChangeLog");
});
modelBuilder.Entity("Persistence.Database.Model.DataSaub", b =>
{
b.Property<DateTimeOffset>("Date")

View File

@ -3,7 +3,7 @@ using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace Persistence.Database.Postgres.Migrations
namespace Persistence.Database.Postgres.Migrations.PersistencePostgres
{
/// <inheritdoc />
public partial class TechMessageMigration : Migration
@ -31,10 +31,9 @@ namespace Persistence.Database.Postgres.Migrations
EventId = table.Column<Guid>(type: "uuid", nullable: false, comment: "Id события"),
CategoryId = table.Column<int>(type: "integer", nullable: false, comment: "Id Категории важности"),
Timestamp = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, comment: "Дата возникновения"),
Depth = table.Column<double>(type: "double precision", nullable: true, comment: "Глубина забоя"),
MessageText = table.Column<string>(type: "varchar(512)", nullable: false, comment: "Текст сообщения"),
SystemId = table.Column<Guid>(type: "uuid", nullable: false, comment: "Id системы автобурения, к которой относится сообщение"),
UserId = table.Column<Guid>(type: "uuid", nullable: false, comment: "Id пользователя за пультом бурильщика")
Text = table.Column<string>(type: "varchar(512)", nullable: false, comment: "Текст сообщения"),
SystemId = table.Column<Guid>(type: "uuid", nullable: false, comment: "Id системы, к которой относится сообщение"),
EventState = table.Column<int>(type: "integer", nullable: false, comment: "Статус события")
},
constraints: table =>
{

View File

@ -1,6 +1,5 @@
// <auto-generated />
using System;
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
@ -48,8 +47,8 @@ namespace Persistence.Database.Postgres.Migrations
modelBuilder.Entity("Persistence.Database.Entity.ParameterData", b =>
{
b.Property<int>("DiscriminatorId")
.HasColumnType("integer")
b.Property<Guid>("DiscriminatorId")
.HasColumnType("uuid")
.HasComment("Дискриминатор системы");
b.Property<int>("ParameterId")
@ -81,27 +80,23 @@ namespace Persistence.Database.Postgres.Migrations
.HasColumnType("integer")
.HasComment("Id Категории важности");
b.Property<double?>("Depth")
.HasColumnType("double precision")
.HasComment("Глубина забоя");
b.Property<int>("EventState")
.HasColumnType("integer")
.HasComment("Статус события");
b.Property<string>("MessageText")
b.Property<Guid>("SystemId")
.HasColumnType("uuid")
.HasComment("Id системы, к которой относится сообщение");
b.Property<string>("Text")
.IsRequired()
.HasColumnType("varchar(512)")
.HasComment("Текст сообщения");
b.Property<Guid>("SystemId")
.HasColumnType("uuid")
.HasComment("Id системы автобурения, к которой относится сообщение");
b.Property<DateTimeOffset>("Timestamp")
.HasColumnType("timestamp with time zone")
.HasComment("Дата возникновения");
b.Property<Guid>("UserId")
.HasColumnType("uuid")
.HasComment("Id пользователя за пультом бурильщика");
b.HasKey("EventId");
b.HasIndex("SystemId");
@ -137,48 +132,48 @@ namespace Persistence.Database.Postgres.Migrations
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("uuid")
.HasColumnName("Id");
.HasComment("Ключ записи");
b.Property<DateTimeOffset>("Creation")
.HasColumnType("timestamp with time zone")
.HasColumnName("Creation");
.HasComment("Дата создания записи");
b.Property<double>("DepthEnd")
.HasColumnType("double precision")
.HasColumnName("DepthEnd");
.HasComment("Глубина забоя на дату окончания интервала");
b.Property<double>("DepthStart")
.HasColumnType("double precision")
.HasColumnName("DepthStart");
.HasComment("Глубина забоя на дату начала интервала");
b.Property<Guid>("IdAuthor")
.HasColumnType("uuid")
.HasColumnName("IdAuthor");
.HasComment("Автор изменения");
b.Property<Guid>("IdDiscriminator")
.HasColumnType("uuid")
.HasColumnName("IdDiscriminator");
.HasComment("Дискриминатор таблицы");
b.Property<Guid?>("IdEditor")
.HasColumnType("uuid")
.HasColumnName("IdEditor");
.HasComment("Редактор");
b.Property<Guid?>("IdNext")
.HasColumnType("uuid")
.HasColumnName("IdNext");
.HasComment("Id заменяющей записи");
b.Property<Guid>("IdSection")
.HasColumnType("uuid")
.HasColumnName("IdSection");
.HasComment("Ключ секции");
b.Property<DateTimeOffset?>("Obsolete")
.HasColumnType("timestamp with time zone")
.HasColumnName("Obsolete");
.HasComment("Дата устаревания (например при удалении)");
b.Property<IDictionary<string, object>>("Value")
b.Property<string>("Value")
.IsRequired()
.HasColumnType("jsonb")
.HasColumnName("Value");
.HasComment("Значение");
b.HasKey("Id");

View File

@ -15,19 +15,16 @@ namespace Persistence.Database.Entity
[Comment("Дата возникновения")]
public DateTimeOffset Timestamp { get; set; }
[Comment("Глубина забоя")]
public double? Depth { get; set; }
[Column(TypeName = "varchar(512)"), Comment("Текст сообщения")]
public required string MessageText { get; set; }
public required string Text { get; set; }
[Required, Comment("Id системы автобурения, к которой относится сообщение")]
[Required, Comment("Id системы, к которой относится сообщение")]
public required Guid SystemId { get; set; }
[Required, ForeignKey(nameof(SystemId)), Comment("Система автобурения, к которой относится сообщение")]
public virtual required DrillingSystem System { get; set; }
[Comment("Id пользователя за пультом бурильщика")]
public Guid UserId { get; set; }
[Comment("Статус события")]
public int EventState { get; set; }
}
}

View File

@ -4,6 +4,7 @@ using Persistence.Client;
using Persistence.Client.Clients.Interfaces;
using Persistence.Database.Entity;
using Persistence.Models;
using Persistence.Models.Enumerations;
using Persistence.Models.Requests;
using Xunit;
@ -53,7 +54,7 @@ namespace Persistence.IntegrationTests.Controllers
public async Task GetPage_AfterSave_returns_success()
{
//arrange
var dtos = await InsertRange();
var dtos = await InsertRange(Guid.NewGuid());
var dtosCount = dtos.Count();
var requestDto = new PaginationRequest()
{
@ -73,7 +74,7 @@ namespace Persistence.IntegrationTests.Controllers
[Fact]
public async Task InsertRange_returns_success()
{
await InsertRange();
await InsertRange(Guid.NewGuid());
}
[Fact]
@ -81,6 +82,7 @@ namespace Persistence.IntegrationTests.Controllers
{
//arrange
const string exceptionMessage = "Ошибка валидации, формата или маршрутизации запроса";
var systemId = Guid.NewGuid();
var dtos = new List<TechMessageDto>()
{
new TechMessageDto()
@ -88,17 +90,15 @@ namespace Persistence.IntegrationTests.Controllers
EventId = Guid.NewGuid(),
CategoryId = -1, // < 0
Timestamp = DateTimeOffset.UtcNow,
Depth = -1, // < 0
MessageText = string.Empty, // length < 0
System = string.Concat(Enumerable.Repeat(nameof(TechMessageDto.System), 100)), // length > 256
UserId = Guid.NewGuid()
Text = string.Empty, // length < 0
EventState = EventState.Triggered
}
};
try
{
//act
var response = await techMessagesClient.AddRange(dtos, new CancellationToken());
var response = await techMessagesClient.AddRange(systemId, dtos, new CancellationToken());
}
catch (Exception ex)
{
@ -127,19 +127,15 @@ namespace Persistence.IntegrationTests.Controllers
public async Task GetSystems_AfterSave_returns_success()
{
//arrange
var dtos = await InsertRange();
var systems = dtos
.Select(e => e.System)
.Distinct()
.ToArray();
var dtos = await InsertRange(Guid.NewGuid());
//act
var response = await techMessagesClient.GetSystems(CancellationToken.None);
//assert
Assert.NotNull(response);
string?[]? content = response?.ToArray();
Assert.Equal(systems, content);
var expectedSystemCount = 1;
Assert.Equal(expectedSystemCount, response!.Count());
}
[Fact]
@ -150,11 +146,11 @@ namespace Persistence.IntegrationTests.Controllers
dbContext.CleanupDbSet<TechMessage>();
dbContext.CleanupDbSet<Database.Entity.DrillingSystem>();
var imortantId = 1;
var autoDrillingSystem = nameof(TechMessageDto.System);
var imortantIds = new [] { 1 };
var systemIds = new [] { Guid.NewGuid() };
//act
var response = await techMessagesClient.GetStatistics(autoDrillingSystem, imortantId, CancellationToken.None);
var response = await techMessagesClient.GetStatistics(systemIds, imortantIds, CancellationToken.None);
//assert
Assert.NotNull(response);
@ -165,19 +161,20 @@ namespace Persistence.IntegrationTests.Controllers
public async Task GetStatistics_AfterSave_returns_success()
{
//arrange
var imortantId = 0;
var autoDrillingSystem = nameof(TechMessageDto.System);
var dtos = await InsertRange();
var filteredDtos = dtos.Where(e => e.CategoryId == imortantId && e.System == autoDrillingSystem);
var categoryIds = new[] { 1 };
var systemId = Guid.NewGuid();
var dtos = await InsertRange(systemId);
var filteredDtos = dtos.Where(e => categoryIds.Contains(e.CategoryId));
//act
var response = await techMessagesClient.GetStatistics(autoDrillingSystem, imortantId, CancellationToken.None);
var response = await techMessagesClient.GetStatistics([systemId], categoryIds, CancellationToken.None);
//assert
Assert.NotNull(response);
Assert.NotEmpty(response);
var categories = response
.FirstOrDefault()?.Categories
.FirstOrDefault(e => e.Key == 0).Value;
.FirstOrDefault()!.Categories
.Count();
Assert.Equal(filteredDtos.Count(), categories);
}
@ -202,7 +199,7 @@ namespace Persistence.IntegrationTests.Controllers
public async Task GetDatesRange_AfterSave_returns_success()
{
//arrange
await InsertRange();
await InsertRange(Guid.NewGuid());
//act
var response = await techMessagesClient.GetDatesRangeAsync(CancellationToken.None);
@ -234,7 +231,7 @@ namespace Persistence.IntegrationTests.Controllers
//arrange
var dateBegin = DateTimeOffset.UtcNow;
var take = 1;
await InsertRange();
await InsertRange(Guid.NewGuid());
//act
var response = await techMessagesClient.GetPart(dateBegin, take, CancellationToken.None);
@ -244,7 +241,7 @@ namespace Persistence.IntegrationTests.Controllers
Assert.NotEmpty(response);
}
private async Task<IEnumerable<TechMessageDto>> InsertRange()
private async Task<IEnumerable<TechMessageDto>> InsertRange(Guid systemId)
{
//arrange
memoryCache.Remove(SystemCacheKey);
@ -258,26 +255,22 @@ namespace Persistence.IntegrationTests.Controllers
EventId = Guid.NewGuid(),
CategoryId = 1,
Timestamp = DateTimeOffset.UtcNow,
Depth = 1.11,
MessageText = nameof(TechMessageDto.MessageText),
System = nameof(TechMessageDto.System).ToLower(),
UserId = Guid.NewGuid()
Text = nameof(TechMessageDto.Text),
EventState = Models.Enumerations.EventState.Triggered
},
new TechMessageDto()
{
EventId = Guid.NewGuid(),
CategoryId = 2,
Timestamp = DateTimeOffset.UtcNow,
Depth = 2.22,
MessageText = nameof(TechMessageDto.MessageText),
System = nameof(TechMessageDto.System).ToLower(),
UserId = Guid.NewGuid()
Text = nameof(TechMessageDto.Text),
EventState = Models.Enumerations.EventState.Triggered
}
};
//act
var response = await techMessagesClient.AddRange(dtos, CancellationToken.None);
var response = await techMessagesClient.AddRange(systemId, dtos, CancellationToken.None);
//assert
Assert.Equal(dtos.Count, response);

View File

@ -1,6 +1,7 @@
using Mapster;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Caching.Memory;
using Newtonsoft.Json.Linq;
using Persistence.Database.Entity;
using Persistence.Models;
using Persistence.Models.Requests;
@ -50,12 +51,11 @@ namespace Persistence.Repository.Repositories
return dto;
}
public async Task<IEnumerable<MessagesStatisticDto>> GetStatistics(IEnumerable<string> autoDrillingSystem, IEnumerable<int> categoryIds, CancellationToken token)
public async Task<IEnumerable<MessagesStatisticDto>> GetStatistics(IEnumerable<Guid> systems, IEnumerable<int> categoryIds, CancellationToken token)
{
var query = GetQueryReadOnly();
var systems = autoDrillingSystem.Select(s => s.ToLower().Trim());
var result = await query
.Where(e => systems.Count() == 0 || systems.Contains(e.System.Name.ToLower().Trim()))
.Where(e => systems.Count() == 0 || systems.Contains(e.System.SystemId))
.GroupBy(e => e.System.Name, (key, group) => new
{
System = key,
@ -81,27 +81,16 @@ namespace Persistence.Repository.Repositories
return entities;
}
public async Task<IEnumerable<string>> GetSystems(CancellationToken token)
{
var entities = await GetDrillingSystems(token);
var result = entities.Select(e => e.Name);
return result;
}
public async Task<int> AddRange(IEnumerable<TechMessageDto> dtos, Guid userId, CancellationToken token)
public async Task<int> AddRange(Guid systemId, IEnumerable<TechMessageDto> dtos, Guid userId, CancellationToken token)
{
var entities = new List<TechMessage>();
foreach (var dto in dtos)
{
var entity = dto.Adapt<TechMessage>();
var systems = await GetDrillingSystems(token);
var systemId = systems.FirstOrDefault(e => e.Name.ToLower().Trim() == dto.System.ToLower().Trim())?.SystemId
?? await CreateDrillingSystem(dto.System, token);
await CreateSystemIfNotExist(systemId, token);
entity.SystemId = systemId;
entity.UserId = userId;
entities.Add(entity);
}
@ -125,6 +114,22 @@ namespace Persistence.Repository.Repositories
return dtos;
}
public async Task<IEnumerable<DrillingSystemDto>> GetSystems(CancellationToken token)
{
var systems = await memoryCache.GetOrCreateAsync(SystemCacheKey, async f =>
{
f.AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(CacheExpirationInMinutes);
var query = db.Set<Database.Entity.DrillingSystem>();
var entities = await query.ToListAsync(token);
var dtos = entities.Select(e => e.Adapt<Models.DrillingSystemDto>());
return dtos;
});
return systems ?? [];
}
public async Task<DatesRangeDto> GetDatesRangeAsync(CancellationToken token)
{
var query = GetQueryReadOnly()
@ -144,35 +149,26 @@ namespace Persistence.Repository.Repositories
return result;
}
private async Task<IEnumerable<Models.DrillingSystemDto>> GetDrillingSystems(CancellationToken token)
private async Task CreateSystemIfNotExist(Guid systemId, CancellationToken token)
{
var systems = await memoryCache.GetOrCreateAsync(SystemCacheKey, async f =>
var systems = await GetSystems(token);
var system = systems?.FirstOrDefault(e => e.SystemId == systemId);
if (system == null)
{
f.AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(CacheExpirationInMinutes);
var query = db.Set<Database.Entity.DrillingSystem>();
var entities = await query.ToListAsync(token);
var dtos = entities.Select(e => e.Adapt<Models.DrillingSystemDto>());
return dtos;
});
return systems!;
}
private async Task<Guid> CreateDrillingSystem(string name, CancellationToken token)
system = new DrillingSystemDto()
{
memoryCache.Remove(SystemCacheKey);
var entity = new Database.Entity.DrillingSystem()
{
SystemId = default,
Name = name.ToLower().Trim()
SystemId = systemId,
Name = string.Empty
};
var entity = system.Adapt<DrillingSystem>();
await db.Set<Database.Entity.DrillingSystem>().AddAsync(entity);
await db.SaveChangesAsync(token);
return entity.SystemId;
memoryCache.Remove(SystemCacheKey);
}
}
}
}

View File

@ -0,0 +1,6 @@
namespace Persistence.Models.Enumerations;
public enum EventState
{
NotTriggered = 0,
Triggered = 1,
}

View File

@ -1,4 +1,5 @@
using System.ComponentModel.DataAnnotations;
using Persistence.Models.Enumerations;
namespace Persistence.Models
{
@ -24,29 +25,16 @@ namespace Persistence.Models
/// </summary>
public DateTimeOffset Timestamp { get; set; }
/// <summary>
/// Глубина забоя
/// </summary>
[Range(0, double.MaxValue, ErrorMessage = "Глубина забоя не может быть меньше 0")]
public double? Depth { get; set; }
/// <summary>
/// Текст сообщения
/// </summary>
[Required]
[StringLength(512, MinimumLength = 1, ErrorMessage = "Допустимая длина текста сообщения от 1 до 512 символов")]
public required string MessageText { get; set; }
public required string Text { get; set; }
/// <summary>
/// Система автобурения, к которой относится сообщение
/// Статус события
/// </summary>
[Required]
[StringLength(256, MinimumLength = 1, ErrorMessage = "Допустимая длина наименования системы АБ от 1 до 256 символов")]
public required string System { get; set; }
/// <summary>
/// Id пользователя за пультом бурильщика
/// </summary>
public Guid UserId { get; set; }
public EventState EventState { get; set; }
}
}

View File

@ -22,14 +22,14 @@ namespace Persistence.Repositories
/// <param name="dtos"></param>
/// <param name="token"></param>
/// <returns></returns>
Task<int> AddRange(IEnumerable<TechMessageDto> dtos, Guid userId, CancellationToken token);
Task<int> AddRange(Guid systemId, IEnumerable<TechMessageDto> dtos, Guid userId, CancellationToken token);
/// <summary>
/// Получение списка уникальных названий систем АБ
/// Получение списка систем
/// </summary>
/// <param name="token"></param>
/// <returns></returns>
Task<IEnumerable<string>> GetSystems(CancellationToken token);
Task<IEnumerable<DrillingSystemDto>> GetSystems(CancellationToken token);
/// <summary>
/// Получение количества сообщений по категориям и системам автобурения
@ -38,7 +38,7 @@ namespace Persistence.Repositories
/// <param name="autoDrillingSystem">Система автобурения</param>
/// <param name="token"></param>
/// <returns></returns>
Task<IEnumerable<MessagesStatisticDto>> GetStatistics(IEnumerable<string> autoDrillingSystem, IEnumerable<int> categoryIds, CancellationToken token);
Task<IEnumerable<MessagesStatisticDto>> GetStatistics(IEnumerable<Guid> autoDrillingSystem, IEnumerable<int> categoryIds, CancellationToken token);
/// <summary>
/// Получить порцию записей, начиная с заданной даты