All files stats.ts

100% Statements 33/33
80% Branches 4/5
100% Functions 9/9
100% Lines 33/33
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 84 85 86 87 88  1x     17x 17x 17x 17x 17x     19x     19x     22x 22x     17x 17x 17x     1x     19x 19x 19x 19x 19x 19x     17x 17x 17x 17x 17x     17x 17x 17x 17x 17x     1x 1x                   1x                                                  
export class BatchStats {
  public commands: Array<any>
  public start: number;
  public end: number|null;
  public response: Array<any>
  public error: Error|null
 
  constructor({ commands, start = Date.now() }) {
    this.start = start
    this.end = null
    this.commands = commands
    this.response = null
    this.error = null
  }
 
  get commandCount () {
    return this.commands.length
  }
 
  get responseCount () {
    return (this.response || []).length
  }
 
  get timeInRedis () {
    const end = this.end || Date.now()
    return end - this.start
  }
 
  finish(error: Error|null, response: Array<any>) {
    this.end = Date.now()
    this.error = error
    this.response = response
  }
}
 
export class RedisStats {
  public batches: Set<BatchStats>
  public batchCount: number
  public commandCount: number
  public responseCount: number
  public timeInRedis: number
  public lastBatch: BatchStats | null
 
  constructor() {
    this.batches = new Set()
    this.batchCount = 0
    this.commandCount = 0
    this.responseCount = 0
    this.timeInRedis = 0
    this.lastBatch = null
  }
 
  startBatch(commands) {
    const batch = new BatchStats({ commands })
    this.batches.add(batch)
    this.batchCount++
    this.commandCount += batch.commandCount
    return batch
  }
 
  endBatch(batch: BatchStats, error: Error|null, response: Array<any>) {
    batch.finish(error, response)
    this.timeInRedis += batch.timeInRedis
    this.responseCount += batch.responseCount
    this.lastBatch = batch
    this.batches.delete(batch)
  }
 
  toJSON() {
    const {
      batches,
      batchCount,
      commandCount,
      responseCount,
      timeInRedis,
      lastBatch
    } = this
    return {
      batches: Array.from(batches),
      batchCount,
      commandCount,
      responseCount,
      timeInRedis,
      lastBatch
    }
  }
}