Skip to content

Commit

Permalink
add raw metrics and config methods
Browse files Browse the repository at this point in the history
  • Loading branch information
archeoss committed Nov 16, 2023
1 parent a260117 commit 4965db6
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 2 deletions.
84 changes: 82 additions & 2 deletions backend/src/services/api.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
use crate::models::bob::{DiskName, IsActive};
use axum::extract::Path;

use crate::{
connector::dto::{MetricsSnapshotModel, NodeConfiguration},
models::bob::{DiskName, IsActive},
};

use super::{
methods::{request_nodes, request_vdisks},
auth::HttpClient,
methods::{request_configuration, request_metrics, request_nodes, request_vdisks},
prelude::*,
};

Expand Down Expand Up @@ -551,3 +557,77 @@ fn disk_status_from_space(space: &dto::SpaceInfo, occupied_space: u64) -> DiskSt
DiskStatus::Good
}
}

/// Get Raw Metrics from Node
///
/// # Errors
///
/// This function will return an error if the server was unable to get node'a client or the request to get metrics fails
#[cfg_attr(feature = "swagger", utoipa::path(
get,
context_path = ApiV1::to_path(),
path = "/nodes/{node_name}/metrics",
responses(
(status = 200, body = MetricsSnapshotModel, content_type = "application/json", description = "Node's metrics"),
(status = 401, description = "Unauthorized"),
(status = 404, description = "Node Not Found")
),
security(("api_key" = []))
))]
pub async fn raw_metrics_by_node(
Extension(client): Extension<HttpBobClient>,
Path(node_name): Path<NodeName>,
) -> AxumResult<Json<MetricsSnapshotModel>> {
let client = get_client_by_node(&client, node_name).await?;

Ok(Json(request_metrics(client.as_ref()).await?))
}

/// Get Configuration from Node
///
/// # Errors
///
/// This function will return an error if the server was unable to get node'a client or the request to get configuration fails
#[cfg_attr(feature = "swagger", utoipa::path(
get,
context_path = ApiV1::to_path(),
path = "/nodes/{node_name}/configuration",
responses(
(status = 200, body = NodeConfiguration, content_type = "application/json", description = "Node's configuration"),
(status = 401, description = "Unauthorized"),
(status = 404, description = "Node Not Found")
),
security(("api_key" = []))
))]
pub async fn raw_configuration_by_node(
Extension(client): Extension<HttpBobClient>,
Path(node_name): Path<NodeName>,
) -> AxumResult<Json<NodeConfiguration>> {
let client = get_client_by_node(&client, node_name).await?;

Ok(Json(request_configuration(client.as_ref()).await?))
}

async fn get_client_by_node(
client: &HttpBobClient,
node_name: NodeName,
) -> AxumResult<Arc<HttpClient>> {
let nodes = request_nodes(client.api_main()).await?;

let node = nodes
.iter()
.find(|node| node.name == node_name)
.ok_or_else(|| {
tracing::error!("Couldn't find specified node");
APIError::RequestFailed
})?;

client
.cluster_with_addr()
.get(&node.name)
.ok_or_else(|| {
tracing::error!("Couldn't find specified node");
APIError::RequestFailed.into()
})
.cloned()
}
13 changes: 13 additions & 0 deletions backend/src/services/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ use api::{get_disks_count, get_nodes_count, get_rps, get_space};
use auth::{login, logout, require_auth, AuthState, BobUser, HttpBobClient, InMemorySessionStore};
use prelude::*;

use self::api::{get_nodes, raw_configuration_by_node, raw_metrics_by_node};

type BobAuthState = AuthState<
BobUser,
Uuid,
Expand All @@ -48,6 +50,17 @@ pub fn api_router_v1(auth_state: BobAuthState) -> Result<Router<BobAuthState>, R
.api_route("/nodes/count", &Method::GET, get_nodes_count)
.api_route("/nodes/rps", &Method::GET, get_rps)
.api_route("/nodes/space", &Method::GET, get_space)
.api_route("/nodes", &Method::GET, get_nodes)
.api_route(
"/nodes/:node_name/metrics",
&Method::GET,
raw_metrics_by_node,
)
.api_route(
"/nodes/:node_name/configuration",
&Method::GET,
raw_configuration_by_node,
)
.unwrap()?
.route_layer(from_fn_with_state(auth_state, require_auth))
.with_context::<ApiV1, ApiDoc>()
Expand Down

0 comments on commit 4965db6

Please sign in to comment.