schema.controller.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import { NextFunction, Request, Response, Router } from 'express';
  2. import { dtoValidationMiddleware } from '../middlewares/validation';
  3. import { SchemaService } from './schema.service';
  4. import { milvusService } from '../milvus';
  5. import { ManageIndexDto } from './dto';
  6. export class SchemaController {
  7. private router: Router;
  8. private schemaService: SchemaService;
  9. constructor() {
  10. this.schemaService = new SchemaService(milvusService);
  11. this.router = Router();
  12. }
  13. generateRoutes() {
  14. this.router.post(
  15. '/index',
  16. dtoValidationMiddleware(ManageIndexDto),
  17. this.manageIndex.bind(this)
  18. );
  19. this.router.get('/index', this.describeIndex.bind(this));
  20. this.router.get('/index/progress', this.getIndexBuildProgress.bind(this));
  21. this.router.get('/index/state', this.getIndexState.bind(this));
  22. return this.router;
  23. }
  24. async manageIndex(req: Request, res: Response, next: NextFunction) {
  25. const { type, collection_name, index_name, extra_params, field_name } =
  26. req.body;
  27. try {
  28. const result =
  29. type.toLocaleLowerCase() === 'create'
  30. ? await this.schemaService.createIndex({
  31. collection_name,
  32. extra_params,
  33. field_name,
  34. index_name,
  35. })
  36. : await this.schemaService.dropIndex({
  37. collection_name,
  38. field_name,
  39. index_name,
  40. });
  41. res.send(result);
  42. } catch (error) {
  43. next(error);
  44. }
  45. }
  46. async describeIndex(req: Request, res: Response, next: NextFunction) {
  47. const data = '' + req.query?.collection_name;
  48. try {
  49. const result = await this.schemaService.describeIndex({
  50. collection_name: data,
  51. });
  52. res.send(result);
  53. } catch (error) {
  54. next(error);
  55. }
  56. }
  57. async getIndexBuildProgress(req: Request, res: Response, next: NextFunction) {
  58. const collection_name = '' + req.query?.collection_name;
  59. const index_name = '' + req.query?.index_name;
  60. const field_name = '' + req.query?.field_name;
  61. try {
  62. const result = await this.schemaService.getIndexBuildProgress({
  63. collection_name,
  64. index_name,
  65. field_name,
  66. });
  67. res.send(result);
  68. } catch (error) {
  69. next(error);
  70. }
  71. }
  72. async getIndexState(req: Request, res: Response, next: NextFunction) {
  73. const collection_name = '' + req.query?.collection_name;
  74. const index_name = '' + req.query?.index_name;
  75. const field_name = '' + req.query?.field_name;
  76. try {
  77. const result = await this.schemaService.getIndexState({
  78. collection_name,
  79. index_name,
  80. field_name,
  81. });
  82. res.send(result);
  83. } catch (error) {
  84. next(error);
  85. }
  86. }
  87. }