Schema.tsx 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. import { makeStyles, Theme, Typography, Chip } from '@material-ui/core';
  2. import { FC, useCallback, useEffect, useState } from 'react';
  3. import AttuGrid 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 { FieldHttp } from '@/http/Field';
  9. import { FieldView } from './Types';
  10. import IndexTypeElement from './IndexTypeElement';
  11. import { IndexHttp } from '@/http/Index';
  12. import { DataTypeStringEnum } from '../collections/Types';
  13. const useStyles = makeStyles((theme: Theme) => ({
  14. wrapper: {
  15. height: `calc(100vh - 160px)`,
  16. },
  17. icon: {
  18. fontSize: '20px',
  19. marginLeft: theme.spacing(0.5),
  20. },
  21. iconTitle: {
  22. fontSize: '8px',
  23. position: 'relative',
  24. top: '3px',
  25. color: 'grey',
  26. },
  27. chip: {
  28. marginLeft: theme.spacing(0.5),
  29. marginRight: theme.spacing(0.5),
  30. },
  31. nameWrapper: {
  32. display: 'flex',
  33. alignItems: 'center',
  34. '& .key': {
  35. width: '16px',
  36. height: '16px',
  37. marginLeft: theme.spacing(0.5),
  38. },
  39. },
  40. paramWrapper: {
  41. // set min width to prevent other table cell stretching
  42. minWidth: 180,
  43. '& .param': {
  44. marginRight: theme.spacing(2),
  45. '& .key': {
  46. color: theme.palette.attuGrey.dark,
  47. display: 'inline-block',
  48. marginRight: theme.spacing(0.5),
  49. },
  50. '& .value': {
  51. color: theme.palette.attuDark.main,
  52. },
  53. },
  54. },
  55. }));
  56. const Schema: FC<{
  57. collectionName: string;
  58. }> = ({ collectionName }) => {
  59. const classes = useStyles();
  60. const { t: collectionTrans } = useTranslation('collection');
  61. const { t: indexTrans } = useTranslation('index');
  62. const [fields, setFields] = useState<FieldView[]>([]);
  63. const [loading, setLoading] = useState<boolean>(true);
  64. const {
  65. pageSize,
  66. handlePageSize,
  67. currentPage,
  68. handleCurrentPage,
  69. total,
  70. data: schemaList,
  71. order,
  72. orderBy,
  73. handleGridSort,
  74. } = usePaginationHook(fields);
  75. const fetchSchemaListWithIndex = async (
  76. collectionName: string
  77. ): Promise<FieldView[]> => {
  78. const indexList = await IndexHttp.getIndexInfo(collectionName);
  79. const schemaList = await FieldHttp.getFields(collectionName);
  80. let fields: FieldView[] = [];
  81. for (const schema of schemaList) {
  82. let field: FieldView = Object.assign(schema, {
  83. _indexParameterPairs: [],
  84. _indexType: '',
  85. _indexName: '',
  86. });
  87. const index = indexList.find(i => i._fieldName === schema.name);
  88. field._indexParameterPairs = index?._indexParameterPairs || [];
  89. field._indexType = index?._indexType || '';
  90. field._indexName = index?._indexName || '';
  91. fields = [...fields, field];
  92. }
  93. return fields;
  94. };
  95. const fetchFields = useCallback(
  96. async (collectionName: string) => {
  97. const KeyIcon = icons.key;
  98. try {
  99. const list = await fetchSchemaListWithIndex(collectionName);
  100. const fields: FieldView[] = list.map(f =>
  101. Object.assign(f, {
  102. _fieldNameElement: (
  103. <div className={classes.nameWrapper}>
  104. {f._fieldName}
  105. {f._isPrimaryKey ? (
  106. <div
  107. className={classes.iconTitle}
  108. title={collectionTrans('idFieldName')}
  109. >
  110. <KeyIcon classes={{ root: 'key' }} />
  111. </div>
  112. ) : null}
  113. {f.is_partition_key ? (
  114. <Chip
  115. className={classes.chip}
  116. size="small"
  117. label="Partition key"
  118. variant="outlined"
  119. />
  120. ) : null}
  121. {f._isAutoId ? (
  122. <Chip
  123. className={classes.chip}
  124. size="small"
  125. label="auto id"
  126. variant="outlined"
  127. />
  128. ) : null}
  129. </div>
  130. ),
  131. _fieldTypeElement: (
  132. <div className={classes.nameWrapper}>
  133. {f._fieldType}
  134. {f._dimension ? (
  135. <Chip
  136. className={classes.chip}
  137. size="small"
  138. label={`dim: ${f._dimension}`}
  139. variant="outlined"
  140. />
  141. ) : null}
  142. {f._maxLength && f._maxLength !== 'null' ? (
  143. <Chip
  144. className={classes.chip}
  145. size="small"
  146. label={`max: ${f._maxLength}`}
  147. variant="outlined"
  148. />
  149. ) : null}
  150. </div>
  151. ),
  152. _indexParamElement: (
  153. <div className={classes.paramWrapper}>
  154. {f._indexParameterPairs?.length > 0 ? (
  155. f._indexParameterPairs.map(p => (
  156. <span key={p.key} className="param">
  157. <Typography variant="body1" className="key">
  158. {`${p.key}:`}
  159. </Typography>
  160. <Typography variant="body1" className="value">
  161. {p.value}
  162. </Typography>
  163. </span>
  164. ))
  165. ) : (
  166. <>--</>
  167. )}
  168. </div>
  169. ),
  170. _indexTypeElement: (
  171. <>
  172. {f._fieldType !== DataTypeStringEnum.JSON ? (
  173. <IndexTypeElement
  174. data={f}
  175. collectionName={collectionName}
  176. cb={fetchFields}
  177. />
  178. ) : null}
  179. </>
  180. ),
  181. })
  182. );
  183. setFields(fields);
  184. setLoading(false);
  185. } catch (err) {
  186. setLoading(false);
  187. throw err;
  188. }
  189. },
  190. [classes.nameWrapper, classes.paramWrapper]
  191. );
  192. useEffect(() => {
  193. fetchFields(collectionName);
  194. }, [collectionName, fetchFields]);
  195. const colDefinitions: ColDefinitionsType[] = [
  196. {
  197. id: '_fieldNameElement',
  198. align: 'left',
  199. disablePadding: true,
  200. label: collectionTrans('fieldName'),
  201. sortBy: '_fieldName',
  202. },
  203. {
  204. id: '_fieldTypeElement',
  205. align: 'left',
  206. disablePadding: false,
  207. label: collectionTrans('fieldType'),
  208. },
  209. // {
  210. // id: '_dimension',
  211. // align: 'left',
  212. // disablePadding: false,
  213. // label: (
  214. // <span className="flex-center">
  215. // {collectionTrans('dimension')}
  216. // <CustomToolTip title={collectionTrans('dimensionTooltip')}>
  217. // <InfoIcon classes={{ root: classes.icon }} />
  218. // </CustomToolTip>
  219. // </span>
  220. // ),
  221. // },
  222. // {
  223. // id: '_maxLength',
  224. // align: 'left',
  225. // disablePadding: true,
  226. // label: collectionTrans('maxLength'),
  227. // },
  228. {
  229. id: '_indexName',
  230. align: 'left',
  231. disablePadding: true,
  232. label: 'Index name',
  233. },
  234. {
  235. id: '_indexTypeElement',
  236. align: 'left',
  237. disablePadding: true,
  238. label: indexTrans('type'),
  239. sortBy: '_indexType',
  240. },
  241. {
  242. id: '_indexParamElement',
  243. align: 'left',
  244. disablePadding: false,
  245. label: indexTrans('param'),
  246. notSort: true,
  247. },
  248. {
  249. id: '_desc',
  250. align: 'left',
  251. disablePadding: false,
  252. label: indexTrans('desc'),
  253. },
  254. ];
  255. const handlePageChange = (e: any, page: number) => {
  256. handleCurrentPage(page);
  257. };
  258. return (
  259. <section className={classes.wrapper}>
  260. <AttuGrid
  261. toolbarConfigs={[]}
  262. colDefinitions={colDefinitions}
  263. rows={schemaList}
  264. rowCount={total}
  265. primaryKey="_fieldId"
  266. showHoverStyle={false}
  267. page={currentPage}
  268. onChangePage={handlePageChange}
  269. rowsPerPage={pageSize}
  270. setRowsPerPage={handlePageSize}
  271. isLoading={loading}
  272. openCheckBox={false}
  273. order={order}
  274. orderBy={orderBy}
  275. handleSort={handleGridSort}
  276. />
  277. </section>
  278. );
  279. };
  280. export default Schema;