From 421ab4185513aa49360aef509665cfdffbd2956d Mon Sep 17 00:00:00 2001 From: Roman Efremov Date: Thu, 9 Jan 2025 09:29:42 +0500 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D1=82?= =?UTF-8?q?=D1=8C=20TagBag,=20=D0=B8=D0=B7=D0=BC=D0=B5=D0=BD=D0=B8=D1=82?= =?UTF-8?q?=D1=8C=20Date=20=D0=BD=D0=B0=20Timestamp?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DD.Persistence.Database/Entity/DataSaub.cs | 63 ------------------- .../Entity/ITimestampedData.cs | 2 +- DD.Persistence.Database/Entity/TagBag.cs | 18 ++++++ .../Controllers/DataSaubControllerTest.cs | 2 +- .../TimeSeriesBaseControllerTest.cs | 6 +- .../DependencyInjection.cs | 4 +- .../Extensions/EFExtensionsSortBy.cs | 6 +- ...aRepository.cs => TagBagDataRepository.cs} | 16 +++-- 8 files changed, 35 insertions(+), 82 deletions(-) delete mode 100644 DD.Persistence.Database/Entity/DataSaub.cs create mode 100644 DD.Persistence.Database/Entity/TagBag.cs rename DD.Persistence.Repository/Repositories/{TimeSeriesDataRepository.cs => TagBagDataRepository.cs} (82%) diff --git a/DD.Persistence.Database/Entity/DataSaub.cs b/DD.Persistence.Database/Entity/DataSaub.cs deleted file mode 100644 index d4515b2..0000000 --- a/DD.Persistence.Database/Entity/DataSaub.cs +++ /dev/null @@ -1,63 +0,0 @@ -using System.ComponentModel.DataAnnotations; -using System.ComponentModel.DataAnnotations.Schema; - -namespace DD.Persistence.Database.Model; -public class DataSaub : ITimestampedData -{ - [Key, Column("date")] - public DateTimeOffset Date { get; set; } - - [Column("mode")] - public int? Mode { get; set; } - - [Column("user")] - public string? User { get; set; } - - [Column("wellDepth")] - public double? WellDepth { get; set; } - - [Column("bitDepth")] - public double? BitDepth { get; set; } - - [Column("blockPosition")] - public double? BlockPosition { get; set; } - - [Column("blockSpeed")] - public double? BlockSpeed { get; set; } - - [Column("pressure")] - public double? Pressure { get; set; } - - [Column("axialLoad")] - public double? AxialLoad { get; set; } - - [Column("hookWeight")] - public double? HookWeight { get; set; } - - [Column("rotorTorque")] - public double? RotorTorque { get; set; } - - [Column("rotorSpeed")] - public double? RotorSpeed { get; set; } - - [Column("flow")] - public double? Flow { get; set; } - - [Column("mseState")] - public short MseState { get; set; } - - [Column("idFeedRegulator")] - public int IdFeedRegulator { get; set; } - - [Column("mse")] - public double? Mse { get; set; } - - [Column("pump0Flow")] - public double? Pump0Flow { get; set; } - - [Column("pump1Flow")] - public double? Pump1Flow { get; set; } - - [Column("pump2Flow")] - public double? Pump2Flow { get; set; } -} diff --git a/DD.Persistence.Database/Entity/ITimestampedData.cs b/DD.Persistence.Database/Entity/ITimestampedData.cs index ce5468c..5a1b25c 100644 --- a/DD.Persistence.Database/Entity/ITimestampedData.cs +++ b/DD.Persistence.Database/Entity/ITimestampedData.cs @@ -4,5 +4,5 @@ public interface ITimestampedData /// /// Дата (должна быть обязательно в UTC) /// - DateTimeOffset Date { get; set; } + DateTimeOffset Timestamp { get; set; } } diff --git a/DD.Persistence.Database/Entity/TagBag.cs b/DD.Persistence.Database/Entity/TagBag.cs new file mode 100644 index 0000000..2aa7546 --- /dev/null +++ b/DD.Persistence.Database/Entity/TagBag.cs @@ -0,0 +1,18 @@ +using DD.Persistence.Database.Model; +using Microsoft.EntityFrameworkCore; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; + +namespace DD.Persistence.Database.Entity; + +[PrimaryKey(nameof(BagId), nameof(Timestamp))] +public class TagBag : ITimestampedData +{ + [Comment("Временная отметка"), Key] + public DateTimeOffset Timestamp { get; set; } + + public Guid BagId { get; set; } + + [Column(TypeName = "jsonb")] + public required object[] Values { get; set; } +} diff --git a/DD.Persistence.IntegrationTests/Controllers/DataSaubControllerTest.cs b/DD.Persistence.IntegrationTests/Controllers/DataSaubControllerTest.cs index 7daad50..3429651 100644 --- a/DD.Persistence.IntegrationTests/Controllers/DataSaubControllerTest.cs +++ b/DD.Persistence.IntegrationTests/Controllers/DataSaubControllerTest.cs @@ -34,7 +34,7 @@ public class DataSaubControllerTest : TimeSeriesBaseControllerTest : BaseIntegrat var datesRangeExpected = 30; var entity2 = entity.Adapt(); - entity2.Date = entity.Date.AddDays(datesRangeExpected); + entity2.Timestamp = entity.Timestamp.AddDays(datesRangeExpected); var dbset = dbContext.Set(); dbset.Add(entity); @@ -86,7 +86,7 @@ public abstract class TimeSeriesBaseControllerTest : BaseIntegrat for (var i = 1; i <= differenceBetweenStartAndEndDays; i++) { var entity2 = entity.Adapt(); - entity2.Date = entity.Date.AddDays(i - 1); + entity2.Timestamp = entity.Timestamp.AddDays(i - 1); entities.Add(entity2); } @@ -96,7 +96,7 @@ public abstract class TimeSeriesBaseControllerTest : BaseIntegrat dbContext.SaveChanges(); - var response = await timeSeriesClient.GetResampledData(entity.Date.AddMinutes(-1), differenceBetweenStartAndEndDays * 24 * 60 * 60 + 60, approxPointsCount, new CancellationToken()); + var response = await timeSeriesClient.GetResampledData(entity.Timestamp.AddMinutes(-1), differenceBetweenStartAndEndDays * 24 * 60 * 60 + 60, approxPointsCount, new CancellationToken()); //assert Assert.NotNull(response); diff --git a/DD.Persistence.Repository/DependencyInjection.cs b/DD.Persistence.Repository/DependencyInjection.cs index df15095..5114cd0 100644 --- a/DD.Persistence.Repository/DependencyInjection.cs +++ b/DD.Persistence.Repository/DependencyInjection.cs @@ -35,9 +35,9 @@ public static class DependencyInjection MapsterSetup(); - services.AddTransient, TimeSeriesDataRepository>(); + services.AddTransient, TagBagDataRepository>(); services.AddTransient(); - services.AddTransient, TimeSeriesDataCachedRepository>(); + //services.AddTransient, TimeSeriesDataCachedRepository>(); services.AddTransient(); services.AddTransient(); services.AddTransient(); diff --git a/DD.Persistence.Repository/Extensions/EFExtensionsSortBy.cs b/DD.Persistence.Repository/Extensions/EFExtensionsSortBy.cs index bdfc8a2..bc03e3b 100644 --- a/DD.Persistence.Repository/Extensions/EFExtensionsSortBy.cs +++ b/DD.Persistence.Repository/Extensions/EFExtensionsSortBy.cs @@ -71,7 +71,7 @@ public static class EFExtensionsSortBy /// и опционально указания направления сортировки "asc" или "desc" /// /// - /// var query = query("Date desc"); + /// var query = query("Timestamp desc"); /// /// Запрос с примененной сортировкой public static IOrderedQueryable SortBy( @@ -103,7 +103,7 @@ public static class EFExtensionsSortBy /// и опционально указания направления сортировки "asc" или "desc" /// /// - /// var query = query("Date desc"); + /// var query = query("Timestamp desc"); /// /// Запрос с примененной сортировкой public static IOrderedQueryable SortBy( @@ -129,7 +129,7 @@ public static class EFExtensionsSortBy /// и опционально указания направления сортировки "asc" или "desc" /// /// - /// var query = query("Date desc"); + /// var query = query("Timestamp desc"); /// /// Запрос с примененной сортировкой public static IOrderedQueryable ThenSortBy( diff --git a/DD.Persistence.Repository/Repositories/TimeSeriesDataRepository.cs b/DD.Persistence.Repository/Repositories/TagBagDataRepository.cs similarity index 82% rename from DD.Persistence.Repository/Repositories/TimeSeriesDataRepository.cs rename to DD.Persistence.Repository/Repositories/TagBagDataRepository.cs index c4c634b..320b99b 100644 --- a/DD.Persistence.Repository/Repositories/TimeSeriesDataRepository.cs +++ b/DD.Persistence.Repository/Repositories/TagBagDataRepository.cs @@ -5,13 +5,11 @@ using DD.Persistence.Models; using DD.Persistence.Repositories; namespace DD.Persistence.Repository.Repositories; -public class TimeSeriesDataRepository : ITimeSeriesDataRepository - where TEntity : class, ITimestampedData, new() - where TDto : class, ITimeSeriesAbstractDto, new() +public class TagBagRepository : ITimeSeriesDataRepository { private readonly DbContext db; - public TimeSeriesDataRepository(DbContext db) + public TagBagRepository(DbContext db) { this.db = db; } @@ -21,8 +19,8 @@ public class TimeSeriesDataRepository : ITimeSeriesDataRepository public virtual async Task GetDatesRange(CancellationToken token) { var query = GetQueryReadOnly(); - var minDate = await query.MinAsync(o => o.Date, token); - var maxDate = await query.MaxAsync(o => o.Date, token); + var minDate = await query.MinAsync(o => o.Timestamp, token); + var maxDate = await query.MaxAsync(o => o.Timestamp, token); return new DatesRangeDto { @@ -33,7 +31,7 @@ public class TimeSeriesDataRepository : ITimeSeriesDataRepository public virtual async Task> GetGtDate(DateTimeOffset date, CancellationToken token) { - var query = this.db.Set().Where(e => e.Date > date); + var query = this.db.Set().Where(e => e.Timestamp > date); var entities = await query.ToArrayAsync(token); var dtos = entities.Select(e => e.Adapt()); @@ -54,7 +52,7 @@ public class TimeSeriesDataRepository : ITimeSeriesDataRepository protected async Task> GetLastAsync(int takeCount, CancellationToken token) { var query = GetQueryReadOnly() - .OrderByDescending(e => e.Date) + .OrderByDescending(e => e.Timestamp) .Take(takeCount); var entities = await query.ToArrayAsync(token); @@ -66,7 +64,7 @@ public class TimeSeriesDataRepository : ITimeSeriesDataRepository protected async Task GetFirstAsync(CancellationToken token) { var query = GetQueryReadOnly() - .OrderBy(e => e.Date); + .OrderBy(e => e.Timestamp); var entity = await query.FirstOrDefaultAsync(token);