private async Task CacheAssetPairCandlesAsync(AssetPair assetPair, DateTime now, SlotType slotType) { try { _log.Info(nameof(InitializeCacheAsync), $"Caching {assetPair.Id} candles history..."); foreach (CandlePriceType priceType in Constants.StoredPriceTypes) { foreach (CandleTimeInterval timeInterval in Constants.InitFromDbIntervals) { DateTime alignedToDate = now.TruncateTo(timeInterval).AddIntervalTicks(1, timeInterval); DateTime alignedFromDate = alignedToDate.AddIntervalTicks(-_amountOfCandlesToStore[timeInterval] - 1, timeInterval); if (alignedFromDate < _minDate) { alignedFromDate = _minDate.TruncateTo(timeInterval); } ICandle[] candles = (await _candlesHistoryRepository.GetCandlesAsync(assetPair.Id, timeInterval, priceType, alignedFromDate, alignedToDate)).ToArray(); if (!candles.Any()) { continue; } var intervals = _candlesCacheService.GetRedisCacheIntervals(timeInterval); foreach (var interval in intervals) { ICandle[] intervalCandles = interval != timeInterval ? CandlesMerger.MergeIntoBiggerIntervals(candles, interval).ToArray() : candles; await _candlesCacheService.InitializeAsync(assetPair.Id, priceType, interval, intervalCandles, slotType); } } } _log.Info($"{assetPair.Id} candles history is cached."); } catch (Exception ex) { _log.Error(nameof(CacheAssetPairCandlesAsync), ex); } }
private async Task CacheAssetPairCandlesAsync(IAssetPair assetPair, DateTime toDate) { await _log.WriteInfoAsync(nameof(CandlesCacheInitalizationService), nameof(InitializeCacheAsync), null, $"Caching {assetPair.Id} candles history..."); foreach (var priceType in Constants.StoredPriceTypes) { foreach (var timeInterval in Constants.StoredIntervals) { var alignedToDate = toDate.TruncateTo(timeInterval); var alignedFromDate = alignedToDate.AddIntervalTicks(-_amountOfCandlesToStore, timeInterval); var candles = await _candlesHistoryRepository.GetCandlesAsync(assetPair.Id, timeInterval, priceType, alignedFromDate, alignedToDate); await _candlesCacheService.InitializeAsync(assetPair.Id, priceType, timeInterval, candles.ToArray()); } } await _log.WriteInfoAsync(nameof(CandlesCacheInitalizationService), nameof(InitializeCacheAsync), null, $"{assetPair.Id} candles history is cached"); }
private async Task CacheAssetPairCandlesAsync(AssetPair assetPair, DateTime now) { if (!_candlesShardValidator.CanHandle(assetPair.Id)) { await _log.WriteInfoAsync(nameof(CandlesCacheInitalizationService), nameof(InitializeCacheAsync), null, $"Skipping {assetPair.Id} caching, since it doesn't meet sharding condition"); return; } await _log.WriteInfoAsync(nameof(CandlesCacheInitalizationService), nameof(InitializeCacheAsync), null, $"Caching {assetPair.Id} candles history..."); try { foreach (var priceType in Constants.StoredPriceTypes) { foreach (var timeInterval in Constants.StoredIntervals) { var alignedToDate = now.TruncateTo(timeInterval).AddIntervalTicks(1, timeInterval); var candlesAmountToStore = _candlesAmountManager.GetCandlesAmountToStore(timeInterval); var candles = await _candlesHistoryRepository.GetLastCandlesAsync(assetPair.Id, timeInterval, priceType, alignedToDate, candlesAmountToStore); await _candlesCacheService.InitializeAsync(assetPair.Id, priceType, timeInterval, candles.ToArray()); } } } catch (Exception e) { await _log.WriteErrorAsync(nameof(CandlesCacheInitalizationService), nameof(CacheAssetPairCandlesAsync), $"Couldn't cache candles history for asset pair [{assetPair.Id}]", e); } finally { await _log.WriteInfoAsync(nameof(CandlesCacheInitalizationService), nameof(CacheAssetPairCandlesAsync), null, $"{assetPair.Id} candles history caching finished"); } }