<commit_before>/* eslint-disable no-console */
(function () {
    'use strict';

    const { config } = require('data-service');
    const { flatten, pipe, prop, map } = require('ramda');
    const POLL_DELAY = 800;

    /**
     * @param {app.utils} utils
     * @param {TimeLine} timeLine
     * @param {SymbolInfoService} symbolInfoService
     * @param {Waves} waves
     * @return {CandlesService}
     */
    const factory = function (utils, timeLine, symbolInfoService, waves) {

        class CandlesService {

            constructor() {
                this._lastTime = 0;
                this._subscriber = null;
                this.onLoadError = angular.noop;
            }

            static _getAndHandleCandles(symbolInfo, from, to, resolution, handleCandles, handleError = angular.noop) {
                CandlesService
                    ._getCandles(
                        symbolInfo,
                        from,
                        to,
                        resolution
                    )
                    .then(handleCandles)
                    .catch(handleError);
            }

            /**
             * @param {string} symbolInfo
             * @param {number} from
             * @param {number} to
             * @param {number} interval
             * @return {Promise<T | Array>}
             * @private
             */
            static _getCandles(symbolInfo, from, to, interval) {
                from = from || to;
                const amountId = symbolInfo._wavesData.amountAsset.id;
                const priceId = symbolInfo._wavesData.priceAsset.id;
                const { options, config: candleConfig } = utils.getValidCandleOptions(from, to, interval);
                const promises = options.map(option => config.getDataService().getCandles(amountId, priceId, option));

                const convertBigNumber = num => num.isNaN() ? null : num.toNumber();

                const candles = Promise.all(promises)
                    .then(pipe(map(prop('data')), flatten))
                    .then(list => list
                        .filter(candle => !candle.open.isNaN())
                        .map(candle => ({
                            txsCount: candle.txsCount || 0,
                            high: convertBigNumber(candle.high),
                            low: convertBigNumber(candle.low),
                            close: convertBigNumber(candle.close),
                            open: convertBigNumber(candle.open),
                            volume: convertBigNumber(candle.volume),
                            time: new Date(candle.time).getTime()
                        })));

                const lastTrade = ds.api.pairs.get(amountId, priceId)
                    .then(pair => waves.matcher.getLastPrice(pair)
                        .catch(() => null));

                return Promise.all([candles, lastTrade])
                    .then(([candles, lastTrade]) => {
                        if (candles.length === 1 && lastTrade) {
                            const lastCandle = candles[candles.length - 1];

                            if (lastCandle.open != null) {
                                lastCandle.close = Number(lastTrade.price.toTokens());
                            }
                        } else {
                            candles = candleConfig.converter(candles);
                        }
                        return candles;
                    }).catch(() => utils.wait(5000).then(() => []));
            }

            static convertToMilliseconds(seconds) {
                return Number(seconds) ? seconds * 1000 : Date.now();
            }<commit_msg>CLIENT-1777: filter only for last candle<commit_after>/* eslint-disable no-console */
(function () {
    'use strict';

    const { config } = require('data-service');
    const { flatten, pipe, prop, map } = require('ramda');
    const POLL_DELAY = 800;

    /**
     * @param {app.utils} utils
     * @param {TimeLine} timeLine
     * @param {SymbolInfoService} symbolInfoService
     * @param {Waves} waves
     * @return {CandlesService}
     */
    const factory = function (utils, timeLine, symbolInfoService, waves) {

        class CandlesService {

            constructor() {
                this._lastTime = 0;
                this._subscriber = null;
                this.onLoadError = angular.noop;
            }

            static _getAndHandleCandles(symbolInfo, from, to, resolution, handleCandles, handleError = angular.noop) {
                CandlesService
                    ._getCandles(
                        symbolInfo,
                        from,
                        to,
                        resolution
                    )
                    .then(handleCandles)
                    .catch(handleError);
            }

            /**
             * @param {string} symbolInfo
             * @param {number} from
             * @param {number} to
             * @param {number} interval
             * @return {Promise<T | Array>}
             * @private
             */
            static _getCandles(symbolInfo, from, to, interval) {
                from = from || to;
                const amountId = symbolInfo._wavesData.amountAsset.id;
                const priceId = symbolInfo._wavesData.priceAsset.id;
                const { options, config: candleConfig } = utils.getValidCandleOptions(from, to, interval);
                const promises = options.map(option => config.getDataService().getCandles(amountId, priceId, option));

                const convertBigNumber = num => num.isNaN() ? null : num.toNumber();

                const candles = Promise.all(promises)
                    .then(pipe(map(prop('data')), flatten))
                    .then(list => list
                        .map(candle => ({
                            txsCount: candle.txsCount || 0,
                            high: convertBigNumber(candle.high),
                            low: convertBigNumber(candle.low),
                            close: convertBigNumber(candle.close),
                            open: convertBigNumber(candle.open),
                            volume: convertBigNumber(candle.volume),
                            time: new Date(candle.time).getTime()
                        })));

                const lastTrade = ds.api.pairs.get(amountId, priceId)
                    .then(pair => waves.matcher.getLastPrice(pair)
                        .catch(() => null));

                return Promise.all([candles, lastTrade])
                    .then(([candles, lastTrade]) => {
                        if (candles.length === 1 && lastTrade) {
                            candles[candles.length - 1].close = Number(lastTrade.price.toTokens());
                        } else {
                            candles = candleConfig.converter(candles);
                        }
                        return candles
                            .filter(candle => candle.open != null);
                    }).catch(() => utils.wait(5000).then(() => []));
            }

            static convertToMilliseconds(seconds) {
                return Number(seconds) ? seconds * 1000 : Date.now();
            }
{'fraction_non_alphanumeric': 0.10699986193566202, 'fraction_numerical': 0.0046941874913709785, 'mean_word_length': 1.2765556253928347, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4031203', 'n_tokens_mistral': 1896, 'n_tokens_neox': 1755, 'n_words': 358}