Collection.ts 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. import { ChildrenStatusType } from '../components/status/Types';
  2. import {
  3. CollectionView,
  4. DeleteEntitiesReq,
  5. InsertDataParam,
  6. LoadReplicaReq,
  7. Replica,
  8. } from '../pages/collections/Types';
  9. import { LoadSampleParam } from '../pages/dialogs/Types';
  10. import { Field } from '@/pages/schema/Types';
  11. import { VectorSearchParam } from '../types/SearchTypes';
  12. import { QueryParam } from '@/pages/query/Types';
  13. import { IndexState, ShowCollectionsType } from '../types/Milvus';
  14. import { formatNumber } from '../utils/Common';
  15. import BaseModel from './BaseModel';
  16. import { FieldHttp } from './Field';
  17. import dayjs from 'dayjs';
  18. import { LOADING_STATE } from '@/consts';
  19. export class CollectionHttp extends BaseModel implements CollectionView {
  20. private aliases!: string[];
  21. private autoID!: boolean;
  22. private collection_name!: string;
  23. private description!: string;
  24. private consistency_level!: string;
  25. private rowCount!: string;
  26. private index_status!: string;
  27. private id!: string;
  28. private loadedPercentage!: string;
  29. private createdTime!: string;
  30. private sampleFile!: string;
  31. private schema!: {
  32. fields: Field[];
  33. autoID: boolean;
  34. description: string;
  35. enable_dynamic_field: boolean;
  36. };
  37. private replicas!: Replica[];
  38. static COLLECTIONS_URL = '/collections';
  39. static COLLECTIONS_INDEX_STATUS_URL = '/collections/indexes/status';
  40. static COLLECTIONS_STATISTICS_URL = '/collections/statistics';
  41. constructor(props: CollectionView) {
  42. super(props);
  43. Object.assign(this, props);
  44. }
  45. static getCollections(data?: {
  46. type: ShowCollectionsType;
  47. }): Promise<CollectionHttp[]> {
  48. return super.findAll({ path: this.COLLECTIONS_URL, params: data || {} });
  49. }
  50. static getCollection(name: string) {
  51. return super.search({
  52. path: `${this.COLLECTIONS_URL}/${name}`,
  53. params: {},
  54. }) as Promise<CollectionHttp>;
  55. }
  56. static createCollection(data: any) {
  57. return super.create({ path: this.COLLECTIONS_URL, data });
  58. }
  59. static getCollectionsIndexState(): Promise<CollectionHttp[]> {
  60. return super.findAll({
  61. path: this.COLLECTIONS_INDEX_STATUS_URL,
  62. params: {},
  63. });
  64. }
  65. static deleteCollection(collectionName: string) {
  66. return super.delete({ path: `${this.COLLECTIONS_URL}/${collectionName}` });
  67. }
  68. static loadCollection(collectionName: string, param?: LoadReplicaReq) {
  69. return super.update({
  70. path: `${this.COLLECTIONS_URL}/${collectionName}/load`,
  71. data: param,
  72. });
  73. }
  74. static releaseCollection(collectionName: string) {
  75. return super.update({
  76. path: `${this.COLLECTIONS_URL}/${collectionName}/release`,
  77. });
  78. }
  79. static renameCollection(
  80. collectionName: string,
  81. params: { new_collection_name: string }
  82. ) {
  83. return super.create({
  84. path: `${this.COLLECTIONS_URL}/${collectionName}`,
  85. data: params,
  86. });
  87. }
  88. static getStatistics() {
  89. return super.search({ path: this.COLLECTIONS_STATISTICS_URL, params: {} });
  90. }
  91. static getPSegments(collectionName: string) {
  92. return super.search({
  93. path: `${this.COLLECTIONS_URL}/${collectionName}/psegments`,
  94. params: {},
  95. });
  96. }
  97. static count(collectionName: string) {
  98. return super.search({
  99. path: `${this.COLLECTIONS_URL}/${collectionName}/count`,
  100. params: {},
  101. });
  102. }
  103. static getQSegments(collectionName: string) {
  104. return super.search({
  105. path: `${this.COLLECTIONS_URL}/${collectionName}/qsegments`,
  106. params: {},
  107. });
  108. }
  109. static insertData(collectionName: string, param: InsertDataParam) {
  110. return super.create({
  111. path: `${this.COLLECTIONS_URL}/${collectionName}/insert`,
  112. data: param,
  113. });
  114. }
  115. static importSample(collectionName: string, param: LoadSampleParam) {
  116. return super.create({
  117. path: `${this.COLLECTIONS_URL}/${collectionName}/importSample`,
  118. data: param,
  119. });
  120. }
  121. static deleteEntities(collectionName: string, param: DeleteEntitiesReq) {
  122. return super.update({
  123. path: `${this.COLLECTIONS_URL}/${collectionName}/entities`,
  124. data: param,
  125. });
  126. }
  127. static vectorSearchData(collectionName: string, params: VectorSearchParam) {
  128. return super.query({
  129. path: `${this.COLLECTIONS_URL}/${collectionName}/search`,
  130. data: params,
  131. });
  132. }
  133. static createAlias(collectionName: string, params: { alias: string }) {
  134. return super.create({
  135. path: `${this.COLLECTIONS_URL}/${collectionName}/alias`,
  136. data: params,
  137. });
  138. }
  139. static dropAlias(collectionName: string, params: { alias: string }) {
  140. return super.delete({
  141. path: `${this.COLLECTIONS_URL}/${collectionName}/alias/${params.alias}`,
  142. });
  143. }
  144. static queryData(collectionName: string, params: QueryParam) {
  145. return super.query({
  146. path: `${this.COLLECTIONS_URL}/${collectionName}/query`,
  147. data: params,
  148. });
  149. }
  150. static compact(collectionName: string) {
  151. return super.update({
  152. path: `${this.COLLECTIONS_URL}/${collectionName}/compact`,
  153. });
  154. }
  155. get _autoId() {
  156. return this.autoID;
  157. }
  158. get _aliases() {
  159. return this.aliases || [];
  160. }
  161. get _desc() {
  162. return this.description || '--';
  163. }
  164. get _id() {
  165. return this.id;
  166. }
  167. get _name() {
  168. return this.collection_name;
  169. }
  170. get _rowCount() {
  171. return formatNumber(Number(this.rowCount));
  172. }
  173. get _loadedPercentage() {
  174. return this.loadedPercentage;
  175. }
  176. // load status
  177. get _status() {
  178. // If not load, insight server will return '-1'. Otherwise milvus will return percentage
  179. return this._loadedPercentage === '-1'
  180. ? LOADING_STATE.UNLOADED
  181. : this._loadedPercentage === '100'
  182. ? LOADING_STATE.LOADED
  183. : LOADING_STATE.LOADING;
  184. // return LOADING_STATE.LOADING
  185. }
  186. get _consistencyLevel() {
  187. return this.consistency_level;
  188. }
  189. get _fields() {
  190. return this.schema.fields.map(f => new FieldHttp(f));
  191. }
  192. get _indexState() {
  193. switch (this.index_status) {
  194. case IndexState.InProgress:
  195. return ChildrenStatusType.CREATING;
  196. case IndexState.Failed:
  197. return ChildrenStatusType.ERROR;
  198. default:
  199. return ChildrenStatusType.FINISH;
  200. }
  201. }
  202. // Befor milvus-2.0-rc3 will return '0'.
  203. // If milvus is stable, we can remote this condition
  204. get _createdTime(): string {
  205. return this.createdTime && this.createdTime !== '0'
  206. ? dayjs(Number(this.createdTime)).format('YYYY-MM-DD HH:mm:ss')
  207. : '';
  208. }
  209. get _replicas(): Replica[] {
  210. return this.replicas || [];
  211. }
  212. get _enableDynamicField(): boolean {
  213. return this.schema && this.schema.enable_dynamic_field;
  214. }
  215. get _schema() {
  216. return this.schema;
  217. }
  218. get _sampleFile() {
  219. return this.sampleFile;
  220. }
  221. }