diff --git a/Persistence.API/Controllers/TechMessagesController.cs b/Persistence.API/Controllers/TechMessagesController.cs index 10efb04..12b23b3 100644 --- a/Persistence.API/Controllers/TechMessagesController.cs +++ b/Persistence.API/Controllers/TechMessagesController.cs @@ -1,4 +1,4 @@ -using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Persistence.Models; using Persistence.Models.Requests; @@ -44,17 +44,17 @@ public class TechMessagesController : ControllerBase return Ok(result); } - /// - /// Получить статистику по системам - /// - /// - /// - /// - /// - [HttpGet("statistics")] - public async Task>> GetStatistics([FromQuery] IEnumerable autoDrillingSystem, [FromQuery] IEnumerable categoryIds, CancellationToken token) - { - var result = await techMessagesRepository.GetStatistics(autoDrillingSystem, categoryIds, token); + /// + /// Получить статистику по системам + /// + /// + /// + /// + /// + [HttpGet("statistics")] + public async Task>> GetStatistics([FromQuery] IEnumerable autoDrillingSystem, [FromQuery] IEnumerable categoryIds, CancellationToken token) + { + var result = await techMessagesRepository.GetStatistics(autoDrillingSystem, categoryIds, token); return Ok(result); } @@ -100,19 +100,20 @@ public class TechMessagesController : ControllerBase return Ok(result); } - /// - /// Добавить новые технологические сообщения - /// - /// - /// - /// - [HttpPost] - [ProducesResponseType(typeof(int), (int)HttpStatusCode.Created)] - public async Task AddRange([FromBody] IEnumerable dtos, CancellationToken token) - { - var userId = User.GetUserId(); + /// + /// Добавить новые технологические сообщения + /// + /// + /// + /// + /// + [HttpPost("{systemId}")] + [ProducesResponseType(typeof(int), (int)HttpStatusCode.Created)] + public async Task AddRange([FromRoute] Guid systemId, [FromBody] IEnumerable dtos, CancellationToken token) + { + var userId = User.GetUserId(); - var result = await techMessagesRepository.AddRange(dtos, userId, token); + var result = await techMessagesRepository.AddRange(systemId, dtos, userId, token); return CreatedAtAction(nameof(AddRange), result); } diff --git a/Persistence.Client/Clients/Interfaces/ITechMessagesClient.cs b/Persistence.Client/Clients/Interfaces/ITechMessagesClient.cs index bbf635c..ce8a7ca 100644 --- a/Persistence.Client/Clients/Interfaces/ITechMessagesClient.cs +++ b/Persistence.Client/Clients/Interfaces/ITechMessagesClient.cs @@ -14,7 +14,7 @@ public interface ITechMessagesClient : IDisposable /// /// /// - Task AddRange(IEnumerable dtos, CancellationToken token); + Task AddRange(Guid systemId, IEnumerable dtos, CancellationToken token); /// /// Получить диапазон дат, для которых есть данные в репозитории @@ -43,16 +43,16 @@ public interface ITechMessagesClient : IDisposable /// /// Получить статистику по системам /// - /// + /// /// /// /// - Task> GetStatistics(string autoDrillingSystem, int categoryId, CancellationToken token); + Task> GetStatistics(IEnumerable systemIds, IEnumerable categoryIds, CancellationToken token); /// /// Получить список всех систем /// /// /// - Task> GetSystems(CancellationToken token); + Task> GetSystems(CancellationToken token); } \ No newline at end of file diff --git a/Persistence.Client/Clients/Interfaces/Refit/IRefitTechMessagesClient.cs b/Persistence.Client/Clients/Interfaces/Refit/IRefitTechMessagesClient.cs index 64cb49e..138a0cd 100644 --- a/Persistence.Client/Clients/Interfaces/Refit/IRefitTechMessagesClient.cs +++ b/Persistence.Client/Clients/Interfaces/Refit/IRefitTechMessagesClient.cs @@ -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>> GetPage([Query] PaginationRequest request, CancellationToken token); - [Post($"{BaseRoute}")] - Task> AddRange([Body] IEnumerable dtos, CancellationToken token); + [Post($"{BaseRoute}/{{systemId}}")] + Task> AddRange(Guid systemId, [Body] IEnumerable dtos, CancellationToken token); [Get($"{BaseRoute}/systems")] - Task>> GetSystems(CancellationToken token); + Task>> GetSystems(CancellationToken token); [Get($"{BaseRoute}/range")] Task> GetDatesRangeAsync(CancellationToken token); @@ -24,6 +25,6 @@ namespace Persistence.Client.Clients.Interfaces.Refit Task>> GetPart(DateTimeOffset dateBegin, int take, CancellationToken token); [Get($"{BaseRoute}/statistics")] - Task>> GetStatistics([Query] string autoDrillingSystem, [Query] int categoryId, CancellationToken token); + Task>> GetStatistics([Query] IEnumerable systemIds, [Query] IEnumerable categoryIds, CancellationToken token); } } diff --git a/Persistence.Client/Clients/TechMessagesClient.cs b/Persistence.Client/Clients/TechMessagesClient.cs index c1000b1..7950784 100644 --- a/Persistence.Client/Clients/TechMessagesClient.cs +++ b/Persistence.Client/Clients/TechMessagesClient.cs @@ -24,17 +24,17 @@ public class TechMessagesClient : BaseClient, ITechMessagesClient return result; } - public async Task AddRange(IEnumerable dtos, CancellationToken token) + public async Task AddRange(Guid systemId, IEnumerable 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> GetSystems(CancellationToken token) + public async Task> GetSystems(CancellationToken token) { - var result = await ExecuteGetResponse>( + var result = await ExecuteGetResponse>( async () => await refitTechMessagesClient.GetSystems(token), token); return result; @@ -56,10 +56,10 @@ public class TechMessagesClient : BaseClient, ITechMessagesClient return result; } - public async Task> GetStatistics(string autoDrillingSystem, int categoryId, CancellationToken token) + public async Task> GetStatistics(IEnumerable systemIds, IEnumerable categoryIds, CancellationToken token) { var result = await ExecuteGetResponse>( - async () => await refitTechMessagesClient.GetStatistics(autoDrillingSystem, categoryId, token), token); + async () => await refitTechMessagesClient.GetStatistics(systemIds, categoryIds, token), token); return result; } diff --git a/Persistence.Database.Postgres/Migrations/20241202072250_TechMessageMigration.Designer.cs b/Persistence.Database.Postgres/Migrations/20241212041758_TechMessageMigration.Designer.cs similarity index 70% rename from Persistence.Database.Postgres/Migrations/20241202072250_TechMessageMigration.Designer.cs rename to Persistence.Database.Postgres/Migrations/20241212041758_TechMessageMigration.Designer.cs index 6ed33f7..0124f24 100644 --- a/Persistence.Database.Postgres/Migrations/20241202072250_TechMessageMigration.Designer.cs +++ b/Persistence.Database.Postgres/Migrations/20241212041758_TechMessageMigration.Designer.cs @@ -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 { /// @@ -48,6 +48,30 @@ namespace Persistence.Database.Postgres.Migrations b.ToTable("DrillingSystem"); }); + modelBuilder.Entity("Persistence.Database.Entity.ParameterData", b => + { + b.Property("DiscriminatorId") + .HasColumnType("uuid") + .HasComment("Дискриминатор системы"); + + b.Property("ParameterId") + .HasColumnType("integer") + .HasComment("Id параметра"); + + b.Property("Timestamp") + .HasColumnType("timestamp with time zone") + .HasComment("Временная отметка"); + + b.Property("Value") + .IsRequired() + .HasColumnType("varchar(256)") + .HasComment("Значение параметра в виде строки"); + + b.HasKey("DiscriminatorId", "ParameterId", "Timestamp"); + + b.ToTable("ParameterData"); + }); + modelBuilder.Entity("Persistence.Database.Entity.TechMessage", b => { b.Property("EventId") @@ -59,27 +83,23 @@ namespace Persistence.Database.Postgres.Migrations .HasColumnType("integer") .HasComment("Id Категории важности"); - b.Property("Depth") - .HasColumnType("double precision") - .HasComment("Глубина забоя"); + b.Property("EventState") + .HasColumnType("integer") + .HasComment("Статус события"); - b.Property("MessageText") + b.Property("SystemId") + .HasColumnType("uuid") + .HasComment("Id системы, к которой относится сообщение"); + + b.Property("Text") .IsRequired() .HasColumnType("varchar(512)") .HasComment("Текст сообщения"); - b.Property("SystemId") - .HasColumnType("uuid") - .HasComment("Id системы автобурения, к которой относится сообщение"); - b.Property("Timestamp") .HasColumnType("timestamp with time zone") .HasComment("Дата возникновения"); - b.Property("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("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid") + .HasComment("Ключ записи"); + + b.Property("Creation") + .HasColumnType("timestamp with time zone") + .HasComment("Дата создания записи"); + + b.Property("DepthEnd") + .HasColumnType("double precision") + .HasComment("Глубина забоя на дату окончания интервала"); + + b.Property("DepthStart") + .HasColumnType("double precision") + .HasComment("Глубина забоя на дату начала интервала"); + + b.Property("IdAuthor") + .HasColumnType("uuid") + .HasComment("Автор изменения"); + + b.Property("IdDiscriminator") + .HasColumnType("uuid") + .HasComment("Дискриминатор таблицы"); + + b.Property("IdEditor") + .HasColumnType("uuid") + .HasComment("Редактор"); + + b.Property("IdNext") + .HasColumnType("uuid") + .HasComment("Id заменяющей записи"); + + b.Property("IdSection") + .HasColumnType("uuid") + .HasComment("Ключ секции"); + + b.Property("Obsolete") + .HasColumnType("timestamp with time zone") + .HasComment("Дата устаревания (например при удалении)"); + + b.Property("Value") + .IsRequired() + .HasColumnType("jsonb") + .HasComment("Значение"); + + b.HasKey("Id"); + + b.ToTable("ChangeLog"); + }); + modelBuilder.Entity("Persistence.Database.Model.DataSaub", b => { b.Property("Date") diff --git a/Persistence.Database.Postgres/Migrations/20241202072250_TechMessageMigration.cs b/Persistence.Database.Postgres/Migrations/20241212041758_TechMessageMigration.cs similarity index 81% rename from Persistence.Database.Postgres/Migrations/20241202072250_TechMessageMigration.cs rename to Persistence.Database.Postgres/Migrations/20241212041758_TechMessageMigration.cs index 41ef8cb..c6806a4 100644 --- a/Persistence.Database.Postgres/Migrations/20241202072250_TechMessageMigration.cs +++ b/Persistence.Database.Postgres/Migrations/20241212041758_TechMessageMigration.cs @@ -2,7 +2,7 @@ #nullable disable -namespace Persistence.Database.Postgres.Migrations +namespace Persistence.Database.Postgres.Migrations.PersistencePostgres { /// public partial class TechMessageMigration : Migration @@ -30,10 +30,9 @@ namespace Persistence.Database.Postgres.Migrations EventId = table.Column(type: "uuid", nullable: false, comment: "Id события"), CategoryId = table.Column(type: "integer", nullable: false, comment: "Id Категории важности"), Timestamp = table.Column(type: "timestamp with time zone", nullable: false, comment: "Дата возникновения"), - Depth = table.Column(type: "double precision", nullable: true, comment: "Глубина забоя"), - MessageText = table.Column(type: "varchar(512)", nullable: false, comment: "Текст сообщения"), - SystemId = table.Column(type: "uuid", nullable: false, comment: "Id системы автобурения, к которой относится сообщение"), - UserId = table.Column(type: "uuid", nullable: false, comment: "Id пользователя за пультом бурильщика") + Text = table.Column(type: "varchar(512)", nullable: false, comment: "Текст сообщения"), + SystemId = table.Column(type: "uuid", nullable: false, comment: "Id системы, к которой относится сообщение"), + EventState = table.Column(type: "integer", nullable: false, comment: "Статус события") }, constraints: table => { diff --git a/Persistence.Database.Postgres/Migrations/PersistencePostgresContextModelSnapshot.cs b/Persistence.Database.Postgres/Migrations/PersistencePostgresContextModelSnapshot.cs index 6ab8159..5a67396 100644 --- a/Persistence.Database.Postgres/Migrations/PersistencePostgresContextModelSnapshot.cs +++ b/Persistence.Database.Postgres/Migrations/PersistencePostgresContextModelSnapshot.cs @@ -1,6 +1,5 @@ // 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("DiscriminatorId") - .HasColumnType("integer") + b.Property("DiscriminatorId") + .HasColumnType("uuid") .HasComment("Дискриминатор системы"); b.Property("ParameterId") @@ -81,27 +80,23 @@ namespace Persistence.Database.Postgres.Migrations .HasColumnType("integer") .HasComment("Id Категории важности"); - b.Property("Depth") - .HasColumnType("double precision") - .HasComment("Глубина забоя"); + b.Property("EventState") + .HasColumnType("integer") + .HasComment("Статус события"); - b.Property("MessageText") + b.Property("SystemId") + .HasColumnType("uuid") + .HasComment("Id системы, к которой относится сообщение"); + + b.Property("Text") .IsRequired() .HasColumnType("varchar(512)") .HasComment("Текст сообщения"); - b.Property("SystemId") - .HasColumnType("uuid") - .HasComment("Id системы автобурения, к которой относится сообщение"); - b.Property("Timestamp") .HasColumnType("timestamp with time zone") .HasComment("Дата возникновения"); - b.Property("UserId") - .HasColumnType("uuid") - .HasComment("Id пользователя за пультом бурильщика"); - b.HasKey("EventId"); b.HasIndex("SystemId"); @@ -137,48 +132,48 @@ namespace Persistence.Database.Postgres.Migrations b.Property("Id") .ValueGeneratedOnAdd() .HasColumnType("uuid") - .HasColumnName("Id"); + .HasComment("Ключ записи"); b.Property("Creation") .HasColumnType("timestamp with time zone") - .HasColumnName("Creation"); + .HasComment("Дата создания записи"); b.Property("DepthEnd") .HasColumnType("double precision") - .HasColumnName("DepthEnd"); + .HasComment("Глубина забоя на дату окончания интервала"); b.Property("DepthStart") .HasColumnType("double precision") - .HasColumnName("DepthStart"); + .HasComment("Глубина забоя на дату начала интервала"); b.Property("IdAuthor") .HasColumnType("uuid") - .HasColumnName("IdAuthor"); + .HasComment("Автор изменения"); b.Property("IdDiscriminator") .HasColumnType("uuid") - .HasColumnName("IdDiscriminator"); + .HasComment("Дискриминатор таблицы"); b.Property("IdEditor") .HasColumnType("uuid") - .HasColumnName("IdEditor"); + .HasComment("Редактор"); b.Property("IdNext") .HasColumnType("uuid") - .HasColumnName("IdNext"); + .HasComment("Id заменяющей записи"); b.Property("IdSection") .HasColumnType("uuid") - .HasColumnName("IdSection"); + .HasComment("Ключ секции"); b.Property("Obsolete") .HasColumnType("timestamp with time zone") - .HasColumnName("Obsolete"); + .HasComment("Дата устаревания (например при удалении)"); - b.Property>("Value") + b.Property("Value") .IsRequired() .HasColumnType("jsonb") - .HasColumnName("Value"); + .HasComment("Значение"); b.HasKey("Id"); diff --git a/Persistence.Database/Entity/TechMessage.cs b/Persistence.Database/Entity/TechMessage.cs index 8051415..e94d141 100644 --- a/Persistence.Database/Entity/TechMessage.cs +++ b/Persistence.Database/Entity/TechMessage.cs @@ -1,4 +1,4 @@ -using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; @@ -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 Text { get; set; } - [Column(TypeName = "varchar(512)"), Comment("Текст сообщения")] - public required string MessageText { get; set; } - - [Required, Comment("Id системы автобурения, к которой относится сообщение")] - public required Guid SystemId { get; set; } + [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; } + } } diff --git a/Persistence.IntegrationTests/Controllers/TechMessagesControllerTest.cs b/Persistence.IntegrationTests/Controllers/TechMessagesControllerTest.cs index 3ec7fce..97c0621 100644 --- a/Persistence.IntegrationTests/Controllers/TechMessagesControllerTest.cs +++ b/Persistence.IntegrationTests/Controllers/TechMessagesControllerTest.cs @@ -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 System.Net; using Xunit; @@ -50,18 +51,18 @@ namespace Persistence.IntegrationTests.Controllers Assert.Equal(requestDto.Take, response.Take); } - [Fact] - public async Task GetPage_AfterSave_returns_success() - { - //arrange - var dtos = await InsertRange(); - var dtosCount = dtos.Count(); - var requestDto = new PaginationRequest() - { - Skip = 0, - Take = 2, - SortSettings = nameof(TechMessage.CategoryId) - }; + [Fact] + public async Task GetPage_AfterSave_returns_success() + { + //arrange + var dtos = await InsertRange(Guid.NewGuid()); + var dtosCount = dtos.Count(); + var requestDto = new PaginationRequest() + { + Skip = 0, + Take = 2, + SortSettings = nameof(TechMessage.CategoryId) + }; //act var response = await techMessagesClient.GetPage(requestDto, CancellationToken.None); @@ -71,17 +72,18 @@ namespace Persistence.IntegrationTests.Controllers Assert.Equal(dtosCount, response.Count); } - [Fact] - public async Task InsertRange_returns_success() - { - await InsertRange(); - } + [Fact] + public async Task InsertRange_returns_success() + { + await InsertRange(Guid.NewGuid()); + } [Fact] public async Task InsertRange_returns_BadRequest() { //arrange const string exceptionMessage = "Ошибка валидации, формата или маршрутизации запроса"; + var systemId = Guid.NewGuid(); var dtos = new List() { new TechMessageDto() @@ -89,17 +91,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) { @@ -124,23 +124,19 @@ namespace Persistence.IntegrationTests.Controllers Assert.Empty(response); } - [Fact] - public async Task GetSystems_AfterSave_returns_success() - { - //arrange - var dtos = await InsertRange(); - var systems = dtos - .Select(e => e.System) - .Distinct() - .ToArray(); + [Fact] + public async Task GetSystems_AfterSave_returns_success() + { + //arrange + 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] @@ -151,34 +147,35 @@ namespace Persistence.IntegrationTests.Controllers dbContext.CleanupDbSet(); dbContext.CleanupDbSet(); - var importantId = 1; - var autoDrillingSystem = nameof(TechMessageDto.System); + var imortantIds = new [] { 1 }; + var systemIds = new [] { Guid.NewGuid() }; - //act - var response = await techMessagesClient.GetStatistics(autoDrillingSystem, importantId, CancellationToken.None); + //act + var response = await techMessagesClient.GetStatistics(systemIds, imortantIds, CancellationToken.None); //assert Assert.NotNull(response); Assert.Empty(response); } - [Fact] - public async Task GetStatistics_AfterSave_returns_success() - { - //arrange - var importantId = 0; - var autoDrillingSystem = nameof(TechMessageDto.System); - var dtos = await InsertRange(); - var filteredDtos = dtos.Where(e => e.CategoryId == importantId && e.System == autoDrillingSystem); + [Fact] + public async Task GetStatistics_AfterSave_returns_success() + { + //arrange + 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, importantId, CancellationToken.None); + //act + 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); } @@ -199,11 +196,11 @@ namespace Persistence.IntegrationTests.Controllers Assert.Equal(DateTimeOffset.MaxValue, response?.To); } - [Fact] - public async Task GetDatesRange_AfterSave_returns_success() - { - //arrange - await InsertRange(); + [Fact] + public async Task GetDatesRange_AfterSave_returns_success() + { + //arrange + await InsertRange(Guid.NewGuid()); //act var response = await techMessagesClient.GetDatesRangeAsync(CancellationToken.None); @@ -229,13 +226,13 @@ namespace Persistence.IntegrationTests.Controllers Assert.Empty(response); } - [Fact] - public async Task GetPart_AfterSave_returns_success() - { - //arrange - var dateBegin = DateTimeOffset.UtcNow; - var take = 1; - await InsertRange(); + [Fact] + public async Task GetPart_AfterSave_returns_success() + { + //arrange + var dateBegin = DateTimeOffset.UtcNow; + var take = 1; + await InsertRange(Guid.NewGuid()); //act var response = await techMessagesClient.GetPart(dateBegin, take, CancellationToken.None); @@ -245,40 +242,36 @@ namespace Persistence.IntegrationTests.Controllers Assert.NotEmpty(response); } - private async Task> InsertRange() - { - //arrange - memoryCache.Remove(SystemCacheKey); - dbContext.CleanupDbSet(); - dbContext.CleanupDbSet(); + private async Task> InsertRange(Guid systemId) + { + //arrange + memoryCache.Remove(SystemCacheKey); + dbContext.CleanupDbSet(); + dbContext.CleanupDbSet(); - var dtos = new List() - { - new() - { - EventId = Guid.NewGuid(), - CategoryId = 1, - Timestamp = DateTimeOffset.UtcNow, - Depth = 1.11, - MessageText = nameof(TechMessageDto.MessageText), - System = nameof(TechMessageDto.System).ToLower(), - UserId = Guid.NewGuid() - }, - new() - { - EventId = Guid.NewGuid(), - CategoryId = 2, - Timestamp = DateTimeOffset.UtcNow, - Depth = 2.22, - MessageText = nameof(TechMessageDto.MessageText), - System = nameof(TechMessageDto.System).ToLower(), - UserId = Guid.NewGuid() - } - }; + var dtos = new List() + { + new TechMessageDto() + { + EventId = Guid.NewGuid(), + CategoryId = 1, + Timestamp = DateTimeOffset.UtcNow, + Text = nameof(TechMessageDto.Text), + EventState = Models.Enumerations.EventState.Triggered + }, + new TechMessageDto() + { + EventId = Guid.NewGuid(), + CategoryId = 2, + Timestamp = DateTimeOffset.UtcNow, + Text = nameof(TechMessageDto.Text), + EventState = Models.Enumerations.EventState.Triggered + } + }; - //act - var response = await techMessagesClient.AddRange(dtos, CancellationToken.None); + //act + var response = await techMessagesClient.AddRange(systemId, dtos, CancellationToken.None); //assert Assert.Equal(dtos.Count, response); diff --git a/Persistence.Repository/Repositories/TechMessagesRepository.cs b/Persistence.Repository/Repositories/TechMessagesRepository.cs index db59f63..0b13bc4 100644 --- a/Persistence.Repository/Repositories/TechMessagesRepository.cs +++ b/Persistence.Repository/Repositories/TechMessagesRepository.cs @@ -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,19 +51,18 @@ namespace Persistence.Repository.Repositories return dto; } - public async Task> GetStatistics(IEnumerable autoDrillingSystem, IEnumerable categoryIds, CancellationToken token) - { - var query = GetQueryReadOnly(); - var systems = autoDrillingSystem.Select(s => s.ToLower().Trim()); - var result = await query - .Where(e => !systems.Any() || systems.Contains(e.System.Name.ToLower().Trim())) - .GroupBy(e => e.System.Name, (key, group) => new - { - System = key, - Categories = group - .Where(g => !categoryIds.Any() || categoryIds.Contains(g.CategoryId)) - }) - .ToArrayAsync(token); + public async Task> GetStatistics(IEnumerable systems, IEnumerable categoryIds, CancellationToken token) + { + var query = GetQueryReadOnly(); + var result = await query + .Where(e => systems.Count() == 0 || systems.Contains(e.System.SystemId)) + .GroupBy(e => e.System.Name, (key, group) => new + { + System = key, + Categories = group + .Where(g => categoryIds.Count() == 0 || categoryIds.Contains(g.CategoryId)) + }) + .ToArrayAsync(token); var entities = new List(); foreach (var e in result) @@ -81,27 +81,16 @@ namespace Persistence.Repository.Repositories return entities; } - public async Task> GetSystems(CancellationToken token) - { - var entities = await GetDrillingSystems(token); - var result = entities.Select(e => e.Name); + public async Task AddRange(Guid systemId, IEnumerable dtos, Guid userId, CancellationToken token) + { - return result; - } + var entities = new List(); + foreach (var dto in dtos) + { + var entity = dto.Adapt(); - public async Task AddRange(IEnumerable dtos, Guid userId, CancellationToken token) - { - - var entities = new List(); - foreach (var dto in dtos) - { - var entity = dto.Adapt(); - var systems = await GetDrillingSystems(token); - var systemId = systems.FirstOrDefault(e => e.Name.ToLower().Trim() == dto.System.ToLower().Trim())?.SystemId - ?? await CreateDrillingSystem(dto.System, token); - - entity.SystemId = systemId; - entity.UserId = userId; + await CreateSystemIfNotExist(systemId, token); + entity.SystemId = systemId; entities.Add(entity); } @@ -125,54 +114,61 @@ namespace Persistence.Repository.Repositories return dtos; } - public async Task GetDatesRangeAsync(CancellationToken token) - { - var query = GetQueryReadOnly() - .GroupBy(e => 1) - .Select(group => new - { - Min = group.Min(e => e.Timestamp), - Max = group.Max(e => e.Timestamp), - }); - var values = await query.FirstOrDefaultAsync(token); - var result = new DatesRangeDto() - { - From = values?.Min ?? DateTimeOffset.MinValue, - To = values?.Max ?? DateTimeOffset.MaxValue - }; + public async Task> GetSystems(CancellationToken token) + { + var systems = await memoryCache.GetOrCreateAsync(SystemCacheKey, async f => + { + f.AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(CacheExpirationInMinutes); + + var query = db.Set(); + var entities = await query.ToListAsync(token); + var dtos = entities.Select(e => e.Adapt()); + + return dtos; + }); + + return systems ?? []; + } + + public async Task GetDatesRangeAsync(CancellationToken token) + { + var query = GetQueryReadOnly() + .GroupBy(e => 1) + .Select(group => new + { + Min = group.Min(e => e.Timestamp), + Max = group.Max(e => e.Timestamp), + }); + var values = await query.FirstOrDefaultAsync(token); + var result = new DatesRangeDto() + { + From = values?.Min ?? DateTimeOffset.MinValue, + To = values?.Max ?? DateTimeOffset.MaxValue + }; return result; } - private async Task> GetDrillingSystems(CancellationToken token) - { - var systems = await memoryCache.GetOrCreateAsync(SystemCacheKey, async f => - { - f.AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(CacheExpirationInMinutes); + private async Task CreateSystemIfNotExist(Guid systemId, CancellationToken token) + { + var systems = await GetSystems(token); + var system = systems?.FirstOrDefault(e => e.SystemId == systemId); - var query = db.Set(); - var entities = await query.ToListAsync(token); - var dtos = entities.Select(e => e.Adapt()); + if (system == null) + { + system = new DrillingSystemDto() + { + SystemId = systemId, + Name = string.Empty + }; - return dtos; - }); + var entity = system.Adapt(); - return systems!; - } - private async Task CreateDrillingSystem(string name, CancellationToken token) - { - memoryCache.Remove(SystemCacheKey); + await db.Set().AddAsync(entity); + await db.SaveChangesAsync(token); - var entity = new DrillingSystem() - { - SystemId = Uuid7.Guid(), - Name = name.ToLower().Trim() - }; - - await db.Set().AddAsync(entity, token); - await db.SaveChangesAsync(token); - - return entity.SystemId; - } - } + memoryCache.Remove(SystemCacheKey); + } + } + } } diff --git a/Persistence/Models/Enumerations/EventState.cs b/Persistence/Models/Enumerations/EventState.cs new file mode 100644 index 0000000..3f4103c --- /dev/null +++ b/Persistence/Models/Enumerations/EventState.cs @@ -0,0 +1,6 @@ +namespace Persistence.Models.Enumerations; +public enum EventState +{ + NotTriggered = 0, + Triggered = 1, +} diff --git a/Persistence/Models/TechMessageDto.cs b/Persistence/Models/TechMessageDto.cs index 927ba83..e076443 100644 --- a/Persistence/Models/TechMessageDto.cs +++ b/Persistence/Models/TechMessageDto.cs @@ -1,4 +1,5 @@ -using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations; +using Persistence.Models.Enumerations; namespace Persistence.Models { @@ -24,29 +25,16 @@ namespace Persistence.Models /// public DateTimeOffset Timestamp { get; set; } - /// - /// Глубина забоя - /// - [Range(0, double.MaxValue, ErrorMessage = "Глубина забоя не может быть меньше 0")] - public double? Depth { get; set; } + /// + /// Текст сообщения + /// + [Required] + [StringLength(512, MinimumLength = 1, ErrorMessage = "Допустимая длина текста сообщения от 1 до 512 символов")] + public required string Text { get; set; } - /// - /// Текст сообщения - /// - [Required] - [StringLength(512, MinimumLength = 1, ErrorMessage = "Допустимая длина текста сообщения от 1 до 512 символов")] - public required string MessageText { get; set; } - - /// - /// Система автобурения, к которой относится сообщение - /// - [Required] - [StringLength(256, MinimumLength = 1, ErrorMessage = "Допустимая длина наименования системы АБ от 1 до 256 символов")] - public required string System { get; set; } - - /// - /// Id пользователя за пультом бурильщика - /// - public Guid UserId { get; set; } - } + /// + /// Статус события + /// + public EventState EventState { get; set; } + } } diff --git a/Persistence/Repositories/ITechMessagesRepository.cs b/Persistence/Repositories/ITechMessagesRepository.cs index ae92912..c442e94 100644 --- a/Persistence/Repositories/ITechMessagesRepository.cs +++ b/Persistence/Repositories/ITechMessagesRepository.cs @@ -22,14 +22,14 @@ namespace Persistence.Repositories /// /// /// - Task AddRange(IEnumerable dtos, Guid userId, CancellationToken token); + Task AddRange(Guid systemId, IEnumerable dtos, Guid userId, CancellationToken token); /// - /// Получение списка уникальных названий систем АБ + /// Получение списка систем /// /// /// - Task> GetSystems(CancellationToken token); + Task> GetSystems(CancellationToken token); /// /// Получение количества сообщений по категориям и системам автобурения @@ -38,7 +38,7 @@ namespace Persistence.Repositories /// Система автобурения /// /// - Task> GetStatistics(IEnumerable autoDrillingSystem, IEnumerable categoryIds, CancellationToken token); + Task> GetStatistics(IEnumerable autoDrillingSystem, IEnumerable categoryIds, CancellationToken token); /// /// Получить порцию записей, начиная с заданной даты