Skip to main content

universal_exports/
source_facts.rs

1//! What the Stations publish of each Operation's Source instances at check-in.
2//!
3//! The gateway loads no Source provider, so a Station, which does, publishes each instance's web
4//! page, project and intake labels under `source_facts` (never a credential). The gateway takes an
5//! instance's facts from the newest check-in of a Station that serves its Operation; a Station
6//! that does not serve it is not believed, and a check-in with no `source_facts` says nothing.
7
8use std::sync::Arc;
9use std::time::Duration;
10
11use ai6::{RegistryBackend, SourceFacts, StationRecord};
12
13use crate::SourceFactsFn;
14
15/// The hook that reads Source facts from the Stations' latest check-ins in `registry`.
16pub fn published_source_facts(registry: Arc<dyn RegistryBackend>) -> SourceFactsFn {
17    Arc::new(move |operation, instance, _provider, _settings| {
18        let records = registry.stations(Duration::MAX).ok()?;
19        newest(&records, operation, instance)
20    })
21}
22
23/// Whether the check-in lists `operation` as one its Station has a checkout of or may place
24/// Missions of.
25fn serves(record: &StationRecord, operation: &str) -> bool {
26    let check_in = &record.check_in().common;
27    [&check_in.operations, &check_in.runnable_operations]
28        .into_iter()
29        .any(|listed| listed.iter().any(|id| id == operation))
30}
31
32/// What the newest check-in of a Station serving `operation` says of its `instance`.
33fn newest(records: &[StationRecord], operation: &str, instance: &str) -> Option<SourceFacts> {
34    records
35        .iter()
36        .filter(|record| serves(record, operation))
37        .filter_map(|record| {
38            let published = record
39                .check_in()
40                .common
41                .source_facts
42                .as_ref()?
43                .get(operation)?
44                .get(instance)?;
45            Some((record.seen(), published))
46        })
47        .max_by(|left, right| left.0.total_cmp(&right.0))
48        .map(|(_, published)| ai6::published_facts(published))
49}