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

feat: add cutoff param in active-nodes and active-clients api #88

Merged
merged 1 commit into from
Dec 9, 2023
Merged
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
49 changes: 37 additions & 12 deletions bins/api-server/src/peerdb/routes.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
use super::app_state::AppState;
use axum::{
extract::{Path, State},
extract::{Path, Query, State},
routing::get,
Json, Router,
};
use chrono::{Days, Utc};
use reth_crawler_db::{types::ClientData, PeerDB, PeerData};
use serde::Deserialize;
use std::sync::Arc;

/// The max `last_seen` a node can have and still be considered active (in DAYS).
const MAX_LAST_SEEN: u64 = 5;
const MAX_LAST_SEEN: u64 = 1;

/// Cutoff parameter for letting a client choose the cutoff parameter on its own.
#[derive(Deserialize)]
struct CutoffParam {
cutoff: u64,
}

/// Router for all APIs.
pub(crate) fn rest_router() -> Router<AppState> {
Expand All @@ -26,23 +33,41 @@ async fn get_nodes(State(store): State<Arc<dyn PeerDB>>) -> Json<Vec<PeerData>>
Json(store.all_peers(Some(50)).await.unwrap())
}

async fn get_active_nodes(State(store): State<Arc<dyn PeerDB>>) -> Json<Vec<PeerData>> {
let cutoff = Utc::now()
.checked_sub_days(Days::new(MAX_LAST_SEEN))
.expect("Data is not ouf of range")
.to_string();
async fn get_active_nodes(
State(store): State<Arc<dyn PeerDB>>,
cutoff_param: Option<Query<CutoffParam>>,
) -> Json<Vec<PeerData>> {
let cutoff = match cutoff_param {
Some(cutoffparam) => Utc::now()
.checked_sub_days(Days::new(cutoffparam.cutoff))
.expect("Data is not ouf of range")
.to_string(),
None => Utc::now()
.checked_sub_days(Days::new(MAX_LAST_SEEN))
.expect("Data is not ouf of range")
.to_string(),
};
Json(store.all_active_peers(cutoff, Some(50)).await.unwrap())
}

async fn get_clients(State(store): State<Arc<dyn PeerDB>>) -> Json<Vec<ClientData>> {
Json(store.clients(Some(50)).await.unwrap())
}

async fn get_active_clients(State(store): State<Arc<dyn PeerDB>>) -> Json<Vec<ClientData>> {
let cutoff = Utc::now()
.checked_sub_days(Days::new(MAX_LAST_SEEN))
.expect("Data is not ouf of range")
.to_string();
async fn get_active_clients(
State(store): State<Arc<dyn PeerDB>>,
cutoff_param: Option<Query<CutoffParam>>,
) -> Json<Vec<ClientData>> {
let cutoff = match cutoff_param {
Some(cutoff) => Utc::now()
.checked_sub_days(Days::new(cutoff.cutoff))
.expect("Data is not ouf of range")
.to_string(),
None => Utc::now()
.checked_sub_days(Days::new(MAX_LAST_SEEN))
.expect("Data is not ouf of range")
.to_string(),
};
Json(store.active_clients(cutoff, Some(50)).await.unwrap())
}

Expand Down