schemas_v1_db_queries_alert - OmniCloudOrg/OmniOrchestrator GitHub Wiki
Path: src/schemas/v1/db/queries/alert.rs
- async fn list_alerts
- async fn get_alert_with_related_data
- async fn count_alerts
- async fn create_alert
- async fn update_alert_status
- async fn acknowledge_alert
- async fn resolve_alert
- async fn create_alert_escalation
- async fn add_alert_history
- async fn get_recent_app_alerts
- async fn get_org_active_alerts
- async fn get_alert_stats
- async fn get_alerts_needing_escalation
- async fn auto_resolve_old_alerts
- async fn search_alerts
- async fn count_search_alerts
- async fn bulk_update_alert_status
pub async fn list_alerts(
pool: &Pool<MySql>,
page: i64,
per_page: i64,
status: Option<&str>,
severity: Option<&str>,
org_id: Option<i64>,
app_id: Option<i64>,
service: Option<&str>,
from_date: Option<DateTime<Utc>>,
to_date: Option<DateTime<Utc>>,
) -> anyhow::Result<Vec<Alert>> {
// ... function body
}
Retrieves a paginated list of alerts from the database. This function fetches a subset of alerts based on pagination parameters, ordering them by timestamp in descending order (newest first). Filtering options allow for narrowing down results by various criteria.
-
pool
- Database connection pool for executing the query -
page
- Zero-based page number (e.g., 0 for first page, 1 for second page) -
per_page
- Number of records to fetch per page -
status
- Optional filter for alert status -
severity
- Optional filter for alert severity -
org_id
- Optional filter for organization ID -
app_id
- Optional filter for application ID -
service
- Optional filter for service name -
from_date
- Optional filter for alerts after this timestamp -
to_date
- Optional filter for alerts before this timestamp
-
Ok(Vec<Alert>)
- Successfully retrieved list of alerts -
Err(anyhow::Error)
- Failed to fetch alerts, with context
pub async fn get_alert_with_related_data(
pool: &Pool<MySql>,
id: i64,
) -> anyhow::Result<AlertWithRelatedData> {
// ... function body
}
Retrieves a specific alert by its ID, along with related acknowledgments and escalations. This function fetches a single alert record along with its associated acknowledgments, escalations, and history records to provide comprehensive information about the alert.
-
pool
- Database connection pool for executing the query -
id
- Unique identifier of the alert to retrieve
-
Ok(AlertWithRelatedData)
- Successfully retrieved alert with related data -
Err(anyhow::Error)
- Failed to fetch alert or related data
pub async fn count_alerts(
pool: &Pool<MySql>,
status: Option<&str>,
severity: Option<&str>,
org_id: Option<i64>,
app_id: Option<i64>,
) -> anyhow::Result<i64> {
// ... function body
}
Counts the total number of alerts in the database with optional filtering. This function retrieves the total count of alerts that match the provided filter criteria, which is useful for pagination and reporting purposes.
-
pool
- Database connection pool for executing the query -
status
- Optional filter for alert status -
severity
- Optional filter for alert severity -
org_id
- Optional filter for organization ID -
app_id
- Optional filter for application ID
-
Ok(i64)
- Successfully retrieved count of alerts -
Err(anyhow::Error)
- Failed to count alerts
pub async fn create_alert(
pool: &Pool<MySql>,
alert_type: &str,
severity: &str,
service: &str,
message: &str,
metadata: Option<JsonValue>,
org_id: Option<i64>,
app_id: Option<i64>,
instance_id: Option<i64>,
region_id: Option<i64>,
node_id: Option<i64>,
) -> anyhow::Result<Alert> {
// ... function body
}
Creates a new alert in the database. This function inserts a new alert record with the provided parameters and adds an initial history record to document the alert creation.
-
pool
- Database connection pool for executing the query -
alert_type
- Type of alert (e.g., "cpu_usage", "memory_usage", "disk_space") -
severity
- Severity level of the alert (critical, warning, info) -
service
- Name of the service that generated the alert -
message
- Alert message text describing the issue -
metadata
- Optional JSON data with additional alert details -
org_id
- Optional organization ID related to the alert -
app_id
- Optional application ID related to the alert -
instance_id
- Optional instance ID related to the alert -
region_id
- Optional region ID related to the alert -
node_id
- Optional node/worker ID related to the alert
-
Ok(Alert)
- Successfully created alert, including database-assigned fields -
Err(anyhow::Error)
- Failed to create alert
pub async fn update_alert_status(
pool: &Pool<MySql>,
id: i64,
new_status: &str,
user_id: Option<i64>,
notes: Option<&str>,
) -> anyhow::Result<Alert> {
// ... function body
}
Updates the status of an alert. This function changes the status of an alert and records the change in the alert history table. It can mark alerts as acknowledged, resolved, or auto-resolved based on system or user actions.
-
pool
- Database connection pool for executing the query -
id
- Unique identifier of the alert to update -
new_status
- New status for the alert (active, acknowledged, resolved, auto_resolved) -
user_id
- Optional ID of the user who performed the action -
notes
- Optional notes about the status change
-
Ok(Alert)
- Successfully updated alert -
Err(anyhow::Error)
- Failed to update alert
pub async fn acknowledge_alert(
pool: &Pool<MySql>,
alert_id: i64,
user_id: i64,
notes: Option<&str>,
update_status: bool,
) -> anyhow::Result<AlertAcknowledgment> {
// ... function body
}
Acknowledges an alert by a specific user. This function creates an acknowledgment record for the alert and optionally updates the alert status to 'acknowledged' if it's currently 'active'.
-
pool
- Database connection pool for executing the query -
alert_id
- ID of the alert to acknowledge -
user_id
- ID of the user acknowledging the alert -
notes
- Optional notes about the acknowledgment -
update_status
- Whether to update the alert status to 'acknowledged'
-
Ok(AlertAcknowledgment)
- Successfully created acknowledgment -
Err(anyhow::Error)
- Failed to acknowledge alert
pub async fn resolve_alert(
pool: &Pool<MySql>,
id: i64,
user_id: i64,
notes: Option<&str>,
) -> anyhow::Result<Alert> {
// ... function body
}
Resolves an alert by a specific user. This function updates the alert status to 'resolved', sets the resolved_at timestamp and resolved_by user ID, and creates a history record for the resolution.
-
pool
- Database connection pool for executing the query -
id
- ID of the alert to resolve -
user_id
- ID of the user resolving the alert -
notes
- Optional notes about the resolution
-
Ok(Alert)
- Successfully resolved alert -
Err(anyhow::Error)
- Failed to resolve alert
pub async fn create_alert_escalation(
pool: &Pool<MySql>,
alert_id: i64,
escalation_level: i64,
escalated_to: JsonValue,
escalation_method: &str,
response_required_by: Option<DateTime<Utc>>,
) -> anyhow::Result<AlertEscalation> {
// ... function body
}
Creates an escalation record for an alert. This function adds an escalation record to indicate that an alert has been escalated to another level of attention, such as notifying administrators or external systems when an alert has not been addressed in a timely manner.
-
pool
- Database connection pool for executing the query -
alert_id
- ID of the alert being escalated -
escalation_level
- Level of escalation (typically 1, 2, 3, etc.) -
escalated_to
- JSON data specifying where/who the alert was escalated to -
escalation_method
- Method used for escalation (email, SMS, webhook, etc.) -
response_required_by
- Optional deadline for when a response is required
-
Ok(AlertEscalation)
- Successfully created escalation record -
Err(anyhow::Error)
- Failed to create escalation record
pub async fn add_alert_history(
pool: &Pool<MySql>,
alert_id: i64,
action: &str,
performed_by: Option<i64>,
notes: Option<&str>,
previous_state: Option<JsonValue>,
new_state: Option<JsonValue>,
) -> anyhow::Result<AlertHistory> {
// ... function body
}
Adds a custom history entry for an alert. This function allows adding arbitrary history records for an alert, which is useful for tracking manual interventions or system actions that don't fit into predefined categories.
-
pool
- Database connection pool for executing the query -
alert_id
- ID of the alert -
action
- Description of the action being recorded -
performed_by
- Optional ID of the user who performed the action -
notes
- Optional notes about the action -
previous_state
- Optional JSON data representing the state before the action -
new_state
- Optional JSON data representing the state after the action
-
Ok(AlertHistory)
- Successfully created history record -
Err(anyhow::Error)
- Failed to create history record
pub async fn get_recent_app_alerts(
pool: &Pool<MySql>,
app_id: i64,
limit: i64,
include_resolved: bool,
) -> anyhow::Result<Vec<Alert>> {
// ... function body
}
Retrieves a list of recent alerts for a specific application. This function fetches alerts associated with an application, typically for display on an application dashboard or status page.
-
pool
- Database connection pool for executing the query -
app_id
- ID of the application -
limit
- Maximum number of alerts to retrieve -
include_resolved
- Whether to include resolved alerts
-
Ok(Vec<Alert>)
- Successfully retrieved list of alerts -
Err(anyhow::Error)
- Failed to fetch alerts
pub async fn get_org_active_alerts(
pool: &Pool<MySql>,
org_id: i64,
limit: i64,
) -> anyhow::Result<Vec<Alert>> {
// ... function body
}
Retrieves a list of active alerts for a specific organization. This function fetches active and acknowledged alerts across all applications and services within an organization, ordered by severity and timestamp.
-
pool
- Database connection pool for executing the query -
org_id
- ID of the organization -
limit
- Maximum number of alerts to retrieve
-
Ok(Vec<Alert>)
- Successfully retrieved list of alerts -
Err(anyhow::Error)
- Failed to fetch alerts
pub async fn get_alert_stats(
pool: &Pool<MySql>,
org_id: i64,
days: i64,
) -> anyhow::Result<JsonValue> {
// ... function body
}
Gets statistics about alerts for an organization grouped by severity and status. This function retrieves counts of alerts for an organization, grouped by different categories to provide an overview of the alert landscape.
-
pool
- Database connection pool for executing the query -
org_id
- ID of the organization -
days
- Number of days to look back for statistics
-
Ok(JsonValue)
- JSON object with alert statistics -
Err(anyhow::Error)
- Failed to fetch alert statistics
pub async fn get_alerts_needing_escalation(
pool: &Pool<MySql>,
org_id: Option<i64>,
hours_threshold: i64,
) -> anyhow::Result<Vec<Alert>> {
// ... function body
}
Retrieves alerts that need escalation based on age and status. This function identifies alerts that have been active for longer than a specified threshold period without being acknowledged or resolved, which may indicate they need to be escalated to ensure appropriate attention.
-
pool
- Database connection pool for executing the query -
org_id
- Optional organization ID to filter alerts -
hours_threshold
- Age in hours after which an alert should be considered for escalation
-
Ok(Vec<Alert>)
- List of alerts needing escalation -
Err(anyhow::Error)
- Failed to fetch alerts
pub async fn auto_resolve_old_alerts(
pool: &Pool<MySql>,
days_threshold: i64,
severity_levels: Option<Vec<&str>>,
) -> anyhow::Result<i64> {
// ... function body
}
Auto-resolves alerts that have been active for longer than a specified period. This function updates the status of old alerts to 'auto_resolved' based on criteria such as age, severity, and current status. It's typically used for housekeeping to prevent the accumulation of stale alerts.
-
pool
- Database connection pool for executing the query -
days_threshold
- Age in days after which an alert should be auto-resolved -
severity_levels
- Optional vector of severity levels to include (e.g., only auto-resolve 'info' alerts)
-
Ok(i64)
- Number of alerts auto-resolved -
Err(anyhow::Error)
- Failed to auto-resolve alerts
pub async fn search_alerts(
pool: &Pool<MySql>,
search_query: &str,
org_id: Option<i64>,
page: i64,
per_page: i64,
) -> anyhow::Result<Vec<Alert>> {
// ... function body
}
Retrieves alerts that match a specific search query. This function searches for alerts containing specific text in their message, type, or service fields. It's useful for implementing search functionality in the alerts UI.
-
pool
- Database connection pool for executing the query -
search_query
- Text to search for in alert fields -
org_id
- Optional organization ID to filter alerts -
page
- Zero-based page number for pagination -
per_page
- Number of records per page
-
Ok(Vec<Alert>)
- List of matching alerts -
Err(anyhow::Error)
- Failed to search for alerts
pub async fn count_search_alerts(
pool: &Pool<MySql>,
search_query: &str,
org_id: Option<i64>,
) -> anyhow::Result<i64> {
// ... function body
}
Gets the count of matching alerts for a search query. This function counts the number of alerts that match a specific search query, which is useful for pagination in search results.
-
pool
- Database connection pool for executing the query -
search_query
- Text to search for in alert fields -
org_id
- Optional organization ID to filter alerts
-
Ok(i64)
- Count of matching alerts -
Err(anyhow::Error)
- Failed to count alerts
pub async fn bulk_update_alert_status(
pool: &Pool<MySql>,
ids: Option<Vec<i64>>,
service: Option<&str>,
app_id: Option<i64>,
new_status: &str,
user_id: i64,
notes: Option<&str>,
) -> anyhow::Result<i64> {
// ... function body
}
Bulk updates the status of multiple alerts. This function changes the status of multiple alerts at once based on provided criteria, which is useful for operations like resolving all alerts for a specific service or application.
-
pool
- Database connection pool for executing the query -
ids
- Optional vector of alert IDs to update -
service
- Optional service name to filter alerts -
app_id
- Optional application ID to filter alerts -
new_status
- Status to set for matching alerts -
user_id
- ID of the user performing the bulk update -
notes
- Optional notes about the bulk update
-
Ok(i64)
- Number of alerts updated -
Err(anyhow::Error)
- Failed to update alerts