change stats model to cater for both endpoints after nym-node intro

This commit is contained in:
Tommy Verrall
2024-05-02 11:01:50 +02:00
parent e59a444074
commit bbf57482fc
2 changed files with 23 additions and 12 deletions
+16 -10
View File
@@ -34,7 +34,7 @@ async fn get_mix_node_description(host: &str, port: u16) -> Result<NodeDescripti
let first_try = reqwest::get(format!("http://{host}:{port}/description")).await;
match first_try {
//fallback for new endpoint for nym-nodes
// new endpoint for nym-nodes
Ok(response) => response.json::<NodeDescription>().await,
Err(_) => {
let second_try = reqwest::get(format!("http://{host}:{port}/api/v1/description")).await;
@@ -44,19 +44,25 @@ async fn get_mix_node_description(host: &str, port: u16) -> Result<NodeDescripti
}
async fn get_mix_node_stats(host: &str, port: u16) -> Result<NodeStats, ReqwestError> {
let first_try = reqwest::get(format!("http://{host}:{port}/stats")).await;
// old endpoint for nym-mixnodes
let primary_url = format!("http://{host}:{port}/stats");
// new endpoint for nym-nodes
let secondary_url = format!("http://{host}:{port}/api/v1/metrics/mixing");
match first_try {
Ok(response) => response.json::<NodeStats>().await,
Err(_) => {
//fallback for new endpoint for nym-nodes
let second_try =
reqwest::get(format!("http://{host}:{port}/api/v1/metrics/mixing")).await;
second_try?.json::<NodeStats>().await
let primary_response = reqwest::get(&primary_url).await;
if let Ok(response) = primary_response {
if let Ok(stats) = response.json::<NodeStats>().await {
return Ok(stats);
}
}
}
let secondary_response = reqwest::get(&secondary_url).await;
if let Ok(response) = secondary_response {
return response.json::<NodeStats>().await;
}
Err("failed to fetch stats from both endpoints")
}
#[openapi(tag = "mix_nodes")]
#[get("/<mix_id>")]
pub(crate) async fn get_by_id(
+7 -2
View File
@@ -98,25 +98,30 @@ pub(crate) struct NodeDescription {
pub(crate) location: String,
}
#[derive(Serialize, Clone, Deserialize, JsonSchema)]
#[derive(Serialize, Clone, Deserialize, JsonSchema, Debug)]
pub(crate) struct NodeStats {
#[serde(
serialize_with = "humantime_serde::serialize",
deserialize_with = "humantime_serde::deserialize"
)]
update_time: SystemTime,
#[serde(
serialize_with = "humantime_serde::serialize",
deserialize_with = "humantime_serde::deserialize"
)]
previous_update_time: SystemTime,
#[serde(alias = "received_since_startup", default)]
packets_received_since_startup: u64,
#[serde(alias = "sent_since_startup", default)]
packets_sent_since_startup: u64,
#[serde(alias = "dropped_since_startup", default)]
packets_explicitly_dropped_since_startup: u64,
#[serde(alias = "received_since_last_update", default)]
packets_received_since_last_update: u64,
#[serde(alias = "sent_since_last_update", default)]
packets_sent_since_last_update: u64,
#[serde(alias = "dropped_since_last_update", default)]
packets_explicitly_dropped_since_last_update: u64,
}