1
1
import { Static , Type } from "@sinclair/typebox" ;
2
2
import { FastifyInstance } from "fastify" ;
3
3
import { StatusCodes } from "http-status-codes" ;
4
+ import { prisma } from "../../../db/client" ;
4
5
import { getQueueStatus } from "../../../db/transactions/getQueueStatus" ;
6
+ import { getPercentile } from "../../../utils/math" ;
5
7
import { standardResponseSchema } from "../../schemas/sharedApiSchemas" ;
6
8
7
9
const QuerySchema = Type . Object ( {
@@ -12,6 +14,16 @@ const responseBodySchema = Type.Object({
12
14
result : Type . Object ( {
13
15
queued : Type . Number ( ) ,
14
16
pending : Type . Number ( ) ,
17
+ latency : Type . Object ( {
18
+ msToSend : Type . Object ( {
19
+ p50 : Type . Number ( ) ,
20
+ p90 : Type . Number ( ) ,
21
+ } ) ,
22
+ msToMine : Type . Object ( {
23
+ p50 : Type . Number ( ) ,
24
+ p90 : Type . Number ( ) ,
25
+ } ) ,
26
+ } ) ,
15
27
} ) ,
16
28
} ) ;
17
29
@@ -37,11 +49,46 @@ export async function queueStatus(fastify: FastifyInstance) {
37
49
handler : async ( req , res ) => {
38
50
const { walletAddress } = req . query ;
39
51
52
+ // Get # queued and sent transactions.
40
53
const { queued, pending } = await getQueueStatus ( { walletAddress } ) ;
54
+
55
+ // Get last 1k sent transactions.
56
+ const recentTransactions = await prisma . transactions . findMany ( {
57
+ orderBy : { queuedAt : "desc" } ,
58
+ where : { sentAt : { not : null } } ,
59
+ take : 1_000 ,
60
+ } ) ;
61
+
62
+ // Get "queue -> send" and "queue -> mine" times.
63
+ const msToSendArr : number [ ] = [ ] ;
64
+ const msToMineArr : number [ ] = [ ] ;
65
+ for ( const transaction of recentTransactions ) {
66
+ const queuedAt = transaction . queuedAt . getTime ( ) ;
67
+ const sentAt = transaction . sentAt ?. getTime ( ) ;
68
+ const minedAt = transaction . minedAt ?. getTime ( ) ;
69
+
70
+ if ( sentAt ) {
71
+ msToSendArr . push ( sentAt - queuedAt ) ;
72
+ }
73
+ if ( minedAt ) {
74
+ msToMineArr . push ( minedAt - queuedAt ) ;
75
+ }
76
+ }
77
+
41
78
res . status ( StatusCodes . OK ) . send ( {
42
79
result : {
43
80
queued,
44
81
pending,
82
+ latency : {
83
+ msToSend : {
84
+ p50 : getPercentile ( msToSendArr , 50 ) ,
85
+ p90 : getPercentile ( msToSendArr , 90 ) ,
86
+ } ,
87
+ msToMine : {
88
+ p50 : getPercentile ( msToMineArr , 50 ) ,
89
+ p90 : getPercentile ( msToMineArr , 90 ) ,
90
+ } ,
91
+ } ,
45
92
} ,
46
93
} ) ;
47
94
} ,
0 commit comments