Skip to content

Commit

Permalink
small improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Tguntenaar committed Dec 3, 2024
1 parent 700f59e commit da3af23
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/api_background_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ impl ApiBackgroundService {
contract.parse::<AccountId>().unwrap(),
None,
Some(50),
None,
Some("asc".to_string()),
Some(1),
)
.await
Expand Down
23 changes: 21 additions & 2 deletions src/db/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,23 @@ impl DB {
Ok(())
}

pub async fn remove_all_data(&self) -> anyhow::Result<()> {
sqlx::query!(r#"DELETE FROM proposals"#)
.execute(&self.0)
.await?;

sqlx::query!(r#"DELETE FROM rfps"#).execute(&self.0).await?;

sqlx::query!(r#"DELETE FROM proposal_snapshots"#)
.execute(&self.0)
.await?;

sqlx::query!(r#"DELETE FROM rfp_snapshots"#)
.execute(&self.0)
.await?;
Ok(())
}

pub async fn insert_rfp_snapshot(
tx: &mut Transaction<'static, Postgres>,
snapshot: &RfpSnapshotRecord,
Expand Down Expand Up @@ -642,10 +659,10 @@ impl DB {
AND ($5 IS NULL OR ps.timeline::text ~ $5)
AND ($6 IS NULL OR ps.category = $6)
AND ($7 IS NULL OR ps.labels::jsonb ?| $7)
ORDER BY {order}
ORDER BY {}
LIMIT $1 OFFSET $2
"#,
order = order_clause,
order_clause,
);

// Build the SQL query for counting total records
Expand Down Expand Up @@ -803,6 +820,8 @@ impl DB {
proposal_snapshots proposal
WHERE
proposal.proposal_id = $1
ORDER BY
proposal.ts DESC
"#;

// Execute the data query
Expand Down
11 changes: 9 additions & 2 deletions src/entrypoints/proposal/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,12 +161,20 @@ async fn set_timestamp(block_height: i64, db: &State<DB>) -> Result<(), Status>
// TODO remove after testing
#[get("/info/clean")]
async fn clean(db: &State<DB>) -> Result<(), Status> {
match db.remove_all_snapshots().await {
let _ = match db.remove_all_snapshots().await {
Ok(()) => Ok(()),
Err(e) => {
eprintln!("Error cleaning snapshots: {:?}", e);
Err(Status::InternalServerError)
}
};

match db.remove_all_data().await {
Ok(()) => Ok(()),
Err(e) => {
eprintln!("Error cleaning data: {:?}", e);
Err(Status::InternalServerError)
}
}
}

Expand All @@ -183,7 +191,6 @@ async fn get_proposal(
contract: &State<AccountId>,
) -> Result<Json<VersionedProposal>, rocket::http::Status> {
let rpc_service = RpcService::new(contract.inner().clone());
// We should cache this in the future
// We should also add rate limiting to this endpoint
match rpc_service.get_proposal(proposal_id).await {
Ok(proposal) => Ok(Json(proposal.data)),
Expand Down
6 changes: 3 additions & 3 deletions src/nearblocks_client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,16 @@ impl ApiClient {
pub async fn get_account_txns_by_pagination(
&self,
account_id: AccountId,
since_block: Option<i64>,
after_block: Option<i64>,
limit: Option<i32>,
order: Option<String>,
page: Option<i32>,
) -> Result<ApiResponse, reqwest::Error> {
let query_params = format!(
"?after_block={}&per_page={}&order={}&page={}",
since_block.unwrap_or(0),
after_block.unwrap_or(0),
limit.unwrap_or(50),
order.unwrap_or("desc".to_string()),
order.unwrap_or("asc".to_string()),
page.unwrap_or(1)
);
let endpoint = format!("v1/account/{}/txns", account_id);
Expand Down
5 changes: 2 additions & 3 deletions src/rpc_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ impl RpcService {
&self,
proposal_id: i32,
) -> Result<Data<VersionedProposal>, near_api::errors::QueryError<RpcQueryRequest>> {
// TODO get cached proposal
let result: Result<Data<VersionedProposal>, _> = self
.contract
.call_function("get_proposal", json!({ "proposal_id": proposal_id }))
Expand Down Expand Up @@ -121,8 +120,8 @@ impl RpcService {
// TODO caught error:
// Failed to get proposal on block: DeserializeError(Error("missing field `kyc_verified`", line: 0, column: 0))
eprintln!(
"Failed to get proposal on block: {:?}, block_id: {}",
e, block_id
"Failed to get proposal on block: {:?}, Error: {:?}",
block_id, e
);
Err(Status::InternalServerError)
}
Expand Down

0 comments on commit da3af23

Please sign in to comment.