diff --git a/Persistence.API/Controllers/TechMessagesController.cs b/Persistence.API/Controllers/TechMessagesController.cs index 12b23b3..5945208 100644 --- a/Persistence.API/Controllers/TechMessagesController.cs +++ b/Persistence.API/Controllers/TechMessagesController.cs @@ -82,7 +82,7 @@ public class TechMessagesController : ControllerBase { var result = await techMessagesRepository.GetDatesRangeAsync(token); - return Ok(result); + return result == null ? NoContent() : Ok(result); } /// diff --git a/Persistence.Client/Clients/ChangeLogClient.cs b/Persistence.Client/Clients/ChangeLogClient.cs index 00aee71..bd3e49e 100644 --- a/Persistence.Client/Clients/ChangeLogClient.cs +++ b/Persistence.Client/Clients/ChangeLogClient.cs @@ -16,7 +16,7 @@ public class ChangeLogClient : BaseClient, IChangeLogClient public async Task ClearAndAddRange(Guid idDiscriminator, IEnumerable dtos, CancellationToken token) { - var result = await ExecuteGetResponse( + var result = await ExecuteGetResponse( async () => await refitChangeLogClient.ClearAndAddRange(idDiscriminator, dtos, token), token); return result; @@ -25,7 +25,7 @@ public class ChangeLogClient : BaseClient, IChangeLogClient public async Task> GetByDate(Guid idDiscriminator, DateTimeOffset moment, SectionPartRequest filterRequest, PaginationRequest paginationRequest, CancellationToken token) { - var result = await ExecuteGetResponse>( + var result = await ExecuteGetResponse( async () => await refitChangeLogClient.GetByDate(idDiscriminator, moment, filterRequest, paginationRequest, token), token); return result; @@ -33,7 +33,7 @@ public class ChangeLogClient : BaseClient, IChangeLogClient public async Task> GetChangeLogForInterval(Guid idDiscriminator, DateTimeOffset dateBegin, DateTimeOffset dateEnd, CancellationToken token) { - var result = await ExecuteGetResponse>( + var result = await ExecuteGetResponse( async () => await refitChangeLogClient.GetChangeLogForInterval(idDiscriminator, dateBegin, dateEnd, token), token); return result; @@ -89,7 +89,7 @@ public class ChangeLogClient : BaseClient, IChangeLogClient public async Task GetDatesRange(Guid idDiscriminator, CancellationToken token) { - var result = await ExecuteGetResponse( + var result = await ExecuteGetResponse( async () => await refitChangeLogClient.GetDatesRange(idDiscriminator, token), token); return result; @@ -98,5 +98,7 @@ public class ChangeLogClient : BaseClient, IChangeLogClient public void Dispose() { refitChangeLogClient.Dispose(); - } + + GC.SuppressFinalize(this); + } } diff --git a/Persistence.Client/Clients/DataSourceSystemClient.cs b/Persistence.Client/Clients/DataSourceSystemClient.cs index fbec180..f68f0c3 100644 --- a/Persistence.Client/Clients/DataSourceSystemClient.cs +++ b/Persistence.Client/Clients/DataSourceSystemClient.cs @@ -22,7 +22,7 @@ public class DataSourceSystemClient : BaseClient, IDataSourceSystemClient public async Task> Get(CancellationToken token) { - var result = await ExecuteGetResponse>( + var result = await ExecuteGetResponse( async () => await dataSourceSystemClient.Get(token), token); return result; @@ -31,5 +31,7 @@ public class DataSourceSystemClient : BaseClient, IDataSourceSystemClient public void Dispose() { dataSourceSystemClient.Dispose(); - } + + GC.SuppressFinalize(this); + } } diff --git a/Persistence.Client/Clients/Interfaces/ITechMessagesClient.cs b/Persistence.Client/Clients/Interfaces/ITechMessagesClient.cs index 3cd0d92..2d6abe9 100644 --- a/Persistence.Client/Clients/Interfaces/ITechMessagesClient.cs +++ b/Persistence.Client/Clients/Interfaces/ITechMessagesClient.cs @@ -44,7 +44,7 @@ public interface ITechMessagesClient : IDisposable /// Получить статистику по системам /// /// - /// + /// /// /// Task> GetStatistics(IEnumerable systemIds, IEnumerable categoryIds, CancellationToken token); diff --git a/Persistence.Client/Clients/Interfaces/Refit/IRefitTechMessagesClient.cs b/Persistence.Client/Clients/Interfaces/Refit/IRefitTechMessagesClient.cs index 6937ff7..b1b90b2 100644 --- a/Persistence.Client/Clients/Interfaces/Refit/IRefitTechMessagesClient.cs +++ b/Persistence.Client/Clients/Interfaces/Refit/IRefitTechMessagesClient.cs @@ -25,6 +25,6 @@ namespace Persistence.Client.Clients.Interfaces.Refit Task>> GetPart(DateTimeOffset dateBegin, int take, CancellationToken token); [Get($"{BaseRoute}/statistics")] - Task>> GetStatistics([Query] IEnumerable systemIds, [Query] IEnumerable categoryIds, CancellationToken token); + Task>> GetStatistics([Query(CollectionFormat.Multi)] IEnumerable systemIds, [Query(CollectionFormat.Multi)] IEnumerable categoryIds, CancellationToken token); } } diff --git a/Persistence.Client/Clients/SetpointClient.cs b/Persistence.Client/Clients/SetpointClient.cs index b1df4b4..3dba7a0 100644 --- a/Persistence.Client/Clients/SetpointClient.cs +++ b/Persistence.Client/Clients/SetpointClient.cs @@ -17,7 +17,7 @@ public class SetpointClient : BaseClient, ISetpointClient public async Task> GetCurrent(IEnumerable setpointKeys, CancellationToken token) { - var result = await ExecuteGetResponse>( + var result = await ExecuteGetResponse( async () => await refitSetpointClient.GetCurrent(setpointKeys, token), token); return result; @@ -25,7 +25,7 @@ public class SetpointClient : BaseClient, ISetpointClient public async Task> GetHistory(IEnumerable setpointKeys, DateTimeOffset historyMoment, CancellationToken token) { - var result = await ExecuteGetResponse>( + var result = await ExecuteGetResponse( async () => await refitSetpointClient.GetHistory(setpointKeys, historyMoment, token), token); return result; @@ -33,7 +33,7 @@ public class SetpointClient : BaseClient, ISetpointClient public async Task>> GetLog(IEnumerable setpointKeys, CancellationToken token) { - var result = await ExecuteGetResponse>>( + var result = await ExecuteGetResponse( async () => await refitSetpointClient.GetLog(setpointKeys, token), token); return result; @@ -41,7 +41,7 @@ public class SetpointClient : BaseClient, ISetpointClient public async Task GetDatesRangeAsync(CancellationToken token) { - var result = await ExecuteGetResponse( + var result = await ExecuteGetResponse( async () => await refitSetpointClient.GetDatesRangeAsync(token), token); return result; @@ -49,7 +49,7 @@ public class SetpointClient : BaseClient, ISetpointClient public async Task> GetPart(DateTimeOffset dateBegin, int take, CancellationToken token) { - var result = await ExecuteGetResponse>( + var result = await ExecuteGetResponse( async () => await refitSetpointClient.GetPart(dateBegin, take, token), token); return result; @@ -64,5 +64,7 @@ public class SetpointClient : BaseClient, ISetpointClient public void Dispose() { refitSetpointClient.Dispose(); - } + + GC.SuppressFinalize(this); + } } diff --git a/Persistence.Client/Clients/TechMessagesClient.cs b/Persistence.Client/Clients/TechMessagesClient.cs index d097451..5c90359 100644 --- a/Persistence.Client/Clients/TechMessagesClient.cs +++ b/Persistence.Client/Clients/TechMessagesClient.cs @@ -18,7 +18,7 @@ public class TechMessagesClient : BaseClient, ITechMessagesClient public async Task> GetPage(PaginationRequest request, CancellationToken token) { - var result = await ExecuteGetResponse>( + var result = await ExecuteGetResponse( async () => await refitTechMessagesClient.GetPage(request, token), token); return result; @@ -34,7 +34,7 @@ public class TechMessagesClient : BaseClient, ITechMessagesClient public async Task> GetSystems(CancellationToken token) { - var result = await ExecuteGetResponse>( + var result = await ExecuteGetResponse( async () => await refitTechMessagesClient.GetSystems(token), token); return result; @@ -42,7 +42,7 @@ public class TechMessagesClient : BaseClient, ITechMessagesClient public async Task GetDatesRangeAsync(CancellationToken token) { - var result = await ExecuteGetResponse( + var result = await ExecuteGetResponse( async () => await refitTechMessagesClient.GetDatesRangeAsync(token), token); return result; @@ -50,7 +50,7 @@ public class TechMessagesClient : BaseClient, ITechMessagesClient public async Task> GetPart(DateTimeOffset dateBegin, int take, CancellationToken token) { - var result = await ExecuteGetResponse>( + var result = await ExecuteGetResponse( async () => await refitTechMessagesClient.GetPart(dateBegin, take, token), token); return result; @@ -58,7 +58,7 @@ public class TechMessagesClient : BaseClient, ITechMessagesClient public async Task> GetStatistics(IEnumerable systemIds, IEnumerable categoryIds, CancellationToken token) { - var result = await ExecuteGetResponse>( + var result = await ExecuteGetResponse( async () => await refitTechMessagesClient.GetStatistics(systemIds, categoryIds, token), token); return result; @@ -67,5 +67,7 @@ public class TechMessagesClient : BaseClient, ITechMessagesClient public void Dispose() { refitTechMessagesClient.Dispose(); - } + + GC.SuppressFinalize(this); + } } diff --git a/Persistence.Client/Clients/TimeSeriesClient.cs b/Persistence.Client/Clients/TimeSeriesClient.cs index a3fba7d..416ae84 100644 --- a/Persistence.Client/Clients/TimeSeriesClient.cs +++ b/Persistence.Client/Clients/TimeSeriesClient.cs @@ -24,7 +24,7 @@ public class TimeSeriesClient : BaseClient, ITimeSeriesClient where public async Task> Get(DateTimeOffset dateBegin, DateTimeOffset dateEnd, CancellationToken token) { - var result = await ExecuteGetResponse>( + var result = await ExecuteGetResponse( async () => await timeSeriesClient.Get(dateBegin, dateEnd, token), token); return result; @@ -32,7 +32,7 @@ public class TimeSeriesClient : BaseClient, ITimeSeriesClient where public async Task> GetResampledData(DateTimeOffset dateBegin, double intervalSec = 600d, int approxPointsCount = 1024, CancellationToken token = default) { - var result = await ExecuteGetResponse>( + var result = await ExecuteGetResponse( async () => await timeSeriesClient.GetResampledData(dateBegin, intervalSec, approxPointsCount, token), token); return result; @@ -40,7 +40,7 @@ public class TimeSeriesClient : BaseClient, ITimeSeriesClient where public async Task GetDatesRange(CancellationToken token) { - var result = await ExecuteGetResponse( + var result = await ExecuteGetResponse( async () => await timeSeriesClient.GetDatesRange(token), token); return result; @@ -49,5 +49,7 @@ public class TimeSeriesClient : BaseClient, ITimeSeriesClient where public void Dispose() { timeSeriesClient.Dispose(); - } + + GC.SuppressFinalize(this); + } } diff --git a/Persistence.Client/Clients/TimestampedSetClient.cs b/Persistence.Client/Clients/TimestampedSetClient.cs index 4d40d8b..e82abee 100644 --- a/Persistence.Client/Clients/TimestampedSetClient.cs +++ b/Persistence.Client/Clients/TimestampedSetClient.cs @@ -24,7 +24,7 @@ public class TimestampedSetClient : BaseClient, ITimestampedSetClient public async Task> Get(Guid idDiscriminator, DateTimeOffset? geTimestamp, IEnumerable? columnNames, int skip, int take, CancellationToken token) { - var result = await ExecuteGetResponse>( + var result = await ExecuteGetResponse( async () => await refitTimestampedSetClient.Get(idDiscriminator, geTimestamp, columnNames, skip, take, token), token); return result; @@ -32,7 +32,7 @@ public class TimestampedSetClient : BaseClient, ITimestampedSetClient public async Task> GetLast(Guid idDiscriminator, IEnumerable? columnNames, int take, CancellationToken token) { - var result = await ExecuteGetResponse>( + var result = await ExecuteGetResponse( async () => await refitTimestampedSetClient.GetLast(idDiscriminator, columnNames, take, token), token); return result; @@ -40,7 +40,7 @@ public class TimestampedSetClient : BaseClient, ITimestampedSetClient public async Task Count(Guid idDiscriminator, CancellationToken token) { - var result = await ExecuteGetResponse( + var result = await ExecuteGetResponse( async () => await refitTimestampedSetClient.Count(idDiscriminator, token), token); return result; @@ -48,7 +48,7 @@ public class TimestampedSetClient : BaseClient, ITimestampedSetClient public async Task GetDatesRange(Guid idDiscriminator, CancellationToken token) { - var result = await ExecuteGetResponse( + var result = await ExecuteGetResponse( async () => await refitTimestampedSetClient.GetDatesRange(idDiscriminator, token), token); return result; @@ -57,5 +57,7 @@ public class TimestampedSetClient : BaseClient, ITimestampedSetClient public void Dispose() { refitTimestampedSetClient.Dispose(); - } + + GC.SuppressFinalize(this); + } } diff --git a/Persistence.Client/Clients/WitsDataClient.cs b/Persistence.Client/Clients/WitsDataClient.cs index ad58be1..acd8bbf 100644 --- a/Persistence.Client/Clients/WitsDataClient.cs +++ b/Persistence.Client/Clients/WitsDataClient.cs @@ -24,7 +24,7 @@ public class WitsDataClient : BaseClient, IWitsDataClient public async Task GetDatesRangeAsync(Guid discriminatorId, CancellationToken token) { - var result = await ExecuteGetResponse( + var result = await ExecuteGetResponse( async () => await refitWitsDataClient.GetDatesRangeAsync(discriminatorId, token), token); return result; @@ -32,7 +32,7 @@ public class WitsDataClient : BaseClient, IWitsDataClient public async Task> GetPart(Guid discriminatorId, DateTimeOffset dateBegin, int take = 86400, CancellationToken token = default) { - var result = await ExecuteGetResponse>( + var result = await ExecuteGetResponse( async () => await refitWitsDataClient.GetPart(discriminatorId, dateBegin, take, token), token); return result; @@ -40,7 +40,7 @@ public class WitsDataClient : BaseClient, IWitsDataClient public async Task> GetValuesForGraph(Guid discriminatorId, DateTimeOffset dateFrom, DateTimeOffset dateTo, int approxPointsCount, CancellationToken token) { - var result = await ExecuteGetResponse>( + var result = await ExecuteGetResponse( async () => await refitWitsDataClient.GetValuesForGraph(discriminatorId, dateFrom, dateTo, approxPointsCount, token), token); return result; @@ -49,5 +49,7 @@ public class WitsDataClient : BaseClient, IWitsDataClient public void Dispose() { refitWitsDataClient.Dispose(); - } + + GC.SuppressFinalize(this); + } } diff --git a/Persistence.IntegrationTests/Controllers/TechMessagesControllerTest.cs b/Persistence.IntegrationTests/Controllers/TechMessagesControllerTest.cs index 242615a..b01f9e9 100644 --- a/Persistence.IntegrationTests/Controllers/TechMessagesControllerTest.cs +++ b/Persistence.IntegrationTests/Controllers/TechMessagesControllerTest.cs @@ -99,7 +99,7 @@ namespace Persistence.IntegrationTests.Controllers try { //act - var response = await techMessagesClient.AddRange(systemId, dtos, new CancellationToken()); + var response = await techMessagesClient.AddRange(systemId, dtos, CancellationToken.None); } catch (Exception ex) { @@ -114,7 +114,7 @@ namespace Persistence.IntegrationTests.Controllers //arrange memoryCache.Remove(SystemCacheKey); dbContext.CleanupDbSet(); - dbContext.CleanupDbSet(); + dbContext.CleanupDbSet(); //act var response = await techMessagesClient.GetSystems(CancellationToken.None); @@ -128,7 +128,7 @@ namespace Persistence.IntegrationTests.Controllers public async Task GetSystems_AfterSave_returns_success() { //arrange - var dtos = await InsertRange(Guid.NewGuid()); + await InsertRange(Guid.NewGuid()); //act var response = await techMessagesClient.GetSystems(CancellationToken.None); @@ -145,13 +145,13 @@ namespace Persistence.IntegrationTests.Controllers //arrange memoryCache.Remove(SystemCacheKey); dbContext.CleanupDbSet(); - dbContext.CleanupDbSet(); + dbContext.CleanupDbSet(); - var imortantIds = new [] { 1 }; + var categoryIds = new [] { 1, 2 }; var systemIds = new [] { Guid.NewGuid() }; //act - var response = await techMessagesClient.GetStatistics(systemIds, imortantIds, CancellationToken.None); + var response = await techMessagesClient.GetStatistics(systemIds, categoryIds, CancellationToken.None); //assert Assert.NotNull(response); diff --git a/Persistence.Repository/Repositories/DataSourceSystemCachedRepository.cs b/Persistence.Repository/Repositories/DataSourceSystemCachedRepository.cs index 11a08dd..b95790e 100644 --- a/Persistence.Repository/Repositories/DataSourceSystemCachedRepository.cs +++ b/Persistence.Repository/Repositories/DataSourceSystemCachedRepository.cs @@ -8,6 +8,7 @@ public class DataSourceSystemCachedRepository : DataSourceSystemRepository private static readonly string SystemCacheKey = $"{typeof(Database.Entity.DataSourceSystem).FullName}CacheKey"; private readonly IMemoryCache memoryCache; private const int CacheExpirationInMinutes = 60; + private readonly TimeSpan? AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(60); public DataSourceSystemCachedRepository(DbContext db, IMemoryCache memoryCache) : base(db) { @@ -19,11 +20,12 @@ public class DataSourceSystemCachedRepository : DataSourceSystemRepository memoryCache.Remove(SystemCacheKey); } - public override async Task> Get(CancellationToken token) { - var systems = await memoryCache.GetOrCreateAsync(SystemCacheKey, async f => + var systems = await memoryCache.GetOrCreateAsync(SystemCacheKey, async (cacheEntry) => { + cacheEntry.AbsoluteExpirationRelativeToNow = AbsoluteExpirationRelativeToNow; + var dtos = await base.Get(token); return dtos; diff --git a/Persistence.Repository/Repositories/DataSourceSystemRepository.cs b/Persistence.Repository/Repositories/DataSourceSystemRepository.cs index 9fade43..5862ea3 100644 --- a/Persistence.Repository/Repositories/DataSourceSystemRepository.cs +++ b/Persistence.Repository/Repositories/DataSourceSystemRepository.cs @@ -25,14 +25,14 @@ public class DataSourceSystemRepository : IDataSourceSystemRepository { var entity = dataSourceSystemDto.Adapt(); - await db.Set().AddAsync(entity); + await db.Set().AddAsync(entity, token); await db.SaveChangesAsync(token); } - public virtual async Task> Get(CancellationToken token) + public virtual async Task> Get(CancellationToken token) { var query = GetQueryReadOnly(); - var entities = await query.ToListAsync(token); + var entities = await query.ToArrayAsync(token); var dtos = entities.Select(e => e.Adapt()); return dtos; diff --git a/Persistence.Repository/Repositories/TechMessagesRepository.cs b/Persistence.Repository/Repositories/TechMessagesRepository.cs index f542265..a2394ee 100644 --- a/Persistence.Repository/Repositories/TechMessagesRepository.cs +++ b/Persistence.Repository/Repositories/TechMessagesRepository.cs @@ -53,10 +53,10 @@ namespace Persistence.Repository.Repositories { var query = GetQueryReadOnly(); var result = await query - .Where(e => systems.Count() == 0 || systems.Contains(e.System.SystemId)) - .GroupBy(e => e.System.Name, (key, group) => new + .Where(e => !systems.Any() || systems.Contains(e.System.SystemId)) + .GroupBy(e => e.System.SystemId, (key, group) => new { - System = key, + System = group.FirstOrDefault()!.System.Name, Categories = group .Where(g => categoryIds.Count() == 0 || categoryIds.Contains(g.CategoryId)) }) @@ -81,14 +81,16 @@ namespace Persistence.Repository.Repositories public async Task AddRange(Guid systemId, IEnumerable dtos, Guid userId, CancellationToken token) { + await CreateSystemIfNotExist(systemId, token); - var entities = new List(); + var entities = new List(); foreach (var dto in dtos) { var entity = dto.Adapt(); - await CreateSystemIfNotExist(systemId, token); - entity.SystemId = systemId; + await CreateSystemIfNotExist(systemId, token); + + entity.SystemId = systemId; entities.Add(entity); } @@ -116,7 +118,7 @@ namespace Persistence.Repository.Repositories { var systems = await sourceSystemRepository.Get(token); - return systems ?? []; + return systems!; } public async Task GetDatesRangeAsync(CancellationToken token) diff --git a/Persistence/Repositories/IDataSourceSystemRepository.cs b/Persistence/Repositories/IDataSourceSystemRepository.cs index 1d964eb..d6cf29c 100644 --- a/Persistence/Repositories/IDataSourceSystemRepository.cs +++ b/Persistence/Repositories/IDataSourceSystemRepository.cs @@ -3,7 +3,7 @@ namespace Persistence.Repositories; /// -/// Интерфейс по работе с системами +/// Интерфейс по работе с системами - источниками данных /// public interface IDataSourceSystemRepository { @@ -14,9 +14,9 @@ public interface IDataSourceSystemRepository /// public Task Add(DataSourceSystemDto dataSourceSystemDto, CancellationToken token); - /// - /// Получить список систем - /// - /// - public Task> Get(CancellationToken token); + /// + /// Получить список систем + /// + /// + public Task> Get(CancellationToken token); }