Merge handle_control_event and persist_event_if_needed

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 <noreply@anthropic.com>
This commit is contained in:
caoqianming 2026-04-22 08:39:17 +08:00
parent 52cd3e630e
commit 3b92c0028a
1 changed files with 13 additions and 27 deletions

View File

@ -105,28 +105,7 @@ async fn handle_control_event(
ws_manager: Option<&Arc<WebSocketManager>>, ws_manager: Option<&Arc<WebSocketManager>>,
metadata: &MetadataCache, metadata: &MetadataCache,
) { ) {
// UnitStateChanged is high-frequency and intentionally not persisted; let record: Option<EventInsert> = match &event {
// 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<WebSocketManager>>,
metadata: &MetadataCache,
) {
let record: Option<EventInsert> = match event {
AppEvent::EquipmentStartCommandSent { AppEvent::EquipmentStartCommandSent {
equipment_id, equipment_id,
unit_id, unit_id,
@ -280,11 +259,18 @@ async fn persist_event_if_needed(
payload: serde_json::json!({ "unit_id": unit_id }), 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 { if let Some(record) = record {
return;
};
record_event(pool, ws_manager.map(Arc::as_ref), record).await; record_event(pool, ws_manager.map(Arc::as_ref), record).await;
} }
}