collections.controller.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 check getCollections and getStatistics for detail
  28. @ApiTags('collections')
  29. @Controller('collections')
  30. export class CollectionsController {
  31. constructor(private collectionsService: CollectionsService, @Inject(CACHE_MANAGER) private cacheManager: Cache) { }
  32. // manually control cache if logic is complicated
  33. @Get()
  34. async getCollections(@Query() data?: ShowCollections) {
  35. if (Number(data.type) === 1) {
  36. let loadedCollections = await this.cacheManager.get(cacheKeys.LOADEDCOLLECTIONS);
  37. if (loadedCollections) {
  38. return loadedCollections;
  39. }
  40. loadedCollections = await this.collectionsService.getLoadedColletions();
  41. await this.cacheManager.set(cacheKeys.LOADEDCOLLECTIONS, loadedCollections);
  42. return loadedCollections;
  43. }
  44. let allCollections = await this.cacheManager.get(cacheKeys.ALLCOLLECTIONS);
  45. if (allCollections) {
  46. return allCollections;
  47. }
  48. allCollections = await this.collectionsService.getAllCollections();
  49. await this.cacheManager.set(cacheKeys.ALLCOLLECTIONS, allCollections);
  50. return allCollections;
  51. }
  52. // use interceptor to control cache automatically
  53. @Get('statistics')
  54. @UseInterceptors(CacheInterceptor)
  55. async getStatistics() {
  56. return await this.collectionsService.getStatistics();
  57. }
  58. @Post()
  59. @UsePipes(new ValidationPipe())
  60. async createCollection(@Body() data: CreateCollection) {
  61. await this.cacheManager.del(cacheKeys.ALLCOLLECTIONS);
  62. return await this.collectionsService.createCollection(data);
  63. }
  64. @Delete(':name')
  65. // todo: need check some special symbols
  66. async deleteCollection(@Param('name') name: string) {
  67. await this.cacheManager.del(cacheKeys.ALLCOLLECTIONS);
  68. return await this.collectionsService.dropCollection({
  69. collection_name: name,
  70. });
  71. }
  72. @Get(':name')
  73. async describeCollection(@Param('name') name: string) {
  74. return await this.collectionsService.describeCollection({
  75. collection_name: name,
  76. });
  77. }
  78. @Get(':name/statistics')
  79. async getCollectionStatistics(@Param('name') name: string) {
  80. return await this.collectionsService.getCollectionStatistics({
  81. collection_name: name,
  82. });
  83. }
  84. @Get('indexes/status')
  85. async getCollectionsIndexState() {
  86. return await this.collectionsService.getCollectionsIndexStatus();
  87. }
  88. @Put(':name/load')
  89. async loadCollection(@Param('name') name: string) {
  90. await this.cacheManager.del(cacheKeys.LOADEDCOLLECTIONS);
  91. return await this.collectionsService.loadCollection({
  92. collection_name: name,
  93. });
  94. }
  95. @Put(':name/release')
  96. async releaseCollection(@Param('name') name: string) {
  97. await this.cacheManager.del(cacheKeys.LOADEDCOLLECTIONS);
  98. return await this.collectionsService.releaseCollection({
  99. collection_name: name,
  100. });
  101. }
  102. @Post(':name/insert')
  103. async insertData(@Param('name') name: string, @Body() data: InsertData) {
  104. await this.cacheManager.del(cacheKeys.ALLCOLLECTIONS);
  105. return await this.collectionsService.insert({
  106. collection_name: name,
  107. ...data,
  108. });
  109. }
  110. @Post(':name/search')
  111. async vectorSearch(@Param('name') name: string, @Body() data: VectorSearch) {
  112. return await this.collectionsService.vectorSearch({
  113. collection_name: name,
  114. ...data,
  115. });
  116. }
  117. }