Compare commits
No commits in common. "c496cea07a62c01becf81d43c900e74b3949fb18" and "7d5370bf43eda21fb71d142302a09cfba95f4e67" have entirely different histories.
c496cea07a
...
7d5370bf43
@ -19,32 +19,24 @@ public class TimeSeriesController<TDto> : ControllerBase, ITimeSeriesDataApi<TDt
|
||||
|
||||
[HttpGet]
|
||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||
public async Task<IActionResult> Get(DateTimeOffset dateBegin, CancellationToken token)
|
||||
public async Task<IActionResult> GetAsync(DateTimeOffset dateBegin, DateTimeOffset dateEnd, CancellationToken token)
|
||||
{
|
||||
var result = await this.timeSeriesDataRepository.GetGtDate(dateBegin, token);
|
||||
var result = await this.timeSeriesDataRepository.GetAsync(dateBegin, dateEnd, token);
|
||||
return Ok(result);
|
||||
|
||||
}
|
||||
|
||||
[HttpGet("datesRange")]
|
||||
public async Task<IActionResult> GetDatesRange(CancellationToken token)
|
||||
public async Task<IActionResult> GetDatesRangeAsync(CancellationToken token)
|
||||
{
|
||||
var result = await this.timeSeriesDataRepository.GetDatesRange(token);
|
||||
return Ok(result);
|
||||
}
|
||||
|
||||
[HttpGet("resampled")]
|
||||
public async Task<IActionResult> GetResampledData(DateTimeOffset dateBegin, double intervalSec = 600d, int approxPointsCount = 1024, CancellationToken token = default)
|
||||
{
|
||||
var result = await this.timeSeriesDataRepository.GetResampledData(dateBegin, intervalSec, approxPointsCount, token);
|
||||
var result = await this.timeSeriesDataRepository.GetDatesRangeAsync(token);
|
||||
return Ok(result);
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
public async Task<IActionResult> InsertRange(IEnumerable<TDto> dtos, CancellationToken token)
|
||||
public async Task<IActionResult> InsertRangeAsync(IEnumerable<TDto> dtos, CancellationToken token)
|
||||
{
|
||||
var result = await this.timeSeriesDataRepository.InsertRange(dtos, token);
|
||||
return Ok(result);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@ -1,13 +1,10 @@
|
||||
|
||||
using Persistence.Models;
|
||||
|
||||
namespace Persistence.API;
|
||||
|
||||
public class Program
|
||||
{
|
||||
public static void Main(string[] args)
|
||||
{
|
||||
|
||||
var host = CreateHostBuilder(args).Build();
|
||||
Startup.BeforeRunHandler(host);
|
||||
host.Run();
|
||||
|
@ -33,11 +33,6 @@ public class Startup
|
||||
|
||||
app.UseRouting();
|
||||
|
||||
if (env.IsDevelopment())
|
||||
{
|
||||
app.UseDeveloperExceptionPage();
|
||||
}
|
||||
|
||||
app.UseAuthentication();
|
||||
app.UseAuthorization();
|
||||
|
||||
|
@ -4,12 +4,5 @@
|
||||
"Port": 5432,
|
||||
"Username": "postgres",
|
||||
"Password": "q"
|
||||
},
|
||||
"KeycloakTestUser": {
|
||||
"username": "myuser",
|
||||
"password": 12345,
|
||||
"clientId": "webapi",
|
||||
"grantType": "password"
|
||||
},
|
||||
"KeycloakGetTokenUrl": "http://192.168.0.10:8321/realms/Persistence/protocol/openid-connect/token"
|
||||
}
|
||||
}
|
||||
|
@ -10,9 +10,9 @@
|
||||
},
|
||||
"AllowedHosts": "*",
|
||||
"Authentication": {
|
||||
"MetadataAddress": "http://192.168.0.10:8321/realms/Persistence/.well-known/openid-configuration",
|
||||
"MetadataAddress": "http://localhost:8080/realms/TestRealm/.well-known/openid-configuration",
|
||||
"Audience": "account",
|
||||
"ValidIssuer": "http://192.168.0.10:8321/realms/Persistence",
|
||||
"AuthorizationUrl": "http://192.168.0.10:8321/realms/Persistence/protocol/openid-connect/auth"
|
||||
"ValidIssuer": "http://localhost:8080/realms/TestRealm",
|
||||
"AuthorizationUrl": "http://localhost:8080/realms/TestRealm/protocol/openid-connect/auth"
|
||||
}
|
||||
}
|
||||
|
@ -12,7 +12,7 @@ using Persistence.Database.Model;
|
||||
namespace Persistence.Database.Postgres.Migrations
|
||||
{
|
||||
[DbContext(typeof(PersistenceDbContext))]
|
||||
[Migration("20241122074646_InitialCreate")]
|
||||
[Migration("20241118091712_InitialCreate")]
|
||||
partial class InitialCreate
|
||||
{
|
||||
/// <inheritdoc />
|
||||
@ -29,9 +29,12 @@ namespace Persistence.Database.Postgres.Migrations
|
||||
|
||||
modelBuilder.Entity("Persistence.Database.Model.DataSaub", b =>
|
||||
{
|
||||
b.Property<DateTimeOffset>("Date")
|
||||
.HasColumnType("timestamp with time zone")
|
||||
.HasColumnName("date");
|
||||
b.Property<int>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("integer")
|
||||
.HasColumnName("id");
|
||||
|
||||
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));
|
||||
|
||||
b.Property<double?>("AxialLoad")
|
||||
.HasColumnType("double precision")
|
||||
@ -49,6 +52,10 @@ 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");
|
||||
@ -105,7 +112,7 @@ namespace Persistence.Database.Postgres.Migrations
|
||||
.HasColumnType("double precision")
|
||||
.HasColumnName("wellDepth");
|
||||
|
||||
b.HasKey("Date");
|
||||
b.HasKey("Id");
|
||||
|
||||
b.ToTable("DataSaub");
|
||||
});
|
@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
|
||||
|
||||
#nullable disable
|
||||
|
||||
@ -18,6 +19,8 @@ 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),
|
||||
@ -40,7 +43,7 @@ namespace Persistence.Database.Postgres.Migrations
|
||||
},
|
||||
constraints: table =>
|
||||
{
|
||||
table.PrimaryKey("PK_DataSaub", x => x.date);
|
||||
table.PrimaryKey("PK_DataSaub", x => x.id);
|
||||
});
|
||||
}
|
||||
|
@ -26,9 +26,12 @@ namespace Persistence.Database.Postgres.Migrations
|
||||
|
||||
modelBuilder.Entity("Persistence.Database.Model.DataSaub", b =>
|
||||
{
|
||||
b.Property<DateTimeOffset>("Date")
|
||||
.HasColumnType("timestamp with time zone")
|
||||
.HasColumnName("date");
|
||||
b.Property<int>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("integer")
|
||||
.HasColumnName("id");
|
||||
|
||||
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));
|
||||
|
||||
b.Property<double?>("AxialLoad")
|
||||
.HasColumnType("double precision")
|
||||
@ -46,6 +49,10 @@ 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");
|
||||
@ -102,7 +109,7 @@ namespace Persistence.Database.Postgres.Migrations
|
||||
.HasColumnType("double precision")
|
||||
.HasColumnName("wellDepth");
|
||||
|
||||
b.HasKey("Date");
|
||||
b.HasKey("Id");
|
||||
|
||||
b.ToTable("DataSaub");
|
||||
});
|
||||
|
@ -1,11 +1,12 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
|
||||
namespace Persistence.Database.Model;
|
||||
public class DataSaub : ITimestampedData
|
||||
{
|
||||
[Key, Column("date")]
|
||||
[Column("id")]
|
||||
public int Id { get; set; }
|
||||
|
||||
[Column("date")]
|
||||
public DateTimeOffset Date { get; set; }
|
||||
|
||||
[Column("mode")]
|
||||
|
@ -1,22 +1,13 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Persistence.Models;
|
||||
using Refit;
|
||||
|
||||
namespace Persistence.IntegrationTests.Clients;
|
||||
public interface ITimeSeriesClient<TDto>
|
||||
where TDto : class, new()
|
||||
{
|
||||
private const string BaseRoute = "/api/dataSaub";
|
||||
[Post("/api/dataSaub")]
|
||||
Task<IApiResponse<int>> InsertRangeAsync(IEnumerable<TDto> dtos);
|
||||
|
||||
[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();
|
||||
[Get("/api/dataSaub")]
|
||||
Task<IApiResponse<IEnumerable<TDto>>> GetAsync(DateTimeOffset dateBegin, DateTimeOffset dateEnd);
|
||||
}
|
||||
|
@ -11,9 +11,10 @@ public class DataSaubControllerTest : TimeSeriesBaseControllerTest<DataSaub, Dat
|
||||
BitDepth = 2,
|
||||
BlockPosition = 3,
|
||||
BlockSpeed = 4,
|
||||
Date = DateTimeOffset.UtcNow,
|
||||
Date = DateTimeOffset.Now,
|
||||
Flow = 5,
|
||||
HookWeight = 6,
|
||||
Id = 7,
|
||||
IdFeedRegulator = 8,
|
||||
Mode = 9,
|
||||
Mse = 10,
|
||||
@ -37,6 +38,7 @@ public class DataSaubControllerTest : TimeSeriesBaseControllerTest<DataSaub, Dat
|
||||
Date = DateTimeOffset.UtcNow,
|
||||
Flow = 5,
|
||||
HookWeight = 6,
|
||||
Id = 7,
|
||||
IdFeedRegulator = 8,
|
||||
Mode = 9,
|
||||
Mse = 10,
|
||||
@ -68,18 +70,4 @@ 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,12 +1,20 @@
|
||||
using Mapster;
|
||||
using Persistence.Database.Model;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Configuration.UserSecrets;
|
||||
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, ITimestampedData, new()
|
||||
where TEntity : class
|
||||
where TDto : class, new()
|
||||
{
|
||||
private ITimeSeriesClient<TDto> client;
|
||||
@ -14,11 +22,7 @@ public abstract class TimeSeriesBaseControllerTest<TEntity, TDto> : BaseIntegrat
|
||||
public TimeSeriesBaseControllerTest(WebAppFactoryFixture factory) : base(factory)
|
||||
{
|
||||
dbContext.CleanupDbSet<TEntity>();
|
||||
|
||||
Task.Run(async () =>
|
||||
{
|
||||
client = await factory.GetAuthorizedHttpClient<ITimeSeriesClient<TDto>>(string.Empty);
|
||||
}).Wait();
|
||||
client = factory.GetHttpClient<ITimeSeriesClient<TDto>>(string.Empty);
|
||||
}
|
||||
|
||||
public async Task InsertRangeSuccess(TDto dto)
|
||||
@ -27,7 +31,7 @@ public abstract class TimeSeriesBaseControllerTest<TEntity, TDto> : BaseIntegrat
|
||||
var expected = dto.Adapt<TDto>();
|
||||
|
||||
//act
|
||||
var response = await client.InsertRange(new TDto[] { expected });
|
||||
var response = await client.InsertRangeAsync(new TDto[] { expected });
|
||||
|
||||
//assert
|
||||
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
||||
@ -43,80 +47,11 @@ public abstract class TimeSeriesBaseControllerTest<TEntity, TDto> : BaseIntegrat
|
||||
|
||||
dbContext.SaveChanges();
|
||||
|
||||
var response = await client.Get(beginDate, endDate);
|
||||
var response = await client.GetAsync(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());
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@ -1,8 +0,0 @@
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace Persistence.IntegrationTests;
|
||||
public class JwtToken
|
||||
{
|
||||
[JsonPropertyName("access_token")]
|
||||
public required string AccessToken { get; set; }
|
||||
}
|
@ -1,27 +0,0 @@
|
||||
namespace Persistence.IntegrationTests;
|
||||
|
||||
/// <summary>
|
||||
/// настройки credentials для пользователя в KeyCloak
|
||||
/// </summary>
|
||||
public class KeyCloakUser
|
||||
{
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public required string Username { get; set; }
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public required string Password { get; set; }
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public required string ClientId { get; set; }
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public required string GrantType { get; set; }
|
||||
}
|
@ -15,7 +15,6 @@
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration" Version="9.0.0" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
|
||||
<PackageReference Include="Refit" Version="8.0.0" />
|
||||
<PackageReference Include="RestSharp" Version="112.1.0" />
|
||||
<PackageReference Include="xunit" Version="2.9.2" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
|
@ -3,12 +3,10 @@ using Microsoft.AspNetCore.Mvc.Testing;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Persistence.API;
|
||||
using Persistence.Database.Model;
|
||||
using Persistence.Database.Postgres;
|
||||
using Persistence.API;
|
||||
using Refit;
|
||||
using RestSharp;
|
||||
using System.Net.Http.Headers;
|
||||
using System.Text.Json;
|
||||
using Persistence.Database.Postgres;
|
||||
|
||||
@ -25,8 +23,6 @@ public class WebAppFactoryFixture : WebApplicationFactory<Startup>
|
||||
private static readonly RefitSettings RefitSettings = new(new SystemTextJsonContentSerializer(JsonSerializerOptions));
|
||||
|
||||
private readonly string connectionString;
|
||||
private readonly KeyCloakUser keycloakTestUser;
|
||||
public readonly string KeycloakGetTokenUrl;
|
||||
|
||||
public WebAppFactoryFixture()
|
||||
{
|
||||
@ -36,10 +32,6 @@ public class WebAppFactoryFixture : WebApplicationFactory<Startup>
|
||||
|
||||
var dbConnection = configuration.GetSection("DbConnection").Get<DbConnection>()!;
|
||||
connectionString = dbConnection.GetConnectionString();
|
||||
|
||||
keycloakTestUser = configuration.GetSection("KeycloakTestUser").Get<KeyCloakUser>()!;
|
||||
|
||||
KeycloakGetTokenUrl = configuration.GetSection("KeycloakGetTokenUrl").Value!;
|
||||
}
|
||||
|
||||
protected override void ConfigureWebHost(IWebHostBuilder builder)
|
||||
@ -61,6 +53,7 @@ public class WebAppFactoryFixture : WebApplicationFactory<Startup>
|
||||
var dbContext = scopedServices.GetRequiredService<PersistenceDbContext>();
|
||||
|
||||
dbContext.Database.EnsureCreatedAndMigrated();
|
||||
//dbContext.Deposits.AddRange(Data.Defaults.Deposits);
|
||||
dbContext.SaveChanges();
|
||||
});
|
||||
}
|
||||
@ -87,44 +80,23 @@ public class WebAppFactoryFixture : WebApplicationFactory<Startup>
|
||||
return RestService.For<T>(httpClient, RefitSettings);
|
||||
}
|
||||
|
||||
public async Task<T> GetAuthorizedHttpClient<T>(string uriSuffix)
|
||||
{
|
||||
var httpClient = await GetAuthorizedHttpClient();
|
||||
if (string.IsNullOrEmpty(uriSuffix))
|
||||
return RestService.For<T>(httpClient, RefitSettings);
|
||||
//public T GetAuthorizedHttpClient<T>(string uriSuffix)
|
||||
//{
|
||||
// var httpClient = GetAuthorizedHttpClient();
|
||||
// if (string.IsNullOrEmpty(uriSuffix))
|
||||
// return RestService.For<T>(httpClient, RefitSettings);
|
||||
|
||||
if (httpClient.BaseAddress is not null)
|
||||
httpClient.BaseAddress = new Uri(httpClient.BaseAddress, uriSuffix);
|
||||
// if (httpClient.BaseAddress is not null)
|
||||
// httpClient.BaseAddress = new Uri(httpClient.BaseAddress, uriSuffix);
|
||||
|
||||
return RestService.For<T>(httpClient, RefitSettings);
|
||||
}
|
||||
|
||||
private async Task<HttpClient> GetAuthorizedHttpClient()
|
||||
{
|
||||
var httpClient = CreateClient();
|
||||
var token = await GetTokenAsync();
|
||||
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
|
||||
|
||||
return httpClient;
|
||||
}
|
||||
|
||||
private async Task<string> GetTokenAsync()
|
||||
{
|
||||
var restClient = new RestClient();
|
||||
|
||||
var request = new RestRequest(KeycloakGetTokenUrl, Method.Post);
|
||||
request.AddParameter("username", keycloakTestUser.Username);
|
||||
request.AddParameter("password", keycloakTestUser.Password);
|
||||
request.AddParameter("client_id", keycloakTestUser.ClientId);
|
||||
request.AddParameter("grant_type", keycloakTestUser.GrantType);
|
||||
|
||||
var keyCloackResponse = await restClient.PostAsync(request);
|
||||
if (keyCloackResponse.IsSuccessful && !String.IsNullOrEmpty(keyCloackResponse.Content))
|
||||
{
|
||||
var token = JsonSerializer.Deserialize<JwtToken>(keyCloackResponse.Content)!;
|
||||
return token.AccessToken;
|
||||
}
|
||||
|
||||
return String.Empty;
|
||||
}
|
||||
// return RestService.For<T>(httpClient, RefitSettings);
|
||||
//}
|
||||
|
||||
//private HttpClient GetAuthorizedHttpClient()
|
||||
//{
|
||||
// var httpClient = CreateClient();
|
||||
// var jwtToken = ApiTokenHelper.GetAdminUserToken();
|
||||
// httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", jwtToken);
|
||||
// return httpClient;
|
||||
//}
|
||||
}
|
||||
|
@ -4,6 +4,8 @@ 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; }
|
||||
|
@ -10,8 +10,8 @@ public class TimeSeriesDataCachedRepository<TEntity, TDto> : TimeSeriesDataRepos
|
||||
where TEntity : class, ITimestampedData, new()
|
||||
where TDto : class, ITimeSeriesAbstractDto, new()
|
||||
{
|
||||
public static TDto? FirstByDate { get; private set; }
|
||||
public static CyclicArray<TDto> LastData { get; } = new CyclicArray<TDto>(CacheItemsCount);
|
||||
public static TDto FirstByDate { get; set; } = default!;
|
||||
public static CyclicArray<TDto> LastData { get; set; } = null!;
|
||||
|
||||
private const int CacheItemsCount = 3600;
|
||||
|
||||
@ -19,6 +19,8 @@ public class TimeSeriesDataCachedRepository<TEntity, TDto> : TimeSeriesDataRepos
|
||||
{
|
||||
Task.Run(async () =>
|
||||
{
|
||||
LastData = new CyclicArray<TDto>(CacheItemsCount);
|
||||
|
||||
var firstDateItem = await base.GetFirstAsync(CancellationToken.None);
|
||||
if (firstDateItem == null)
|
||||
{
|
||||
@ -33,17 +35,17 @@ public class TimeSeriesDataCachedRepository<TEntity, TDto> : TimeSeriesDataRepos
|
||||
}).Wait();
|
||||
}
|
||||
|
||||
public override async Task<IEnumerable<TDto>> GetGtDate(DateTimeOffset dateBegin, CancellationToken token)
|
||||
public override async Task<IEnumerable<TDto>> GetAsync(DateTimeOffset dateBegin, DateTimeOffset dateEnd, CancellationToken token)
|
||||
{
|
||||
|
||||
if (LastData.Count() == 0 || LastData[0].Date > dateBegin)
|
||||
{
|
||||
var dtos = await base.GetGtDate(dateBegin, token);
|
||||
var dtos = await base.GetAsync(dateBegin, dateEnd, token);
|
||||
return dtos;
|
||||
}
|
||||
|
||||
var items = LastData
|
||||
.Where(i => i.Date >= dateBegin);
|
||||
.Where(i => i.Date >= dateBegin && i.Date <= dateEnd);
|
||||
|
||||
return items;
|
||||
}
|
||||
@ -62,44 +64,5 @@ public class TimeSeriesDataCachedRepository<TEntity, TDto> : TimeSeriesDataRepos
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public override async Task<DatesRangeDto?> GetDatesRange(CancellationToken token)
|
||||
{
|
||||
if (FirstByDate == null)
|
||||
return null;
|
||||
|
||||
return await Task.Run(() =>
|
||||
{
|
||||
return new DatesRangeDto
|
||||
{
|
||||
From = FirstByDate.Date,
|
||||
To = LastData[^1].Date
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
public override async Task<IEnumerable<TDto>> GetResampledData(
|
||||
DateTimeOffset dateBegin,
|
||||
double intervalSec = 600d,
|
||||
int approxPointsCount = 1024,
|
||||
CancellationToken token = default)
|
||||
{
|
||||
var dtos = LastData.Where(i => i.Date >= dateBegin);
|
||||
if (LastData.Count == 0 || LastData[0].Date > dateBegin)
|
||||
{
|
||||
dtos = await base.GetGtDate(dateBegin, token);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -18,7 +18,16 @@ public class TimeSeriesDataRepository<TEntity, TDto> : ITimeSeriesDataRepository
|
||||
|
||||
protected virtual IQueryable<TEntity> GetQueryReadOnly() => this.db.Set<TEntity>();
|
||||
|
||||
public virtual async Task<DatesRangeDto?> GetDatesRange(CancellationToken token)
|
||||
public virtual async Task<IEnumerable<TDto>> GetAsync(DateTimeOffset dateBegin, DateTimeOffset dateEnd, CancellationToken token)
|
||||
{
|
||||
var query = GetQueryReadOnly();
|
||||
var entities = await query.ToArrayAsync(token);
|
||||
var dtos = entities.Select(e => e.Adapt<TDto>());
|
||||
|
||||
return dtos;
|
||||
}
|
||||
|
||||
public virtual async Task<DatesRangeDto> GetDatesRangeAsync(CancellationToken token)
|
||||
{
|
||||
var query = GetQueryReadOnly();
|
||||
var minDate = await query.MinAsync(o => o.Date, token);
|
||||
@ -51,7 +60,7 @@ public class TimeSeriesDataRepository<TEntity, TDto> : ITimeSeriesDataRepository
|
||||
return result;
|
||||
}
|
||||
|
||||
protected async Task<IEnumerable<TDto>> GetLastAsync(int takeCount, CancellationToken token)
|
||||
public async Task<IEnumerable<TDto>> GetLastAsync(int takeCount, CancellationToken token)
|
||||
{
|
||||
var query = GetQueryReadOnly()
|
||||
.OrderByDescending(e => e.Date)
|
||||
@ -63,7 +72,7 @@ public class TimeSeriesDataRepository<TEntity, TDto> : ITimeSeriesDataRepository
|
||||
return dtos;
|
||||
}
|
||||
|
||||
protected async Task<TDto?> GetFirstAsync(CancellationToken token)
|
||||
public async Task<TDto?> GetFirstAsync(CancellationToken token)
|
||||
{
|
||||
var query = GetQueryReadOnly()
|
||||
.OrderBy(e => e.Date);
|
||||
@ -76,24 +85,4 @@ public class TimeSeriesDataRepository<TEntity, TDto> : ITimeSeriesDataRepository
|
||||
var dto = entity.Adapt<TDto>();
|
||||
return dto;
|
||||
}
|
||||
|
||||
public async virtual Task<IEnumerable<TDto>> GetResampledData(
|
||||
DateTimeOffset dateBegin,
|
||||
double intervalSec = 600d,
|
||||
int approxPointsCount = 1024,
|
||||
CancellationToken token = default)
|
||||
{
|
||||
var dtos = await GetGtDate(dateBegin, token);
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
@ -31,7 +31,7 @@ public interface IChangeLogApi<TDto, TChangeLogDto>
|
||||
/// <param name="dto"></param>
|
||||
/// <param name="token"></param>
|
||||
/// <returns></returns>
|
||||
Task<ActionResult<int>> Add(TDto dto, CancellationToken token);
|
||||
Task<ActionResult<int>> AddAsync(TDto dto, CancellationToken token);
|
||||
|
||||
/// <summary>
|
||||
/// Добавить несколько записей
|
||||
@ -39,7 +39,7 @@ public interface IChangeLogApi<TDto, TChangeLogDto>
|
||||
/// <param name="dtos"></param>
|
||||
/// <param name="token"></param>
|
||||
/// <returns></returns>
|
||||
Task<ActionResult<int>> AddRange(IEnumerable<TDto> dtos, CancellationToken token);
|
||||
Task<ActionResult<int>> AddRangeAsync(IEnumerable<TDto> dtos, CancellationToken token);
|
||||
|
||||
/// <summary>
|
||||
/// Обновить одну запись
|
||||
@ -47,7 +47,7 @@ public interface IChangeLogApi<TDto, TChangeLogDto>
|
||||
/// <param name="dto"></param>
|
||||
/// <param name="token"></param>
|
||||
/// <returns></returns>
|
||||
Task<ActionResult<int>> Update(TDto dto, CancellationToken token);
|
||||
Task<ActionResult<int>> UpdateAsync(TDto dto, CancellationToken token);
|
||||
|
||||
/// <summary>
|
||||
/// Обновить несколько записей
|
||||
@ -55,7 +55,7 @@ public interface IChangeLogApi<TDto, TChangeLogDto>
|
||||
/// <param name="dtos"></param>
|
||||
/// <param name="token"></param>
|
||||
/// <returns></returns>
|
||||
Task<ActionResult<int>> UpdateRange(IEnumerable<TDto> dtos, CancellationToken token);
|
||||
Task<ActionResult<int>> UpdateRangeAsync(IEnumerable<TDto> dtos, CancellationToken token);
|
||||
|
||||
/// <summary>
|
||||
/// Удалить одну запись
|
||||
@ -63,7 +63,7 @@ public interface IChangeLogApi<TDto, TChangeLogDto>
|
||||
/// <param name="id"></param>
|
||||
/// <param name="token"></param>
|
||||
/// <returns></returns>
|
||||
Task<ActionResult<int>> Delete(int id, CancellationToken token);
|
||||
Task<ActionResult<int>> DeleteAsync(int id, CancellationToken token);
|
||||
|
||||
/// <summary>
|
||||
/// Удалить несколько записей
|
||||
@ -71,5 +71,5 @@ public interface IChangeLogApi<TDto, TChangeLogDto>
|
||||
/// <param name="ids"></param>
|
||||
/// <param name="token"></param>
|
||||
/// <returns></returns>
|
||||
Task<ActionResult<int>> DeleteRange(IEnumerable<int> ids, CancellationToken token);
|
||||
Task<ActionResult<int>> DeleteRangeAsync(IEnumerable<int> ids, CancellationToken token);
|
||||
}
|
||||
|
@ -13,7 +13,7 @@ public interface IDictionaryElementApi<TDto> where TDto : class, new()
|
||||
/// <param name="dictionaryKey">ключ справочника</param>
|
||||
/// <param name="token"></param>
|
||||
/// <returns></returns>
|
||||
Task<ActionResult<IEnumerable<TDto>>> Get(Guid dictionaryKey, CancellationToken token);
|
||||
Task<ActionResult<IEnumerable<TDto>>> GetAsync(Guid dictionaryKey, CancellationToken token);
|
||||
|
||||
/// <summary>
|
||||
/// Добавить элемент в справочник
|
||||
@ -22,7 +22,7 @@ public interface IDictionaryElementApi<TDto> where TDto : class, new()
|
||||
/// <param name="dto"></param>
|
||||
/// <param name="token"></param>
|
||||
/// <returns></returns>
|
||||
Task<ActionResult<Guid>> Add(Guid dictionaryKey, TDto dto, CancellationToken token);
|
||||
Task<ActionResult<Guid>> AddAsync(Guid dictionaryKey, TDto dto, CancellationToken token);
|
||||
|
||||
/// <summary>
|
||||
/// Изменить одну запись
|
||||
@ -32,7 +32,7 @@ public interface IDictionaryElementApi<TDto> where TDto : class, new()
|
||||
/// <param name="dto"></param>
|
||||
/// <param name="token"></param>
|
||||
/// <returns></returns>
|
||||
Task<ActionResult<Guid>> Update(Guid dictionaryKey, Guid dictionaryElementKey, TDto dto, CancellationToken token);
|
||||
Task<ActionResult<Guid>> UpdateAsync(Guid dictionaryKey, Guid dictionaryElementKey, TDto dto, CancellationToken token);
|
||||
|
||||
/// <summary>
|
||||
/// Удалить одну запись
|
||||
@ -41,5 +41,5 @@ public interface IDictionaryElementApi<TDto> where TDto : class, new()
|
||||
/// <param name="dictionaryElementKey">ключ элемента в справочнике</param>
|
||||
/// <param name="token"></param>
|
||||
/// <returns></returns>
|
||||
Task<ActionResult<int>> Delete(Guid dictionaryKey, Guid dictionaryElementKey, CancellationToken token);
|
||||
Task<ActionResult<int>> DeleteAsync(Guid dictionaryKey, Guid dictionaryElementKey, CancellationToken token);
|
||||
}
|
||||
|
@ -4,27 +4,23 @@ using Persistence.Models;
|
||||
namespace Persistence.API;
|
||||
|
||||
/// <summary>
|
||||
/// Базовый интерфейс для работы с временными рядами
|
||||
/// Интерфейс для работы с API графиков
|
||||
/// </summary>
|
||||
public interface ITimeSeriesBaseDataApi<TDto>
|
||||
public interface IGraphDataApi<TDto>
|
||||
{
|
||||
/// <summary>
|
||||
/// Получить список объектов с прореживанием, удовлетворяющий диапазону дат
|
||||
/// Получить список объектов с прореживанием, удовлетворящий диапазону дат
|
||||
/// </summary>
|
||||
/// <param name="dateBegin">дата начала</param>
|
||||
/// <param name="dateEnd">дата окончания</param>
|
||||
/// <param name="approxPointsCount"></param>
|
||||
/// <returns></returns>
|
||||
Task<IActionResult> GetResampledData(
|
||||
DateTimeOffset dateBegin,
|
||||
double intervalSec = 600d,
|
||||
int approxPointsCount = 1024,
|
||||
CancellationToken token = default);
|
||||
Task<ActionResult<IEnumerable<TDto>>> GetThinnedDataAsync(DateTimeOffset dateBegin, DateTimeOffset dateEnd, int approxPointsCount = 1024);
|
||||
|
||||
/// <summary>
|
||||
/// Получить диапазон дат, для которых есть данные в репозитории
|
||||
/// </summary>
|
||||
/// <param name="token"></param>
|
||||
/// <returns></returns>
|
||||
Task<IActionResult> GetDatesRange(CancellationToken token);
|
||||
Task<ActionResult<DatesRangeDto>> GetDatesRangeAsync(CancellationToken token);
|
||||
}
|
@ -15,7 +15,7 @@ public interface ISyncApi<TDto> where TDto : class, new()
|
||||
/// <param name="take">количество записей</param>
|
||||
/// <param name="token"></param>
|
||||
/// <returns></returns>
|
||||
Task<ActionResult<IEnumerable<TDto>>> GetPart(DateTimeOffset dateBegin, int take = 24 * 60 * 60, CancellationToken token = default);
|
||||
Task<ActionResult<IEnumerable<TDto>>> GetPartAsync(DateTimeOffset dateBegin, int take = 24 * 60 * 60, CancellationToken token = default);
|
||||
|
||||
/// <summary>
|
||||
/// Получить диапазон дат, для которых есть данные в репозитории
|
||||
|
@ -19,5 +19,5 @@ public interface ITableDataApi<TDto, TRequest>
|
||||
/// <param name="request">параметры фильтрации</param>
|
||||
/// <param name="token"></param>
|
||||
/// <returns></returns>
|
||||
Task<ActionResult<PaginationContainer<TDto>>> GetPage(TRequest request, CancellationToken token);
|
||||
Task<ActionResult<PaginationContainer<TDto>>> GetPageAsync(TRequest request, CancellationToken token);
|
||||
}
|
||||
|
@ -11,16 +11,24 @@ namespace Persistence.API;
|
||||
/// <summary>
|
||||
/// Интерфейс для работы с API временных данных
|
||||
/// </summary>
|
||||
public interface ITimeSeriesDataApi<TDto> : ITimeSeriesBaseDataApi<TDto>
|
||||
public interface ITimeSeriesDataApi<TDto>
|
||||
where TDto : class, ITimeSeriesAbstractDto, new()
|
||||
{
|
||||
/// <summary>
|
||||
/// Получить список объектов, удовлетворяющий диапазон дат
|
||||
/// </summary>
|
||||
/// <param name="dateBegin">дата начала</param>
|
||||
/// <param name="dateEnd">дата окончания</param>
|
||||
/// <param name="token"></param>
|
||||
/// <returns></returns>
|
||||
Task<IActionResult> Get(DateTimeOffset dateBegin, CancellationToken token);
|
||||
Task<IActionResult> GetAsync(DateTimeOffset dateBegin, DateTimeOffset dateEnd, CancellationToken token);
|
||||
|
||||
/// <summary>
|
||||
/// Получить диапазон дат, для которых есть данные в репозитории
|
||||
/// </summary>
|
||||
/// <param name="token"></param>
|
||||
/// <returns></returns>
|
||||
Task<IActionResult> GetDatesRangeAsync(CancellationToken token);
|
||||
|
||||
/// <summary>
|
||||
/// Добавление записей
|
||||
@ -28,7 +36,7 @@ public interface ITimeSeriesDataApi<TDto> : ITimeSeriesBaseDataApi<TDto>
|
||||
/// <param name="dtos"></param>
|
||||
/// <param name="token"></param>
|
||||
/// <returns></returns>
|
||||
Task<IActionResult> InsertRange(IEnumerable<TDto> dtos, CancellationToken token);
|
||||
Task<IActionResult> InsertRangeAsync(IEnumerable<TDto> dtos, CancellationToken token);
|
||||
|
||||
|
||||
}
|
||||
|
@ -26,7 +26,7 @@ public class ChangeLogDto<T> where T: class
|
||||
public DateTimeOffset Creation { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Дата устаревания (например, при удалении)
|
||||
/// Дата устаревания (например при удалении)
|
||||
/// </summary>
|
||||
public DateTimeOffset? Obsolete { get; set; }
|
||||
|
||||
|
@ -5,25 +5,22 @@ namespace Persistence.Repositories;
|
||||
/// <summary>
|
||||
/// Интерфейс по работе с прореженными данными
|
||||
/// </summary>
|
||||
public interface ITimeSeriesBaseRepository<TDto>
|
||||
public interface IGraphDataRepository<TDto>
|
||||
where TDto : class, new()
|
||||
{
|
||||
/// <summary>
|
||||
/// Получить список объектов с прореживанием
|
||||
/// </summary>
|
||||
/// <param name="dateBegin">дата начала</param>
|
||||
/// <param name="dateEnd">дата окончания</param>
|
||||
/// <param name="approxPointsCount"></param>
|
||||
/// <returns></returns>
|
||||
Task<IEnumerable<TDto>> GetResampledData(
|
||||
DateTimeOffset dateBegin,
|
||||
double intervalSec = 600d,
|
||||
int approxPointsCount = 1024,
|
||||
CancellationToken token = default);
|
||||
Task<IEnumerable<TDto>> GetThinnedDataAsync(DateTimeOffset dateBegin, DateTimeOffset dateEnd, int approxPointsCount = 1024);
|
||||
|
||||
/// <summary>
|
||||
/// Получить диапазон дат, для которых есть данные в репозитории
|
||||
/// </summary>
|
||||
/// <param name="token"></param>
|
||||
/// <returns></returns>
|
||||
Task<DatesRangeDto?> GetDatesRange(CancellationToken token);
|
||||
Task<DatesRangeDto> GetDatesRangeAsync(CancellationToken token);
|
||||
}
|
@ -15,5 +15,5 @@ public interface ITableDataRepository<TDto, TRequest>
|
||||
/// <param name="request">параметры фильтрации</param>
|
||||
/// <param name="token"></param>
|
||||
/// <returns></returns>
|
||||
Task<IEnumerable<TDto>> Get(TRequest request, CancellationToken token);
|
||||
Task<IEnumerable<TDto>> GetAsync(TRequest request, CancellationToken token);
|
||||
}
|
||||
|
@ -6,9 +6,24 @@ namespace Persistence.Repositories;
|
||||
/// Интерфейс по работе с временными данными
|
||||
/// </summary>
|
||||
/// <typeparam name="TDto"></typeparam>
|
||||
public interface ITimeSeriesDataRepository<TDto> : ISyncRepository<TDto>, ITimeSeriesBaseRepository<TDto>
|
||||
public interface ITimeSeriesDataRepository<TDto> : ISyncRepository<TDto>
|
||||
where TDto : class, ITimeSeriesAbstractDto, new()
|
||||
{
|
||||
/// <summary>
|
||||
/// Получить страницу списка объектов
|
||||
/// </summary>
|
||||
/// <param name="dateBegin">дата начала</param>
|
||||
/// <param name="dateEnd">дата окончания</param>
|
||||
/// <param name="token"></param>
|
||||
/// <returns></returns>
|
||||
Task<IEnumerable<TDto>> GetAsync(DateTimeOffset dateBegin, DateTimeOffset dateEnd, CancellationToken token);
|
||||
|
||||
/// <summary>
|
||||
/// Получить диапазон дат, для которых есть данные в репозитории
|
||||
/// </summary>
|
||||
/// <param name="token"></param>
|
||||
/// <returns></returns>
|
||||
Task<DatesRangeDto> GetDatesRangeAsync(CancellationToken token);
|
||||
/// <summary>
|
||||
/// Добавление записей
|
||||
/// </summary>
|
||||
@ -16,4 +31,19 @@ public interface ITimeSeriesDataRepository<TDto> : ISyncRepository<TDto>, ITimeS
|
||||
/// <param name="token"></param>
|
||||
/// <returns></returns>
|
||||
Task<int> InsertRange(IEnumerable<TDto> dtos, CancellationToken token);
|
||||
|
||||
/// <summary>
|
||||
/// Получение списка последних записей
|
||||
/// </summary>
|
||||
/// <param name="takeCount">количество записей</param>
|
||||
/// <param name="token"></param>
|
||||
/// <returns></returns>
|
||||
Task<IEnumerable<TDto>> GetLastAsync(int takeCount, CancellationToken token);
|
||||
|
||||
/// <summary>
|
||||
/// Получение первой записи
|
||||
/// </summary>
|
||||
/// <param name="token"></param>
|
||||
/// <returns></returns>
|
||||
Task<TDto?> GetFirstAsync(CancellationToken token);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user