introduce UNSTABLE endpoints for returning network monitor run details (#5214)

This commit is contained in:
Jędrzej Stuczyński
2024-12-04 16:49:26 +00:00
committed by GitHub
parent 29ea4623c8
commit 78bf413e6a
9 changed files with 425 additions and 90 deletions
@@ -1,6 +1,8 @@
// Copyright 2021-2024 - Nym Technologies SA <contact@nymtech.net>
// SPDX-License-Identifier: GPL-3.0-only
use super::unstable;
use crate::node_status_api::handlers::unstable::{latest_monitor_run_report, monitor_run_report};
use crate::node_status_api::handlers::MixIdParam;
use crate::node_status_api::helpers::{
_compute_mixnode_reward_estimation, _gateway_core_status_count, _gateway_report,
@@ -23,8 +25,6 @@ use nym_api_requests::models::{
use serde::Deserialize;
use utoipa::IntoParams;
use super::unstable;
// we want to mark the routes as deprecated in swagger, but still expose them
#[allow(deprecated)]
pub(super) fn network_monitor_routes() -> Router<AppState> {
@@ -84,6 +84,18 @@ pub(super) fn network_monitor_routes() -> Router<AppState> {
axum::routing::get(unstable::gateway_test_results),
),
)
.nest(
"/network-monitor/unstable",
Router::new()
.route(
"/run/:monitor_run_id/details",
axum::routing::get(monitor_run_report),
)
.route(
"/run/latest/details",
axum::routing::get(latest_monitor_run_report),
),
)
}
#[utoipa::path(
@@ -6,15 +6,17 @@ use crate::node_status_api::models::{AxumErrorResponse, AxumResult};
use crate::support::http::helpers::PaginationRequest;
use crate::support::http::state::AppState;
use crate::support::storage::NymApiStorage;
use anyhow::bail;
use axum::extract::{Path, Query, State};
use axum::Json;
use nym_api_requests::models::{
GatewayTestResultResponse, MixnodeTestResultResponse, PartialTestResult, TestNode, TestRoute,
GatewayTestResultResponse, MixnodeTestResultResponse, NetworkMonitorRunDetailsResponse,
PartialTestResult, TestNode, TestRoute,
};
use nym_api_requests::pagination::Pagination;
use nym_mixnet_contract_common::NodeId;
use std::cmp::min;
use std::collections::HashMap;
use std::collections::{BTreeMap, HashMap};
use std::sync::Arc;
use tokio::sync::RwLock;
use tracing::{error, trace};
@@ -301,3 +303,88 @@ pub async fn gateway_test_results(
))),
}
}
async fn _monitor_run_report(
monitor_run_id: i64,
storage: &NymApiStorage,
) -> anyhow::Result<NetworkMonitorRunDetailsResponse> {
let Some((raw_report, raw_scores)) = storage.get_monitor_run_report(monitor_run_id).await?
else {
bail!("no results found for monitor run {monitor_run_id}");
};
let mut mixnode_results = BTreeMap::new();
let mut gateway_results = BTreeMap::new();
for score in raw_scores {
if score.typ == "mixnode" {
mixnode_results.insert(score.rounded_score, score.nodes_count as usize);
} else if score.typ == "gateway" {
gateway_results.insert(score.rounded_score, score.nodes_count as usize);
}
}
Ok(NetworkMonitorRunDetailsResponse {
monitor_run_id,
network_reliability: raw_report.network_reliability,
total_sent: raw_report.packets_sent as usize,
total_received: raw_report.packets_received as usize,
mixnode_results,
gateway_results,
})
}
async fn _latest_monitor_run_report(
storage: &NymApiStorage,
) -> anyhow::Result<NetworkMonitorRunDetailsResponse> {
let Some(latest_id) = storage.get_latest_monitor_run_id().await? else {
bail!("no network monitor run found");
};
_monitor_run_report(latest_id, storage).await
}
#[utoipa::path(
tag = "UNSTABLE - DO **NOT** USE",
get,
params(
PaginationRequest
),
path = "/v1/status/network-monitor/unstable/run/{monitor_run_id}/details",
responses(
(status = 200, body = NetworkMonitorRunDetailsResponse)
)
)]
pub async fn monitor_run_report(
Path(monitor_run_id): Path<i64>,
State(state): State<AppState>,
) -> AxumResult<Json<NetworkMonitorRunDetailsResponse>> {
match _monitor_run_report(monitor_run_id, state.storage()).await {
Ok(res) => Ok(Json(res)),
Err(err) => Err(AxumErrorResponse::internal_msg(format!(
"failed to retrieve monitor run report for run {monitor_run_id}: {err}"
))),
}
}
#[utoipa::path(
tag = "UNSTABLE - DO **NOT** USE",
get,
params(
PaginationRequest
),
path = "/v1/status/network-monitor/unstable/run/latest/details",
responses(
(status = 200, body = NetworkMonitorRunDetailsResponse)
)
)]
pub async fn latest_monitor_run_report(
State(state): State<AppState>,
) -> AxumResult<Json<NetworkMonitorRunDetailsResponse>> {
match _latest_monitor_run_report(state.storage()).await {
Ok(res) => Ok(Json(res)),
Err(err) => Err(AxumErrorResponse::internal_msg(format!(
"failed to retrieve the latest monitor run report: {err}"
))),
}
}