From 0bf201b22e5d223f0c7b9aa58022bc709c0b1ccf Mon Sep 17 00:00:00 2001 From: s1q1ch4n Date: Thu, 27 Jun 2024 13:48:08 +0800 Subject: [PATCH 1/2] optimize: query tx by time. --- docs/api.md | 16 +++++++++------- explorer/src/service/v2/transaction.rs | 8 ++++++++ 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/docs/api.md b/docs/api.md index 2387716..1d64c0c 100644 --- a/docs/api.md +++ b/docs/api.md @@ -97,13 +97,15 @@

1.2 获取交易列表

-| 参数 | 类型 | 必传 | 说明 | -|-----------|--------|----|------------| -| from | string | N | 发送者 | -| to | string | N | 接收者 | -| ty | number | N | 类型 | -| page | number | N | 页码,缺省值为1 | -| page_size | number | N | 页大小,缺省值为10 | +| 参数 | 类型 | 必传 | 说明 | +|------------|--------|----|------------| +| from | string | N | 发送者 | +| to | string | N | 接收者 | +| ty | number | N | 类型 | +| start_time | number | N | 开始时间戳 | +| end_time | number | N | 结束时间戳 | +| page | number | N | 页码,缺省值为1 | +| page_size | number | N | 页大小,缺省值为10 | * Request: `/api/txs?page=2&page_size=2` * Response: diff --git a/explorer/src/service/v2/transaction.rs b/explorer/src/service/v2/transaction.rs index eaf25db..9648b5a 100644 --- a/explorer/src/service/v2/transaction.rs +++ b/explorer/src/service/v2/transaction.rs @@ -16,6 +16,8 @@ pub struct GetTxsParams { pub from: Option, pub to: Option, pub ty: Option, + pub start_time: Option, + pub end_time: Option, pub page: Option, pub page_size: Option, } @@ -40,6 +42,12 @@ pub async fn get_txs( if let Some(ty) = params.ty { query_params.push(format!("ty={} ", ty)); } + if let Some(start_time) = params.start_time { + query_params.push(format!("timestamp >= {} ", start_time)); + } + if let Some(end_time) = params.end_time { + query_params.push(format!("timestamp <={} ", end_time)); + } if !query_params.is_empty() { sql_query = sql_query .add("WHERE ") From b5956977b0c34fcbe946230e95efe76abe1f21c3 Mon Sep 17 00:00:00 2001 From: s1q1ch4n Date: Thu, 27 Jun 2024 13:49:41 +0800 Subject: [PATCH 2/2] optimize. --- explorer/src/service/v2/transaction.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/explorer/src/service/v2/transaction.rs b/explorer/src/service/v2/transaction.rs index 9648b5a..fc1f299 100644 --- a/explorer/src/service/v2/transaction.rs +++ b/explorer/src/service/v2/transaction.rs @@ -43,10 +43,10 @@ pub async fn get_txs( query_params.push(format!("ty={} ", ty)); } if let Some(start_time) = params.start_time { - query_params.push(format!("timestamp >= {} ", start_time)); + query_params.push(format!("timestamp>={} ", start_time)); } if let Some(end_time) = params.end_time { - query_params.push(format!("timestamp <={} ", end_time)); + query_params.push(format!("timestamp<={} ", end_time)); } if !query_params.is_empty() { sql_query = sql_query