Skip to content

Commit 5d1011d

Browse files
committed
refactoring
1 parent 03e9b27 commit 5d1011d

File tree

2 files changed

+74
-52
lines changed

2 files changed

+74
-52
lines changed

backend/src/models/api.rs

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use super::prelude::*;
44

55
pub const DEFAULT_MAX_CPU: u64 = 90;
6-
pub const DEFAULT_MIN_FREE_SPACE: f64 = 0.1;
6+
pub const DEFAULT_MIN_FREE_SPACE_PERCENTAGE: f64 = 0.1;
77

88
/// Connection Data
99
pub use crate::models::shared::{BobConnectionData, Credentials};
@@ -32,6 +32,25 @@ pub enum DiskStatus {
3232
Offline,
3333
}
3434

35+
impl DiskStatus {
36+
#[must_use]
37+
pub fn from_space_info(space: &dto::SpaceInfo, disk_name: &str) -> Self {
38+
if let Some(&occupied_space) = space.occupied_disk_space_by_disk.get(disk_name) {
39+
#[allow(clippy::cast_precision_loss)]
40+
if ((space.total_disk_space_bytes - occupied_space) as f64
41+
/ space.total_disk_space_bytes as f64)
42+
< DEFAULT_MIN_FREE_SPACE_PERCENTAGE
43+
{
44+
Self::Bad(vec![DiskProblem::FreeSpaceRunningOut])
45+
} else {
46+
Self::Good
47+
}
48+
} else {
49+
Self::Offline
50+
}
51+
}
52+
}
53+
3554
/// Defines disk status names
3655
#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize, Hash, EnumIter)]
3756
#[serde(rename_all = "camelCase")]
@@ -61,15 +80,19 @@ pub enum NodeProblem {
6180
impl NodeProblem {
6281
#[must_use]
6382
pub fn default_from_metrics(node_metrics: &TypedMetrics) -> Vec<Self> {
64-
Self::from_metrics(node_metrics, DEFAULT_MAX_CPU, DEFAULT_MIN_FREE_SPACE)
83+
Self::from_metrics(
84+
node_metrics,
85+
DEFAULT_MAX_CPU,
86+
DEFAULT_MIN_FREE_SPACE_PERCENTAGE,
87+
)
6588
}
6689

6790
#[must_use]
6891
#[allow(clippy::cast_precision_loss)]
6992
pub fn from_metrics(
7093
node_metrics: &TypedMetrics,
7194
max_cpu: u64,
72-
min_free_space: f64,
95+
min_free_space_perc: f64,
7396
) -> Vec<Self> {
7497
let mut res = vec![];
7598
if node_metrics[RawMetricEntry::BackendAlienCount].value != 0 {
@@ -85,7 +108,7 @@ impl NodeProblem {
85108
- (node_metrics[RawMetricEntry::HardwareTotalSpace].value
86109
- node_metrics[RawMetricEntry::HardwareFreeSpace].value) as f64
87110
/ node_metrics[RawMetricEntry::HardwareTotalSpace].value as f64)
88-
< min_free_space
111+
< min_free_space_perc
89112
{
90113
res.push(Self::FreeSpaceRunningOut);
91114
}
@@ -127,6 +150,22 @@ impl NodeStatus {
127150
}
128151
}
129152

153+
impl TypedMetrics {
154+
#[allow(clippy::cast_precision_loss)]
155+
#[must_use]
156+
pub fn is_bad_node(&self) -> bool {
157+
self[RawMetricEntry::BackendAlienCount].value != 0
158+
|| self[RawMetricEntry::BackendCorruptedBlobCount].value != 0
159+
|| self[RawMetricEntry::HardwareBobCpuLoad].value >= DEFAULT_MAX_CPU
160+
|| (1.
161+
- (self[RawMetricEntry::HardwareTotalSpace].value
162+
- self[RawMetricEntry::HardwareFreeSpace].value) as f64
163+
/ self[RawMetricEntry::HardwareTotalSpace].value as f64)
164+
< DEFAULT_MIN_FREE_SPACE_PERCENTAGE
165+
|| self[RawMetricEntry::HardwareBobVirtualRam] > self[RawMetricEntry::HardwareTotalRam]
166+
}
167+
}
168+
130169
/// Defines node status names
131170
#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize, Hash, EnumIter)]
132171
#[serde(rename_all = "camelCase")]

backend/src/services/api.rs

Lines changed: 31 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,17 @@ use super::prelude::*;
22

33
/// Returns count of Physical Disks per status
44
///
5-
#[cfg_attr(feature = "swagger",
5+
#[cfg_attr(all(feature = "swagger", debug_assertions),
66
utoipa::path(
77
get,
88
context_path = ApiV1::to_path(),
99
path = "/disks/count",
1010
responses(
11-
(status = 200, body = DiskCount, content_type = "application/json", description = "Returns a list with count of physical disks per status"),
11+
(
12+
status = 200, body = DiskCount,
13+
content_type = "application/json",
14+
description = "Returns a list with count of physical disks per status"
15+
),
1216
(status = 401, description = "Unauthorized")
1317
),
1418
security(("api_key" = []))
@@ -65,14 +69,10 @@ pub async fn get_disks_count(Extension(client): Extension<HttpBobClient>) -> Jso
6569
};
6670
let active_disks = disks.iter().filter(|disk| disk.is_active);
6771
for disk in active_disks {
68-
if let Some(&occupied_space) = space.occupied_disk_space_by_disk.get(&disk.name) {
69-
#[allow(clippy::cast_precision_loss)]
70-
match disk_status_from_space(&space, occupied_space) {
71-
DiskStatus::Good => count[DiskStatusName::Good] += 1,
72-
_ => count[DiskStatusName::Bad] += 1,
73-
}
74-
} else {
75-
count[DiskStatusName::Offline] += 1;
72+
match DiskStatus::from_space_info(&space, &disk.name) {
73+
DiskStatus::Good => count[DiskStatusName::Good] += 1,
74+
DiskStatus::Offline => count[DiskStatusName::Offline] += 1,
75+
DiskStatus::Bad(_) => count[DiskStatusName::Bad] += 1,
7676
}
7777
}
7878
}
@@ -83,16 +83,21 @@ pub async fn get_disks_count(Extension(client): Extension<HttpBobClient>) -> Jso
8383

8484
/// Get Nodes count per Status
8585
///
86-
#[cfg_attr(feature = "swagger", utoipa::path(
86+
#[cfg_attr(all(feature = "swagger", debug_assertions),
87+
utoipa::path(
8788
get,
8889
context_path = ApiV1::to_path(),
8990
path = "/nodes/count",
9091
responses(
91-
(status = 200, body = NodeCount, content_type = "application/json", description = "Node count list per status"),
92+
(
93+
status = 200, body = NodeCount,
94+
content_type = "application/json",
95+
description = "Node count list per status"
96+
),
9297
(status = 401, description = "Unauthorized")
9398
),
9499
security(("api_key" = []))
95-
))]
100+
))]
96101
pub async fn get_nodes_count(Extension(client): Extension<HttpBobClient>) -> Json<NodeCount> {
97102
tracing::info!("get /nodes/count : {:?}", client);
98103

@@ -109,9 +114,8 @@ pub async fn get_nodes_count(Extension(client): Extension<HttpBobClient>) -> Jso
109114
let mut counter = 0;
110115
while let Some(res) = metrics.next().await {
111116
if let Ok(Ok(GetMetricsResponse::Metrics(metrics))) = res {
112-
tracing::info!("#{counter}: metrics received successfully");
113-
let metrics = Into::<TypedMetrics>::into(metrics);
114-
if is_bad_node(&metrics) {
117+
tracing::trace!("#{counter}: metrics received successfully");
118+
if Into::<TypedMetrics>::into(metrics).is_bad_node() {
115119
count[NodeStatusName::Bad] += 1;
116120
} else {
117121
count[NodeStatusName::Good] += 1;
@@ -129,16 +133,21 @@ pub async fn get_nodes_count(Extension(client): Extension<HttpBobClient>) -> Jso
129133

130134
/// Returns Total RPS on cluster
131135
///
132-
#[cfg_attr(feature = "swagger", utoipa::path(
136+
#[cfg_attr(all(feature = "swagger", debug_assertions),
137+
utoipa::path(
133138
get,
134139
context_path = ApiV1::to_path(),
135140
path = "/nodes/rps",
136141
responses(
137-
(status = 200, body = RPS, content_type = "application/json", description = "RPS list per operation on all nodes"),
142+
(
143+
status = 200, body = RPS,
144+
content_type = "application/json",
145+
description = "RPS list per operation on all nodes"
146+
),
138147
(status = 401, description = "Unauthorized")
139148
),
140149
security(("api_key" = []))
141-
))]
150+
))]
142151
pub async fn get_rps(Extension(client): Extension<HttpBobClient>) -> Json<RPS> {
143152
tracing::info!("get /nodes/rps : {:?}", client);
144153

@@ -155,7 +164,6 @@ pub async fn get_rps(Extension(client): Extension<HttpBobClient>) -> Json<RPS> {
155164
while let Some(res) = metrics.next().await {
156165
if let Ok(Ok(metrics)) = res {
157166
tracing::info!("#{counter}: metrics received successfully");
158-
159167
let GetMetricsResponse::Metrics(metrics) = metrics;
160168
let metrics = Into::<TypedMetrics>::into(metrics);
161169
rps[Operation::Get] += metrics[RawMetricEntry::ClusterGrinderGetCountRate].value;
@@ -174,7 +182,8 @@ pub async fn get_rps(Extension(client): Extension<HttpBobClient>) -> Json<RPS> {
174182

175183
/// Return inforamtion about space on cluster
176184
///
177-
#[cfg_attr(feature = "swagger", utoipa::path(
185+
#[cfg_attr(all(feature = "swagger", debug_assertions),
186+
utoipa::path(
178187
get,
179188
context_path = ApiV1::to_path(),
180189
path = "/nodes/space",
@@ -183,7 +192,7 @@ pub async fn get_rps(Extension(client): Extension<HttpBobClient>) -> Json<RPS> {
183192
(status = 401, description = "Unauthorized")
184193
),
185194
security(("api_key" = []))
186-
))]
195+
))]
187196
pub async fn get_space(Extension(client): Extension<HttpBobClient>) -> Json<SpaceInfo> {
188197
tracing::info!("get /space : {:?}", client);
189198
let mut spaces: FuturesUnordered<_> = client
@@ -214,29 +223,3 @@ pub async fn get_space(Extension(client): Extension<HttpBobClient>) -> Json<Spac
214223

215224
Json(total_space)
216225
}
217-
218-
#[allow(clippy::cast_precision_loss)]
219-
fn is_bad_node(node_metrics: &TypedMetrics) -> bool {
220-
node_metrics[RawMetricEntry::BackendAlienCount].value != 0
221-
|| node_metrics[RawMetricEntry::BackendCorruptedBlobCount].value != 0
222-
|| node_metrics[RawMetricEntry::HardwareBobCpuLoad].value >= DEFAULT_MAX_CPU
223-
|| (1.
224-
- (node_metrics[RawMetricEntry::HardwareTotalSpace].value
225-
- node_metrics[RawMetricEntry::HardwareFreeSpace].value) as f64
226-
/ node_metrics[RawMetricEntry::HardwareTotalSpace].value as f64)
227-
< DEFAULT_MIN_FREE_SPACE
228-
|| node_metrics[RawMetricEntry::HardwareBobVirtualRam]
229-
> node_metrics[RawMetricEntry::HardwareTotalRam]
230-
}
231-
232-
#[allow(clippy::cast_precision_loss)]
233-
fn disk_status_from_space(space: &dto::SpaceInfo, occupied_space: u64) -> DiskStatus {
234-
if ((space.total_disk_space_bytes - occupied_space) as f64
235-
/ space.total_disk_space_bytes as f64)
236-
< DEFAULT_MIN_FREE_SPACE
237-
{
238-
DiskStatus::Bad(vec![DiskProblem::FreeSpaceRunningOut])
239-
} else {
240-
DiskStatus::Good
241-
}
242-
}

0 commit comments

Comments
 (0)