forked from opensearch-project/dashboards-flow-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworkflows_reducer.ts
265 lines (252 loc) · 7.9 KB
/
workflows_reducer.ts
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
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/
import { createAsyncThunk, createSlice } from '@reduxjs/toolkit';
import {
Workflow,
ReactFlowComponent,
ReactFlowEdge,
KnnIndex,
TextEmbeddingProcessor,
generateId,
initComponentData,
WORKFLOW_STATE,
WorkflowDict,
} from '../../../common';
import { HttpFetchError } from '../../../../../src/core/public';
import { getRouteService } from '../../services';
// TODO: remove hardcoded initial state below after fetching from server side,
// and workflow data model interface is more defined.
// const id1 = generateId('text_embedding_processor');
// const id2 = generateId('text_embedding_processor');
// const id3 = generateId('knn_index');
// const dummyNodes = [
// {
// id: id1,
// position: { x: 0, y: 500 },
// data: initComponentData(new TextEmbeddingProcessor().toObj(), id1),
// type: 'customComponent',
// },
// {
// id: id2,
// position: { x: 0, y: 200 },
// data: initComponentData(new TextEmbeddingProcessor().toObj(), id2),
// type: 'customComponent',
// },
// {
// id: id3,
// position: { x: 500, y: 500 },
// data: initComponentData(new KnnIndex().toObj(), id3),
// type: 'customComponent',
// },
// ] as ReactFlowComponent[];
// let workflows = {} as { [workflowId: string]: Workflow };
// workflows['workflow-1-id'] = {
// name: 'Workflow-1',
// id: 'workflow-1-id',
// description: 'description for workflow 1',
// state: WORKFLOW_STATE.SUCCEEDED,
// workspaceFlowState: {
// nodes: dummyNodes,
// edges: [] as ReactFlowEdge[],
// },
// template: {},
// } as Workflow;
// workflows['workflow-2-id'] = {
// name: 'Workflow-2',
// id: 'workflow-2-id',
// description: 'description for workflow 2',
// state: WORKFLOW_STATE.FAILED,
// workspaceFlowState: {
// nodes: dummyNodes,
// edges: [] as ReactFlowEdge[],
// },
// template: {},
// } as Workflow;
// const initialState = {
// loading: false,
// errorMessage: '',
// workflows,
// };
const initialState = {
loading: false,
errorMessage: '',
workflows: {} as WorkflowDict,
};
const WORKFLOWS_ACTION_PREFIX = 'workflows';
const GET_WORKFLOW_ACTION = `${WORKFLOWS_ACTION_PREFIX}/getWorkflow`;
const SEARCH_WORKFLOWS_ACTION = `${WORKFLOWS_ACTION_PREFIX}/searchWorkflows`;
const GET_WORKFLOW_STATE_ACTION = `${WORKFLOWS_ACTION_PREFIX}/getWorkflowState`;
const CREATE_WORKFLOW_ACTION = `${WORKFLOWS_ACTION_PREFIX}/createWorkflow`;
const DELETE_WORKFLOW_ACTION = `${WORKFLOWS_ACTION_PREFIX}/deleteWorkflow`;
export const getWorkflow = createAsyncThunk(
GET_WORKFLOW_ACTION,
async (workflowId: string, { rejectWithValue }) => {
const response: any | HttpFetchError = await getRouteService().getWorkflow(
workflowId
);
if (response instanceof HttpFetchError) {
return rejectWithValue(
'Error getting workflow: ' + response.body.message
);
} else {
return response;
}
}
);
export const searchWorkflows = createAsyncThunk(
SEARCH_WORKFLOWS_ACTION,
async (body: {}, { rejectWithValue }) => {
const response:
| any
| HttpFetchError = await getRouteService().searchWorkflows(body);
if (response instanceof HttpFetchError) {
return rejectWithValue(
'Error searching workflows: ' + response.body.message
);
} else {
return response;
}
}
);
export const getWorkflowState = createAsyncThunk(
GET_WORKFLOW_STATE_ACTION,
async (workflowId: string, { rejectWithValue }) => {
const response:
| any
| HttpFetchError = await getRouteService().getWorkflowState(workflowId);
if (response instanceof HttpFetchError) {
return rejectWithValue(
'Error getting workflow state: ' + response.body.message
);
} else {
return response;
}
}
);
export const createWorkflow = createAsyncThunk(
CREATE_WORKFLOW_ACTION,
async (body: {}, { rejectWithValue }) => {
const response:
| any
| HttpFetchError = await getRouteService().createWorkflow(body);
if (response instanceof HttpFetchError) {
return rejectWithValue(
'Error creating workflow: ' + response.body.message
);
} else {
return response;
}
}
);
export const deleteWorkflow = createAsyncThunk(
DELETE_WORKFLOW_ACTION,
async (workflowId: string, { rejectWithValue }) => {
const response:
| any
| HttpFetchError = await getRouteService().deleteWorkflow(workflowId);
if (response instanceof HttpFetchError) {
return rejectWithValue(
'Error deleting workflow: ' + response.body.message
);
} else {
return response;
}
}
);
const workflowsSlice = createSlice({
name: 'workflows',
initialState,
reducers: {},
extraReducers: (builder) => {
builder
// Pending states: set state consistently across all actions
.addCase(getWorkflow.pending, (state, action) => {
state.loading = true;
state.errorMessage = '';
})
.addCase(searchWorkflows.pending, (state, action) => {
state.loading = true;
state.errorMessage = '';
})
.addCase(createWorkflow.pending, (state, action) => {
state.loading = true;
state.errorMessage = '';
})
.addCase(deleteWorkflow.pending, (state, action) => {
state.loading = true;
state.errorMessage = '';
})
.addCase(getWorkflowState.pending, (state, action) => {
state.loading = true;
state.errorMessage = '';
})
// Fulfilled states: mutate state depending on the action type
// and payloads
.addCase(getWorkflow.fulfilled, (state, action) => {
// TODO: add logic to mutate state
// const workflow = action.payload;
// state.workflows = {
// ...state.workflows,
// [workflow.id]: workflow,
// };
state.loading = false;
state.errorMessage = '';
})
.addCase(searchWorkflows.fulfilled, (state, action) => {
const { workflows } = action.payload as { workflows: WorkflowDict };
state.workflows = workflows;
state.loading = false;
state.errorMessage = '';
})
.addCase(getWorkflowState.fulfilled, (state, action) => {
// TODO: add logic to mutate state
// const workflow = action.payload;
// state.workflows = {
// ...state.workflows,
// [workflow.id]: workflow,
// };
state.loading = false;
state.errorMessage = '';
})
.addCase(createWorkflow.fulfilled, (state, action) => {
// TODO: add logic to mutate state
// const workflow = action.payload;
// state.workflows = {
// ...state.workflows,
// [workflow.id]: workflow,
// };
state.loading = false;
state.errorMessage = '';
})
.addCase(deleteWorkflow.fulfilled, (state, action) => {
const workflowId = action.payload.id;
delete state.workflows[workflowId];
state.loading = false;
state.errorMessage = '';
})
// Rejected states: set state consistently across all actions
.addCase(getWorkflow.rejected, (state, action) => {
state.errorMessage = action.payload as string;
state.loading = false;
})
.addCase(searchWorkflows.rejected, (state, action) => {
state.errorMessage = action.payload as string;
state.loading = false;
})
.addCase(getWorkflowState.rejected, (state, action) => {
state.errorMessage = action.payload as string;
state.loading = false;
})
.addCase(createWorkflow.rejected, (state, action) => {
state.errorMessage = action.payload as string;
state.loading = false;
})
.addCase(deleteWorkflow.rejected, (state, action) => {
state.errorMessage = action.payload as string;
state.loading = false;
});
},
});
export const workflowsReducer = workflowsSlice.reducer;