1use std::collections::{BTreeMap, HashMap};
8
9use jiff::Timestamp;
10use schedr_config::masked::mask_text;
11use schedr_db::backfills::BackfillRecord;
12use schedr_db::commands::CommandRecord;
13use schedr_db::dags::StoredDag;
14use schedr_db::reads::{AttemptRecord, InstanceRecord, RunRecord};
15use schedr_db::runs::Held;
16use schedr_db::workers::WorkerRecord;
17use schedr_model::{DagId, DagLocation, RunState};
18use serde::{Deserialize, Serialize};
19use utoipa::{IntoParams, ToSchema};
20
21mod form;
22#[cfg(test)]
23mod tests;
24
25pub use form::{ParamFieldView, ParamKind};
26
27#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, ToSchema)]
29pub struct ErrorBody {
30 pub error: String,
32}
33
34#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, ToSchema)]
36pub struct LocationView {
37 pub root: String,
39 pub path: String,
41}
42
43impl From<&DagLocation> for LocationView {
44 fn from(location: &DagLocation) -> Self {
45 Self {
46 root: location.root.clone(),
47 path: location.path.clone(),
48 }
49 }
50}
51
52#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, ToSchema)]
54#[serde(rename_all = "snake_case")]
55pub enum TriggerState {
56 Paused,
58 Active,
60}
61
62impl TriggerState {
63 #[must_use]
65 pub const fn shown(state: Option<Self>) -> &'static str {
66 match state {
67 Some(Self::Paused) => "paused",
68 Some(Self::Active) => "active",
69 None => "manual",
70 }
71 }
72}
73
74#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, ToSchema)]
76pub struct DagView {
77 pub dag_id: String,
79 pub trigger_state: Option<TriggerState>,
82 pub location: Option<LocationView>,
84 pub conflict: Option<Vec<LocationView>>,
86 #[schema(value_type = Option<Object>)]
88 pub trigger: Option<serde_json::Value>,
89 pub description: Option<String>,
91 pub tags: Vec<String>,
93 pub held: Option<HeldView>,
97 #[serde(default, skip_serializing_if = "Vec::is_empty")]
100 pub limits: Vec<LimitView>,
101 #[serde(default, skip_serializing_if = "Vec::is_empty")]
104 pub params: Vec<ParamFieldView>,
105 #[serde(default, skip_serializing_if = "Vec::is_empty")]
109 pub warnings: Vec<String>,
110}
111
112#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, ToSchema)]
115pub struct LimitView {
116 pub limit: String,
118 pub task_id: Option<String>,
120 pub value: u32,
122 pub running: u32,
125}
126
127impl LimitView {
128 #[must_use]
130 pub fn of(
131 dag: &schedr_model::Dag,
132 counts: &[(String, schedr_model::TaskId, u32)],
133 ) -> Vec<Self> {
134 let mut limits = Vec::new();
135 if let Some(value) = dag.max_active_tasks {
136 limits.push(Self {
137 limit: "max_active_tasks".to_owned(),
138 task_id: None,
139 value,
140 running: counts.iter().map(|c| c.2).sum(),
141 });
142 }
143 for task in &dag.tasks {
144 let of_task = || counts.iter().filter(|c| c.1 == task.id).map(|c| c.2);
145 if let Some(value) = task.settings.max_active {
146 limits.push(Self {
147 limit: "max_active".to_owned(),
148 task_id: Some(task.id.to_string()),
149 value,
150 running: of_task().sum(),
151 });
152 }
153 if let Some(value) = task.settings.max_active_per_run {
154 limits.push(Self {
155 limit: "max_active_per_run".to_owned(),
156 task_id: Some(task.id.to_string()),
157 value,
158 running: of_task().max().unwrap_or(0),
159 });
160 }
161 }
162 limits
163 }
164}
165
166#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, ToSchema)]
169pub struct HeldView {
170 pub reason: String,
173 pub path: String,
175 pub exception: String,
177 pub message: String,
179}
180
181impl DagView {
182 #[must_use]
185 pub fn of(dag: &StoredDag, held: &HashMap<(String, String), Held>) -> Self {
186 let current = dag.current.as_ref().map(|c| &c.dag);
187 Self {
188 dag_id: dag.dag_id.to_string(),
189 trigger_state: dag.paused.map(|paused| {
190 if paused {
191 TriggerState::Paused
192 } else {
193 TriggerState::Active
194 }
195 }),
196 location: current.map(|d| (&d.location).into()),
197 conflict: dag
198 .conflict
199 .as_ref()
200 .map(|places| places.iter().map(Into::into).collect()),
201 trigger: current.and_then(|d| serde_json::to_value(&d.trigger).ok()),
202 description: current.and_then(|d| d.description.clone()),
203 tags: current.map(|d| d.tags.clone()).unwrap_or_default(),
204 held: current.and_then(|d| Held::of(d, held)).map(|h| HeldView {
205 reason: h.reason(dag.dag_id.as_str()),
206 path: h.path,
207 exception: h.exception,
208 message: mask_text(&h.message),
209 }),
210 limits: Vec::new(),
211 params: Vec::new(),
212 warnings: Vec::new(),
213 }
214 }
215}
216
217#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, ToSchema)]
219pub struct DagList {
220 pub dags: Vec<DagView>,
222}
223
224#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, ToSchema)]
226pub struct RunView {
227 pub run_id: String,
229 pub dag_id: String,
231 pub kind: String,
233 pub state: String,
235 pub planned_for: Timestamp,
237 pub since: Timestamp,
239 pub triggered_at: Timestamp,
241 pub triggered_by: String,
243 pub started_at: Option<Timestamp>,
245 pub ended_at: Option<Timestamp>,
247 pub late_failure: Option<String>,
250 #[serde(default)]
253 pub parent_run_id: Option<String>,
254 #[serde(default)]
256 pub backfill: Option<RunBackfillView>,
257}
258
259#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, ToSchema)]
261pub struct RunBackfillView {
262 pub backfill_id: String,
264 pub index: u32,
266 pub total: u32,
268}
269
270impl RunView {
271 #[must_use]
274 pub fn kind_label(&self) -> String {
275 match &self.backfill {
276 Some(place) => format!("{} {}/{}", self.kind, place.index, place.total),
277 None => self.kind.clone(),
278 }
279 }
280}
281
282impl From<&RunRecord> for RunView {
283 fn from(run: &RunRecord) -> Self {
284 Self {
285 run_id: run.id.clone(),
286 dag_id: run.dag_id.to_string(),
287 kind: run.kind.to_string(),
288 state: run.state.to_string(),
289 planned_for: run.planned_for,
290 since: run.since,
291 triggered_at: run.triggered_at,
292 triggered_by: run.triggered_by.clone(),
293 started_at: run.started_at,
294 ended_at: run.ended_at,
295 late_failure: run.late_failure.clone(),
296 parent_run_id: run.parent_run_id.clone(),
297 backfill: run.backfill.as_ref().map(|b| RunBackfillView {
298 backfill_id: b.id.clone(),
299 index: b.index,
300 total: b.total,
301 }),
302 }
303 }
304}
305
306#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, ToSchema)]
308pub struct RunList {
309 pub runs: Vec<RunView>,
312}
313
314#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, ToSchema)]
316pub struct TaskView {
317 pub task_id: String,
319 #[serde(default = "unmapped")]
322 pub map_index: i32,
323 #[serde(default)]
326 pub instances: Option<usize>,
327 pub state: String,
329 pub outcome: Option<String>,
331 pub try_number: u32,
333 pub reason: Option<String>,
336 #[serde(default)]
339 pub cleanup: Option<String>,
340 pub updated_at: Timestamp,
342}
343
344const fn unmapped() -> i32 {
346 -1
347}
348
349impl TaskView {
350 #[must_use]
352 pub fn of(instance: &InstanceRecord, instances: Option<usize>) -> Self {
353 Self {
354 task_id: instance.task_id.to_string(),
355 map_index: instance.map_index,
356 instances,
357 state: instance.state.to_string(),
358 outcome: instance.outcome.clone(),
359 try_number: instance.try_number,
360 reason: instance
361 .reason
362 .clone()
363 .or_else(|| instance.queued_reason.clone()),
364 cleanup: instance.cleanup_result.map(|r| r.to_string()),
365 updated_at: instance.updated_at,
366 }
367 }
368}
369
370#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize, IntoParams)]
372#[serde(deny_unknown_fields)]
373#[into_params(parameter_in = Query)]
374pub struct InstanceQuery {
375 #[serde(default)]
378 pub map_index: Option<i32>,
379}
380
381#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, ToSchema)]
383pub struct RunDetail {
384 pub run: RunView,
386 pub tasks: Vec<TaskView>,
388 pub code_hashes: Vec<String>,
391 #[serde(default)]
394 #[schema(value_type = Object)]
395 pub params: serde_json::Value,
396 #[serde(default)]
398 pub resources: Vec<ResourceView>,
399}
400
401#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, ToSchema)]
403pub struct ResourceList {
404 pub resources: Vec<ResourceView>,
406}
407
408#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, ToSchema)]
410pub struct SensorView {
411 pub dag_id: String,
413 pub sensor: String,
415 pub strikes: u32,
417 pub switched_off_at: String,
419}
420
421#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, ToSchema)]
423pub struct SensorList {
424 pub sensors: Vec<SensorView>,
426}
427
428#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, ToSchema)]
430pub struct ResourceView {
431 pub setup: String,
433 pub cleanup: String,
435 pub users: Vec<String>,
437 pub blocking: bool,
439 pub state: String,
442}
443
444#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, ToSchema)]
446pub struct AttemptView {
447 pub attempt_id: String,
449 pub try_number: u32,
451 pub worker_id: Option<String>,
453 pub status: Option<String>,
455 pub exit_code: Option<i32>,
457 pub error_kind: Option<String>,
459 #[schema(value_type = Option<Object>)]
461 pub error_summary: Option<serde_json::Value>,
462 pub assigned_at: Timestamp,
464 pub started_at: Option<Timestamp>,
466 pub ended_at: Option<Timestamp>,
468 pub duration_ms: Option<u64>,
471 pub peak_memory_bytes: Option<u64>,
474 pub start_method: Option<String>,
478 pub usage: Option<UsageView>,
482 pub core_dumped: bool,
484 #[serde(default)]
486 pub cleanup: Option<CleanupView>,
487 #[serde(default)]
489 pub chained_run_id: Option<String>,
490}
491
492#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, ToSchema)]
494pub struct CleanupView {
495 pub result: Option<String>,
497 pub tries: Option<u32>,
499 pub duration_ms: Option<u64>,
501 pub error_kind: Option<String>,
503 #[schema(value_type = Option<Object>)]
505 pub error_summary: Option<serde_json::Value>,
506 pub moves: u32,
508 #[schema(value_type = Vec<Object>)]
511 pub handles: Vec<serde_json::Value>,
512}
513
514#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, ToSchema)]
516pub struct UsageView {
517 pub user_cpu_micros: u64,
519 pub system_cpu_micros: u64,
521 pub peak_rss_bytes: Option<u64>,
525 pub major_faults: u64,
527 pub voluntary_context_switches: Option<u64>,
530 pub involuntary_context_switches: Option<u64>,
532}
533
534impl From<&AttemptRecord> for AttemptView {
535 fn from(attempt: &AttemptRecord) -> Self {
536 Self {
537 attempt_id: attempt.id.clone(),
538 try_number: attempt.try_number,
539 worker_id: attempt.worker_id.clone(),
540 status: attempt.status.map(|s| s.to_string()),
541 exit_code: attempt.exit_code,
542 error_kind: attempt.error_kind.clone(),
543 error_summary: attempt
544 .error_summary
545 .as_deref()
546 .and_then(|s| serde_json::from_str(s).ok())
547 .map(|mut summary| {
548 schedr_config::masked::mask(&mut summary);
549 summary
550 }),
551 assigned_at: attempt.assigned_at,
552 started_at: attempt.started_at,
553 ended_at: attempt.ended_at,
554 duration_ms: attempt.duration_ms,
555 peak_memory_bytes: attempt.peak_memory_bytes,
556 start_method: attempt.start_method.clone(),
557 usage: attempt.usage.map(|u| UsageView {
558 user_cpu_micros: u.user_cpu_micros,
559 system_cpu_micros: u.system_cpu_micros,
560 peak_rss_bytes: u.peak_rss_bytes,
561 major_faults: u.major_faults,
562 voluntary_context_switches: u.voluntary_context_switches,
563 involuntary_context_switches: u.involuntary_context_switches,
564 }),
565 core_dumped: attempt.core_dumped,
566 cleanup: (!attempt.cleanup.is_empty()).then(|| CleanupView {
567 result: attempt.cleanup.result.map(|r| r.to_string()),
568 tries: attempt.cleanup.tries,
569 duration_ms: attempt.cleanup.duration_ms,
570 error_kind: attempt.cleanup.error_kind.clone(),
571 error_summary: attempt
572 .cleanup
573 .error_summary
574 .as_deref()
575 .and_then(|s| serde_json::from_str(s).ok())
576 .map(|mut summary| {
577 schedr_config::masked::mask(&mut summary);
578 summary
579 }),
580 moves: attempt.cleanup.moves,
581 handles: attempt
582 .cleanup
583 .handles
584 .iter()
585 .map(|h| shown_handle(h))
586 .collect(),
587 }),
588 chained_run_id: attempt.chained_run_id.clone(),
589 }
590 }
591}
592
593fn shown_handle(stored: &str) -> serde_json::Value {
597 let mut value = serde_json::from_str(stored)
598 .unwrap_or_else(|_| serde_json::json!({ "unreadable": mask_text(stored) }));
599 schedr_config::masked::mask_data(&mut value);
600 value
601}
602
603#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, ToSchema)]
605pub struct AttemptList {
606 pub attempts: Vec<AttemptView>,
608}
609
610#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, ToSchema)]
612pub struct CommandView {
613 pub command_id: String,
615 pub kind: String,
617 pub status: String,
619 pub reason: Option<String>,
621 #[schema(value_type = Option<Object>)]
623 pub result: Option<serde_json::Value>,
624 pub requested_by: String,
626 pub requested_at: Timestamp,
628 pub decided_at: Option<Timestamp>,
630}
631
632impl From<&CommandRecord> for CommandView {
633 fn from(command: &CommandRecord) -> Self {
634 Self {
635 command_id: command.id.clone(),
636 kind: command.kind.clone(),
637 status: serde_json::to_value(command.status)
638 .ok()
639 .and_then(|v| v.as_str().map(str::to_owned))
640 .unwrap_or_default(),
641 reason: command
643 .reason
644 .as_deref()
645 .map(schedr_config::masked::mask_text),
646 result: command.result.clone(),
647 requested_by: command.requested_by.clone(),
648 requested_at: command.requested_at,
649 decided_at: command.decided_at,
650 }
651 }
652}
653
654#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize, ToSchema)]
656#[serde(deny_unknown_fields)]
657pub struct TriggerRequest {
658 #[serde(default)]
660 pub planned_for: Option<Timestamp>,
661 #[serde(default, skip_serializing_if = "Option::is_none")]
664 #[schema(value_type = Option<Object>)]
665 pub params: Option<serde_json::Map<String, serde_json::Value>>,
666 #[serde(default, skip_serializing_if = "Option::is_none")]
668 pub event: Option<String>,
669 #[serde(default, skip_serializing_if = "Option::is_none")]
671 pub event_key: Option<String>,
672 #[serde(default, skip_serializing_if = "Option::is_none")]
674 pub event_data: Option<serde_json::Value>,
675 #[serde(default, skip_serializing_if = "Option::is_none")]
677 pub event_at: Option<Timestamp>,
678}
679
680#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, ToSchema)]
683#[serde(deny_unknown_fields)]
684pub struct BackfillRequest {
685 pub from: Timestamp,
687 #[serde(default, skip_serializing_if = "Option::is_none")]
689 pub to: Option<Timestamp>,
690 #[serde(default)]
692 pub all: bool,
693 #[serde(default, skip_serializing_if = "Option::is_none")]
695 #[schema(value_type = Option<Object>)]
696 pub params: Option<serde_json::Map<String, serde_json::Value>>,
697 #[serde(default)]
699 pub dry_run: bool,
700}
701
702#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize, ToSchema)]
704pub struct BackfillRunCounts {
705 pub queued: u32,
707 pub running: u32,
709 pub success: u32,
711 pub failed: u32,
713 pub cancelled: u32,
715}
716
717#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, ToSchema)]
719pub struct BackfillView {
720 pub backfill_id: String,
722 pub dag_id: String,
724 pub from: Timestamp,
726 pub to: Timestamp,
728 pub all: bool,
730 pub requested_by: String,
732 pub created_at: Timestamp,
734 pub fire_times: u32,
736 pub created_runs: u32,
738 pub runs: BackfillRunCounts,
740 pub unfinished: bool,
742}
743
744impl From<&BackfillRecord> for BackfillView {
745 fn from(backfill: &BackfillRecord) -> Self {
746 let mut runs = BackfillRunCounts::default();
747 for (state, count) in &backfill.runs {
748 let slot = match state {
749 RunState::Queued => &mut runs.queued,
750 RunState::Running => &mut runs.running,
751 RunState::Success => &mut runs.success,
752 RunState::Failed => &mut runs.failed,
753 RunState::Cancelled => &mut runs.cancelled,
754 };
755 *slot += count;
756 }
757 Self {
758 backfill_id: backfill.id.clone(),
759 dag_id: backfill.dag_id.to_string(),
760 from: backfill.range_from,
761 to: backfill.range_to,
762 all: backfill.all_fire_times,
763 requested_by: backfill.requested_by.clone(),
764 created_at: backfill.created_at,
765 fire_times: backfill.fire_times,
766 created_runs: backfill.created_runs,
767 runs,
768 unfinished: backfill.unfinished(),
769 }
770 }
771}
772
773#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, ToSchema)]
775pub struct BackfillList {
776 pub backfills: Vec<BackfillView>,
778}
779
780#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, ToSchema)]
782pub struct TriggerView {
783 pub dag_id: String,
785 pub trigger: String,
787 pub state: String,
790 pub worker: Option<String>,
792 pub bookmark: Option<String>,
794 pub stopped_why: Option<String>,
796 pub failures: u32,
798 pub last_error: Option<String>,
800 pub not_before: Option<Timestamp>,
802 pub started_at: Option<Timestamp>,
804 pub last_event_at: Option<Timestamp>,
806 pub last_poked_at: Option<Timestamp>,
808 pub last_restart_at: Option<Timestamp>,
810 #[serde(default)]
813 pub stuck: bool,
814 #[serde(default, skip_serializing_if = "Option::is_none")]
817 pub called_for: Option<String>,
818}
819
820#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, ToSchema)]
822pub struct TriggerList {
823 pub triggers: Vec<TriggerView>,
825}
826
827#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, ToSchema)]
829pub struct EventView {
830 pub key: String,
832 pub data: serde_json::Value,
834 pub at: Option<Timestamp>,
836 pub received_at: Timestamp,
838}
839
840#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, ToSchema)]
842#[serde(deny_unknown_fields)]
843pub struct PostEventRequest {
844 pub name: String,
846 pub key: String,
848 #[serde(default)]
850 pub data: serde_json::Value,
851 #[serde(default)]
853 pub at: Option<Timestamp>,
854}
855
856#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, ToSchema)]
858pub struct NamedEventView {
859 pub id: String,
861 pub name: String,
863 pub key: String,
865 pub data: serde_json::Value,
867 pub at: Timestamp,
869 pub posted_at: Timestamp,
871 pub posted_by: String,
873}
874
875#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, ToSchema)]
877pub struct NamedEventList {
878 pub events: Vec<NamedEventView>,
880}
881
882#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, ToSchema)]
884pub struct EventList {
885 pub events: Vec<EventView>,
887}
888
889#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize, ToSchema)]
891#[serde(deny_unknown_fields)]
892pub struct CheckConnectionsRequest {
893 #[serde(default, skip_serializing_if = "Option::is_none")]
895 pub root: Option<String>,
896 #[serde(default, skip_serializing_if = "Option::is_none")]
898 pub name: Option<String>,
899 #[serde(default, skip_serializing_if = "Option::is_none")]
901 pub worker: Option<String>,
902}
903
904#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize, ToSchema)]
906#[serde(deny_unknown_fields)]
907pub struct TestConnectionRequest {
908 #[serde(default, skip_serializing_if = "Option::is_none")]
910 pub root: Option<String>,
911 #[serde(default, skip_serializing_if = "Option::is_none")]
913 pub worker: Option<String>,
914 #[serde(default, skip_serializing_if = "Option::is_none")]
916 pub timeout_ms: Option<u64>,
917}
918
919#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize, ToSchema)]
921#[serde(deny_unknown_fields)]
922pub struct PauseRequest {
923 #[serde(default)]
925 pub cancel_runs: bool,
926}
927
928#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize, IntoParams)]
930#[serde(deny_unknown_fields)]
931#[into_params(parameter_in = Query)]
932pub struct RunsQuery {
933 #[serde(default, rename = "for")]
935 #[param(rename = "for", value_type = Option<String>, format = DateTime)]
936 pub planned_for: Option<Timestamp>,
937 #[serde(default)]
939 pub limit: Option<u32>,
940}
941
942#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize, IntoParams)]
944#[serde(deny_unknown_fields)]
945#[into_params(parameter_in = Query)]
946pub struct AllRunsQuery {
947 #[serde(default)]
949 #[param(value_type = Option<String>)]
950 pub dag: Option<DagId>,
951 #[serde(default)]
954 pub dag_contains: Option<String>,
955 #[serde(default)]
957 #[param(value_type = Option<String>)]
958 pub state: Option<RunState>,
959 #[serde(default)]
962 #[param(value_type = Option<String>, format = DateTime)]
963 pub before: Option<Timestamp>,
964 #[serde(default)]
967 pub before_run: Option<String>,
968 #[serde(default)]
970 pub limit: Option<u32>,
971}
972
973#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, ToSchema)]
975pub struct OutputView {
976 pub outcome: String,
978 pub value: serde_json::Value,
981}
982
983#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, ToSchema)]
985pub struct CommandList {
986 pub commands: Vec<CommandView>,
988}
989
990#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize, IntoParams)]
992#[serde(deny_unknown_fields)]
993#[into_params(parameter_in = Query)]
994pub struct LogQuery {
995 #[serde(default)]
997 pub from: Option<u64>,
998 #[serde(default)]
1000 pub limit: Option<u64>,
1001 #[serde(default)]
1003 pub map_index: Option<i32>,
1004}
1005
1006#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, ToSchema)]
1008pub struct LogChunk {
1009 pub attempt_id: String,
1011 pub try_number: u32,
1013 pub worker_id: String,
1015 pub from: u64,
1018 pub next: u64,
1021 pub size: u64,
1023 pub running: bool,
1025 pub content: String,
1031}
1032
1033#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, ToSchema)]
1035pub struct WorkerView {
1036 pub worker_id: String,
1038 pub state: String,
1040 pub incarnation_id: String,
1042 pub queues: Vec<String>,
1044 pub slots: u32,
1046 pub running: u32,
1048 pub schedr_version: String,
1050 pub protocol_version: String,
1052 pub log_endpoint: String,
1054 pub registered_at: Timestamp,
1056 pub last_heartbeat_at: Timestamp,
1058 pub offline_at: Option<Timestamp>,
1060 pub drained: bool,
1062 #[serde(default)]
1064 pub warnings: Vec<WarningView>,
1065 #[serde(default, skip_serializing_if = "Option::is_none")]
1068 pub master_id: Option<String>,
1069 #[serde(default)]
1071 pub watcher_slots: u32,
1072 #[serde(default)]
1075 pub watcher_hosts: Vec<WatcherHostView>,
1076 #[serde(default, skip_serializing_if = "Option::is_none")]
1079 pub memory: Option<WorkerMemoryView>,
1080}
1081
1082#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, ToSchema)]
1084pub struct WorkerMemoryView {
1085 pub whole_bytes: u64,
1088 pub tasks_bytes: u64,
1090 #[serde(default, skip_serializing_if = "Option::is_none")]
1092 pub container_bytes: Option<u64>,
1093 #[serde(default, skip_serializing_if = "Option::is_none")]
1095 pub container_limit_bytes: Option<u64>,
1096}
1097
1098impl From<&schedr_model::usage::WorkerUsage> for WorkerMemoryView {
1099 fn from(usage: &schedr_model::usage::WorkerUsage) -> Self {
1100 Self {
1101 whole_bytes: usage
1102 .process
1103 .resident_bytes
1104 .saturating_add(usage.fork_servers_bytes)
1105 .saturating_add(usage.watcher_hosts_bytes)
1106 .saturating_add(usage.tasks_bytes),
1107 tasks_bytes: usage.tasks_bytes,
1108 container_bytes: usage.process.container_bytes,
1109 container_limit_bytes: usage.process.container_limit_bytes,
1110 }
1111 }
1112}
1113
1114#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, ToSchema)]
1116pub struct WatcherHostView {
1117 pub code: String,
1119 pub wait_steps: u32,
1121 pub threads: u32,
1123 pub tied_threads: u32,
1126 pub memory_bytes: u64,
1128 pub hosts_lost: u64,
1130}
1131
1132#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, ToSchema)]
1134pub struct WarningView {
1135 pub kind: String,
1137 pub interpreter: String,
1139 pub message: String,
1141}
1142
1143impl From<&WorkerRecord> for WorkerView {
1144 fn from(worker: &WorkerRecord) -> Self {
1145 Self {
1146 worker_id: worker.id.clone(),
1147 state: worker.state.to_string(),
1148 incarnation_id: worker.incarnation.clone(),
1149 queues: worker.queues.clone(),
1150 slots: worker.slots,
1151 running: worker.running,
1152 schedr_version: worker.schedr_version.clone(),
1153 protocol_version: worker.protocol_version.clone(),
1154 log_endpoint: worker.log_endpoint.clone(),
1155 registered_at: worker.registered_at,
1156 last_heartbeat_at: worker.last_heartbeat_at,
1157 offline_at: worker.offline_at,
1158 drained: worker.drained,
1159 warnings: worker
1160 .warnings
1161 .iter()
1162 .map(|w| WarningView {
1163 kind: w.kind.clone(),
1164 interpreter: w.interpreter.clone(),
1165 message: mask_text(&w.message),
1166 })
1167 .collect(),
1168 master_id: None,
1169 memory: None,
1170 watcher_slots: worker.watcher_slots,
1171 watcher_hosts: worker
1172 .watcher_hosts
1173 .iter()
1174 .map(|host| WatcherHostView {
1175 code: host.code.clone(),
1176 wait_steps: host.wait_steps,
1177 threads: host.threads,
1178 tied_threads: host.tied_threads,
1179 memory_bytes: host.memory_bytes,
1180 hosts_lost: host.hosts_lost,
1181 })
1182 .collect(),
1183 }
1184 }
1185}
1186
1187#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, ToSchema)]
1191pub struct DeploymentView {
1192 pub webserver: NodeView,
1194 pub masters: Vec<MasterView>,
1196 pub workers: Vec<WorkerNodeView>,
1198 pub user: String,
1201}
1202
1203#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, ToSchema)]
1205pub struct NodeView {
1206 pub process_id: String,
1208 pub host: String,
1210 pub schedr_version: String,
1212 #[schema(value_type = Object)]
1214 pub config: serde_json::Value,
1215 #[serde(default, skip_serializing_if = "Option::is_none")]
1218 pub usage: Option<NodeUsageView>,
1219}
1220
1221#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, ToSchema)]
1223pub struct NodeUsageView {
1224 pub resident_bytes: u64,
1226 pub cpu_seconds: f64,
1228 #[serde(default, skip_serializing_if = "Option::is_none")]
1230 pub container_bytes: Option<u64>,
1231 #[serde(default, skip_serializing_if = "Option::is_none")]
1233 pub container_limit_bytes: Option<u64>,
1234}
1235
1236impl From<schedr_model::usage::NodeUsage> for NodeUsageView {
1237 fn from(usage: schedr_model::usage::NodeUsage) -> Self {
1238 Self {
1239 resident_bytes: usage.resident_bytes,
1240 cpu_seconds: usage.cpu_seconds,
1241 container_bytes: usage.container_bytes,
1242 container_limit_bytes: usage.container_limit_bytes,
1243 }
1244 }
1245}
1246
1247#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, ToSchema)]
1249pub struct WorkerUsageView {
1250 pub process: NodeUsageView,
1252 pub fork_servers_bytes: u64,
1254 pub watcher_hosts_bytes: u64,
1256 pub tasks_bytes: u64,
1258 #[serde(default)]
1260 pub largest_tasks: Vec<TaskMemoryView>,
1261}
1262
1263#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, ToSchema)]
1265pub struct TaskMemoryView {
1266 pub attempt_id: String,
1268 pub dag_id: String,
1270 pub run_id: String,
1272 pub task_id: String,
1274 #[serde(default, skip_serializing_if = "Option::is_none")]
1276 pub map_index: Option<u32>,
1277 pub bytes: u64,
1279}
1280
1281impl From<schedr_model::usage::WorkerUsage> for WorkerUsageView {
1282 fn from(usage: schedr_model::usage::WorkerUsage) -> Self {
1283 Self {
1284 process: usage.process.into(),
1285 fork_servers_bytes: usage.fork_servers_bytes,
1286 watcher_hosts_bytes: usage.watcher_hosts_bytes,
1287 tasks_bytes: usage.tasks_bytes,
1288 largest_tasks: usage
1289 .largest_tasks
1290 .into_iter()
1291 .map(TaskMemoryView::from)
1292 .collect(),
1293 }
1294 }
1295}
1296
1297impl From<schedr_model::usage::TaskMemory> for TaskMemoryView {
1298 fn from(t: schedr_model::usage::TaskMemory) -> Self {
1299 Self {
1300 attempt_id: t.attempt_id,
1301 dag_id: t.dag_id,
1302 run_id: t.run_id,
1303 task_id: t.task_id,
1304 map_index: t.map_index,
1305 bytes: t.bytes,
1306 }
1307 }
1308}
1309
1310#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, ToSchema)]
1313pub struct SampleSeriesList {
1314 pub range: String,
1316 pub step_s: u32,
1318 pub from: Timestamp,
1320 pub to: Timestamp,
1322 pub processes: Vec<ProcessSamplesView>,
1324 #[serde(default)]
1328 pub coverage: Vec<CoverageView>,
1329}
1330
1331#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, ToSchema)]
1333pub struct CoverageView {
1334 pub at: Timestamp,
1336 pub minutes: u32,
1338}
1339
1340#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, ToSchema)]
1342pub struct ProcessSamplesView {
1343 pub process_id: String,
1345 pub roles: BTreeMap<String, String>,
1348 pub points: Vec<SamplePointView>,
1350 #[serde(default, skip_serializing_if = "Option::is_none")]
1353 pub container_limit: Option<u64>,
1354 #[serde(default, skip_serializing_if = "Option::is_none")]
1357 pub parts: Option<WorkerPartsView>,
1358 #[serde(default)]
1361 pub ended: bool,
1362}
1363
1364#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, ToSchema)]
1366pub struct SamplePointView {
1367 pub at: Timestamp,
1369 pub resident_avg: u64,
1371 pub resident_peak: u64,
1373 pub memory_avg: u64,
1376 pub memory_peak: u64,
1378 pub minutes: u32,
1381 #[serde(default, skip_serializing_if = "Option::is_none")]
1383 pub cpu_avg: Option<f64>,
1384 #[serde(default, skip_serializing_if = "Option::is_none")]
1386 pub cpu_peak: Option<f64>,
1387 #[serde(default, skip_serializing_if = "Option::is_none")]
1389 pub container_avg: Option<u64>,
1390 #[serde(default, skip_serializing_if = "Option::is_none")]
1392 pub container_peak: Option<u64>,
1393 #[serde(default, skip_serializing_if = "Option::is_none")]
1395 pub container_limit: Option<u64>,
1396 #[serde(default, skip_serializing_if = "Option::is_none")]
1398 pub parts: Option<PartsPointView>,
1399}
1400
1401#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, ToSchema)]
1403pub struct PartsPointView {
1404 pub fork_servers_bytes: u64,
1406 pub watcher_hosts_bytes: u64,
1408 pub tasks_bytes: u64,
1410}
1411
1412#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, ToSchema)]
1414pub struct WorkerPartsView {
1415 pub at: Timestamp,
1417 pub fork_servers_bytes: u64,
1419 pub watcher_hosts_bytes: u64,
1421 pub tasks_bytes: u64,
1423 #[serde(default)]
1425 pub largest_tasks: Vec<TaskMemoryView>,
1426}
1427
1428#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, ToSchema)]
1430pub struct MemoryTotals {
1431 pub step_s: u32,
1433 pub from: Timestamp,
1435 pub to: Timestamp,
1437 pub points: Vec<MemoryPointView>,
1440}
1441
1442#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, ToSchema)]
1444pub struct MemoryPointView {
1445 pub at: Timestamp,
1447 pub resident: u64,
1449}
1450
1451#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, ToSchema)]
1454pub struct ProblemView {
1455 pub error: bool,
1457 pub message: String,
1459}
1460
1461impl From<schedr_db::nodes::NodeProblem> for ProblemView {
1462 fn from(problem: schedr_db::nodes::NodeProblem) -> Self {
1463 Self {
1464 error: problem.error,
1465 message: mask_text(&problem.message),
1466 }
1467 }
1468}
1469
1470#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, ToSchema)]
1472pub struct MasterView {
1473 pub master_id: String,
1475 pub role: String,
1477 #[serde(flatten)]
1479 pub node: NodeView,
1480 pub started_at: Timestamp,
1482 pub heard_at: Timestamp,
1484 #[serde(default)]
1487 pub problems: Vec<ProblemView>,
1488}
1489
1490#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, ToSchema)]
1492pub struct WorkerNodeView {
1493 pub worker_id: String,
1495 pub state: String,
1497 pub schedr_version: String,
1499 pub master_id: Option<String>,
1501 pub process_id: Option<String>,
1503 #[schema(value_type = Option<Object>)]
1505 pub config: Option<serde_json::Value>,
1506 #[serde(default)]
1508 pub queues: Vec<String>,
1509 #[serde(default)]
1511 pub slots: u32,
1512 #[serde(default)]
1514 pub running: u32,
1515 #[serde(default, skip_serializing_if = "Option::is_none")]
1517 pub usage: Option<WorkerUsageView>,
1518 #[serde(default)]
1521 pub problems: Vec<ProblemView>,
1522}
1523
1524impl From<schedr_db::nodes::MasterRecord> for MasterView {
1525 fn from(master: schedr_db::nodes::MasterRecord) -> Self {
1526 Self {
1527 master_id: master.master_id,
1528 role: if master.leading { "leader" } else { "standby" }.to_owned(),
1529 node: NodeView {
1530 process_id: master.process_id,
1531 host: master.host,
1532 schedr_version: master.schedr_version,
1533 config: master.config,
1534 usage: master.usage.map(NodeUsageView::from),
1535 },
1536 started_at: master.started_at,
1537 heard_at: master.heard_at,
1538 problems: master.problems.into_iter().map(ProblemView::from).collect(),
1539 }
1540 }
1541}
1542
1543#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, ToSchema)]
1545pub struct ImportErrorView {
1546 pub root: String,
1548 pub path: String,
1550 pub content_hash: String,
1552 pub parsed_at: Timestamp,
1554 pub worker_id: String,
1556 #[serde(default, skip_serializing_if = "Option::is_none")]
1558 pub master_id: Option<String>,
1559 pub schedr_version: String,
1561 pub exception: String,
1564 pub message: String,
1566 pub traceback: String,
1568}
1569
1570impl From<&schedr_db::dags::ImportErrorRecord> for ImportErrorView {
1571 fn from(record: &schedr_db::dags::ImportErrorRecord) -> Self {
1572 Self {
1573 root: record.root.clone(),
1574 path: record.path.clone(),
1575 content_hash: record.content_hash.clone(),
1576 parsed_at: record.parsed_at,
1577 worker_id: record.worker_id.clone(),
1578 master_id: record.master_id.clone(),
1579 schedr_version: record.schedr_version.clone(),
1580 exception: record.error.exception.clone(),
1581 message: mask_text(&record.error.message),
1582 traceback: mask_text(&record.error.traceback),
1583 }
1584 }
1585}
1586
1587#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, ToSchema)]
1589pub struct ImportErrorList {
1590 pub errors: Vec<ImportErrorView>,
1592}
1593
1594#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize, ToSchema)]
1597pub struct ConnectionList {
1598 pub roots: Vec<RootConnections>,
1600}
1601
1602#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, ToSchema)]
1604pub struct RootConnections {
1605 pub root: String,
1607 pub connections: Vec<ConnectionInfo>,
1609}
1610
1611#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, ToSchema)]
1613pub struct ConnectionInfo {
1614 pub name: String,
1616 #[serde(rename = "type", default, skip_serializing_if = "Option::is_none")]
1619 pub kind: Option<String>,
1620 #[serde(default, skip_serializing_if = "Option::is_none")]
1622 pub file: Option<String>,
1623 #[serde(default, skip_serializing_if = "Option::is_none")]
1625 pub refused: Option<String>,
1626 #[serde(default)]
1628 pub used_by: Vec<ConnectionUse>,
1629}
1630
1631#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, ToSchema)]
1633pub struct ConnectionUse {
1634 pub dag_id: String,
1636 pub tasks: Vec<String>,
1638}
1639
1640impl ConnectionList {
1641 #[must_use]
1644 pub fn of(
1645 registries: &std::collections::BTreeMap<String, schedr_model::connection::Registry>,
1646 dags: &[StoredDag],
1647 ) -> Self {
1648 use std::collections::BTreeMap;
1649 let mut uses: BTreeMap<&str, BTreeMap<&str, BTreeMap<String, Vec<String>>>> =
1651 BTreeMap::new();
1652 for dag in dags.iter().filter(|d| !d.removed) {
1653 let Some(current) = &dag.current else {
1654 continue;
1655 };
1656 let root = current.dag.location.root.as_str();
1657 for task in ¤t.dag.tasks {
1658 for name in &task.connections {
1659 uses.entry(root)
1660 .or_default()
1661 .entry(name.as_str())
1662 .or_default()
1663 .entry(dag.dag_id.to_string())
1664 .or_default()
1665 .push(task.id.to_string());
1666 }
1667 }
1668 }
1669 let empty = schedr_model::connection::Registry::default();
1670 let mut roots: Vec<&str> = registries.keys().map(String::as_str).collect();
1671 roots.extend(uses.keys().filter(|r| !registries.contains_key(**r)));
1672 roots.sort_unstable();
1673 roots.dedup();
1674 let mut list = Self::default();
1675 for root in roots {
1676 let registry = registries.get(root).unwrap_or(&empty);
1677 let declared = uses.get(root);
1678 let mut names: Vec<&str> = registry.names().map(String::as_str).collect();
1679 names.extend(declared.into_iter().flat_map(|d| d.keys().copied()));
1680 names.sort_unstable();
1681 names.dedup();
1682 if names.is_empty() {
1683 continue;
1684 }
1685 let connections = names
1686 .into_iter()
1687 .map(|name| {
1688 let found = registry.get(name);
1689 ConnectionInfo {
1690 name: name.to_owned(),
1691 kind: found
1692 .as_ref()
1693 .ok()
1694 .and_then(|def| def.kind)
1695 .map(connection_type),
1696 file: registry.files.get(name).cloned(),
1697 refused: found.err(),
1698 used_by: declared
1699 .and_then(|d| d.get(name))
1700 .map(|by_dag| {
1701 by_dag
1702 .iter()
1703 .map(|(dag_id, tasks)| ConnectionUse {
1704 dag_id: dag_id.clone(),
1705 tasks: tasks.clone(),
1706 })
1707 .collect()
1708 })
1709 .unwrap_or_default(),
1710 }
1711 })
1712 .collect();
1713 list.roots.push(RootConnections {
1714 root: root.to_owned(),
1715 connections,
1716 });
1717 }
1718 list
1719 }
1720}
1721
1722fn connection_type(kind: schedr_model::connection::ConnectionType) -> String {
1724 use schedr_model::connection::ConnectionType;
1725 match kind {
1726 ConnectionType::Http => "http",
1727 ConnectionType::Ssh => "ssh",
1728 ConnectionType::Mongo => "mongo",
1729 ConnectionType::Sql => "sql",
1730 ConnectionType::Generic => "generic",
1731 ConnectionType::Schedr => "schedr",
1732 }
1733 .to_owned()
1734}
1735
1736#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, ToSchema)]
1738pub struct WorkerList {
1739 pub workers: Vec<WorkerView>,
1741}
1742
1743#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, ToSchema)]
1745pub struct AttemptMemoryList {
1746 pub task_id: String,
1748 pub limit_bytes: Option<u64>,
1751 pub attempts: Vec<AttemptMemoryView>,
1753}
1754
1755#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, ToSchema)]
1757pub struct AttemptMemoryView {
1758 pub attempt_id: String,
1760 pub run_id: String,
1762 pub map_index: Option<u32>,
1764 pub try_number: u32,
1766 pub status: String,
1768 pub peak_memory_bytes: Option<u64>,
1770 pub ended_at: Timestamp,
1772}
1773
1774#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, ToSchema)]
1777pub struct DagMemoryList {
1778 pub since: Timestamp,
1781 pub tasks: Vec<TaskPeakView>,
1784}
1785
1786#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, ToSchema)]
1788pub struct TaskPeakView {
1789 pub task_id: String,
1791 pub attempts: u32,
1793 pub p95_bytes: u64,
1795 pub max_bytes: u64,
1797 pub oom_killed: u32,
1799 pub limit_bytes: Option<u64>,
1801 pub of_limit: Option<f64>,
1803}
1804
1805#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, ToSchema)]
1808pub struct TaskFlowList {
1809 pub range: String,
1811 pub step_s: u32,
1813 pub from: Timestamp,
1815 pub to: Timestamp,
1817 pub buckets: Vec<TaskFlowView>,
1819}
1820
1821#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, ToSchema)]
1823pub struct TaskFlowView {
1824 pub at: Timestamp,
1826 pub succeeded: u64,
1828 pub failed: u64,
1830 pub stopped: u64,
1833 pub started: u64,
1835 pub start_p50_ms: Option<u64>,
1837 pub start_p95_ms: Option<u64>,
1839}