Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

optimize v2 claims. #240

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion explorer/src/service/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -845,7 +845,7 @@ impl Api {
#[oai(path = "/v2/staking/claims", method = "get", tag = "ApiTags::Staking")]
async fn v2_get_claims(
&self,
address: Query<String>,
address: Query<Option<String>>,
page: Query<Option<i64>>,
page_size: Query<Option<i64>>,
) -> poem::Result<V2ClaimsResponse> {
Expand Down
29 changes: 18 additions & 11 deletions explorer/src/service/v2/claim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,25 +96,32 @@ pub struct V2ClaimsData {

pub async fn v2_get_claims(
api: &Api,
address: Query<String>,
address: Query<Option<String>>,
page: Query<Option<i64>>,
page_size: Query<Option<i64>>,
) -> Result<V2ClaimsResponse> {
let page = page.0.unwrap_or(1);
let page_size = page_size.0.unwrap_or(10);
let mut conn = api.storage.lock().await.acquire().await?;
let sql_count = format!(
"SELECT count(*) AS cnt FROM claims WHERE sender='{}'",
address.0.to_lowercase()
);
let row_cnt = sqlx::query(sql_count.as_str()).fetch_one(&mut conn).await?;
let total: i64 = row_cnt.try_get("cnt")?;

let sql_query = format!(
"SELECT tx,block,sender,amount,height,timestamp,content FROM claims WHERE sender='{}' ORDER BY timestamp DESC LIMIT {} OFFSET {}",
address.0.to_lowercase(), page_size, (page-1)*page_size
);
let (sql_count, sql_query) = if let Some(addr) = address.0 {
(format!(
"SELECT count(*) AS cnt FROM claims WHERE sender='{}'",
addr.to_lowercase()
),format!(
"SELECT tx,block,sender,amount,height,timestamp,content FROM claims WHERE sender='{}' ORDER BY timestamp DESC LIMIT {} OFFSET {}",
addr.to_lowercase(), page_size, (page-1)*page_size
))
} else {
("SELECT count(*) AS cnt 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
))
};

let row_cnt = sqlx::query(sql_count.as_str()).fetch_one(&mut conn).await?;
let total: i64 = row_cnt.try_get("cnt")?;
let mut res: Vec<V2Claim> = vec![];
let rows = sqlx::query(sql_query.as_str()).fetch_all(&mut conn).await?;
for row in rows {
Expand Down
Loading