-
-
Notifications
You must be signed in to change notification settings - Fork 226
Expand file tree
/
Copy pathrun.ts
More file actions
175 lines (147 loc) · 6.21 KB
/
run.ts
File metadata and controls
175 lines (147 loc) · 6.21 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
import { sortBy as _sortBy } from 'lodash';
import { API } from 'api';
import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react';
import fetchBaseQueryHeaders from 'libs/fetchBaseQueryHeaders';
import { getExtendedModelFromRun } from '../libs/run';
import { unfinishedRuns } from '../pages/Runs/constants';
import { IModelExtended } from '../pages/Models/List/types';
const reduceInvalidateTagsFromRunNames = (names: Array<string>) => {
return names.reduce((accumulator, runName: string) => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
accumulator.push({ type: 'Runs', id: runName });
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
accumulator.push({ type: 'AllRuns', id: runName });
return accumulator;
}, []);
};
export const runApi = createApi({
reducerPath: 'runApi',
refetchOnMountOrArgChange: true,
baseQuery: fetchBaseQuery({
prepareHeaders: fetchBaseQueryHeaders,
}),
tagTypes: ['Runs', 'Models', 'Metrics'],
endpoints: (builder) => ({
getRuns: builder.query<IRun[], TRunsRequestParams>({
query: (body = {}) => {
return {
url: API.RUNS.LIST(),
method: 'POST',
body,
};
},
providesTags: (result) =>
result ? [...result.map(({ id }) => ({ type: 'Runs' as const, id: id })), 'Runs'] : ['Runs'],
}),
getRun: builder.query<IRun | undefined, { project_name: string; id: string }>({
query: ({ project_name, ...body }) => {
return {
url: API.PROJECTS.RUN_DETAILS(project_name ?? ''),
method: 'POST',
body,
};
},
providesTags: (result) => (result ? [{ type: 'Runs' as const, id: result?.id }] : []),
}),
stopRuns: builder.mutation<void, TStopRunsRequestParams>({
query: ({ project_name, ...body }) => ({
url: API.PROJECTS.RUNS_STOP(project_name),
method: 'POST',
body,
}),
invalidatesTags: (result, error, params) => reduceInvalidateTagsFromRunNames(params.runs_names),
}),
deleteRuns: builder.mutation<void, TDeleteRunsRequestParams>({
query: ({ project_name, ...body }) => ({
url: API.PROJECTS.RUNS_DELETE(project_name),
method: 'POST',
body,
}),
invalidatesTags: (result, error, params) => reduceInvalidateTagsFromRunNames(params.runs_names),
async onQueryStarted({ runs_names, project_name }, { dispatch, queryFulfilled }) {
const patchGetRunResult = dispatch(
runApi.util.updateQueryData('getRuns', { project_name }, (draftRuns) => {
runs_names.forEach((runName) => {
const index = draftRuns.findIndex((run) => {
return run.run_spec.run_name === runName && run.project_name === project_name;
});
if (index >= 0) {
draftRuns.splice(index, 1);
}
});
}),
);
const patchGetAllRunResult = dispatch(
runApi.util.updateQueryData('getRuns', {}, (draftRuns) => {
runs_names.forEach((runName) => {
const index =
draftRuns?.findIndex((run) => {
return run.run_spec.run_name === runName && run.project_name === project_name;
}) ?? -1;
if (index >= 0) {
draftRuns?.splice(index, 1);
}
});
}),
);
try {
await queryFulfilled;
} catch (e) {
patchGetRunResult.undo();
patchGetAllRunResult.undo();
}
},
}),
getModels: builder.query<IModelExtended[], TRunsRequestParams>({
query: (body = {}) => {
return {
url: API.RUNS.LIST(),
method: 'POST',
body,
};
},
transformResponse: (runs: IRun[]): IModelExtended[] => {
return (
_sortBy<IRun>(runs, [(i) => -i.submitted_at])
// Should show models of active runs only
.filter((run) => unfinishedRuns.includes(run.status) && run.service?.model)
.reduce<IModelExtended[]>((acc, run) => {
const model = getExtendedModelFromRun(run);
if (model) acc.push(model);
return acc;
}, [])
);
},
providesTags: (result) =>
result ? [...result.map(({ id }) => ({ type: 'Models' as const, id: id })), 'Models'] : ['Models'],
}),
getMetrics: builder.query<IMetricsItem[], TJobMetricsRequestParams>({
query: ({ project_name, run_name, ...params }) => {
return {
url: API.PROJECTS.JOB_METRICS(project_name, run_name),
method: 'GET',
params,
};
},
providesTags: ['Metrics'],
transformResponse: ({ metrics }: { metrics: IMetricsItem[] }): IMetricsItem[] => {
return metrics.map(({ timestamps, values, ...metric }) => ({
...metric,
timestamps: timestamps.reverse(),
values: values.reverse(),
}));
},
}),
}),
});
export const {
useGetRunsQuery,
useLazyGetRunsQuery,
useGetRunQuery,
useStopRunsMutation,
useDeleteRunsMutation,
useLazyGetModelsQuery,
useGetMetricsQuery,
} = runApi;