diff --git a/Persistence.API/Controllers/WitsDataController.cs b/Persistence.API/Controllers/WitsDataController.cs index f66b0c3..10dd7c7 100644 --- a/Persistence.API/Controllers/WitsDataController.cs +++ b/Persistence.API/Controllers/WitsDataController.cs @@ -27,12 +27,12 @@ public class WitsDataController : ControllerBase, IWitsDataApi /// /// /// - [HttpGet("datesRange")] - public async Task> GetDatesRangeAsync([FromQuery] int discriminatorId, CancellationToken token) + [HttpGet("{discriminatorId}/datesRange")] + public async Task> GetDatesRangeAsync([FromRoute] int discriminatorId, CancellationToken token) { var result = await witsDataService.GetDatesRangeAsync(discriminatorId, token); - return Ok(result); + return result == null ? NoContent() : Ok(result); } /// @@ -43,8 +43,8 @@ public class WitsDataController : ControllerBase, IWitsDataApi /// /// /// - [HttpGet("part")] - public async Task>> GetPart([FromQuery] int discriminatorId, [FromQuery] DateTimeOffset dateBegin, [FromQuery] int take, CancellationToken token) + [HttpGet("{discriminatorId}/part")] + public async Task>> GetPart([FromRoute] int discriminatorId, [FromQuery] DateTimeOffset dateBegin, [FromQuery] int take, CancellationToken token) { var result = await witsDataService.GetPart(discriminatorId, dateBegin, take, token); @@ -60,8 +60,8 @@ public class WitsDataController : ControllerBase, IWitsDataApi /// Количество точек /// /// - [HttpGet("graph")] - public async Task>> GetValuesForGraph([FromQuery] int discriminatorId, + [HttpGet("{discriminatorId}/graph")] + public async Task>> GetValuesForGraph([FromRoute] int discriminatorId, [FromQuery] DateTimeOffset dateFrom, [FromQuery] DateTimeOffset dateTo, [FromQuery] int approxPointsCount, CancellationToken token) { var result = await witsDataService.GetValuesForGraph(discriminatorId, dateFrom, dateTo, approxPointsCount, token); diff --git a/Persistence.Client/Clients/IWitsDataClient.cs b/Persistence.Client/Clients/IWitsDataClient.cs index 528cb0c..108a4d7 100644 --- a/Persistence.Client/Clients/IWitsDataClient.cs +++ b/Persistence.Client/Clients/IWitsDataClient.cs @@ -1,4 +1,5 @@ -using Persistence.Models; +using Microsoft.AspNetCore.Mvc; +using Persistence.Models; using Refit; namespace Persistence.Client.Clients; @@ -6,15 +7,15 @@ public interface IWitsDataClient { private const string BaseRoute = "/api/witsData"; - [Get($"{BaseRoute}/graph")] - Task>> GetValuesForGraph([Query] int discriminatorId, [Query] DateTimeOffset dateFrom, [Query] DateTimeOffset dateTo, [Query] int approxPointsCount, CancellationToken token); + [Get($"{BaseRoute}/{{discriminatorId}}/graph")] + Task>> GetValuesForGraph(int discriminatorId, [Query] DateTimeOffset dateFrom, [Query] DateTimeOffset dateTo, [Query] int approxPointsCount, CancellationToken token); [Post($"{BaseRoute}/")] - Task> AddRange([Body] IEnumerable dtos, CancellationToken token); + Task> AddRange(IEnumerable dtos, CancellationToken token); - [Get($"{BaseRoute}/part")] - Task>> GetPart([Query] int discriminatorId, [Query] DateTimeOffset dateBegin, [Query] int take = 24 * 60 * 60, CancellationToken token = default); + [Get($"{BaseRoute}/{{discriminatorId}}/part")] + Task>> GetPart(int discriminatorId, [Query] DateTimeOffset dateBegin, [Query] int take = 24 * 60 * 60, CancellationToken token = default); - [Get($"{BaseRoute}/datesRange")] - Task> GetDatesRangeAsync([Query] int discriminatorId, CancellationToken token); + [Get($"{BaseRoute}/{{discriminatorId}}/datesRange")] + Task> GetDatesRangeAsync(int discriminatorId, CancellationToken token); } diff --git a/Persistence.IntegrationTests/Controllers/SetpointControllerTest.cs b/Persistence.IntegrationTests/Controllers/SetpointControllerTest.cs index 2c455a0..ab3659c 100644 --- a/Persistence.IntegrationTests/Controllers/SetpointControllerTest.cs +++ b/Persistence.IntegrationTests/Controllers/SetpointControllerTest.cs @@ -140,7 +140,7 @@ namespace Persistence.IntegrationTests.Controllers dbContext.CleanupDbSet(); //act - var response = await setpointClient.GetDatesRangeAsync(new CancellationToken()); + var response = await setpointClient.GetDatesRangeAsync(CancellationToken.None); //assert Assert.Equal(HttpStatusCode.OK, response.StatusCode); @@ -159,10 +159,10 @@ namespace Persistence.IntegrationTests.Controllers var dateBegin = DateTimeOffset.MinValue; var take = 1; - var part = await setpointClient.GetPart(dateBegin, take, new CancellationToken()); + var part = await setpointClient.GetPart(dateBegin, take, CancellationToken.None); //act - var response = await setpointClient.GetDatesRangeAsync(new CancellationToken()); + var response = await setpointClient.GetDatesRangeAsync(CancellationToken.None); //assert Assert.Equal(HttpStatusCode.OK, response.StatusCode); @@ -188,7 +188,7 @@ namespace Persistence.IntegrationTests.Controllers var take = 2; //act - var response = await setpointClient.GetPart(dateBegin, take, new CancellationToken()); + var response = await setpointClient.GetPart(dateBegin, take, CancellationToken.None); //assert Assert.Equal(HttpStatusCode.OK, response.StatusCode); @@ -205,7 +205,7 @@ namespace Persistence.IntegrationTests.Controllers await Add(); //act - var response = await setpointClient.GetPart(dateBegin, take, new CancellationToken()); + var response = await setpointClient.GetPart(dateBegin, take, CancellationToken.None); //assert Assert.Equal(HttpStatusCode.OK, response.StatusCode); diff --git a/Persistence.IntegrationTests/Controllers/TechMessagesControllerTest.cs b/Persistence.IntegrationTests/Controllers/TechMessagesControllerTest.cs index 1f194ad..08eb120 100644 --- a/Persistence.IntegrationTests/Controllers/TechMessagesControllerTest.cs +++ b/Persistence.IntegrationTests/Controllers/TechMessagesControllerTest.cs @@ -40,7 +40,7 @@ namespace Persistence.IntegrationTests.Controllers }; //act - var response = await techMessagesClient.GetPage(requestDto, new CancellationToken()); + var response = await techMessagesClient.GetPage(requestDto, CancellationToken.None); //assert Assert.Equal(HttpStatusCode.OK, response.StatusCode); @@ -64,7 +64,7 @@ namespace Persistence.IntegrationTests.Controllers }; //act - var response = await techMessagesClient.GetPage(requestDto, new CancellationToken()); + var response = await techMessagesClient.GetPage(requestDto, CancellationToken.None); //assert Assert.Equal(HttpStatusCode.OK, response.StatusCode); @@ -97,7 +97,7 @@ namespace Persistence.IntegrationTests.Controllers }; //act - var response = await techMessagesClient.AddRange(dtos, new CancellationToken()); + var response = await techMessagesClient.AddRange(dtos, CancellationToken.None); //assert Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode); @@ -112,7 +112,7 @@ namespace Persistence.IntegrationTests.Controllers dbContext.CleanupDbSet(); //act - var response = await techMessagesClient.GetSystems(new CancellationToken()); + var response = await techMessagesClient.GetSystems(CancellationToken.None); //assert Assert.Equal(HttpStatusCode.OK, response.StatusCode); @@ -131,7 +131,7 @@ namespace Persistence.IntegrationTests.Controllers .ToArray(); //act - var response = await techMessagesClient.GetSystems(new CancellationToken()); + var response = await techMessagesClient.GetSystems(CancellationToken.None); //assert Assert.Equal(HttpStatusCode.OK, response.StatusCode); @@ -152,7 +152,7 @@ namespace Persistence.IntegrationTests.Controllers var autoDrillingSystem = nameof(TechMessageDto.System); //act - var response = await techMessagesClient.GetStatistics(autoDrillingSystem, imortantId, new CancellationToken()); + var response = await techMessagesClient.GetStatistics(autoDrillingSystem, imortantId, CancellationToken.None); //assert Assert.Equal(HttpStatusCode.OK, response.StatusCode); @@ -170,7 +170,7 @@ namespace Persistence.IntegrationTests.Controllers var filteredDtos = dtos.Where(e => e.CategoryId == imortantId && e.System == autoDrillingSystem); //act - var response = await techMessagesClient.GetStatistics(autoDrillingSystem, imortantId, new CancellationToken()); + var response = await techMessagesClient.GetStatistics(autoDrillingSystem, imortantId, CancellationToken.None); //assert Assert.Equal(HttpStatusCode.OK, response.StatusCode); @@ -185,7 +185,7 @@ namespace Persistence.IntegrationTests.Controllers public async Task GetDatesRange_returns_success() { //act - var response = await techMessagesClient.GetDatesRangeAsync(new CancellationToken()); + var response = await techMessagesClient.GetDatesRangeAsync(CancellationToken.None); //assert Assert.Equal(HttpStatusCode.OK, response.StatusCode); @@ -201,7 +201,7 @@ namespace Persistence.IntegrationTests.Controllers await InsertRange(); //act - var response = await techMessagesClient.GetDatesRangeAsync(new CancellationToken()); + var response = await techMessagesClient.GetDatesRangeAsync(CancellationToken.None); //assert Assert.Equal(HttpStatusCode.OK, response.StatusCode); @@ -218,7 +218,7 @@ namespace Persistence.IntegrationTests.Controllers var take = 2; //act - var response = await techMessagesClient.GetPart(dateBegin, take, new CancellationToken()); + var response = await techMessagesClient.GetPart(dateBegin, take, CancellationToken.None); //assert Assert.Equal(HttpStatusCode.OK, response.StatusCode); @@ -235,7 +235,7 @@ namespace Persistence.IntegrationTests.Controllers await InsertRange(); //act - var response = await techMessagesClient.GetPart(dateBegin, take, new CancellationToken()); + var response = await techMessagesClient.GetPart(dateBegin, take, CancellationToken.None); //assert Assert.Equal(HttpStatusCode.OK, response.StatusCode); @@ -276,7 +276,7 @@ namespace Persistence.IntegrationTests.Controllers //act - var response = await techMessagesClient.AddRange(dtos, new CancellationToken()); + var response = await techMessagesClient.AddRange(dtos, CancellationToken.None); //assert Assert.Equal(HttpStatusCode.Created, response.StatusCode); diff --git a/Persistence.IntegrationTests/Controllers/WitsDataControllerTest.cs b/Persistence.IntegrationTests/Controllers/WitsDataControllerTest.cs index fc0a02d..9ecae04 100644 --- a/Persistence.IntegrationTests/Controllers/WitsDataControllerTest.cs +++ b/Persistence.IntegrationTests/Controllers/WitsDataControllerTest.cs @@ -29,7 +29,7 @@ public class WitsDataControllerTest : BaseIntegrationTest var discriminatorId = 1; //act - var response = await witsDataClient.GetDatesRangeAsync(discriminatorId, new CancellationToken()); + var response = await witsDataClient.GetDatesRangeAsync(discriminatorId, CancellationToken.None); //assert Assert.Equal(HttpStatusCode.OK, response.StatusCode); @@ -47,7 +47,7 @@ public class WitsDataControllerTest : BaseIntegrationTest var take = 1; //act - var response = await witsDataClient.GetPart(discriminatorId, dateBegin, take, new CancellationToken()); + var response = await witsDataClient.GetPart(discriminatorId, dateBegin, take, CancellationToken.None); //assert Assert.Equal(HttpStatusCode.OK, response.StatusCode); @@ -77,7 +77,7 @@ public class WitsDataControllerTest : BaseIntegrationTest var approxPointCount = 12; //act - var response = await witsDataClient.GetValuesForGraph(discriminatorId, dateFrom, dateTo, approxPointCount, new CancellationToken()); + var response = await witsDataClient.GetValuesForGraph(discriminatorId, dateFrom, dateTo, approxPointCount, CancellationToken.None); //assert Assert.Equal(HttpStatusCode.OK, response.StatusCode); @@ -95,7 +95,7 @@ public class WitsDataControllerTest : BaseIntegrationTest var discriminatorId = dtos.FirstOrDefault()!.DiscriminatorId; //act - var response = await witsDataClient.GetDatesRangeAsync(discriminatorId, new CancellationToken()); + var response = await witsDataClient.GetDatesRangeAsync(discriminatorId, CancellationToken.None); //assert Assert.Equal(HttpStatusCode.OK, response.StatusCode); @@ -111,9 +111,9 @@ public class WitsDataControllerTest : BaseIntegrationTest var expectedDateTo = dtos .Select(e => e.Timestamped) - .Min() + .Max() .ToString("dd.MM.yyyy-HH:mm:ss"); - var actualDateTo = response.Content.From.DateTime + var actualDateTo = response.Content.To.DateTime .ToString("dd.MM.yyyy-HH:mm:ss"); Assert.Equal(expectedDateTo, actualDateTo); } @@ -130,13 +130,22 @@ public class WitsDataControllerTest : BaseIntegrationTest var take = 1; //act - var response = await witsDataClient.GetPart(discriminatorId, dateBegin, take, new CancellationToken()); + var response = await witsDataClient.GetPart(discriminatorId, dateBegin, take, CancellationToken.None); //assert Assert.Equal(HttpStatusCode.OK, response.StatusCode); Assert.NotNull(response.Content); Assert.NotEmpty(response.Content); Assert.Equal(take, response.Content.Count()); + + var expectedDto = dtos.FirstOrDefault(); + var actualDto = response.Content.FirstOrDefault(); + Assert.Equal(expectedDto?.DiscriminatorId, actualDto?.DiscriminatorId); + + var expectedValueDto = expectedDto?.Values.FirstOrDefault(); + var actualValueDto = actualDto?.Values.FirstOrDefault(); + Assert.Equal(expectedValueDto?.ItemId, actualValueDto?.ItemId); + Assert.Equal(expectedValueDto?.RecordId, actualValueDto?.RecordId); } [Fact] @@ -152,7 +161,7 @@ public class WitsDataControllerTest : BaseIntegrationTest var approxPointCount = 12; //act - var response = await witsDataClient.GetValuesForGraph(discriminatorId, dateFrom, dateTo, approxPointCount, new CancellationToken()); + var response = await witsDataClient.GetValuesForGraph(discriminatorId, dateFrom, dateTo, approxPointCount, CancellationToken.None); //assert Assert.Equal(HttpStatusCode.OK, response.StatusCode); @@ -161,7 +170,7 @@ public class WitsDataControllerTest : BaseIntegrationTest } [Fact] - public async Task InsertRange_returns_BadRequest() + public async Task AddRange_returns_BadRequest() { //arrange var dtos = new List() @@ -183,7 +192,7 @@ public class WitsDataControllerTest : BaseIntegrationTest }; //act - var response = await witsDataClient.AddRange(dtos, new CancellationToken()); + var response = await witsDataClient.AddRange(dtos, CancellationToken.None); //assert Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode); @@ -193,8 +202,9 @@ public class WitsDataControllerTest : BaseIntegrationTest { var dtos = new List(); var timestamped = DateTimeOffset.UtcNow; - for (var i = 0; i < countToCreate && i < 100; i++) + for (var i = 0; i < countToCreate; i++) { + var random = new Random(); dtos.Add(new WitsDataDto() { DiscriminatorId = 1, @@ -205,14 +215,14 @@ public class WitsDataControllerTest : BaseIntegrationTest { RecordId = i + 1, ItemId = i + 1, - Value = new Random().Next(1, 100) + Value = random.Next(1, 100) } } }); } //act - var response = await witsDataClient.AddRange(dtos, new CancellationToken()); + var response = await witsDataClient.AddRange(dtos, CancellationToken.None); //assert var count = dtos.SelectMany(e => e.Values).Count(); diff --git a/Persistence.Repository/Repositories/ParameterRepository.cs b/Persistence.Repository/Repositories/ParameterRepository.cs index 51510ec..dbf387f 100644 --- a/Persistence.Repository/Repositories/ParameterRepository.cs +++ b/Persistence.Repository/Repositories/ParameterRepository.cs @@ -58,7 +58,8 @@ public class ParameterRepository : IParameterRepository var universalDateTo = dateTo.ToUniversalTime(); query = query - .Where(e => e.DiscriminatorId == discriminatorId && e.Timestamp >= universalDateFrom && e.Timestamp <= universalDateTo) + .Where(e => e.DiscriminatorId == discriminatorId) + .Where(e => e.Timestamp >= universalDateFrom && e.Timestamp <= universalDateTo) .OrderBy(e => e.Timestamp); if (ratio != null) { @@ -67,7 +68,7 @@ public class ParameterRepository : IParameterRepository var entities = await query .Take((int)(2.5 * approxPointsCount)) - .ToListAsync(token); + .ToArrayAsync(token); var dtos = entities.Select(e => e.Adapt()); diff --git a/Persistence/Models/Configurations/WitsInfo.cs b/Persistence/Models/Configurations/WitsInfo.cs index 79e40bb..f096d11 100644 --- a/Persistence/Models/Configurations/WitsInfo.cs +++ b/Persistence/Models/Configurations/WitsInfo.cs @@ -2,6 +2,9 @@ namespace Persistence.Models.Configurations; +/// +/// Протокол Wits +/// public class WitsInfo { public int RecordId { get; set; } diff --git a/Persistence/Services/WitsDataService.cs b/Persistence/Services/WitsDataService.cs index 3ff0c32..873ef1b 100644 --- a/Persistence/Services/WitsDataService.cs +++ b/Persistence/Services/WitsDataService.cs @@ -18,7 +18,7 @@ public class WitsDataService : IWitsDataService private const int multiplier = 1000; private const string witsConfigPath = "Persistence.Services.Config.WitsConfig.json"; - public WitsDataService(IParameterRepository witsDataRepository, IConfiguration configuration) + public WitsDataService(IParameterRepository witsDataRepository) { this.witsDataRepository = witsDataRepository; @@ -96,30 +96,60 @@ public class WitsDataService : IWitsDataService private IEnumerable AdaptToWitsData(IEnumerable dtos) { var result = new List(); - foreach (var dto in dtos) + var witsGroup = dtos + .GroupBy(e => new { e.DiscriminatorId, e.Timestamp }); + foreach (var witsInGroup in witsGroup) { - var witsDataDto = result.FirstOrDefault(e => e.DiscriminatorId == dto.DiscriminatorId && e.Timestamped == dto.Timestamp); - if (witsDataDto == null) + var witsDataDto = new WitsDataDto() { - witsDataDto = new WitsDataDto() - { - DiscriminatorId = dto.DiscriminatorId, - Timestamped = dto.Timestamp - }; - result.Add(witsDataDto); - } - var recordId = DecodeRecordId(dto.ParameterId); - var itemId = DecodeItemId(dto.ParameterId); - var witsValueDto = new WitsValueDto() - { - RecordId = recordId, - ItemId = itemId, - Value = ConvertValue(recordId, itemId, dto.Value) + DiscriminatorId = witsInGroup.Key.DiscriminatorId, + Timestamped = witsInGroup.Key.Timestamp }; - witsDataDto.Values.Append(witsValueDto); + witsDataDto.Values = witsInGroup.Select(e => + { + var recordId = DecodeRecordId(e.ParameterId); + var itemId = DecodeItemId(e.ParameterId); + + return new WitsValueDto() + { + RecordId = recordId, + ItemId = itemId, + Value = ConvertValue(recordId, itemId, e.Value) + }; + }); + + result.Add(witsDataDto); } + + //foreach (var dto in dtos) + //{ + // var witsDataDto = result + // .Where(e => e.DiscriminatorId == dto.DiscriminatorId) + // .Where(e => e.Timestamped == dto.Timestamp) + // .FirstOrDefault(); + // if (witsDataDto == null) + // { + // witsDataDto = new WitsDataDto() + // { + // DiscriminatorId = dto.DiscriminatorId, + // Timestamped = dto.Timestamp + // }; + // result.Add(witsDataDto); + // } + // var recordId = DecodeRecordId(dto.ParameterId); + // var itemId = DecodeItemId(dto.ParameterId); + // var witsValueDto = new WitsValueDto() + // { + // RecordId = recordId, + // ItemId = itemId, + // Value = ConvertValue(recordId, itemId, dto.Value) + // }; + + // witsDataDto.Values = witsDataDto.Values.Append(witsValueDto); + //} + return result; }