scientific-workflow 0.18.0

Configuration-driven scientific tasks, typed state, and durable recordings
Documentation
use super::*;

pub(super) fn apply(
    status: &mut Status,
    kind: &str,
    data: &Value,
    control: &RunControl,
    start: Instant,
    active_start: Instant,
) {
    status.control = control.status().into();
    status.sequence += 1;
    status.updated_at = utc_now();
    status.wall_ms = start.elapsed().as_millis() as u64;
    status.active_ms = control
        .now()
        .saturating_duration_since(active_start)
        .as_millis() as u64;
    status.pause_reason = if control.storage_paused() {
        Some("storage_pressure".into())
    } else if control.disk_paused() {
        Some("disk_pressure".into())
    } else if control.paused() {
        Some("manual".into())
    } else {
        None
    };
    if kind == "engine_running" {
        status.outcome = Outcome::Running;
    }
    if kind == "study_started" {
        status.study = data["study"].as_str().map(str::to_owned);
    }
    if kind == "engine_completed" {
        status.outcome = Outcome::Completed;
    }
    if kind == "engine_failed" {
        status.outcome = Outcome::Failed;
        status.message = data["reason"].as_str().map(str::to_owned);
    }
    if kind == "engine_cancelled" {
        status.outcome = Outcome::Cancelled;
    }
    if kind == "thread_allocations" {
        status.allocations = data.clone();
    }
    if let (Some(identity), Some(replicate)) =
        (data["identity"].as_str(), data["replicate"].as_u64())
    {
        let key = format!(
            "{}/{replicate}/{identity}",
            status.study.as_deref().unwrap_or("study")
        );
        let active_ms = status.active_ms;
        let task = status.tasks.entry(key).or_insert_with(|| json!({}));
        if let Some(fields) = data.as_object() {
            for (k, v) in fields {
                task[k] = v.clone();
            }
        }
        task["event"] = kind.into();
        if kind == "task_started" {
            task["active_ms_start"] = active_ms.into();
        }
        let done = task["iteration"]
            .as_u64()
            .or_else(|| task["completed"].as_u64());
        let total = task["target_iteration"]
            .as_u64()
            .or_else(|| task["total"].as_u64());
        if let (Some(done), Some(total), Some(start)) =
            (done, total, task["active_ms_start"].as_u64())
        {
            let elapsed = active_ms.saturating_sub(start) as f64 / 1000.;
            if done > 0 && elapsed > 0. {
                task["throughput_per_second"] = (done as f64 / elapsed).into();
                task["eta_seconds"] =
                    (total.saturating_sub(done) as f64 * elapsed / done as f64).into();
            }
        }
    }
}