Browse Source

add load collection api and change milvus-node-sdk-dev to milvus2-node-sdk

nameczz 4 years ago
parent
commit
2ca20e2fa2

+ 1 - 1
server/package.json

@@ -30,7 +30,7 @@
     "@nestjs/swagger": "^4.8.0",
     "@nestjs/swagger": "^4.8.0",
     "@types/passport-jwt": "^3.0.5",
     "@types/passport-jwt": "^3.0.5",
     "@types/passport-local": "^1.0.33",
     "@types/passport-local": "^1.0.33",
-    "@zilliz/milvus-sdk-node-dev": "^0.1.4",
+    "@zilliz/milvus2-sdk-node": "^0.1.0",
     "class-transformer": "^0.4.0",
     "class-transformer": "^0.4.0",
     "class-validator": "^0.13.1",
     "class-validator": "^0.13.1",
     "passport": "^0.4.1",
     "passport": "^0.4.1",

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

@@ -6,19 +6,22 @@ import {
   Param,
   Param,
   Post,
   Post,
   Put,
   Put,
+  Query,
   UsePipes,
   UsePipes,
   ValidationPipe,
   ValidationPipe,
 } from '@nestjs/common';
 } from '@nestjs/common';
 import { CollectionsService } from './collections.service';
 import { CollectionsService } from './collections.service';
-import { CreateCollection } from './dto';
+import { CreateCollection, ShowCollections } from './dto';
 
 
 @Controller('collections')
 @Controller('collections')
 export class CollectionsController {
 export class CollectionsController {
   constructor(private collectionsService: CollectionsService) {}
   constructor(private collectionsService: CollectionsService) {}
 
 
   @Get()
   @Get()
-  async getCollections() {
-    return await this.collectionsService.showCollections();
+  async getCollections(@Query() data?: ShowCollections) {
+    return Number(data.type) === 1
+      ? await this.collectionsService.getLoadedColletions()
+      : await this.collectionsService.getAllCollections();
   }
   }
 
 
   @Get('statistics')
   @Get('statistics')

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

@@ -8,10 +8,14 @@ import {
   GetIndexStateReq,
   GetIndexStateReq,
   LoadCollectionReq,
   LoadCollectionReq,
   ReleaseLoadCollectionReq,
   ReleaseLoadCollectionReq,
-} from '@zilliz/milvus-sdk-node-dev/dist/milvus/types';
+} from '@zilliz/milvus2-sdk-node/dist/milvus/types';
 import { throwErrorFromSDK } from '../utils/Error';
 import { throwErrorFromSDK } from '../utils/Error';
 import { findKeyValue } from '../utils/Helper';
 import { findKeyValue } from '../utils/Helper';
 import { ROW_COUNT } from '../utils/Const';
 import { ROW_COUNT } from '../utils/Const';
+import {
+  ShowCollectionsReq,
+  ShowCollectionsType,
+} from '@zilliz/milvus2-sdk-node/dist/milvus/types/Collection';
 @Injectable()
 @Injectable()
 export class CollectionsService {
 export class CollectionsService {
   constructor(private milvusService: MilvusService) {}
   constructor(private milvusService: MilvusService) {}
@@ -20,8 +24,8 @@ export class CollectionsService {
     return this.milvusService.milvusClientGetter;
     return this.milvusService.milvusClientGetter;
   }
   }
 
 
-  async getCollectionNames() {
-    const res = await this.milvusClient.showCollections();
+  async getCollectionNames(data?: ShowCollectionsReq) {
+    const res = await this.milvusClient.showCollections(data);
     throwErrorFromSDK(res.status);
     throwErrorFromSDK(res.status);
     return res;
     return res;
   }
   }
@@ -78,9 +82,12 @@ export class CollectionsService {
    * Get all collections meta data
    * Get all collections meta data
    * @returns {id:string, collection_name:string, schema:Field[], autoID:boolean, rowCount: string}
    * @returns {id:string, collection_name:string, schema:Field[], autoID:boolean, rowCount: string}
    */
    */
-  async showCollections() {
+  async getAllCollections() {
     const data = [];
     const data = [];
     const res = await this.getCollectionNames();
     const res = await this.getCollectionNames();
+    const loadedCollections = await this.getCollectionNames({
+      type: ShowCollectionsType.InMemory,
+    });
     if (res.collection_names.length > 0) {
     if (res.collection_names.length > 0) {
       for (const name of res.collection_names) {
       for (const name of res.collection_names) {
         const collectionInfo = await this.describeCollection({
         const collectionInfo = await this.describeCollection({
@@ -96,6 +103,27 @@ export class CollectionsService {
           autoID: collectionInfo.schema.autoID,
           autoID: collectionInfo.schema.autoID,
           rowCount: findKeyValue(collectionStatistics.stats, ROW_COUNT),
           rowCount: findKeyValue(collectionStatistics.stats, ROW_COUNT),
           id: collectionInfo.collectionID,
           id: collectionInfo.collectionID,
+          isLoaded: loadedCollections.collection_names.includes(name),
+        });
+      }
+    }
+    return data;
+  }
+
+  async getLoadedColletions() {
+    const data = [];
+    const res = await this.getCollectionNames({
+      type: ShowCollectionsType.InMemory,
+    });
+    if (res.collection_names.length > 0) {
+      for (const [index, value] of res.collection_names.entries()) {
+        const collectionStatistics = await this.getCollectionStatistics({
+          collection_name: value,
+        });
+        data.push({
+          id: res.collection_ids[index],
+          collection_name: value,
+          rowCount: findKeyValue(collectionStatistics.stats, ROW_COUNT),
         });
         });
       }
       }
     }
     }

+ 12 - 1
server/src/collections/dto.ts

@@ -5,8 +5,12 @@ import {
   IsOptional,
   IsOptional,
   IsArray,
   IsArray,
   ArrayNotEmpty,
   ArrayNotEmpty,
+  IsEnum,
 } from 'class-validator';
 } from 'class-validator';
-import { FieldType } from '@zilliz/milvus-sdk-node-dev/dist/milvus/types/Collection'; // todo: need improve like export types in root file.
+import {
+  FieldType,
+  ShowCollectionsType,
+} from '@zilliz/milvus2-sdk-node/dist/milvus/types/Collection'; // todo: need improve like export types in root file.
 import { ApiProperty } from '@nestjs/swagger';
 import { ApiProperty } from '@nestjs/swagger';
 
 
 export class CreateCollection {
 export class CreateCollection {
@@ -30,3 +34,10 @@ export class CreateCollection {
   })
   })
   readonly fields: FieldType[];
   readonly fields: FieldType[];
 }
 }
+
+export class ShowCollections {
+  @ApiProperty({ enum: ShowCollectionsType })
+  @IsOptional()
+  @IsEnum(ShowCollectionsType, { message: 'Type allow all->0 inmemory->1' })
+  readonly type: ShowCollectionsType;
+}

+ 1 - 1
server/src/main.ts

@@ -6,6 +6,7 @@ async function bootstrap() {
   const app = await NestFactory.create(AppModule, {
   const app = await NestFactory.create(AppModule, {
     cors: true,
     cors: true,
   });
   });
+  app.setGlobalPrefix('/api/v1');
 
 
   const config = new DocumentBuilder()
   const config = new DocumentBuilder()
     .setTitle('Milvus admin')
     .setTitle('Milvus admin')
@@ -14,7 +15,6 @@ async function bootstrap() {
   const document = SwaggerModule.createDocument(app, config);
   const document = SwaggerModule.createDocument(app, config);
   SwaggerModule.setup('api', app, document);
   SwaggerModule.setup('api', app, document);
 
 
-  app.setGlobalPrefix('/api/v1');
   await app.listen(3000);
   await app.listen(3000);
 }
 }
 bootstrap();
 bootstrap();

+ 1 - 1
server/src/milvus/milvus.service.ts

@@ -1,5 +1,5 @@
 import { Injectable } from '@nestjs/common';
 import { Injectable } from '@nestjs/common';
-import { MilvusNode } from '@zilliz/milvus-sdk-node-dev';
+import { MilvusNode } from '@zilliz/milvus2-sdk-node';
 @Injectable()
 @Injectable()
 export class MilvusService {
 export class MilvusService {
   private milvusAddress: string;
   private milvusAddress: string;

+ 1 - 1
server/src/partitions/partitions.service.ts

@@ -7,7 +7,7 @@ import {
   LoadPartitionsReq,
   LoadPartitionsReq,
   ReleasePartitionsReq,
   ReleasePartitionsReq,
   ShowPartitionsReq,
   ShowPartitionsReq,
-} from '@zilliz/milvus-sdk-node-dev/dist/milvus/types'; // todo: need improve like export types in root file.
+} from '@zilliz/milvus2-sdk-node/dist/milvus/types'; // todo: need improve like export types in root file.
 import { throwErrorFromSDK } from 'src/utils/Error';
 import { throwErrorFromSDK } from 'src/utils/Error';
 import { findKeyValue } from '../utils/Helper';
 import { findKeyValue } from '../utils/Helper';
 import { ROW_COUNT } from '../utils/Const';
 import { ROW_COUNT } from '../utils/Const';

+ 1 - 1
server/src/schema/schema.service.ts

@@ -5,7 +5,7 @@ import {
   DropIndexReq,
   DropIndexReq,
   GetIndexBuildProgressReq,
   GetIndexBuildProgressReq,
   GetIndexStateReq,
   GetIndexStateReq,
-} from '@zilliz/milvus-sdk-node-dev/dist/milvus/types';
+} from '@zilliz/milvus2-sdk-node/dist/milvus/types';
 import { throwErrorFromSDK } from 'src/utils/Error';
 import { throwErrorFromSDK } from 'src/utils/Error';
 import { MilvusService } from '../milvus/milvus.service';
 import { MilvusService } from '../milvus/milvus.service';
 
 

+ 1 - 1
server/src/utils/Error.ts

@@ -1,7 +1,7 @@
 import {
 import {
   ErrorCode,
   ErrorCode,
   ResStatus,
   ResStatus,
-} from '@zilliz/milvus-sdk-node-dev/dist/milvus/types/Response';
+} from '@zilliz/milvus2-sdk-node/dist/milvus/types/Response';
 
 
 export const throwErrorFromSDK = (res: ResStatus) => {
 export const throwErrorFromSDK = (res: ResStatus) => {
   if (res.error_code !== ErrorCode.SUCCESS) {
   if (res.error_code !== ErrorCode.SUCCESS) {

+ 1 - 1
server/src/utils/Helper.ts

@@ -1,4 +1,4 @@
-import { KeyValuePair } from '@zilliz/milvus-sdk-node-dev/dist/milvus/types/Common';
+import { KeyValuePair } from '@zilliz/milvus2-sdk-node/dist/milvus/types/Common';
 
 
 export const findKeyValue = (obj: KeyValuePair[], key: string) =>
 export const findKeyValue = (obj: KeyValuePair[], key: string) =>
   obj.find((v) => v.key === key)?.value;
   obj.find((v) => v.key === key)?.value;

+ 4 - 4
server/yarn.lock

@@ -1287,10 +1287,10 @@
   resolved "https://registry.yarnpkg.com/@xtuc/long/-/long-4.2.2.tgz#d291c6a4e97989b5c61d9acf396ae4fe133a718d"
   resolved "https://registry.yarnpkg.com/@xtuc/long/-/long-4.2.2.tgz#d291c6a4e97989b5c61d9acf396ae4fe133a718d"
   integrity sha512-NuHqBY1PB/D8xU6s/thBgOAiAP7HOYDQ32+BFZILJ8ivkUkAHQnWfn6WhL79Owj1qmUnoN/YPhktdIoucipkAQ==
   integrity sha512-NuHqBY1PB/D8xU6s/thBgOAiAP7HOYDQ32+BFZILJ8ivkUkAHQnWfn6WhL79Owj1qmUnoN/YPhktdIoucipkAQ==
 
 
-"@zilliz/milvus-sdk-node-dev@^0.1.4":
-  version "0.1.4"
-  resolved "https://registry.yarnpkg.com/@zilliz/milvus-sdk-node-dev/-/milvus-sdk-node-dev-0.1.4.tgz#858e83b0ba0a309cbdf0b87e313b3b41de0052a4"
-  integrity sha512-4REU6TxqjsIZ7a7mL1M19IPg3oBBfE9kJmC7XUmcFz1+F2fYWpl7Iw4R8g69trpzUNnaFIBgwjAnSb0oVrRLeQ==
+"@zilliz/milvus2-sdk-node@^0.1.0":
+  version "0.1.0"
+  resolved "https://registry.yarnpkg.com/@zilliz/milvus2-sdk-node/-/milvus2-sdk-node-0.1.0.tgz#feab0f956a84f73e228d40ce4efc358e86c97e60"
+  integrity sha512-sehxvOndtqFknKdHYSaoks1kZo5q8t72mHqWiduIwsuIsCkpQMoOUyHOpWXvakWajrKrfksIdgn2oaUj5/JcBQ==
   dependencies:
   dependencies:
     "@grpc/grpc-js" "^1.2.12"
     "@grpc/grpc-js" "^1.2.12"
     "@grpc/proto-loader" "^0.6.0"
     "@grpc/proto-loader" "^0.6.0"