Browse Source

add fetch index build progress

tumao 3 years ago
parent
commit
f29fe380d0

+ 18 - 2
client/src/http/Index.ts

@@ -24,9 +24,9 @@ export class IndexHttp extends BaseModel implements IndexView {
   static async getIndexStatus(
   static async getIndexStatus(
     collectionName: string,
     collectionName: string,
     fieldName: string
     fieldName: string
-  ): Promise<IndexState> {
+  ): Promise<{ state: IndexState }> {
     const path = `${this.BASE_URL}/state`;
     const path = `${this.BASE_URL}/state`;
-    return super.findAll({
+    return super.search({
       path,
       path,
       params: { collection_name: collectionName, field_name: fieldName },
       params: { collection_name: collectionName, field_name: fieldName },
     });
     });
@@ -59,6 +59,22 @@ export class IndexHttp extends BaseModel implements IndexView {
     return super.batchDelete({ path, data: { ...param, type } });
     return super.batchDelete({ path, data: { ...param, type } });
   }
   }
 
 
+  static async getIndexBuildProgress(
+    collectionName: string,
+    fieldName: string
+  ) {
+    const path = `${this.BASE_URL}/progress`;
+    return super.search({
+      path,
+      params: {
+        collection_name: collectionName,
+        field_name: fieldName,
+        // user can't set index_name, use empty string as its value
+        index_name: '',
+      },
+    });
+  }
+
   get _indexType() {
   get _indexType() {
     return this.params.find(p => p.key === 'index_type')?.value || '';
     return this.params.find(p => p.key === 'index_type')?.value || '';
   }
   }

+ 1 - 1
client/src/i18n/cn/index.ts

@@ -8,7 +8,7 @@ const indexTrans = {
   desc: 'Description',
   desc: 'Description',
   creating: 'Creating Index',
   creating: 'Creating Index',
 
 
-  createSuccess: 'Creating index successfully',
+  createSuccess: 'Start creating index',
   deleteWarning:
   deleteWarning:
     'You are trying to delete an index. This action cannot be undone.',
     'You are trying to delete an index. This action cannot be undone.',
 };
 };

+ 1 - 1
client/src/i18n/en/index.ts

@@ -9,7 +9,7 @@ const indexTrans = {
   creating: 'Creating Index',
   creating: 'Creating Index',
 
 
   metric: 'Metric Type',
   metric: 'Metric Type',
-  createSuccess: 'Creating index successfully',
+  createSuccess: 'Start creating index',
   deleteWarning:
   deleteWarning:
     'You are trying to delete an index. This action cannot be undone.',
     'You are trying to delete an index. This action cannot be undone.',
 };
 };

+ 48 - 4
client/src/pages/schema/IndexTypeElement.tsx

@@ -61,21 +61,23 @@ const useStyles = makeStyles((theme: Theme) => ({
   },
   },
 }));
 }));
 
 
+let timer: NodeJS.Timeout | null = null;
+
 const IndexTypeElement: FC<{
 const IndexTypeElement: FC<{
   data: FieldView;
   data: FieldView;
   collectionName: string;
   collectionName: string;
   cb: (collectionName: string) => void;
   cb: (collectionName: string) => void;
 }> = ({ data, collectionName, cb }) => {
 }> = ({ data, collectionName, cb }) => {
   const classes = useStyles();
   const classes = useStyles();
-
-  const [status, setStatus] = useState<string>('');
+  // set in progress as defalut status
+  const [status, setStatus] = useState<string>(IndexState.InProgress);
 
 
   const { t: indexTrans } = useTranslation('index');
   const { t: indexTrans } = useTranslation('index');
   const { t: btnTrans } = useTranslation('btn');
   const { t: btnTrans } = useTranslation('btn');
   const { t: dialogTrans } = useTranslation('dialog');
   const { t: dialogTrans } = useTranslation('dialog');
   const { t: successTrans } = useTranslation('success');
   const { t: successTrans } = useTranslation('success');
 
 
-  const [createProgress, setCreateProgress] = useState<number>(40);
+  const [createProgress, setCreateProgress] = useState<number>(0);
 
 
   const { setDialog, handleCloseDialog, openSnackBar } =
   const { setDialog, handleCloseDialog, openSnackBar } =
     useContext(rootContext);
     useContext(rootContext);
@@ -85,7 +87,7 @@ const IndexTypeElement: FC<{
 
 
   const fetchStatus = useCallback(async () => {
   const fetchStatus = useCallback(async () => {
     if (data._indexType !== '') {
     if (data._indexType !== '') {
-      const status = await IndexHttp.getIndexStatus(
+      const { state: status } = await IndexHttp.getIndexStatus(
         collectionName,
         collectionName,
         data._fieldName
         data._fieldName
       );
       );
@@ -97,6 +99,48 @@ const IndexTypeElement: FC<{
     fetchStatus();
     fetchStatus();
   }, [fetchStatus]);
   }, [fetchStatus]);
 
 
+  const fetchProgress = useCallback(() => {
+    if (timer) {
+      clearTimeout(timer);
+    }
+    if (data._indexType !== '' && status === IndexState.InProgress) {
+      timer = setTimeout(async () => {
+        const res = await IndexHttp.getIndexBuildProgress(
+          collectionName,
+          data._fieldName
+        );
+
+        const { indexed_rows, total_rows } = res;
+        const percent = Number(indexed_rows) / Number(total_rows);
+        const value = Math.floor(percent * 100);
+        setCreateProgress(value);
+
+        if (value !== 100) {
+          fetchProgress();
+        } else {
+          console.log(
+            '--- percent value:',
+            value,
+            'indexed rows',
+            indexed_rows,
+            'total_rows',
+            total_rows
+          );
+          timer && clearTimeout(timer);
+          // reset build progress
+          setCreateProgress(0);
+          // change index create status
+          setStatus(IndexState.Finished);
+        }
+      }, 500);
+    }
+  }, [collectionName, data._fieldName, status, data._indexType]);
+
+  // get index build progress
+  useEffect(() => {
+    fetchProgress();
+  }, [fetchProgress]);
+
   const requestCreateIndex = async (params: ParamPair[]) => {
   const requestCreateIndex = async (params: ParamPair[]) => {
     const indexCreateParam: IndexCreateParam = {
     const indexCreateParam: IndexCreateParam = {
       collection_name: collectionName,
       collection_name: collectionName,

+ 3 - 3
server/src/schema/dto.ts

@@ -100,9 +100,9 @@ export class GetIndexProgress {
     description: 'index name',
     description: 'index name',
   })
   })
   @IsString()
   @IsString()
-  @IsNotEmpty({
-    message: 'index_name is empty',
-  })
+  // @IsNotEmpty({
+  //   message: 'index_name is empty',
+  // })
   readonly index_name: string;
   readonly index_name: string;
 
 
   @ApiProperty({
   @ApiProperty({