Schema.tsx 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. import { makeStyles, Theme, Typography } from '@material-ui/core';
  2. import { FC, useCallback, useEffect, useState } from 'react';
  3. import MilvusGrid from '../../components/grid/Grid';
  4. import { ColDefinitionsType } from '../../components/grid/Types';
  5. import { useTranslation } from 'react-i18next';
  6. import { usePaginationHook } from '../../hooks/Pagination';
  7. import icons from '../../components/icons/Icons';
  8. import CustomToolTip from '../../components/customToolTip/CustomToolTip';
  9. import { FieldHttp } from '../../http/Field';
  10. import { FieldView } from './Types';
  11. import IndexTypeElement from './IndexTypeElement';
  12. import { DataType } from '../collections/Types';
  13. import { IndexHttp } from '../../http/Index';
  14. const useStyles = makeStyles((theme: Theme) => ({
  15. wrapper: {
  16. height: '100%',
  17. },
  18. icon: {
  19. fontSize: '20px',
  20. marginLeft: theme.spacing(0.5),
  21. },
  22. nameWrapper: {
  23. display: 'flex',
  24. alignItems: 'center',
  25. '& .key': {
  26. width: '16px',
  27. height: '16px',
  28. marginLeft: theme.spacing(0.5),
  29. },
  30. },
  31. paramWrapper: {
  32. '& .param': {
  33. marginRight: theme.spacing(2),
  34. '& .key': {
  35. color: theme.palette.milvusGrey.dark,
  36. display: 'inline-block',
  37. marginRight: theme.spacing(0.5),
  38. },
  39. '& .value': {
  40. color: '#010e29',
  41. },
  42. },
  43. },
  44. }));
  45. const Schema: FC<{
  46. collectionName: string;
  47. }> = ({ collectionName }) => {
  48. const classes = useStyles();
  49. const { t: collectionTrans } = useTranslation('collection');
  50. const { t: indexTrans } = useTranslation('index');
  51. const InfoIcon = icons.info;
  52. const [fields, setFields] = useState<FieldView[]>([]);
  53. const [loading, setLoading] = useState<boolean>(true);
  54. const {
  55. pageSize,
  56. handlePageSize,
  57. currentPage,
  58. handleCurrentPage,
  59. total,
  60. data: schemaList,
  61. } = usePaginationHook(fields);
  62. const fetchSchemaListWithIndex = async (
  63. collectionName: string
  64. ): Promise<FieldView[]> => {
  65. const vectorTypes: DataType[] = ['BinaryVector', 'FloatVector'];
  66. const indexList = await IndexHttp.getIndexInfo(collectionName);
  67. const schemaList = await FieldHttp.getFields(collectionName);
  68. let fields: FieldView[] = [];
  69. for (const schema of schemaList) {
  70. let field: FieldView = Object.assign(schema, {
  71. _indexParameterPairs: [],
  72. _indexType: '',
  73. });
  74. if (vectorTypes.includes(schema.data_type)) {
  75. const index = indexList.find(i => i._fieldName === schema.name);
  76. field._indexParameterPairs = index?._indexParameterPairs || [];
  77. field._indexType = index?._indexType || '';
  78. field._createIndexDisabled = indexList.length > 0;
  79. }
  80. fields = [...fields, field];
  81. }
  82. return fields;
  83. };
  84. const fetchFields = useCallback(
  85. async (collectionName: string) => {
  86. const KeyIcon = icons.key;
  87. try {
  88. const list = await fetchSchemaListWithIndex(collectionName);
  89. const fields: FieldView[] = list.map(f =>
  90. Object.assign(f, {
  91. _fieldNameElement: (
  92. <div className={classes.nameWrapper}>
  93. {f._fieldName}
  94. {f._isPrimaryKey && <KeyIcon classes={{ root: 'key' }} />}
  95. </div>
  96. ),
  97. _indexParamElement: (
  98. <div className={classes.paramWrapper}>
  99. {f._indexParameterPairs?.length > 0 ? (
  100. f._indexParameterPairs.map(p => (
  101. <span key={p.key} className="param">
  102. <Typography variant="body1" className="key">
  103. {`${p.key}:`}
  104. </Typography>
  105. <Typography variant="body1" className="value">
  106. {p.value}
  107. </Typography>
  108. </span>
  109. ))
  110. ) : (
  111. <>--</>
  112. )}
  113. </div>
  114. ),
  115. _indexTypeElement: (
  116. <IndexTypeElement
  117. data={f}
  118. collectionName={collectionName}
  119. cb={fetchFields}
  120. />
  121. ),
  122. })
  123. );
  124. setFields(fields);
  125. setLoading(false);
  126. } catch (err) {
  127. setLoading(false);
  128. throw err;
  129. }
  130. },
  131. [classes.nameWrapper, classes.paramWrapper]
  132. );
  133. useEffect(() => {
  134. fetchFields(collectionName);
  135. }, [collectionName, fetchFields]);
  136. const colDefinitions: ColDefinitionsType[] = [
  137. {
  138. id: '_fieldNameElement',
  139. align: 'left',
  140. disablePadding: true,
  141. label: collectionTrans('fieldName'),
  142. sortBy: '_fieldName',
  143. },
  144. {
  145. id: '_fieldType',
  146. align: 'left',
  147. disablePadding: false,
  148. label: collectionTrans('fieldType'),
  149. },
  150. {
  151. id: '_dimension',
  152. align: 'left',
  153. disablePadding: false,
  154. label: (
  155. <span className="flex-center">
  156. {collectionTrans('dimension')}
  157. <CustomToolTip title={collectionTrans('dimensionTooltip')}>
  158. <InfoIcon classes={{ root: classes.icon }} />
  159. </CustomToolTip>
  160. </span>
  161. ),
  162. },
  163. {
  164. id: '_indexTypeElement',
  165. align: 'left',
  166. disablePadding: true,
  167. label: indexTrans('type'),
  168. sortBy: '_indexType',
  169. },
  170. {
  171. id: '_indexParamElement',
  172. align: 'left',
  173. disablePadding: false,
  174. label: indexTrans('param'),
  175. notSort: true,
  176. },
  177. {
  178. id: '_desc',
  179. align: 'left',
  180. disablePadding: false,
  181. label: indexTrans('desc'),
  182. },
  183. ];
  184. const handlePageChange = (e: any, page: number) => {
  185. handleCurrentPage(page);
  186. };
  187. return (
  188. <section className={classes.wrapper}>
  189. <MilvusGrid
  190. toolbarConfigs={[]}
  191. colDefinitions={colDefinitions}
  192. rows={schemaList}
  193. rowCount={total}
  194. primaryKey="_fieldId"
  195. showHoverStyle={false}
  196. page={currentPage}
  197. onChangePage={handlePageChange}
  198. rowsPerPage={pageSize}
  199. setRowsPerPage={handlePageSize}
  200. isLoading={loading}
  201. />
  202. </section>
  203. );
  204. };
  205. export default Schema;