public LongTaskLaunchResult Filtrate(ICandlesFiltrationRequest request, bool analyzeOnly) { // We should not run filtration multiple times before the first attempt ends. if (Health != null && Health.State == CandlesFiltrationState.InProgress) { return(LongTaskLaunchResult.AlreadyInProgress); } // And also we should check if the specified asset pair is enabled. var storedAssetPair = _assetPairsManager.TryGetEnabledPairAsync(request.AssetPairId).GetAwaiter().GetResult(); if (storedAssetPair == null || !_candlesHistoryRepository.CanStoreAssetPair(request.AssetPairId)) { return(LongTaskLaunchResult.AssetPairNotSupported); } var epsilon = Math.Pow(10, -storedAssetPair.Accuracy); _log.Info(nameof(Filtrate), $"Starting candles with extreme price filtration for {request.AssetPairId}..."); Health = new CandlesFiltrationHealthReport(request.AssetPairId, request.LimitLow, request.LimitHigh, analyzeOnly); var priceTypeTasks = new List <Task>(); if (request.PriceType.HasValue) { priceTypeTasks.Add( DoFiltrateAsync(request.AssetPairId, request.LimitLow, request.LimitHigh, request.PriceType.Value, epsilon, analyzeOnly)); } else { foreach (var priceType in Constants.StoredPriceTypes) { priceTypeTasks.Add( DoFiltrateAsync(request.AssetPairId, request.LimitLow, request.LimitHigh, priceType, epsilon, analyzeOnly)); } } Task.WhenAll(priceTypeTasks.ToArray()).ContinueWith(t => { Health.State = CandlesFiltrationState.Finished; if (analyzeOnly) { _log.Info(nameof(Filtrate), $"Filtration for {request.AssetPairId} finished: analyze only. Total amount of candles to delete: {Health.DeletedCandlesCount.Values.Sum()}, " + $"total amount of candles to replace: {Health.ReplacedCandlesCount.Values.Sum()}. Errors count: {Health.Errors.Count}."); } else { _log.Info(nameof(Filtrate), $"Filtration for {request.AssetPairId} finished. Total amount of deleted Sec candles: {Health.DeletedCandlesCount.Values.Sum()}, " + $"total amount of replaced bigger candles: {Health.ReplacedCandlesCount.Values.Sum()}. Errors count: {Health.Errors.Count}."); } }); return(LongTaskLaunchResult.Started); }
public CandlesFiltrationManager( IAssetPairsManager assetPairsManager, ICandlesHistoryRepository candlesHistoryRepository, ICandlesFiltrationService candlesFiltrationService, ILog log ) { _assetPairsManager = assetPairsManager; _candlesHistoryRepository = candlesHistoryRepository; _candlesFiltrationService = candlesFiltrationService; _log = log; Health = null; }
public CandlesFiltrationManager( IAssetPairsManager assetPairsManager, ICandlesHistoryRepository candlesHistoryRepository, ICandlesFiltrationService candlesFiltrationService, ILogFactory logFactory ) { _assetPairsManager = assetPairsManager; _candlesHistoryRepository = candlesHistoryRepository; _candlesFiltrationService = candlesFiltrationService; _log = logFactory.CreateLog(this); Health = null; }