Skip to content

Commit

Permalink
[ML] show success toast on model deletion, fix models counter
Browse files Browse the repository at this point in the history
  • Loading branch information
darnautov committed Aug 14, 2020
1 parent d6123a3 commit 4ce85fb
Showing 1 changed file with 36 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,16 @@ export const ModelsList: FC = () => {
const [sortDirection, setSortDirection] = useState<Direction>('asc');

const [isLoading, setIsLoading] = useState(false);
const [modelsStats, setModelsStats] = useState<ModelsBarStats | undefined>();
const [items, setItems] = useState<ModelItem[]>([]);
const [selectedModels, setSelectedModels] = useState<ModelItem[]>([]);

const [modelsToDelete, setModelsToDelete] = useState<ModelWithStats[]>([]);

const [itemIdToExpandedRowMap, setItemIdToExpandedRowMap] = useState<Record<string, any>>({});

/**
* Fetches inference trained models.
*/
async function fetchData() {
setIsLoading(true);
try {
Expand All @@ -80,16 +82,6 @@ export const ModelsList: FC = () => {
: {}),
}))
);

setModelsStats({
total: {
show: true,
value: response.trained_model_configs.length,
label: i18n.translate('xpack.ml.inference.modelsList.totalAmountLabel', {
defaultMessage: 'Total inference trained models',
}),
},
});
} catch (error) {
toasts.addError(new Error(error.body.message), {
title: i18n.translate('xpack.ml.inference.modelsList.fetchFailedErrorMessage', {
Expand All @@ -100,6 +92,18 @@ export const ModelsList: FC = () => {
setIsLoading(false);
}

const modelsStats: ModelsBarStats = useMemo(() => {
return {
total: {
show: true,
value: items.length,
label: i18n.translate('xpack.ml.inference.modelsList.totalAmountLabel', {
defaultMessage: 'Total inference trained models',
}),
},
};
}, [items]);

useEffect(() => {
fetchData();
}, []);
Expand Down Expand Up @@ -167,20 +171,38 @@ export const ModelsList: FC = () => {
}
}

/**
* Deletes the models marked for deletion.
*/
async function deleteModels() {
const modelsToDeleteIds = modelsToDelete.map((model) => model.model_id);

try {
await Promise.all(
modelsToDelete.map((model) => inferenceApiService.deleteInferenceModel(model.model_id))
modelsToDeleteIds.map((modelId) => inferenceApiService.deleteInferenceModel(modelId))
);
setItems(
items.filter(
(model) => !modelsToDelete.some((toDelete) => toDelete.model_id === model.model_id)
)
);
toasts.addSuccess(
i18n.translate('xpack.ml.inference.modelsList.successfullyDeletedMessage', {
defaultMessage:
'{modelsCount, plural, one {Model} other {Models}} {modelsToDeleteIds} {modelsCount, plural, one {has} other {have}} been successfully deleted',
values: {
modelsCount: modelsToDeleteIds.length,
modelsToDeleteIds: modelsToDeleteIds.join(', '),
},
})
);
} catch (error) {
toasts.addError(new Error(error.body.message), {
toasts.addError(new Error(error?.body?.message), {
title: i18n.translate('xpack.ml.inference.modelsList.fetchDeletionErrorMessage', {
defaultMessage: 'Model deletion failed',
defaultMessage: '{modelsCount, plural, one {Model} other {Models}} deletion failed',
values: {
modelsCount: modelsToDeleteIds.length,
},
}),
});
}
Expand Down

0 comments on commit 4ce85fb

Please sign in to comment.