collections.controller.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. import {
  2. Body,
  3. Controller,
  4. Delete,
  5. Get,
  6. Param,
  7. Post,
  8. Put,
  9. Query,
  10. UsePipes,
  11. ValidationPipe,
  12. // CACHE_MANAGER,
  13. // Inject,
  14. // UseInterceptors,
  15. // CacheInterceptor,
  16. } from '@nestjs/common';
  17. // import { Cache } from 'cache-manager';
  18. import { ApiTags } from '@nestjs/swagger';
  19. import { CollectionsService } from './collections.service';
  20. import {
  21. CreateCollection,
  22. InsertData,
  23. ShowCollections,
  24. VectorSearch,
  25. } from './dto';
  26. // import { cacheKeys } from '../cache/config';
  27. //Including 2 kind of cache contorl, check getCollections and getStatistics for detail
  28. @ApiTags('collections')
  29. @Controller('collections')
  30. export class CollectionsController {
  31. constructor(
  32. private collectionsService: CollectionsService, // @Inject(CACHE_MANAGER) private cacheManager: Cache,
  33. ) {}
  34. // manually control cache if logic is complicated
  35. @Get()
  36. async getCollections(@Query() data?: ShowCollections) {
  37. // if (Number(data.type) === 1) {
  38. // let loadedCollections = await this.cacheManager.get(
  39. // cacheKeys.LOADEDCOLLECTIONS,
  40. // );
  41. // if (loadedCollections) {
  42. // return loadedCollections;
  43. // }
  44. // loadedCollections = await this.collectionsService.getLoadedColletions();
  45. // await this.cacheManager.set(
  46. // cacheKeys.LOADEDCOLLECTIONS,
  47. // loadedCollections,
  48. // );
  49. // return loadedCollections;
  50. // }
  51. // let allCollections = await this.cacheManager.get(cacheKeys.ALLCOLLECTIONS);
  52. // if (allCollections) {
  53. // return allCollections;
  54. // }
  55. // allCollections = await this.collectionsService.getAllCollections();
  56. // await this.cacheManager.set(cacheKeys.ALLCOLLECTIONS, allCollections);
  57. // return allCollections;
  58. return Number(data.type === 1)
  59. ? await this.collectionsService.getLoadedColletions()
  60. : await this.collectionsService.getAllCollections();
  61. }
  62. // use interceptor to control cache automatically
  63. @Get('statistics')
  64. // @UseInterceptors(CacheInterceptor)
  65. async getStatistics() {
  66. return await this.collectionsService.getStatistics();
  67. }
  68. @Post()
  69. @UsePipes(new ValidationPipe())
  70. async createCollection(@Body() data: CreateCollection) {
  71. // await this.cacheManager.del(cacheKeys.ALLCOLLECTIONS);
  72. return await this.collectionsService.createCollection(data);
  73. }
  74. @Delete(':name')
  75. async deleteCollection(@Param('name') name: string) {
  76. // await this.cacheManager.del(cacheKeys.ALLCOLLECTIONS);
  77. return await this.collectionsService.dropCollection({
  78. collection_name: name,
  79. });
  80. }
  81. @Get(':name')
  82. async describeCollection(@Param('name') name: string) {
  83. return await this.collectionsService.describeCollection({
  84. collection_name: name,
  85. });
  86. }
  87. @Get(':name/statistics')
  88. async getCollectionStatistics(@Param('name') name: string) {
  89. return await this.collectionsService.getCollectionStatistics({
  90. collection_name: name,
  91. });
  92. }
  93. @Get('indexes/status')
  94. async getCollectionsIndexState() {
  95. return await this.collectionsService.getCollectionsIndexStatus();
  96. }
  97. @Put(':name/load')
  98. async loadCollection(@Param('name') name: string) {
  99. // await this.cacheManager.del(cacheKeys.LOADEDCOLLECTIONS);
  100. return await this.collectionsService.loadCollection({
  101. collection_name: name,
  102. });
  103. }
  104. @Put(':name/release')
  105. async releaseCollection(@Param('name') name: string) {
  106. // await this.cacheManager.del(cacheKeys.LOADEDCOLLECTIONS);
  107. return await this.collectionsService.releaseCollection({
  108. collection_name: name,
  109. });
  110. }
  111. @Post(':name/insert')
  112. async insertData(@Param('name') name: string, @Body() data: InsertData) {
  113. // await this.cacheManager.del(cacheKeys.ALLCOLLECTIONS);
  114. return await this.collectionsService.insert({
  115. collection_name: name,
  116. ...data,
  117. });
  118. }
  119. @Post(':name/search')
  120. async vectorSearch(@Param('name') name: string, @Body() data: VectorSearch) {
  121. return await this.collectionsService.vectorSearch({
  122. collection_name: name,
  123. ...data,
  124. });
  125. }
  126. }