Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/analysis/src/control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ thread_local! {
}

/// The control of the run on this thread, if any.
pub(crate) fn current() -> Option<Arc<Control>> {
pub fn current() -> Option<Arc<Control>> {
CURRENT.with(|c| c.borrow().clone())
}

Expand Down
170 changes: 155 additions & 15 deletions crates/engine/src/cmd/gp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,117 @@ pub fn tool_complete(s: &mut Session, id: u64, done: ToolDone) -> Result<(), Str
complete(s, id, done.result, Some(done.full_ids))
}

#[cfg(not(target_arch = "wasm32"))]
/// A tool's run made ahead on a copy of the session, for a model the app runs on a thread of its
/// own ([`super::workflows::model_ahead`]): what it read and what it made, for the model's run
/// here to take instead of running the tool again.
pub(crate) struct RanAhead {
tool: String,
params: Value,
read: Snapshot<'static>,
/// The view's extent, when the tool asked for it.
view: Option<Option<rustgis_geom::Extent>>,
result: Result<rustgis_analysis::ToolResult, String>,
full_ids: FullIds,
millis: u64,
}

#[cfg(not(target_arch = "wasm32"))]
/// The tools of a model run ahead: recorded on the copy, then taken in order by the run here.
pub(crate) enum Ahead {
Recording(Vec<RanAhead>),
Taking(std::collections::VecDeque<RanAhead>),
}

#[cfg(not(target_arch = "wasm32"))]
/// What a model's run on a copy of the session stops at, as its run here does them: a tool that
/// writes files (which would be written twice), and plugin and GeoLibre tools, which run as they
/// always have.
const NOT_AHEAD: &str = "runs where the model runs";

#[cfg(not(target_arch = "wasm32"))]
/// Runs `run` taking the tools `ran` ahead: each one whose inputs are still the same as the run
/// ahead read them is taken; from the first that isn't, the tools run here. Restored on every
/// path, a panic included (raised again), so no later run takes them.
pub(crate) fn taking<T>(s: &mut Session, ran: Vec<RanAhead>, run: impl FnOnce(&mut Session) -> T) -> T {
s.tools_ahead = Some(Ahead::Taking(ran.into()));
let r = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| run(s)));
s.tools_ahead = None;
r.unwrap_or_else(|p| std::panic::resume_unwind(p))
}

#[cfg(not(target_arch = "wasm32"))]
/// The tools a copy of the session recorded ([`Ahead::Recording`]), taken.
pub(crate) fn ran_ahead(s: &mut Session) -> Vec<RanAhead> {
match s.tools_ahead.take() {
Some(Ahead::Recording(ran)) => ran,
_ => Vec::new(),
}
}

#[cfg(not(target_arch = "wasm32"))]
/// The next tool run ahead, if it is this one on the same inputs: its result, the streamed rows
/// it read whole and how long it took. Any other drops the rest, which came after it.
fn take_ahead(s: &mut Session, tool: &str, params: &Value) -> Option<(Result<rustgis_analysis::ToolResult, String>, FullIds, u64)> {
let Some(Ahead::Taking(ran)) = &mut s.tools_ahead else { return None };
let next = ran.pop_front()?;
let now = Snapshot::of(s);
let view = next.view.is_none_or(|v| v == view_extent(&now));
if next.tool == tool && next.params == *params && view && same_read(&next.read, &now) {
#[cfg(test)]
{
s.tools_taken += 1;
}
return Some((next.result, next.full_ids, next.millis));
}
s.tools_ahead = None;
None
}

#[cfg(not(target_arch = "wasm32"))]
/// Whether a tool reading `now` reads what it read in `was`: the same layers with the same rows
/// (for a streamed layer read whole, the same source), selections, rasters and projection, the
/// same tables and environment.
fn same_read(was: &Snapshot, now: &Snapshot) -> bool {
let layer = |a: &rustgis_map::Layer, b: &rustgis_map::Layer| {
let rows = match (&a.source, &b.source) {
// Read whole from its source, unless it has a selection (rows of the window).
(Some(x), Some(y)) if a.selection.is_empty() => x.same_opening(y) && a.dataset.fields == b.dataset.fields,
(None, None) => Read::unchanged(&a.dataset, &b.dataset),
_ => false,
} && a.selection == b.selection;
let raster = match (&a.raster, &b.raster) {
(Some(x), Some(y)) => {
x.path == y.path
&& match (&x.data, &y.data) {
(Some(p), Some(q)) => std::sync::Arc::ptr_eq(p, q),
(None, None) => true,
_ => false,
}
}
(None, None) => true,
_ => false,
};
a.id == b.id && a.name == b.name && a.definition_query == b.definition_query && rows && raster
};
let (m, n) = (&was.map, &now.map);
m.layers.len() == n.layers.len()
&& m.layers.iter().zip(&n.layers).all(|(a, b)| layer(a, b))
&& m.projection == n.projection
&& was.tables.len() == now.tables.len()
&& was.tables.iter().zip(now.tables.iter()).all(|(a, b)| a.id == b.id && Read::unchanged(&a.dataset, &b.dataset))
&& was.env == now.env
}

/// The view's longitude/latitude extent, as a tool asks for it ([`Inputs::view_extent`]): the
/// globe shows the whole Earth (the camera's flat extent means nothing there).
fn view_extent(snap: &Snapshot) -> Option<rustgis_geom::Extent> {
if snap.map.projection.is_globe() {
return Some(rustgis_geom::Extent::new(-180.0, -90.0, 180.0, 90.0));
}
Some(super::workspace::view_extent_of(&snap.map, snap.view_size)).filter(|e| !e.is_empty())
}

/// Fits a background run's result to edits made while it ran: its layer is the one it read,
/// whatever its name now; a selection of a layer that changed, or of a streamed layer whose
/// window moved, selects the same features (by id) where they now are; and an update of a layer
Expand Down Expand Up @@ -925,13 +1036,7 @@ impl Inputs for MapInputs<'_> {
flat.forward(c).map(|m| rustgis_geom::Coord::new(m.x * to_meter, m.y * to_meter))
}
fn view_extent(&self) -> Option<rustgis_geom::Extent> {
*self.view.get_or_init(|| {
// The globe shows the whole Earth (the camera's flat extent means nothing there).
if self.snap.map.projection.is_globe() {
return Some(rustgis_geom::Extent::new(-180.0, -90.0, 180.0, 90.0));
}
Some(super::workspace::view_extent_of(&self.snap.map, self.snap.view_size)).filter(|e| !e.is_empty())
})
*self.view.get_or_init(|| view_extent(&self.snap))
}
fn raster(&self, layer: &str) -> Option<RasterInput<'_>> {
let rl = self.snap.map.layer_by_name(layer)?.raster.as_ref()?;
Expand Down Expand Up @@ -1033,15 +1138,50 @@ pub fn run_tool_with(
let e = format!("{name} was cancelled before it started");
return apply(s, Ran { name, id: tool, command: None, started: t0 }, recorded(spec, params), Err(e), 0);
}
let inputs = map_inputs(s, rustgis_analysis::tool(&tool), &params)?;
let mut res = rustgis_analysis::run_with(&tool, &inputs, &params, control);
if let Ok(result) = &mut res {
result.messages.extend(inputs.messages.iter().cloned());
}
let ids = full_ids(&inputs.loaded);
drop(inputs);
#[cfg(not(target_arch = "wasm32"))]
let ran = take_ahead(s, &tool, &params);
#[cfg(target_arch = "wasm32")]
let ran = None;
let (mut res, ids, millis) = match ran {
Some(ran) => ran,
None => {
#[cfg(not(target_arch = "wasm32"))]
let recording = matches!(s.tools_ahead, Some(Ahead::Recording(_)));
#[cfg(not(target_arch = "wasm32"))]
if recording && !rustgis_analysis::tool(&tool).is_some_and(|t| !rustgis_analysis::is_external(t) && !writes_files(t)) {
Comment thread
pick2510 marked this conversation as resolved.
Resolved
return Err(NOT_AHEAD.into());
}
// A copy of the session runs under the control of the thread it runs on, which Cancel stops.
#[cfg(not(target_arch = "wasm32"))]
let current = rustgis_analysis::control::current().filter(|_| recording);
Comment thread
pick2510 marked this conversation as resolved.
Resolved
#[cfg(not(target_arch = "wasm32"))]
let control = current.as_ref().unwrap_or(control);
#[cfg(not(target_arch = "wasm32"))]
let read = recording.then(|| Snapshot::owned(s));
Comment thread
pick2510 marked this conversation as resolved.
Resolved
let inputs = map_inputs(s, rustgis_analysis::tool(&tool), &params)?;
let mut res = rustgis_analysis::run_with(&tool, &inputs, &params, control);
if let Ok(result) = &mut res {
result.messages.extend(inputs.messages.iter().cloned());
}
let ids = full_ids(&inputs.loaded);
#[cfg(not(target_arch = "wasm32"))]
let view = inputs.view.get().copied();
drop(inputs);
// A file made, with a SaveFile parameter or not, is written where the model runs.
#[cfg(not(target_arch = "wasm32"))]
if recording && matches!(&res, Ok(r) if matches!(r.output, Output::File { .. })) {
return Err(NOT_AHEAD.into());
}
let millis = now_ms().saturating_sub(t0);
#[cfg(not(target_arch = "wasm32"))]
if let (Some(read), Some(Ahead::Recording(ran))) = (read, &mut s.tools_ahead) {
let (result, full_ids) = (res.clone(), ids.clone());
ran.push(RanAhead { tool: tool.clone(), params: params.clone(), read, view, result, full_ids, millis });
}
(res, ids, millis)
}
};
fit_to_windows(s, &mut res, &ids);
let millis = now_ms().saturating_sub(t0);
let spec = rustgis_analysis::tool(&tool);
let name = spec.map(|t| t.name).unwrap_or("Tool").to_string();
apply(s, Ran { name, id: tool, command: None, started: t0 }, recorded(spec, params), res, millis)
Expand Down
5 changes: 4 additions & 1 deletion crates/engine/src/cmd/reads.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Reads the desktop app makes on a thread of its own before a command adds what they read
//! (#1291 step 3): Add Data's whole-file reads (a raster, a raster GDAL converts, a GeoJSON,
//! delimited text or spreadsheet read whole) and Load All Features. [`read_ahead`] takes what
//! delimited text or spreadsheet read whole), Load All Features, and a model's tools, run on a
//! copy of the session. [`read_ahead`] takes what
//! the read needs of the session, [`ReadAhead::run`] reads on the thread, and the app then runs
//! the command as it would have with the read [`Read::placed`] in the session: the command takes
//! it instead of reading again, so what it adds, says, logs and puts in Undo is the same as when
Expand Down Expand Up @@ -62,6 +63,8 @@ pub fn read_ahead(s: &Session, command: &str, p: &Value) -> Option<ReadAhead> {
match command {
"layer.addData" => super::layer::add_data_ahead(p),
"layer.loadAll" => super::layer::load_all_ahead(s, p),
#[cfg(not(target_arch = "wasm32"))]
"model.run" => Some(super::workflows::model_ahead(s, p)),
_ => None,
}
}
Expand Down
39 changes: 38 additions & 1 deletion crates/engine/src/cmd/workflows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -497,8 +497,45 @@ fn model_graph(s: &Session, p: &Value) -> Result<(String, crate::project::ModelG
Ok((model.name.clone(), g, s.trusted_models.contains(&model.name.to_ascii_lowercase())))
}

/// Runs a model's graph, or the graph passed.
/// Runs a model's graph, or the graph passed: taking the tools the app ran ahead
/// ([`model_ahead`]) whose inputs are still the same.
fn run_model(s: &mut Session, p: &Value) -> CmdResult {
#[cfg(not(target_arch = "wasm32"))]
if let Some(ran) = super::reads::take::<Vec<super::gp::RanAhead>>(s, MODEL_AHEAD) {
return super::gp::taking(s, ran, |s| run_model_here(s, p));
}
run_model_here(s, p)
}

#[cfg(not(target_arch = "wasm32"))]
/// The key of a model's tools run ahead ([`model_ahead`]).
const MODEL_AHEAD: &str = "model.run";

#[cfg(not(target_arch = "wasm32"))]
/// A model's run on a copy of the session, made on a thread of its own: it records what each of
/// its tools read and made, for `model.run` to take instead of running them again, so what the
/// run adds, says, logs and puts in Undo is the same as when it runs here. It stops at a tool
/// that writes files, a plugin's or GeoLibre's: from there the run here runs them as it always
/// has. What it says and how it ends are the run here's to say.
pub(crate) fn model_ahead(s: &Session, p: &Value) -> super::reads::ReadAhead {
let (project, trusted) = (s.project.clone(), s.trusted_models.clone());
let (view_size, active_layer, p) = (s.view_size, s.active_layer, p.clone());
// What its tools ask of servers and files goes in this session's Diagnostics, as when they
// run here: the run here takes their results and asks nothing.
let requests = s.diagnostics.capture();
super::reads::ahead("Run Model", MODEL_AHEAD.into(), move || {
let mut copy = Session::new(project);
copy.requests_into = Some(requests);
copy.trusted_models = trusted;
copy.view_size = view_size;
copy.active_layer = active_layer;
copy.tools_ahead = Some(super::gp::Ahead::Recording(Vec::new()));
let _ = copy.run("model.run", &p);
super::gp::ran_ahead(&mut copy)
})
}

fn run_model_here(s: &mut Session, p: &Value) -> CmdResult {
// A model saved as steps runs as steps: `%n` may stand anywhere in them, and inputs may be files.
if p.get("graph").is_none_or(Value::is_null) {
let model = match ps(p, "name") {
Expand Down
21 changes: 21 additions & 0 deletions crates/engine/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -753,6 +753,18 @@ pub struct Session {
/// A read the app made on a thread of its own for the command it runs next, which takes it
/// instead of reading again ([`cmd::reads`]).
pub(crate) read_ahead: Option<cmd::reads::Read>,
/// A model's tools run ahead on a copy of the session, which a model's run here takes
/// ([`cmd::gp::Ahead::Taking`]), or which that copy records ([`cmd::gp::Ahead::Recording`]).
#[cfg(not(target_arch = "wasm32"))]
pub(crate) tools_ahead: Option<cmd::gp::Ahead>,
/// Where the requests a copy of the session makes for a model's run ahead go
/// ([`cmd::workflows::model_ahead`]): the session's own Diagnostics, as when they're made
/// there. Its notices stay its own: the run there says them.
#[cfg(not(target_arch = "wasm32"))]
pub(crate) requests_into: Option<rustgis_data::diagnostics::Capture>,
/// The tools run ahead that a model's run took, for the tests.
#[cfg(test)]
pub(crate) tools_taken: usize,
/// Set where a person at a terminal waits on a command (`rustgis-cli run` with stdin and
/// stderr a terminal), who can stop it with Ctrl-C: tools and rasters take no work cap
/// ([`rustgis_analysis::control::Control::stoppable`]). A program that calls it (an agent, a
Expand Down Expand Up @@ -1015,6 +1027,12 @@ impl Session {
zarr_off_frame: false,
zarr_seen: 0,
read_ahead: None,
#[cfg(not(target_arch = "wasm32"))]
tools_ahead: None,
#[cfg(not(target_arch = "wasm32"))]
requests_into: None,
#[cfg(test)]
tools_taken: 0,
stoppable: false,
};
for text in label_notices {
Expand Down Expand Up @@ -1194,6 +1212,9 @@ impl Session {
/// panic restores it. A command run by itself (not as a part of another) that changes the
/// project, or reaches a file or a service, goes into the session's log (#797).
pub fn run(&mut self, id: &str, params: &Value) -> CmdResult {
#[cfg(not(target_arch = "wasm32"))]
let _diagnostic_scope = self.requests_into.as_ref().map_or_else(|| self.diagnostics.enter(), |c| c.enter());
#[cfg(target_arch = "wasm32")]
let _diagnostic_scope = self.diagnostics.enter();
let source = self.registry.get(id).map(|spec| spec.id).or_else(|| id.starts_with(rustgis_plugins::PREFIX).then_some("Plugin"));
// Restored on every path, including panics outside `guarded`, so a later notice is not
Expand Down
Loading
Loading