collections.service.ts 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. import { Injectable } from '@nestjs/common';
  2. import { MilvusService } from '../milvus/milvus.service';
  3. import {
  4. CreateCollectionReq,
  5. DescribeCollectionReq,
  6. DropCollectionReq,
  7. GetCollectionStatisticsReq,
  8. GetIndexStateReq,
  9. LoadCollectionReq,
  10. ReleaseLoadCollectionReq,
  11. } from '@zilliz/milvus2-sdk-node/dist/milvus/types';
  12. import { throwErrorFromSDK } from '../utils/Error';
  13. import { findKeyValue } from '../utils/Helper';
  14. import { ROW_COUNT } from '../utils/Const';
  15. import {
  16. ShowCollectionsReq,
  17. ShowCollectionsType,
  18. } from '@zilliz/milvus2-sdk-node/dist/milvus/types/Collection';
  19. @Injectable()
  20. export class CollectionsService {
  21. constructor(private milvusService: MilvusService) {}
  22. get milvusClient() {
  23. return this.milvusService.milvusClientGetter;
  24. }
  25. async getCollectionNames(data?: ShowCollectionsReq) {
  26. const res = await this.milvusClient.showCollections(data);
  27. throwErrorFromSDK(res.status);
  28. return res;
  29. }
  30. async createCollection(data: CreateCollectionReq) {
  31. const res = await this.milvusClient.createCollection(data);
  32. throwErrorFromSDK(res);
  33. return res;
  34. }
  35. async describeCollection(data: DescribeCollectionReq) {
  36. const res = await this.milvusClient.describeCollection(data);
  37. throwErrorFromSDK(res.status);
  38. return res;
  39. }
  40. async dropCollection(data: DropCollectionReq) {
  41. const res = await this.milvusClient.dropCollection(data);
  42. throwErrorFromSDK(res);
  43. return res;
  44. }
  45. async loadCollection(data: LoadCollectionReq) {
  46. const res = await this.milvusClient.loadCollection(data);
  47. throwErrorFromSDK(res);
  48. return res;
  49. }
  50. async releaseCollection(data: ReleaseLoadCollectionReq) {
  51. const res = await this.milvusClient.releaseCollection(data);
  52. throwErrorFromSDK(res);
  53. return res;
  54. }
  55. async getCollectionStatistics(data: GetCollectionStatisticsReq) {
  56. const res = await this.milvusClient.getCollectionStatistics(data);
  57. throwErrorFromSDK(res.status);
  58. return res;
  59. }
  60. /**
  61. * We do not throw error for this.
  62. * Because if collection dont have index, it will throw error.
  63. * We need wait for milvus error code.
  64. * @param data
  65. * @returns
  66. */
  67. async getIndexStatus(data: GetIndexStateReq) {
  68. const res = await this.milvusClient.getIndexState(data);
  69. return res;
  70. }
  71. /**
  72. * Get all collections meta data
  73. * @returns {id:string, collection_name:string, schema:Field[], autoID:boolean, rowCount: string}
  74. */
  75. async getAllCollections() {
  76. const data = [];
  77. const res = await this.getCollectionNames();
  78. const loadedCollections = await this.getCollectionNames({
  79. type: ShowCollectionsType.Loaded,
  80. });
  81. if (res.collection_names.length > 0) {
  82. for (const name of res.collection_names) {
  83. const collectionInfo = await this.describeCollection({
  84. collection_name: name,
  85. });
  86. const collectionStatistics = await this.getCollectionStatistics({
  87. collection_name: name,
  88. });
  89. data.push({
  90. collection_name: name,
  91. schema: collectionInfo.schema,
  92. description: collectionInfo.schema.description,
  93. autoID: collectionInfo.schema.autoID,
  94. rowCount: findKeyValue(collectionStatistics.stats, ROW_COUNT),
  95. id: collectionInfo.collectionID,
  96. isLoaded: loadedCollections.collection_names.includes(name),
  97. });
  98. }
  99. }
  100. return data;
  101. }
  102. async getLoadedColletions() {
  103. const data = [];
  104. const res = await this.getCollectionNames({
  105. type: ShowCollectionsType.Loaded,
  106. });
  107. if (res.collection_names.length > 0) {
  108. for (const [index, value] of res.collection_names.entries()) {
  109. const collectionStatistics = await this.getCollectionStatistics({
  110. collection_name: value,
  111. });
  112. data.push({
  113. id: res.collection_ids[index],
  114. collection_name: value,
  115. rowCount: findKeyValue(collectionStatistics.stats, ROW_COUNT),
  116. });
  117. }
  118. }
  119. return data;
  120. }
  121. /**
  122. * Get collections statistics data
  123. * @returns {collectionCount:number, totalData:number}
  124. */
  125. async getStatistics() {
  126. const data = {
  127. collectionCount: 0,
  128. totalData: 0,
  129. };
  130. const res = await this.getCollectionNames();
  131. data.collectionCount = res.collection_names.length;
  132. if (res.collection_names.length > 0) {
  133. for (const name of res.collection_names) {
  134. const collectionStatistics = await this.getCollectionStatistics({
  135. collection_name: name,
  136. });
  137. const rowCount = findKeyValue(collectionStatistics.stats, ROW_COUNT);
  138. data.totalData += isNaN(Number(rowCount)) ? 0 : Number(rowCount);
  139. }
  140. }
  141. return data;
  142. }
  143. /**
  144. * Get all collection index status
  145. * @returns {collection_name:string, index_state: IndexState}[]
  146. */
  147. async getCollectionsIndexStatus() {
  148. const data = [];
  149. const res = await this.getCollectionNames();
  150. if (res.collection_names.length > 0) {
  151. for (const name of res.collection_names) {
  152. const indexRes = await this.getIndexStatus({ collection_name: name });
  153. data.push({
  154. collection_name: name,
  155. index_state: indexRes.state,
  156. });
  157. }
  158. }
  159. return data;
  160. }
  161. }