Skip to main content

hpr_net/
catalog_update.rs

1//! The solid motors and data files [ThrustCurve.org](https://www.thrustcurve.org) lists that the
2//! bundled catalog lacks ([M5.7d][m5-7d], `hpr motors update`; [ADR-288][adr-288] ยง4).
3//!
4//! `hpr_motor` compiles in ThrustCurve.org's whole solid catalog as of one snapshot
5//! ([`hpr_motor::Catalog::bundled`]). [`update`] asks ThrustCurve's [search](crate::thrustcurve::Search)
6//! for every motor at once, keeps the solid ones (single-use and reload), and compares them with
7//! the bundle ([`Bundle`]). A motor is looked at more closely when its id isn't in the bundle,
8//! when its record lists another number of data files than the bundle holds for it, or when its
9//! record was updated on or after the snapshot's day. For each, it [downloads](crate::thrustcurve::Download)
10//! the RASP and the RockSim files, and every file whose id the bundle doesn't hold is newer than
11//! it. Each answer goes through the [`Client`], so it lands in the cache: the downloads are the
12//! ones [`crate::on_demand::find`] reads, so a caller that then finds each newer motor by name
13//! has it offline too.
14//!
15//! > **How far to trust it.** It finds what ThrustCurve's records say changed. A file replaced by
16//! > another under the same motor, with the record's file count and update day unchanged, isn't
17//! > found: no answer short of downloading every motor's files shows it. A motor the bundle holds
18//! > keeps flying its bundled file; a newer file of it is only cached. Tested on ThrustCurve's
19//! > answers recorded on 2026-10-29, which list nothing newer than the bundle, and on the same
20//! > answers against a bundle with one motor and one file taken out, which finds exactly those.
21//!
22//! [m5-7d]: https://hpr.fusionspace.co/decisions-and-roadmap.html#m5-7d
23//! [adr-288]: https://github.com/nrdptel/fusionspace-eridanus/blob/main/docs/decisions/0288-every-solid-motor-ships-in-the-package.md
24
25use std::collections::{BTreeMap, BTreeSet};
26
27use hpr_motor::Catalog;
28use serde::{Deserialize, Serialize};
29
30use crate::thrustcurve::{
31    self, DataFile, Download, Format, MAX_RESULTS, MotorRecord, Search, ThrustCurveError,
32};
33use crate::{Client, Fetched, Transport};
34
35/// What a bundled catalog holds, as [`update`] compares it: its snapshot's day and, for each
36/// motor's ThrustCurve id, the ids of the data files the snapshot listed for it, indexed or left
37/// out.
38#[derive(Debug, Clone, PartialEq, Eq)]
39pub struct Bundle {
40    /// The snapshot's day, `YYYY-MM-DD`.
41    pub captured: String,
42    motors: BTreeMap<String, BTreeSet<String>>,
43}
44
45impl Bundle {
46    /// What `catalog` holds. Ids are kept in lower case, as the API writes them.
47    #[must_use]
48    pub fn of(catalog: &Catalog) -> Self {
49        let mut motors: BTreeMap<String, BTreeSet<String>> = catalog
50            .motors
51            .iter()
52            .map(|motor| {
53                let files = motor
54                    .curves
55                    .iter()
56                    .map(|curve| curve.simfile_id.to_ascii_lowercase())
57                    .collect();
58                (motor.motor_id.to_ascii_lowercase(), files)
59            })
60            .collect();
61        for out in &catalog.left_out {
62            motors
63                .entry(out.motor_id.to_ascii_lowercase())
64                .or_default()
65                .insert(out.simfile_id.to_ascii_lowercase());
66        }
67        Self {
68            captured: catalog.snapshot.captured.clone(),
69            motors,
70        }
71    }
72
73    /// Takes a motor out, with its files, as if the snapshot hadn't listed it. Whether it held it.
74    pub fn remove_motor(&mut self, motor_id: &str) -> bool {
75        self.motors.remove(&motor_id.to_ascii_lowercase()).is_some()
76    }
77
78    /// Takes one data file out, as if the snapshot hadn't listed it. Whether it held it.
79    pub fn remove_file(&mut self, simfile_id: &str) -> bool {
80        let id = simfile_id.to_ascii_lowercase();
81        self.motors.values_mut().any(|files| files.remove(&id))
82    }
83
84    /// How many motors it holds.
85    #[must_use]
86    pub fn motors(&self) -> usize {
87        self.motors.len()
88    }
89
90    /// Whether ThrustCurve's record may list what the bundle lacks: a motor it doesn't hold,
91    /// another number of files than it holds, or an update on or after the snapshot's day (the
92    /// record gives a day, not a time, so that day counts as after).
93    fn may_differ(&self, record: &MotorRecord) -> bool {
94        let Some(files) = self.motors.get(&record.motor_id.to_ascii_lowercase()) else {
95            return true;
96        };
97        let listed = record.data_files.unwrap_or(0);
98        let updated = record
99            .updated_on
100            .as_deref()
101            .is_some_and(|day| day >= self.captured.as_str());
102        usize::try_from(listed).ok() != Some(files.len()) || updated
103    }
104
105    fn holds_file(&self, file: &DataFile) -> bool {
106        self.motors
107            .get(&file.motor_id.to_ascii_lowercase())
108            .is_some_and(|files| files.contains(&file.simfile_id.to_ascii_lowercase()))
109    }
110}
111
112/// A solid motor ThrustCurve lists with something the bundle lacks.
113#[non_exhaustive]
114#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
115pub struct Newer {
116    /// Its record, as the search answered it.
117    pub record: MotorRecord,
118    /// Whether the bundle lacks the motor itself, not only some of its files.
119    pub new_motor: bool,
120    /// Its RASP and RockSim files the bundle lacks, in the answers' order, RASP first.
121    pub files: Vec<DataFile>,
122}
123
124/// What [`update`] found.
125#[non_exhaustive]
126#[derive(Debug, Clone, PartialEq)]
127pub struct Update {
128    /// The solid motors ThrustCurve lists.
129    pub listed: usize,
130    /// The motors whose files were downloaded, as [`Bundle`] picks them.
131    pub looked_at: usize,
132    /// The motors with something newer than the bundle, in the search's order.
133    pub newer: Vec<Newer>,
134    /// The bundle's motors ThrustCurve no longer lists, by id.
135    pub unlisted: Vec<String>,
136    /// Each answer read, the search first.
137    pub fetched: Vec<Fetched>,
138}
139
140/// Compares ThrustCurve's solid catalog with `bundle`, as the module describes, and returns what
141/// it lists that the bundle lacks.
142///
143/// # Errors
144/// Any error of [`thrustcurve::fetch_whole_search`], which refuses a search that returns fewer
145/// motors than match and never caches it, or of [`thrustcurve::fetch_download`]; offline,
146/// [`crate::NetError::NotCached`] for an answer not in the cache.
147pub fn update<T: Transport>(
148    client: &Client<T>,
149    bundle: &Bundle,
150    now_s: u64,
151) -> Result<Update, ThrustCurveError> {
152    let search = Search {
153        max_results: Some(MAX_RESULTS),
154        ..Search::default()
155    };
156    let (answer, got) = thrustcurve::fetch_whole_search(client, &search, now_s)?;
157    let mut fetched = vec![got];
158    let solid: Vec<MotorRecord> = answer
159        .results
160        .into_iter()
161        .filter(|record| matches!(record.motor_type.as_deref(), Some("SU" | "reload")))
162        .collect();
163    let listed: BTreeSet<String> = solid
164        .iter()
165        .map(|record| record.motor_id.to_ascii_lowercase())
166        .collect();
167    let unlisted = bundle
168        .motors
169        .keys()
170        .filter(|id| !listed.contains(*id))
171        .cloned()
172        .collect();
173    let mut looked_at = 0;
174    let mut newer = Vec::new();
175    for record in &solid {
176        if !bundle.may_differ(record) {
177            continue;
178        }
179        looked_at += 1;
180        let mut files = Vec::new();
181        for format in [Format::Rasp, Format::RockSim] {
182            let download = Download::new(&record.motor_id, format)?;
183            let (answer, got) = thrustcurve::fetch_download(client, &download, now_s)?;
184            fetched.push(got);
185            files.extend(
186                answer
187                    .results
188                    .into_iter()
189                    .filter(|file| !bundle.holds_file(file)),
190            );
191        }
192        let new_motor = !bundle
193            .motors
194            .contains_key(&record.motor_id.to_ascii_lowercase());
195        if new_motor || !files.is_empty() {
196            newer.push(Newer {
197                record: record.clone(),
198                new_motor,
199                files,
200            });
201        }
202    }
203    Ok(Update {
204        listed: solid.len(),
205        looked_at,
206        newer,
207        unlisted,
208        fetched,
209    })
210}
211
212#[cfg(test)]
213mod tests {
214    use super::*;
215
216    fn record(id: &str, files: u32, updated: &str) -> MotorRecord {
217        let json = format!(
218            r#"{{"motorId":"{id}","manufacturer":"M","designation":"D","type":"SU",
219                "dataFiles":{files},"updatedOn":"{updated}"}}"#
220        );
221        serde_json::from_str(&json).unwrap()
222    }
223
224    fn bundle() -> Bundle {
225        let mut motors = BTreeMap::new();
226        motors.insert(
227            "aa".repeat(12),
228            ["bb".repeat(12), "cc".repeat(12)].into_iter().collect(),
229        );
230        Bundle {
231            captured: "2026-10-28".to_owned(),
232            motors,
233        }
234    }
235
236    /// A motor is looked at when the bundle lacks it, holds another number of its files, or its
237    /// record was updated on the snapshot's day or later; not otherwise.
238    #[test]
239    fn a_motor_is_looked_at_only_when_its_record_may_differ() {
240        let bundle = bundle();
241        let held = "aa".repeat(12);
242        assert!(!bundle.may_differ(&record(&held, 2, "2026-10-27")));
243        assert!(bundle.may_differ(&record(&held, 3, "2026-10-27")));
244        assert!(bundle.may_differ(&record(&held, 1, "2026-10-27")));
245        assert!(bundle.may_differ(&record(&held, 2, "2026-10-28")));
246        assert!(bundle.may_differ(&record(&held, 2, "2027-01-02")));
247        assert!(bundle.may_differ(&record(&"dd".repeat(12), 0, "2019-04-17")));
248        // Ids in either case are one motor.
249        assert!(!bundle.may_differ(&record(&"AA".repeat(12), 2, "2026-10-27")));
250    }
251
252    /// Taking a motor or a file out says whether the bundle held it.
253    #[test]
254    fn removing_says_whether_it_was_held() {
255        let mut bundle = bundle();
256        assert!(bundle.remove_file(&"CC".repeat(12)));
257        assert!(!bundle.remove_file(&"cc".repeat(12)));
258        assert!(bundle.remove_motor(&"aa".repeat(12)));
259        assert!(!bundle.remove_motor(&"aa".repeat(12)));
260        assert_eq!(bundle.motors(), 0);
261    }
262}