Collection.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 { IndexState, ShowCollectionsType } from '../types/Milvus';
  5. import { formatNumber } from '../utils/Common';
  6. import BaseModel from './BaseModel';
  7. import { FieldHttp } from './Field';
  8. export class CollectionHttp extends BaseModel implements CollectionView {
  9. private autoID!: string;
  10. private collection_name!: string;
  11. private description!: string;
  12. private rowCount!: string;
  13. private index_status!: string;
  14. private id!: string;
  15. private isLoaded!: boolean;
  16. private schema!: {
  17. fields: Field[];
  18. };
  19. static COLLECTIONS_URL = '/collections';
  20. static COLLECTIONS_INDEX_STATUS_URL = '/collections/indexes/status';
  21. static COLLECTIONS_STATISTICS_URL = '/collections/statistics';
  22. static CHECK_URL = '/milvus/check';
  23. constructor(props: CollectionView) {
  24. super(props);
  25. Object.assign(this, props);
  26. }
  27. static getCollections(data?: {
  28. type: ShowCollectionsType;
  29. }): Promise<CollectionHttp[]> {
  30. return super.findAll({ path: this.COLLECTIONS_URL, params: data || {} });
  31. }
  32. static getCollection(name: string) {
  33. return super.findAll({
  34. path: `${this.COLLECTIONS_URL}/${name}`,
  35. params: {},
  36. });
  37. }
  38. static createCollection(data: any) {
  39. return super.create({ path: this.COLLECTIONS_URL, data });
  40. }
  41. static getCollectionsIndexState(): Promise<CollectionHttp[]> {
  42. return super.findAll({
  43. path: this.COLLECTIONS_INDEX_STATUS_URL,
  44. params: {},
  45. });
  46. }
  47. static deleteCollection(collectionName: string) {
  48. return super.delete({ path: `${this.COLLECTIONS_URL}/${collectionName}` });
  49. }
  50. static loadCollection(collectionName: string) {
  51. return super.update({
  52. path: `${this.COLLECTIONS_URL}/${collectionName}/load`,
  53. });
  54. }
  55. static releaseCollection(collectionName: string) {
  56. return super.update({
  57. path: `${this.COLLECTIONS_URL}/${collectionName}/release`,
  58. });
  59. }
  60. static getStatistics() {
  61. return super.search({ path: this.COLLECTIONS_STATISTICS_URL, params: {} });
  62. }
  63. static insertData(collectionName: string, param: InsertDataParam) {
  64. return super.create({
  65. path: `${this.COLLECTIONS_URL}/${collectionName}/insert`,
  66. data: param,
  67. });
  68. }
  69. get _autoId() {
  70. return this.autoID;
  71. }
  72. get _desc() {
  73. return this.description || '--';
  74. }
  75. get _id() {
  76. return this.id;
  77. }
  78. get _name() {
  79. return this.collection_name;
  80. }
  81. get _rowCount() {
  82. return formatNumber(Number(this.rowCount));
  83. }
  84. get _status() {
  85. return this.isLoaded === true ? StatusEnum.loaded : StatusEnum.unloaded;
  86. }
  87. get _fields() {
  88. return this.schema.fields.map(f => new FieldHttp(f));
  89. }
  90. get _indexState() {
  91. switch (this.index_status) {
  92. case IndexState.InProgress:
  93. return ChildrenStatusType.CREATING;
  94. case IndexState.Failed:
  95. return ChildrenStatusType.ERROR;
  96. default:
  97. return ChildrenStatusType.FINISH;
  98. }
  99. }
  100. }