refactor(events): add shared event envelopes with namespaces
This commit is contained in:
parent
7d83cf27dd
commit
de1879bbf2
|
|
@ -0,0 +1,17 @@
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use serde_json::Value;
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||||
|
pub struct EventEnvelope {
|
||||||
|
pub event_type: String,
|
||||||
|
pub payload: Value,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl EventEnvelope {
|
||||||
|
pub fn new(event_type: impl Into<String>, payload: Value) -> Self {
|
||||||
|
Self {
|
||||||
|
event_type: event_type.into(),
|
||||||
|
payload,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,9 +1,12 @@
|
||||||
pub mod bootstrap;
|
pub mod bootstrap;
|
||||||
pub mod connection;
|
pub mod connection;
|
||||||
pub mod db;
|
pub mod db;
|
||||||
|
pub mod event;
|
||||||
pub mod model;
|
pub mod model;
|
||||||
pub mod platform_context;
|
pub mod platform_context;
|
||||||
pub mod service;
|
pub mod service;
|
||||||
pub mod telemetry;
|
pub mod telemetry;
|
||||||
pub mod util;
|
pub mod util;
|
||||||
|
|
||||||
|
pub use event::EventEnvelope;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,16 @@
|
||||||
|
use plc_platform_core::event::EventEnvelope;
|
||||||
|
use serde_json::json;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn namespaced_event_types_keep_their_prefix() {
|
||||||
|
let envelope = EventEnvelope::new(
|
||||||
|
"feeder.auto_control_started",
|
||||||
|
json!({"unit_id": "00000000-0000-0000-0000-000000000000"}),
|
||||||
|
);
|
||||||
|
|
||||||
|
assert!(envelope.event_type.starts_with("feeder."));
|
||||||
|
assert_eq!(
|
||||||
|
envelope.payload["unit_id"],
|
||||||
|
"00000000-0000-0000-0000-000000000000"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
use plc_platform_core::event::EventEnvelope;
|
||||||
use tokio::sync::mpsc;
|
use tokio::sync::mpsc;
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
use plc_platform_core::model::EventRecord;
|
use plc_platform_core::model::EventRecord;
|
||||||
|
|
@ -457,6 +458,7 @@ async fn persist_event_if_needed(
|
||||||
let Some((event_type, level, unit_id, equipment_id, source_id, message, payload)) = record else {
|
let Some((event_type, level, unit_id, equipment_id, source_id, message, payload)) = record else {
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
|
let envelope = EventEnvelope::new(event_type, payload);
|
||||||
|
|
||||||
let inserted = sqlx::query_as::<_, EventRecord>(
|
let inserted = sqlx::query_as::<_, EventRecord>(
|
||||||
r#"
|
r#"
|
||||||
|
|
@ -465,13 +467,13 @@ async fn persist_event_if_needed(
|
||||||
RETURNING *
|
RETURNING *
|
||||||
"#,
|
"#,
|
||||||
)
|
)
|
||||||
.bind(event_type)
|
.bind(envelope.event_type)
|
||||||
.bind(level)
|
.bind(level)
|
||||||
.bind(unit_id as Option<Uuid>)
|
.bind(unit_id as Option<Uuid>)
|
||||||
.bind(equipment_id as Option<Uuid>)
|
.bind(equipment_id as Option<Uuid>)
|
||||||
.bind(source_id)
|
.bind(source_id)
|
||||||
.bind(message)
|
.bind(message)
|
||||||
.bind(sqlx::types::Json(payload))
|
.bind(sqlx::types::Json(envelope.payload))
|
||||||
.fetch_one(pool)
|
.fetch_one(pool)
|
||||||
.await;
|
.await;
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue