Skip to main content

elasticctl_api/alerts_ops/
recheck.rs

1//! Alert state re-checks.
2
3use super::*;
4use crate::{Observed, Preconditions};
5
6/// The alert state needed for a re-check.
7#[derive(Debug, Clone, Copy, PartialEq, Eq)]
8pub enum AlertFacet {
9    /// Compare workflow status.
10    Status,
11    /// Compare presence only.
12    Presence,
13}
14
15/// Read alert states by ID, returning absent state for missing alerts.
16pub async fn observe_alerts(
17    t: &Transport,
18    ids: &[String],
19    facet: AlertFacet,
20) -> Result<Preconditions> {
21    super::require_targets(ids)?;
22    let mut unique = Vec::with_capacity(ids.len());
23    for id in ids {
24        if !unique.contains(id) {
25            unique.push(id.clone());
26        }
27    }
28    let page = alerts::search(t, &super::resolve_body(&unique)).await?;
29    Ok(Preconditions(
30        unique
31            .into_iter()
32            .map(|id| {
33                let state = page.hits.iter().find(|hit| hit.id == id).map(|hit| {
34                    super::observed::alert_state(super::workflow_status(&hit.source), facet)
35                });
36                Observed {
37                    object: "alert",
38                    id,
39                    state,
40                }
41            })
42            .collect(),
43    ))
44}