示例#1
0
        public async Task RestoreSwaps()
        {
            var positions = _ordersCache.Positions.GetAllPositions();

            if (positions.Any())
            {
                var positionsIds         = positions.Select(x => x.Id);
                var swapTotalPerPosition = await _accountHistoryRepository.GetSwapTotalPerPosition(positionsIds);

                if (swapTotalPerPosition.Any())
                {
                    swapTotalPerPosition.ForEach(x =>
                    {
                        var position = positions.Single(p => p.Id == x.Key);
                        position.SetSwapTotal(x.Value);
                    });

                    try
                    {
                        await _blobRepository.WriteAsync(LykkeConstants.StateBlobContainer, OrderCacheManager.PositionsBlobName, positions);
                    }
                    catch (Exception ex)
                    {
                        await _log.WriteErrorAsync(nameof(OrdersCache), "Save positions", "", ex);
                    }
                }
            }
        }
示例#2
0
        /// <summary>
        /// Infer init data from blob and history.
        /// </summary>
        private (List <Order> Orders, List <Position> Positions) InferInitDataFromBlobAndHistory()
        {
            _log.WriteInfo(nameof(OrderCacheManager), nameof(InferInitDataFromBlobAndHistory),
                           $"Start reading order and position data from blob.");

            var blobOrdersTask = _blobRepository.ReadWithTimestampAsync <List <Order> >(
                LykkeConstants.StateBlobContainer, OrdersBlobName);
            var blobPositionsTask = _blobRepository.ReadWithTimestampAsync <List <Position> >(
                LykkeConstants.StateBlobContainer, PositionsBlobName);

            var(blobOrders, blobOrdersTimestamp)       = blobOrdersTask.GetAwaiter().GetResult();
            var(blobPositions, blobPositionsTimestamp) = blobPositionsTask.GetAwaiter().GetResult();

            _log.WriteInfo(nameof(OrderCacheManager), nameof(InferInitDataFromBlobAndHistory),
                           $"Finish reading data from blob, there are [{blobOrders.Count}] orders, [{blobPositions.Count}] positions. Start checking historical data.");

            var orderSnapshotsTask    = _ordersHistoryRepository.GetLastSnapshot(blobOrdersTimestamp);
            var positionSnapshotsTask = _positionsHistoryRepository.GetLastSnapshot(blobPositionsTimestamp);
            var orderSnapshots        = orderSnapshotsTask.GetAwaiter().GetResult().Select(OrderHistory.Create).ToList();

            PreProcess(orderSnapshots);
            var positionSnapshots = positionSnapshotsTask.GetAwaiter().GetResult();

            _log.WriteInfo(nameof(OrderCacheManager), nameof(InferInitDataFromBlobAndHistory),
                           $"Finish reading historical data. #{orderSnapshots.Count} order history items since [{blobOrdersTimestamp:s}], #{positionSnapshots.Count} position history items since [{blobPositionsTimestamp:s}].");

            var(ordersResult, orderIdsChangedFromHistory) = MapOrders(blobOrders.ToDictionary(x => x.Id, x => x),
                                                                      orderSnapshots.ToDictionary(x => x.Id, x => x), _log);
            var(positionsResult, positionIdsChangedFromHistory) = MapPositions(
                blobPositions.ToDictionary(x => x.Id, x => x), positionSnapshots.ToDictionary(x => x.Id, x => x));

            if (positionIdsChangedFromHistory.Any())
            {
                var swapTotalPerPosition = _accountHistoryRepository.GetSwapTotalPerPosition(positionIdsChangedFromHistory).GetAwaiter().GetResult();
                swapTotalPerPosition.ForEach(x =>
                {
                    var position = positionsResult.Single(p => p.Id == x.Key);
                    position.SetSwapTotal(x.Value);
                });
            }

            RefreshRelated(ordersResult.ToDictionary(x => x.Id), positionsResult.ToDictionary(x => x.Id), orderSnapshots);
            ApplyExpirationDateFix(ordersResult);

            _log.WriteInfo(nameof(OrderCacheManager), nameof(InferInitDataFromBlobAndHistory),
                           $"Initializing cache with [{ordersResult.Count}] orders and [{positionsResult.Count}] positions.");

            _orderCache.InitOrders(ordersResult, positionsResult);

            if (orderIdsChangedFromHistory.Any() || positionIdsChangedFromHistory.Any())
            {
                _log.WriteInfo(nameof(OrderCacheManager), nameof(InferInitDataFromBlobAndHistory),
                               (orderIdsChangedFromHistory.Any() ? $"Some orders state was different from history: [{string.Join(",", orderIdsChangedFromHistory)}]. " : string.Empty)
                               + (positionIdsChangedFromHistory.Any() ? $"Some positions state was different from history: [{string.Join(",", positionIdsChangedFromHistory)}]. " : string.Empty)
                               + "Dumping merged order and position data to the blob."
                               );

                if (orderIdsChangedFromHistory.Any())
                {
                    DumpOrdersToRepository().Wait();
                }

                if (positionIdsChangedFromHistory.Any())
                {
                    DumpPositionsToRepository().Wait();
                }

                _log.WriteInfo(nameof(OrderCacheManager), nameof(InferInitDataFromBlobAndHistory),
                               "Finished dumping merged order and position data to the blob."
                               );
            }

            return(ordersResult, positionsResult);
        }