Create.tsx 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. import { useEffect, useMemo, useState } from 'react';
  2. import { useTranslation } from 'react-i18next';
  3. import DialogTemplate from '../../components/customDialog/DialogTemplate';
  4. import {
  5. EmbeddingTypeEnum,
  6. INDEX_CONFIG,
  7. INDEX_OPTIONS_MAP,
  8. MetricType,
  9. } from '../../consts/Milvus';
  10. import { useFormValidation } from '../../hooks/Form';
  11. import { formatForm, getMetricOptions } from '../../utils/Form';
  12. import { DataType } from '../collections/Types';
  13. import CreateForm from './CreateForm';
  14. import { IndexType, ParamPair } from './Types';
  15. const CreateIndex = (props: {
  16. collectionName: string;
  17. fieldType: DataType;
  18. handleCreate: (params: ParamPair[]) => void;
  19. handleCancel: () => void;
  20. }) => {
  21. const { collectionName, fieldType, handleCreate, handleCancel } = props;
  22. const { t: indexTrans } = useTranslation('index');
  23. const { t: dialogTrans } = useTranslation('dialog');
  24. const { t: btnTrans } = useTranslation('btn');
  25. const defaultMetricType = fieldType === 'BinaryVector' ? 'Hamming' : 'L2';
  26. const [indexSetting, setIndexSetting] = useState<{
  27. index_type: IndexType;
  28. metric_type: MetricType;
  29. [x: string]: string;
  30. }>({
  31. index_type: 'IVF_FLAT',
  32. metric_type: defaultMetricType,
  33. M: '',
  34. m: '4',
  35. efConstruction: '',
  36. nlist: '',
  37. n_trees: '',
  38. outDegree: '',
  39. candidatePoolSize: '',
  40. searchLength: '',
  41. knng: '',
  42. });
  43. const indexCreateParams = useMemo(() => {
  44. if (!INDEX_CONFIG[indexSetting.index_type]) {
  45. return [];
  46. }
  47. return INDEX_CONFIG[indexSetting.index_type].create;
  48. }, [indexSetting.index_type]);
  49. const metricOptions = useMemo(
  50. () => getMetricOptions(indexSetting.index_type, fieldType),
  51. [indexSetting.index_type, fieldType]
  52. );
  53. const indexParams = useMemo(() => {
  54. const params: { [x: string]: string } = {};
  55. indexCreateParams.forEach(v => {
  56. params[v] = indexSetting[v];
  57. });
  58. return params;
  59. }, [indexCreateParams, indexSetting]);
  60. const indexOptions = useMemo(() => {
  61. const type =
  62. fieldType === 'BinaryVector'
  63. ? EmbeddingTypeEnum.binary
  64. : EmbeddingTypeEnum.float;
  65. return INDEX_OPTIONS_MAP[type];
  66. }, [fieldType]);
  67. const checkedForm = useMemo(() => {
  68. const paramsForm: any = { metric_type: indexSetting.metric_type };
  69. indexCreateParams.forEach(v => {
  70. paramsForm[v] = indexSetting[v];
  71. });
  72. const form = formatForm(paramsForm);
  73. return form;
  74. }, [indexSetting, indexCreateParams]);
  75. const { validation, checkIsValid, disabled, setDisabled, resetValidation } =
  76. useFormValidation(checkedForm);
  77. // reset index params
  78. useEffect(() => {
  79. setDisabled(true);
  80. setIndexSetting(v => ({
  81. ...v,
  82. metric_type: defaultMetricType,
  83. M: '',
  84. m: '4',
  85. efConstruction: '',
  86. nlist: '',
  87. n_trees: '',
  88. out_degree: '',
  89. candidate_pool_size: '',
  90. search_length: '',
  91. knng: '',
  92. }));
  93. }, [indexCreateParams, setDisabled, defaultMetricType]);
  94. const updateStepTwoForm = (type: string, value: string) => {
  95. setIndexSetting(v => ({ ...v, [type]: value }));
  96. };
  97. const onIndexTypeChange = (type: string) => {
  98. let paramsForm: { [key in string]: string } = {};
  99. // m is selector not input
  100. (INDEX_CONFIG[type].create || [])
  101. .filter(t => t !== 'm')
  102. .forEach(item => {
  103. paramsForm[item] = '';
  104. });
  105. const form = formatForm(paramsForm);
  106. resetValidation(form);
  107. };
  108. const handleCreateIndex = () => {
  109. const { index_type, metric_type } = indexSetting;
  110. const params: ParamPair[] = [
  111. {
  112. key: 'index_type',
  113. value: index_type,
  114. },
  115. {
  116. key: 'metric_type',
  117. value: metric_type,
  118. },
  119. {
  120. key: 'params',
  121. value: JSON.stringify(indexParams),
  122. },
  123. ];
  124. handleCreate(params);
  125. };
  126. return (
  127. <DialogTemplate
  128. title={dialogTrans('createTitle', {
  129. type: indexTrans('index'),
  130. name: collectionName,
  131. })}
  132. handleClose={handleCancel}
  133. confirmLabel={btnTrans('create')}
  134. handleConfirm={handleCreateIndex}
  135. confirmDisabled={disabled}
  136. >
  137. <CreateForm
  138. updateForm={updateStepTwoForm}
  139. metricOptions={metricOptions}
  140. indexOptions={indexOptions}
  141. formValue={indexSetting}
  142. checkIsValid={checkIsValid}
  143. validation={validation}
  144. indexParams={indexCreateParams}
  145. indexTypeChange={onIndexTypeChange}
  146. />
  147. </DialogTemplate>
  148. );
  149. };
  150. export default CreateIndex;