-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathSilkActivityControl.tsx
More file actions
423 lines (393 loc) · 16.1 KB
/
SilkActivityControl.tsx
File metadata and controls
423 lines (393 loc) · 16.1 KB
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
import React, { useEffect, useRef, useState } from "react";
import { Intent } from "@blueprintjs/core/src/common/intent";
import { Icon, Spacing } from "../../";
import { IntentTypes } from "../../common/Intent";
import { TestableComponent } from "../../components/interfaces";
import { ElapsedDateTimeDisplay, ElapsedDateTimeDisplayUnits } from "../DateTimeDisplay/ElapsedDateTimeDisplay";
import { SilkActivityStatusConcrete, SilkActivityStatusProps } from "./ActivityControlTypes";
import { ActivityControlWidget, ActivityControlWidgetProps } from "./ActivityControlWidget";
import { ActivityExecutionErrorReportModal } from "./ActivityExecutionErrorReportModal";
const progressBreakpointIndetermination = 10;
const progressBreakpointAnimation = 99;
export interface SilkActivityControlProps extends TestableComponent {
// The label of this activity
label: string | React.JSX.Element;
/**
* To add tags in addition to the widget status description
*/
tags?: React.JSX.Element;
// Initial state
initialStatus?: SilkActivityStatusProps;
// Register a function in order to receive callbacks
registerForUpdates: (callback: (status: SilkActivityStatusProps) => any) => any;
// Un-register this component from any updates
unregisterFromUpdates: () => any;
// If the start action should be available
showStartAction: boolean;
// If the stop action should be available. Else actions can only be started, but not stopped.
showStopAction: boolean;
// Allow display and download of activity execution failure reports
failureReportAction?: IErrorReportAction;
// Show reload action, e.g. to refresh caches from scratch
showReloadAction: boolean;
// If defined, shows the 'View value' action, e.g. to check cache values
viewValueAction?: {
// Tooltip to show on icon button
tooltip?: string;
// The action, either a URL that will be opened in a new tab or a callback function
action: string | (() => any);
};
// DI activity actions
executeActivityAction: (action: SilkActivityControlAction) => void;
/** If specified, the activity control will offer a "Start prioritized" button while the activity is in the waiting state.
* When the button is clicked it should start the activity via the startPrioritized endpoint.
*/
executePrioritized?: () => void;
// Get the translation for a specific key
translate: (key: SilkActivityControlTranslationKeys) => string;
// When defined the elapsed time since the last start is displayed next to the label
elapsedTimeOfLastStart?: {
// Prefix before the elapsed time
prefix?: string;
// Suffix after the elapsed time
suffix?: string;
// The translation of the time units
translate: (unit: ElapsedDateTimeDisplayUnits) => string;
};
// configure how the widget is displayed
layoutConfig?: SilkActivityControlLayoutProps;
/** Configures when the status message should be hidden, e.g. because it is uninteresting. */
hideMessageOnStatus?: (concreteStatus: SilkActivityStatusConcrete | undefined) => boolean;
/**
* The translation of the time units
*/
translateUnits?: (unit: ElapsedDateTimeDisplayUnits) => string;
}
export interface SilkActivityControlLayoutProps {
// show small version of the widget
small?: boolean;
// display widget inside rectange
border?: boolean;
// add a bit spacing
hasSpacing?: boolean;
// only use necessary width, not always the available 100% of parent element
canShrink?: boolean;
// what type of progrss display should be uses, horizontal progress bar, circular spinner, or none of that
visualization?: "none" | "progressbar" | "spinner";
// wrapper around label
labelWrapper?: React.JSX.Element;
}
const defaultLayout: SilkActivityControlLayoutProps = {
small: false,
border: false,
canShrink: false,
visualization: "spinner",
};
interface IErrorReportAction {
// The title of the error report modal
title?: string;
// The element that will be rendered in the modal, either as Markdown or object
renderReport: (report: string | SilkActivityExecutionReportProps) => React.JSX.Element;
// What version of the report should be handed to the renderReport function, if false SilkActivityExecutionReportProps, if true the Markdown string
renderMarkdown: boolean;
// The function to fetch the error report. It returns undefined if something went wrong.
fetchErrorReport: (markdown: boolean) => Promise<string | SilkActivityExecutionReportProps | undefined>;
// If besides showing the error report, there should also be an option to download it.
allowDownload?: boolean;
// The text of the download button in the modal
downloadButtonValue: string;
// The text of the close button in the modal
closeButtonValue: string;
}
export interface SilkActivityExecutionReportProps {
// Summary of the activity execution error
errorSummary: string;
// If the activity was running in a project context, the project ID
projectId?: string;
// If the activity was running in a task context, the task ID
taskId?: string;
// The activity ID
activityId: string;
// If the activity was running in a project context, the project label
projectLabel?: string;
// If the activity was running in a task context, the task label
taskLabel?: string;
// If the activity was running in a task context, the optional task description
taskDescription?: string;
// The error message of the error/exception that has occurred
errorMessage?: string;
// The stacktrace leading to the error
stackTrace?: IStacktrace;
}
interface IStacktrace {
// The final error message of the stacktrace
errorMessage?: string;
// The individual elements of the stack trace
lines: string[];
// In case of nested stacktraces this may contain the cause of the failure
cause?: IStacktrace;
}
export type SilkActivityControlTranslationKeys =
| "startActivity"
| "stopActivity"
| "reloadActivity"
| "showErrorReport"
| "startPrioritized";
export type SilkActivityControlAction = "start" | "cancel" | "restart";
/** Silk activity control. */
export function SilkActivityControl(props: SilkActivityControlProps) {
const { widget } = useSilkActivityControl(props);
return widget;
}
export function useSilkActivityControl({
label,
initialStatus,
registerForUpdates,
executeActivityAction,
showReloadAction,
showStartAction,
viewValueAction,
showStopAction,
failureReportAction,
unregisterFromUpdates,
translate,
elapsedTimeOfLastStart,
tags,
layoutConfig = defaultLayout,
hideMessageOnStatus = () => false,
executePrioritized,
translateUnits = (unit: ElapsedDateTimeDisplayUnits) => unit.toString(),
...props
}: SilkActivityControlProps) {
const [activityStatus, setActivityStatus] = useState<SilkActivityStatusProps | undefined>(initialStatus);
const currentStatus = useRef<SilkActivityStatusProps | undefined>(initialStatus);
const [showStartPrioritized, setShowStartPrioritized] = useState(false);
const [errorReport, setErrorReport] = useState<string | SilkActivityExecutionReportProps | undefined>(undefined);
// Register update function
useEffect(
() => {
const updateActivityStatus = (status: SilkActivityStatusProps | undefined) => {
if (status?.concreteStatus !== "Waiting") {
setShowStartPrioritized(false);
} else if (executePrioritized) {
// Show start prioritized button only-if the activity is still in Waiting status after 2s
setTimeout(() => {
if (currentStatus.current?.concreteStatus === "Waiting") {
setShowStartPrioritized(true);
}
}, 2000);
}
currentStatus.current = status;
setActivityStatus(status);
};
registerForUpdates(updateActivityStatus);
return unregisterFromUpdates;
},
[]
);
// Create activity actions
const actions: ActivityControlWidgetProps["activityActions"] = [];
if (failureReportAction && activityStatus?.failed && activityStatus.concreteStatus !== "Cancelled") {
actions.push({
"data-test-id": "activity-show-error-report",
icon: "artefact-report",
action: () => showErrorReport(failureReportAction),
tooltip: translate("showErrorReport"),
hasStateWarning: true,
});
}
if (showStartAction) {
if (showStartPrioritized && executePrioritized) {
actions.push({
"data-test-id": "activity-start-prioritized-activity",
icon: "item-skip-forward",
action: executePrioritized,
tooltip: translate("startPrioritized"),
});
} else {
actions.push({
"data-test-id": "activity-start-activity",
icon: "item-start",
action: () => executeActivityAction("start"),
tooltip: translate("startActivity"),
disabled: activityStatus?.isRunning === true,
});
}
}
if (showReloadAction) {
actions.push({
"data-test-id": "activity-reload-activity",
icon: "item-reload",
action: () => executeActivityAction("restart"),
tooltip: translate("reloadActivity"),
disabled: activityStatus?.isRunning === true,
});
}
if (showStopAction) {
actions.push({
"data-test-id": "activity-stop-activity",
icon: "item-stop",
action: () => executeActivityAction("cancel"),
tooltip: translate("stopActivity"),
disabled: activityStatus?.isRunning === false,
});
}
if (viewValueAction && activityStatus?.concreteStatus !== "Not executed") {
const action: () => any =
typeof viewValueAction.action === "string"
? () => {
window.open(viewValueAction.action as string, "_blank");
}
: viewValueAction.action;
actions.push({
"data-test-id": "activity-view-data",
icon: "artefact-rawdata",
action,
tooltip: viewValueAction.tooltip,
});
}
const showErrorReport = async (action: IErrorReportAction) => {
const errorReport = await action.fetchErrorReport(action.renderMarkdown);
setErrorReport(errorReport);
};
const closeErrorReport = () => {
setErrorReport(undefined);
};
const activityControlLabel =
activityStatus?.startTime && elapsedTimeOfLastStart ? (
<>
{label}
<Spacing vertical={true} size="tiny" />
<ElapsedDateTimeDisplay
dateTime={activityStatus.startTime}
prefix={elapsedTimeOfLastStart.prefix}
suffix={elapsedTimeOfLastStart.suffix}
translateUnits={elapsedTimeOfLastStart.translate}
/>
</>
) : (
label
);
const timerExecutionMessage =
(activityStatus?.startTime || activityStatus?.queueTime) && activityStatus.statusName !== "Finished" ? (
<ElapsedDateTimeDisplay
includeSeconds
dateTime={
(activityStatus.statusName === "Running"
? activityStatus?.startTime
: activityStatus.statusName === "Waiting"
? activityStatus.queueTime
: activityStatus?.startTime)!
}
translateUnits={translateUnits}
/>
) : null;
const { visualization, ...otherLayoutConfig } = layoutConfig;
let visualizationProps = {}; // visualization==="none" or undefined
const runningProgress = activityStatus && activityStatus.isRunning;
const waitingProgress = activityStatus && activityStatus.concreteStatus === "Waiting";
const animateProgress =
activityStatus && activityStatus.progress > 0 && activityStatus.progress < progressBreakpointAnimation;
const indeterminateProgress = activityStatus && activityStatus.progress < progressBreakpointIndetermination;
const intent = activityStatus ? calcIntent(activityStatus) : "none";
if (visualization === "progressbar") {
visualizationProps = {
progressBar: {
animate: waitingProgress || (runningProgress && animateProgress),
stripes: waitingProgress || (runningProgress && animateProgress),
value:
waitingProgress || (runningProgress && indeterminateProgress)
? undefined
: activityStatus && activityStatus.progress > 0
? activityStatus.progress / 100
: 0,
intent,
},
};
}
if (visualization === "spinner") {
visualizationProps = {
progressSpinner: {
value:
waitingProgress || (runningProgress && indeterminateProgress)
? undefined
: activityStatus && activityStatus.progress > 0
? activityStatus.progress / 100
: 0,
intent,
},
};
}
if (activityStatus?.statusName === "Finished" && intent !== "none" && intent !== "primary") {
visualizationProps = {
...visualizationProps,
progressSpinnerFinishedIcon: <Icon name={[`state-${intent}`]} intent={intent as IntentTypes} />,
};
}
const widget = (
<>
<ActivityControlWidget
key={"activity-control"}
tags={tags}
data-test-id={props["data-test-id"]}
label={activityControlLabel}
activityActions={actions}
timerExecutionMsg={timerExecutionMessage}
statusMessage={
hideMessageOnStatus(activityStatus?.concreteStatus) ? undefined : activityStatus?.message
}
{...visualizationProps}
{...otherLayoutConfig}
/>
{errorReport && failureReportAction && (
<ActivityExecutionErrorReportModal
title={failureReportAction.title}
key={"error-report-modal"}
closeButtonValue={failureReportAction.closeButtonValue}
downloadButtonValue={failureReportAction.downloadButtonValue}
fetchErrorReport={async () => {
return (await failureReportAction.fetchErrorReport(true)) as string | undefined;
}}
report={failureReportAction.renderReport(errorReport)}
onDiscard={closeErrorReport}
/>
)}
</>
);
return {
elapsedDateTime:
activityStatus?.startTime && elapsedTimeOfLastStart ? (
<ElapsedDateTimeDisplay
dateTime={activityStatus.startTime}
translateUnits={elapsedTimeOfLastStart.translate}
/>
) : (
<></>
),
intent,
widget,
} as const;
}
export const calcIntent = (activityStatus: SilkActivityStatusProps): Intent => {
const concreteStatus = activityStatus.concreteStatus;
let intent: Intent;
switch (concreteStatus) {
case "Running":
case "Successful":
intent = "success";
break;
case "Cancelled":
case "Canceling":
intent = "warning";
break;
case "Failed":
intent = "danger";
break;
case "Waiting":
intent = "none";
break;
default:
intent = "none";
}
return intent;
};