use cognee_core::pipeline_run_registry::RunEventKind;
use cognee_database::PipelineRunStatus;
use serde::{Deserialize, Serialize};
use utoipa::ToSchema;
use uuid::Uuid;
#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
#[serde(rename_all = "snake_case")]
pub struct PipelineRunInfoDTO {
pub status: String,
pub pipeline_run_id: Uuid,
pub dataset_id: Uuid,
pub dataset_name: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub payload: Option<serde_json::Value>,
#[serde(skip_serializing_if = "Option::is_none")]
pub error: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub data_ingestion_info: Option<Vec<DataIngestionInfoDTO>>,
}
#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
#[serde(rename_all = "snake_case")]
pub struct DataIngestionInfoDTO {
pub data_id: Uuid,
pub content_hash: String,
pub name: String,
pub extension: String,
pub mime_type: String,
pub raw_data_location: String,
}
pub fn event_kind_to_python_string(kind: &RunEventKind) -> &'static str {
match kind {
RunEventKind::Started => "PipelineRunStarted",
RunEventKind::Yield => "PipelineRunYield",
RunEventKind::Completed => "PipelineRunCompleted",
RunEventKind::Errored { .. } => "PipelineRunErrored",
RunEventKind::AlreadyCompleted => "PipelineRunAlreadyCompleted",
}
}
pub fn python_string_to_event_kind(s: &str) -> Option<RunEventKind> {
match s {
"PipelineRunStarted" => Some(RunEventKind::Started),
"PipelineRunYield" => Some(RunEventKind::Yield),
"PipelineRunCompleted" => Some(RunEventKind::Completed),
"PipelineRunErrored" => Some(RunEventKind::Errored {
message: String::new(),
}),
"PipelineRunAlreadyCompleted" => Some(RunEventKind::AlreadyCompleted),
_ => None,
}
}
pub fn pipeline_status_to_db_string(status: &PipelineRunStatus) -> &'static str {
match status {
PipelineRunStatus::Initiated => "DATASET_PROCESSING_INITIATED",
PipelineRunStatus::Started => "DATASET_PROCESSING_STARTED",
PipelineRunStatus::Completed => "DATASET_PROCESSING_COMPLETED",
PipelineRunStatus::Errored => "DATASET_PROCESSING_ERRORED",
}
}
pub fn db_string_to_pipeline_status(s: &str) -> Option<PipelineRunStatus> {
match s {
"DATASET_PROCESSING_INITIATED" => Some(PipelineRunStatus::Initiated),
"DATASET_PROCESSING_STARTED" => Some(PipelineRunStatus::Started),
"DATASET_PROCESSING_COMPLETED" => Some(PipelineRunStatus::Completed),
"DATASET_PROCESSING_ERRORED" => Some(PipelineRunStatus::Errored),
_ => None,
}
}
#[cfg(test)]
#[allow(
clippy::unwrap_used,
clippy::expect_used,
reason = "test code — panics are acceptable failures"
)]
mod tests {
use super::*;
#[test]
fn event_kind_started_round_trip() {
let s = event_kind_to_python_string(&RunEventKind::Started);
assert_eq!(s, "PipelineRunStarted");
assert!(matches!(
python_string_to_event_kind(s).unwrap(),
RunEventKind::Started
));
}
#[test]
fn event_kind_yield_round_trip() {
let s = event_kind_to_python_string(&RunEventKind::Yield);
assert_eq!(s, "PipelineRunYield");
assert!(matches!(
python_string_to_event_kind(s).unwrap(),
RunEventKind::Yield
));
}
#[test]
fn event_kind_completed_round_trip() {
let s = event_kind_to_python_string(&RunEventKind::Completed);
assert_eq!(s, "PipelineRunCompleted");
assert!(matches!(
python_string_to_event_kind(s).unwrap(),
RunEventKind::Completed
));
}
#[test]
fn event_kind_errored_round_trip() {
let kind = RunEventKind::Errored {
message: "boom".into(),
};
let s = event_kind_to_python_string(&kind);
assert_eq!(s, "PipelineRunErrored");
assert!(matches!(
python_string_to_event_kind(s).unwrap(),
RunEventKind::Errored { .. }
));
}
#[test]
fn event_kind_already_completed_round_trip() {
let s = event_kind_to_python_string(&RunEventKind::AlreadyCompleted);
assert_eq!(s, "PipelineRunAlreadyCompleted");
assert!(matches!(
python_string_to_event_kind(s).unwrap(),
RunEventKind::AlreadyCompleted
));
}
#[test]
fn unknown_live_event_string_returns_none() {
assert!(python_string_to_event_kind("UnknownStatus").is_none());
assert!(python_string_to_event_kind("").is_none());
}
#[test]
fn pipeline_status_initiated_round_trip() {
let s = pipeline_status_to_db_string(&PipelineRunStatus::Initiated);
assert_eq!(s, "DATASET_PROCESSING_INITIATED");
assert!(matches!(
db_string_to_pipeline_status(s).unwrap(),
PipelineRunStatus::Initiated
));
}
#[test]
fn pipeline_status_started_round_trip() {
let s = pipeline_status_to_db_string(&PipelineRunStatus::Started);
assert_eq!(s, "DATASET_PROCESSING_STARTED");
assert!(matches!(
db_string_to_pipeline_status(s).unwrap(),
PipelineRunStatus::Started
));
}
#[test]
fn pipeline_status_completed_round_trip() {
let s = pipeline_status_to_db_string(&PipelineRunStatus::Completed);
assert_eq!(s, "DATASET_PROCESSING_COMPLETED");
assert!(matches!(
db_string_to_pipeline_status(s).unwrap(),
PipelineRunStatus::Completed
));
}
#[test]
fn pipeline_status_errored_round_trip() {
let s = pipeline_status_to_db_string(&PipelineRunStatus::Errored);
assert_eq!(s, "DATASET_PROCESSING_ERRORED");
assert!(matches!(
db_string_to_pipeline_status(s).unwrap(),
PipelineRunStatus::Errored
));
}
#[test]
fn unknown_db_string_returns_none() {
assert!(db_string_to_pipeline_status("UNKNOWN").is_none());
assert!(db_string_to_pipeline_status("").is_none());
}
#[test]
fn all_live_event_strings_are_python_literals() {
let expected = [
"PipelineRunStarted",
"PipelineRunYield",
"PipelineRunCompleted",
"PipelineRunErrored",
"PipelineRunAlreadyCompleted",
];
let produced = [
event_kind_to_python_string(&RunEventKind::Started),
event_kind_to_python_string(&RunEventKind::Yield),
event_kind_to_python_string(&RunEventKind::Completed),
event_kind_to_python_string(&RunEventKind::Errored {
message: "x".into(),
}),
event_kind_to_python_string(&RunEventKind::AlreadyCompleted),
];
assert_eq!(expected, produced);
}
#[test]
fn all_durable_strings_are_python_literals() {
let expected = [
"DATASET_PROCESSING_INITIATED",
"DATASET_PROCESSING_STARTED",
"DATASET_PROCESSING_COMPLETED",
"DATASET_PROCESSING_ERRORED",
];
let produced = [
pipeline_status_to_db_string(&PipelineRunStatus::Initiated),
pipeline_status_to_db_string(&PipelineRunStatus::Started),
pipeline_status_to_db_string(&PipelineRunStatus::Completed),
pipeline_status_to_db_string(&PipelineRunStatus::Errored),
];
assert_eq!(expected, produced);
}
}