partitions.service.ts 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import {
  2. CreatePartitionReq,
  3. DropPartitionReq,
  4. GetPartitionStatisticsReq,
  5. LoadPartitionsReq,
  6. ReleasePartitionsReq,
  7. ShowPartitionsReq,
  8. } from '@zilliz/milvus2-sdk-node';
  9. import { throwErrorFromSDK } from '../utils/Error';
  10. import { findKeyValue } from '../utils/Helper';
  11. import { ROW_COUNT } from '../utils';
  12. import { clientCache } from '../app';
  13. import { PartitionData } from '../types';
  14. export class PartitionsService {
  15. async getPartitionsInfo(
  16. clientId: string,
  17. data: ShowPartitionsReq
  18. ): Promise<PartitionData[]> {
  19. const result = [];
  20. const res = await this.getPartitions(clientId, data);
  21. if (res.partition_names && res.partition_names.length) {
  22. for (const [index, name] of res.partition_names.entries()) {
  23. const statistics = await this.getPartitionStatistics(clientId, {
  24. ...data,
  25. partition_name: name,
  26. });
  27. result.push({
  28. name,
  29. id: res.partitionIDs[index],
  30. rowCount: findKeyValue(statistics.stats, ROW_COUNT),
  31. createdTime: res.created_utc_timestamps[index],
  32. });
  33. }
  34. }
  35. return result;
  36. }
  37. async getPartitions(clientId: string, data: ShowPartitionsReq) {
  38. const { milvusClient } = clientCache.get(clientId);
  39. const res = await milvusClient.showPartitions(data);
  40. throwErrorFromSDK(res.status);
  41. return res;
  42. }
  43. async createPartition(clientId: string, data: CreatePartitionReq) {
  44. const { milvusClient } = clientCache.get(clientId);
  45. const res = await milvusClient.createPartition(data);
  46. throwErrorFromSDK(res);
  47. return res;
  48. }
  49. async deletePartition(clientId: string, data: DropPartitionReq) {
  50. const { milvusClient } = clientCache.get(clientId);
  51. const res = await milvusClient.dropPartition(data);
  52. throwErrorFromSDK(res);
  53. return res;
  54. }
  55. async getPartitionStatistics(
  56. clientId: string,
  57. data: GetPartitionStatisticsReq
  58. ) {
  59. const { milvusClient } = clientCache.get(clientId);
  60. const res = await milvusClient.getPartitionStatistics(data);
  61. throwErrorFromSDK(res.status);
  62. return res;
  63. }
  64. async loadPartitions(clientId: string, data: LoadPartitionsReq) {
  65. const { milvusClient } = clientCache.get(clientId);
  66. const res = await milvusClient.loadPartitions(data);
  67. throwErrorFromSDK(res);
  68. return res;
  69. }
  70. async releasePartitions(clientId: string, data: ReleasePartitionsReq) {
  71. const { milvusClient } = clientCache.get(clientId);
  72. const res = await milvusClient.releasePartitions(data);
  73. throwErrorFromSDK(res);
  74. return res;
  75. }
  76. }