Collection.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. import { ChildrenStatusType } from '../components/status/Types';
  2. import {
  3. CollectionView,
  4. DeleteEntitiesReq,
  5. InsertDataParam,
  6. } from '../pages/collections/Types';
  7. import { Field } from '../pages/schema/Types';
  8. import { VectorSearchParam } from '../types/SearchTypes';
  9. import { QueryParam } from '../pages/query/Types';
  10. import { IndexState, ShowCollectionsType } from '../types/Milvus';
  11. import { formatNumber } from '../utils/Common';
  12. import BaseModel from './BaseModel';
  13. import { FieldHttp } from './Field';
  14. import dayjs from 'dayjs';
  15. import { LOADING_STATE } from '../consts/Milvus';
  16. export class CollectionHttp extends BaseModel implements CollectionView {
  17. private autoID!: string;
  18. private collection_name!: string;
  19. private description!: string;
  20. private consistency_level!: string;
  21. private rowCount!: string;
  22. private index_status!: string;
  23. private id!: string;
  24. private loadedPercentage!: string;
  25. private createdTime!: string;
  26. private schema!: {
  27. fields: Field[];
  28. };
  29. static COLLECTIONS_URL = '/collections';
  30. static COLLECTIONS_INDEX_STATUS_URL = '/collections/indexes/status';
  31. static COLLECTIONS_STATISTICS_URL = '/collections/statistics';
  32. constructor(props: CollectionView) {
  33. super(props);
  34. Object.assign(this, props);
  35. }
  36. static getCollections(data?: {
  37. type: ShowCollectionsType;
  38. }): Promise<CollectionHttp[]> {
  39. return super.findAll({ path: this.COLLECTIONS_URL, params: data || {} });
  40. }
  41. static getCollection(name: string) {
  42. return super.search({
  43. path: `${this.COLLECTIONS_URL}/${name}`,
  44. params: {},
  45. });
  46. }
  47. static createCollection(data: any) {
  48. return super.create({ path: this.COLLECTIONS_URL, data });
  49. }
  50. static getCollectionsIndexState(): Promise<CollectionHttp[]> {
  51. return super.findAll({
  52. path: this.COLLECTIONS_INDEX_STATUS_URL,
  53. params: {},
  54. });
  55. }
  56. static deleteCollection(collectionName: string) {
  57. return super.delete({ path: `${this.COLLECTIONS_URL}/${collectionName}` });
  58. }
  59. static loadCollection(collectionName: string) {
  60. return super.update({
  61. path: `${this.COLLECTIONS_URL}/${collectionName}/load`,
  62. });
  63. }
  64. static releaseCollection(collectionName: string) {
  65. return super.update({
  66. path: `${this.COLLECTIONS_URL}/${collectionName}/release`,
  67. });
  68. }
  69. static getStatistics() {
  70. return super.search({ path: this.COLLECTIONS_STATISTICS_URL, params: {} });
  71. }
  72. static insertData(collectionName: string, param: InsertDataParam) {
  73. return super.create({
  74. path: `${this.COLLECTIONS_URL}/${collectionName}/insert`,
  75. data: param,
  76. });
  77. }
  78. static deleteEntities(collectionName: string, param: DeleteEntitiesReq) {
  79. return super.update({
  80. path: `${this.COLLECTIONS_URL}/${collectionName}/entities`,
  81. data: param,
  82. });
  83. }
  84. static vectorSearchData(collectionName: string, params: VectorSearchParam) {
  85. return super.query({
  86. path: `${this.COLLECTIONS_URL}/${collectionName}/search`,
  87. data: params,
  88. });
  89. }
  90. static createAlias(collectionName: string, params: { alias: string }) {
  91. return super.create({
  92. path: `${this.COLLECTIONS_URL}/${collectionName}/alias`,
  93. data: params,
  94. });
  95. }
  96. static queryData(collectionName: string, params: QueryParam) {
  97. return super.query({
  98. path: `${this.COLLECTIONS_URL}/${collectionName}/query`,
  99. data: params,
  100. });
  101. }
  102. get _autoId() {
  103. return this.autoID;
  104. }
  105. get _desc() {
  106. return this.description || '--';
  107. }
  108. get _id() {
  109. return this.id;
  110. }
  111. get _name() {
  112. return this.collection_name;
  113. }
  114. get _rowCount() {
  115. return formatNumber(Number(this.rowCount));
  116. }
  117. get _loadedPercentage() {
  118. return this.loadedPercentage;
  119. }
  120. // load status
  121. get _status() {
  122. // If not load, insight server will return '-1'. Otherwise milvus will return percentage
  123. return this._loadedPercentage === '-1'
  124. ? LOADING_STATE.UNLOADED
  125. : this._loadedPercentage === '100'
  126. ? LOADING_STATE.LOADED
  127. : LOADING_STATE.LOADING;
  128. // return LOADING_STATE.LOADING
  129. }
  130. get _consistencyLevel() {
  131. return this.consistency_level;
  132. }
  133. get _fields() {
  134. return this.schema.fields.map(f => new FieldHttp(f));
  135. }
  136. get _indexState() {
  137. switch (this.index_status) {
  138. case IndexState.InProgress:
  139. return ChildrenStatusType.CREATING;
  140. case IndexState.Failed:
  141. return ChildrenStatusType.ERROR;
  142. default:
  143. return ChildrenStatusType.FINISH;
  144. }
  145. }
  146. // Befor milvus-2.0-rc3 will return '0'.
  147. // If milvus is stable, we can remote this condition
  148. get _createdTime(): string {
  149. return this.createdTime && this.createdTime !== '0'
  150. ? dayjs(Number(this.createdTime)).format('YYYY-MM-DD HH:mm:ss')
  151. : '';
  152. }
  153. }