Skip to main content

ironflow_api/entities/
stats.rs

1//! Statistics DTOs.
2
3use chrono::{DateTime, Utc};
4use rust_decimal::Decimal;
5use serde::{Deserialize, Serialize};
6
7use ironflow_store::entities::{HistoryGranularity, HistoryPeriod};
8
9/// Aggregate statistics response.
10///
11/// Computed from all runs in the store.
12///
13/// # Examples
14///
15/// ```
16/// use ironflow_api::entities::StatsResponse;
17/// ```
18#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
19#[derive(Debug, Serialize)]
20pub struct StatsResponse {
21    /// Total number of runs.
22    pub total_runs: u64,
23    /// Number of completed runs.
24    pub completed_runs: u64,
25    /// Number of failed runs.
26    pub failed_runs: u64,
27    /// Number of cancelled runs.
28    pub cancelled_runs: u64,
29    /// Number of pending or running runs.
30    pub active_runs: u64,
31    /// Success rate: completed / (completed + failed), as a percentage.
32    pub success_rate_percent: f64,
33    /// Aggregated cost across all runs in USD.
34    pub total_cost_usd: Decimal,
35    /// Aggregated duration across all runs in milliseconds.
36    pub total_duration_ms: u64,
37}
38
39/// Query parameters for `GET /api/v1/stats/history`.
40///
41/// # Examples
42///
43/// ```
44/// use ironflow_api::entities::StatsHistoryQuery;
45/// ```
46#[cfg_attr(feature = "openapi", derive(utoipa::IntoParams))]
47#[derive(Debug, Deserialize)]
48pub struct StatsHistoryQuery {
49    /// Filter by workflow name. Omit to aggregate all workflows.
50    pub workflow: Option<String>,
51    /// Time period to query. Defaults to `7d`.
52    pub period: Option<HistoryPeriod>,
53    /// Bucket granularity. Auto-derived from period when omitted.
54    pub granularity: Option<HistoryGranularity>,
55}
56
57/// Time-bucketed historical statistics response.
58///
59/// # Examples
60///
61/// ```
62/// use ironflow_api::entities::StatsHistoryResponse;
63/// ```
64#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
65#[derive(Debug, Serialize)]
66pub struct StatsHistoryResponse {
67    /// The period that was queried.
68    pub period: HistoryPeriod,
69    /// The granularity of each bucket.
70    pub granularity: HistoryGranularity,
71    /// Workflow name filter, if applied.
72    pub workflow: Option<String>,
73    /// Aggregated buckets, sorted by time ascending.
74    pub buckets: Vec<StatsHistoryBucketResponse>,
75}
76
77/// One time bucket in the history response.
78///
79/// # Examples
80///
81/// ```
82/// use ironflow_api::entities::StatsHistoryBucketResponse;
83/// ```
84#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
85#[derive(Debug, Serialize)]
86pub struct StatsHistoryBucketResponse {
87    /// Start of the time bucket.
88    pub time: DateTime<Utc>,
89    /// Number of completed runs in this bucket.
90    pub completed: u64,
91    /// Number of failed runs in this bucket.
92    pub failed: u64,
93    /// Number of cancelled runs in this bucket.
94    pub cancelled: u64,
95    /// Average duration in milliseconds.
96    pub avg_duration_ms: u64,
97    /// 95th percentile duration in milliseconds.
98    pub p95_duration_ms: u64,
99    /// Total cost in USD.
100    pub total_cost_usd: Decimal,
101}