Schema.tsx 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. // set min width to prevent other table cell stretching
  33. minWidth: 180,
  34. '& .param': {
  35. marginRight: theme.spacing(2),
  36. '& .key': {
  37. color: theme.palette.milvusGrey.dark,
  38. display: 'inline-block',
  39. marginRight: theme.spacing(0.5),
  40. },
  41. '& .value': {
  42. color: theme.palette.milvusDark.main,
  43. },
  44. },
  45. },
  46. }));
  47. const Schema: FC<{
  48. collectionName: string;
  49. }> = ({ collectionName }) => {
  50. const classes = useStyles();
  51. const { t: collectionTrans } = useTranslation('collection');
  52. const { t: indexTrans } = useTranslation('index');
  53. const InfoIcon = icons.info;
  54. const [fields, setFields] = useState<FieldView[]>([]);
  55. const [loading, setLoading] = useState<boolean>(true);
  56. const {
  57. pageSize,
  58. handlePageSize,
  59. currentPage,
  60. handleCurrentPage,
  61. total,
  62. data: schemaList,
  63. order,
  64. orderBy,
  65. handleGridSort,
  66. } = usePaginationHook(fields);
  67. const fetchSchemaListWithIndex = async (
  68. collectionName: string
  69. ): Promise<FieldView[]> => {
  70. const vectorTypes: DataType[] = ['BinaryVector', 'FloatVector'];
  71. const indexList = await IndexHttp.getIndexInfo(collectionName);
  72. const schemaList = await FieldHttp.getFields(collectionName);
  73. let fields: FieldView[] = [];
  74. for (const schema of schemaList) {
  75. let field: FieldView = Object.assign(schema, {
  76. _indexParameterPairs: [],
  77. _indexType: '',
  78. });
  79. if (vectorTypes.includes(schema.data_type)) {
  80. const index = indexList.find(i => i._fieldName === schema.name);
  81. field._indexParameterPairs = index?._indexParameterPairs || [];
  82. field._indexType = index?._indexType || '';
  83. field._createIndexDisabled = indexList.length > 0;
  84. }
  85. fields = [...fields, field];
  86. }
  87. return fields;
  88. };
  89. const fetchFields = useCallback(
  90. async (collectionName: string) => {
  91. const KeyIcon = icons.key;
  92. try {
  93. const list = await fetchSchemaListWithIndex(collectionName);
  94. const fields: FieldView[] = list.map(f =>
  95. Object.assign(f, {
  96. _fieldNameElement: (
  97. <div className={classes.nameWrapper}>
  98. {f._fieldName}
  99. {f._isPrimaryKey && <KeyIcon classes={{ root: 'key' }} />}
  100. </div>
  101. ),
  102. _indexParamElement: (
  103. <div className={classes.paramWrapper}>
  104. {f._indexParameterPairs?.length > 0 ? (
  105. f._indexParameterPairs.map(p => (
  106. <span key={p.key} className="param">
  107. <Typography variant="body1" className="key">
  108. {`${p.key}:`}
  109. </Typography>
  110. <Typography variant="body1" className="value">
  111. {p.value}
  112. </Typography>
  113. </span>
  114. ))
  115. ) : (
  116. <>--</>
  117. )}
  118. </div>
  119. ),
  120. _indexTypeElement: (
  121. <IndexTypeElement
  122. data={f}
  123. collectionName={collectionName}
  124. cb={fetchFields}
  125. />
  126. ),
  127. })
  128. );
  129. setFields(fields);
  130. setLoading(false);
  131. } catch (err) {
  132. setLoading(false);
  133. throw err;
  134. }
  135. },
  136. [classes.nameWrapper, classes.paramWrapper]
  137. );
  138. useEffect(() => {
  139. fetchFields(collectionName);
  140. }, [collectionName, fetchFields]);
  141. const colDefinitions: ColDefinitionsType[] = [
  142. {
  143. id: '_fieldNameElement',
  144. align: 'left',
  145. disablePadding: true,
  146. label: collectionTrans('fieldName'),
  147. sortBy: '_fieldName',
  148. },
  149. {
  150. id: '_fieldType',
  151. align: 'left',
  152. disablePadding: false,
  153. label: collectionTrans('fieldType'),
  154. },
  155. {
  156. id: '_dimension',
  157. align: 'left',
  158. disablePadding: false,
  159. label: (
  160. <span className="flex-center">
  161. {collectionTrans('dimension')}
  162. <CustomToolTip title={collectionTrans('dimensionTooltip')}>
  163. <InfoIcon classes={{ root: classes.icon }} />
  164. </CustomToolTip>
  165. </span>
  166. ),
  167. },
  168. {
  169. id: '_indexTypeElement',
  170. align: 'left',
  171. disablePadding: true,
  172. label: indexTrans('type'),
  173. sortBy: '_indexType',
  174. },
  175. {
  176. id: '_indexParamElement',
  177. align: 'left',
  178. disablePadding: false,
  179. label: indexTrans('param'),
  180. notSort: true,
  181. },
  182. {
  183. id: '_desc',
  184. align: 'left',
  185. disablePadding: false,
  186. label: indexTrans('desc'),
  187. },
  188. ];
  189. const handlePageChange = (e: any, page: number) => {
  190. handleCurrentPage(page);
  191. };
  192. return (
  193. <section className={classes.wrapper}>
  194. <MilvusGrid
  195. toolbarConfigs={[]}
  196. colDefinitions={colDefinitions}
  197. rows={schemaList}
  198. rowCount={total}
  199. primaryKey="_fieldId"
  200. showHoverStyle={false}
  201. page={currentPage}
  202. onChangePage={handlePageChange}
  203. rowsPerPage={pageSize}
  204. setRowsPerPage={handlePageSize}
  205. isLoading={loading}
  206. openCheckBox={false}
  207. order={order}
  208. orderBy={orderBy}
  209. handleSort={handleGridSort}
  210. />
  211. </section>
  212. );
  213. };
  214. export default Schema;