1use std::collections::HashMap;
4
5use chrono::{DateTime, Utc};
6use ironflow_store::models::{ConcurrencyLimit, Run, RunStatus, TriggerKind};
7use rust_decimal::Decimal;
8use serde::{Deserialize, Serialize};
9use serde_json::Value;
10use uuid::Uuid;
11
12use super::{CreatedBy, StepResponse};
13
14#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
25#[derive(Debug, Serialize, Deserialize)]
26pub struct RunResponse {
27 pub id: Uuid,
29 pub workflow_name: String,
31 pub status: RunStatus,
33 pub trigger: TriggerKind,
35 pub error: Option<String>,
37 pub retry_count: u32,
39 pub max_retries: u32,
41 #[cfg_attr(feature = "openapi", schema(value_type = f64))]
43 pub cost_usd: Decimal,
44 pub duration_ms: u64,
46 pub created_at: DateTime<Utc>,
48 pub updated_at: DateTime<Utc>,
50 pub started_at: Option<DateTime<Utc>>,
52 pub completed_at: Option<DateTime<Utc>>,
54 pub handler_version: Option<String>,
56 #[serde(default, skip_serializing_if = "HashMap::is_empty")]
58 pub labels: HashMap<String, String>,
59 #[serde(default, skip_serializing_if = "Option::is_none")]
61 pub scheduled_at: Option<DateTime<Utc>>,
62 pub created_by: CreatedBy,
64 #[serde(default, skip_serializing_if = "Option::is_none")]
66 pub idempotency_key: Option<String>,
67 #[serde(default, skip_serializing_if = "Option::is_none")]
70 pub concurrency_key: Option<String>,
71 #[serde(default, skip_serializing_if = "Vec::is_empty")]
74 pub concurrency_limits: Vec<ConcurrencyLimit>,
75 #[cfg_attr(feature = "openapi", schema(value_type = Option<f64>))]
77 #[serde(default, skip_serializing_if = "Option::is_none")]
78 pub max_cost_usd: Option<Decimal>,
79 #[serde(default, skip_serializing_if = "Option::is_none")]
84 pub output: Option<Value>,
85}
86
87impl From<Run> for RunResponse {
88 fn from(run: Run) -> Self {
89 let created_by = CreatedBy::from(&run);
90 RunResponse {
91 id: run.id,
92 workflow_name: run.workflow_name,
93 status: run.status.state,
94 trigger: run.trigger,
95 error: run.error,
96 retry_count: run.retry_count,
97 max_retries: run.max_retries,
98 cost_usd: run.cost_usd,
99 duration_ms: run.duration_ms,
100 created_at: run.created_at,
101 updated_at: run.updated_at,
102 started_at: run.started_at,
103 completed_at: run.completed_at,
104 handler_version: run.handler_version,
105 labels: run.labels,
106 scheduled_at: run.scheduled_at,
107 created_by,
108 idempotency_key: run.idempotency_key,
109 concurrency_key: run.concurrency_key,
110 concurrency_limits: run.concurrency_limits,
111 max_cost_usd: run.max_cost_usd,
112 output: run.output,
113 }
114 }
115}
116
117#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
119#[derive(Debug, Serialize)]
120pub struct RunDetailResponse {
121 pub run: RunResponse,
123 pub steps: Vec<StepResponse>,
125 pub payload: serde_json::Value,
127 pub active_descendant_count: u64,
130}
131
132#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
138#[derive(Debug, Serialize)]
139pub struct CancelRunResponse {
140 #[serde(flatten)]
142 pub run: RunResponse,
143 pub cancelled_descendants: Vec<Uuid>,
146}
147
148#[cfg_attr(feature = "openapi", derive(utoipa::IntoParams, utoipa::ToSchema))]
150#[derive(Debug, Deserialize)]
151pub struct ListRunsQuery {
152 pub workflow: Option<String>,
154 pub status: Option<RunStatus>,
156 pub has_steps: Option<bool>,
161 pub label: Option<String>,
163 pub created_by: Option<Uuid>,
167 pub concurrency_group: Option<String>,
169 pub page: Option<u32>,
171 pub per_page: Option<u32>,
173}
174
175impl ListRunsQuery {
176 pub fn parse_labels(&self) -> Option<HashMap<String, String>> {
178 parse_label_param(&self.label)
179 }
180}
181
182pub(crate) fn parse_label_param(raw: &Option<String>) -> Option<HashMap<String, String>> {
187 raw.as_ref().and_then(|raw| {
188 let mut map = HashMap::new();
189 for entry in raw.split(',') {
190 let entry = entry.trim();
191 if let Some((k, v)) = entry.split_once(':') {
192 map.insert(k.to_string(), v.to_string());
193 }
194 }
195 if map.is_empty() { None } else { Some(map) }
196 })
197}