use serde::{Deserialize, Serialize};
use uuid::Uuid;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DataStream {
pub id: Uuid,
pub source_system: String,
pub source_key: String,
pub source_name: Option<String>,
pub source_path: Option<String>,
pub metadata: serde_json::Value,
pub site_parameter_id: Option<Uuid>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub measurement_type: Option<String>,
pub is_active: bool,
pub last_data_time: Option<chrono::DateTime<chrono::Utc>>,
}
#[derive(Debug, Serialize)]
pub struct RegisterStreamRequest {
pub source_system: String,
pub source_key: String,
pub source_name: Option<String>,
pub source_path: Option<String>,
pub metadata: serde_json::Value,
#[serde(skip_serializing_if = "Option::is_none")]
pub measurement_type: Option<String>,
}
#[derive(Debug, Clone, Serialize)]
pub struct IngestReading {
pub time: chrono::DateTime<chrono::Utc>,
pub raw_value: f64,
#[serde(skip_serializing_if = "is_zero")]
pub replicate_index: i16,
#[serde(skip_serializing_if = "Option::is_none")]
pub sensor_id: Option<Uuid>,
#[serde(skip_serializing_if = "Option::is_none")]
pub calibration_id: Option<Uuid>,
#[serde(skip_serializing_if = "Option::is_none")]
pub deployment_id: Option<Uuid>,
#[serde(skip_serializing_if = "Option::is_none")]
pub measurement_type: Option<String>,
}
impl IngestReading {
pub fn new(time: chrono::DateTime<chrono::Utc>, raw_value: f64) -> Self {
Self {
time,
raw_value,
replicate_index: 0,
sensor_id: None,
calibration_id: None,
deployment_id: None,
measurement_type: None,
}
}
}
fn is_zero(v: &i16) -> bool {
*v == 0
}
#[derive(Debug, Serialize)]
pub struct IngestStatusEvent {
pub time: chrono::DateTime<chrono::Utc>,
pub value: String,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_ingest_reading_serialization() {
let r = IngestReading::new(chrono::Utc::now(), 42.5);
let json = serde_json::to_value(&r).unwrap();
assert_eq!(json["raw_value"], 42.5);
assert!(json.get("replicate_index").is_none());
assert!(json.get("sensor_id").is_none());
assert!(json.get("measurement_type").is_none());
}
#[test]
fn test_register_stream_request() {
let req = RegisterStreamRequest {
source_system: "test_system".to_string(),
source_key: "source_1".to_string(),
source_name: Some("stream_a".to_string()),
source_path: None,
metadata: serde_json::json!({"device": "dev_001"}),
measurement_type: None,
};
let json = serde_json::to_value(&req).unwrap();
assert_eq!(json["source_system"], "test_system");
assert_eq!(json["metadata"]["device"], "dev_001");
}
#[test]
fn test_data_stream_deserialization() {
let json = serde_json::json!({
"id": "550e8400-e29b-41d4-a716-446655440000",
"source_system": "test_system",
"source_key": "source_1",
"source_name": "stream_a",
"source_path": null,
"metadata": {},
"site_parameter_id": null,
"is_active": true,
"last_data_time": null
});
let stream: DataStream = serde_json::from_value(json).unwrap();
assert_eq!(stream.source_system, "test_system");
assert!(stream.is_active);
assert!(stream.site_parameter_id.is_none());
}
}