Emit tracing inside record_event and drop duplicate feeder match

record_event now logs at the matching tracing level (error/warn/info)
using the persisted message — giving every app uniform event logs for
free. Feeder's handle_control_event collapses from a 60-line match
(which just duplicated the persisted message with less-readable UUIDs)
to a single if-let for UnitStateChanged, which is the only AppEvent
that is intentionally not persisted.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
caoqianming 2026-04-21 20:11:59 +08:00
parent 1c646dfaa7
commit 3e0d4c242b
2 changed files with 19 additions and 64 deletions

View File

@ -92,71 +92,19 @@ async fn handle_control_event(
pool: &sqlx::PgPool, pool: &sqlx::PgPool,
ws_manager: Option<&std::sync::Arc<WebSocketManager>>, ws_manager: Option<&std::sync::Arc<WebSocketManager>>,
) { ) {
persist_event_if_needed(&event, pool, ws_manager).await; // UnitStateChanged is high-frequency and intentionally not persisted;
// it still needs tracing for local observability. All other events are
match event { // persisted via record_event, which emits tracing automatically.
AppEvent::EquipmentStartCommandSent { if let AppEvent::UnitStateChanged {
equipment_id, unit_id,
unit_id, from_state,
point_id, to_state,
} => { } = &event
tracing::info!( {
"Equipment start command sent: equipment={}, unit={:?}, point={}", tracing::info!("Unit {} state: {} -> {}", unit_id, from_state, to_state);
equipment_id,
unit_id,
point_id
);
}
AppEvent::EquipmentStopCommandSent {
equipment_id,
unit_id,
point_id,
} => {
tracing::info!(
"Equipment stop command sent: equipment={}, unit={:?}, point={}",
equipment_id,
unit_id,
point_id
);
}
AppEvent::AutoControlStarted { unit_id } => {
tracing::info!("Auto control started for unit {}", unit_id);
}
AppEvent::AutoControlStopped { unit_id } => {
tracing::info!("Auto control stopped for unit {}", unit_id);
}
AppEvent::FaultLocked {
unit_id,
equipment_id,
} => {
tracing::warn!("Fault locked: unit={}, equipment={}", unit_id, equipment_id);
}
AppEvent::FaultAcked { unit_id } => {
tracing::info!("Fault acked for unit {}", unit_id);
}
AppEvent::CommLocked { unit_id } => {
tracing::warn!("Comm locked for unit {}", unit_id);
}
AppEvent::CommRecovered { unit_id } => {
tracing::info!("Comm recovered for unit {}", unit_id);
}
AppEvent::RemLocal {
unit_id,
equipment_id,
} => {
tracing::warn!("REM local: unit={}, equipment={}", unit_id, equipment_id);
}
AppEvent::RemRecovered { unit_id } => {
tracing::info!("REM recovered for unit {}", unit_id);
}
AppEvent::UnitStateChanged {
unit_id,
from_state,
to_state,
} => {
tracing::info!("Unit {} state: {} -> {}", unit_id, from_state, to_state);
}
} }
persist_event_if_needed(&event, pool, ws_manager).await;
} }
async fn fetch_unit_code(pool: &sqlx::PgPool, id: Uuid) -> String { async fn fetch_unit_code(pool: &sqlx::PgPool, id: Uuid) -> String {

View File

@ -64,6 +64,13 @@ pub async fn record_event(
event: EventInsert, event: EventInsert,
) { ) {
let event_type = event.event_type; let event_type = event.event_type;
match event.level {
"error" => tracing::error!(event_type, "{}", event.message),
"warn" => tracing::warn!(event_type, "{}", event.message),
_ => tracing::info!(event_type, "{}", event.message),
}
let envelope = EventEnvelope::new(event_type, event.payload); let envelope = EventEnvelope::new(event_type, event.payload);
let inserted = sqlx::query_as::<_, EventRecord>( let inserted = sqlx::query_as::<_, EventRecord>(