use chrono::{DateTime, Utc};
use rand::RngExt;
use rand_chacha::ChaCha8Rng;
use serde_yaml_ng::Value;
use crate::diagnostic::DiagnosticBag;
use crate::generate::registry::{
ArgumentSpec, Buffering, ColumnScope, CompileContext, CompiledPlanner, Determinism,
PlannerDescriptor, PlannerFactory, PlannerPredicate, PredicateGuard, Verification,
};
use crate::generate::seed::StreamId;
use crate::generate::value::{GenerateError, GeneratedValue};
use crate::synthetic::model::PlannerConfig;
use crate::synthetic::schema::{PortableColumn, PortableTable, SqlTypeFamily};
const COMPLETION_BASE_SECS: i64 = 1_704_067_200;
const COMPLETION_SPAN_SECS: i64 = 30 * 86_400;
pub static WORKFLOW_PROGRESS_COUNTERS_DESCRIPTOR: PlannerDescriptor = PlannerDescriptor {
kind: "workflow.progress_counters",
aliases: &[],
summary: "Coordinates total/processed/succeeded/failed/pending counters, status, and completion so each row's lifecycle counters agree exactly.",
arguments: &[
ArgumentSpec {
name: "columns",
required: true,
summary: "Maps total and optional counter, status, and completion roles to columns.",
},
ArgumentSpec {
name: "total",
required: false,
summary: "Configures the fixed or uniform total-count draw.",
},
ArgumentSpec {
name: "progress",
required: false,
summary: "Selects a fixed lifecycle state or a weighted state mixture.",
},
ArgumentSpec {
name: "partition",
required: false,
summary: "Selects exact or allow_unclassified counter partitioning.",
},
ArgumentSpec {
name: "unclassified_ratio",
required: false,
summary: "Share of processed rows left unclassified under allow_unclassified partitioning.",
},
ArgumentSpec {
name: "success_ratio",
required: false,
summary: "Share of classified processed rows assigned to succeeded rather than failed.",
},
ArgumentSpec {
name: "completed_statuses",
required: false,
summary: "Status labels available to completed rows.",
},
ArgumentSpec {
name: "active_statuses",
required: false,
summary: "Status labels available to active and not-started rows.",
},
],
writes: ColumnScope::Configured,
reads: ColumnScope::None,
determinism: Determinism::Deterministic,
buffering: Buffering::Streaming,
verification: Verification::Supported,
cross_table: false,
};
pub struct ProgressCountersFactory;
impl PlannerFactory for ProgressCountersFactory {
fn descriptor(&self) -> &'static PlannerDescriptor {
&WORKFLOW_PROGRESS_COUNTERS_DESCRIPTOR
}
fn compile(
&self,
config: &PlannerConfig,
context: &CompileContext<'_>,
) -> Result<Box<dyn CompiledPlanner>, DiagnosticBag> {
compile_progress(config, context)
.map(|planner| Box::new(planner) as Box<dyn CompiledPlanner>)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum LifecycleState {
Complete,
Active,
NotStarted,
}
enum StateMix {
Fixed(LifecycleState),
Weighted {
complete: f64,
active: f64,
not_started: f64,
},
}
impl StateMix {
fn select(&self, r: f64) -> LifecycleState {
match self {
StateMix::Fixed(state) => *state,
StateMix::Weighted {
complete,
active,
not_started,
} => {
let total = complete + active + not_started;
let point = r * total;
if point < *complete {
LifecycleState::Complete
} else if point < complete + active {
LifecycleState::Active
} else {
LifecycleState::NotStarted
}
}
}
}
fn reachable(&self) -> (bool, bool, bool) {
match self {
StateMix::Fixed(LifecycleState::Complete) => (true, false, false),
StateMix::Fixed(LifecycleState::Active) => (false, true, false),
StateMix::Fixed(LifecycleState::NotStarted) => (false, false, true),
StateMix::Weighted {
complete,
active,
not_started,
} => (*complete > 0.0, *active > 0.0, *not_started > 0.0),
}
}
}
enum TotalDraw {
Fixed(i128),
Uniform { min: i128, max: i128 },
}
impl TotalDraw {
fn draw(&self, rng: &mut ChaCha8Rng) -> i128 {
match self {
TotalDraw::Fixed(value) => *value,
TotalDraw::Uniform { min, max } => {
if max <= min {
*min
} else {
rng.random_range(*min..=*max)
}
}
}
}
fn maximum(&self) -> i128 {
match self {
TotalDraw::Fixed(value) => *value,
TotalDraw::Uniform { max, .. } => *max,
}
}
}
#[derive(Clone, Copy)]
enum Partition {
Exact,
AllowUnclassified { unclassified_ratio: f64 },
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Role {
Total,
Processed,
Succeeded,
Failed,
Pending,
Status,
CompletedAt,
}
const ROLE_ORDER: [(&str, Role); 7] = [
("total", Role::Total),
("processed", Role::Processed),
("succeeded", Role::Succeeded),
("failed", Role::Failed),
("pending", Role::Pending),
("status", Role::Status),
("completed_at", Role::CompletedAt),
];
struct Slot {
role: Role,
name: String,
family: SqlTypeFamily,
}
struct ProgressCountersPlanner {
writes: Vec<String>,
slots: Vec<Slot>,
total_draw: TotalDraw,
state_mix: StateMix,
partition: Partition,
success_ratio: f64,
completed_statuses: Vec<String>,
active_statuses: Vec<String>,
total_rng: ChaCha8Rng,
state_rng: ChaCha8Rng,
progress_rng: ChaCha8Rng,
status_rng: ChaCha8Rng,
completion_rng: ChaCha8Rng,
predicates: Vec<PlannerPredicate>,
}
struct RowCounts {
total: i128,
processed: i128,
succeeded: i128,
failed: i128,
pending: i128,
status: String,
completed_at: Option<String>,
}
impl CompiledPlanner for ProgressCountersPlanner {
fn writes(&self) -> &[String] {
&self.writes
}
fn generate_row(
&mut self,
_row_index: u64,
output: &mut [GeneratedValue],
) -> Result<(), GenerateError> {
let counts = self.draw_row();
for (slot, cell) in self.slots.iter().zip(output.iter_mut()) {
*cell = match slot.role {
Role::Total => render_counter(counts.total, &slot.family),
Role::Processed => render_counter(counts.processed, &slot.family),
Role::Succeeded => render_counter(counts.succeeded, &slot.family),
Role::Failed => render_counter(counts.failed, &slot.family),
Role::Pending => render_counter(counts.pending, &slot.family),
Role::Status => render_status(&counts.status, &slot.family),
Role::CompletedAt => match &counts.completed_at {
Some(text) => render_datetime(text, &slot.family),
None => GeneratedValue::Null,
},
};
}
Ok(())
}
fn verification_predicates(&self) -> Vec<PlannerPredicate> {
self.predicates.clone()
}
}
impl ProgressCountersPlanner {
fn draw_row(&mut self) -> RowCounts {
let total = self.total_draw.draw(&mut self.total_rng).max(0);
let state_pick = self.state_rng.random::<f64>();
let progress_fraction = self.progress_rng.random::<f64>();
let status_pick = self.status_rng.random::<f64>();
let completion_offset = self.completion_rng.random::<f64>();
let state = self.state_mix.select(state_pick);
let processed = processed_for_state(state, total, progress_fraction);
let pending = total - processed;
let (succeeded, failed) = self.split_processed(processed);
let status = match state {
LifecycleState::Complete => pick(&self.completed_statuses, status_pick),
LifecycleState::Active | LifecycleState::NotStarted => {
pick(&self.active_statuses, status_pick)
}
};
let completed_at = match state {
LifecycleState::Complete => Some(completion_instant(completion_offset)),
_ => None,
};
RowCounts {
total,
processed,
succeeded,
failed,
pending,
status,
completed_at,
}
}
fn split_processed(&self, processed: i128) -> (i128, i128) {
let succeed = self.success_ratio;
let fail = 1.0 - self.success_ratio;
let parts = match self.partition {
Partition::Exact => largest_remainder(processed, &[succeed, fail]),
Partition::AllowUnclassified { unclassified_ratio } => {
let classified = 1.0 - unclassified_ratio;
largest_remainder(
processed,
&[succeed * classified, fail * classified, unclassified_ratio],
)
}
};
(parts[0], parts[1])
}
}
fn processed_for_state(state: LifecycleState, total: i128, progress_fraction: f64) -> i128 {
match state {
LifecycleState::Complete => total,
LifecycleState::NotStarted => 0,
LifecycleState::Active => {
if total <= 0 {
0
} else {
let raw = (progress_fraction * total as f64).floor() as i128;
raw.clamp(0, total - 1)
}
}
}
}
fn largest_remainder(amount: i128, weights: &[f64]) -> Vec<i128> {
let count = weights.len();
let weight_total: f64 = weights.iter().sum();
if amount <= 0 || weight_total <= 0.0 || !weight_total.is_finite() {
return vec![0; count];
}
let mut parts = Vec::with_capacity(count);
let mut remainders = Vec::with_capacity(count);
let mut allocated: i128 = 0;
for &weight in weights {
let exact = amount as f64 * (weight.max(0.0) / weight_total);
let floor = exact.floor();
let whole = floor as i128;
parts.push(whole);
remainders.push(exact - floor);
allocated += whole;
}
let mut leftover = amount - allocated;
let mut order: Vec<usize> = (0..count).collect();
order.sort_by(|&a, &b| {
remainders[b]
.partial_cmp(&remainders[a])
.unwrap_or(std::cmp::Ordering::Equal)
.then(a.cmp(&b))
});
let mut cursor = 0;
while leftover > 0 && !order.is_empty() {
parts[order[cursor % count]] += 1;
leftover -= 1;
cursor += 1;
}
parts
}
fn pick(options: &[String], r: f64) -> String {
if options.is_empty() {
return String::new();
}
let index = ((r * options.len() as f64).floor() as usize).min(options.len() - 1);
options[index].clone()
}
fn completion_instant(offset: f64) -> String {
let extra = (offset * COMPLETION_SPAN_SECS as f64).floor() as i64;
let secs = COMPLETION_BASE_SECS.saturating_add(extra);
DateTime::<Utc>::from_timestamp(secs, 0)
.map(|dt| dt.format("%Y-%m-%d %H:%M:%S").to_string())
.unwrap_or_default()
}
fn render_counter(value: i128, family: &SqlTypeFamily) -> GeneratedValue {
match family {
SqlTypeFamily::Integer | SqlTypeFamily::BigInteger => GeneratedValue::Integer(value),
SqlTypeFamily::Decimal => GeneratedValue::Decimal {
minor: value,
scale: 0,
},
_ => GeneratedValue::Text(value.to_string()),
}
}
fn render_status(status: &str, family: &SqlTypeFamily) -> GeneratedValue {
match family {
SqlTypeFamily::Text | SqlTypeFamily::Uuid | SqlTypeFamily::Other => {
GeneratedValue::Text(status.to_string())
}
_ => GeneratedValue::Text(status.to_string()),
}
}
fn render_datetime(text: &str, family: &SqlTypeFamily) -> GeneratedValue {
match family {
SqlTypeFamily::DateTime => GeneratedValue::DateTime(text.to_string()),
_ => GeneratedValue::Text(text.to_string()),
}
}
fn compile_progress(
config: &PlannerConfig,
context: &CompileContext<'_>,
) -> Result<ProgressCountersPlanner, DiagnosticBag> {
let mut bag = DiagnosticBag::default();
let table = context.table();
let path = context.path();
let columns = config.args.get("columns");
let slots = resolve_slots(columns, table, path, &mut bag);
let total_draw = compile_total(config.args.get("total"), path, &mut bag);
let partition = compile_partition(config, path, &mut bag);
let state_mix = compile_state(config.args.get("progress"), partition, path, &mut bag);
let success_ratio = config
.args
.get("success_ratio")
.and_then(as_f64)
.unwrap_or(0.9)
.clamp(0.0, 1.0);
let completed_statuses = string_list(config.args.get("completed_statuses"));
let active_statuses = string_list(config.args.get("active_statuses"));
let has_role = |role: Role| slots.iter().any(|slot| slot.role == role);
let (complete_reachable, active_reachable, not_started_reachable) = state_mix
.as_ref()
.map_or((false, false, false), StateMix::reachable);
if let Some(draw) = &total_draw {
let maximum = draw.maximum();
for slot in &slots {
if is_counter(slot.role) {
let capacity = family_capacity(&slot.family);
if maximum > capacity {
bag.error(
crate::diagnostic::codes::PROGRESS_OVERFLOW.code,
format!("{path}.total"),
format!(
"workflow.progress_counters `total` can reach {maximum}, exceeding the capacity {capacity} of counter column `{}`",
slot.name
),
);
}
}
}
}
if has_role(Role::Status) {
if complete_reachable && completed_statuses.is_empty() {
bag.error(
crate::diagnostic::codes::PROGRESS_STATUS_VOCABULARY.code,
format!("{path}.completed_statuses"),
"workflow.progress_counters produces completed rows but `completed_statuses` is empty; supply at least one completed status".to_string(),
);
}
if (active_reachable || not_started_reachable) && active_statuses.is_empty() {
bag.error(
crate::diagnostic::codes::PROGRESS_STATUS_VOCABULARY.code,
format!("{path}.active_statuses"),
"workflow.progress_counters produces active or not-started rows but `active_statuses` is empty; supply at least one active status".to_string(),
);
}
if let Some(shared) = completed_statuses
.iter()
.find(|status| active_statuses.contains(status))
{
bag.error(
crate::diagnostic::codes::PROGRESS_STATUS_VOCABULARY.code,
format!("{path}.active_statuses"),
format!(
"workflow.progress_counters status `{shared}` appears in both `completed_statuses` and `active_statuses`; a status must belong to only one lifecycle group"
),
);
}
}
if let Some(slot) = slots.iter().find(|slot| slot.role == Role::CompletedAt) {
if (active_reachable || not_started_reachable) && !column_nullable(table, &slot.name) {
bag.error(
crate::diagnostic::codes::PROGRESS_COMPLETION.code,
format!("{path}.columns.completed_at"),
format!(
"workflow.progress_counters `completed_at` column `{}` is not nullable, but active and not-started rows leave completion null",
slot.name
),
);
}
}
if bag.has_errors() {
return Err(bag);
}
let total_draw = total_draw.expect("total resolved without errors");
let state_mix = state_mix.expect("state resolved without errors");
let writes = slots.iter().map(|slot| slot.name.clone()).collect();
let (total_rng, state_rng, progress_rng, status_rng, completion_rng) = build_streams(context);
let predicates = build_predicates(&slots, partition, &completed_statuses, &active_statuses);
Ok(ProgressCountersPlanner {
writes,
slots,
total_draw,
state_mix,
partition,
success_ratio,
completed_statuses,
active_statuses,
total_rng,
state_rng,
progress_rng,
status_rng,
completion_rng,
predicates,
})
}
fn resolve_slots(
columns: Option<&Value>,
table: &PortableTable,
path: &str,
bag: &mut DiagnosticBag,
) -> Vec<Slot> {
let mut slots = Vec::new();
for (role_key, role) in ROLE_ORDER {
let Some(name) = role_name(columns, role_key) else {
if role == Role::Total {
bag.error(
crate::diagnostic::codes::PROGRESS_COLUMN_MISSING.code,
format!("{path}.columns.total"),
"workflow.progress_counters requires a `total` column under `columns`"
.to_string(),
);
}
continue;
};
match find_column(table, name) {
Some(column) => slots.push(Slot {
role,
name: column.name.clone(),
family: column.family.clone(),
}),
None => {
bag.error(
crate::diagnostic::codes::PROGRESS_COLUMN_MISSING.code,
format!("{path}.columns.{role_key}"),
format!(
"workflow.progress_counters `{role_key}` column `{name}` does not exist on table `{}`",
table.name
),
);
}
}
}
slots
}
fn compile_total(total: Option<&Value>, path: &str, bag: &mut DiagnosticBag) -> Option<TotalDraw> {
let kind = total.and_then(|t| t.get("kind")).and_then(Value::as_str);
let field = |key: &str| total.and_then(|t| t.get(key)).and_then(as_i128);
let draw = match kind {
Some("fixed") => {
let value = field("value").unwrap_or(0);
if value < 0 {
bag.error(
crate::diagnostic::codes::PROGRESS_TOTAL.code,
format!("{path}.total.value"),
format!("workflow.progress_counters `total.value` `{value}` is negative"),
);
return None;
}
TotalDraw::Fixed(value)
}
_ => {
let min = field("min").unwrap_or(0);
let max = field("max").unwrap_or(min);
if min < 0 {
bag.error(
crate::diagnostic::codes::PROGRESS_TOTAL.code,
format!("{path}.total.min"),
format!("workflow.progress_counters `total.min` `{min}` is negative"),
);
return None;
}
if max < min {
bag.error(
crate::diagnostic::codes::PROGRESS_TOTAL.code,
format!("{path}.total"),
"workflow.progress_counters `total.max` is below `total.min`".to_string(),
);
return None;
}
TotalDraw::Uniform { min, max }
}
};
Some(draw)
}
fn compile_partition(config: &PlannerConfig, path: &str, bag: &mut DiagnosticBag) -> Partition {
match config.args.get("partition").and_then(Value::as_str) {
Some("allow_unclassified") => {
let unclassified_ratio = config
.args
.get("unclassified_ratio")
.and_then(as_f64)
.unwrap_or(0.1)
.clamp(0.0, 1.0);
Partition::AllowUnclassified { unclassified_ratio }
}
Some("exact") | None => Partition::Exact,
Some(other) => {
bag.error(
crate::diagnostic::codes::PROGRESS_PARTITION.code,
format!("{path}.partition"),
format!(
"workflow.progress_counters `partition` `{other}` is not one of `exact` or `allow_unclassified`"
),
);
Partition::Exact
}
}
}
fn compile_state(
progress: Option<&Value>,
partition: Partition,
path: &str,
bag: &mut DiagnosticBag,
) -> Option<StateMix> {
let kind = progress
.and_then(|p| p.get("kind"))
.and_then(Value::as_str)
.unwrap_or("mixture");
match kind {
"complete" => Some(StateMix::Fixed(LifecycleState::Complete)),
"in_progress" => Some(StateMix::Fixed(LifecycleState::Active)),
"not_started" => Some(StateMix::Fixed(LifecycleState::NotStarted)),
"observed" => {
if let Partition::Exact = partition {
bag.error(
crate::diagnostic::codes::PROGRESS_OBSERVED.code,
format!("{path}.progress"),
"workflow.progress_counters `progress.kind: observed` needs observed evidence to form an exact partition, but none is available; supply explicit mixture weights or use `partition: allow_unclassified`".to_string(),
);
None
} else {
Some(default_mixture())
}
}
_ => compile_mixture(progress, path, bag),
}
}
fn default_mixture() -> StateMix {
StateMix::Weighted {
complete: 0.7,
active: 0.25,
not_started: 0.05,
}
}
fn compile_mixture(
progress: Option<&Value>,
path: &str,
bag: &mut DiagnosticBag,
) -> Option<StateMix> {
let weight = |key: &str| progress.and_then(|p| p.get(key)).and_then(as_f64);
let complete = weight("complete_weight");
let active = weight("active_weight");
let not_started = weight("not_started_weight");
let (complete, active, not_started) =
if complete.is_none() && active.is_none() && not_started.is_none() {
(1.0, 1.0, 1.0)
} else {
(
complete.unwrap_or(0.0),
active.unwrap_or(0.0),
not_started.unwrap_or(0.0),
)
};
let weights = [complete, active, not_started];
if weights.iter().any(|w| !w.is_finite() || *w < 0.0) {
bag.error(
crate::diagnostic::codes::PROGRESS_WEIGHTS.code,
format!("{path}.progress"),
"workflow.progress_counters mixture weights must be finite and non-negative"
.to_string(),
);
return None;
}
let total: f64 = weights.iter().sum();
if total <= 0.0 {
bag.error(
crate::diagnostic::codes::PROGRESS_WEIGHTS.code,
format!("{path}.progress"),
"workflow.progress_counters mixture weights sum to zero; at least one weight must be positive".to_string(),
);
return None;
}
Some(StateMix::Weighted {
complete,
active,
not_started,
})
}
fn build_streams(
context: &CompileContext<'_>,
) -> (ChaCha8Rng, ChaCha8Rng, ChaCha8Rng, ChaCha8Rng, ChaCha8Rng) {
let table = context.table().name.as_str();
let stream = |suffix: &str| {
context.rng(StreamId::operator(
table,
"workflow.progress_counters",
format!("workflow.progress_counters.{suffix}"),
))
};
(
stream("total"),
stream("state"),
stream("progress"),
stream("status"),
stream("completion"),
)
}
fn build_predicates(
slots: &[Slot],
partition: Partition,
completed_statuses: &[String],
active_statuses: &[String],
) -> Vec<PlannerPredicate> {
let name_of = |role: Role| {
slots
.iter()
.find(|slot| slot.role == role)
.map(|slot| slot.name.clone())
};
let mut predicates = Vec::new();
let total = name_of(Role::Total);
let processed = name_of(Role::Processed);
let succeeded = name_of(Role::Succeeded);
let failed = name_of(Role::Failed);
let pending = name_of(Role::Pending);
let status = name_of(Role::Status);
let completed_at = name_of(Role::CompletedAt);
if let (Partition::Exact, Some(processed), Some(succeeded), Some(failed)) =
(partition, &processed, &succeeded, &failed)
{
predicates.push(PlannerPredicate::CounterSum {
addends: vec![succeeded.clone(), failed.clone()],
sum: processed.clone(),
guard: None,
});
}
if let (Some(total), Some(processed), Some(pending)) = (&total, &processed, &pending) {
predicates.push(PlannerPredicate::CounterSum {
addends: vec![processed.clone(), pending.clone()],
sum: total.clone(),
guard: None,
});
}
let counters: Vec<String> = slots
.iter()
.filter(|slot| is_counter(slot.role))
.map(|slot| slot.name.clone())
.collect();
if !counters.is_empty() {
predicates.push(PlannerPredicate::NonNegative { columns: counters });
}
if let Some(status) = &status {
if let (Some(total), Some(processed)) = (&total, &processed) {
for value in completed_statuses {
predicates.push(PlannerPredicate::CounterSum {
addends: vec![processed.clone()],
sum: total.clone(),
guard: Some(PredicateGuard::Equals {
column: status.clone(),
value: value.clone(),
}),
});
}
}
if let Some(completed_at) = &completed_at {
for value in completed_statuses {
predicates.push(PlannerPredicate::NotNullWhen {
column: completed_at.clone(),
guard: PredicateGuard::Equals {
column: status.clone(),
value: value.clone(),
},
});
}
for value in active_statuses {
predicates.push(PlannerPredicate::NullWhen {
column: completed_at.clone(),
guard: PredicateGuard::Equals {
column: status.clone(),
value: value.clone(),
},
});
}
}
}
predicates
}
fn is_counter(role: Role) -> bool {
matches!(
role,
Role::Total | Role::Processed | Role::Succeeded | Role::Failed | Role::Pending
)
}
fn family_capacity(family: &SqlTypeFamily) -> i128 {
match family {
SqlTypeFamily::Integer => i32::MAX as i128,
SqlTypeFamily::BigInteger => i64::MAX as i128,
_ => i128::MAX,
}
}
fn role_name<'a>(columns: Option<&'a Value>, role: &str) -> Option<&'a str> {
columns?.get(role).and_then(Value::as_str)
}
fn find_column<'a>(table: &'a PortableTable, name: &str) -> Option<&'a PortableColumn> {
table.columns.iter().find(|column| column.name == name)
}
fn column_nullable(table: &PortableTable, name: &str) -> bool {
find_column(table, name).is_some_and(|column| column.nullable)
}
fn string_list(value: Option<&Value>) -> Vec<String> {
match value {
Some(Value::Sequence(items)) => items
.iter()
.filter_map(Value::as_str)
.map(str::to_string)
.collect(),
_ => Vec::new(),
}
}
fn as_i128(value: &Value) -> Option<i128> {
match value {
Value::Number(number) => number
.as_i64()
.map(i128::from)
.or_else(|| number.as_u64().map(i128::from))
.or_else(|| number.as_f64().map(|float| float as i128)),
Value::String(text) => text.trim().parse::<i128>().ok(),
_ => None,
}
}
fn as_f64(value: &Value) -> Option<f64> {
match value {
Value::Number(number) => number.as_f64(),
Value::String(text) => text.trim().parse::<f64>().ok(),
_ => None,
}
}