-
Notifications
You must be signed in to change notification settings - Fork 144
Expand file tree
/
Copy pathworkflow-execution.ts
More file actions
209 lines (198 loc) · 6.48 KB
/
workflow-execution.ts
File metadata and controls
209 lines (198 loc) · 6.48 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
import type {
Callbacks,
PendingActivity,
PendingActivityInfo,
PendingActivityState,
PendingChildren,
PendingNexusOperation,
} from '$lib/types/events';
import type { Callback } from '$lib/types/nexus';
import type {
DecodedWorkflowSearchAttributes,
ListWorkflowExecutionsResponse,
WorkflowExecution,
WorkflowExecutionAPIResponse,
WorkflowSearchAttributes,
} from '$lib/types/workflows';
import { decodePayload } from '$lib/utilities/decode-payload';
import {
toCallbackStateReadable,
toPendingActivityStateReadable,
toPendingNexusOperationStateReadable,
toWorkflowStatusReadable,
} from '$lib/utilities/screaming-enums';
import { writeActionsAreAllowed } from '$lib/utilities/write-actions-are-allowed';
import { simplifyAttributes } from './event-history/simplify-attributes';
export const toPendingActivities = (
pendingActivity: PendingActivityInfo[] = [],
): PendingActivity[] => {
return pendingActivity.map((activity): PendingActivity => {
const attributes = simplifyAttributes(activity, true);
const id = activity.activityId;
const state = activity.state as unknown as PendingActivityState;
return {
...attributes,
id,
state: toPendingActivityStateReadable(state),
};
});
};
const toPendingNexusOperations = (
operations?: PendingNexusOperation[],
): PendingNexusOperation[] => {
if (!operations) return [];
return operations.map((operation): PendingNexusOperation => {
return {
...operation,
state: toPendingNexusOperationStateReadable(operation.state),
};
});
};
const toCallbacks = (callbacks?: Callbacks): Callbacks => {
if (!callbacks) return [];
return callbacks.map((callback): Callback => {
return {
...callback,
state: toCallbackStateReadable(callback.state),
};
});
};
const toSearchAttributes = (
apiSearchAttributes: WorkflowSearchAttributes,
): DecodedWorkflowSearchAttributes => {
if (!apiSearchAttributes || !apiSearchAttributes.indexedFields) return {};
const decoded = Object.entries(apiSearchAttributes.indexedFields).reduce(
(searchAttributes, [searchAttributeName, payload]) => {
return {
...searchAttributes,
[searchAttributeName]: decodePayload(payload),
};
},
{},
);
return {
indexedFields: decoded,
};
};
const getStartDelay = ({
executionTime,
startTime,
}: {
executionTime: string;
startTime: string;
}): string => {
if (!executionTime || !startTime) return undefined;
const delayMs =
new Date(executionTime).getTime() - new Date(startTime).getTime();
if (delayMs > 0) {
return Math.round(delayMs / 1000) + 's';
}
return undefined;
};
export const toWorkflowExecution = (
response?: WorkflowExecutionAPIResponse,
): WorkflowExecution => {
const searchAttributes = toSearchAttributes(
response.workflowExecutionInfo.searchAttributes,
);
const memo = response.workflowExecutionInfo.memo;
const name = response.workflowExecutionInfo.type.name;
const id = response.workflowExecutionInfo.execution.workflowId;
const runId = response.workflowExecutionInfo.execution.runId;
const startTime = response.workflowExecutionInfo.startTime;
const endTime = response.workflowExecutionInfo.closeTime;
const executionTime = response.workflowExecutionInfo.executionTime;
const status = toWorkflowStatusReadable(
response.workflowExecutionInfo.status,
);
const isRunning = status === 'Running';
const isPaused = status === 'Paused';
const historyEvents = response.workflowExecutionInfo.historyLength;
const historySizeBytes = response.workflowExecutionInfo.historySizeBytes;
const url = `/workflows/${id}/${runId}`;
const taskQueue =
response?.executionConfig?.taskQueue?.name ||
response?.workflowExecutionInfo?.taskQueue;
const mostRecentWorkerVersionStamp =
response?.workflowExecutionInfo?.mostRecentWorkerVersionStamp;
const assignedBuildId = response?.workflowExecutionInfo?.assignedBuildId;
const parentNamespaceId = response?.workflowExecutionInfo?.parentNamespaceId;
const parent = response?.workflowExecutionInfo?.parentExecution;
const stateTransitionCount =
response.workflowExecutionInfo.stateTransitionCount;
const defaultWorkflowTaskTimeout =
response.executionConfig?.defaultWorkflowTaskTimeout;
const workflowExecutionTimeout =
response.executionConfig?.workflowExecutionTimeout;
const pendingActivities: PendingActivity[] = toPendingActivities(
response.pendingActivities,
);
const pendingChildren: PendingChildren[] = response?.pendingChildren ?? [];
const pendingNexusOperations: PendingNexusOperation[] =
toPendingNexusOperations(response?.pendingNexusOperations);
const pendingWorkflowTask = response?.pendingWorkflowTask;
const callbacks = toCallbacks(response?.callbacks);
const rootExecution = response.workflowExecutionInfo?.rootExecution;
const versioningInfo = response.workflowExecutionInfo?.versioningInfo;
const priority = response.workflowExecutionInfo?.priority;
const workflowExtendedInfo = response.workflowExtendedInfo ?? {};
const startDelay = getStartDelay({ executionTime, startTime });
const externalPayloadCount =
response.workflowExecutionInfo?.externalPayloadCount;
const externalPayloadSizeBytes =
response.workflowExecutionInfo?.externalPayloadSizeBytes;
let summary;
let details;
if (response?.executionConfig?.userMetadata) {
summary = response?.executionConfig?.userMetadata?.summary;
details = response?.executionConfig?.userMetadata?.details;
}
return {
name,
id,
runId,
startTime,
endTime,
executionTime,
status,
historyEvents,
historySizeBytes,
externalPayloadCount,
externalPayloadSizeBytes,
searchAttributes,
memo,
rootExecution,
url,
taskQueue,
assignedBuildId,
mostRecentWorkerVersionStamp,
pendingActivities,
pendingChildren,
pendingNexusOperations,
pendingWorkflowTask,
callbacks,
versioningInfo,
priority,
summary,
details,
parentNamespaceId,
parent,
stateTransitionCount,
isRunning,
isPaused,
defaultWorkflowTaskTimeout,
workflowExecutionTimeout,
workflowExtendedInfo,
startDelay,
get canBeTerminated(): boolean {
return (isRunning || isPaused) && writeActionsAreAllowed();
},
};
};
export const toWorkflowExecutions = (
response: Pick<ListWorkflowExecutionsResponse, 'executions'>,
): WorkflowExecution[] => {
return (response.executions || []).map((workflowExecutionInfo) =>
toWorkflowExecution({ workflowExecutionInfo }),
);
};