From dd95c47113653d33cbe633012ac6ae082c56f188 Mon Sep 17 00:00:00 2001 From: ssuchichan Date: Sun, 30 Jun 2024 17:00:01 +0800 Subject: [PATCH] optimize api. --- explorer/src/service/v2/asset.rs | 2 +- explorer/src/service/v2/block.rs | 2 +- explorer/src/service/v2/claim.rs | 4 ++-- explorer/src/service/v2/delegation.rs | 4 ++-- explorer/src/service/v2/other.rs | 20 +++++++++---------- .../src/service/v2/prism_evm_to_native.rs | 2 +- .../src/service/v2/prism_native_to_evm.rs | 2 +- explorer/src/service/v2/transaction.rs | 2 +- explorer/src/service/v2/undelegation.rs | 4 ++-- 9 files changed, 21 insertions(+), 21 deletions(-) diff --git a/explorer/src/service/v2/asset.rs b/explorer/src/service/v2/asset.rs index 3f72ac4..012b5bc 100644 --- a/explorer/src/service/v2/asset.rs +++ b/explorer/src/service/v2/asset.rs @@ -36,7 +36,7 @@ pub async fn get_assets( let page = params.page.unwrap_or(1); let page_size = params.page_size.unwrap_or(10); - let mut sql_total = "SELECT count(*) FROM assets ".to_string(); + let mut sql_total = "SELECT count(height) FROM assets ".to_string(); let mut sql_query = "SELECT asset,tx,block,issuer,height,timestamp,ty,content FROM assets ".to_string(); let mut query_params: Vec = vec![]; diff --git a/explorer/src/service/v2/block.rs b/explorer/src/service/v2/block.rs index ffa2cce..6c10790 100644 --- a/explorer/src/service/v2/block.rs +++ b/explorer/src/service/v2/block.rs @@ -233,7 +233,7 @@ pub async fn get_blocks( let sql_total = "SELECT max(height) FROM block"; let row = sqlx::query(sql_total).fetch_one(&mut *pool).await?; - let total = row.try_get("max")?; + let total = row.try_get("max").unwrap_or(10); let sql_query = r#"SELECT block_hash,height,size,tx_count,time,app_hash,proposer,block_data FROM block ORDER BY height DESC LIMIT $1 OFFSET $2"#; diff --git a/explorer/src/service/v2/claim.rs b/explorer/src/service/v2/claim.rs index 734e502..0c99974 100644 --- a/explorer/src/service/v2/claim.rs +++ b/explorer/src/service/v2/claim.rs @@ -78,14 +78,14 @@ pub async fn get_claims( let (sql_count, sql_query) = if let Some(from) = params.from { (format!( - "SELECT count(*) FROM claims WHERE sender='{}'", + "SELECT count(height) FROM claims WHERE sender='{}'", from.to_lowercase() ),format!( "SELECT tx,block,sender,amount,height,timestamp,content FROM claims WHERE sender='{}' ORDER BY timestamp DESC LIMIT {} OFFSET {}", from.to_lowercase(), page_size, (page-1)*page_size )) } else { - ("SELECT count(*) FROM claims".to_string(), + ("SELECT count(height) FROM claims".to_string(), format!( "SELECT tx,block,sender,amount,height,timestamp,content FROM claims ORDER BY timestamp DESC LIMIT {} OFFSET {}", page_size, (page-1)*page_size diff --git a/explorer/src/service/v2/delegation.rs b/explorer/src/service/v2/delegation.rs index 180009c..1e632e0 100644 --- a/explorer/src/service/v2/delegation.rs +++ b/explorer/src/service/v2/delegation.rs @@ -83,7 +83,7 @@ pub async fn get_delegations( let (sql_count, sql_query) = if let Some(addr) = params.from { ( format!( - "SELECT count(*) FROM delegations WHERE sender='{}'", + "SELECT count(height) FROM delegations WHERE sender='{}'", addr.to_lowercase() ), format!( @@ -96,7 +96,7 @@ pub async fn get_delegations( ) } else { ( - "SELECT count(*) FROM delegations".to_string(), + "SELECT count(height) FROM delegations".to_string(), format!( "SELECT tx,block,sender,amount,validator,new_validator,timestamp,height,content \ FROM delegations ORDER BY timestamp DESC LIMIT {} OFFSET {}", diff --git a/explorer/src/service/v2/other.rs b/explorer/src/service/v2/other.rs index 50dbd15..26f748a 100644 --- a/explorer/src/service/v2/other.rs +++ b/explorer/src/service/v2/other.rs @@ -39,7 +39,7 @@ pub async fn get_statistics( let start_time = Local::now().date_naive().and_hms_opt(0, 0, 0).unwrap(); if let Some(tx_type) = params.ty { - let sql_txs_count = format!("SELECT count(*) FROM transaction WHERE ty={}", tx_type); + let sql_txs_count = format!("SELECT count(height) FROM transaction WHERE ty={}", tx_type); let row_txs_count = sqlx::query(sql_txs_count.as_str()) .fetch_one(&mut *conn) .await?; @@ -51,14 +51,14 @@ pub async fn get_statistics( 0 => { sql_addrs_count = "SELECT count(distinct address) FROM native_addrs".to_string(); sql_daily_txs = format!( - "SELECT count(*) FROM transaction WHERE ty=0 AND timestamp>={}", + "SELECT count(height) FROM transaction WHERE ty=0 AND timestamp>={}", start_time.and_utc().timestamp() ); } _ => { sql_addrs_count = "SELECT count(distinct address) FROM evm_addrs".to_string(); sql_daily_txs = format!( - "SELECT count(*) FROM transaction WHERE ty=1 AND timestamp>={}", + "SELECT count(height) FROM transaction WHERE ty=1 AND timestamp>={}", start_time.and_utc().timestamp() ); } @@ -78,7 +78,7 @@ pub async fn get_statistics( stat.total_txs = txs_count; stat.daily_txs = daily_txs } else { - let sql_txs_count = "SELECT count(*) FROM transaction".to_string(); + let sql_txs_count = "SELECT count(height) FROM transaction".to_string(); let row_txs_count = sqlx::query(sql_txs_count.as_str()) .fetch_one(&mut *conn) .await?; @@ -97,7 +97,7 @@ pub async fn get_statistics( let native_addrs: i64 = row_native_addr.try_get("count")?; let sql_daily_txs = format!( - "SELECT count(*) FROM transaction WHERE timestamp>={}", + "SELECT count(height) FROM transaction WHERE timestamp>={}", start_time.and_utc().timestamp() ); let row_daily = sqlx::query(sql_daily_txs.as_str()) @@ -127,23 +127,23 @@ pub async fn get_tx_distribute( ) -> Result> { let mut conn = state.pool.acquire().await?; - let sql_native = "SELECT count(*) FROM transaction WHERE ty=0"; + let sql_native = "SELECT count(height) FROM transaction WHERE ty=0"; let row_native = sqlx::query(sql_native).fetch_one(&mut *conn).await?; let native_count: i64 = row_native.try_get("count")?; - let sql_privacy = "SELECT count(*) FROM transaction WHERE ty_sub=2 or ty_sub=3 or ty_sub=4"; + let sql_privacy = "SELECT count(height) FROM transaction WHERE ty_sub=2 or ty_sub=3 or ty_sub=4"; let row_privacy = sqlx::query(sql_privacy).fetch_one(&mut *conn).await?; let privacy: i64 = row_privacy.try_get("count")?; - let sql_evm = "SELECT count(*) FROM transaction WHERE ty=1"; + let sql_evm = "SELECT count(height) FROM transaction WHERE ty=1"; let row_evm = sqlx::query(sql_evm).fetch_one(&mut *conn).await?; let evm_count: i64 = row_evm.try_get("count")?; - let sql_prism_n2e = "SELECT count(*) FROM n2e"; + let sql_prism_n2e = "SELECT count(height) FROM n2e"; let row_n2e = sqlx::query(sql_prism_n2e).fetch_one(&mut *conn).await?; let n2e_count: i64 = row_n2e.try_get("count")?; - let sql_prism_e2n = "SELECT count(*) FROM e2n"; + let sql_prism_e2n = "SELECT count(height) FROM e2n"; let row_e2n = sqlx::query(sql_prism_e2n).fetch_one(&mut *conn).await?; let e2n_count: i64 = row_e2n.try_get("count")?; diff --git a/explorer/src/service/v2/prism_evm_to_native.rs b/explorer/src/service/v2/prism_evm_to_native.rs index bfc0690..061be9d 100644 --- a/explorer/src/service/v2/prism_evm_to_native.rs +++ b/explorer/src/service/v2/prism_evm_to_native.rs @@ -81,7 +81,7 @@ pub async fn get_e2n_txs( let page = params.page.unwrap_or(1); let page_size = params.page_size.unwrap_or(10); - let mut sql_total = "SELECT count(*) FROM e2n ".to_string(); + let mut sql_total = "SELECT count(height) FROM e2n ".to_string(); let mut sql_query = "SELECT tx_hash,block_hash,sender,receiver,asset,amount,decimal,height,timestamp,value FROM e2n ".to_string(); let mut query_params: Vec = vec![]; diff --git a/explorer/src/service/v2/prism_native_to_evm.rs b/explorer/src/service/v2/prism_native_to_evm.rs index 2bd9974..e051471 100644 --- a/explorer/src/service/v2/prism_native_to_evm.rs +++ b/explorer/src/service/v2/prism_native_to_evm.rs @@ -37,7 +37,7 @@ pub async fn get_n2e_txs( let mut conn = state.pool.acquire().await?; let page = params.page.unwrap_or(1); let page_size = params.page_size.unwrap_or(10); - let mut sql_total = "SELECT count(*) FROM n2e ".to_string(); + let mut sql_total = "SELECT count(height) FROM n2e ".to_string(); let mut sql_query = "SELECT tx,block,sender,receiver,asset,amount,height,timestamp,content FROM n2e " .to_string(); diff --git a/explorer/src/service/v2/transaction.rs b/explorer/src/service/v2/transaction.rs index fc1f299..704d30c 100644 --- a/explorer/src/service/v2/transaction.rs +++ b/explorer/src/service/v2/transaction.rs @@ -31,7 +31,7 @@ pub async fn get_txs( let page_size = params.page_size.unwrap_or(10); let mut sql_query = String::from("SELECT tx_hash,block_hash,ty,timestamp,height,code,log,origin,result,value FROM transaction "); - let mut sql_total = String::from("SELECT count(*) FROM transaction "); + let mut sql_total = String::from("SELECT count(height) FROM transaction "); let mut query_params: Vec = vec![]; if let Some(from) = params.from { query_params.push(format!("sender='{}' ", from)) diff --git a/explorer/src/service/v2/undelegation.rs b/explorer/src/service/v2/undelegation.rs index f4543f2..69539dd 100644 --- a/explorer/src/service/v2/undelegation.rs +++ b/explorer/src/service/v2/undelegation.rs @@ -82,7 +82,7 @@ pub async fn get_undelegations( let (sql_count, sql_query) = if let Some(from) = params.from { ( format!( - "SELECT count(*) FROM undelegations WHERE sender='{}'", + "SELECT count(height) FROM undelegations WHERE sender='{}'", from.to_lowercase() ), format!( @@ -92,7 +92,7 @@ pub async fn get_undelegations( ) } else { ( - "SELECT count(*) FROM undelegations".to_string(), + "SELECT count(height) FROM undelegations".to_string(), format!("SELECT tx,block,sender,amount,target_validator,new_delegator,height,timestamp,content \ FROM undelegations ORDER BY timestamp DESC LIMIT {} OFFSET {}", page_size, (page-1)*page_size) )