Collection.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. import { ChildrenStatusType, StatusEnum } from '../components/status/Types';
  2. import { CollectionView, InsertDataParam } from '../pages/collections/Types';
  3. import { Field } from '../pages/schema/Types';
  4. import { VectorSearchParam } from '../pages/seach/Types';
  5. import { IndexState, ShowCollectionsType } from '../types/Milvus';
  6. import { formatNumber } from '../utils/Common';
  7. import BaseModel from './BaseModel';
  8. import { FieldHttp } from './Field';
  9. import dayjs from 'dayjs';
  10. export class CollectionHttp extends BaseModel implements CollectionView {
  11. private autoID!: string;
  12. private collection_name!: string;
  13. private description!: string;
  14. private rowCount!: string;
  15. private index_status!: string;
  16. private id!: string;
  17. private isLoaded!: boolean;
  18. private createdTime!: string;
  19. private schema!: {
  20. fields: Field[];
  21. };
  22. static COLLECTIONS_URL = '/collections';
  23. static COLLECTIONS_INDEX_STATUS_URL = '/collections/indexes/status';
  24. static COLLECTIONS_STATISTICS_URL = '/collections/statistics';
  25. static CHECK_URL = '/milvus/check';
  26. constructor(props: CollectionView) {
  27. super(props);
  28. Object.assign(this, props);
  29. }
  30. static getCollections(data?: {
  31. type: ShowCollectionsType;
  32. }): Promise<CollectionHttp[]> {
  33. return super.findAll({ path: this.COLLECTIONS_URL, params: data || {} });
  34. }
  35. static getCollection(name: string) {
  36. return super.search({
  37. path: `${this.COLLECTIONS_URL}/${name}`,
  38. params: {},
  39. });
  40. }
  41. static createCollection(data: any) {
  42. return super.create({ path: this.COLLECTIONS_URL, data });
  43. }
  44. static getCollectionsIndexState(): Promise<CollectionHttp[]> {
  45. return super.findAll({
  46. path: this.COLLECTIONS_INDEX_STATUS_URL,
  47. params: {},
  48. });
  49. }
  50. static deleteCollection(collectionName: string) {
  51. return super.delete({ path: `${this.COLLECTIONS_URL}/${collectionName}` });
  52. }
  53. static loadCollection(collectionName: string) {
  54. return super.update({
  55. path: `${this.COLLECTIONS_URL}/${collectionName}/load`,
  56. });
  57. }
  58. static releaseCollection(collectionName: string) {
  59. return super.update({
  60. path: `${this.COLLECTIONS_URL}/${collectionName}/release`,
  61. });
  62. }
  63. static getStatistics() {
  64. return super.search({ path: this.COLLECTIONS_STATISTICS_URL, params: {} });
  65. }
  66. static insertData(collectionName: string, param: InsertDataParam) {
  67. return super.create({
  68. path: `${this.COLLECTIONS_URL}/${collectionName}/insert`,
  69. data: param,
  70. });
  71. }
  72. static vectorSearchData(collectionName: string, params: VectorSearchParam) {
  73. return super.vectorSearch({
  74. path: `${this.COLLECTIONS_URL}/${collectionName}/search`,
  75. data: params,
  76. });
  77. }
  78. get _autoId() {
  79. return this.autoID;
  80. }
  81. get _desc() {
  82. return this.description || '--';
  83. }
  84. get _id() {
  85. return this.id;
  86. }
  87. get _name() {
  88. return this.collection_name;
  89. }
  90. get _rowCount() {
  91. return formatNumber(Number(this.rowCount));
  92. }
  93. get _isLoaded() {
  94. return this.isLoaded;
  95. }
  96. get _status() {
  97. return this.isLoaded === true ? StatusEnum.loaded : StatusEnum.unloaded;
  98. }
  99. get _fields() {
  100. return this.schema.fields.map(f => new FieldHttp(f));
  101. }
  102. get _indexState() {
  103. switch (this.index_status) {
  104. case IndexState.InProgress:
  105. return ChildrenStatusType.CREATING;
  106. case IndexState.Failed:
  107. return ChildrenStatusType.ERROR;
  108. default:
  109. return ChildrenStatusType.FINISH;
  110. }
  111. }
  112. // Befor milvus-2.0-rc3 will return '0'.
  113. // If milvus is stable, we can remote this condition/
  114. get _createdTime(): string {
  115. return this.createdTime && this.createdTime !== '0'
  116. ? dayjs(Number(this.createdTime)).format('YYYY-MM-DD HH:mm:ss')
  117. : '';
  118. }
  119. }