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
use crate::SharedWorkflow;
use css_in_rust_next::Style;
use mcai_models::{Workflow, WorkflowDefinition};
use std::ops::{Deref, Not};
use yew::{html, Component, Context, Html, Properties};

#[derive(PartialEq, Properties)]
pub struct WorkflowPanelProperties {
  pub workflow: SharedWorkflow,
  pub height: String,
  pub width: String,
  pub step_id: Option<u32>,
}

pub struct WorkflowPanel {
  style: Style,
}

impl Component for WorkflowPanel {
  type Message = ();
  type Properties = WorkflowPanelProperties;

  fn create(_ctx: &Context<Self>) -> Self {
    let style = Style::create("Component", include_str!("panel.css")).unwrap();
    Self { style }
  }

  fn view(&self, ctx: &Context<Self>) -> Html {
    let style = format!(
      "height: {}; width: {};",
      ctx.props().height,
      ctx.props().width,
    );

    let step_information = ctx
      .props()
      .step_id
      .map(|step_id| {
        let workflow = ctx
          .props()
          .workflow
          .lock()
          .unwrap();

        let jobs: Vec<Html> = workflow
          .jobs()
          .map(|jobs|
            jobs
              .iter()
              .filter(|job| job.step_id == step_id)
              .map(|job| {
                let parameters = super::parameters_as_html(&job.parameters);

                let progression = job.get_last_progression();

                let classes = format!("job {}", job.status.first().map(|job_status| job_status.state.clone()).unwrap_or_default());

                html!(
                  <div class={classes}>
                    <div class="field">
                      <label>{"Title"}</label>
                      <span>
                        {&job.name}
                      </span>
                    </div>
                    <div class="field status">
                      <label>{"Status"}</label>
                      <span>
                        {job.status.first().map(|job_status| html!({&job_status.state})).unwrap_or_default()}
                      </span>
                    </div>
                    <div class="field">
                      <label>{"Last Worker Instance ID"}</label>
                      <span>
                        {job.last_worker_instance_id.clone().unwrap_or_default()}
                      </span>
                    </div>
                    <div class="field">
                      <label>{"Progression"}</label>
                      <span>
                        {progression.map(|progression| progression.progression).unwrap_or_default()}
                      </span>
                    </div>
                    <div>
                      <label style="font-weight: bold;">{"Parameters"}</label>
                      <span>
                        {parameters}
                      </span>
                    </div>
                  </div>
                )
              })
              .collect()
          )
          .unwrap_or_default();

        let jobs = jobs
          .is_empty()
          .not()
          .then(||
            html!(
              <div class="jobs">
                <label>{"Jobs"}</label>
                <div class="inner">
                  {jobs}
                </div>
              </div>
            )
          )
          .unwrap_or_default();

        workflow
          .steps()
          .iter()
          .find(|step| step.id == step_id)
          .map(|step| {
            let parameters = super::parameters_as_html(&step.parameters);

            html!(
              <div class="step">
                <div class="title">
                  {format!("Step {}", step.label)}
                </div>
                <div class="content">
                  <div>
                    <label style="font-weight: bold;">{"Parameters"}</label>
                    <span>
                      {parameters}
                    </span>
                  </div>
                  {jobs}
                </div>
              </div>
            )
          })
          .unwrap_or_default()
      })
      .unwrap_or_else(|| {
        let reference = ctx
          .props()
          .workflow
          .lock()
          .unwrap()
          .reference()
          .map(|reference| html!(
            <div class="field">
              <label>
                {"Reference"}
              </label>
              <span>
                {reference}
              </span>
            </div>
          ))
          .unwrap_or_default();

        let workflow = ctx.props().workflow.lock().unwrap();
        let identifier = workflow.identifier();
        let version = workflow.version();

        let schema_version = match workflow.deref() {
          Workflow::Definition(WorkflowDefinition::Version1_8(_)) => "1.8".to_string(),
          Workflow::Definition(WorkflowDefinition::Version1_9(_)) => "1.9".to_string(),
          Workflow::Definition(WorkflowDefinition::Version1_10(_)) => "1.10".to_string(),
          Workflow::Instance(workflow_instance) => workflow_instance.schema_version.clone()
        };

        html!(
          <>
            <div class="title">
              <label>{format!("Workflow {}", identifier)}</label>
            </div>
            <div class="content">
              <div class="field">
                <label>
                  {"Version"}
                </label>
                <span>
                  {version}
                </span>
              </div>
              <div class="field">
                <label>
                  {"Schema version"}
                </label>
                <span>
                  {schema_version}
                </span>
              </div>
              {reference}
            </div>
          </>
        )
      });

    html!(
      <span class={self.style.clone()} style={style}>
        {step_information}
      </span>
    )
  }
}