Загрузка данных


Adaptability issue | Not focused
Refactor this function to reduce its Cognitive Complexity from 17 to the 15 allowed.
Cognitive Complexity of functions should not be too hightypescript:S3776
Software qualities impacted:
Maintainability
Not assigned
Code Smell
Critical
Tags

brain-overload
+
Effort
7min
Introduced
38 minutes ago
Where is the issue?
Why is this an issue?
Activity
More Info
moex-terminal-frontsrc/widgets/Chart/components/MoexChart/dataSourceProvide.ts
See all issues in this file


    }
    return result;
  }
  private realtimeConvolution(timeframe: Timeframes, data: Candle, update: (candle: Candle) => void): void {
Refactor this function to reduce its Cognitive Complexity from 17 to the 15 allowed.
    const timeframeSeconds = getTimeframeSeconds(timeframe);
    
if (!this.prevRealtimeData 
|| this.realtimeSessionStart === null) {
      this.realtimeSessionStart = data.time;
      this.prevRealtimeDataArr = [data];
    } 
else {
      const isNewSession = data.time - this.prevRealtimeData.time > timeframeSeconds;
      
if (isNewSession) {
        this.realtimeSessionStart = data.time;
        this.prevRealtimeDataArr = [data];
      } 
else {
        const previousBucketStart =
          this.realtimeSessionStart +
          Math.floor((this.prevRealtimeData.time - this.realtimeSessionStart) / timeframeSeconds) * timeframeSeconds;
        const currentBucketStart =
          this.realtimeSessionStart +
          Math.floor((data.time - this.realtimeSessionStart) / timeframeSeconds) * timeframeSeconds;
        
if (currentBucketStart !== previousBucketStart) {
          this.prevRealtimeDataArr = [data];
        } 
else {
          const candleIndex = this.prevRealtimeDataArr.findIndex((candle) => candle.time === data.time);
          
if (candleIndex === -1) {
            this.prevRealtimeDataArr.push(data);
          } 
else {
            this.prevRealtimeDataArr[candleIndex] = data;
          }
        }
      }
    }
    this.prevRealtimeData = data;
New Code
    const sessionStart = this.realtimeSessionStart;
    
if (sessionStart === null) {
      return;
    }
Covered code
    const bucketStart = sessionStart + Math.floor((data.time - sessionStart) / timeframeSeconds) * timeframeSeconds;
    const candle = aggregateCandles(this.prevRealtimeDataArr, bucketStart);
    
if (candle) {
      update(candle);
    }
  }
}



Adaptability issue | Not focused
Refactor this function to reduce its Cognitive Complexity from 16 to the 15 allowed.
Cognitive Complexity of functions should not be too hightypescript:S3776
Software qualities impacted:
Maintainability
Not assigned
Code Smell
Critical
Tags

brain-overload
+
Effort
6min
Introduced
38 minutes ago
Where is the issue?
Why is this an issue?
Activity
More Info
moex-terminal-frontsrc/widgets/Chart/components/MoexChart/dataSourceProvide.ts
See all issues in this file


      this.realtimeShouldBeConvoluted = true;
      return this.timeframeConvolution(data, timeframe, !until);
    };
  public startRealtime({
Refactor this function to reduce its Cognitive Complexity from 16 to the 15 allowed.
    getSymbols,
    getTimeframe,
    update,
    periodMs = 5000,
    indicativeData,
  }: {
    getSymbols: () => string[];
    getTimeframe: () => Timeframes;
    update: (symbolId: string, candle: Candle) => void;
    periodMs?: number;
    indicativeData?: ChartIndicativeData;
  }): () => void {
    
if (this.realtimeTimer) {
      clearInterval(this.realtimeTimer);
    }
    this.realtimeTimer = setInterval(() => {
      const timeframe = getTimeframe();
      const symbolIds = getSymbols();
      Promise.all(
        symbolIds.map(async (symbolId) => {
          const symbol = getRequestSymbol(symbolId);
          
if (!symbol) {
            return;
          }
          const data = await requestRealtimeBars({
            currencyPair: symbol.replaceAll(':', '.'),
            interval: moexChartTimeConverter(timeframe),
            ticker: symbol,
            indicativeData,
          });
          
if (!data) {
            return;
          }
          
if (this.prevRealtimeData 
&& JSON.stringify(data) === JSON.stringify(this.prevRealtimeData)) {
            return;
          }
          
if (!this.realtimeShouldBeConvoluted) {
            this.prevRealtimeData = data;
            update(symbol, data);
            return;
          }
          this.realtimeConvolution(timeframe, data, (candle) => {
            update(symbol, candle);
          });
        }),
      );
    }, periodMs);
Covered code
    return () => {
      
if (this.realtimeTimer) {
        clearInterval(this.realtimeTimer);
      }
      this.realtimeTimer = null;
    };