Browse Source

Merge pull request #227 from nameczz/dev

Add ws for overview page
ryjiang 3 years ago
parent
commit
45b82fd280

+ 1 - 1
client/src/context/WebSocket.tsx

@@ -18,7 +18,6 @@ const { Provider } = webSokcetContext;
 export const WebSocketProvider = (props: { children: React.ReactNode }) => {
 export const WebSocketProvider = (props: { children: React.ReactNode }) => {
   const [collections, setCollections] = useState<CollectionView[]>([]);
   const [collections, setCollections] = useState<CollectionView[]>([]);
 
 
-  // test code for socket
   useEffect(() => {
   useEffect(() => {
     const socket = io(url);
     const socket = io(url);
 
 
@@ -52,6 +51,7 @@ export const WebSocketProvider = (props: { children: React.ReactNode }) => {
       }
       }
     });
     });
   }, []);
   }, []);
+
   return (
   return (
     <Provider
     <Provider
       value={{
       value={{

+ 22 - 5
client/src/pages/overview/Overview.tsx

@@ -3,13 +3,17 @@ import { useCallback, useContext, useEffect, useMemo, useState } from 'react';
 import { useTranslation } from 'react-i18next';
 import { useTranslation } from 'react-i18next';
 import EmptyCard from '../../components/cards/EmptyCard';
 import EmptyCard from '../../components/cards/EmptyCard';
 import icons from '../../components/icons/Icons';
 import icons from '../../components/icons/Icons';
+import { WS_EVENTS, WS_EVENTS_TYPE } from '../../consts/Http';
 import { LOADING_STATE } from '../../consts/Milvus';
 import { LOADING_STATE } from '../../consts/Milvus';
 import { rootContext } from '../../context/Root';
 import { rootContext } from '../../context/Root';
+import { webSokcetContext } from '../../context/WebSocket';
 import { useLoadAndReleaseDialogHook } from '../../hooks/Dialog';
 import { useLoadAndReleaseDialogHook } from '../../hooks/Dialog';
 import { useNavigationHook } from '../../hooks/Navigation';
 import { useNavigationHook } from '../../hooks/Navigation';
 import { CollectionHttp } from '../../http/Collection';
 import { CollectionHttp } from '../../http/Collection';
+import { MilvusHttp } from '../../http/Milvus';
 import { ALL_ROUTER_TYPES } from '../../router/Types';
 import { ALL_ROUTER_TYPES } from '../../router/Types';
 import { formatNumber } from '../../utils/Common';
 import { formatNumber } from '../../utils/Common';
+import { checkLoading, checkIndexBuilding } from '../../utils/Validation';
 import { CollectionData } from '../collections/Types';
 import { CollectionData } from '../collections/Types';
 import CollectionCard from './collectionCard/CollectionCard';
 import CollectionCard from './collectionCard/CollectionCard';
 import StatisticsCard from './statisticsCard/StatisticsCard';
 import StatisticsCard from './statisticsCard/StatisticsCard';
@@ -44,25 +48,38 @@ const Overview = () => {
   });
   });
   const [loading, setLoading] = useState(false);
   const [loading, setLoading] = useState(false);
 
 
-  const [loadCollections, setLoadCollections] = useState<CollectionHttp[]>([]);
+  const { collections, setCollections } = useContext(webSokcetContext);
+
   const { openSnackBar } = useContext(rootContext);
   const { openSnackBar } = useContext(rootContext);
 
 
   const fetchData = useCallback(async () => {
   const fetchData = useCallback(async () => {
     setLoading(true);
     setLoading(true);
     const res = await CollectionHttp.getStatistics();
     const res = await CollectionHttp.getStatistics();
     const collections = await CollectionHttp.getCollections();
     const collections = await CollectionHttp.getCollections();
-    const loadCollections = collections.filter(
-      c => c._status !== LOADING_STATE.UNLOADED
+    const hasLoadingOrBuildingCollection = collections.some(
+      v => checkLoading(v) || checkIndexBuilding(v)
     );
     );
+    // if some collection is building index or loading, start pulling data
+    if (hasLoadingOrBuildingCollection) {
+      MilvusHttp.triggerCron({
+        name: WS_EVENTS.COLLECTION,
+        type: WS_EVENTS_TYPE.START,
+      });
+    }
     setStatistics(res);
     setStatistics(res);
-    setLoadCollections(loadCollections);
+    setCollections(collections);
     setLoading(false);
     setLoading(false);
-  }, []);
+  }, [setCollections]);
 
 
   useEffect(() => {
   useEffect(() => {
     fetchData();
     fetchData();
   }, [fetchData]);
   }, [fetchData]);
 
 
+  const loadCollections = useMemo(
+    () => collections.filter(c => c._status !== LOADING_STATE.UNLOADED),
+    [collections]
+  );
+
   const fetchRelease = async (data: CollectionData) => {
   const fetchRelease = async (data: CollectionData) => {
     const name = data._name;
     const name = data._name;
     const res = await CollectionHttp.releaseCollection(name);
     const res = await CollectionHttp.releaseCollection(name);

+ 12 - 3
server/src/crons/crons.service.ts

@@ -21,8 +21,17 @@ export class CronsService {
 
 
   @Cron(CronExpression.EVERY_SECOND, { name: WS_EVENTS.COLLECTION })
   @Cron(CronExpression.EVERY_SECOND, { name: WS_EVENTS.COLLECTION })
   async getCollections() {
   async getCollections() {
-    const res = await this.collectionService.getAllCollections();
-    this.eventService.server.emit(WS_EVENTS.COLLECTION, res);
-    return res;
+    try {
+      const res = await this.collectionService.getAllCollections();
+      this.eventService.server.emit(WS_EVENTS.COLLECTION, res);
+      return res;
+    } catch (error) {
+      // When user not connect milvus, stop cron
+      this.toggleCronJobByName({
+        name: WS_EVENTS.COLLECTION,
+        type: WS_EVENTS_TYPE.STOP,
+      });
+      throw new Error(error);
+    }
   }
   }
 }
 }

+ 10 - 0
server/src/milvus/milvus.service.ts

@@ -19,21 +19,31 @@ export class MilvusService {
   }
   }
 
 
   get collectionManager() {
   get collectionManager() {
+    this.checkMilvus();
     return this.milvusClient.collectionManager;
     return this.milvusClient.collectionManager;
   }
   }
 
 
   get partitionManager() {
   get partitionManager() {
+    this.checkMilvus();
     return this.milvusClient.partitionManager;
     return this.milvusClient.partitionManager;
   }
   }
 
 
   get indexManager() {
   get indexManager() {
+    this.checkMilvus();
     return this.milvusClient.indexManager;
     return this.milvusClient.indexManager;
   }
   }
 
 
   get dataManager() {
   get dataManager() {
+    this.checkMilvus();
     return this.milvusClient.dataManager;
     return this.milvusClient.dataManager;
   }
   }
 
 
+  private checkMilvus() {
+    if (!this.milvusClient) {
+      throw new Error('Please connect milvus first');
+    }
+  }
+
   async connectMilvus(address: string) {
   async connectMilvus(address: string) {
     // grpc only need address without http
     // grpc only need address without http
     const milvusAddress = address.replace(/(http|https):\/\//, '');
     const milvusAddress = address.replace(/(http|https):\/\//, '');