git clone https://garage44.eu/jeroen/cartopolis.gitcartopolis-agent · 2026-11-30T18:32:28Z
feat(atlas): a data layer follows its source's commits A layer with follow on (the default) is refreshed as soon as its TripleXL dataset has a new commit, not only on its clock: every minute atlas reads each followed dataset's revision - one small request per dataset, not per layer - and refreshes the layers read at an older one. The revision is read before the query, so a commit landing between the two is seen as new on the next look. A source that failed rests for a tenth of its interval rather than being asked every minute. The clock stays as the backstop. Webhooks would need write rights on the dataset, which a reader does not have; the stream is a connection per dataset to keep open. Polling the revision costs one request a minute and is as current.
crates/atlas/src/feeds.rs | 81 ++++++++++++++++++++++++++++++++++++++++++++---
1 file changed, 77 insertions(+), 4 deletions(-)
diff --git a/crates/atlas/src/feeds.rs b/crates/atlas/src/feeds.rs
index 9a086289..cc6c5ce9 100644
--- a/crates/atlas/src/feeds.rs
+++ b/crates/atlas/src/feeds.rs
@@ -83,6 +83,14 @@ pub struct Def {
/// Ask the source again after this many hours.
#[serde(default = "default_refresh_hours")]
pub refresh_hours: u32,
+ /// Also ask again as soon as the dataset has a new commit: its revision
+ /// is looked at every minute ([`Feeds::moved`]).
+ #[serde(default = "default_follow")]
+ pub follow: bool,
+}
+
+fn default_follow() -> bool {
+ true
}
fn default_geometry() -> String {
@@ -180,6 +188,10 @@ pub struct State {
pub cells: u64,
#[serde(default)]
pub values: Vec<String>,
+ /// The dataset's revision the published records were read at, when the
+ /// source said.
+ #[serde(default)]
+ pub source_revision: Option<u64>,
}
/// A layer and its state, as the Admin page lists them.
@@ -309,8 +321,48 @@ impl Feeds {
/// cannot be written; the error is also kept in the layer's state.
pub fn refresh(&self, id: &str) -> Result<State, String> {
let def = self.get(id).ok_or("There is no such layer.")?;
+ // The revision first: a commit landing between the two is then seen
+ // as new on the next look, rather than taken as already read.
+ let revision = source_revision(&def.source.dataset).ok();
let fetched = fetch(&def);
- self.publish(&def, fetched)
+ let mut state = self.publish(&def, fetched)?;
+ if revision.is_some() {
+ let _guard = self.lock.lock().map_err(|e| e.to_string())?;
+ state.source_revision = revision;
+ self.keep_state(&def.id, &state)?;
+ }
+ Ok(state)
+ }
+
+ /// The layers that follow their source and whose dataset has moved past
+ /// the revision they were read at: one small request per dataset.
+ fn moved(&self, now_ms: u64) -> Vec<String> {
+ let mut by_dataset: BTreeMap<String, Vec<Listed>> = BTreeMap::new();
+ for l in self.list() {
+ let every = u64::from(l.def.refresh_hours) * 3_600_000;
+ // A source that failed is not asked again every minute.
+ let resting =
+ l.state.error.is_some() && now_ms.saturating_sub(l.state.tried_ms) < every / 10;
+ if l.def.follow && !resting {
+ by_dataset
+ .entry(l.def.source.dataset.clone())
+ .or_default()
+ .push(l);
+ }
+ }
+ let mut out = Vec::new();
+ for (dataset, layers) in by_dataset {
+ match source_revision(&dataset) {
+ Ok(r) => out.extend(
+ layers
+ .into_iter()
+ .filter(|l| l.state.source_revision != Some(r))
+ .map(|l| l.def.id),
+ ),
+ Err(error) => tracing::debug!(%dataset, %error, "could not read a dataset's revision"),
+ }
+ }
+ out
}
/// Publish one source answer (or its failure) for `def`.
@@ -445,9 +497,18 @@ impl Feeds {
tokio::spawn(async move {
loop {
let f = feeds.clone();
- let due = tokio::task::spawn_blocking(move || f.due(stamped::now_ms()))
- .await
- .unwrap_or_default();
+ let due = tokio::task::spawn_blocking(move || {
+ let now = stamped::now_ms();
+ let mut ids = f.due(now);
+ for id in f.moved(now) {
+ if !ids.contains(&id) {
+ ids.push(id);
+ }
+ }
+ ids
+ })
+ .await
+ .unwrap_or_default();
for id in due {
let f = feeds.clone();
let i = id.clone();
@@ -557,6 +618,15 @@ pub fn dataset_of(url: &str) -> Result<String, String> {
}
}
+/// A TripleXL dataset's current revision: it moves with every commit.
+fn source_revision(dataset: &str) -> Result<u64, String> {
+ let about: serde_json::Value =
+ serde_json::from_slice(&get(dataset, "application/json")?).map_err(|e| e.to_string())?;
+ about["revision"]
+ .as_u64()
+ .ok_or_else(|| format!("{dataset}: no revision"))
+}
+
/// "CC-BY-4.0" as people write it.
fn licence_words(id: &str) -> String {
if id.to_ascii_uppercase().starts_with("CC") {
@@ -912,6 +982,7 @@ mod tests {
credit: "Waterschap Noorderzijlvest open data".into(),
licence: "CC BY 4.0".into(),
refresh_hours: 6,
+ follow: true,
}
}
@@ -1065,6 +1136,8 @@ mod tests {
let feeds = Feeds::new(&dir.path().join("data"), &dir.path().join("site"));
feeds.save(&def()).unwrap();
let s = feeds.refresh("nzv-stuwen").unwrap();
+ assert!(s.source_revision.is_some(), "the dataset's revision is kept");
+ assert!(feeds.moved(stamped::now_ms()).is_empty(), "nothing moved since");
eprintln!("live: {} rows, {} placed, {} cells, values {:?}", s.rows, s.placed, s.cells, s.values);
assert!(s.cells > 10);
}