use std::path::{Path, PathBuf};
use chrono::{DateTime, Local};
use super::Command;
pub(super) struct Activity {
log: PathBuf,
session: Option<PathBuf>,
now: DateTime<Local>,
}
impl Activity {
pub(super) fn new(log: &Path, session: Option<&Path>, now: DateTime<Local>) -> Self {
Self {
log: log.to_owned(),
session: session.map(crate::logs::session::path),
now,
}
}
pub(super) fn drop_session_unless(&mut self, keep: impl Fn(&Path) -> bool) {
if self.session.as_deref().is_some_and(|path| !keep(path)) {
self.session = None;
}
}
}
impl Command {
pub(in crate::logs) fn activity_paths(&self) -> impl Iterator<Item = &Path> {
std::iter::once(self.activity.log.as_path()).chain(self.activity.session.as_deref())
}
pub(in crate::logs) fn quiet_age(&self) -> u64 {
let Some(stage) = self.record.current_stage() else {
return 0;
};
let Ok(began) = DateTime::parse_from_rfc3339(&stage.began_at) else {
return 0;
};
let since = self
.activity_paths()
.filter_map(|path| self.inventory.activity(path).modified)
.map(|modified| DateTime::<Local>::from(modified).fixed_offset())
.fold(began, std::cmp::max);
let now = self.activity.now.fixed_offset();
let quiet = (now - since).num_seconds().max(0) as u64;
let suspended = stage.suspended_between(since, now);
quiet.saturating_sub(suspended)
}
pub(in crate::logs) fn activity_lengths(&self) -> (u64, u64) {
(
self.inventory.activity(&self.activity.log).length,
self.activity
.session
.as_deref()
.map_or(0, |path| self.inventory.activity(path).length),
)
}
}