1
1
import { Static , Type } from "@sinclair/typebox" ;
2
2
import { FastifyInstance } from "fastify" ;
3
3
import { StatusCodes } from "http-status-codes" ;
4
+ import { TransactionDB } from "../../../db/transactions/db" ;
5
+ import { MinedTransaction } from "../../../utils/transaction/types" ;
4
6
import { MineTransactionQueue } from "../../../worker/queues/mineTransactionQueue" ;
5
7
import { SendTransactionQueue } from "../../../worker/queues/sendTransactionQueue" ;
6
8
import { standardResponseSchema } from "../../schemas/sharedApiSchemas" ;
@@ -9,6 +11,16 @@ const responseBodySchema = Type.Object({
9
11
result : Type . Object ( {
10
12
queued : Type . Number ( ) ,
11
13
pending : Type . Number ( ) ,
14
+ latency : Type . Object ( {
15
+ msToSend : Type . Object ( {
16
+ p50 : Type . Number ( ) ,
17
+ p90 : Type . Number ( ) ,
18
+ } ) ,
19
+ msToMine : Type . Object ( {
20
+ p50 : Type . Number ( ) ,
21
+ p90 : Type . Number ( ) ,
22
+ } ) ,
23
+ } ) ,
12
24
} ) ,
13
25
} ) ;
14
26
@@ -30,13 +42,45 @@ export async function queueStatus(fastify: FastifyInstance) {
30
42
} ,
31
43
} ,
32
44
handler : async ( req , res ) => {
45
+ // Get # queued and sent transactions.
33
46
const queued = await SendTransactionQueue . length ( ) ;
34
47
const pending = await MineTransactionQueue . length ( ) ;
35
48
49
+ // Get last 1k mined transactions.
50
+ const minedTransactionsList =
51
+ await TransactionDB . getTransactionListByStatus ( {
52
+ status : "mined" ,
53
+ page : 1 ,
54
+ limit : 1_000 ,
55
+ } ) ;
56
+ const minedTransactions =
57
+ minedTransactionsList . transactions as MinedTransaction [ ] ;
58
+
59
+ // Get "queue -> send" and "queue -> mine" times.
60
+ const msToSendArr : number [ ] = [ ] ;
61
+ const msToMineArr : number [ ] = [ ] ;
62
+ for ( const transaction of minedTransactions ) {
63
+ const queuedAt = transaction . queuedAt . getTime ( ) ;
64
+ const sentAt = transaction . sentAt . getTime ( ) ;
65
+ const minedAt = transaction . minedAt . getTime ( ) ;
66
+ msToSendArr . push ( sentAt - queuedAt ) ;
67
+ msToMineArr . push ( minedAt - queuedAt ) ;
68
+ }
69
+
36
70
res . status ( StatusCodes . OK ) . send ( {
37
71
result : {
38
72
queued,
39
73
pending,
74
+ latency : {
75
+ msToSend : {
76
+ p50 : getPercentile ( msToSendArr , 50 ) ,
77
+ p90 : getPercentile ( msToSendArr , 90 ) ,
78
+ } ,
79
+ msToMine : {
80
+ p50 : getPercentile ( msToMineArr , 50 ) ,
81
+ p90 : getPercentile ( msToMineArr , 90 ) ,
82
+ } ,
83
+ } ,
40
84
} ,
41
85
} ) ;
42
86
} ,
0 commit comments