Skip to content

Commit

Permalink
fix envs couldn't disable thresholds
Browse files Browse the repository at this point in the history
  • Loading branch information
bragov4ik committed Oct 4, 2024
1 parent a4a0a88 commit de91e3b
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 11 deletions.
18 changes: 11 additions & 7 deletions stats/stats-server/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{
health::HealthService,
read_service::ReadService,
runtime_setup::RuntimeSetup,
settings::{Settings, StartConditionSettings},
settings::{Settings, StartConditionSettings, ToggleableThreshold},
update_service::UpdateService,
};

Expand Down Expand Up @@ -60,11 +60,13 @@ fn grpc_router<S: StatsService>(
}

fn is_threshold_passed(
threshold: Option<f64>,
threshold: &ToggleableThreshold,
float_value: Option<String>,
value_name: &str,
) -> Result<bool, anyhow::Error> {
let Some(threshold) = threshold else {
let threshold = if threshold.enabled {
threshold.threshold
} else {
return Ok(true);
};
let value = float_value
Expand Down Expand Up @@ -110,11 +112,11 @@ async fn wait_for_blockscout_indexing(
match blockscout_client::apis::main_page_api::get_indexing_status(&api_config).await {
Ok(result)
if is_threshold_passed(
wait_config.blocks_ratio_threshold,
&wait_config.blocks_ratio_threshold,
result.indexed_blocks_ratio.clone(),
"indexed_blocks_ratio",
)? && is_threshold_passed(
wait_config.internal_transactions_ratio_threshold,
&wait_config.internal_transactions_ratio_threshold,
result.indexed_internal_transactions_ratio.clone(),
"indexed_internal_transactions_ratio",
)? =>
Expand Down Expand Up @@ -267,8 +269,10 @@ mod tests {
) -> StartConditionSettings {
StartConditionSettings {
enabled: true,
blocks_ratio_threshold: Some(blocks),
internal_transactions_ratio_threshold: Some(internal_transactions),
blocks_ratio_threshold: ToggleableThreshold::enabled(blocks),
internal_transactions_ratio_threshold: ToggleableThreshold::enabled(
internal_transactions,
),
check_period_secs: 0,
}
}
Expand Down
71 changes: 67 additions & 4 deletions stats/stats-server/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ impl Default for LimitsSettings {
#[serde(default, deny_unknown_fields)]
pub struct StartConditionSettings {
pub enabled: bool,
pub blocks_ratio_threshold: Option<f64>,
pub internal_transactions_ratio_threshold: Option<f64>,
pub blocks_ratio_threshold: ToggleableThreshold,
pub internal_transactions_ratio_threshold: ToggleableThreshold,
pub check_period_secs: u32,
}

Expand All @@ -107,13 +107,76 @@ impl Default for StartConditionSettings {
Self {
enabled: true,
// in some networks it's always almost 1
blocks_ratio_threshold: Some(0.98),
internal_transactions_ratio_threshold: Some(0.98),
blocks_ratio_threshold: ToggleableThreshold::default(),
internal_transactions_ratio_threshold: ToggleableThreshold::default(),
check_period_secs: 5,
}
}
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(default, deny_unknown_fields)]
pub struct ToggleableThreshold {
pub enabled: bool,
pub threshold: f64,
}

impl ToggleableThreshold {
pub fn disabled() -> Self {
Self {
enabled: false,
threshold: 0.0,
}
}

pub fn enabled(value: f64) -> Self {
Self {
enabled: true,
threshold: value,
}
}

pub fn set_threshold(mut self, value: f64) -> Self {
self.threshold = value;
self
}

pub fn set_disabled(mut self) -> Self {
self.enabled = false;
self
}
}

impl Default for ToggleableThreshold {
fn default() -> Self {
Self::enabled(0.98)
}
}

impl ConfigSettings for Settings {
const SERVICE_NAME: &'static str = "STATS";
}

#[cfg(test)]
mod tests {
use crate::config_env::test_utils::check_envs_parsed_to;

use super::*;

#[test]
fn start_condition_thresholds_can_be_disabled_with_envs() {
check_envs_parsed_to(
"START_SETTINGS",
[(
"START_SETTINGS__BLOCKS_RATIO_THRESHOLD__ENABLED".to_owned(),
"false".to_owned(),
)]
.into(),
StartConditionSettings {
blocks_ratio_threshold: ToggleableThreshold::default().set_disabled(),
..StartConditionSettings::default()
},
)
.unwrap()
}
}

0 comments on commit de91e3b

Please sign in to comment.