ora_client/
schedule_query.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
//! Query filters for schedules.

use ora_proto::server::v1::{self, ScheduleQueryOrder};
use uuid::Uuid;

use crate::IndexSet;

/// A filter for querying schedules.
#[derive(Debug, Clone, Default)]
#[must_use]
pub struct ScheduleFilter {
    /// The schedule IDs to filter by.
    pub schedule_ids: IndexSet<Uuid>,
    /// The job IDs to filter by.
    ///
    /// If the list is empty, all schedules are included.
    pub job_ids: IndexSet<Uuid>,
    /// The job type IDs to filter by.
    ///
    /// If the list is empty, all job types are included.
    pub job_type_ids: IndexSet<String>,
    /// A list of labels to filter by.
    ///
    /// If multiple filters are specified, all of them
    /// must match.
    pub labels: Vec<ScheduleLabelFilter>,
    /// Only include active or inactive schedules.
    ///
    /// If not provided, all schedules are included.
    pub active: Option<bool>,
}

impl ScheduleFilter {
    /// Create a new job filter that includes all schedules.
    pub fn new() -> Self {
        Self::default()
    }

    /// Filter by a specific job ID.
    pub fn with_job_id(mut self, job_id: Uuid) -> Self {
        self.job_ids.insert(job_id);
        self
    }

    /// Filter by specific job IDs.
    pub fn with_job_ids(mut self, job_ids: impl IntoIterator<Item = Uuid>) -> Self {
        self.job_ids.extend(job_ids);
        self
    }

    /// Filter by a specific job type ID.
    pub fn with_job_type_id(mut self, job_type_id: impl Into<String>) -> Self {
        self.job_type_ids.insert(job_type_id.into());
        self
    }

    /// Filter by specific job type IDs.
    pub fn with_job_type_ids(mut self, job_type_ids: impl IntoIterator<Item = String>) -> Self {
        self.job_type_ids.extend(job_type_ids);
        self
    }

    /// Filter by a specific schedule ID.
    pub fn with_schedule_id(mut self, schedule_id: Uuid) -> Self {
        self.schedule_ids.insert(schedule_id);
        self
    }

    /// Filter by specific schedule IDs.
    pub fn with_schedule_ids(mut self, schedule_ids: impl IntoIterator<Item = Uuid>) -> Self {
        self.schedule_ids.extend(schedule_ids);
        self
    }

    /// Filter by active status.
    pub fn active_only(mut self) -> Self {
        self.active = Some(true);
        self
    }

    /// Filter by inactive status.
    pub fn inactive_only(mut self) -> Self {
        self.active = Some(false);
        self
    }

    /// Filter by a label.
    pub fn with_label_value(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
        self.labels.push(ScheduleLabelFilter {
            key: key.into(),
            value: ScheduleLabelFilterValue::Equals(value.into()),
        });
        self
    }

    /// Filter by a label that must exist.
    pub fn with_label(mut self, key: impl Into<String>) -> Self {
        self.labels.push(ScheduleLabelFilter {
            key: key.into(),
            value: ScheduleLabelFilterValue::Exists,
        });
        self
    }
}

impl From<ScheduleFilter> for v1::ScheduleQueryFilter {
    fn from(filter: ScheduleFilter) -> Self {
        Self {
            job_ids: filter
                .job_ids
                .into_iter()
                .map(|id| id.to_string())
                .collect(),
            job_type_ids: filter
                .job_type_ids
                .into_iter()
                .map(|id| id.to_string())
                .collect(),

            schedule_ids: filter
                .schedule_ids
                .into_iter()
                .map(|id| id.to_string())
                .collect(),
            labels: filter.labels.into_iter().map(Into::into).collect(),
            active: filter.active,
        }
    }
}

/// A label filter for schedules.
#[derive(Debug, Clone)]
pub struct ScheduleLabelFilter {
    /// The key of the label.
    pub key: String,
    /// The condition for the label value.
    pub value: ScheduleLabelFilterValue,
}

/// The condition for a label filter.
#[derive(Debug, Clone)]
pub enum ScheduleLabelFilterValue {
    /// Any label value must exist with the key.
    Exists,
    /// The label value must be equal to the provided value.
    Equals(String),
}

impl From<ScheduleLabelFilter> for v1::ScheduleLabelFilter {
    fn from(filter: ScheduleLabelFilter) -> Self {
        Self {
            key: filter.key,
            value: match filter.value {
                ScheduleLabelFilterValue::Exists => Some(v1::schedule_label_filter::Value::Exists(
                    v1::LabelFilterExistCondition::Exists.into(),
                )),
                ScheduleLabelFilterValue::Equals(value) => {
                    Some(v1::schedule_label_filter::Value::Equals(value))
                }
            },
        }
    }
}

/// The order of schedules returned in a query.
#[derive(Debug, Default, Clone, Copy)]
pub enum ScheduleOrder {
    /// Order by the time the job was created in ascending order.
    CreatedAtAsc,
    /// Order by the time the job was created in descending order.
    #[default]
    CreatedAtDesc,
}

impl From<ScheduleOrder> for ScheduleQueryOrder {
    fn from(value: ScheduleOrder) -> Self {
        match value {
            ScheduleOrder::CreatedAtAsc => Self::CreatedAtAsc,
            ScheduleOrder::CreatedAtDesc => Self::CreatedAtDesc,
        }
    }
}