1 2
import * as IlpPacket from 'ilp-packet'
2
import {
3
  Middleware,
4
  MiddlewareCallback,
5
  MiddlewareServices,
6
  Pipelines
7
} from '../types/middleware'
8
import Stats from '../services/stats'
9
import { AccountInfo } from '../types/accounts'
10

11 2
export default class StatsMiddleware implements Middleware {
12
  private stats: Stats
13

14
  private getInfo: (accountId: string) => AccountInfo
15

16
  constructor (opts: {}, { stats, getInfo }: MiddlewareServices) {
17 2
    this.stats = stats
18 2
    this.getInfo = getInfo
19
  }
20

21
  async applyToPipelines (pipelines: Pipelines, accountId: string) {
22 2
    const accountInfo = this.getInfo(accountId)
23 2
    if (!accountInfo) {
24 0
      throw new Error('could not load info for account. accountId=' + accountId)
25
    }
26 2
    const account = { accountId, accountInfo }
27 2
    pipelines.incomingData.insertLast({
28
      name: 'stats',
29
      method: async (data: Buffer, next: MiddlewareCallback<Buffer, Buffer>) => {
30 2
        try {
31 2
          const result = await next(data)
32 2
          if (result[0] === IlpPacket.Type.TYPE_ILP_FULFILL) {
33 2
            this.stats.incomingDataPackets.increment(account, { result: 'fulfilled' })
34
          } else {
35 2
            this.stats.incomingDataPackets.increment(account, { result: 'rejected' })
36
          }
37 2
          return result
38
        } catch (err) {
39 2
          this.stats.incomingDataPackets.increment(account, { result: 'failed' })
40 2
          throw err
41
        }
42
      }
43
    })
44

45 2
    pipelines.incomingMoney.insertLast({
46
      name: 'stats',
47
      method: async (amount: string, next: MiddlewareCallback<string, void>) => {
48 0
        try {
49 0
          const result = await next(amount)
50 0
          this.stats.incomingMoney.setValue(account, { result: 'succeeded' }, +amount)
51 0
          return result
52
        } catch (err) {
53 0
          this.stats.incomingMoney.setValue(account, { result: 'failed' }, +amount)
54 0
          throw err
55
        }
56
      }
57
    })
58

59 2
    pipelines.outgoingData.insertLast({
60
      name: 'stats',
61
      method: async (data: Buffer, next: MiddlewareCallback<Buffer, Buffer>) => {
62 2
        try {
63 2
          const result = await next(data)
64 2
          if (result[0] === IlpPacket.Type.TYPE_ILP_FULFILL) {
65 2
            this.stats.outgoingDataPackets.increment(account, { result: 'fulfilled' })
66
          } else {
67 2
            const rejectPacket = IlpPacket.deserializeIlpReject(result)
68 2
            const { code } = rejectPacket
69 2
            this.stats.outgoingDataPackets.increment(account,
70
              { result: 'rejected', code })
71
          }
72 2
          return result
73
        } catch (err) {
74 2
          this.stats.outgoingDataPackets.increment(account, { result: 'failed' })
75 2
          throw err
76
        }
77
      }
78
    })
79

80 2
    pipelines.outgoingMoney.insertLast({
81
      name: 'stats',
82
      method: async (amount: string, next: MiddlewareCallback<string, void>) => {
83 0
        try {
84 0
          const result = await next(amount)
85 0
          this.stats.outgoingMoney.setValue(account, { result: 'succeeded' }, +amount)
86 0
          return result
87
        } catch (err) {
88 0
          this.stats.outgoingMoney.setValue(account, { result: 'failed' }, +amount)
89 0
          throw err
90
        }
91
      }
92
    })
93
  }
94
}

Read our documentation on viewing source code .

Loading