Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions crates/api-web/src/managed_host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ struct ManagedHostShow {
active_state_filter: String,
active_time_in_state_above_sla_filter: String,
states: Vec<String>,
health_alert_ids: Vec<String>,
gpus: Vec<String>,
active_gpu_filter: String,
ibs: Vec<String>,
Expand Down Expand Up @@ -456,6 +457,7 @@ pub async fn show_html(
let mut hosts = Vec::new();
let mut models_per_vendor: HashMap<String, Vec<String>> = HashMap::new();
let mut states = HashSet::new();
let mut health_alert_ids = HashSet::new();
let mut gpus = HashSet::new();
let mut ibs = HashSet::new();
let mut mems = HashSet::new();
Expand All @@ -481,6 +483,9 @@ pub async fn show_html(
states.insert(short_state(&m.state).to_string());
gpus.insert(m.num_gpus);
ibs.insert(m.num_ib_ifs);
for alert in &m.health_probe_alerts {
health_alert_ids.insert(alert.id.to_string());
}
let barry = mem_to_size(&m.host_memory);
mems.insert((barry, format!("{barry} GiB")));

Expand Down Expand Up @@ -528,6 +533,15 @@ pub async fn show_html(
if active_health_alerts_filter == "unhealthy" && m.health_probe_alerts.is_empty() {
continue;
}
if active_health_alerts_filter != "healthy"
&& active_health_alerts_filter != "unhealthy"
&& !m
.health_probe_alerts
.iter()
.any(|a| a.id.to_string() == active_health_alerts_filter)
{
continue;
}
}
if active_maintenance_filter != "all" {
if active_maintenance_filter == "active" && !m.maintenance_reference.is_empty() {
Expand Down Expand Up @@ -570,6 +584,8 @@ pub async fn show_html(

let states: Vec<String> = states.into_iter().sorted_unstable().collect();

let health_alert_ids: Vec<String> = health_alert_ids.into_iter().sorted_unstable().collect();

let gpus: Vec<String> = gpus
.into_iter()
.map(|x| x.to_string())
Expand Down Expand Up @@ -633,6 +649,7 @@ pub async fn show_html(
active_state_filter,
active_time_in_state_above_sla_filter,
states,
health_alert_ids,
gpus,
active_gpu_filter,
ibs,
Expand Down
99 changes: 99 additions & 0 deletions crates/api-web/src/tests/managed_host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -337,3 +337,102 @@ async fn test_managed_host_html_uses_runtime_sla_config(pool: sqlx::PgPool) {
assert!(body_str.contains("bubble warning"));
assert!(body_str.contains("Assigned/BootingWithDiscoveryImage"));
}

#[crate::sqlx_test]
async fn test_managed_host_health_alert_exact_filter(pool: sqlx::PgPool) -> eyre::Result<()> {
let env = TestEnv::new(pool).await;
let mh_alerting = env.create_ready_managed_host(1).await.0;
let mh_healthy = env.create_ready_managed_host(2).await.0;

let report = HealthReport {
source: "mock-bmc-intrusion".to_string(),
triggered_by: None,
observed_at: None,
successes: vec![],
alerts: vec![HealthProbeAlert {
id: HealthProbeId::from_str("IntrusionSensorTriggered")?,
target: Some("HostBMC".to_string()),
in_alert_since: None,
message: "Physical Chassis Intrusion Alert".to_string(),
tenant_message: None,
classifications: vec![
HealthAlertClassification::hardware(),
HealthAlertClassification::sensor_critical(),
HealthAlertClassification::prevent_allocations(),
],
}],
};

let mut txn = env.test_harness.db_txn().await;
db::machine::insert_health_report(
&mut txn,
&mh_alerting.host.id,
HealthReportApplyMode::Merge,
&report,
false,
)
.await?;
txn.commit().await?;

let app = make_test_app(&env.test_harness);
let response = app
.oneshot(
web_request_builder()
.uri("/admin/managed-host?health-alerts-filter=IntrusionSensorTriggered")
.body(Body::empty())
.unwrap(),
)
.await
.unwrap();
assert_eq!(response.status(), StatusCode::OK);

let body_bytes = response
.into_body()
.collect()
.await
.expect("Empty response body?")
.to_bytes();
let body_str = std::str::from_utf8(&body_bytes).expect("Invalid UTF-8 in body");

// Only the alerting host should show up, not the healthy one.
assert!(body_str.contains(&mh_alerting.host.id.to_string()));
assert!(!body_str.contains(&mh_healthy.host.id.to_string()));
Comment thread
RajMandaliya marked this conversation as resolved.

// The dynamic dropdown should include this alert ID as the selected option.
assert!(
body_str.contains(r#"<option value="IntrusionSensorTriggered" selected>"#),
"expected the alert ID to appear as a selected dropdown option"
);

Ok(())
}

#[crate::sqlx_test]
async fn test_managed_host_health_alert_filter_empty_state(pool: sqlx::PgPool) -> eyre::Result<()> {
let env = TestEnv::new(pool).await;
let _mh = env.create_ready_managed_host(1).await.0;

let app = make_test_app(&env.test_harness);
let response = app
.oneshot(
web_request_builder()
.uri("/admin/managed-host?health-alerts-filter=SomeAlertNoOneHas")
.body(Body::empty())
.unwrap(),
)
.await
.unwrap();
assert_eq!(response.status(), StatusCode::OK);

let body_bytes = response
.into_body()
.collect()
.await
.expect("Empty response body?")
.to_bytes();
let body_str = std::str::from_utf8(&body_bytes).expect("Invalid UTF-8 in body");

assert!(body_str.contains("No machines match the selected filters."));

Ok(())
}
7 changes: 7 additions & 0 deletions crates/api-web/templates/managed_host_individual_table.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@
<tr/>
</thead>
<tbody>
{% if hosts.is_empty() %}
{% if active_maintenance_filter == "maintenance" %}
<tr><td colspan="11">No machines match the selected filters.</td></tr>
{% else %}
<tr><td colspan="12">No machines match the selected filters.</td></tr>
{% endif %}
{% endif %}
Comment thread
RajMandaliya marked this conversation as resolved.
{% for host in hosts %}
<tr>
<td>{{ host.machine_id|machine_id_link|safe }}</td>
Expand Down
3 changes: 3 additions & 0 deletions crates/api-web/templates/managed_host_show.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ <h1>All Managed Hosts ({{ page.total_items() }})</h1>
<option value="all">All</option>
<option value="healthy" {% if active_health_alerts_filter == "healthy" %}selected{% endif %}>Healthy</option>
<option value="unhealthy" {% if active_health_alerts_filter == "unhealthy" %}selected{% endif %}>Unhealthy</option>
{% for a in health_alert_ids %}
<option value="{{ a }}" {% if active_health_alerts_filter == a.as_str() %}selected{% endif %}>{{ a }}</option>
{% endfor %}
</select>
</div>
<div class="filter-item">
Expand Down