use serde::{Deserialize, Serialize};
use crate::schema::EdgeType;
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct Edge {
pub source_id: String,
pub target_id: String,
pub kind: EdgeType,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub payload: Option<serde_json::Value>,
#[serde(default)]
pub first_seen_ms: u64,
#[serde(default)]
pub last_seen_ms: u64,
}
impl Edge {
#[must_use]
pub fn new(source_id: impl Into<String>, target_id: impl Into<String>, kind: EdgeType) -> Self {
let now = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap_or_default()
.as_millis() as u64;
Self {
source_id: source_id.into(),
target_id: target_id.into(),
kind,
payload: None,
first_seen_ms: now,
last_seen_ms: now,
}
}
#[must_use]
pub fn with_payload(mut self, payload: impl Serialize) -> Self {
self.payload = match serde_json::to_value(payload) {
Ok(v) => Some(v),
Err(e) => {
tracing::warn!(error = %e, "graph: payload serialization failed; payload omitted");
None
}
};
self
}
}