Browse Source

fix count issue(part of)

Signed-off-by: ryjiang <jiangruiyi@gmail.com>
ryjiang 1 year ago
parent
commit
5245ee8f45

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

@@ -98,7 +98,7 @@ export const DataProvider = (props: { children: React.ReactNode }) => {
         return isLoading || isBuildingIndex;
       });
 
-      // trigger cron if it has
+      // trigger cron if it has to
       if (LoadingOrBuildingCollections.length > 0) {
         MilvusService.triggerCron({
           name: WS_EVENTS.COLLECTION_UPDATE,

+ 8 - 2
client/src/hooks/Query.ts

@@ -24,7 +24,7 @@ export const useQuery = (params: {
   // states
   const [currentPage, setCurrentPage] = useState<number>(0);
   const [pageSize, setPageSize] = useState<number>(0);
-  const [total, setTotal] = useState<number>(0);
+  const [total, setTotal] = useState<number>(collection.rowCount);
   const [expr, setExpr] = useState<string>('');
   const [queryResult, setQueryResult] = useState<any>({ data: [], latency: 0 });
 
@@ -68,6 +68,7 @@ export const useQuery = (params: {
     page: number = currentPage,
     consistency_level = consistencyLevel
   ) => {
+    if (!collection || !collection.loaded) return;
     const _expr = getPageExpr(page);
 
     onQueryStart(_expr);
@@ -120,6 +121,10 @@ export const useQuery = (params: {
   };
 
   const count = async (consistency_level = consistencyLevel) => {
+    if (!collection || !collection.loaded) {
+      setTotal(collection.rowCount);
+      return;
+    }
     const count = 'count(*)';
     const res = await CollectionService.queryData(collection.collection_name, {
       expr: expr,
@@ -140,6 +145,7 @@ export const useQuery = (params: {
   useEffect(() => {
     if (!collection || !collection.loaded) {
       setQueryResult({ data: [], latency: 0 });
+      setTotal(collection.rowCount);
       // console.info('[skip running query]: no key yet');
       return;
     } // reset
@@ -148,7 +154,7 @@ export const useQuery = (params: {
     count();
     // do the query
     query();
-  }, [expr, pageSize, collection]);
+  }, [expr, pageSize, collection.collection_name]);
 
   return {
     // total query count

+ 1 - 3
client/src/pages/databases/collections/Collections.tsx

@@ -430,9 +430,7 @@ const Collections = () => {
                   <ImportSampleDialog
                     collection={row}
                     cb={async (collectionName: string) => {
-                      setTimeout(async () => {
-                        await fetchCollection(collectionName);
-                      });
+                      await fetchCollection(collectionName);
                     }}
                   />
                 ),

+ 14 - 3
client/src/pages/databases/collections/data/CollectionData.tsx

@@ -1,7 +1,7 @@
 import { useState, useEffect, useRef, useContext } from 'react';
 import { TextField, Typography } from '@material-ui/core';
 import { useTranslation } from 'react-i18next';
-import { rootContext } from '@/context';
+import { rootContext, dataContext } from '@/context';
 import { DataService } from '@/http';
 import { useQuery } from '@/hooks';
 import { saveCsvAs } from '@/utils';
@@ -62,6 +62,7 @@ const CollectionData = (props: CollectionDataProps) => {
   // UI functions
   const { setDialog, handleCloseDialog, openSnackBar } =
     useContext(rootContext);
+  const { fetchCollection } = useContext(dataContext);
   // icons
   const ResetIcon = icons.refresh;
   // translations
@@ -159,6 +160,10 @@ const CollectionData = (props: CollectionDataProps) => {
     },
   });
 
+  const onInsert = async (collectionName: string) => {
+    await fetchCollection(collectionName);
+  };
+
   // Toolbar settings
   const toolbarConfigs: ToolBarConfig[] = [
     {
@@ -179,7 +184,7 @@ const CollectionData = (props: CollectionDataProps) => {
                 // user can't select partition on collection page, so default value is ''
                 defaultSelectedPartition={''}
                 collections={[collection!]}
-                onInsert={() => {}}
+                onInsert={onInsert}
               />
             ),
           },
@@ -195,7 +200,13 @@ const CollectionData = (props: CollectionDataProps) => {
           type: 'custom',
           params: {
             component: (
-              <ImportSampleDialog collection={collection!} cb={onDelete} />
+              <ImportSampleDialog
+                collection={collection!}
+                cb={async () => {
+                  await onInsert(collection.collection_name);
+                  await onDelete();
+                }}
+              />
             ),
           },
         });

+ 0 - 6
server/src/collections/collections.controller.ts

@@ -337,8 +337,6 @@ export class CollectionController {
     try {
       const limit = parseInt(resultLimit, 10);
       const page = isNaN(resultPage) ? 0 : parseInt(resultPage, 10);
-      // TODO: add page and limit to node SDK
-      // Here may raise "Error: 8 RESOURCE_EXHAUSTED: Received message larger than max"
       const result = await this.collectionsService.query(req.clientId, {
         collection_name: name,
         ...data,
@@ -346,10 +344,6 @@ export class CollectionController {
 
       // const queryResultList = result.data;
       const queryResultLength = result.data.length;
-      // const startNum = page * limit;
-      // const endNum = (page + 1) * limit;
-      // const slicedResult = queryResultList.slice(startNum, endNum);
-      // result.data = slicedResult;
       res.send({ ...result, limit, page, total: queryResultLength });
     } catch (error) {
       next(error);

+ 4 - 5
server/src/collections/collections.service.ts

@@ -342,12 +342,13 @@ export class CollectionsService {
     });
 
     // get collection statistic data
-    let collectionStatisticsRes;
+    let count: number;
 
     try {
-      collectionStatisticsRes = await this.getCollectionStatistics(clientId, {
+      const res = await this.count(clientId, {
         collection_name: collection.name,
       });
+      count = res.rowCount;
     } catch (e) {
       console.log('ignore getCollectionStatistics');
     }
@@ -384,9 +385,7 @@ export class CollectionsService {
     return {
       collection_name: collection.name,
       schema: collectionInfo.schema,
-      rowCount: Number(
-        (collectionStatisticsRes && collectionStatisticsRes.data.row_count) || 0
-      ),
+      rowCount: Number(count || 0),
       createdTime: parseInt(collectionInfo.created_utc_timestamp, 10),
       aliases: collectionInfo.aliases,
       description: collectionInfo.schema.description,