-
Notifications
You must be signed in to change notification settings - Fork 145
Expand file tree
/
Copy pathworkflow-run-layout.svelte
More file actions
220 lines (192 loc) · 6.06 KB
/
workflow-run-layout.svelte
File metadata and controls
220 lines (192 loc) · 6.06 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
<script lang="ts">
import { onDestroy, onMount } from 'svelte';
import { page } from '$app/stores';
import WorkflowError from '$lib/components/workflow/workflow-error.svelte';
import CopyButton from '$lib/holocene/copyable/button.svelte';
import SkeletonWorkflow from '$lib/holocene/skeleton/workflow.svelte';
import { translate } from '$lib/i18n/translate';
import WorkflowHeader from '$lib/layouts/workflow-header.svelte';
import { Action } from '$lib/models/workflow-actions';
import {
fetchAllEvents,
throttleRefresh,
} from '$lib/services/events-service';
import { getPollers } from '$lib/services/pollers-service';
import { getWorkflowMetadata } from '$lib/services/query-service';
import { fetchWorkflow } from '$lib/services/workflow-service';
import { resetLastDataEncoderSuccess } from '$lib/stores/data-encoder-config';
import { eventFilterSort, type EventSortOrder } from '$lib/stores/event-view';
import {
currentEventHistory,
fullEventHistory,
pauseLiveUpdates,
timelineEvents,
} from '$lib/stores/events';
import {
initialWorkflowRun,
refresh,
type RefreshAction,
workflowRun,
} from '$lib/stores/workflow-run';
import type { NetworkError } from '$lib/types/global';
import type { WorkflowExecution } from '$lib/types/workflows';
import { copyToClipboard } from '$lib/utilities/copy-to-clipboard';
import { decodeSingleReadablePayloadWithCodec } from '$lib/utilities/decode-payload';
import { stringifyWithBigInt } from '$lib/utilities/parse-with-big-int';
$: ({ namespace, workflow: workflowId, run: runId } = $page.params);
$: showJson = $page.url.searchParams.has('json');
$: fullJson = { ...$workflowRun, eventHistory: $fullEventHistory };
let workflowError: NetworkError | null = null;
let workflowRunController: AbortController;
let refreshInterval: ReturnType<typeof setInterval> | null = null;
const { copy, copied } = copyToClipboard();
const handleCopy = (e: Event) => {
copy(e, stringifyWithBigInt(fullJson));
};
const decodeUserMetadata = async (workflow: WorkflowExecution) => {
const userMetadata = { summary: '', details: '' };
try {
if (workflow?.summary) {
const decodedSummary = await decodeSingleReadablePayloadWithCodec(
workflow.summary,
);
if (typeof decodedSummary === 'string') {
userMetadata.summary = decodedSummary;
}
}
if (workflow?.details) {
const decodedDetails = await decodeSingleReadablePayloadWithCodec(
workflow.details,
);
if (typeof decodedDetails === 'string') {
userMetadata.details = decodedDetails;
}
}
$workflowRun = { ...$workflowRun, userMetadata };
} catch (e) {
console.error('Error decoding user metadata', e);
}
};
const getWorkflowAndEventHistory = async (
namespace: string,
workflowId: string,
runId: string,
) => {
const { settings } = $page.data;
const { workflow, error } = await fetchWorkflow({
namespace,
workflowId,
runId,
});
if (error) {
workflowError = error;
return;
}
if (!workflow) {
return;
}
await decodeUserMetadata(workflow);
const { taskQueue } = workflow;
const workers = await getPollers({ queue: taskQueue, namespace });
$workflowRun = { ...$workflowRun, workflow, workers, workersLoaded: true };
workflowRunController = new AbortController();
if (workflow.isRunning && workers?.pollers?.length) {
getWorkflowMetadata(
{
namespace,
workflow: {
id: workflowId,
runId,
},
},
settings,
workflowRunController.signal,
).then((metadata) => {
$workflowRun.metadata = metadata;
});
}
$fullEventHistory = await fetchAllEvents({
namespace,
workflowId,
runId,
sort: 'ascending',
signal: workflowRunController.signal,
historySize: workflow.historyEvents,
});
};
const getOnlyWorkflowWithPendingActivities = async (
refresh: RefreshAction,
pause: boolean,
) => {
const shouldFetch =
refresh.timestamp &&
(refresh.action || (!pause && $workflowRun?.workflow?.isRunning));
if (shouldFetch) {
const { workflow, error } = await fetchWorkflow({
namespace,
workflowId,
runId,
});
if (error) {
workflowError = error;
return;
}
$workflowRun.workflow = workflow;
}
};
const abortPolling = () => {
$fullEventHistory = [];
if (workflowRunController) {
workflowRunController.abort();
}
};
const clearWorkflowData = () => {
$timelineEvents = null;
$workflowRun = initialWorkflowRun;
workflowError = undefined;
abortPolling();
resetLastDataEncoderSuccess();
clearInterval(refreshInterval);
refreshInterval = null;
};
$: (runId, clearWorkflowData());
$: getWorkflowAndEventHistory(namespace, workflowId, runId);
$: getOnlyWorkflowWithPendingActivities($refresh, $pauseLiveUpdates);
const setCurrentEvents = (fullHistory, pause) => {
if (!pause) {
$currentEventHistory = fullHistory;
}
};
$: setCurrentEvents($fullEventHistory, $pauseLiveUpdates);
onMount(() => {
const sort = $page.url.searchParams.get('sort');
if (sort) $eventFilterSort = sort as EventSortOrder;
refreshInterval = setInterval(() => {
throttleRefresh();
}, 10000);
});
onDestroy(() => {
clearWorkflowData();
});
</script>
{#if showJson}
<div
class="relative h-auto whitespace-break-spaces break-words bg-primary p-4"
>
<CopyButton
copyIconTitle={translate('common.copy-icon-title')}
copySuccessIconTitle={translate('common.copy-success-icon-title')}
class="absolute right-1 top-1"
on:click={handleCopy}
copied={$copied}
/>
{stringifyWithBigInt(fullJson, null, 2)}
</div>
{:else if workflowError}
<WorkflowError error={workflowError} />
{:else if !$workflowRun.workflow}
<SkeletonWorkflow />
{:else}
<WorkflowHeader />
<slot />
{/if}