models-reducer.ts 7.5 KB
Newer Older
1
// Copyright (C) 2020-2022 Intel Corporation
K
Kirill Lakhov 已提交
2
// Copyright (C) 2022-2023 CVAT.ai Corporation
B
Boris Sekachev 已提交
3 4 5
//
// SPDX-License-Identifier: MIT

V
Vitaliy Nishukov 已提交
6
import { BoundariesActions, BoundariesActionTypes } from 'actions/boundaries-actions';
7 8
import { ModelsActionTypes, ModelsActions } from 'actions/models-actions';
import { AuthActionTypes, AuthActions } from 'actions/auth-actions';
K
Kirill Lakhov 已提交
9 10
import { MLModel, ModelKind } from 'cvat-core-wrapper';
import { ModelsState } from '.';
11 12 13

const defaultState: ModelsState = {
    initialized: false,
14
    fetching: false,
15
    creatingStatus: '',
16 17 18 19
    interactors: [],
    detectors: [],
    trackers: [],
    reid: [],
K
Kirill Lakhov 已提交
20
    classifiers: [],
21 22
    modelRunnerIsVisible: false,
    modelRunnerTask: null,
23
    inferences: {},
K
Kirill Lakhov 已提交
24 25 26 27 28 29 30 31 32 33 34 35 36
    totalCount: 0,
    query: {
        page: 1,
        id: null,
        search: null,
        filter: null,
        sort: null,
    },
    providers: {
        fetching: false,
        list: [],
    },
    previews: {},
37 38
};

V
Vitaliy Nishukov 已提交
39
export default function (state = defaultState, action: ModelsActions | AuthActions | BoundariesActions): ModelsState {
40 41 42 43
    switch (action.type) {
        case ModelsActionTypes.GET_MODELS: {
            return {
                ...state,
44
                fetching: true,
K
Kirill Lakhov 已提交
45 46 47 48
                query: {
                    ...state.query,
                    ...action.payload.query,
                },
49 50 51 52 53
            };
        }
        case ModelsActionTypes.GET_MODELS_SUCCESS: {
            return {
                ...state,
K
Kirill Lakhov 已提交
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
                interactors: action.payload.models.filter((model: MLModel) => (
                    model.kind === ModelKind.INTERACTOR
                )),
                detectors: action.payload.models.filter((model: MLModel) => (
                    model.kind === ModelKind.DETECTOR
                )),
                trackers: action.payload.models.filter((model: MLModel) => (
                    model.kind === ModelKind.TRACKER
                )),
                reid: action.payload.models.filter((model: MLModel) => (
                    model.kind === ModelKind.REID
                )),
                classifiers: action.payload.models.filter((model: MLModel) => (
                    model.kind === ModelKind.CLASSIFIER
                )),
K
Kirill Lakhov 已提交
69
                totalCount: action.payload.count,
70
                initialized: true,
71
                fetching: false,
72 73 74 75 76 77
            };
        }
        case ModelsActionTypes.GET_MODELS_FAILED: {
            return {
                ...state,
                initialized: true,
78
                fetching: false,
79 80
            };
        }
K
Kirill Lakhov 已提交
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
        case ModelsActionTypes.CREATE_MODEL: {
            return {
                ...state,
                fetching: true,
            };
        }
        case ModelsActionTypes.CREATE_MODEL_FAILED: {
            return {
                ...state,
                fetching: false,
            };
        }
        case ModelsActionTypes.CREATE_MODEL_SUCCESS: {
            const mutual = {
                ...state,
                fetching: false,
            };
98

K
Kirill Lakhov 已提交
99 100 101 102 103 104
            if (action.payload.model.kind === ModelKind.REID) {
                return {
                    ...mutual,
                    reid: [...state.reid, action.payload.model],
                };
            }
105

K
Kirill Lakhov 已提交
106 107
            return {
                ...mutual,
108
                [`${action.payload.model.kind}s`]: [...state[`${action.payload.model.kind}s`], action.payload.model],
K
Kirill Lakhov 已提交
109 110
            };
        }
111
        case ModelsActionTypes.SHOW_RUN_MODEL_DIALOG: {
112 113
            return {
                ...state,
114 115
                modelRunnerIsVisible: true,
                modelRunnerTask: action.payload.taskInstance,
116 117
            };
        }
118
        case ModelsActionTypes.CLOSE_RUN_MODEL_DIALOG: {
119 120
            return {
                ...state,
121 122
                modelRunnerIsVisible: false,
                modelRunnerTask: null,
123 124
            };
        }
125
        case ModelsActionTypes.GET_INFERENCE_STATUS_SUCCESS: {
126
            const { inferences } = state;
127

128
            if (action.payload.activeInference.status === 'finished') {
129 130 131
                return {
                    ...state,
                    inferences: Object.fromEntries(
V
Vitaliy Nishukov 已提交
132
                        Object.entries(inferences).filter(([key]): boolean => +key !== action.payload.taskID),
133 134
                    ),
                };
135 136
            }

137 138 139
            const update: any = {};
            update[action.payload.taskID] = action.payload.activeInference;

140 141
            return {
                ...state,
142 143 144 145
                inferences: {
                    ...state.inferences,
                    ...update,
                },
146 147
            };
        }
148
        case ModelsActionTypes.GET_INFERENCE_STATUS_FAILED: {
149 150 151 152
            const { inferences } = state;

            return {
                ...state,
B
Boris Sekachev 已提交
153 154 155 156
                inferences: {
                    ...inferences,
                    [action.payload.taskID]: action.payload.activeInference,
                },
157 158 159 160
            };
        }
        case ModelsActionTypes.CANCEL_INFERENCE_SUCCESS: {
            const { inferences } = state;
161 162
            delete inferences[action.payload.taskID];

163 164
            return {
                ...state,
165
                inferences: { ...inferences },
166 167
            };
        }
K
Kirill Lakhov 已提交
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
        case ModelsActionTypes.GET_MODEL_PROVIDERS: {
            return {
                ...state,
                providers: {
                    ...state.providers,
                    fetching: true,
                },
            };
        }
        case ModelsActionTypes.GET_MODEL_PROVIDERS_SUCCESS: {
            return {
                ...state,
                providers: {
                    fetching: false,
                    list: action.payload.providers,
                },
            };
        }
        case ModelsActionTypes.GET_MODEL_PROVIDERS_FAILED: {
            return {
                ...state,
                providers: {
                    ...state.providers,
                    fetching: false,
                },
            };
        }
        case ModelsActionTypes.GET_MODEL_PREVIEW: {
            const { modelID } = action.payload;
            const { previews } = state;
            return {
                ...state,
                previews: {
                    ...previews,
                    [modelID]: {
                        preview: '',
                        fetching: true,
                        initialized: false,
                    },
                },
            };
        }
        case ModelsActionTypes.GET_MODEL_PREVIEW_SUCCESS: {
            const { modelID, preview } = action.payload;
            const { previews } = state;
            return {
                ...state,
                previews: {
                    ...previews,
                    [modelID]: {
                        preview,
                        fetching: false,
                        initialized: true,
                    },
                },
            };
        }
        case ModelsActionTypes.GET_MODEL_PREVIEW_FAILED: {
            const { modelID } = action.payload;
            const { previews } = state;
            return {
                ...state,
                previews: {
                    ...previews,
                    [modelID]: {
                        ...previews[modelID],
                        fetching: false,
                        initialized: true,
                    },
                },
            };
        }
240
        case BoundariesActionTypes.RESET_AFTER_ERROR:
241
        case AuthActionTypes.LOGOUT_SUCCESS: {
242
            return { ...defaultState };
243
        }
244
        default: {
245
            return state;
246 247 248
        }
    }
}