Skip to content

brokkr-broker::dal::agent_events

Rust

Data Access Layer for AgentEvent operations.

This module provides functionality to interact with the agent_events table in the database. It includes methods for creating, retrieving, updating, and deleting agent events, as well as listing events with various filtering options.

pub

Data Access Layer for AgentEvent operations.

Name Type Description
dal &'a DAL Reference to the main DAL instance.

pub

fn create(
&self,
new_event: &NewAgentEvent,
) -> Result<AgentEvent, diesel::result::Error>

Creates a new agent event in the database.

Parameters:

Name Type Description
new_event &NewAgentEvent A reference to the NewAgentEvent struct containing the event details.

Returns:

Returns a Result containing the created AgentEvent on success, or a diesel::result::Error on failure.

Source
pub fn create(&self, new_event: &NewAgentEvent) -> Result<AgentEvent, diesel::result::Error> {
let conn = &mut self.dal.conn()?;
diesel::insert_into(agent_events::table)
.values(new_event)
.get_result(conn)
}

pub

fn get(
&self,
event_uuid: Uuid,
) -> Result<Option<AgentEvent>, diesel::result::Error>

Retrieves a non-deleted agent event by its UUID.

Parameters:

Name Type Description
event_uuid Uuid The UUID of the agent event to retrieve.

Returns:

Returns a Result containing an Option if found (and not deleted), or a diesel::result::Error on failure.

Source
pub fn get(&self, event_uuid: Uuid) -> Result<Option<AgentEvent>, diesel::result::Error> {
let conn = &mut self.dal.conn()?;
agent_events::table
.filter(agent_events::id.eq(event_uuid))
.filter(agent_events::deleted_at.is_null())
.first(conn)
.optional()
}

pub

fn get_including_deleted(
&self,
event_uuid: Uuid,
) -> Result<Option<AgentEvent>, diesel::result::Error>

Retrieves an agent event by its UUID, including deleted events.

Parameters:

Name Type Description
event_uuid Uuid The UUID of the agent event to retrieve.

Returns:

Returns a Result containing an Option if found (including deleted events), or a diesel::result::Error on failure.

Source
pub fn get_including_deleted(
&self,
event_uuid: Uuid,
) -> Result<Option<AgentEvent>, diesel::result::Error> {
let conn = &mut self.dal.conn()?;
agent_events::table
.filter(agent_events::id.eq(event_uuid))
.first(conn)
.optional()
}

pub

fn list(&self) -> Result<Vec<AgentEvent>, diesel::result::Error>

Lists all non-deleted agent events from the database.

Returns:

Returns a Result containing a Vec of all non-deleted AgentEvents on success, or a diesel::result::Error on failure.

Source
pub fn list(&self) -> Result<Vec<AgentEvent>, diesel::result::Error> {
let conn = &mut self.dal.conn()?;
agent_events::table
.filter(agent_events::deleted_at.is_null())
.load::<AgentEvent>(conn)
}

pub

fn list_for_generator(
&self,
generator_id: Uuid,
) -> Result<Vec<AgentEvent>, diesel::result::Error>

Lists non-deleted events for agents registered under a generator (tenant scoping, BROKKR-T-0270). Single query via an eq_any subselect on agent_generator_registrations.

Source
pub fn list_for_generator(
&self,
generator_id: Uuid,
) -> Result<Vec<AgentEvent>, diesel::result::Error> {
use brokkr_models::schema::agent_generator_registrations;
let conn = &mut self.dal.conn()?;
agent_events::table
.filter(agent_events::deleted_at.is_null())
.filter(
agent_events::agent_id.eq_any(
agent_generator_registrations::table
.filter(agent_generator_registrations::generator_id.eq(generator_id))
.select(agent_generator_registrations::agent_id),
),
)
.load::<AgentEvent>(conn)
}

pub

fn list_all(&self) -> Result<Vec<AgentEvent>, diesel::result::Error>

Lists all agent events from the database, including deleted ones.

Returns:

Returns a Result containing a Vec of all AgentEvents (including deleted ones) on success, or a diesel::result::Error on failure.

Source
pub fn list_all(&self) -> Result<Vec<AgentEvent>, diesel::result::Error> {
let conn = &mut self.dal.conn()?;
agent_events::table.load::<AgentEvent>(conn)
}

pub

fn get_events(
&self,
stack_id: Option<Uuid>,
agent_id: Option<Uuid>,
) -> Result<Vec<AgentEvent>, diesel::result::Error>

Lists agent events from the database with optional filtering by stack and agent.

Parameters:

Name Type Description
stack_id Option<Uuid> Optional UUID to filter events by stack.
agent_id Option<Uuid> Optional UUID to filter events by agent.

Returns:

Returns a Result containing a Vec of filtered AgentEvents on success, or a diesel::result::Error on failure.

Source
pub fn get_events(
&self,
stack_id: Option<Uuid>,
agent_id: Option<Uuid>,
) -> Result<Vec<AgentEvent>, diesel::result::Error> {
let conn = &mut self.dal.conn()?;
let mut query = agent_events::table
.inner_join(deployment_objects::table)
.into_boxed();
if let Some(s_id) = stack_id {
query = query.filter(deployment_objects::stack_id.eq(s_id));
}
if let Some(a_id) = agent_id {
query = query.filter(agent_events::agent_id.eq(a_id));
}
query = query
.filter(agent_events::deleted_at.is_null())
.order(agent_events::created_at.desc());
query
.select(agent_events::all_columns)
.load::<AgentEvent>(conn)
}

pub

fn last_event_at_by_agent(
&self,
) -> Result<Vec<(Uuid, chrono::DateTime<Utc>)>, diesel::result::Error>

Returns the most recent non-deleted event timestamp per agent in a single grouped query (no per-agent fan-out).

Computes MAX(created_at) grouped by agent_id over all non-deleted agent events. Agents with no events are simply absent from the result.

Returns:

Returns a Vec of (agent_id, last_event_at) pairs, or a diesel::result::Error on failure.

Source
pub fn last_event_at_by_agent(
&self,
) -> Result<Vec<(Uuid, chrono::DateTime<Utc>)>, diesel::result::Error> {
let conn = &mut self.dal.conn()?;
agent_events::table
.filter(agent_events::deleted_at.is_null())
.group_by(agent_events::agent_id)
.select((
agent_events::agent_id,
diesel::dsl::max(agent_events::created_at).assume_not_null(),
))
.load::<(Uuid, chrono::DateTime<Utc>)>(conn)
}

pub

fn update(
&self,
event_uuid: Uuid,
updated_event: &AgentEvent,
) -> Result<AgentEvent, diesel::result::Error>

Updates an existing agent event in the database.

Parameters:

Name Type Description
event_uuid Uuid The UUID of the agent event to update.
updated_event &AgentEvent A reference to the AgentEvent struct containing the updated details.

Returns:

Returns a Result containing the updated AgentEvent on success, or a diesel::result::Error on failure.

Source
pub fn update(
&self,
event_uuid: Uuid,
updated_event: &AgentEvent,
) -> Result<AgentEvent, diesel::result::Error> {
let conn = &mut self.dal.conn()?;
diesel::update(agent_events::table.filter(agent_events::id.eq(event_uuid)))
.set(updated_event)
.get_result(conn)
}

pub

fn soft_delete(
&self,
event_uuid: Uuid,
) -> Result<usize, diesel::result::Error>

Soft deletes an agent event by setting its deleted_at timestamp to the current time.

Parameters:

Name Type Description
event_uuid Uuid The UUID of the agent event to soft delete.

Returns:

Returns a Result containing the number of affected rows (0 or 1) on success, or a diesel::result::Error on failure.

Source
pub fn soft_delete(&self, event_uuid: Uuid) -> Result<usize, diesel::result::Error> {
let conn = &mut self.dal.conn()?;
diesel::update(agent_events::table.filter(agent_events::id.eq(event_uuid)))
.set(agent_events::deleted_at.eq(Utc::now()))
.execute(conn)
}

pub

fn hard_delete(
&self,
event_uuid: Uuid,
) -> Result<usize, diesel::result::Error>

Hard deletes an agent event from the database.

Parameters:

Name Type Description
event_uuid Uuid The UUID of the agent event to hard delete.

Returns:

Returns a Result containing the number of affected rows (0 or 1) on success, or a diesel::result::Error on failure.

Source
pub fn hard_delete(&self, event_uuid: Uuid) -> Result<usize, diesel::result::Error> {
let conn = &mut self.dal.conn()?;
diesel::delete(agent_events::table.filter(agent_events::id.eq(event_uuid))).execute(conn)
}

pub

fn delete_older_than(
&self,
cutoff: DateTime<Utc>,
) -> Result<usize, diesel::result::Error>

Hard-deletes all agent events with created_at older than cutoff.

This is the eviction primitive behind the agent_events retention policy (BROKKR-T-0228). Unlike soft_delete, rows are physically removed so the table does not grow without bound. The filter uses created_at (server-side ingestion time), so an agent that backdates an event cannot keep ancient rows alive past the window.

Parameters:

Name Type Description
cutoff DateTime<Utc> Events strictly older than this timestamp are deleted.

Returns:

Returns a Result containing the number of rows deleted on success, or a diesel::result::Error on failure.

Source
pub fn delete_older_than(&self, cutoff: DateTime<Utc>) -> Result<usize, diesel::result::Error> {
let conn = &mut self.dal.conn()?;
diesel::delete(agent_events::table.filter(agent_events::created_at.lt(cutoff)))
.execute(conn)
}