All files / redis-smq-monitor/src/plugins/message-rate/common time-series.ts

89.65% Statements 26/29
50% Branches 3/6
100% Functions 6/6
89.65% Lines 26/29

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 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 8457x 57x       57x     1074x 1074x 1074x         1074x           1074x 1074x 1074x 3x 1074x 6x 1074x 1x 1074x       1x         1x       6x         6x       3x         3x                                   640x 640x 640x       559x      
import { EventEmitter } from 'events';
import { errors, RedisClient } from 'redis-smq-common';
import { TTimeSeriesParams, TTimeSeriesRange } from '../../../../types';
import { ICallback, TRedisClientMulti } from 'redis-smq-common/dist/types';
 
export abstract class TimeSeries<
  TimeSeriesParams extends TTimeSeriesParams,
> extends EventEmitter {
  protected retentionTime = 24 * 60 * 60;
  protected expireAfter = 0;
  protected windowSize = 60;
  protected redisClient: RedisClient;
  protected key: string;
 
  constructor(redisClient: RedisClient, params: TimeSeriesParams) {
    super();
    const {
      key,
      expireAfterInSeconds,
      retentionTimeInSeconds,
      windowSizeInSeconds,
    } = params;
    this.redisClient = redisClient;
    if (expireAfterInSeconds !== undefined)
      this.setExpiration(expireAfterInSeconds);
    if (retentionTimeInSeconds !== undefined)
      this.setRetentionTime(retentionTimeInSeconds);
    if (windowSizeInSeconds !== undefined)
      this.setWindowSize(windowSizeInSeconds);
    this.key = key;
  }
 
  setWindowSize(windowSizeInSeconds: number): void {
    Iif (windowSizeInSeconds < 1) {
      throw new errors.ArgumentError(
        'Expected a positive integer value in milliseconds >= 1',
      );
    }
    this.windowSize = windowSizeInSeconds;
  }
 
  setRetentionTime(retentionTimeInSeconds: number): void {
    Iif (retentionTimeInSeconds < 1) {
      throw new errors.ArgumentError(
        'Expected a positive integer value in milliseconds >= 1',
      );
    }
    this.retentionTime = retentionTimeInSeconds;
  }
 
  setExpiration(expireAfterInSeconds: number): void {
    Iif (expireAfterInSeconds < 0) {
      throw new errors.ArgumentError(
        'Expected a positive integer value in milliseconds',
      );
    }
    this.expireAfter = expireAfterInSeconds;
  }
 
  abstract add(
    ts: number,
    value: number,
    cb: ICallback<void> | TRedisClientMulti,
  ): void;
 
  abstract getRange(
    from: number,
    to: number,
    cb: ICallback<TTimeSeriesRange>,
  ): void;
 
  abstract cleanUp(cb: ICallback<void>): void;
 
  getRangeFrom(from: number, cb: ICallback<TTimeSeriesRange>): void {
    const max = from;
    const min = from - this.windowSize;
    this.getRange(min, max, cb);
  }
 
  static getCurrentTimestamp(): number {
    return Math.ceil(Date.now() / 1000);
  }
}