INSERT alert event into a history table via StatementExecutor.
(
state: &SharedState,
tenant_id: u64,
owner: &str,
table: &str,
columns: &[String],
event: &AlertEvent,
)
| 177 | |
| 178 | /// INSERT alert event into a history table via StatementExecutor. |
| 179 | async fn notify_insert( |
| 180 | state: &SharedState, |
| 181 | tenant_id: u64, |
| 182 | owner: &str, |
| 183 | table: &str, |
| 184 | columns: &[String], |
| 185 | event: &AlertEvent, |
| 186 | ) { |
| 187 | // Build INSERT statement. |
| 188 | let col_list = if columns.is_empty() { |
| 189 | "alert_name, group_key, severity, status, value, threshold, timestamp_ms".to_string() |
| 190 | } else { |
| 191 | columns.join(", ") |
| 192 | }; |
| 193 | |
| 194 | let values = format!( |
| 195 | "'{}', '{}', '{}', '{}', {}, {}, {}", |
| 196 | escape_sql(&event.alert_name), |
| 197 | escape_sql(&event.group_key), |
| 198 | escape_sql(&event.severity), |
| 199 | escape_sql(&event.status), |
| 200 | event.value, |
| 201 | event.threshold, |
| 202 | event.timestamp_ms, |
| 203 | ); |
| 204 | |
| 205 | let sql = format!("BEGIN INSERT INTO {table} ({col_list}) VALUES ({values}); END"); |
| 206 | |
| 207 | let identity = alert_identity(TenantId::new(tenant_id), owner); |
| 208 | let block = match crate::control::planner::procedural::parse_block(&sql) { |
| 209 | Ok(b) => b, |
| 210 | Err(e) => { |
| 211 | warn!( |
| 212 | alert = event.alert_name, |
| 213 | table, |
| 214 | error = %e, |
| 215 | "failed to parse INSERT statement for alert history" |
| 216 | ); |
| 217 | return; |
| 218 | } |
| 219 | }; |
| 220 | |
| 221 | let executor = StatementExecutor::new(state, identity, TenantId::new(tenant_id), 0); |
| 222 | let bindings = RowBindings::empty(); |
| 223 | |
| 224 | if let Err(e) = executor.execute_block(&block, &bindings).await { |
| 225 | warn!( |
| 226 | alert = event.alert_name, |
| 227 | table, |
| 228 | error = %e, |
| 229 | "failed to INSERT alert event into history table" |
| 230 | ); |
| 231 | } else { |
| 232 | info!( |
| 233 | alert = event.alert_name, |
| 234 | table, |
| 235 | status = event.status, |
| 236 | "alert event inserted into history table" |
no test coverage detected