1. Убран Id у DataSaub, теперь в качестве первичного ключа выступает Date
2. Автотесты для методов GetDatesRande, GetResampledData. 3. Рефакторинг
This commit is contained in:
parent
c751f74b35
commit
1fdd199954
@ -32,9 +32,11 @@ public class TimeSeriesController<TDto> : ControllerBase, ITimeSeriesDataApi<TDt
|
||||
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]
|
||||
|
@ -33,6 +33,11 @@ public class Startup
|
||||
|
||||
app.UseRouting();
|
||||
|
||||
if (env.IsDevelopment())
|
||||
{
|
||||
app.UseDeveloperExceptionPage();
|
||||
}
|
||||
|
||||
app.UseAuthentication();
|
||||
app.UseAuthorization();
|
||||
|
||||
|
@ -12,7 +12,7 @@ using Persistence.Database.Model;
|
||||
namespace Persistence.Database.Postgres.Migrations
|
||||
{
|
||||
[DbContext(typeof(PersistenceDbContext))]
|
||||
[Migration("20241118091712_InitialCreate")]
|
||||
[Migration("20241122074646_InitialCreate")]
|
||||
partial class InitialCreate
|
||||
{
|
||||
/// <inheritdoc />
|
||||
@ -29,12 +29,9 @@ namespace Persistence.Database.Postgres.Migrations
|
||||
|
||||
modelBuilder.Entity("Persistence.Database.Model.DataSaub", b =>
|
||||
{
|
||||
b.Property<int>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("integer")
|
||||
.HasColumnName("id");
|
||||
|
||||
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));
|
||||
b.Property<DateTimeOffset>("Date")
|
||||
.HasColumnType("timestamp with time zone")
|
||||
.HasColumnName("date");
|
||||
|
||||
b.Property<double?>("AxialLoad")
|
||||
.HasColumnType("double precision")
|
||||
@ -52,10 +49,6 @@ namespace Persistence.Database.Postgres.Migrations
|
||||
.HasColumnType("double precision")
|
||||
.HasColumnName("blockSpeed");
|
||||
|
||||
b.Property<DateTimeOffset>("Date")
|
||||
.HasColumnType("timestamp with time zone")
|
||||
.HasColumnName("date");
|
||||
|
||||
b.Property<double?>("Flow")
|
||||
.HasColumnType("double precision")
|
||||
.HasColumnName("flow");
|
||||
@ -112,7 +105,7 @@ namespace Persistence.Database.Postgres.Migrations
|
||||
.HasColumnType("double precision")
|
||||
.HasColumnName("wellDepth");
|
||||
|
||||
b.HasKey("Id");
|
||||
b.HasKey("Date");
|
||||
|
||||
b.ToTable("DataSaub");
|
||||
});
|
@ -1,6 +1,5 @@
|
||||
using System;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
|
||||
|
||||
#nullable disable
|
||||
|
||||
@ -19,8 +18,6 @@ namespace Persistence.Database.Postgres.Migrations
|
||||
name: "DataSaub",
|
||||
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),
|
||||
mode = table.Column<int>(type: "integer", nullable: true),
|
||||
user = table.Column<string>(type: "text", nullable: true),
|
||||
@ -43,7 +40,7 @@ namespace Persistence.Database.Postgres.Migrations
|
||||
},
|
||||
constraints: table =>
|
||||
{
|
||||
table.PrimaryKey("PK_DataSaub", x => x.id);
|
||||
table.PrimaryKey("PK_DataSaub", x => x.date);
|
||||
});
|
||||
}
|
||||
|
@ -26,12 +26,9 @@ namespace Persistence.Database.Postgres.Migrations
|
||||
|
||||
modelBuilder.Entity("Persistence.Database.Model.DataSaub", b =>
|
||||
{
|
||||
b.Property<int>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("integer")
|
||||
.HasColumnName("id");
|
||||
|
||||
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));
|
||||
b.Property<DateTimeOffset>("Date")
|
||||
.HasColumnType("timestamp with time zone")
|
||||
.HasColumnName("date");
|
||||
|
||||
b.Property<double?>("AxialLoad")
|
||||
.HasColumnType("double precision")
|
||||
@ -49,10 +46,6 @@ namespace Persistence.Database.Postgres.Migrations
|
||||
.HasColumnType("double precision")
|
||||
.HasColumnName("blockSpeed");
|
||||
|
||||
b.Property<DateTimeOffset>("Date")
|
||||
.HasColumnType("timestamp with time zone")
|
||||
.HasColumnName("date");
|
||||
|
||||
b.Property<double?>("Flow")
|
||||
.HasColumnType("double precision")
|
||||
.HasColumnName("flow");
|
||||
@ -109,7 +102,7 @@ namespace Persistence.Database.Postgres.Migrations
|
||||
.HasColumnType("double precision")
|
||||
.HasColumnName("wellDepth");
|
||||
|
||||
b.HasKey("Id");
|
||||
b.HasKey("Date");
|
||||
|
||||
b.ToTable("DataSaub");
|
||||
});
|
||||
|
@ -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;
|
||||
public class DataSaub : ITimestampedData
|
||||
{
|
||||
[Column("id")]
|
||||
public int Id { get; set; }
|
||||
|
||||
[Column("date")]
|
||||
[Key, Column("date")]
|
||||
public DateTimeOffset Date { get; set; }
|
||||
|
||||
[Column("mode")]
|
||||
|
@ -1,13 +1,22 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Persistence.Models;
|
||||
using Refit;
|
||||
|
||||
namespace Persistence.IntegrationTests.Clients;
|
||||
public interface ITimeSeriesClient<TDto>
|
||||
where TDto : class, new()
|
||||
{
|
||||
[Post("/api/dataSaub")]
|
||||
Task<IApiResponse<int>> InsertRangeAsync(IEnumerable<TDto> dtos);
|
||||
private const string BaseRoute = "/api/dataSaub";
|
||||
|
||||
[Get("/api/dataSaub")]
|
||||
Task<IApiResponse<IEnumerable<TDto>>> GetAsync(DateTimeOffset dateBegin, DateTimeOffset dateEnd);
|
||||
[Post($"{BaseRoute}")]
|
||||
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();
|
||||
}
|
||||
|
@ -14,7 +14,6 @@ public class DataSaubControllerTest : TimeSeriesBaseControllerTest<DataSaub, Dat
|
||||
Date = DateTimeOffset.UtcNow,
|
||||
Flow = 5,
|
||||
HookWeight = 6,
|
||||
Id = 7,
|
||||
IdFeedRegulator = 8,
|
||||
Mode = 9,
|
||||
Mse = 10,
|
||||
@ -38,7 +37,6 @@ public class DataSaubControllerTest : TimeSeriesBaseControllerTest<DataSaub, Dat
|
||||
Date = DateTimeOffset.UtcNow,
|
||||
Flow = 5,
|
||||
HookWeight = 6,
|
||||
Id = 7,
|
||||
IdFeedRegulator = 8,
|
||||
Mode = 9,
|
||||
Mse = 10,
|
||||
@ -70,4 +68,18 @@ public class DataSaubControllerTest : TimeSeriesBaseControllerTest<DataSaub, Dat
|
||||
var endDate = DateTimeOffset.UtcNow;
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
@ -1,20 +1,12 @@
|
||||
using Mapster;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Configuration.UserSecrets;
|
||||
using Persistence.Database.Model;
|
||||
using Persistence.IntegrationTests.Clients;
|
||||
using Persistence.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Xunit;
|
||||
|
||||
namespace Persistence.IntegrationTests.Controllers;
|
||||
public abstract class TimeSeriesBaseControllerTest<TEntity, TDto> : BaseIntegrationTest
|
||||
where TEntity : class
|
||||
where TEntity : class, ITimestampedData, new()
|
||||
where TDto : class, new()
|
||||
{
|
||||
private ITimeSeriesClient<TDto> client;
|
||||
@ -26,7 +18,7 @@ public abstract class TimeSeriesBaseControllerTest<TEntity, TDto> : BaseIntegrat
|
||||
Task.Run(async () =>
|
||||
{
|
||||
client = await factory.GetAuthorizedHttpClient<ITimeSeriesClient<TDto>>(string.Empty);
|
||||
}).Wait();
|
||||
}).Wait();
|
||||
}
|
||||
|
||||
public async Task InsertRangeSuccess(TDto dto)
|
||||
@ -35,7 +27,7 @@ public abstract class TimeSeriesBaseControllerTest<TEntity, TDto> : BaseIntegrat
|
||||
var expected = dto.Adapt<TDto>();
|
||||
|
||||
//act
|
||||
var response = await client.InsertRangeAsync(new TDto[] { expected });
|
||||
var response = await client.InsertRange(new TDto[] { expected });
|
||||
|
||||
//assert
|
||||
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
||||
@ -51,11 +43,80 @@ public abstract class TimeSeriesBaseControllerTest<TEntity, TDto> : BaseIntegrat
|
||||
|
||||
dbContext.SaveChanges();
|
||||
|
||||
var response = await client.GetAsync(beginDate, endDate);
|
||||
var response = await client.Get(beginDate, endDate);
|
||||
|
||||
//assert
|
||||
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
||||
Assert.NotNull(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());
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@ -4,8 +4,6 @@ using System.ComponentModel.DataAnnotations.Schema;
|
||||
namespace Persistence.Repository.Data;
|
||||
public class DataSaubDto : ITimeSeriesAbstractDto
|
||||
{
|
||||
public int Id { get; set; }
|
||||
|
||||
public DateTimeOffset Date { get; set; } = DateTimeOffset.UtcNow;
|
||||
|
||||
public int? Mode { get; set; }
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -84,9 +84,26 @@ public class TimeSeriesDataRepository<TEntity, TDto> : ITimeSeriesDataRepository
|
||||
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
|
||||
throw new NotImplementedException();
|
||||
//if (!caches.TryGetValue(idTelemetry, out TelemetryDataCacheItem? cacheItem))
|
||||
// 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;
|
||||
}
|
||||
}
|
||||
|
@ -15,7 +15,7 @@ public interface ITimeSeriesBaseDataApi<TDto>
|
||||
/// <param name="dateEnd">дата окончания</param>
|
||||
/// <param name="approxPointsCount"></param>
|
||||
/// <returns></returns>
|
||||
Task<IActionResult> GetResampledData(DateTimeOffset dateBegin, DateTimeOffset dateEnd, int approxPointsCount = 1024);
|
||||
Task<IActionResult> GetResampledData(DateTimeOffset dateBegin, double intervalSec = 600d, int approxPointsCount = 1024);
|
||||
|
||||
/// <summary>
|
||||
/// Получить диапазон дат, для которых есть данные в репозитории
|
||||
|
@ -15,7 +15,7 @@ public interface ITimeSeriesBaseRepository<TDto>
|
||||
/// <param name="dateEnd">дата окончания</param>
|
||||
/// <param name="approxPointsCount"></param>
|
||||
/// <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>
|
||||
/// Получить диапазон дат, для которых есть данные в репозитории
|
||||
|
Loading…
Reference in New Issue
Block a user