Browse Source

add index status

nameczz 4 years ago
parent
commit
01b507b921

+ 6 - 1
client/src/components/status/Types.ts

@@ -7,7 +7,12 @@ export type StatusType = {
   status: StatusEnum;
 };
 
-export type ChildrenStatusType = 'creating' | 'finish' | 'error';
+// @todo need rename
+export enum ChildrenStatusType {
+  CREATING = 'creating',
+  FINISH = 'finish',
+  ERROR = 'error',
+}
 
 export type StatusIconType = {
   type: ChildrenStatusType;

+ 8 - 3
client/src/context/Auth.tsx

@@ -19,9 +19,14 @@ export const AuthProvider = (props: { children: React.ReactNode }) => {
   useEffect(() => {
     const check = async () => {
       const milvusAddress = window.localStorage.getItem(MILVUS_ADDRESS) || '';
-      const res = await MilvusHttp.check(milvusAddress);
-      setAddress(res.data.connected ? milvusAddress : '');
-      if (!res.data.connected) {
+      try {
+        const res = await MilvusHttp.check(milvusAddress);
+        setAddress(res.data.connected ? milvusAddress : '');
+        if (!res.data.connected) {
+          window.localStorage.removeItem(MILVUS_ADDRESS);
+        }
+      } catch (error) {
+        setAddress('');
         window.localStorage.removeItem(MILVUS_ADDRESS);
       }
     };

+ 23 - 1
client/src/http/Collection.ts

@@ -1,5 +1,6 @@
-import { StatusEnum } from '../components/status/Types';
+import { ChildrenStatusType, StatusEnum } from '../components/status/Types';
 import { CollectionView } from '../pages/collections/Types';
+import { IndexState } from '../types/Milvus';
 import BaseModel from './BaseModel';
 
 export class CollectionHttp extends BaseModel implements CollectionView {
@@ -7,8 +8,11 @@ export class CollectionHttp extends BaseModel implements CollectionView {
   private collection_name!: string;
   private description!: string;
   private rowCount!: string;
+  private index_status!: string;
 
   static COLLECTIONS_URL = '/collections';
+  static COLLECTIONS_INDEX_STATUS_URL = '/collections/indexes/status';
+
   static CHECK_URL = '/milvus/check';
 
   constructor(props: CollectionView) {
@@ -20,6 +24,13 @@ export class CollectionHttp extends BaseModel implements CollectionView {
     return super.findAll({ path: this.COLLECTIONS_URL, params: {} });
   }
 
+  static getCollectionsIndexState(): Promise<CollectionHttp[]> {
+    return super.findAll({
+      path: this.COLLECTIONS_INDEX_STATUS_URL,
+      params: {},
+    });
+  }
+
   get _autoId() {
     return this.autoID;
   }
@@ -43,4 +54,15 @@ export class CollectionHttp extends BaseModel implements CollectionView {
   get _status() {
     return StatusEnum.loaded;
   }
+
+  get _indexState() {
+    switch (this.index_status) {
+      case IndexState.InProgress:
+        return ChildrenStatusType.CREATING;
+      case IndexState.Failed:
+        return ChildrenStatusType.ERROR;
+      default:
+        return ChildrenStatusType.FINISH;
+    }
+  }
 }

+ 9 - 2
client/src/pages/collections/Collections.tsx

@@ -10,7 +10,7 @@ import icons from '../../components/icons/Icons';
 import EmptyCard from '../../components/cards/EmptyCard';
 import Status from '../../components/status/Status';
 import { useTranslation } from 'react-i18next';
-import { StatusEnum } from '../../components/status/Types';
+import { ChildrenStatusType, StatusEnum } from '../../components/status/Types';
 import { makeStyles, Theme, Link, Typography } from '@material-ui/core';
 import StatusIcon from '../../components/status/StatusIcon';
 import CustomToolTip from '../../components/customToolTip/CustomToolTip';
@@ -66,8 +66,11 @@ const Collections = () => {
 
   const fetchData = async () => {
     const res = await CollectionHttp.getCollections();
+    const statusRes = await CollectionHttp.getCollectionsIndexState();
     setCollections(
       res.map(v => {
+        const indexStatus = statusRes.find(item => item._name === v._name);
+        console.log(indexStatus);
         Object.assign(v, {
           nameElement: (
             <Link href="/overview" underline="always" color="textPrimary">
@@ -75,7 +78,11 @@ const Collections = () => {
             </Link>
           ),
           statusElement: <Status status={v._status} />,
-          indexCreatingElement: <StatusIcon type="creating" />,
+          indexCreatingElement: (
+            <StatusIcon
+              type={indexStatus?._indexState || ChildrenStatusType.FINISH}
+            />
+          ),
         });
 
         return v;

+ 2 - 1
client/src/pages/collections/Types.ts

@@ -1,5 +1,5 @@
 import { Dispatch, ReactElement, SetStateAction } from 'react';
-import { StatusEnum } from '../../components/status/Types';
+import { ChildrenStatusType, StatusEnum } from '../../components/status/Types';
 
 export interface CollectionData {
   _name: string;
@@ -7,6 +7,7 @@ export interface CollectionData {
   _status: StatusEnum;
   _rowCount: string;
   _desc: string;
+  _indexState: ChildrenStatusType;
 }
 
 export interface CollectionView extends CollectionData {

+ 7 - 0
client/src/types/Milvus.ts

@@ -0,0 +1,7 @@
+export enum IndexState {
+  IndexStateNone = 'IndexStateNone',
+  Unissued = 'Unissued',
+  InProgress = 'InProgress',
+  Finished = 'Finished',
+  Failed = 'Failed',
+}

+ 2 - 2
server/src/collections/collections.service.ts

@@ -105,8 +105,8 @@ export class CollectionsService {
       for (const name of res.collection_names) {
         const indexRes = await this.getIndexStatus({ collection_name: name });
         data.push({
-          collectionName: name,
-          indexState: indexRes.state,
+          collection_name: name,
+          index_state: indexRes.state,
         });
       }
     }

+ 6 - 2
server/src/milvus/milvus.service.ts

@@ -22,10 +22,10 @@ export class MilvusService {
   async connectMilvus(address: string) {
     try {
       this.milvusClient = new MilvusNode(address);
-      this.milvusAddress = address;
       await this.milvusClient.hasCollection({
         collection_name: 'not_exist',
       });
+      this.milvusAddress = address;
       return { address: this.milvusAddress };
     } catch (error) {
       throw new Error('Connect milvus failed, check your milvus address.');
@@ -33,8 +33,12 @@ export class MilvusService {
   }
 
   async checkConnect(address: string) {
+    if (address !== this.milvusAddress) {
+      return { connected: false };
+    }
+    const res = await this.connectMilvus(address);
     return {
-      connected: this.milvusAddress ? this.milvusAddress === address : false,
+      connected: res.address ? true : false,
     };
   }
 }