//! 聚合查询容错工具 //! //! 仪表盘等聚合统计端点通常包含多个独立子查询。 //! 单个子查询失败不应导致整个接口 500。 //! `safe_aggregate` 让每个子查询独立容错,失败时返回默认值并记录警告日志。 use std::future::Future; /// 执行一个子查询,失败时返回 `T::default()` 并记录警告日志。 /// /// # 使用场景 /// /// 仪表盘统计 API 聚合多个指标(患者数/咨询数/随访数等), /// 任一子查询失败不应阻塞其他指标返回。 /// /// # 示例 /// /// ```rust,ignore /// let patients = safe_aggregate( /// stats_service::get_patient_statistics(&state, tenant_id), /// "患者统计", /// ).await; /// ``` pub async fn safe_aggregate( fut: impl Future>, label: &str, ) -> T { match fut.await { Ok(v) => { tracing::debug!("聚合子查询 [{label}] 成功"); v } Err(e) => { tracing::warn!("聚合子查询 [{label}] 失败,使用默认值: {e}"); T::default() } } }