use super::super::BULK_WINDOW_SECONDS;
use super::super::history::{HistoryIndex, backfilled};
use crate::{Command, Operation};
use dpm_model::WorkItemId;
pub(super) fn bulk_recorded(index: &HistoryIndex<'_>, work: WorkItemId) -> bool {
let operations = index.of(work);
let Some(position) = operations
.iter()
.rposition(|op| matches!(op.command, Command::Start { .. }))
else {
return false;
};
let claim = operations.get(..position).and_then(|before| {
before
.iter()
.rev()
.find(|op| matches!(op.command, Command::Claim { .. } | Command::Handoff { .. }))
});
let mut after = operations.iter().skip(position);
let (Some(start), Some(submit)) = (
after.next(),
after.find(|op| matches!(op.command, Command::Submit { .. })),
) else {
return false;
};
let together = |a: &Operation, b: &Operation| {
(b.timestamp - a.timestamp).num_seconds().abs() <= BULK_WINDOW_SECONDS
};
backfilled(&start.command).is_none()
&& backfilled(&submit.command).is_none()
&& together(start, submit)
&& claim.is_none_or(|claim| together(claim, start) && together(claim, submit))
}