use std::collections::VecDeque;
use std::time::Duration;
use futures::stream::unfold;
use futures::Stream;
use taskfast_client::api::types::{AgentEvent, AgentEventListResponse, PaginatedMeta};
use taskfast_client::{map_api_error, Error, Result, TaskFastClient};
use tokio::time::sleep;
#[derive(Debug, Clone)]
pub struct UnparseableEvent {
pub raw: serde_json::Value,
pub error: String,
}
#[derive(Debug, Clone)]
pub struct TolerantEventPage {
pub events: Vec<AgentEvent>,
pub unparseable: Vec<UnparseableEvent>,
pub meta: PaginatedMeta,
}
pub const DEFAULT_POLL_INTERVAL: Duration = Duration::from_secs(5);
pub const DEFAULT_PAGE_LIMIT: i64 = 100;
#[derive(Debug, Clone, Copy)]
pub struct PollOptions {
pub poll_interval: Duration,
pub page_limit: i64,
}
impl Default for PollOptions {
fn default() -> Self {
Self {
poll_interval: DEFAULT_POLL_INTERVAL,
page_limit: DEFAULT_PAGE_LIMIT,
}
}
}
pub async fn list_events_page(
client: &TaskFastClient,
cursor: Option<&str>,
limit: Option<i64>,
) -> Result<AgentEventListResponse> {
let limit = limit.and_then(taskfast_client::page_limit);
match client.inner().list_agent_events(cursor, limit).await {
Ok(v) => Ok(v.into_inner()),
Err(e) => Err(map_api_error(e).await),
}
}
pub async fn list_events_page_tolerant(
client: &TaskFastClient,
cursor: Option<&str>,
limit: Option<i64>,
) -> Result<TolerantEventPage> {
match list_events_page(client, cursor, limit).await {
Ok(page) => Ok(TolerantEventPage {
events: page.data,
unparseable: Vec::new(),
meta: page.meta,
}),
Err(Error::Decode(_)) => decode_raw_page(client, cursor, limit).await,
Err(e) => Err(e),
}
}
async fn decode_raw_page(
client: &TaskFastClient,
cursor: Option<&str>,
limit: Option<i64>,
) -> Result<TolerantEventPage> {
let raw = client.list_agent_events_raw(cursor, limit).await?;
let meta_v = raw.get("meta").cloned().unwrap_or(serde_json::Value::Null);
let meta: PaginatedMeta = serde_json::from_value(meta_v)?;
let data = raw
.get("data")
.and_then(|v| v.as_array())
.cloned()
.unwrap_or_default();
let mut events = Vec::with_capacity(data.len());
let mut unparseable = Vec::new();
for item in data {
match serde_json::from_value::<AgentEvent>(item.clone()) {
Ok(ev) => events.push(ev),
Err(e) => unparseable.push(UnparseableEvent {
raw: item,
error: e.to_string(),
}),
}
}
Ok(TolerantEventPage {
events,
unparseable,
meta,
})
}
struct StreamState {
cursor: Option<String>,
buffer: VecDeque<AgentEvent>,
sleep_before_next_fetch: bool,
}
pub fn stream_events(
client: &TaskFastClient,
start_cursor: Option<String>,
opts: PollOptions,
) -> impl Stream<Item = Result<AgentEvent>> + '_ {
let state = StreamState {
cursor: start_cursor,
buffer: VecDeque::new(),
sleep_before_next_fetch: false,
};
unfold(state, move |mut state| async move {
loop {
if let Some(ev) = state.buffer.pop_front() {
return Some((Ok(ev), state));
}
if state.sleep_before_next_fetch {
sleep(opts.poll_interval).await;
}
match list_events_page(client, state.cursor.as_deref(), Some(opts.page_limit)).await {
Ok(page) => {
state.buffer.extend(page.data);
state.cursor = page.meta.next_cursor.or(state.cursor);
state.sleep_before_next_fetch = !page.meta.has_more;
}
Err(e) => {
state.sleep_before_next_fetch = true;
return Some((Err(e), state));
}
}
}
})
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn default_poll_options_are_sane() {
let opts = PollOptions::default();
assert_eq!(opts.poll_interval, Duration::from_secs(5));
assert_eq!(opts.page_limit, 100);
}
}