|
| 1 | +//! Operator policy per reporting source. Currently the reachability mode |
| 2 | +//! (how the source's silence bears on its servers' reachability); the |
| 3 | +//! ingest mode follows in a later change. Absent rows mean the defaults. |
| 4 | +
|
| 5 | +use std::collections::HashMap; |
| 6 | + |
| 7 | +use commons_errors::{AppError, Result}; |
| 8 | +use commons_types::source::ReachabilityMode; |
| 9 | +use diesel::prelude::*; |
| 10 | +use diesel::sql_types; |
| 11 | +use diesel_async::{AsyncPgConnection, RunQueryDsl}; |
| 12 | +use jiff::Timestamp; |
| 13 | + |
| 14 | +/// A reporting source with its reachability policy and fleet-wide |
| 15 | +/// last-seen, for the operator source list. Reserved sources are excluded. |
| 16 | +#[derive(Clone, Debug)] |
| 17 | +pub struct SourceInfo { |
| 18 | + pub source: String, |
| 19 | + pub reachability: ReachabilityMode, |
| 20 | + pub last_seen: Option<Timestamp>, |
| 21 | +} |
| 22 | + |
| 23 | +/// One source's policy row. |
| 24 | +#[derive(Clone, Debug, Queryable, Selectable)] |
| 25 | +#[diesel(table_name = crate::schema::source_policies)] |
| 26 | +#[diesel(check_for_backend(diesel::pg::Pg))] |
| 27 | +pub struct SourcePolicy { |
| 28 | + pub source: String, |
| 29 | + #[diesel(deserialize_as = String, serialize_as = String)] |
| 30 | + pub reachability: ReachabilityMode, |
| 31 | + #[diesel(deserialize_as = jiff_diesel::Timestamp, serialize_as = jiff_diesel::Timestamp)] |
| 32 | + pub created_at: Timestamp, |
| 33 | + #[diesel(deserialize_as = jiff_diesel::Timestamp, serialize_as = jiff_diesel::Timestamp)] |
| 34 | + pub updated_at: Timestamp, |
| 35 | +} |
| 36 | + |
| 37 | +impl SourcePolicy { |
| 38 | + /// Every source policy row, ordered by source. |
| 39 | + pub async fn list(db: &mut AsyncPgConnection) -> Result<Vec<Self>> { |
| 40 | + use crate::schema::source_policies::dsl; |
| 41 | + dsl::source_policies |
| 42 | + .select(Self::as_select()) |
| 43 | + .order(dsl::source.asc()) |
| 44 | + .load(db) |
| 45 | + .await |
| 46 | + .map_err(AppError::from) |
| 47 | + } |
| 48 | + |
| 49 | + /// Every non-reserved reporting source (those with catalogued checks), |
| 50 | + /// with its reachability mode (defaulting to `on`) and the most recent |
| 51 | + /// time any of its checks was seen fleet-wide. Ordered by source. |
| 52 | + pub async fn list_sources(db: &mut AsyncPgConnection) -> Result<Vec<SourceInfo>> { |
| 53 | + #[derive(QueryableByName)] |
| 54 | + struct Row { |
| 55 | + #[diesel(sql_type = sql_types::Text)] |
| 56 | + source: String, |
| 57 | + #[diesel(sql_type = sql_types::Text)] |
| 58 | + reachability: String, |
| 59 | + #[diesel(sql_type = sql_types::Nullable<sql_types::Timestamptz>)] |
| 60 | + last_seen: Option<jiff_diesel::Timestamp>, |
| 61 | + } |
| 62 | + let rows: Vec<Row> = diesel::sql_query( |
| 63 | + "SELECT cp.source, \ |
| 64 | + coalesce(sp.reachability, 'on') AS reachability, \ |
| 65 | + max(cp.last_seen) AS last_seen \ |
| 66 | + FROM check_policies cp \ |
| 67 | + LEFT JOIN source_policies sp ON sp.source = cp.source \ |
| 68 | + WHERE cp.source NOT IN ('canopy', 'manual') \ |
| 69 | + GROUP BY cp.source, sp.reachability \ |
| 70 | + ORDER BY cp.source", |
| 71 | + ) |
| 72 | + .load(db) |
| 73 | + .await?; |
| 74 | + Ok(rows |
| 75 | + .into_iter() |
| 76 | + .map(|r| SourceInfo { |
| 77 | + source: r.source, |
| 78 | + reachability: r.reachability.parse().unwrap_or_default(), |
| 79 | + last_seen: r.last_seen.map(Into::into), |
| 80 | + }) |
| 81 | + .collect()) |
| 82 | + } |
| 83 | + |
| 84 | + /// Each source's reachability mode. Sources without a row are absent |
| 85 | + /// here; callers default them to [`ReachabilityMode::On`]. |
| 86 | + pub async fn modes(db: &mut AsyncPgConnection) -> Result<HashMap<String, ReachabilityMode>> { |
| 87 | + use crate::schema::source_policies::dsl; |
| 88 | + let rows: Vec<(String, String)> = dsl::source_policies |
| 89 | + .select((dsl::source, dsl::reachability)) |
| 90 | + .load(db) |
| 91 | + .await?; |
| 92 | + Ok(rows |
| 93 | + .into_iter() |
| 94 | + .filter_map(|(source, mode)| mode.parse().ok().map(|m| (source, m))) |
| 95 | + .collect()) |
| 96 | + } |
| 97 | + |
| 98 | + /// Set a source's reachability mode, creating its policy row if needed. |
| 99 | + pub async fn set_reachability( |
| 100 | + db: &mut AsyncPgConnection, |
| 101 | + source: &str, |
| 102 | + mode: ReachabilityMode, |
| 103 | + ) -> Result<()> { |
| 104 | + use crate::schema::source_policies::dsl; |
| 105 | + diesel::insert_into(dsl::source_policies) |
| 106 | + .values(( |
| 107 | + dsl::source.eq(source), |
| 108 | + dsl::reachability.eq(mode.to_string()), |
| 109 | + )) |
| 110 | + .on_conflict(dsl::source) |
| 111 | + .do_update() |
| 112 | + .set(( |
| 113 | + dsl::reachability.eq(mode.to_string()), |
| 114 | + dsl::updated_at.eq(jiff_diesel::Timestamp::from(Timestamp::now())), |
| 115 | + )) |
| 116 | + .execute(db) |
| 117 | + .await?; |
| 118 | + Ok(()) |
| 119 | + } |
| 120 | +} |
0 commit comments