13
13
order:: {
14
14
Order ,
15
15
OrderCancellation ,
16
- OrderClass ,
17
16
OrderCreation ,
18
17
OrderCreationAppData ,
19
18
OrderStatus ,
28
27
shared:: {
29
28
metrics:: LivenessChecking ,
30
29
order_quoting:: Quote ,
31
- order_validation:: { OrderValidating , ValidationError } ,
30
+ order_validation:: {
31
+ is_order_outside_market_price,
32
+ Amounts ,
33
+ OrderValidating ,
34
+ ValidationError ,
35
+ } ,
32
36
} ,
33
37
std:: { borrow:: Cow , sync:: Arc } ,
38
+ strum_macros:: Display ,
34
39
thiserror:: Error ,
35
40
} ;
36
41
@@ -42,24 +47,18 @@ struct Metrics {
42
47
orders : prometheus:: IntCounterVec ,
43
48
}
44
49
50
+ #[ derive( Display ) ]
51
+ #[ strum( serialize_all = "snake_case" ) ]
45
52
enum OrderOperation {
46
53
Created ,
47
54
Cancelled ,
48
55
}
49
56
50
- fn operation_label ( op : & OrderOperation ) -> & ' static str {
51
- match op {
52
- OrderOperation :: Created => "created" ,
53
- OrderOperation :: Cancelled => "cancelled" ,
54
- }
55
- }
56
-
57
- fn order_class_label ( class : & OrderClass ) -> & ' static str {
58
- match class {
59
- OrderClass :: Market => "user" ,
60
- OrderClass :: Liquidity => "liquidity" ,
61
- OrderClass :: Limit => "limit" ,
62
- }
57
+ #[ derive( Display ) ]
58
+ #[ strum( serialize_all = "snake_case" ) ]
59
+ enum OrderClass {
60
+ User ,
61
+ Limit ,
63
62
}
64
63
65
64
impl Metrics {
@@ -68,20 +67,42 @@ impl Metrics {
68
67
. expect ( "unexpected error getting metrics instance" )
69
68
}
70
69
71
- fn on_order_operation ( order : & Order , operation : OrderOperation ) {
72
- let class = order_class_label ( & order. metadata . class ) ;
73
- let op = operation_label ( & operation) ;
74
- Self :: get ( ) . orders . with_label_values ( & [ class, op] ) . inc ( ) ;
70
+ fn on_order_operation ( order : & Order , operation : OrderOperation , quote : Option < & Quote > ) {
71
+ let class = match quote {
72
+ Some ( ref quote) if
73
+ // Check if the order at the submission time was billable (`user` order)
74
+ is_order_outside_market_price (
75
+ & Amounts {
76
+ sell : order. data . sell_amount ,
77
+ buy : order. data . buy_amount ,
78
+ fee : order. data . fee_amount ,
79
+ } ,
80
+ & Amounts {
81
+ sell : quote. sell_amount ,
82
+ buy : quote. buy_amount ,
83
+ fee : quote. fee_amount ,
84
+ } ,
85
+ order. data . kind ) =>
86
+ {
87
+ OrderClass :: User
88
+ }
89
+ _ => OrderClass :: Limit ,
90
+ } ;
91
+ Self :: get ( )
92
+ . orders
93
+ . with_label_values ( & [ & class. to_string ( ) , & operation. to_string ( ) ] )
94
+ . inc ( ) ;
75
95
}
76
96
77
97
// Resets all the counters to 0 so we can always use them in Grafana queries.
78
98
fn initialize ( ) {
79
99
let metrics = Self :: get ( ) ;
80
100
for op in & [ OrderOperation :: Created , OrderOperation :: Cancelled ] {
81
- let op = operation_label ( op) ;
82
- for class in & [ OrderClass :: Market , OrderClass :: Liquidity , OrderClass :: Limit ] {
83
- let class = order_class_label ( class) ;
84
- metrics. orders . with_label_values ( & [ class, op] ) . reset ( ) ;
101
+ for class in & [ OrderClass :: User , OrderClass :: Limit ] {
102
+ metrics
103
+ . orders
104
+ . with_label_values ( & [ & class. to_string ( ) , & op. to_string ( ) ] )
105
+ . reset ( ) ;
85
106
}
86
107
}
87
108
}
@@ -220,10 +241,10 @@ impl Orderbook {
220
241
let quote_id = quote. as_ref ( ) . and_then ( |quote| quote. id ) ;
221
242
222
243
self . database
223
- . insert_order ( & order, quote)
244
+ . insert_order ( & order, quote. clone ( ) )
224
245
. await
225
246
. map_err ( |err| AddOrderError :: from_insertion ( err, & order) ) ?;
226
- Metrics :: on_order_operation ( & order, OrderOperation :: Created ) ;
247
+ Metrics :: on_order_operation ( & order, OrderOperation :: Created , quote . as_ref ( ) ) ;
227
248
228
249
Ok ( ( order. metadata . uid , quote_id) )
229
250
}
@@ -281,7 +302,7 @@ impl Orderbook {
281
302
282
303
for order in & orders {
283
304
tracing:: debug!( order_uid =% order. metadata. uid, "order cancelled" ) ;
284
- Metrics :: on_order_operation ( order, OrderOperation :: Cancelled ) ;
305
+ Metrics :: on_order_operation ( order, OrderOperation :: Cancelled , None ) ;
285
306
}
286
307
287
308
Ok ( ( ) )
@@ -310,7 +331,7 @@ impl Orderbook {
310
331
. await ?;
311
332
312
333
tracing:: debug!( order_uid =% order. metadata. uid, "order cancelled" ) ;
313
- Metrics :: on_order_operation ( & order, OrderOperation :: Cancelled ) ;
334
+ Metrics :: on_order_operation ( & order, OrderOperation :: Cancelled , None ) ;
314
335
315
336
Ok ( ( ) )
316
337
}
@@ -367,11 +388,15 @@ impl Orderbook {
367
388
let quote_id = quote. as_ref ( ) . and_then ( |quote| quote. id ) ;
368
389
369
390
self . database
370
- . replace_order ( & old_order. metadata . uid , & validated_new_order, quote)
391
+ . replace_order ( & old_order. metadata . uid , & validated_new_order, quote. clone ( ) )
371
392
. await
372
393
. map_err ( |err| AddOrderError :: from_insertion ( err, & validated_new_order) ) ?;
373
- Metrics :: on_order_operation ( & old_order, OrderOperation :: Cancelled ) ;
374
- Metrics :: on_order_operation ( & validated_new_order, OrderOperation :: Created ) ;
394
+ Metrics :: on_order_operation ( & old_order, OrderOperation :: Cancelled , quote. as_ref ( ) ) ;
395
+ Metrics :: on_order_operation (
396
+ & validated_new_order,
397
+ OrderOperation :: Created ,
398
+ quote. as_ref ( ) ,
399
+ ) ;
375
400
376
401
Ok ( ( validated_new_order. metadata . uid , quote_id) )
377
402
}
0 commit comments