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_idis set: Results are ordered byid(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: boolWhen true, only return API request events (events with token data)
exclude_api_requests: boolWhen true, exclude API request events
Implementations§
Source§impl EventQuery
impl EventQuery
Sourcepub fn with_after_ts(self, ts: DateTime<Utc>) -> Self
pub fn with_after_ts(self, ts: DateTime<Utc>) -> Self
Filter events after the given timestamp (exclusive)
Sourcepub fn with_before_ts(self, ts: DateTime<Utc>) -> Self
pub fn with_before_ts(self, ts: DateTime<Utc>) -> Self
Filter events before the given timestamp (exclusive)
Sourcepub fn with_between_ts(self, start: DateTime<Utc>, end: DateTime<Utc>) -> Self
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.
Sourcepub fn with_after_id(self, id: i64) -> Self
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.
Sourcepub fn with_limit(self, limit: usize) -> Self
pub fn with_limit(self, limit: usize) -> Self
Set the maximum number of events to return
Sourcepub fn with_direction(self, direction: Order) -> Self
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).
Sourcepub fn with_session(self, session_id: Option<String>) -> Self
pub fn with_session(self, session_id: Option<String>) -> Self
Set session filter if value is Some
Sourcepub fn with_event_type(self, event_type: Option<String>) -> Self
pub fn with_event_type(self, event_type: Option<String>) -> Self
Set event type filter if value is Some
Sourcepub fn with_permission_mode(self, permission_mode: Option<String>) -> Self
pub fn with_permission_mode(self, permission_mode: Option<String>) -> Self
Set permission mode filter if value is Some
Sourcepub fn with_framework(self, framework: Option<String>) -> Self
pub fn with_framework(self, framework: Option<String>) -> Self
Set framework filter if value is Some
Sourcepub fn api_requests_only(self) -> Self
pub fn api_requests_only(self) -> Self
Only return API request events (events with token data)
Sourcepub fn exclude_api_requests(self) -> Self
pub fn exclude_api_requests(self) -> Self
Exclude API request events from results
Sourcepub fn effective_direction(&self) -> Order
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
directionis explicitly set, returns that value. - If
after_idis set, defaults toOrder::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);Sourcepub fn orders_by_id(&self) -> bool
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());