Skip to content

Commit

Permalink
optimize. (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
chinyuchan authored Jun 18, 2024
1 parent 6a28fde commit 05630b3
Showing 1 changed file with 16 additions and 11 deletions.
27 changes: 16 additions & 11 deletions indexer/src/validators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ use sqlx::Row;
use std::ops::{Add, Sub};
use std::sync::Arc;

const SQL_QUERY1: &'static str = "ORDER BY power DESC LIMIT 1 OFFSET 0";

#[derive(Serialize, Deserialize)]
pub struct ValidatorSumRewardParams {
pub validator: String,
Expand Down Expand Up @@ -125,7 +127,7 @@ pub async fn get_validators(
WHERE ev.block_num=(SELECT max(block_num) FROM evm_validators) ".to_string();

let mut query_params: Vec<String> = vec![];
if let Some(validator) = params.0.validator {
if let Some(ref validator) = params.0.validator {
query_params.push(format!("ev.validator='{}' ", validator));
}
if let Some(online) = params.0.online {
Expand All @@ -142,16 +144,19 @@ pub async fn get_validators(
.add("AND ")
.add(query_params.join("AND ").as_str());
}

sql_query = sql_query.add(
format!(
"ORDER BY power DESC LIMIT {} OFFSET {}",
page_size,
(page - 1) * page_size
)
.as_str(),
);

if params.0.validator.is_some() {
sql_query = sql_query.add(SQL_QUERY1);
} else {
sql_query = sql_query.add(
format!(
"ORDER BY power DESC LIMIT {} OFFSET {}",
page_size,
(page - 1) * page_size
)
.as_str(),
);
}
println!("{}", sql_query);
let row = sqlx::query(&sql_total).fetch_one(&mut *pool).await?;
let total: i64 = row.try_get("count")?;

Expand Down

0 comments on commit 05630b3

Please sign in to comment.