All files / src/services/ProviderTracker TrackedChainHistoryProvider.ts

100% Statements 31/31
100% Branches 0/0
100% Functions 12/12
100% Lines 27/27

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 5043x 43x     43x 135x 135x 135x 135x     119x 119x 119x 119x       4x 4x 4x 4x         43x 135x             135x 135x   135x 135x 143x 143x     135x 9x 135x 2x      
import { BehaviorSubject } from 'rxjs';
import { CLEAN_FN_STATS, ProviderFnStats, ProviderTracker } from './ProviderTracker';
import { ChainHistoryProvider } from '@cardano-sdk/core';
 
export class ChainHistoryProviderStats {
  readonly healthCheck$ = new BehaviorSubject<ProviderFnStats>(CLEAN_FN_STATS);
  readonly blocksByHashes$ = new BehaviorSubject<ProviderFnStats>(CLEAN_FN_STATS);
  readonly transactionsByAddresses$ = new BehaviorSubject<ProviderFnStats>(CLEAN_FN_STATS);
  readonly transactionsByHashes$ = new BehaviorSubject<ProviderFnStats>(CLEAN_FN_STATS);
 
  shutdown() {
    this.healthCheck$.complete();
    this.blocksByHashes$.complete();
    this.transactionsByAddresses$.complete();
    this.transactionsByHashes$.complete();
  }
 
  reset() {
    this.healthCheck$.next(CLEAN_FN_STATS);
    this.blocksByHashes$.next(CLEAN_FN_STATS);
    this.transactionsByAddresses$.next(CLEAN_FN_STATS);
    this.transactionsByHashes$.next(CLEAN_FN_STATS);
  }
}
 
/** Wraps a ChainHistoryProvider, tracking # of calls of each function */
export class TrackedChainHistoryProvider extends ProviderTracker implements ChainHistoryProvider {
  readonly stats = new ChainHistoryProviderStats();
  readonly healthCheck: ChainHistoryProvider['healthCheck'];
  readonly transactionsByAddresses: ChainHistoryProvider['transactionsByAddresses'];
  readonly transactionsByHashes: ChainHistoryProvider['transactionsByHashes'];
  readonly blocksByHashes: ChainHistoryProvider['blocksByHashes'];
 
  constructor(chainHistoryProvider: ChainHistoryProvider) {
    super();
    chainHistoryProvider = chainHistoryProvider;
 
    this.healthCheck = () => this.trackedCall(() => chainHistoryProvider.healthCheck(), this.stats.healthCheck$);
    this.transactionsByAddresses = ({ addresses, pagination, blockRange }) =>
      this.trackedCall(
        () => chainHistoryProvider.transactionsByAddresses({ addresses, blockRange, pagination }),
        this.stats.transactionsByAddresses$
      );
    this.transactionsByHashes = (hashes) =>
      this.trackedCall(() => chainHistoryProvider.transactionsByHashes(hashes), this.stats.transactionsByHashes$);
    this.blocksByHashes = (hashes) =>
      this.trackedCall(() => chainHistoryProvider.blocksByHashes(hashes), this.stats.blocksByHashes$);
  }
}