Skip to content

Commit 38d78fd

Browse files
authored
Fix the indexer metrics with nullable timestamp. (#510)
1 parent a553ebd commit 38d78fd

File tree

1 file changed

+84
-8
lines changed

1 file changed

+84
-8
lines changed

rust/indexer-metrics/src/main.rs

Lines changed: 84 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,9 @@ struct FullnodeResponse {
3131
#[derive(Debug, Deserialize, Serialize)]
3232
struct ProcessorStatus {
3333
processor: String,
34-
#[serde(deserialize_with = "deserialize_from_string")]
3534
last_updated: NaiveDateTime,
3635
last_success_version: u64,
37-
#[serde(deserialize_with = "deserialize_from_string")]
38-
last_transaction_timestamp: NaiveDateTime,
36+
last_transaction_timestamp: Option<NaiveDateTime>,
3937
}
4038

4139
#[derive(Debug, Deserialize, Serialize)]
@@ -150,6 +148,13 @@ async fn start_processor_status_fetch(url: String, chain_name: String) {
150148
// Process the data as needed
151149
let system_time_now = chrono::Utc::now().naive_utc();
152150
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+
};
153158
HASURA_API_LATEST_VERSION
154159
.with_label_values(&[&processor.processor, &chain_name])
155160
.set(processor.last_success_version as i64);
@@ -159,13 +164,10 @@ async fn start_processor_status_fetch(url: String, chain_name: String) {
159164
HASURA_API_LATEST_TRANSACTION_TIMESTAMP
160165
.with_label_values(&[&processor.processor, &chain_name])
161166
.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
166168
* 1e-6,
167169
);
168-
let latency = system_time_now - processor.last_transaction_timestamp;
170+
let latency = system_time_now - last_transaction_timestamp;
169171
HASURA_API_LATEST_TRANSACTION_LATENCY_IN_SECS
170172
.with_label_values(&[&processor.processor, &chain_name])
171173
.set(latency.num_milliseconds() as f64 * 1e-3);
@@ -195,3 +197,77 @@ async fn start_processor_status_fetch(url: String, chain_name: String) {
195197
}
196198
}
197199
}
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

Comments
 (0)