partitions.controller.ts 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import { NextFunction, Request, Response, Router } from 'express';
  2. import { dtoValidationMiddleware } from '../middlewares/validation';
  3. import { PartitionsService } from './partitions.service';
  4. import { milvusService } from '../milvus';
  5. import {
  6. GetPartitionsInfoDto,
  7. ManagePartitionDto,
  8. LoadPartitionsDto,
  9. } from './dto';
  10. export class PartitionController {
  11. private router: Router;
  12. private partitionsService: PartitionsService;
  13. constructor() {
  14. this.partitionsService = new PartitionsService(milvusService);
  15. this.router = Router();
  16. }
  17. generateRoutes() {
  18. this.router.get(
  19. '/',
  20. dtoValidationMiddleware(GetPartitionsInfoDto),
  21. this.getPartitionsInfo.bind(this)
  22. );
  23. this.router.post(
  24. '/',
  25. dtoValidationMiddleware(ManagePartitionDto),
  26. this.managePartition.bind(this)
  27. );
  28. this.router.post(
  29. '/load',
  30. dtoValidationMiddleware(LoadPartitionsDto),
  31. this.loadPartition.bind(this)
  32. );
  33. this.router.post(
  34. '/release',
  35. dtoValidationMiddleware(LoadPartitionsDto),
  36. this.releasePartition.bind(this)
  37. );
  38. return this.router;
  39. }
  40. async getPartitionsInfo(req: Request, res: Response, next: NextFunction) {
  41. const collectionName = '' + req.query?.collection_name;
  42. try {
  43. const result = await this.partitionsService.getPartitionsInfo({
  44. collection_name: collectionName,
  45. });
  46. res.send(result);
  47. } catch (error) {
  48. next(error);
  49. }
  50. }
  51. async managePartition(req: Request, res: Response, next: NextFunction) {
  52. const { type, ...params } = req.body;
  53. try {
  54. const result =
  55. type.toLocaleLowerCase() === 'create'
  56. ? await this.partitionsService.createPartition(params)
  57. : await this.partitionsService.deletePartition(params);
  58. res.send(result);
  59. } catch (error) {
  60. next(error);
  61. }
  62. }
  63. async loadPartition(req: Request, res: Response, next: NextFunction) {
  64. const data = req.body;
  65. try {
  66. const result = await this.partitionsService.loadPartitions(data);
  67. res.send(result);
  68. } catch (error) {
  69. next(error);
  70. }
  71. }
  72. async releasePartition(req: Request, res: Response, next: NextFunction) {
  73. const data = req.body;
  74. try {
  75. const result = await this.partitionsService.releasePartitions(data);
  76. res.send(result);
  77. } catch (error) {
  78. next(error);
  79. }
  80. }
  81. }