@@ -31,11 +31,9 @@ struct FullnodeResponse {
31
31
#[ derive( Debug , Deserialize , Serialize ) ]
32
32
struct ProcessorStatus {
33
33
processor : String ,
34
- #[ serde( deserialize_with = "deserialize_from_string" ) ]
35
34
last_updated : NaiveDateTime ,
36
35
last_success_version : u64 ,
37
- #[ serde( deserialize_with = "deserialize_from_string" ) ]
38
- last_transaction_timestamp : NaiveDateTime ,
36
+ last_transaction_timestamp : Option < NaiveDateTime > ,
39
37
}
40
38
41
39
#[ derive( Debug , Deserialize , Serialize ) ]
@@ -150,6 +148,13 @@ async fn start_processor_status_fetch(url: String, chain_name: String) {
150
148
// Process the data as needed
151
149
let system_time_now = chrono:: Utc :: now ( ) . naive_utc ( ) ;
152
150
for processor in resp. data . processor_status {
151
+ // If the last_transaction_timestamp is None, then the processor has not processed any transactions.
152
+ // Skip.
153
+ let last_transaction_timestamp = match processor. last_transaction_timestamp
154
+ {
155
+ Some ( timestamp) => timestamp,
156
+ None => continue ,
157
+ } ;
153
158
HASURA_API_LATEST_VERSION
154
159
. with_label_values ( & [ & processor. processor , & chain_name] )
155
160
. set ( processor. last_success_version as i64 ) ;
@@ -159,13 +164,10 @@ async fn start_processor_status_fetch(url: String, chain_name: String) {
159
164
HASURA_API_LATEST_TRANSACTION_TIMESTAMP
160
165
. with_label_values ( & [ & processor. processor , & chain_name] )
161
166
. set (
162
- processor
163
- . last_transaction_timestamp
164
- . and_utc ( )
165
- . timestamp_micros ( ) as f64
167
+ last_transaction_timestamp. and_utc ( ) . timestamp_micros ( ) as f64
166
168
* 1e-6 ,
167
169
) ;
168
- let latency = system_time_now - processor . last_transaction_timestamp ;
170
+ let latency = system_time_now - last_transaction_timestamp;
169
171
HASURA_API_LATEST_TRANSACTION_LATENCY_IN_SECS
170
172
. with_label_values ( & [ & processor. processor , & chain_name] )
171
173
. set ( latency. num_milliseconds ( ) as f64 * 1e-3 ) ;
@@ -195,3 +197,77 @@ async fn start_processor_status_fetch(url: String, chain_name: String) {
195
197
}
196
198
}
197
199
}
200
+
201
+ #[ cfg( test) ]
202
+ mod test {
203
+ use super :: * ;
204
+
205
+ #[ tokio:: test]
206
+ async fn test_response_parsing ( ) {
207
+ let response_str = r#"
208
+ {
209
+ "data": {
210
+ "processor_status": [
211
+ {
212
+ "processor": "token_processor",
213
+ "last_updated": "2024-07-02T17:23:50.47637",
214
+ "last_success_version": 1010349813,
215
+ "last_transaction_timestamp": "2024-07-02T17:23:49.595574"
216
+ }
217
+ ]
218
+ }
219
+ }
220
+ "# ;
221
+ let resp: ProcessorsResponse = serde_json:: from_str ( response_str) . unwrap ( ) ;
222
+ assert_eq ! ( resp. data. processor_status. len( ) , 1 ) ;
223
+ assert ! ( resp. data. processor_status[ 0 ]
224
+ . last_transaction_timestamp
225
+ . is_some( ) ) ;
226
+ assert_eq ! ( resp. data. processor_status[ 0 ] . processor, "token_processor" ) ;
227
+ let expected_last_updated =
228
+ NaiveDateTime :: parse_from_str ( "2024-07-02T17:23:50.47637" , "%Y-%m-%dT%H:%M:%S%.f" )
229
+ . unwrap ( ) ;
230
+ assert_eq ! (
231
+ resp. data. processor_status[ 0 ] . last_updated,
232
+ expected_last_updated
233
+ ) ;
234
+ let timestamp = resp. data . processor_status [ 0 ]
235
+ . last_transaction_timestamp
236
+ . unwrap ( ) ;
237
+ let actual_datetime =
238
+ NaiveDateTime :: parse_from_str ( "2024-07-02T17:23:49.595574" , "%Y-%m-%dT%H:%M:%S%.f" )
239
+ . unwrap ( ) ;
240
+ assert_eq ! ( timestamp, actual_datetime) ;
241
+ }
242
+
243
+ #[ tokio:: test]
244
+ async fn test_response_parsing_with_null ( ) {
245
+ let response_str = r#"
246
+ {
247
+ "data": {
248
+ "processor_status": [
249
+ {
250
+ "processor": "token_processor",
251
+ "last_updated": "2024-07-02T17:23:50.47637",
252
+ "last_success_version": 1010349813,
253
+ "last_transaction_timestamp": null
254
+ }
255
+ ]
256
+ }
257
+ }
258
+ "# ;
259
+ let resp: ProcessorsResponse = serde_json:: from_str ( response_str) . unwrap ( ) ;
260
+ assert_eq ! ( resp. data. processor_status. len( ) , 1 ) ;
261
+ assert_eq ! ( resp. data. processor_status[ 0 ] . processor, "token_processor" ) ;
262
+ let expected_last_updated =
263
+ NaiveDateTime :: parse_from_str ( "2024-07-02T17:23:50.47637" , "%Y-%m-%dT%H:%M:%S%.f" )
264
+ . unwrap ( ) ;
265
+ assert_eq ! (
266
+ resp. data. processor_status[ 0 ] . last_updated,
267
+ expected_last_updated
268
+ ) ;
269
+ assert ! ( resp. data. processor_status[ 0 ]
270
+ . last_transaction_timestamp
271
+ . is_none( ) ) ;
272
+ }
273
+ }
0 commit comments