use super::{
durable_event_sequence, parse, DateTime, GroupFailure, HashMap, HistoryEvent, Utc, Value,
};
use crate::{recorded_parallel_group_path, ParallelGroupMetadata};
use std::collections::HashSet;
pub(super) fn all_path(path: &[ParallelGroupMetadata], kind: &str) -> bool {
!path.is_empty()
&& path.iter().all(|entry| {
(entry.parallel_group_kind == kind || entry.parallel_group_kind == "mixed")
&& entry
.parallel_group_mode
.as_deref()
.is_none_or(|mode| mode == "all")
&& entry.selection_member_key.is_none()
&& entry.selection_member_index.is_none()
&& entry.selection_member_base_sequence.is_none()
&& entry.selection_member_size.is_none()
&& entry.selection_member_kind.is_none()
})
}
pub(super) fn round_microseconds(micros: i64) -> Option<i64> {
let magnitude = micros.unsigned_abs().checked_add(500)? / 1000;
let milliseconds = i64::try_from(magnitude).ok()?;
if micros < 0 {
milliseconds.checked_neg()
} else {
Some(milliseconds)
}
}
pub(super) fn milliseconds(time: DateTime<Utc>) -> Option<i64> {
if time.timestamp_subsec_nanos() % 1000 != 0 {
return None;
}
let micros = time
.timestamp()
.checked_mul(1_000_000)?
.checked_add(i64::from(time.timestamp_subsec_micros()))?;
round_microseconds(micros)
}
pub(super) fn event_time(event: &HistoryEvent) -> Option<DateTime<Utc>> {
match event.raw.get("recorded_at") {
None | Some(Value::Null) => parse(event.raw.get("created_at")),
present => parse(present),
}
}
pub(super) fn event_indices(history: &[HistoryEvent], prefix: &str) -> HashMap<u64, Vec<usize>> {
let mut events: HashMap<u64, Vec<usize>> = HashMap::new();
for (index, event) in history.iter().enumerate() {
if event.event_type.starts_with(prefix) {
if let Some(sequence) = durable_event_sequence(event) {
events.entry(sequence).or_default().push(index);
}
}
}
events
}
pub(super) fn member_path(
history: &[HistoryEvent],
indices: &[usize],
sequence: u64,
path: &[ParallelGroupMetadata],
) -> bool {
indices.iter().all(|&index| {
let event = &history[index];
event.payload.get("sequence").and_then(Value::as_u64) == Some(sequence)
&& event
.payload
.get("workflow_sequence")
.is_none_or(|value| value.as_u64() == Some(sequence))
&& recorded_parallel_group_path(&[event], sequence)
.ok()
.flatten()
.as_deref()
== Some(path)
})
}
#[derive(Debug)]
pub(super) struct Member {
pub(super) path: Vec<ParallelGroupMetadata>,
pub(super) identity: String,
pub(super) terminal: Option<usize>,
pub(super) failed: bool,
}
#[derive(Debug)]
pub(super) struct Members(pub(super) HashMap<u64, Member>);
impl Members {
pub(super) fn consumed_time(
families: &[(&str, &Members)],
history: &[HistoryEvent],
sequences: &[u64],
failed: Option<GroupFailure>,
) -> Option<DateTime<Utc>> {
let member_at = |sequence| {
let mut matches = families
.iter()
.filter_map(|&(kind, members)| Some((kind, members.0.get(sequence)?)));
let member = matches.next()?;
matches.next().is_none().then_some(member)
};
let first_sequence = sequences.first()?;
let (first_kind, first_member) = member_at(first_sequence)?;
let root = first_member.path.first()?;
let declared_mixed = root.parallel_group_kind == "mixed";
if root.parallel_group_base_sequence != *first_sequence
|| root.parallel_group_size != sequences.len()
|| (!declared_mixed && root.parallel_group_kind != first_kind)
{
return None;
}
let mut mixed = false;
let mut identities = HashSet::new();
let mut success: Option<(i64, DateTime<Utc>)> = None;
let mut failure: Option<(i64, u64)> = None;
let recorded_failure = match failed {
Some(GroupFailure::FirstRecorded(sequence)) => Some(sequence),
_ => None,
};
for (index, sequence) in sequences.iter().enumerate() {
let (kind, member) = member_at(sequence)?;
mixed |= kind != first_kind;
let mut expected = root.clone();
expected.parallel_group_index = index;
if member.path.first()? != &expected
|| root
.parallel_group_base_sequence
.checked_add(index as u64)?
!= *sequence
|| !identities.insert((kind, &member.identity))
{
return None;
}
if member.failed && recorded_failure.is_none_or(|selected| selected == *sequence) {
let ms = milliseconds(event_time(history.get(member.terminal?)?)?)?;
if failure.is_none_or(|(current, _)| ms < current) {
failure = Some((ms, *sequence));
}
} else if failed.is_none() {
let time = event_time(history.get(member.terminal?)?)?;
let ms = milliseconds(time)?;
if success.is_none_or(|(current, _)| ms > current) {
success = Some((ms, time));
}
}
}
if mixed != declared_mixed {
return None;
}
match failed {
Some(selected) => {
let (ms, canonical_selected) = failure?;
(selected.sequence() == canonical_selected)
.then(|| DateTime::from_timestamp_millis(ms))
.flatten()
}
None if failure.is_none() => success.map(|(_, time)| time),
None => None,
}
}
}