Skip to content

Commit

Permalink
optimize create index progress (#147)
Browse files Browse the repository at this point in the history
  • Loading branch information
shanghaikid authored Dec 15, 2022
1 parent 4c1f05d commit 3a3b2c2
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 29 deletions.
56 changes: 27 additions & 29 deletions client/src/pages/schema/IndexTypeElement.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
import {
FC,
useCallback,
useContext,
useEffect,
useMemo,
useState,
} from 'react';
import { FC, useContext, useEffect, useMemo, useState } from 'react';
import Chip from '@material-ui/core/Chip';
import { IndexHttp } from '../../http/Index';
import { IndexState } from '../../types/Milvus';
Expand All @@ -23,6 +16,7 @@ import CreateIndex from './Create';
import DeleteTemplate from '../../components/customDialog/DeleteDialogTemplate';
import StatusIcon from '../../components/status/StatusIcon';
import { ChildrenStatusType } from '../../components/status/Types';
import { sleep } from '../../utils/Common';

const useStyles = makeStyles((theme: Theme) => ({
wrapper: {
Expand Down Expand Up @@ -73,8 +67,6 @@ const useStyles = makeStyles((theme: Theme) => ({
},
}));

let timer: NodeJS.Timeout | null = null;

const IndexTypeElement: FC<{
data: FieldView;
collectionName: string;
Expand All @@ -83,7 +75,6 @@ const IndexTypeElement: FC<{
const classes = useStyles();
// set empty string as default status
const [status, setStatus] = useState<string>(IndexState.Default);

const { t: indexTrans } = useTranslation('index');
const { t: btnTrans } = useTranslation('btn');
const { t: dialogTrans } = useTranslation('dialog');
Expand All @@ -102,27 +93,34 @@ const IndexTypeElement: FC<{
[status]
);

const fetchStatus = useCallback(async () => {
useEffect(() => {
// define async data getter
const fetchStatus = async (
collectionName: string,
fieldName: string,
indexName: string
) => {
// get fetch data
const { state } = await IndexHttp.getIndexStatus(
collectionName,
fieldName,
indexName
);
if (state !== IndexState.Finished) {
// if not finished, sleep 3s
await sleep(3000);
// call self again
fetchStatus(collectionName, fieldName, indexName);
}

// update state
setStatus(state);
};
// prevent delete index trigger fetching index status
if (data._indexType !== '' && status !== IndexState.Delete) {
timer = setTimeout(async () => {
const { state: status } = await IndexHttp.getIndexStatus(
collectionName,
data._fieldName,
data._indexName
);

status !== IndexState.Finished
? fetchStatus()
: timer && clearTimeout(timer);
setStatus(status);
}, 3000);
fetchStatus(collectionName, data._fieldName, data._indexName);
}
}, [collectionName, data, status]);

useEffect(() => {
fetchStatus();
}, [fetchStatus]);
}, [collectionName, data._indexType, data._fieldName, data._indexName]);

const requestCreateIndex = async (
params: IndexExtraParam,
Expand Down
4 changes: 4 additions & 0 deletions client/src/utils/Common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,7 @@ export const generateVector = (dim: number) => {
export const cloneObj = (obj: any) => {
return JSON.parse(JSON.stringify(obj));
};

export const sleep = (ms: number) => {
return new Promise(resolve => setTimeout(resolve, ms));
};

0 comments on commit 3a3b2c2

Please sign in to comment.