use std::collections::HashMap;
use serde::{Deserialize, Serialize};
use serde_json::Value;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LogEntry {
pub id: String,
pub name: String,
pub run_type: String,
#[serde(default)]
pub tags: Vec<String>,
#[serde(default)]
pub metadata: HashMap<String, Value>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub start_time: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub end_time: Option<String>,
#[serde(default)]
pub streamed_output: Vec<Value>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub streamed_output_str: Option<Vec<String>>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub final_output: Option<Value>,
}
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct RunState {
pub id: String,
#[serde(default)]
pub streamed_output: Vec<Value>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub final_output: Option<Value>,
#[serde(default)]
pub logs: HashMap<String, LogEntry>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub name: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub run_type: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct JsonPatchOp {
pub op: String,
pub path: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub value: Option<Value>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RunLogPatch {
pub ops: Vec<JsonPatchOp>,
}
impl RunLogPatch {
pub fn new(ops: Vec<JsonPatchOp>) -> Self {
Self { ops }
}
pub fn add_log_entry(name: &str, entry: &LogEntry) -> Self {
Self::new(vec![JsonPatchOp {
op: "add".to_string(),
path: format!("/logs/{}", name),
value: Some(serde_json::to_value(entry).unwrap_or_default()),
}])
}
pub fn add_streamed_output(index: usize, value: Value) -> Self {
Self::new(vec![JsonPatchOp {
op: "add".to_string(),
path: format!("/streamed_output/{}", index),
value: Some(value),
}])
}
pub fn set_final_output(value: Value) -> Self {
Self::new(vec![JsonPatchOp {
op: "replace".to_string(),
path: "/final_output".to_string(),
value: Some(value),
}])
}
}