Skip to content

Commit

Permalink
pushed to fly to run @petersalomonsen his test
Browse files Browse the repository at this point in the history
  • Loading branch information
Tguntenaar committed Dec 4, 2024
1 parent 41017e4 commit a79e0a9
Show file tree
Hide file tree
Showing 12 changed files with 581 additions and 327 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
/target
.envrc
.bin
.env
.env
devhub.near*
events-committee.near*
infrastructure-committee.near*
10 changes: 5 additions & 5 deletions bring-cache-up-to-date.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import time
import requests

local = False
reset_from_zero = True # False to continue from where it left off
fly_app_name = "events-cache-api-rs"
local = True
reset_from_zero = False # False to continue from where it left off
fly_app_name = "devhub-cache-api-rs"
# ~120 calls for devhub
# ~20 calls for infra
# ~40 calls for events
Expand All @@ -12,11 +12,11 @@
base_url = f"http://localhost:8080/" if local else f"https://{fly_app_name}.fly.dev/"

def call_api(count):
url = f"{base_url}proposal/256/snapshots" # Replace with your API URL
url = f"{base_url}proposals" # Replace with your API URL
try:
response = requests.get(url)
if response.status_code == 200:
print(f"{count} API call successful: - response length {len(response.json())}")
print(f"{count} API call successful: - response length {response.json().get('total_records')}")
else:
print("API call failed with status code:", response.status_code)
except requests.exceptions.RequestException as e:
Expand Down
11 changes: 7 additions & 4 deletions scripts/download_nearblocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,13 @@ const path = require("path");

const ACCOUNT = "devhub.near";
const BASE_URL = "https://api.nearblocks.io/v1/account";
const PER_PAGE = 25;
const API_KEY = "API_KEY";
const PER_PAGE = 50;
const API_KEY = process.env.NEARBLOCKS_API_KEY;
if (!API_KEY) {
throw new Error("NEARBLOCKS_API_KEY environment variable is required");
}
const START_AFTER_BLOCK = 0;
const RECEIPT = false;
const RECEIPT = false; // Can't use receipt because it's not supported by the API after_block only checks after the block

async function saveTransactions(blockHeight, transactions) {
// Create a Blob containing the JSON data
Expand All @@ -44,7 +47,7 @@ async function saveTransactions(blockHeight, transactions) {
}

async function fetchTransactions(afterBlock) {
const url = `${BASE_URL}/${ACCOUNT}/txns?to=${ACCOUNT}&after_block=${afterBlock}&per_page=${PER_PAGE}&order=asc&page=1`;
const url = `${BASE_URL}/${ACCOUNT}/txns?&after_block=${afterBlock}&per_page=${PER_PAGE}&order=asc&page=1`;

try {
console.log(url);
Expand Down
37 changes: 37 additions & 0 deletions src/db/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -908,6 +908,43 @@ impl DB {

Ok((rfps, total_count))
}

pub async fn get_proposal_with_latest_snapshot_view(
&self,
proposal_id: i32,
) -> Result<Option<ProposalWithLatestSnapshotView>, sqlx::Error> {
let sql = r#"
SELECT *
FROM proposals_with_latest_snapshot
WHERE proposal_id = $1
"#;
let proposal = sqlx::query_as::<_, ProposalWithLatestSnapshotView>(sql)
.bind(proposal_id)
.fetch_optional(&self.0)
.await?;

Ok(proposal)
}

pub async fn get_latest_rfp_snapshot(
&self,
rfp_id: i32,
) -> Result<Option<RfpSnapshotRecord>, sqlx::Error> {
let sql = r#"
SELECT *
FROM rfp_snapshots
WHERE rfp_id = $1
ORDER BY ts DESC
LIMIT 1
"#;

let snapshot = sqlx::query_as::<_, RfpSnapshotRecord>(sql)
.bind(rfp_id)
.fetch_optional(&self.0)
.await?;

Ok(snapshot)
}
}

async fn run_migrations(rocket: Rocket<Build>) -> fairing::Result {
Expand Down
6 changes: 3 additions & 3 deletions src/entrypoints/proposal/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use rocket::{get, http::Status, State};
use std::convert::TryInto;
pub mod proposal_types;

// TODO use caching in search
// TODO Use caching of search terms
#[utoipa::path(get, path = "/proposals/search?<input>", params(
("input"= &str, Path, description ="The string to search for in proposal name, description, summary, and category fields."),
))]
Expand Down Expand Up @@ -158,7 +158,7 @@ async fn set_timestamp(block_height: i64, db: &State<DB>) -> Result<(), Status>
}
}

// TODO remove after testing
// TODO Remove this once we go in production or put it behind authentication or a flag
#[get("/info/clean")]
async fn clean(db: &State<DB>) -> Result<(), Status> {
let _ = match db.remove_all_snapshots().await {
Expand Down Expand Up @@ -190,7 +190,7 @@ async fn get_proposal(
proposal_id: i32,
contract: &State<AccountId>,
) -> Result<Json<VersionedProposal>, rocket::http::Status> {
let rpc_service = RpcService::new(contract.inner().clone());
let rpc_service = RpcService::new(contract);
// 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
7 changes: 2 additions & 5 deletions src/entrypoints/rfp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use rocket::{delete, get, http::Status, State};
use std::convert::TryInto;
pub mod rfp_types;

// TODO use caching in search
// TODO Use caching of search terms
#[utoipa::path(get, path = "/rfps/search/<input>", params(
("input"= &str, Path, description ="The string to search for in rfp name, description, summary, and category fields."),
))]
Expand Down Expand Up @@ -116,10 +116,7 @@ async fn get_rfps(
#[get("/<rfp_id>")]
async fn get_rfp(rfp_id: i32, contract: &State<AccountId>) -> Result<Json<VersionedRFP>, Status> {
// TODO Get cached rfp
match RpcService::new(contract.inner().clone())
.get_rfp(rfp_id)
.await
{
match RpcService::new(contract).get_rfp(rfp_id).await {
Ok(rfp) => Ok(Json(rfp.data)),
Err(e) => {
eprintln!("In /rfp/rfp_id; Failed to get rfp from RPC: {:?}", e);
Expand Down
29 changes: 22 additions & 7 deletions src/nearblocks_client/mod.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use near_sdk::AccountId;
use reqwest::Client;
use serde::{Deserialize, Serialize};
pub mod proposal;
pub mod rfp;
pub mod transactions;
pub mod types;
use types::Transaction;

// TODO use nearblocks API KEY

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct ApiResponse {
#[serde(default)]
Expand Down Expand Up @@ -56,13 +56,28 @@ impl ApiClient {
);
let endpoint = format!("v1/account/{}/txns", account_id);
let url = self.base_url.clone() + &endpoint + &query_params;
println!("Fetching from {}", url);
self.client

println!("Fetching transactions from {}", url);

let response = self
.client
.get(&url)
.header("Authorization", format!("Bearer {}", self.api_key))
.send()
.await?
.json::<ApiResponse>()
.await
.await?;

match response.json::<ApiResponse>().await {
Ok(api_response) => {
println!(
"Successfully fetched {} transactions",
api_response.txns.len()
);
Ok(api_response)
}
Err(e) => {
eprintln!("Failed to parse API response: {}", e);
Err(e)
}
}
}
}
Loading

0 comments on commit a79e0a9

Please sign in to comment.