Skip to main content

EventQuery

Struct EventQuery 

Source
pub struct EventQuery {
    pub session_id: Option<String>,
    pub event_type: Option<String>,
    pub permission_mode: Option<String>,
    pub framework: Option<String>,
    pub after_ts: Option<DateTime<Utc>>,
    pub before_ts: Option<DateTime<Utc>>,
    pub after_id: Option<i64>,
    pub limit: Option<usize>,
    pub direction: Option<Order>,
    pub api_requests_only: bool,
    pub exclude_api_requests: bool,
}
Expand description

Composable query builder for event retrieval.

EventQuery provides a fluent API for constructing event queries with filters, ordering, and pagination.

§Ordering Behavior

The query uses implicit ordering based on the query type:

  • When after_id is set: Results are ordered by id (ascending by default) to ensure consistent pagination when watching for new events.
  • Otherwise: Results are ordered by timestamp (descending by default) to show most recent events first.

The direction() method controls the sort direction (Asc/Desc) but not the column being sorted. This design ensures correct behavior for the two main use cases:

  • Recent events: Order by timestamp DESC to see newest first
  • Watching new events: Order by ID ASC to get events in insertion order

§Limitations

The ordering column (timestamp vs ID) is determined by query parameters. You cannot order by timestamp when using after_id, or order by ID when not using after_id. This is by design to optimize for the two main use cases, but reduces composability.

§Example

ⓘ
// Get 50 most recent events for a session
let query = EventQuery::new()
    .with_session(Some("session-123".to_string()))
    .with_limit(50);

// Watch for new events after ID 100
let query = EventQuery::new()
    .with_after_id(100)
    .with_limit(10);

Fields§

§session_id: Option<String>

Filter by session ID

§event_type: Option<String>

Filter by event type (case-insensitive)

§permission_mode: Option<String>

Filter by permission mode

§framework: Option<String>

Filter by framework (claude, cursor, gemini)

§after_ts: Option<DateTime<Utc>>

Filter events after this timestamp (exclusive)

§before_ts: Option<DateTime<Utc>>

Filter events before this timestamp (exclusive)

§after_id: Option<i64>

Filter events after this ID (exclusive) - triggers ID-based ordering

§limit: Option<usize>

Maximum number of events to return

§direction: Option<Order>

Sort direction (default: Desc for timestamp queries, Asc for after_id queries)

§api_requests_only: bool

When true, only return API request events (events with token data)

§exclude_api_requests: bool

When true, exclude API request events

Implementations§

Source§

impl EventQuery

Source

pub fn new() -> Self

Create a new empty query

Source

pub fn with_after_ts(self, ts: DateTime<Utc>) -> Self

Filter events after the given timestamp (exclusive)

Source

pub fn with_before_ts(self, ts: DateTime<Utc>) -> Self

Filter events before the given timestamp (exclusive)

Source

pub fn with_between_ts(self, start: DateTime<Utc>, end: DateTime<Utc>) -> Self

Filter events between two timestamps (exclusive on both ends).

Note: If start >= end, the filter will return no results. The caller is responsible for ensuring start < end for meaningful queries.

Source

pub fn with_after_id(self, id: i64) -> Self

Filter events after the given ID (exclusive).

Important: Setting after_id changes the ordering behavior. Results will be ordered by ID instead of timestamp to ensure consistent pagination when watching for new events.

Source

pub fn with_limit(self, limit: usize) -> Self

Set the maximum number of events to return

Source

pub fn with_direction(self, direction: Order) -> Self

Set the sort direction (Asc/Desc).

Note: This controls the direction only, not the column being sorted. The column is determined by the query type (ID when after_id is set, timestamp otherwise).

Source

pub fn with_session(self, session_id: Option<String>) -> Self

Set session filter if value is Some

Source

pub fn with_event_type(self, event_type: Option<String>) -> Self

Set event type filter if value is Some

Source

pub fn with_permission_mode(self, permission_mode: Option<String>) -> Self

Set permission mode filter if value is Some

Source

pub fn with_framework(self, framework: Option<String>) -> Self

Set framework filter if value is Some

Source

pub fn api_requests_only(self) -> Self

Only return API request events (events with token data)

Source

pub fn exclude_api_requests(self) -> Self

Exclude API request events from results

Source

pub fn effective_direction(&self) -> Order

Returns the effective sort direction for this query.

Storage implementations should use this method to determine the sort direction when executing queries.

§Returns
  • If direction is explicitly set, returns that value.
  • If after_id is set, defaults to Order::Asc (for watching new events).
  • Otherwise, defaults to Order::Desc (most recent first).
§Example
use fyi_core::{EventQuery, Order};

// Default: Desc for recent events
let query = EventQuery::new();
assert_eq!(query.effective_direction(), Order::Desc);

// With after_id: Asc for watching
let query = EventQuery::new().with_after_id(100);
assert_eq!(query.effective_direction(), Order::Asc);

// Explicit direction overrides default
let query = EventQuery::new().with_direction(Order::Asc);
assert_eq!(query.effective_direction(), Order::Asc);
Source

pub fn orders_by_id(&self) -> bool

Returns true if this query should order by ID instead of timestamp.

Storage implementations should use this method to determine which column to use for ordering results.

When after_id is set, the query is typically used for “watching” new events, so ordering by ID ensures consistent pagination.

§Example
use fyi_core::EventQuery;

// Without after_id: order by timestamp
let query = EventQuery::new();
assert!(!query.orders_by_id());

// With after_id: order by ID
let query = EventQuery::new().with_after_id(100);
assert!(query.orders_by_id());

Trait Implementations§

Source§

impl Clone for EventQuery

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Default for EventQuery

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.