use super::{
AgentRunSnapshot, AgentServiceCaller, AgentServiceError, AgentServiceRunOperation,
AgentServiceRunTarget, AgentServiceV1,
};
use stateknot_core::{JournalHead, RunId};
use stateknot_store_postgres::JournalPageSize;
#[derive(Clone, Debug)]
pub struct AgentRunActivityPage {
snapshot: AgentRunSnapshot,
head: Option<JournalHead>,
has_more: bool,
}
impl AgentRunActivityPage {
#[must_use]
pub const fn snapshot(&self) -> &AgentRunSnapshot {
&self.snapshot
}
#[must_use]
pub const fn head(&self) -> Option<&JournalHead> {
self.head.as_ref()
}
#[must_use]
pub const fn has_more(&self) -> bool {
self.has_more
}
}
impl AgentServiceV1 {
pub async fn load_activity(
&self,
caller: AgentServiceCaller,
run_id: RunId,
after: Option<&JournalHead>,
) -> Result<AgentRunActivityPage, AgentServiceError> {
self.authorize_run(
&caller,
AgentServiceRunTarget::Run(run_id),
AgentServiceRunOperation::Read,
)
.await?;
let snapshot = self.runs.load(caller.tenant_id(), run_id).await?;
let page = self
.store
.load_journal_page(
caller.tenant_id(),
run_id,
after,
JournalPageSize::new(1).expect("static bounded journal page"),
)
.await?;
Ok(AgentRunActivityPage {
snapshot,
head: page.next_cursor(),
has_more: page.has_more(),
})
}
}