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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
use super::{NotificationHook, StartParameter, Step, WorkflowDefinition, WorkflowInstance};
use crate::common::Job;
use mcai_graph::{Graph, GraphConfiguration, ToGraph};
use semver::Version;
use std::collections::HashMap;

#[derive(PartialEq)]
pub enum SchemaVersion {
  _1_8,
  _1_9,
  _1_10,
  _1_11,
}

#[derive(Clone, PartialEq)]
pub enum Workflow {
  Definition(WorkflowDefinition),
  Instance(WorkflowInstance),
}

impl Workflow {
  pub fn is_definition(&self) -> bool {
    match self {
      Workflow::Definition(_) => true,
      Workflow::Instance(_) => false,
    }
  }

  pub fn schema_version(&self) -> SchemaVersion {
    match self {
      Workflow::Definition(workflow) => workflow.schema_version(),
      Workflow::Instance(workflow) => workflow.schema_version(),
    }
  }

  pub fn identifier(&self) -> &str {
    match self {
      Workflow::Definition(workflow) => workflow.identifier(),
      Workflow::Instance(workflow) => &workflow.identifier,
    }
  }

  pub fn label(&self) -> &str {
    match self {
      Workflow::Definition(workflow) => workflow.label(),
      Workflow::Instance(workflow) => &workflow.label,
    }
  }

  pub fn version(&self) -> Version {
    match self {
      Workflow::Definition(workflow) => workflow.version(),
      Workflow::Instance(workflow) => Version::new(
        workflow.version_major as u64,
        workflow.version_minor as u64,
        workflow.version_micro as u64,
      ),
    }
  }

  pub fn is_live(&self) -> bool {
    match self {
      Workflow::Definition(workflow) => workflow.is_live(),
      Workflow::Instance(workflow) => workflow.is_live,
    }
  }

  pub fn tags(&self) -> &Vec<String> {
    match self {
      Workflow::Definition(workflow) => workflow.tags(),
      Workflow::Instance(workflow) => &workflow.tags,
    }
  }

  pub fn reference(&self) -> Option<String> {
    match self {
      Workflow::Definition(_) => None,
      Workflow::Instance(workflow) => workflow.reference.clone(),
    }
  }

  pub fn jobs(&self) -> Option<&Vec<Job>> {
    match self {
      Workflow::Definition(_) => None,
      Workflow::Instance(workflow) => Some(&workflow.jobs),
    }
  }

  pub fn steps(&self) -> &Vec<Step> {
    match self {
      Workflow::Definition(workflow) => workflow.steps(),
      Workflow::Instance(workflow) => &workflow.steps,
    }
  }

  pub fn get_start_parameters(&self) -> &Vec<StartParameter> {
    match self {
      Workflow::Definition(workflow) => workflow.get_start_parameters(),
      Workflow::Instance(workflow) => &workflow.start_parameters,
    }
  }

  pub fn get_notification_hooks(&self) -> Option<&Vec<NotificationHook>> {
    match self {
      Workflow::Definition(workflow) => workflow.get_notification_hooks(),
      Workflow::Instance(_workflow) => None,
    }
  }

  pub fn update_steps_coordinates_from_graph(&mut self, graph: &Graph) {
    if let Workflow::Definition(workflow_definition) = self {
      for (id, node) in graph.nodes() {
        if let Some(step) = workflow_definition.get_mut_step(id) {
          step.coordinates = Some(node.borrow().coordinates().clone());
        }
        continue;
      }
    }
  }
}

impl ToGraph for Workflow {
  fn to_graph(&self, configuration: GraphConfiguration) -> Graph {
    let steps = self.steps().clone();

    let mut graph = Graph::new(configuration.clone());

    let mut nodes = HashMap::with_capacity(steps.len());
    let mut node_level = HashMap::with_capacity(steps.len());

    let mut level = 0;
    let mut column = 0;

    for step in self.steps().iter() {
      // Add node from step coordinates
      if let Some(coordinates) = &step.coordinates {
        let node = graph.add_node(step.id, coordinates.clone());
        nodes.insert(step.id, node);
        node_level.insert(step.id, level);
        continue;
      }

      // Add node from step without parents
      if step.parent_ids.is_empty() {
        let node = graph.add_node(
          step.id,
          configuration
            .node_configuration()
            .get_coordinates(level, column),
        );
        nodes.insert(step.id, node);
        node_level.insert(step.id, level);

        column += 1;
      }
    }

    loop {
      column = 0;
      if nodes.len() == steps.len() {
        break;
      }
      level += 1;

      // Add nodes from step with parents
      for step in steps.iter() {
        if nodes.contains_key(&step.id) {
          continue;
        }

        if let Some(latest_level) = step
          .parent_ids
          .iter()
          .filter_map(|parent_id| node_level.get(parent_id))
          .max()
        {
          let step_level = latest_level + 1;
          if step_level == level {
            let node = graph.add_node(
              step.id,
              configuration
                .node_configuration()
                .get_coordinates(step_level, column),
            );

            nodes.insert(step.id, node);
            node_level.insert(step.id, step_level);

            column += 1;
          }
        }
      }
    }

    // Set step parents and required steps
    for step in steps.iter() {
      let parents = step
        .parent_ids
        .iter()
        .filter_map(|parent_id| nodes.get(parent_id).cloned())
        .collect();

      let required_to_start = step
        .required_to_start
        .iter()
        .filter_map(|required_step_id| nodes.get(required_step_id).cloned())
        .collect();

      if let Some(node) = nodes.get(&step.id) {
        node.borrow_mut().set_parents(parents);
        node.borrow_mut().set_required_nodes(required_to_start);
      }
    }

    graph
  }
}