Compare commits

...

5 Commits

20 changed files with 562 additions and 126 deletions

View File

@ -1,9 +1,11 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Persistence.Repositories;
using Persistence.Repository.Data;
namespace Persistence.API.Controllers;
[ApiController]
[Authorize]
[Route("api/[controller]")]
public class DataSaubController : TimeSeriesController<DataSaubDto>
{

View File

@ -15,36 +15,37 @@ namespace Persistence.API.Controllers
this.setpointRepository = setpointRepository;
}
[HttpPost("current")]
public async Task<ActionResult<IEnumerable<SetpointValueDto>>> GetCurrent(IEnumerable<Guid> setpointKeys, CancellationToken token)
[HttpGet("current")]
public async Task<ActionResult<IEnumerable<SetpointValueDto>>> GetCurrent([FromQuery] IEnumerable<Guid> setpointKeys, CancellationToken token)
{
var result = await setpointRepository.GetCurrent(setpointKeys, token);
return Ok(result);
}
[HttpPost("history")]
public async Task<ActionResult<IEnumerable<SetpointValueDto>>> GetHistory(IEnumerable<Guid> setpointKeys, DateTimeOffset historyMoment, CancellationToken token)
[HttpGet("history")]
public async Task<ActionResult<IEnumerable<SetpointValueDto>>> GetHistory([FromQuery] IEnumerable<Guid> setpointKeys, [FromQuery] DateTimeOffset historyMoment, CancellationToken token)
{
var result = await setpointRepository.GetHistory(setpointKeys, historyMoment, token);
return Ok(result);
}
[HttpPost("log")]
public async Task<ActionResult<Dictionary<Guid, IEnumerable<SetpointLogDto>>>> GetLog([FromBody] IEnumerable<Guid> setpointKeys, CancellationToken token)
[HttpGet("log")]
public async Task<ActionResult<Dictionary<Guid, IEnumerable<SetpointLogDto>>>> GetLog([FromQuery] IEnumerable<Guid> setpointKeys, CancellationToken token)
{
var result = await setpointRepository.GetLog(setpointKeys, token);
return Ok(result);
}
[HttpPost("save")]
[HttpPost]
public async Task<ActionResult<int>> Save(Guid setpointKey, object newValue, CancellationToken token)
{
var result = await setpointRepository.Save(setpointKey, newValue, 0, token);
// ToDo: вычитка idUser
await setpointRepository.Save(setpointKey, newValue, 0, token);
return Ok(result);
return Ok();
}
}
}

View File

@ -1,9 +1,11 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Persistence.Models;
using Persistence.Repositories;
namespace Persistence.API.Controllers;
[ApiController]
[Authorize]
[Route("api/[controller]")]
public class TimeSeriesController<TDto> : ControllerBase, ITimeSeriesDataApi<TDto>
where TDto : class, ITimeSeriesAbstractDto, new()
@ -21,6 +23,7 @@ public class TimeSeriesController<TDto> : ControllerBase, ITimeSeriesDataApi<TDt
{
var result = await this.timeSeriesDataRepository.GetAsync(dateBegin, dateEnd, token);
return Ok(result);
}
[HttpGet("datesRange")]

View File

@ -0,0 +1,88 @@
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.IdentityModel.Tokens;
using Microsoft.OpenApi.Any;
using Microsoft.OpenApi.Models;
using System.Text.Json.Nodes;
namespace Persistence.API;
public static class DependencyInjection
{
public static void AddSwagger(this IServiceCollection services, IConfiguration configuration)
{
services.AddSwaggerGen(c =>
{
c.MapType<TimeSpan>(() => new OpenApiSchema { Type = "string", Example = new OpenApiString("0.00:00:00") });
c.MapType<DateOnly>(() => new OpenApiSchema { Type = "string", Format = "date" });
c.MapType<JsonValue>(() => new OpenApiSchema
{
AnyOf = new OpenApiSchema[]
{
new OpenApiSchema {Type = "string", Format = "string" },
new OpenApiSchema {Type = "number", Format = "int32" },
new OpenApiSchema {Type = "number", Format = "float" },
}
});
c.CustomOperationIds(e =>
{
return $"{e.ActionDescriptor.RouteValues["action"]}";
});
c.SwaggerDoc("v1", new OpenApiInfo { Title = "Persistence web api", Version = "v1" });
c.AddSecurityDefinition("Keycloack", new OpenApiSecurityScheme
{
Description = @"JWT Authorization header using the Bearer scheme. Enter 'Bearer' [space] and then your token in the text input below. Example: 'Bearer 12345abcdef'",
Name = "Authorization",
In = ParameterLocation.Header,
Type = SecuritySchemeType.OAuth2,
Flows = new OpenApiOAuthFlows
{
Implicit = new OpenApiOAuthFlow
{
AuthorizationUrl = new Uri(configuration["Authentication:AuthorizationUrl"]),
}
}
});
c.AddSecurityRequirement(new OpenApiSecurityRequirement()
{
{
new OpenApiSecurityScheme
{
Reference = new OpenApiReference
{
Type = ReferenceType.SecurityScheme,
Id = "Keycloack"
},
Scheme = "Bearer",
Name = "Bearer",
In = ParameterLocation.Header,
},
new List<string>()
}
});
//var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
//var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
//var includeControllerXmlComment = true;
//c.IncludeXmlComments(xmlPath, includeControllerXmlComment);
//c.IncludeXmlComments(Path.Combine(AppContext.BaseDirectory, "AsbCloudApp.xml"), includeControllerXmlComment);
});
}
public static void AddJWTAuthentication(this IServiceCollection services, IConfiguration configuration)
{
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(o =>
{
o.RequireHttpsMetadata = false;
o.Audience = configuration["Authentication:Audience"];
o.MetadataAddress = configuration["Authentication:MetadataAddress"]!;
o.TokenValidationParameters = new TokenValidationParameters
{
ValidIssuer = configuration["Authentication:ValidIssuer"],
};
});
}
}

View File

@ -0,0 +1,26 @@
using System.ComponentModel;
using System.Security.Claims;
namespace Persistence.API;
public static class Extensions
{
public static T GetUserId<T>(this ClaimsPrincipal principal)
{
if (principal == null)
throw new ArgumentNullException(nameof(principal));
var loggedInUserId = principal.FindFirstValue(ClaimTypes.NameIdentifier);
if (String.IsNullOrEmpty(loggedInUserId))
throw new ArgumentNullException(nameof(loggedInUserId));
var result = TypeDescriptor.GetConverter(typeof(T)).ConvertFromInvariantString(loggedInUserId);
if (result is null)
throw new ArgumentNullException(nameof(result));
return (T)result;
}
}

View File

@ -8,6 +8,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.10" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.19.6" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
</ItemGroup>

View File

@ -19,9 +19,10 @@ public class Startup
services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
services.AddEndpointsApiExplorer();
services.AddSwaggerGen();
services.AddPersistenceDbContext(Configuration);
services.AddSwagger(Configuration);
services.AddInfrastructure();
services.AddPersistenceDbContext(Configuration);
services.AddJWTAuthentication(Configuration);
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
@ -32,7 +33,8 @@ public class Startup
app.UseRouting();
//app.UseAuthorization();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{

View File

@ -6,7 +6,13 @@
}
},
"ConnectionStrings": {
"DefaultConnection": "Host=localhost;Database=persistence;Username=postgres;Password=q;Persist Security Info=True",
"DefaultConnection": "Host=localhost;Database=persistence;Username=postgres;Password=q;Persist Security Info=True"
},
"AllowedHosts": "*"
"AllowedHosts": "*",
"Authentication": {
"MetadataAddress": "http://localhost:8080/realms/TestRealm/.well-known/openid-configuration",
"Audience": "account",
"ValidIssuer": "http://localhost:8080/realms/TestRealm",
"AuthorizationUrl": "http://localhost:8080/realms/TestRealm/protocol/openid-connect/auth"
}
}

View File

@ -1,6 +1,5 @@
using System.ComponentModel.DataAnnotations.Schema;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
namespace Persistence.Database.Model
{
@ -13,7 +12,7 @@ namespace Persistence.Database.Model
[Column(TypeName = "jsonb"), Comment("Значение уставки")]
public required object Value { get; set; }
[Comment("Дата изменения уставки")]
[Comment("Дата создания уставки")]
public DateTimeOffset Created { get; set; }
[Comment("Id автора последнего изменения")]

View File

@ -1,18 +0,0 @@
using System.ComponentModel.DataAnnotations;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
namespace Persistence.Database.Model
{
public class SetpointDictionary
{
[Key, Comment("Ключ")]
public Guid Key { get; set; }
[Comment("Наименование")]
public required string Name { get; set; }
[Comment("Описание")]
public string? Description { get; set; }
}
}

View File

@ -3,18 +3,23 @@ using Refit;
namespace Persistence.IntegrationTests.Clients
{
/// <summary>
/// Интерфейс для тестирования API, предназначенного для работы с уставками
/// </summary>
public interface ISetpointClient
{
[Post("/current")]
Task<IApiResponse<IEnumerable<SetpointValueDto>>> GetCurrent(IEnumerable<Guid> setpointKeys);
private const string BaseRoute = "/api/setpoint";
[Get($"{BaseRoute}/current")]
Task<IApiResponse<IEnumerable<SetpointValueDto>>> GetCurrent([Query(CollectionFormat.Multi)] IEnumerable<Guid> setpointKeys);
[Post("/history")]
Task<IApiResponse<IEnumerable<SetpointValueDto>>> GetHistory(IEnumerable<Guid> setpointKeys, DateTimeOffset historyMoment);
[Get($"{BaseRoute}/history")]
Task<IApiResponse<IEnumerable<SetpointValueDto>>> GetHistory([Query(CollectionFormat.Multi)] IEnumerable<Guid> setpointKeys, [Query] DateTimeOffset historyMoment);
[Post("/log")]
Task<IApiResponse<Dictionary<Guid, IEnumerable<SetpointLogDto>>>> GetLog(IEnumerable<Guid> setpoitKeys);
[Get($"{BaseRoute}/log")]
Task<IApiResponse<Dictionary<Guid, IEnumerable<SetpointLogDto>>>> GetLog([Query(CollectionFormat.Multi)] IEnumerable<Guid> setpointKeys);
[Post("/save")]
Task<IApiResponse<int>> Save(Guid setpointKey, object newValue);
[Post($"{BaseRoute}/")]
Task<IApiResponse> Save(Guid setpointKey, object newValue);
}
}

View File

@ -1,6 +1,4 @@
using System.Net;
using System.Text.Json;
using Mapster;
using Persistence.IntegrationTests.Clients;
using Xunit;
@ -16,8 +14,6 @@ namespace Persistence.IntegrationTests.Controllers
}
public SetpointControllerTest(WebAppFactoryFixture factory) : base(factory)
{
factory.ClientOptions.BaseAddress = new Uri($"http://localhost/api/Setpoint");
client = factory.GetHttpClient<ISetpointClient>(string.Empty);
}
@ -40,6 +36,22 @@ namespace Persistence.IntegrationTests.Controllers
Assert.Empty(response.Content);
}
[Fact]
public async Task GetCurrent_AfterSave_returns_success()
{
//arrange
var setpointKey = await Save();
//act
var response = await client.GetCurrent([setpointKey]);
//assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.NotNull(response.Content);
Assert.NotEmpty(response.Content);
Assert.Equal(response.Content.FirstOrDefault()?.Key, setpointKey);
}
[Fact]
public async Task GetHistory_returns_success()
{
@ -49,7 +61,7 @@ namespace Persistence.IntegrationTests.Controllers
Guid.NewGuid(),
Guid.NewGuid()
};
var historyMoment = DateTimeOffset.Now.ToUniversalTime();
var historyMoment = DateTimeOffset.UtcNow;
//act
var response = await client.GetHistory(setpointKeys, historyMoment);
@ -60,6 +72,24 @@ namespace Persistence.IntegrationTests.Controllers
Assert.Empty(response.Content);
}
[Fact]
public async Task GetHistory_AfterSave_returns_success()
{
//arrange
var setpointKey = await Save();
var historyMoment = DateTimeOffset.UtcNow;
historyMoment = historyMoment.AddDays(1);
//act
var response = await client.GetHistory([setpointKey], historyMoment);
//assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.NotNull(response.Content);
Assert.NotEmpty(response.Content);
Assert.Equal(response.Content.FirstOrDefault()?.Key, setpointKey);
}
[Fact]
public async Task GetLog_returns_success()
{
@ -79,8 +109,29 @@ namespace Persistence.IntegrationTests.Controllers
Assert.Empty(response.Content);
}
[Fact]
public async Task GetLog_AfterSave_returns_success()
{
//arrange
var setpointKey = await Save();
//act
var response = await client.GetLog([setpointKey]);
//assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.NotNull(response.Content);
Assert.NotEmpty(response.Content);
Assert.Equal(response.Content.FirstOrDefault().Value.FirstOrDefault()?.Key, setpointKey);
}
[Fact]
public async Task Save_returns_success()
{
await Save();
}
private async Task<Guid> Save()
{
//arrange
var setpointKey = Guid.NewGuid();
@ -95,53 +146,8 @@ namespace Persistence.IntegrationTests.Controllers
//assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.Equal(1, response.Content);
}
[Fact]
public async Task General_test_success()
{
//save
var setpointKey = Guid.NewGuid();
var setpointValue = new TestObject()
{
value1 = "1",
value2 = 2
};
var saveResponse = await client.Save(setpointKey, setpointValue);
Assert.Equal(HttpStatusCode.OK, saveResponse.StatusCode);
Assert.Equal(1, saveResponse.Content);
//current
var currentResponse = await client.GetCurrent([setpointKey]);
Assert.Equal(HttpStatusCode.OK, currentResponse.StatusCode);
var currentContent = currentResponse.Content;
Assert.NotNull(currentContent);
Assert.NotEmpty(currentContent);
var currentContentValue = currentContent.FirstOrDefault()?.Value?.ToString();
Assert.NotNull(currentContentValue);
Assert.NotEmpty(currentContentValue);
var testObjectValue = JsonSerializer.Deserialize<TestObject>(currentContentValue);
Assert.NotNull(testObjectValue);
Assert.Equal(setpointValue.value1, testObjectValue.value1);
Assert.Equal(setpointValue.value2, testObjectValue.value2);
//history
var historyMoment = DateTimeOffset.Now.ToUniversalTime();
var historyResponse = await client.GetHistory([setpointKey], historyMoment);
Assert.Equal(HttpStatusCode.OK, historyResponse.StatusCode);
Assert.NotNull(historyResponse.Content);
Assert.NotEmpty(historyResponse.Content);
//log
var logResponse = await client.GetLog([setpointKey]);
Assert.Equal(HttpStatusCode.OK, logResponse.StatusCode);
Assert.NotNull(logResponse.Content);
Assert.NotEmpty(logResponse.Content);
return setpointKey;
}
}
}

View File

@ -0,0 +1,199 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Persistence.Repository;
/// <summary>
/// Цикличный массив
/// </summary>
/// <typeparam name="T"></typeparam>
public class CyclicArray<T> : IEnumerable<T>
{
readonly T[] array;
int used, current = -1;
/// <summary>
/// constructor
/// </summary>
/// <param name="capacity"></param>
public CyclicArray(int capacity)
{
array = new T[capacity];
}
/// <summary>
/// Количество элементов в массиве
/// </summary>
public int Count => used;
/// <summary>
/// Добавить новый элемент<br/>
/// Если capacity достигнуто, то вытеснит самый первый элемент
/// </summary>
/// <param name="item"></param>
public void Add(T item)
{
current = (++current) % array.Length;
array[current] = item;
if (used < array.Length)
used++;
UpdatedInvoke(current, item);
}
/// <summary>
/// Добавить новые элементы.<br/>
/// Если capacity достигнуто, то вытеснит самые первые элементы.<br/>
/// Не вызывает Updated!
/// </summary>
/// <param name="items"></param>
public void AddRange(IEnumerable<T> items)
{
var capacity = array.Length;
var newItems = items.TakeLast(capacity).ToArray();
if (newItems.Length == capacity)
{
Array.Copy(newItems, array, capacity);
current = capacity - 1;
}
else
{
current = (++current) % capacity;
var countToEndOfArray = capacity - current;
if (newItems.Length <= countToEndOfArray)
{
Array.Copy(newItems, 0, array, current, newItems.Length);
current += newItems.Length - 1;
}
else
{
var firstStepLength = countToEndOfArray;
Array.Copy(newItems, 0, array, current, firstStepLength);
var secondStepCount = newItems.Length - firstStepLength;
Array.Copy(newItems, firstStepLength, array, 0, secondStepCount);
current = secondStepCount - 1;
}
}
if (used < capacity)
{
used += newItems.Length;
used = used > capacity ? capacity : used;
}
}
/// <summary>
/// Индекс
/// </summary>
/// <param name="index"></param>
/// <returns></returns>
public T this[int index]
{
get
{
if (used == 0)
throw new IndexOutOfRangeException();
var i = (current + 1 + index) % used;
return array[i];
}
set
{
var devider = used > 0 ? used : array.Length;
var i = (current + 1 + index) % devider;
array[i] = value;
UpdatedInvoke(current, value);
}
}
/// <summary>
/// событие на изменение элемента в массиве
/// </summary>
public event EventHandler<(int index, T value)>? Updated;
private void UpdatedInvoke(int index, T value)
{
Updated?.Invoke(this, (index, value));
}
/// <summary>
/// Агрегирование значения по всему массиву
/// </summary>
/// <typeparam name="Tout"></typeparam>
/// <param name="func"></param>
/// <param name="startValue"></param>
/// <returns></returns>
public Tout Aggregate<Tout>(Func<T, Tout, Tout> func, Tout startValue)
{
Tout result = startValue;
for (int i = 0; i < used; i++)
result = func(this[i], result);
return result;
}
/// <inheritdoc/>
public IEnumerator<T> GetEnumerator()
=> new CyclycListEnumerator<T>(array, current, used);
/// <inheritdoc/>
IEnumerator IEnumerable.GetEnumerator()
=> GetEnumerator();
class CyclycListEnumerator<Te> : IEnumerator<Te>
{
private readonly Te[] array;
private readonly int used;
private readonly int first;
private int current = -1;
public CyclycListEnumerator(Te[] array, int first, int used)
{
this.array = new Te[array.Length];
array.CopyTo(this.array, 0);
this.used = used;
this.first = first;
}
public Te Current
{
get
{
if (IsCurrentOk())
{
var i = (current + first + 1) % used;
return array[i];
}
else
return default!;
}
}
object? IEnumerator.Current => Current;
public void Dispose() {; }
private bool IsCurrentOk() => current >= 0 && current < used;
public bool MoveNext()
{
if (current < used)
current++;
return IsCurrentOk();
}
public void Reset()
{
current = -1;
}
}
/// <summary>
/// Очистить весь массив
/// </summary>
public void Clear()
{
used = 0;
current = -1;
}
}

View File

@ -1,10 +1,28 @@
namespace Persistence.Repository.Data
{
/// <summary>
/// Модель для работы с уставкой
/// </summary>
public class SetpointDto
{
/// <summary>
/// Идентификатор уставки
/// </summary>
public int Id { get; set; }
/// <summary>
/// Значение уставки
/// </summary>
public required object Value { get; set; }
/// <summary>
/// Дата сохранения уставки
/// </summary>
public DateTimeOffset Edit { get; set; }
/// <summary>
/// Ключ пользователя
/// </summary>
public int IdUser { get; set; }
}
}

View File

@ -1,11 +1,8 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Persistence.Repositories;
using Persistence.Database.Model;
using Persistence.Repositories;
using Persistence.Repository.Data;
using Persistence.Repository.Repositories;
using Persistence.Database;
namespace Persistence.Repository;
public static class DependencyInjection

View File

@ -31,9 +31,13 @@ namespace Persistence.Repository.Repositories
{
var query = GetQueryReadOnly();
var entities = await query
.Where(e => setpointKeys.Contains(e.Key) && e.Created.Date == historyMoment.Date)
.Where(e => setpointKeys.Contains(e.Key))
.ToArrayAsync(token);
var dtos = entities.Select(e => e.Adapt<SetpointValueDto>());
var filteredEntities = entities
.GroupBy(e => e.Key)
.Select(e => e.Where(e => e.Created <= historyMoment).Last());
var dtos = filteredEntities
.Select(e => e.Adapt<SetpointValueDto>());
return dtos;
}
@ -46,35 +50,23 @@ namespace Persistence.Repository.Repositories
.ToArrayAsync(token);
var dtos = entities
.GroupBy(e => e.Key)
.Select(e => new KeyValuePair<Guid, IEnumerable<SetpointLogDto>>(
e.Key,
e.Select(s => s.Adapt<SetpointLogDto>())
)).ToDictionary();
.ToDictionary(e => e.Key, v => v.Select(z => z.Adapt<SetpointLogDto>()));
return dtos;
}
public async Task<int> Save(Guid setpointKey, object newValue, int idUser, CancellationToken token)
public async Task Save(Guid setpointKey, object newValue, int idUser, CancellationToken token)
{
try
var entity = new Setpoint()
{
var entity = new Setpoint()
{
Key = setpointKey,
Value = newValue,
IdUser = idUser,
Created = DateTimeOffset.Now.ToUniversalTime()
};
Key = setpointKey,
Value = newValue,
IdUser = idUser,
Created = DateTimeOffset.UtcNow
};
await db.Set<Setpoint>().AddAsync(entity, token);
var result = await db.SaveChangesAsync(token);
return result;
}
catch (Exception)
{
return 0;
}
await db.Set<Setpoint>().AddAsync(entity, token);
await db.SaveChangesAsync(token);
}
}
}

View File

@ -0,0 +1,68 @@
using Mapster;
using Microsoft.EntityFrameworkCore;
using Newtonsoft.Json.Linq;
using Persistence.Database.Model;
using Persistence.Models;
namespace Persistence.Repository.Repositories;
public class TimeSeriesDataCachedRepository<TEntity, TDto> : TimeSeriesDataRepository<TEntity, TDto>
where TEntity : class, ITimestampedData, new()
where TDto : class, ITimeSeriesAbstractDto, new()
{
public static TDto FirstByDate { get; set; } = default!;
public static CyclicArray<TDto> LastData { get; set; } = null!;
private const int CacheItemsCount = 3600;
public TimeSeriesDataCachedRepository(DbContext db) : base(db)
{
Task.Run(async () =>
{
LastData = new CyclicArray<TDto>(CacheItemsCount);
var firstDateItem = await base.GetFirstAsync(CancellationToken.None);
if (firstDateItem == null)
{
return;
}
FirstByDate = firstDateItem;
var dtos = await base.GetLastAsync(CacheItemsCount, CancellationToken.None);
dtos = dtos.OrderBy(d => d.Date);
LastData.AddRange(dtos);
}).Wait();
}
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.GetAsync(dateBegin, dateEnd, token);
return dtos;
}
var items = LastData
.Where(i => i.Date >= dateBegin && i.Date <= dateEnd);
return items;
}
public override async Task<int> InsertRange(IEnumerable<TDto> dtos, CancellationToken token)
{
var result = await base.InsertRange(dtos, token);
if (result > 0)
{
dtos = dtos.OrderBy(x => x.Date);
FirstByDate = dtos.First();
LastData.AddRange(dtos);
}
return result;
}
}

View File

@ -18,7 +18,7 @@ public class TimeSeriesDataRepository<TEntity, TDto> : ITimeSeriesDataRepository
protected virtual IQueryable<TEntity> GetQueryReadOnly() => this.db.Set<TEntity>();
public async Task<IEnumerable<TDto>> GetAsync(DateTimeOffset dateBegin, DateTimeOffset dateEnd, CancellationToken token)
public virtual async Task<IEnumerable<TDto>> GetAsync(DateTimeOffset dateBegin, DateTimeOffset dateEnd, CancellationToken token)
{
var query = GetQueryReadOnly();
var entities = await query.ToArrayAsync(token);
@ -27,7 +27,7 @@ public class TimeSeriesDataRepository<TEntity, TDto> : ITimeSeriesDataRepository
return dtos;
}
public async Task<DatesRangeDto> GetDatesRangeAsync(CancellationToken token)
public virtual async Task<DatesRangeDto> GetDatesRangeAsync(CancellationToken token)
{
var query = GetQueryReadOnly();
var minDate = await query.MinAsync(o => o.Date, token);
@ -40,7 +40,7 @@ public class TimeSeriesDataRepository<TEntity, TDto> : ITimeSeriesDataRepository
};
}
public async Task<IEnumerable<TDto>> GetGtDate(DateTimeOffset date, CancellationToken token)
public virtual async Task<IEnumerable<TDto>> GetGtDate(DateTimeOffset date, CancellationToken token)
{
var query = this.db.Set<TEntity>().Where(e => e.Date > date);
var entities = await query.ToArrayAsync(token);
@ -50,7 +50,7 @@ public class TimeSeriesDataRepository<TEntity, TDto> : ITimeSeriesDataRepository
return dtos;
}
public async Task<int> InsertRange(IEnumerable<TDto> dtos, CancellationToken token)
public virtual async Task<int> InsertRange(IEnumerable<TDto> dtos, CancellationToken token)
{
var entities = dtos.Select(d => d.Adapt<TEntity>());
@ -59,4 +59,30 @@ public class TimeSeriesDataRepository<TEntity, TDto> : ITimeSeriesDataRepository
return result;
}
public async Task<IEnumerable<TDto>> GetLastAsync(int takeCount, CancellationToken token)
{
var query = GetQueryReadOnly()
.OrderByDescending(e => e.Date)
.Take(takeCount);
var entities = await query.ToArrayAsync(token);
var dtos = entities.Select(e => e.Adapt<TDto>());
return dtos;
}
public async Task<TDto?> GetFirstAsync(CancellationToken token)
{
var query = GetQueryReadOnly()
.OrderBy(e => e.Date);
var entity = await query.FirstOrDefaultAsync(token);
if(entity == null)
return null;
var dto = entity.Adapt<TDto>();
return dto;
}
}

View File

@ -42,5 +42,5 @@ public interface ISetpointRepository
/// <returns></returns>
/// to do
/// id User учесть в соответствующем методе репозитория
Task<int> Save(Guid setpointKey, object newValue, int idUser, CancellationToken token);
Task Save(Guid setpointKey, object newValue, int idUser, CancellationToken token);
}

View File

@ -31,4 +31,19 @@ public interface ITimeSeriesDataRepository<TDto> : ISyncRepository<TDto>
/// <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);
}