Правки после ревью
Some checks failed
Unit tests / test (push) Failing after 2m51s

This commit is contained in:
Roman Efremov 2025-01-22 16:13:56 +05:00
parent 3262201973
commit 4af65e258b
8 changed files with 81 additions and 80 deletions

View File

@ -1,5 +1,6 @@
// <auto-generated /> // <auto-generated />
using System; using System;
using System.Text.Json;
using DD.Persistence.Database.Model; using DD.Persistence.Database.Model;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure; using Microsoft.EntityFrameworkCore.Infrastructure;
@ -12,7 +13,7 @@ using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
namespace DD.Persistence.Database.Postgres.Migrations namespace DD.Persistence.Database.Postgres.Migrations
{ {
[DbContext(typeof(PersistencePostgresContext))] [DbContext(typeof(PersistencePostgresContext))]
[Migration("20250116093615_Init")] [Migration("20250122111321_Init")]
partial class Init partial class Init
{ {
/// <inheritdoc /> /// <inheritdoc />
@ -25,6 +26,23 @@ namespace DD.Persistence.Database.Postgres.Migrations
NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);
modelBuilder.Entity("DD.Persistence.Database.Entity.DataScheme", b =>
{
b.Property<Guid>("DiscriminatorId")
.ValueGeneratedOnAdd()
.HasColumnType("uuid")
.HasComment("Идентификатор схемы данных");
b.Property<string>("PropNames")
.IsRequired()
.HasColumnType("jsonb")
.HasComment("Наименования полей в порядке индексации");
b.HasKey("DiscriminatorId");
b.ToTable("DataSchemes");
});
modelBuilder.Entity("DD.Persistence.Database.Entity.DataSourceSystem", b => modelBuilder.Entity("DD.Persistence.Database.Entity.DataSourceSystem", b =>
{ {
b.Property<Guid>("SystemId") b.Property<Guid>("SystemId")
@ -125,23 +143,6 @@ namespace DD.Persistence.Database.Postgres.Migrations
b.ToTable("TimestampedValues"); b.ToTable("TimestampedValues");
}); });
modelBuilder.Entity("DD.Persistence.Database.Entity.ValuesIdentity", b =>
{
b.Property<Guid>("DiscriminatorId")
.ValueGeneratedOnAdd()
.HasColumnType("uuid")
.HasComment("Дискриминатор системы");
b.Property<string[]>("Identity")
.IsRequired()
.HasColumnType("jsonb")
.HasComment("Идентификаторы");
b.HasKey("DiscriminatorId");
b.ToTable("ValuesIdentities");
});
modelBuilder.Entity("DD.Persistence.Database.Model.ChangeLog", b => modelBuilder.Entity("DD.Persistence.Database.Model.ChangeLog", b =>
{ {
b.Property<Guid>("Id") b.Property<Guid>("Id")
@ -209,8 +210,7 @@ namespace DD.Persistence.Database.Postgres.Migrations
.HasColumnType("uuid") .HasColumnType("uuid")
.HasComment("Id автора последнего изменения"); .HasComment("Id автора последнего изменения");
b.Property<object>("Value") b.Property<JsonElement>("Value")
.IsRequired()
.HasColumnType("jsonb") .HasColumnType("jsonb")
.HasComment("Значение уставки"); .HasComment("Значение уставки");
@ -232,13 +232,13 @@ namespace DD.Persistence.Database.Postgres.Migrations
modelBuilder.Entity("DD.Persistence.Database.Entity.TimestampedValues", b => modelBuilder.Entity("DD.Persistence.Database.Entity.TimestampedValues", b =>
{ {
b.HasOne("DD.Persistence.Database.Entity.ValuesIdentity", "ValuesIdentity") b.HasOne("DD.Persistence.Database.Entity.DataScheme", "DataScheme")
.WithMany() .WithMany()
.HasForeignKey("DiscriminatorId") .HasForeignKey("DiscriminatorId")
.OnDelete(DeleteBehavior.Cascade) .OnDelete(DeleteBehavior.Cascade)
.IsRequired(); .IsRequired();
b.Navigation("ValuesIdentity"); b.Navigation("DataScheme");
}); });
#pragma warning restore 612, 618 #pragma warning restore 612, 618
} }

View File

@ -1,4 +1,5 @@
using System; using System;
using System.Text.Json;
using Microsoft.EntityFrameworkCore.Migrations; using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable #nullable disable
@ -32,6 +33,18 @@ namespace DD.Persistence.Database.Postgres.Migrations
table.PrimaryKey("PK_ChangeLog", x => x.Id); table.PrimaryKey("PK_ChangeLog", x => x.Id);
}); });
migrationBuilder.CreateTable(
name: "DataSchemes",
columns: table => new
{
DiscriminatorId = table.Column<Guid>(type: "uuid", nullable: false, comment: "Идентификатор схемы данных"),
PropNames = table.Column<string>(type: "jsonb", nullable: false, comment: "Наименования полей в порядке индексации")
},
constraints: table =>
{
table.PrimaryKey("PK_DataSchemes", x => x.DiscriminatorId);
});
migrationBuilder.CreateTable( migrationBuilder.CreateTable(
name: "DataSourceSystem", name: "DataSourceSystem",
columns: table => new columns: table => new
@ -65,7 +78,7 @@ namespace DD.Persistence.Database.Postgres.Migrations
{ {
Key = table.Column<Guid>(type: "uuid", nullable: false, comment: "Ключ"), Key = table.Column<Guid>(type: "uuid", nullable: false, comment: "Ключ"),
Timestamp = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, comment: "Дата создания уставки"), Timestamp = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, comment: "Дата создания уставки"),
Value = table.Column<object>(type: "jsonb", nullable: false, comment: "Значение уставки"), Value = table.Column<JsonElement>(type: "jsonb", nullable: false, comment: "Значение уставки"),
IdUser = table.Column<Guid>(type: "uuid", nullable: false, comment: "Id автора последнего изменения") IdUser = table.Column<Guid>(type: "uuid", nullable: false, comment: "Id автора последнего изменения")
}, },
constraints: table => constraints: table =>
@ -74,15 +87,22 @@ namespace DD.Persistence.Database.Postgres.Migrations
}); });
migrationBuilder.CreateTable( migrationBuilder.CreateTable(
name: "ValuesIdentities", name: "TimestampedValues",
columns: table => new columns: table => new
{ {
Timestamp = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, comment: "Временная отметка"),
DiscriminatorId = table.Column<Guid>(type: "uuid", nullable: false, comment: "Дискриминатор системы"), DiscriminatorId = table.Column<Guid>(type: "uuid", nullable: false, comment: "Дискриминатор системы"),
Identity = table.Column<string[]>(type: "jsonb", nullable: false, comment: "Идентификаторы") Values = table.Column<string>(type: "jsonb", nullable: false, comment: "Данные")
}, },
constraints: table => constraints: table =>
{ {
table.PrimaryKey("PK_ValuesIdentities", x => x.DiscriminatorId); table.PrimaryKey("PK_TimestampedValues", x => new { x.DiscriminatorId, x.Timestamp });
table.ForeignKey(
name: "FK_TimestampedValues_DataSchemes_DiscriminatorId",
column: x => x.DiscriminatorId,
principalTable: "DataSchemes",
principalColumn: "DiscriminatorId",
onDelete: ReferentialAction.Cascade);
}); });
migrationBuilder.CreateTable( migrationBuilder.CreateTable(
@ -107,25 +127,6 @@ namespace DD.Persistence.Database.Postgres.Migrations
onDelete: ReferentialAction.Cascade); onDelete: ReferentialAction.Cascade);
}); });
migrationBuilder.CreateTable(
name: "TimestampedValues",
columns: table => new
{
Timestamp = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, comment: "Временная отметка"),
DiscriminatorId = table.Column<Guid>(type: "uuid", nullable: false, comment: "Дискриминатор системы"),
Values = table.Column<string>(type: "jsonb", nullable: false, comment: "Данные")
},
constraints: table =>
{
table.PrimaryKey("PK_TimestampedValues", x => new { x.DiscriminatorId, x.Timestamp });
table.ForeignKey(
name: "FK_TimestampedValues_ValuesIdentities_DiscriminatorId",
column: x => x.DiscriminatorId,
principalTable: "ValuesIdentities",
principalColumn: "DiscriminatorId",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateIndex( migrationBuilder.CreateIndex(
name: "IX_TechMessage_SystemId", name: "IX_TechMessage_SystemId",
table: "TechMessage", table: "TechMessage",
@ -154,7 +155,7 @@ namespace DD.Persistence.Database.Postgres.Migrations
name: "DataSourceSystem"); name: "DataSourceSystem");
migrationBuilder.DropTable( migrationBuilder.DropTable(
name: "ValuesIdentities"); name: "DataSchemes");
} }
} }
} }

View File

@ -1,5 +1,6 @@
// <auto-generated /> // <auto-generated />
using System; using System;
using System.Text.Json;
using DD.Persistence.Database.Model; using DD.Persistence.Database.Model;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure; using Microsoft.EntityFrameworkCore.Infrastructure;
@ -22,6 +23,23 @@ namespace DD.Persistence.Database.Postgres.Migrations
NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);
modelBuilder.Entity("DD.Persistence.Database.Entity.DataScheme", b =>
{
b.Property<Guid>("DiscriminatorId")
.ValueGeneratedOnAdd()
.HasColumnType("uuid")
.HasComment("Идентификатор схемы данных");
b.Property<string>("PropNames")
.IsRequired()
.HasColumnType("jsonb")
.HasComment("Наименования полей в порядке индексации");
b.HasKey("DiscriminatorId");
b.ToTable("DataSchemes");
});
modelBuilder.Entity("DD.Persistence.Database.Entity.DataSourceSystem", b => modelBuilder.Entity("DD.Persistence.Database.Entity.DataSourceSystem", b =>
{ {
b.Property<Guid>("SystemId") b.Property<Guid>("SystemId")
@ -122,23 +140,6 @@ namespace DD.Persistence.Database.Postgres.Migrations
b.ToTable("TimestampedValues"); b.ToTable("TimestampedValues");
}); });
modelBuilder.Entity("DD.Persistence.Database.Entity.ValuesIdentity", b =>
{
b.Property<Guid>("DiscriminatorId")
.ValueGeneratedOnAdd()
.HasColumnType("uuid")
.HasComment("Дискриминатор системы");
b.Property<string[]>("Identity")
.IsRequired()
.HasColumnType("jsonb")
.HasComment("Идентификаторы");
b.HasKey("DiscriminatorId");
b.ToTable("ValuesIdentities");
});
modelBuilder.Entity("DD.Persistence.Database.Model.ChangeLog", b => modelBuilder.Entity("DD.Persistence.Database.Model.ChangeLog", b =>
{ {
b.Property<Guid>("Id") b.Property<Guid>("Id")
@ -206,8 +207,7 @@ namespace DD.Persistence.Database.Postgres.Migrations
.HasColumnType("uuid") .HasColumnType("uuid")
.HasComment("Id автора последнего изменения"); .HasComment("Id автора последнего изменения");
b.Property<object>("Value") b.Property<JsonElement>("Value")
.IsRequired()
.HasColumnType("jsonb") .HasColumnType("jsonb")
.HasComment("Значение уставки"); .HasComment("Значение уставки");
@ -229,13 +229,13 @@ namespace DD.Persistence.Database.Postgres.Migrations
modelBuilder.Entity("DD.Persistence.Database.Entity.TimestampedValues", b => modelBuilder.Entity("DD.Persistence.Database.Entity.TimestampedValues", b =>
{ {
b.HasOne("DD.Persistence.Database.Entity.ValuesIdentity", "ValuesIdentity") b.HasOne("DD.Persistence.Database.Entity.DataScheme", "DataScheme")
.WithMany() .WithMany()
.HasForeignKey("DiscriminatorId") .HasForeignKey("DiscriminatorId")
.OnDelete(DeleteBehavior.Cascade) .OnDelete(DeleteBehavior.Cascade)
.IsRequired(); .IsRequired();
b.Navigation("ValuesIdentity"); b.Navigation("DataScheme");
}); });
#pragma warning restore 612, 618 #pragma warning restore 612, 618
} }

View File

@ -6,7 +6,7 @@ namespace DD.Persistence.Database.Entity;
public class DataScheme public class DataScheme
{ {
[Key, Comment("Дискриминатор системы"),] [Key, Comment("Идентификатор схемы данных"),]
public Guid DiscriminatorId { get; set; } public Guid DiscriminatorId { get; set; }
[Comment("Наименования полей в порядке индексации"), Column(TypeName = "jsonb")] [Comment("Наименования полей в порядке индексации"), Column(TypeName = "jsonb")]

View File

@ -22,10 +22,10 @@ public class DataSchemeRepository : IDataSchemeRepository
await db.SaveChangesAsync(token); await db.SaveChangesAsync(token);
} }
public virtual async Task<DataSchemeDto?> GetByDiscriminator(Guid discriminatorId, CancellationToken token) public virtual async Task<DataSchemeDto?> Get(Guid dataSchemeId, CancellationToken token)
{ {
var query = GetQueryReadOnly() var query = GetQueryReadOnly()
.Where(e => e.DiscriminatorId == discriminatorId); .Where(e => e.DiscriminatorId == dataSchemeId);
var entity = await query.ToArrayAsync(); var entity = await query.ToArrayAsync();
var dto = entity.Select(e => e.Adapt<DataSchemeDto>()).FirstOrDefault(); var dto = entity.Select(e => e.Adapt<DataSchemeDto>()).FirstOrDefault();

View File

@ -20,10 +20,10 @@ public class DataSchemeCachedRepository : DataSchemeRepository
memoryCache.Set(dataSourceSystemDto.DiscriminatorId, dataSourceSystemDto); memoryCache.Set(dataSourceSystemDto.DiscriminatorId, dataSourceSystemDto);
} }
public override async Task<DataSchemeDto?> GetByDiscriminator(Guid discriminatorId, CancellationToken token) public override async Task<DataSchemeDto?> Get(Guid discriminatorId, CancellationToken token)
{ {
var result = memoryCache.Get<DataSchemeDto>(discriminatorId) var result = memoryCache.Get<DataSchemeDto>(discriminatorId)
?? await base.GetByDiscriminator(discriminatorId, token); ?? await base.Get(discriminatorId, token);
return result; return result;
} }

View File

@ -18,8 +18,8 @@ public interface IDataSchemeRepository
/// <summary> /// <summary>
/// Вычитать схему /// Вычитать схему
/// </summary> /// </summary>
/// <param name="discriminatorId">Дискриминатор системы</param> /// <param name="dataSchemeId">Идентификатор схемы</param>
/// <param name="token"></param> /// <param name="token"></param>
/// <returns></returns> /// <returns></returns>
Task<DataSchemeDto?> GetByDiscriminator(Guid discriminatorId, CancellationToken token); Task<DataSchemeDto?> Get(Guid dataSchemeId, CancellationToken token);
} }

View File

@ -113,14 +113,14 @@ public class TimestampedValuesService : ITimestampedValuesService
/// <summary> /// <summary>
/// Преобразовать результат запроса в набор dto /// Преобразовать результат запроса в набор dto
/// </summary> /// </summary>
/// <param name="discriminatorId"></param> /// <param name="dataSchemeId"></param>
/// <param name="queryResult"></param> /// <param name="queryResult"></param>
/// <param name="token"></param> /// <param name="token"></param>
/// <returns></returns> /// <returns></returns>
private async Task<IEnumerable<TimestampedValuesDto>> Materialize(Guid discriminatorId, IEnumerable<Tuple<DateTimeOffset, object[]>> queryResult, CancellationToken token) private async Task<IEnumerable<TimestampedValuesDto>> Materialize(Guid dataSchemeId, IEnumerable<Tuple<DateTimeOffset, object[]>> queryResult, CancellationToken token)
{ {
var systemSpecification = await dataSchemeRepository.GetByDiscriminator(discriminatorId, token); var dataScheme = await dataSchemeRepository.Get(dataSchemeId, token);
if (systemSpecification is null) if (dataScheme is null)
{ {
return []; return [];
} }
@ -132,7 +132,7 @@ public class TimestampedValuesService : ITimestampedValuesService
Timestamp = entity.Item1.ToUniversalTime() Timestamp = entity.Item1.ToUniversalTime()
}; };
var identity = systemSpecification!.PropNames; var identity = dataScheme!.PropNames;
var indexedIdentity = identity var indexedIdentity = identity
.Select((value, index) => new { index, value }); .Select((value, index) => new { index, value });
dto.Values = indexedIdentity dto.Values = indexedIdentity
@ -154,7 +154,7 @@ public class TimestampedValuesService : ITimestampedValuesService
/// <exception cref="InvalidOperationException">Некорректный набор наименований полей</exception> /// <exception cref="InvalidOperationException">Некорректный набор наименований полей</exception>
private async Task CreateSystemSpecificationIfNotExist(Guid discriminatorId, string[] fieldNames, CancellationToken token) private async Task CreateSystemSpecificationIfNotExist(Guid discriminatorId, string[] fieldNames, CancellationToken token)
{ {
var systemSpecification = await dataSchemeRepository.GetByDiscriminator(discriminatorId, token); var systemSpecification = await dataSchemeRepository.Get(discriminatorId, token);
if (systemSpecification is null) if (systemSpecification is null)
{ {
systemSpecification = new DataSchemeDto() systemSpecification = new DataSchemeDto()