Browse Source

update collections sort method

Signed-off-by: ryjiang <jiangruiyi@gmail.com>
ryjiang 1 year ago
parent
commit
2216b29c9d
2 changed files with 26 additions and 6 deletions
  1. 15 2
      client/src/context/Data.tsx
  2. 11 4
      server/src/collections/collections.service.ts

+ 15 - 2
client/src/context/Data.tsx

@@ -172,8 +172,21 @@ export const DataProvider = (props: { children: React.ReactNode }) => {
   const createCollection = async (data: any) => {
     // create collection
     const newCollection = await CollectionService.createCollection(data);
-    // inset collection to state
-    setCollections(prev => [...prev, newCollection]);
+
+    // combine new collection with old collections
+    // sort state by createdTime.
+    const newCollections = collections.concat(newCollection).sort((a, b) => {
+      if (a.loadedPercentage === b.loadedPercentage && a.schema && b.schema) {
+        if (a.schema.hasVectorIndex === b.schema.hasVectorIndex) {
+          return b.createdTime - a.createdTime;
+        }
+        return a.schema.hasVectorIndex ? -1 : 1;
+      }
+      return (b.loadedPercentage || 0) - (a.loadedPercentage || 0);
+    });
+
+    // update collection
+    setCollections(newCollections);
 
     return newCollection;
   };

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

@@ -422,10 +422,6 @@ export class CollectionsService {
 
     // data container
     const data: CollectionObject[] = [];
-    // sort by created time
-    allCollections.data.sort(
-      (a, b) => Number(b.timestamp) - Number(a.timestamp)
-    );
 
     // get target collections details
     const targetCollections = allCollections.data.filter(
@@ -475,6 +471,17 @@ export class CollectionsService {
       }, 5);
     }
 
+    // sort data by loadedPercentage and has index or not, then createdTime.
+    data.sort((a, b) => {
+      if (a.loadedPercentage === b.loadedPercentage && a.schema && b.schema) {
+        if (a.schema.hasVectorIndex === b.schema.hasVectorIndex) {
+          return b.createdTime - a.createdTime;
+        }
+        return a.schema.hasVectorIndex ? -1 : 1;
+      }
+      return (b.loadedPercentage || 0) - (a.loadedPercentage || 0);
+    });
+
     // return data
     return data;
   }