universal_exports/
source_facts.rs1use std::sync::Arc;
9use std::time::Duration;
10
11use ai6::{RegistryBackend, SourceFacts, StationRecord};
12
13use crate::SourceFactsFn;
14
15pub 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
23fn 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
32fn 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}