diff --git a/src/MarginTrading.Backend.Core/Repositories/IOrdersHistoryRepository.cs b/src/MarginTrading.Backend.Core/Repositories/IOrdersHistoryRepository.cs index 500154f82..326ddaa61 100644 --- a/src/MarginTrading.Backend.Core/Repositories/IOrdersHistoryRepository.cs +++ b/src/MarginTrading.Backend.Core/Repositories/IOrdersHistoryRepository.cs @@ -10,6 +10,6 @@ namespace MarginTrading.Backend.Core.Repositories { public interface IOrdersHistoryRepository { - Task> GetLastSnapshot(DateTime @from); + Task> GetLastSnapshot(DateTime @from, DateTime? @to = null); } } diff --git a/src/MarginTrading.Backend.Services/Infrastructure/SnapshotValidationService.cs b/src/MarginTrading.Backend.Services/Infrastructure/SnapshotValidationService.cs index cfb4253d5..edbaf2710 100644 --- a/src/MarginTrading.Backend.Services/Infrastructure/SnapshotValidationService.cs +++ b/src/MarginTrading.Backend.Services/Infrastructure/SnapshotValidationService.cs @@ -62,7 +62,8 @@ await _log.WriteInfoAsync(nameof(SnapshotValidationService), nameof(ValidateCurr var lastOrders = GetOrders(tradingEngineSnapshot); var lastPositions = GetPositions(tradingEngineSnapshot); - var ordersHistory = await _ordersHistoryRepository.GetLastSnapshot(tradingEngineSnapshot.Timestamp); + var latestOrder = currentOrders.MaxBy(x => x.LastModified); + var ordersHistory = await _ordersHistoryRepository.GetLastSnapshot(tradingEngineSnapshot.Timestamp, latestOrder?.LastModified); var positionsHistory = await _positionsHistoryRepository.GetLastSnapshot(tradingEngineSnapshot.Timestamp); var restoredOrders = RestoreOrdersCurrentStateFromHistory(lastOrders, ordersHistory); diff --git a/src/MarginTrading.SqlRepositories/Repositories/OrdersHistoryRepository.cs b/src/MarginTrading.SqlRepositories/Repositories/OrdersHistoryRepository.cs index 4977b17cd..b23e82f38 100644 --- a/src/MarginTrading.SqlRepositories/Repositories/OrdersHistoryRepository.cs +++ b/src/MarginTrading.SqlRepositories/Repositories/OrdersHistoryRepository.cs @@ -34,7 +34,7 @@ CASE [Status] ELSE 99 END as StatusOrder FROM [{0}] oh (NOLOCK) - WHERE oh.ModifiedTimestamp > @Timestamp + WHERE oh.ModifiedTimestamp > @From AND (@To IS NULL OR oh.ModifiedTimestamp <= @To) ), filteredOrderHistWithRowNumber AS ( SELECT *, ROW_NUMBER() OVER (PARTITION BY Id ORDER BY @@ -54,11 +54,11 @@ public OrdersHistoryRepository(string connectionString, string tableName, int ge _getLastSnapshotTimeoutS = getLastSnapshotTimeoutS; } - public async Task> GetLastSnapshot(DateTime @from) + public async Task> GetLastSnapshot(DateTime @from, DateTime? @to = null) { using (var conn = new SqlConnection(_connectionString)) { - var data = await conn.QueryAsync(_select, new { Timestamp = @from }, commandTimeout: _getLastSnapshotTimeoutS); + var data = await conn.QueryAsync(_select, new { From = @from, To = @to }, commandTimeout: _getLastSnapshotTimeoutS); return data.Cast().ToList(); } diff --git a/tests/MarginTradingTests/Infrastructure/SnapshotValidationServiceTests.cs b/tests/MarginTradingTests/Infrastructure/SnapshotValidationServiceTests.cs index 33dccad2e..3b43f72ea 100644 --- a/tests/MarginTradingTests/Infrastructure/SnapshotValidationServiceTests.cs +++ b/tests/MarginTradingTests/Infrastructure/SnapshotValidationServiceTests.cs @@ -62,8 +62,8 @@ public void SetUp() _orderCacheMock.Setup(o => o.GetPositions()) .Returns(() => _currentPositions.ToImmutableArray()); - _ordersHistoryRepositoryMock.Setup(o => o.GetLastSnapshot(It.IsAny())) - .ReturnsAsync((DateTime date) => _ordersHistory); + _ordersHistoryRepositoryMock.Setup(o => o.GetLastSnapshot(It.IsAny(), It.IsAny())) + .ReturnsAsync((DateTime from, DateTime? to) => _ordersHistory); _positionsHistoryRepositoryMock.Setup(o => o.GetLastSnapshot(It.IsAny())) .ReturnsAsync((DateTime date) => _positionsHistory); diff --git a/tests/MarginTradingTests/Modules/MockRepositoriesModule.cs b/tests/MarginTradingTests/Modules/MockRepositoriesModule.cs index 9cfd403a4..22fd9eede 100644 --- a/tests/MarginTradingTests/Modules/MockRepositoriesModule.cs +++ b/tests/MarginTradingTests/Modules/MockRepositoriesModule.cs @@ -29,7 +29,7 @@ protected override void Load(ContainerBuilder builder) blobRepository.Setup(s => s.ReadWithTimestampAsync>(It.IsAny(), It.IsAny())) .ReturnsAsync((new List(), DateTime.UtcNow)); var orderHistoryRepository = new Mock(); - orderHistoryRepository.Setup(s => s.GetLastSnapshot(It.IsAny())) + orderHistoryRepository.Setup(s => s.GetLastSnapshot(It.IsAny(), It.IsAny())) .ReturnsAsync(new List()); var positionHistoryRepository = new Mock(); var accountHistoryRepository = new Mock();