From 3b92c0028ad74c08e7d1ab22d14de0ef28659de8 Mon Sep 17 00:00:00 2001 From: caoqianming Date: Wed, 22 Apr 2026 08:39:17 +0800 Subject: [PATCH] Merge handle_control_event and persist_event_if_needed MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The split existed only so handle_control_event could log UnitStateChanged and then delegate; that's no longer worth its own function. Co-locating the UnitStateChanged special case with the other match arms makes its 'log-only, no persist' treatment self-evident, and the call chain drops from handle → persist → record to handle → record. Co-Authored-By: Claude Opus 4.7 --- crates/app_feeder_distributor/src/event.rs | 40 +++++++--------------- 1 file changed, 13 insertions(+), 27 deletions(-) diff --git a/crates/app_feeder_distributor/src/event.rs b/crates/app_feeder_distributor/src/event.rs index a712509..6db4fef 100644 --- a/crates/app_feeder_distributor/src/event.rs +++ b/crates/app_feeder_distributor/src/event.rs @@ -105,28 +105,7 @@ async fn handle_control_event( ws_manager: Option<&Arc>, metadata: &MetadataCache, ) { - // UnitStateChanged is high-frequency and intentionally not persisted; - // it still needs tracing for local observability. All other events are - // persisted via record_event, which emits tracing automatically. - if let AppEvent::UnitStateChanged { - unit_id, - from_state, - to_state, - } = &event - { - tracing::info!("Unit {} state: {} -> {}", unit_id, from_state, to_state); - } - - persist_event_if_needed(&event, pool, ws_manager, metadata).await; -} - -async fn persist_event_if_needed( - event: &AppEvent, - pool: &sqlx::PgPool, - ws_manager: Option<&Arc>, - metadata: &MetadataCache, -) { - let record: Option = match event { + let record: Option = match &event { AppEvent::EquipmentStartCommandSent { equipment_id, unit_id, @@ -280,11 +259,18 @@ async fn persist_event_if_needed( payload: serde_json::json!({ "unit_id": unit_id }), }) } - AppEvent::UnitStateChanged { .. } => None, + // High-frequency, intentionally not persisted; tracing only for local observability. + AppEvent::UnitStateChanged { + unit_id, + from_state, + to_state, + } => { + tracing::info!("Unit {} state: {} -> {}", unit_id, from_state, to_state); + None + } }; - let Some(record) = record else { - return; - }; - record_event(pool, ws_manager.map(Arc::as_ref), record).await; + if let Some(record) = record { + record_event(pool, ws_manager.map(Arc::as_ref), record).await; + } }