Skip to content

brokkr-broker::dal::deployment_health

Rust

Data Access Layer for DeploymentHealth operations.

This module provides functionality to interact with the deployment_health table in the database. It includes methods for upserting health status, querying health by agent/deployment/stack, and aggregating health across deployments.

pub

Data Access Layer for DeploymentHealth operations.

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

pub

fn upsert(
&self,
new_health: &NewDeploymentHealth,
) -> Result<DeploymentHealth, diesel::result::Error>

Upserts a deployment health record.

If a record already exists for the agent+deployment_object combination, it will be updated. Otherwise, a new record will be created.

Parameters:

Name Type Description
new_health &NewDeploymentHealth The health record to upsert.

Returns:

Returns the upserted DeploymentHealth record.

Source
pub fn upsert(
&self,
new_health: &NewDeploymentHealth,
) -> Result<DeploymentHealth, diesel::result::Error> {
let conn = &mut self.dal.conn()?;
diesel::insert_into(deployment_health::table)
.values(new_health)
.on_conflict((
deployment_health::agent_id,
deployment_health::deployment_object_id,
))
.do_update()
.set((
deployment_health::status.eq(excluded(deployment_health::status)),
deployment_health::summary.eq(excluded(deployment_health::summary)),
deployment_health::checked_at.eq(excluded(deployment_health::checked_at)),
))
.get_result(conn)
}

pub

fn upsert_batch(
&self,
health_records: &[NewDeploymentHealth],
) -> Result<usize, diesel::result::Error>

Upserts multiple deployment health records in a batch.

Parameters:

Name Type Description
health_records &[NewDeploymentHealth] A slice of health records to upsert.

Returns:

Returns the number of records affected.

Source
pub fn upsert_batch(
&self,
health_records: &[NewDeploymentHealth],
) -> Result<usize, diesel::result::Error> {
if health_records.is_empty() {
return Ok(0);
}
let conn = &mut self.dal.conn()?;
diesel::insert_into(deployment_health::table)
.values(health_records)
.on_conflict((
deployment_health::agent_id,
deployment_health::deployment_object_id,
))
.do_update()
.set((
deployment_health::status.eq(excluded(deployment_health::status)),
deployment_health::summary.eq(excluded(deployment_health::summary)),
deployment_health::checked_at.eq(excluded(deployment_health::checked_at)),
))
.execute(conn)
}

pub

fn get_by_agent_and_deployment(
&self,
agent_id: Uuid,
deployment_object_id: Uuid,
) -> Result<Option<DeploymentHealth>, diesel::result::Error>

Gets the health record for a specific agent and deployment object.

Parameters:

Name Type Description
agent_id Uuid The agent UUID.
deployment_object_id Uuid The deployment object UUID.

Returns:

Returns the health record if found.

Source
pub fn get_by_agent_and_deployment(
&self,
agent_id: Uuid,
deployment_object_id: Uuid,
) -> Result<Option<DeploymentHealth>, diesel::result::Error> {
let conn = &mut self.dal.conn()?;
deployment_health::table
.filter(deployment_health::agent_id.eq(agent_id))
.filter(deployment_health::deployment_object_id.eq(deployment_object_id))
.first(conn)
.optional()
}

pub

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

Gets the health record by its ID.

Parameters:

Name Type Description
id Uuid The health record UUID.

Returns:

Returns the health record if found.

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

pub

fn list_by_deployment_object(
&self,
deployment_object_id: Uuid,
) -> Result<Vec<DeploymentHealth>, diesel::result::Error>

Lists all health records for a specific deployment object (across all agents).

Parameters:

Name Type Description
deployment_object_id Uuid The deployment object UUID.

Returns:

Returns a list of health records.

Source
pub fn list_by_deployment_object(
&self,
deployment_object_id: Uuid,
) -> Result<Vec<DeploymentHealth>, diesel::result::Error> {
let conn = &mut self.dal.conn()?;
deployment_health::table
.filter(deployment_health::deployment_object_id.eq(deployment_object_id))
.order(deployment_health::checked_at.desc())
.load(conn)
}

pub

fn list_by_agent(
&self,
agent_id: Uuid,
) -> Result<Vec<DeploymentHealth>, diesel::result::Error>

Lists all health records for a specific agent.

Parameters:

Name Type Description
agent_id Uuid The agent UUID.

Returns:

Returns a list of health records.

Source
pub fn list_by_agent(
&self,
agent_id: Uuid,
) -> Result<Vec<DeploymentHealth>, diesel::result::Error> {
let conn = &mut self.dal.conn()?;
deployment_health::table
.filter(deployment_health::agent_id.eq(agent_id))
.order(deployment_health::checked_at.desc())
.load(conn)
}

pub

fn list_by_stack(
&self,
stack_id: Uuid,
) -> Result<Vec<DeploymentHealth>, diesel::result::Error>

Lists all health records for deployment objects in a specific stack.

Parameters:

Name Type Description
stack_id Uuid The stack UUID.

Returns:

Returns a list of health records for all deployment objects in the stack.

Source
pub fn list_by_stack(
&self,
stack_id: Uuid,
) -> Result<Vec<DeploymentHealth>, diesel::result::Error> {
let conn = &mut self.dal.conn()?;
deployment_health::table
.inner_join(deployment_objects::table)
.filter(deployment_objects::stack_id.eq(stack_id))
.filter(deployment_objects::deleted_at.is_null())
.select(deployment_health::all_columns)
.order(deployment_health::checked_at.desc())
.load(conn)
}

pub

fn list_by_status(
&self,
status: &str,
) -> Result<Vec<DeploymentHealth>, diesel::result::Error>

Lists all health records with a specific status.

Parameters:

Name Type Description
status &str The health status to filter by (e.g., “degraded”, “failing”).

Returns:

Returns a list of health records matching the status.

Source
pub fn list_by_status(
&self,
status: &str,
) -> Result<Vec<DeploymentHealth>, diesel::result::Error> {
let conn = &mut self.dal.conn()?;
deployment_health::table
.filter(deployment_health::status.eq(status))
.order(deployment_health::checked_at.desc())
.load(conn)
}

pub

fn status_counts_by_agent(
&self,
) -> Result<Vec<(Uuid, String, i64)>, diesel::result::Error>

Returns deployment-health record counts grouped by (agent_id, status) in a single query (no per-agent fan-out).

Useful for the fleet rollup, which needs per-agent counts of failing and degraded health records without issuing one query per agent.

Returns:

Returns a Vec of (agent_id, status, count) tuples, or a diesel::result::Error on failure.

Source
pub fn status_counts_by_agent(
&self,
) -> Result<Vec<(Uuid, String, i64)>, diesel::result::Error> {
let conn = &mut self.dal.conn()?;
deployment_health::table
.group_by((deployment_health::agent_id, deployment_health::status))
.select((
deployment_health::agent_id,
deployment_health::status,
diesel::dsl::count_star(),
))
.load::<(Uuid, String, i64)>(conn)
}
delete_​by_​agent_​and_​deployment
Section titled “delete_​by_​agent_​and_​deployment”

pub

fn delete_by_agent_and_deployment(
&self,
agent_id: Uuid,
deployment_object_id: Uuid,
) -> Result<usize, diesel::result::Error>

Deletes the health record for a specific agent and deployment object.

Parameters:

Name Type Description
agent_id Uuid The agent UUID.
deployment_object_id Uuid The deployment object UUID.

Returns:

Returns the number of records deleted.

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

pub

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

Deletes all health records for a specific agent.

Parameters:

Name Type Description
agent_id Uuid The agent UUID.

Returns:

Returns the number of records deleted.

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