Skip to content

Commit

Permalink
refac: /proposals/ + /rfps/
Browse files Browse the repository at this point in the history
  • Loading branch information
Tguntenaar committed Nov 21, 2024
1 parent 02e0be1 commit c5dc5e6
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 160 deletions.
129 changes: 50 additions & 79 deletions src/entrypoints/proposal/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use self::proposal_types::*;
use crate::db::db_types::{ProposalSnapshotRecord, ProposalWithLatestSnapshotView};
use crate::db::DB;
use crate::nearblocks_client::transactions::update_nearblocks_data;
use crate::rpc_service::RpcService;
use crate::separate_number_and_text;
use crate::types::PaginatedResponse;
use crate::{nearblocks_client, separate_number_and_text};
use devhub_shared::proposal::VersionedProposal;
use near_account_id::AccountId;
use rocket::delete;
Expand Down Expand Up @@ -49,6 +50,25 @@ async fn search(
}
}

async fn fetch_proposals(
db: &DB,
limit: i64,
order: &str,
offset: i64,
filters: Option<GetProposalFilters>,
) -> (Vec<ProposalWithLatestSnapshotView>, i64) {
match db
.get_proposals_with_latest_snapshot(limit, order, offset, filters)
.await
{
Err(e) => {
eprintln!("Failed to get proposals: {:?}", e);
(vec![], 0)
}
Ok(result) => result,
}
}

#[utoipa::path(get, path = "/proposals?<order>&<limit>&<offset>&<filters>", params(
("order"= &str, Path, description ="default order id_desc (ts_asc)"),
("limit"= i64, Path, description = "default limit 10"),
Expand All @@ -65,92 +85,26 @@ async fn get_proposals(
contract: &State<AccountId>,
nearblocks_api_key: &State<String>,
) -> Option<Json<PaginatedResponse<ProposalWithLatestSnapshotView>>> {
let current_timestamp_nano = chrono::Utc::now().timestamp_nanos_opt().unwrap();
let last_updated_info = db.get_last_updated_info().await.unwrap();

let order = order.unwrap_or("id_desc");
let limit = limit.unwrap_or(10);
let offset = offset.unwrap_or(0);

let current_timestamp_nano = chrono::Utc::now().timestamp_nanos_opt().unwrap();
let last_updated_info = db.get_last_updated_info().await.unwrap();

if current_timestamp_nano - last_updated_info.0
< chrono::Duration::seconds(60).num_nanoseconds().unwrap()
>= chrono::Duration::seconds(60).num_nanoseconds().unwrap()
{
println!("Returning cached proposals");
let (proposals, total) = match db
.get_proposals_with_latest_snapshot(limit, order, offset, filters)
.await
{
Err(e) => {
eprintln!("Failed to get proposals: {:?}", e);
(vec![], 0)
}
Ok(result) => result,
};

return Some(Json(PaginatedResponse::new(
proposals.into_iter().map(Into::into).collect(),
1,
limit.try_into().unwrap(),
total.try_into().unwrap(),
)));
}

println!("Fetching not yet indexed method calls from nearblocks");

let nearblocks_client = nearblocks_client::ApiClient::new(nearblocks_api_key.inner().clone());

let nearblocks_unwrapped = match nearblocks_client
.get_account_txns_by_pagination(
contract.inner().clone(),
Some(last_updated_info.1),
Some(50),
Some("asc".to_string()),
Some(1),
update_nearblocks_data(
db.inner(),
contract.inner(),
nearblocks_api_key.inner(),
last_updated_info,
)
.await
{
Ok(nearblocks_unwrapped) => nearblocks_unwrapped,
Err(e) => {
eprintln!("Failed to fetch proposals from nearblocks: {:?}", e);
nearblocks_client::ApiResponse { txns: vec![] }
}
};

println!(
"Fetched {} method calls from nearblocks",
nearblocks_unwrapped.clone().txns.len()
);

let _ = nearblocks_client::transactions::process(
&nearblocks_unwrapped.txns,
db,
contract.inner().clone(),
)
.await;
.await;
}

match nearblocks_unwrapped.txns.last() {
Some(transaction) => {
let timestamp_nano: i64 = transaction.receipt_block.block_timestamp;

db.set_last_updated_info(timestamp_nano, transaction.block.block_height)
.await
.unwrap();
}
None => {
println!("No transactions found")
}
};

let (proposals, total) = match db
.get_proposals_with_latest_snapshot(limit, order, offset, filters)
.await
{
Err(e) => {
eprintln!("Failed to get proposals: {:?}", e);
(vec![], 0)
}
Ok(result) => result,
};
let (proposals, total) = fetch_proposals(db.inner(), limit, order, offset, filters).await;

Some(Json(PaginatedResponse::new(
proposals.into_iter().map(Into::into).collect(),
Expand All @@ -165,7 +119,24 @@ async fn get_proposals(
async fn get_proposal_with_all_snapshots(
proposal_id: i32,
db: &State<DB>,
contract: &State<AccountId>,
nearblocks_api_key: &State<String>,
) -> Result<Json<Vec<ProposalSnapshotRecord>>, Status> {
let current_timestamp_nano = chrono::Utc::now().timestamp_nanos_opt().unwrap();
let last_updated_info = db.get_last_updated_info().await.unwrap();

if current_timestamp_nano - last_updated_info.0
>= chrono::Duration::seconds(60).num_nanoseconds().unwrap()
{
update_nearblocks_data(
db.inner(),
contract.inner(),
nearblocks_api_key.inner(),
last_updated_info,
)
.await;
}

match db.get_proposal_with_all_snapshots(proposal_id).await {
Err(e) => {
eprintln!("Failed to get proposal snapshots: {:?}", e);
Expand Down
132 changes: 51 additions & 81 deletions src/entrypoints/rfp/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use self::rfp_types::*;
use crate::db::db_types::{RfpSnapshotRecord, RfpWithLatestSnapshotView};
use crate::db::DB;
use crate::nearblocks_client::transactions::update_nearblocks_data;
use crate::rpc_service::RpcService;
use crate::separate_number_and_text;
use crate::types::PaginatedResponse;
use crate::{nearblocks_client, separate_number_and_text, timestamp_to_date_string};
use devhub_shared::rfp::VersionedRFP;
use near_account_id::AccountId;
use rocket::serde::json::Json;
Expand Down Expand Up @@ -47,6 +48,25 @@ async fn search(
}
}

async fn fetch_rfps(
db: &DB,
limit: i64,
order: &str,
offset: i64,
filters: Option<GetRfpFilters>,
) -> (Vec<RfpWithLatestSnapshotView>, i64) {
match db
.get_rfps_with_latest_snapshot(limit, order, offset, filters)
.await
{
Err(e) => {
eprintln!("Failed to get rfps: {:?}", e);
(vec![], 0)
}
Ok(result) => result,
}
}

#[utoipa::path(get, path = "/rfps?<order>&<limit>&<offset>&<filters>", params(
("order"= &str, Path, description ="default order id_desc"),
("limit"= i64, Path, description = "default limit 10"),
Expand All @@ -63,93 +83,26 @@ async fn get_rfps(
contract: &State<AccountId>,
nearblocks_api_key: &State<String>,
) -> Option<Json<PaginatedResponse<RfpWithLatestSnapshotView>>> {
let current_timestamp_nano = chrono::Utc::now().timestamp_nanos_opt().unwrap();
let last_updated_info = db.get_last_updated_info().await.unwrap();

// A day in nanos
println!(
"Last updated timestamp date: {}",
timestamp_to_date_string(last_updated_info.0)
);

let order = order.unwrap_or("id_desc");
let limit = limit.unwrap_or(10);
let offset = offset.unwrap_or(0);

let current_timestamp_nano = chrono::Utc::now().timestamp_nanos_opt().unwrap();
let last_updated_info = db.get_last_updated_info().await.unwrap();

if current_timestamp_nano - last_updated_info.0
< chrono::Duration::seconds(60).num_nanoseconds().unwrap()
>= chrono::Duration::seconds(60).num_nanoseconds().unwrap()
{
println!("Returning cached proposals");
let (rfps, total) = match db
.get_rfps_with_latest_snapshot(limit, order, offset, filters)
.await
{
Err(e) => {
eprintln!("Failed to get proposals: {:?}", e);
(vec![], 0)
}
Ok(result) => result,
};

return Some(Json(PaginatedResponse::new(
rfps.into_iter().map(Into::into).collect(),
1,
limit.try_into().unwrap(),
total.try_into().unwrap(),
)));
}
println!("Fetching not yet indexed method calls from nearblocks");

let nearblocks_client = nearblocks_client::ApiClient::new(nearblocks_api_key.inner().clone());

let nearblocks_unwrapped = match nearblocks_client
.get_account_txns_by_pagination(
contract.inner().clone(),
Some(last_updated_info.1),
Some(50),
Some("asc".to_string()),
Some(1),
update_nearblocks_data(
db.inner(),
contract.inner(),
nearblocks_api_key.inner(),
last_updated_info,
)
.await
{
Ok(nearblocks_unwrapped) => nearblocks_unwrapped,
Err(e) => {
eprintln!("Failed to fetch rfp from nearblocks: {:?}", e);
nearblocks_client::ApiResponse { txns: vec![] }
}
};
.await;
}

let _ = nearblocks_client::transactions::process(
&nearblocks_unwrapped.txns,
db,
contract.inner().clone(),
)
.await;

// TODO: Check if the last transaction is the same day as the last updated timestamp
// If it is, then we need to use the cursor from the nearblocks response
match nearblocks_unwrapped.txns.last() {
Some(transaction) => {
let timestamp_nano: i64 = transaction.receipt_block.block_timestamp;
db.set_last_updated_info(timestamp_nano, transaction.block.block_height)
.await
.unwrap();
}
None => {
println!("No transactions found")
}
};

let (rfps, total) = match db
.get_rfps_with_latest_snapshot(limit, order, offset, filters)
.await
{
Err(e) => {
eprintln!("Failed to get rfps: {:?}", e);
(vec![], 0)
}
Ok(result) => result,
};
let (rfps, total) = fetch_rfps(db, limit, order, offset, filters).await;

Some(Json(PaginatedResponse::new(
rfps.into_iter().map(Into::into).collect(),
Expand All @@ -175,12 +128,29 @@ async fn get_rfp(rfp_id: i32, contract: &State<AccountId>) -> Result<Json<Versio
}
}

#[utoipa::path(get, path = "/{rfp_id}/snapshots")]
#[utoipa::path(get, path = "/rfp/{rfp_id}/snapshots")]
#[get("/<rfp_id>/snapshots")]
async fn get_rfp_with_snapshots(
rfp_id: i64,
db: &State<DB>,
contract: &State<AccountId>,
nearblocks_api_key: &State<String>,
) -> Result<Json<Vec<RfpSnapshotRecord>>, Status> {
let current_timestamp_nano = chrono::Utc::now().timestamp_nanos_opt().unwrap();
let last_updated_info = db.get_last_updated_info().await.unwrap();

if current_timestamp_nano - last_updated_info.0
>= chrono::Duration::seconds(60).num_nanoseconds().unwrap()
{
update_nearblocks_data(
db.inner(),
contract.inner(),
nearblocks_api_key.inner(),
last_updated_info,
)
.await;
}

match db.get_rfp_with_all_snapshots(rfp_id).await {
Err(e) => {
eprintln!("Failed to get rfps: {:?}", e);
Expand Down
Loading

0 comments on commit c5dc5e6

Please sign in to comment.