use std::{cmp::Reverse, collections::BTreeMap, path::Path, str::FromStr};
use serde::Deserialize;
use super::{
generation::UNSUPPORTED_MANIFEST_VERSION, plan_relative, read_json, run_relative, WorkflowStore,
};
use crate::workflow::{
check_schema_version, NodeId, NodeState, PlanId, RunId, RunLifecycle, ScopeInstanceId,
WorkflowError, WorkflowOutcome, WorkflowResult, PLAN_MANIFEST_VERSION, RUN_MANIFEST_VERSION,
};
#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) struct UnsupportedRecord<Id> {
pub(crate) id: Id,
pub(crate) created_at_unix_nanos: u64,
pub(crate) workspace_identity: String,
pub(crate) name: String,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) struct Inventory<T, Id> {
pub(crate) items: Vec<T>,
pub(crate) unsupported: Vec<UnsupportedRecord<Id>>,
}
impl<T, Id> Default for Inventory<T, Id> {
fn default() -> Self {
Self {
items: Vec::new(),
unsupported: Vec::new(),
}
}
}
pub(crate) type PlanInventory = Inventory<PlanInventoryItem, PlanId>;
pub(crate) type RunInventory = Inventory<RunInventoryItem, RunId>;
pub(super) enum InventoryRow<T, Id> {
Current(T),
Unsupported(UnsupportedRecord<Id>),
}
#[derive(Deserialize)]
struct InventoryManifest {
schema_version: u32,
#[serde(default)]
created_at_unix_nanos: u64,
workspace_identity: String,
#[serde(default)]
name: String,
#[serde(default)]
step_count: usize,
}
impl InventoryManifest {
fn unsupported<Id>(self, id: Id) -> UnsupportedRecord<Id> {
UnsupportedRecord {
id,
created_at_unix_nanos: self.created_at_unix_nanos,
workspace_identity: self.workspace_identity,
name: self.name,
}
}
}
#[derive(Deserialize)]
struct PlanSummary {
plan_id: PlanId,
#[serde(flatten)]
metadata: InventoryManifest,
}
#[derive(Deserialize)]
struct RunSummary {
run_id: RunId,
#[serde(flatten)]
metadata: InventoryManifest,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) struct PlanInventoryItem {
pub(crate) plan_id: PlanId,
pub(crate) created_at_unix_nanos: u64,
pub(crate) workspace_identity: String,
pub(crate) name: String,
pub(crate) step_count: usize,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) struct RunInventoryItem {
pub(crate) run_id: RunId,
pub(crate) created_at_unix_nanos: u64,
pub(crate) workspace_identity: String,
pub(crate) name: String,
pub(crate) lifecycle: RunLifecycle,
pub(crate) outcome: Option<WorkflowOutcome>,
pub(crate) done_steps: usize,
pub(crate) total_steps: usize,
}
#[derive(Debug, Deserialize)]
struct InventoryStateFile {
state: InventoryWorkflowState,
}
#[derive(Debug, Deserialize)]
struct InventoryWorkflowState {
lifecycle: RunLifecycle,
scopes: BTreeMap<ScopeInstanceId, InventoryScopeState>,
}
#[derive(Debug, Deserialize)]
struct InventoryScopeState {
nodes: BTreeMap<NodeId, NodeState>,
result: Option<InventoryScopeResult>,
}
#[derive(Debug, Deserialize)]
struct InventoryScopeResult {
outcome: WorkflowOutcome,
}
#[derive(Debug, Deserialize)]
struct RevisionStateFile {
state: RevisionOnly,
}
#[derive(Debug, Deserialize)]
struct RevisionOnly {
revision: u64,
}
#[derive(Debug, Deserialize)]
struct LifecycleStateFile {
state: LifecycleOnly,
}
#[derive(Debug, Deserialize)]
struct LifecycleOnly {
lifecycle: RunLifecycle,
}
impl WorkflowStore {
pub(crate) fn list_plan_inventory(&self) -> WorkflowResult<PlanInventory> {
let mut inventory = PlanInventory::default();
for name in self.root.directory_names(Path::new("plans"))? {
let Ok(name) = name.into_string() else {
continue;
};
let Ok(id) = PlanId::from_str(&name) else {
continue;
};
match self.read_plan_row(id)? {
InventoryRow::Current(plan) => inventory.items.push(plan),
InventoryRow::Unsupported(record) => inventory.unsupported.push(record),
}
}
inventory
.items
.sort_by_key(|plan| Reverse((plan.created_at_unix_nanos, plan.plan_id)));
inventory
.unsupported
.sort_by_key(|record| Reverse((record.created_at_unix_nanos, record.id)));
Ok(inventory)
}
pub(crate) fn list_run_inventory(&self) -> WorkflowResult<RunInventory> {
let mut inventory = RunInventory::default();
for name in self.root.directory_names(Path::new("runs"))? {
let Ok(name) = name.into_string() else {
continue;
};
let Ok(id) = RunId::from_str(&name) else {
continue;
};
match self.read_run_row(id)? {
InventoryRow::Current(run) => inventory.items.push(run),
InventoryRow::Unsupported(record) => inventory.unsupported.push(record),
}
}
inventory
.items
.sort_by_key(|run| Reverse((run.created_at_unix_nanos, run.run_id)));
inventory
.unsupported
.sort_by_key(|record| Reverse((record.created_at_unix_nanos, record.id)));
Ok(inventory)
}
pub(crate) fn read_run_revision(&self, id: RunId) -> WorkflowResult<u64> {
let state: RevisionStateFile =
read_json(&self.root, &run_relative(id, Path::new("state.json")))?;
Ok(state.state.revision)
}
pub(crate) fn read_run_lifecycle(&self, id: RunId) -> WorkflowResult<RunLifecycle> {
let state: LifecycleStateFile =
read_json(&self.root, &run_relative(id, Path::new("state.json")))?;
Ok(state.state.lifecycle)
}
pub(crate) fn plan_workspace_identity(&self, id: PlanId) -> WorkflowResult<String> {
Ok(match self.read_plan_row(id)? {
InventoryRow::Current(plan) => plan.workspace_identity,
InventoryRow::Unsupported(record) => record.workspace_identity,
})
}
pub(crate) fn run_workspace_identity(&self, id: RunId) -> WorkflowResult<String> {
Ok(match self.read_run_row(id)? {
InventoryRow::Current(run) => run.workspace_identity,
InventoryRow::Unsupported(record) => record.workspace_identity,
})
}
pub(super) fn read_run_row(
&self,
id: RunId,
) -> WorkflowResult<InventoryRow<RunInventoryItem, RunId>> {
let summary: RunSummary =
read_json(&self.root, &run_relative(id, Path::new("manifest.json")))?;
if summary.run_id != id {
return super::corrupt(
&self.layout.run_manifest(id),
"run manifest ID differs from its directory ID",
);
}
let manifest = summary.metadata;
if manifest.schema_version == UNSUPPORTED_MANIFEST_VERSION {
return Ok(InventoryRow::Unsupported(manifest.unsupported(id)));
}
check_schema_version(
"run manifest",
manifest.schema_version,
RUN_MANIFEST_VERSION,
)?;
let state: InventoryStateFile =
read_json(&self.root, &run_relative(id, Path::new("state.json")))?;
let scope = state
.state
.scopes
.get(&ScopeInstanceId::ROOT)
.ok_or_else(|| WorkflowError::Corrupt {
path: self.layout.run_state(id),
reason: "run has no root scope instance".to_owned(),
})?;
Ok(InventoryRow::Current(RunInventoryItem {
run_id: id,
created_at_unix_nanos: manifest.created_at_unix_nanos,
workspace_identity: manifest.workspace_identity,
name: manifest.name,
lifecycle: state.state.lifecycle,
outcome: if state.state.lifecycle == RunLifecycle::Completed {
scope.result.as_ref().map(|result| result.outcome)
} else {
None
},
done_steps: terminal_count(scope.nodes.values()),
total_steps: manifest.step_count,
}))
}
pub(super) fn read_plan_row(
&self,
id: PlanId,
) -> WorkflowResult<InventoryRow<PlanInventoryItem, PlanId>> {
let summary: PlanSummary =
read_json(&self.root, &plan_relative(id, Path::new("manifest.json")))?;
if summary.plan_id != id {
return super::corrupt(
&self.layout.plan_manifest(id),
"plan manifest ID differs from its directory ID",
);
}
let manifest = summary.metadata;
if manifest.schema_version == UNSUPPORTED_MANIFEST_VERSION {
return Ok(InventoryRow::Unsupported(manifest.unsupported(id)));
}
check_schema_version(
"plan manifest",
manifest.schema_version,
PLAN_MANIFEST_VERSION,
)?;
Ok(InventoryRow::Current(PlanInventoryItem {
plan_id: id,
created_at_unix_nanos: manifest.created_at_unix_nanos,
workspace_identity: manifest.workspace_identity,
name: manifest.name,
step_count: manifest.step_count,
}))
}
}
fn terminal_count<'a>(nodes: impl Iterator<Item = &'a NodeState>) -> usize {
nodes.filter(|node| node.terminal().is_some()).count()
}