1. Убран Id у DataSaub, теперь в качестве первичного ключа выступает Date

2. Автотесты для методов GetDatesRande, GetResampledData.
3. Рефакторинг
This commit is contained in:
Olga Nemt 2024-11-22 15:47:00 +05:00
parent c751f74b35
commit 1fdd199954
14 changed files with 166 additions and 60 deletions

View File

@ -32,9 +32,11 @@ public class TimeSeriesController<TDto> : ControllerBase, ITimeSeriesDataApi<TDt
return Ok(result); return Ok(result);
} }
public Task<IActionResult> GetResampledData(DateTimeOffset dateBegin, DateTimeOffset dateEnd, int approxPointsCount = 1024) [HttpGet("resampled")]
public async Task<IActionResult> GetResampledData(DateTimeOffset dateBegin, double intervalSec = 600d, int approxPointsCount = 1024)
{ {
throw new NotImplementedException(); var result = await this.timeSeriesDataRepository.GetResampledData(dateBegin, intervalSec, approxPointsCount);
return Ok(result);
} }
[HttpPost] [HttpPost]

View File

@ -33,6 +33,11 @@ public class Startup
app.UseRouting(); app.UseRouting();
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseAuthentication(); app.UseAuthentication();
app.UseAuthorization(); app.UseAuthorization();

View File

@ -12,7 +12,7 @@ using Persistence.Database.Model;
namespace Persistence.Database.Postgres.Migrations namespace Persistence.Database.Postgres.Migrations
{ {
[DbContext(typeof(PersistenceDbContext))] [DbContext(typeof(PersistenceDbContext))]
[Migration("20241118091712_InitialCreate")] [Migration("20241122074646_InitialCreate")]
partial class InitialCreate partial class InitialCreate
{ {
/// <inheritdoc /> /// <inheritdoc />
@ -29,12 +29,9 @@ namespace Persistence.Database.Postgres.Migrations
modelBuilder.Entity("Persistence.Database.Model.DataSaub", b => modelBuilder.Entity("Persistence.Database.Model.DataSaub", b =>
{ {
b.Property<int>("Id") b.Property<DateTimeOffset>("Date")
.ValueGeneratedOnAdd() .HasColumnType("timestamp with time zone")
.HasColumnType("integer") .HasColumnName("date");
.HasColumnName("id");
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));
b.Property<double?>("AxialLoad") b.Property<double?>("AxialLoad")
.HasColumnType("double precision") .HasColumnType("double precision")
@ -52,10 +49,6 @@ namespace Persistence.Database.Postgres.Migrations
.HasColumnType("double precision") .HasColumnType("double precision")
.HasColumnName("blockSpeed"); .HasColumnName("blockSpeed");
b.Property<DateTimeOffset>("Date")
.HasColumnType("timestamp with time zone")
.HasColumnName("date");
b.Property<double?>("Flow") b.Property<double?>("Flow")
.HasColumnType("double precision") .HasColumnType("double precision")
.HasColumnName("flow"); .HasColumnName("flow");
@ -112,7 +105,7 @@ namespace Persistence.Database.Postgres.Migrations
.HasColumnType("double precision") .HasColumnType("double precision")
.HasColumnName("wellDepth"); .HasColumnName("wellDepth");
b.HasKey("Id"); b.HasKey("Date");
b.ToTable("DataSaub"); b.ToTable("DataSaub");
}); });

View File

@ -1,6 +1,5 @@
using System; using System;
using Microsoft.EntityFrameworkCore.Migrations; using Microsoft.EntityFrameworkCore.Migrations;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
#nullable disable #nullable disable
@ -19,8 +18,6 @@ namespace Persistence.Database.Postgres.Migrations
name: "DataSaub", name: "DataSaub",
columns: table => new columns: table => new
{ {
id = table.Column<int>(type: "integer", nullable: false)
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
date = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false), date = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false),
mode = table.Column<int>(type: "integer", nullable: true), mode = table.Column<int>(type: "integer", nullable: true),
user = table.Column<string>(type: "text", nullable: true), user = table.Column<string>(type: "text", nullable: true),
@ -43,7 +40,7 @@ namespace Persistence.Database.Postgres.Migrations
}, },
constraints: table => constraints: table =>
{ {
table.PrimaryKey("PK_DataSaub", x => x.id); table.PrimaryKey("PK_DataSaub", x => x.date);
}); });
} }

View File

@ -26,12 +26,9 @@ namespace Persistence.Database.Postgres.Migrations
modelBuilder.Entity("Persistence.Database.Model.DataSaub", b => modelBuilder.Entity("Persistence.Database.Model.DataSaub", b =>
{ {
b.Property<int>("Id") b.Property<DateTimeOffset>("Date")
.ValueGeneratedOnAdd() .HasColumnType("timestamp with time zone")
.HasColumnType("integer") .HasColumnName("date");
.HasColumnName("id");
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));
b.Property<double?>("AxialLoad") b.Property<double?>("AxialLoad")
.HasColumnType("double precision") .HasColumnType("double precision")
@ -49,10 +46,6 @@ namespace Persistence.Database.Postgres.Migrations
.HasColumnType("double precision") .HasColumnType("double precision")
.HasColumnName("blockSpeed"); .HasColumnName("blockSpeed");
b.Property<DateTimeOffset>("Date")
.HasColumnType("timestamp with time zone")
.HasColumnName("date");
b.Property<double?>("Flow") b.Property<double?>("Flow")
.HasColumnType("double precision") .HasColumnType("double precision")
.HasColumnName("flow"); .HasColumnName("flow");
@ -109,7 +102,7 @@ namespace Persistence.Database.Postgres.Migrations
.HasColumnType("double precision") .HasColumnType("double precision")
.HasColumnName("wellDepth"); .HasColumnName("wellDepth");
b.HasKey("Id"); b.HasKey("Date");
b.ToTable("DataSaub"); b.ToTable("DataSaub");
}); });

View File

@ -1,12 +1,11 @@
using System.ComponentModel.DataAnnotations.Schema; using Microsoft.EntityFrameworkCore;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace Persistence.Database.Model; namespace Persistence.Database.Model;
public class DataSaub : ITimestampedData public class DataSaub : ITimestampedData
{ {
[Column("id")] [Key, Column("date")]
public int Id { get; set; }
[Column("date")]
public DateTimeOffset Date { get; set; } public DateTimeOffset Date { get; set; }
[Column("mode")] [Column("mode")]

View File

@ -1,13 +1,22 @@
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Persistence.Models;
using Refit; using Refit;
namespace Persistence.IntegrationTests.Clients; namespace Persistence.IntegrationTests.Clients;
public interface ITimeSeriesClient<TDto> public interface ITimeSeriesClient<TDto>
where TDto : class, new() where TDto : class, new()
{ {
[Post("/api/dataSaub")] private const string BaseRoute = "/api/dataSaub";
Task<IApiResponse<int>> InsertRangeAsync(IEnumerable<TDto> dtos);
[Get("/api/dataSaub")] [Post($"{BaseRoute}")]
Task<IApiResponse<IEnumerable<TDto>>> GetAsync(DateTimeOffset dateBegin, DateTimeOffset dateEnd); Task<IApiResponse<int>> InsertRange(IEnumerable<TDto> dtos);
[Get($"{BaseRoute}")]
Task<IApiResponse<IEnumerable<TDto>>> Get(DateTimeOffset dateBegin, DateTimeOffset dateEnd);
[Get($"{BaseRoute}/resampled")]
Task<IApiResponse<IEnumerable<TDto>>> GetResampledData(DateTimeOffset dateBegin, double intervalSec = 600d, int approxPointsCount = 1024);
[Get($"{BaseRoute}/datesRange")]
Task<IApiResponse<DatesRangeDto?>> GetDatesRange();
} }

View File

@ -14,7 +14,6 @@ public class DataSaubControllerTest : TimeSeriesBaseControllerTest<DataSaub, Dat
Date = DateTimeOffset.UtcNow, Date = DateTimeOffset.UtcNow,
Flow = 5, Flow = 5,
HookWeight = 6, HookWeight = 6,
Id = 7,
IdFeedRegulator = 8, IdFeedRegulator = 8,
Mode = 9, Mode = 9,
Mse = 10, Mse = 10,
@ -38,7 +37,6 @@ public class DataSaubControllerTest : TimeSeriesBaseControllerTest<DataSaub, Dat
Date = DateTimeOffset.UtcNow, Date = DateTimeOffset.UtcNow,
Flow = 5, Flow = 5,
HookWeight = 6, HookWeight = 6,
Id = 7,
IdFeedRegulator = 8, IdFeedRegulator = 8,
Mode = 9, Mode = 9,
Mse = 10, Mse = 10,
@ -70,4 +68,18 @@ public class DataSaubControllerTest : TimeSeriesBaseControllerTest<DataSaub, Dat
var endDate = DateTimeOffset.UtcNow; var endDate = DateTimeOffset.UtcNow;
await GetSuccess(beginDate, endDate, entity); await GetSuccess(beginDate, endDate, entity);
} }
[Fact]
public async Task GetDatesRange_returns_success()
{
await GetDatesRangeSuccess(entity);
}
[Fact]
public async Task GetResampledData_returns_success()
{
await GetResampledDataSuccess(entity);
}
} }

View File

@ -1,20 +1,12 @@
using Mapster; using Mapster;
using Microsoft.AspNetCore.Mvc; using Persistence.Database.Model;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration.UserSecrets;
using Persistence.IntegrationTests.Clients; using Persistence.IntegrationTests.Clients;
using Persistence.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net; using System.Net;
using System.Text;
using System.Threading.Tasks;
using Xunit; using Xunit;
namespace Persistence.IntegrationTests.Controllers; namespace Persistence.IntegrationTests.Controllers;
public abstract class TimeSeriesBaseControllerTest<TEntity, TDto> : BaseIntegrationTest public abstract class TimeSeriesBaseControllerTest<TEntity, TDto> : BaseIntegrationTest
where TEntity : class where TEntity : class, ITimestampedData, new()
where TDto : class, new() where TDto : class, new()
{ {
private ITimeSeriesClient<TDto> client; private ITimeSeriesClient<TDto> client;
@ -26,7 +18,7 @@ public abstract class TimeSeriesBaseControllerTest<TEntity, TDto> : BaseIntegrat
Task.Run(async () => Task.Run(async () =>
{ {
client = await factory.GetAuthorizedHttpClient<ITimeSeriesClient<TDto>>(string.Empty); client = await factory.GetAuthorizedHttpClient<ITimeSeriesClient<TDto>>(string.Empty);
}).Wait(); }).Wait();
} }
public async Task InsertRangeSuccess(TDto dto) public async Task InsertRangeSuccess(TDto dto)
@ -35,7 +27,7 @@ public abstract class TimeSeriesBaseControllerTest<TEntity, TDto> : BaseIntegrat
var expected = dto.Adapt<TDto>(); var expected = dto.Adapt<TDto>();
//act //act
var response = await client.InsertRangeAsync(new TDto[] { expected }); var response = await client.InsertRange(new TDto[] { expected });
//assert //assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode); Assert.Equal(HttpStatusCode.OK, response.StatusCode);
@ -51,11 +43,80 @@ public abstract class TimeSeriesBaseControllerTest<TEntity, TDto> : BaseIntegrat
dbContext.SaveChanges(); dbContext.SaveChanges();
var response = await client.GetAsync(beginDate, endDate); var response = await client.Get(beginDate, endDate);
//assert //assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode); Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.NotNull(response.Content); Assert.NotNull(response.Content);
Assert.Single(response.Content); Assert.Single(response.Content);
} }
public async Task GetDatesRangeSuccess(TEntity entity)
{
//arrange
var datesRangeExpected = 30;
var entity2 = entity.Adapt<TEntity>();
entity2.Date = entity.Date.AddDays(datesRangeExpected);
var dbset = dbContext.Set<TEntity>();
dbset.Add(entity);
dbset.Add(entity2);
dbContext.SaveChanges();
var response = await client.GetDatesRange();
//assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.NotNull(response.Content);
var datesRangeActual = (response.Content.To - response.Content.From).Days;
Assert.Equal(datesRangeExpected, datesRangeActual);
}
public async Task GetResampledDataSuccess(TEntity entity)
{
//arrange
var approxPointsCount = 10;
var differenceBetweenStartAndEndDays = 50;
var entities = new List<TEntity>();
for (var i = 1; i <= differenceBetweenStartAndEndDays; i++)
{
var entity2 = entity.Adapt<TEntity>();
entity2.Date = entity.Date.AddDays(i - 1);
entities.Add(entity2);
}
var dbset = dbContext.Set<TEntity>();
dbset.AddRange(entities);
dbContext.SaveChanges();
var response = await client.GetResampledData(entity.Date.AddMinutes(-1), differenceBetweenStartAndEndDays * 24 * 60 * 60 + 60, approxPointsCount);
//assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.NotNull(response.Content);
var ratio = entities.Count() / approxPointsCount;
if (ratio > 1)
{
var expectedResampledCount = entities
.Where((_, index) => index % ratio == 0)
.Count();
Assert.Equal(expectedResampledCount, response.Content.Count());
}
else
{
Assert.Equal(entities.Count(), response.Content.Count());
}
}
} }

View File

@ -4,8 +4,6 @@ using System.ComponentModel.DataAnnotations.Schema;
namespace Persistence.Repository.Data; namespace Persistence.Repository.Data;
public class DataSaubDto : ITimeSeriesAbstractDto public class DataSaubDto : ITimeSeriesAbstractDto
{ {
public int Id { get; set; }
public DateTimeOffset Date { get; set; } = DateTimeOffset.UtcNow; public DateTimeOffset Date { get; set; } = DateTimeOffset.UtcNow;
public int? Mode { get; set; } public int? Mode { get; set; }

View File

@ -77,5 +77,25 @@ public class TimeSeriesDataCachedRepository<TEntity, TDto> : TimeSeriesDataRepos
}; };
}); });
} }
public override async Task<IEnumerable<TDto>> GetResampledData(DateTimeOffset dateBegin, double intervalSec = 600d, int approxPointsCount = 1024)
{
var dtos = LastData.Where(i => i.Date >= dateBegin);
if (LastData.Count == 0 || LastData[0].Date > dateBegin)
{
dtos = await base.GetGtDate(dateBegin, CancellationToken.None);
}
var dateEnd = dateBegin.AddSeconds(intervalSec);
dtos = dtos
.Where(i => i.Date <= dateEnd);
var ratio = dtos.Count() / approxPointsCount;
if (ratio > 1)
dtos = dtos
.Where((_, index) => index % ratio == 0);
return dtos;
}
} }

View File

@ -84,9 +84,26 @@ public class TimeSeriesDataRepository<TEntity, TDto> : ITimeSeriesDataRepository
return dto; return dto;
} }
public Task<IEnumerable<TDto>> GetResampledData(DateTimeOffset dateBegin, DateTimeOffset dateEnd, int approxPointsCount = 1024) public virtual Task<IEnumerable<TDto>> GetResampledData(DateTimeOffset dateBegin, double intervalSec = 600d, int approxPointsCount = 1024)
{ {
//todo //if (!caches.TryGetValue(idTelemetry, out TelemetryDataCacheItem? cacheItem))
throw new NotImplementedException(); // return null;
//var cacheLastData = cacheItem.LastData;
//if (cacheLastData.Count == 0 || cacheLastData[0].DateTime > dateBegin)
// return null;
//var dateEnd = dateBegin.AddSeconds(intervalSec);
//var items = cacheLastData
// .Where(i => i.DateTime >= dateBegin && i.DateTime <= dateEnd);
//var ratio = items.Count() / approxPointsCount;
//if (ratio > 1)
// items = items
// .Where((_, index) => index % ratio == 0);
//return items;
return null;
} }
} }

View File

@ -15,7 +15,7 @@ public interface ITimeSeriesBaseDataApi<TDto>
/// <param name="dateEnd">дата окончания</param> /// <param name="dateEnd">дата окончания</param>
/// <param name="approxPointsCount"></param> /// <param name="approxPointsCount"></param>
/// <returns></returns> /// <returns></returns>
Task<IActionResult> GetResampledData(DateTimeOffset dateBegin, DateTimeOffset dateEnd, int approxPointsCount = 1024); Task<IActionResult> GetResampledData(DateTimeOffset dateBegin, double intervalSec = 600d, int approxPointsCount = 1024);
/// <summary> /// <summary>
/// Получить диапазон дат, для которых есть данные в репозитории /// Получить диапазон дат, для которых есть данные в репозитории

View File

@ -15,7 +15,7 @@ public interface ITimeSeriesBaseRepository<TDto>
/// <param name="dateEnd">дата окончания</param> /// <param name="dateEnd">дата окончания</param>
/// <param name="approxPointsCount"></param> /// <param name="approxPointsCount"></param>
/// <returns></returns> /// <returns></returns>
Task<IEnumerable<TDto>> GetResampledData(DateTimeOffset dateBegin, DateTimeOffset dateEnd, int approxPointsCount = 1024); Task<IEnumerable<TDto>> GetResampledData(DateTimeOffset dateBegin, double intervalSec = 600d, int approxPointsCount = 1024);
/// <summary> /// <summary>
/// Получить диапазон дат, для которых есть данные в репозитории /// Получить диапазон дат, для которых есть данные в репозитории