partitions.tsx 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. import { makeStyles, Theme } from '@material-ui/core';
  2. import { FC, useContext, useEffect, useState } from 'react';
  3. import {
  4. PartitionManageParam,
  5. // PartitionParam,
  6. PartitionView,
  7. } from './Types';
  8. import MilvusGrid from '../../components/grid';
  9. import { ColDefinitionsType, ToolBarConfig } from '../../components/grid/Types';
  10. import { useTranslation } from 'react-i18next';
  11. import { usePaginationHook } from '../../hooks/Pagination';
  12. import icons from '../../components/icons/Icons';
  13. import CustomToolTip from '../../components/customToolTip/CustomToolTip';
  14. import { rootContext } from '../../context/Root';
  15. import CreatePartition from './Create';
  16. import { PartitionHttp } from '../../http/Partition';
  17. import Status from '../../components/status/Status';
  18. import { ManageRequestMethods } from '../../types/Common';
  19. // import { StatusEnum } from '../../components/status/Types';
  20. // import { useDialogHook } from '../../hooks/Dialog';
  21. import DeleteTemplate from '../../components/customDialog/DeleteDialogTemplate';
  22. const useStyles = makeStyles((theme: Theme) => ({
  23. wrapper: {
  24. height: '100%',
  25. },
  26. icon: {
  27. fontSize: '20px',
  28. marginLeft: theme.spacing(0.5),
  29. },
  30. }));
  31. const Partitions: FC<{
  32. collectionName: string;
  33. }> = ({ collectionName }) => {
  34. const classes = useStyles();
  35. const { t } = useTranslation('partition');
  36. const { t: successTrans } = useTranslation('success');
  37. const { t: btnTrans } = useTranslation('btn');
  38. const { t: dialogTrans } = useTranslation('dialog');
  39. const InfoIcon = icons.info;
  40. // const LoadIcon = icons.load;
  41. // const ReleaseIcon = icons.release;
  42. // const { handleAction } = useDialogHook({ type: 'partition' });
  43. const [selectedPartitions, setSelectedPartitions] = useState<PartitionView[]>(
  44. []
  45. );
  46. const [partitions, setPartitions] = useState<PartitionView[]>([]);
  47. const {
  48. pageSize,
  49. handlePageSize,
  50. currentPage,
  51. handleCurrentPage,
  52. total,
  53. data: partitionList,
  54. } = usePaginationHook(partitions);
  55. const [loading, setLoading] = useState<boolean>(true);
  56. const { setDialog, handleCloseDialog, openSnackBar } =
  57. useContext(rootContext);
  58. useEffect(() => {
  59. fetchPartitions(collectionName);
  60. }, [collectionName]);
  61. const fetchPartitions = async (collectionName: string) => {
  62. try {
  63. const res = await PartitionHttp.getPartitions(collectionName);
  64. const partitons: PartitionView[] = res.map(p =>
  65. Object.assign(p, { _statusElement: <Status status={p._status} /> })
  66. );
  67. setLoading(false);
  68. setPartitions(partitons);
  69. } catch (err) {
  70. setLoading(false);
  71. }
  72. };
  73. const handleDelete = async () => {
  74. for (const partition of selectedPartitions) {
  75. const param: PartitionManageParam = {
  76. partitionName: partition._name,
  77. collectionName,
  78. type: ManageRequestMethods.DELETE,
  79. };
  80. await PartitionHttp.managePartition(param);
  81. }
  82. openSnackBar(successTrans('delete', { name: t('partition') }));
  83. fetchPartitions(collectionName);
  84. handleCloseDialog();
  85. };
  86. // const handleRelease = async (data: PartitionView) => {
  87. // const param: PartitionParam = {
  88. // collectionName,
  89. // partitionNames: [data._name],
  90. // };
  91. // const res = await PartitionHttp.releasePartition(param);
  92. // openSnackBar(successTrans('release', { name: t('partition') }));
  93. // fetchPartitions(collectionName);
  94. // return res;
  95. // };
  96. // const handleLoad = async (data: PartitionView) => {
  97. // const param: PartitionParam = {
  98. // collectionName,
  99. // partitionNames: [data._name!],
  100. // };
  101. // const res = await PartitionHttp.loadPartition(param);
  102. // openSnackBar(successTrans('load', { name: t('partition') }));
  103. // fetchPartitions(collectionName);
  104. // return res;
  105. // };
  106. const toolbarConfigs: ToolBarConfig[] = [
  107. {
  108. label: t('create'),
  109. onClick: () => {
  110. setDialog({
  111. open: true,
  112. type: 'custom',
  113. params: {
  114. component: (
  115. <CreatePartition
  116. handleCreate={handleCreatePartition}
  117. handleClose={handleCloseDialog}
  118. />
  119. ),
  120. },
  121. });
  122. },
  123. icon: 'add',
  124. },
  125. {
  126. type: 'iconBtn',
  127. onClick: () => {
  128. setDialog({
  129. open: true,
  130. type: 'custom',
  131. params: {
  132. component: (
  133. <DeleteTemplate
  134. label={btnTrans('delete')}
  135. title={dialogTrans('deleteTitle', { type: t('partition') })}
  136. text={t('deleteWarning')}
  137. handleDelete={handleDelete}
  138. />
  139. ),
  140. },
  141. });
  142. },
  143. label: '',
  144. icon: 'delete',
  145. // can't delete default partition
  146. disabled: () =>
  147. selectedPartitions.length === 0 ||
  148. selectedPartitions.some(p => p._name === '_default'),
  149. tooltip: selectedPartitions.some(p => p._name === '_default')
  150. ? t('deletePartitionError')
  151. : '',
  152. },
  153. ];
  154. const colDefinitions: ColDefinitionsType[] = [
  155. {
  156. id: '_id',
  157. align: 'left',
  158. disablePadding: true,
  159. label: t('id'),
  160. },
  161. {
  162. id: '_formatName',
  163. align: 'left',
  164. disablePadding: false,
  165. label: t('name'),
  166. },
  167. {
  168. id: '_statusElement',
  169. align: 'left',
  170. disablePadding: false,
  171. label: t('status'),
  172. },
  173. {
  174. id: '_rowCount',
  175. align: 'left',
  176. disablePadding: false,
  177. label: (
  178. <span className="flex-center">
  179. {t('rowCount')}
  180. <CustomToolTip title={t('tooltip')}>
  181. <InfoIcon classes={{ root: classes.icon }} />
  182. </CustomToolTip>
  183. </span>
  184. ),
  185. },
  186. // {
  187. // id: 'action',
  188. // align: 'center',
  189. // disablePadding: false,
  190. // label: '',
  191. // showActionCell: true,
  192. // isHoverAction: true,
  193. // actionBarConfigs: [
  194. // {
  195. // onClick: (e: React.MouseEvent, row: PartitionView) => {
  196. // const cb =
  197. // row._status === StatusEnum.unloaded ? handleLoad : handleRelease;
  198. // handleAction(row, cb);
  199. // },
  200. // icon: 'load',
  201. // label: 'load',
  202. // showIconMethod: 'renderFn',
  203. // getLabel: (row: PartitionView) =>
  204. // row._status === StatusEnum.loaded ? 'release' : 'load',
  205. // renderIconFn: (row: PartitionView) =>
  206. // row._status === StatusEnum.loaded ? <ReleaseIcon /> : <LoadIcon />,
  207. // },
  208. // ],
  209. // },
  210. ];
  211. const handleSelectChange = (value: PartitionView[]) => {
  212. setSelectedPartitions(value);
  213. };
  214. const handlePageChange = (e: any, page: number) => {
  215. handleCurrentPage(page);
  216. setSelectedPartitions([]);
  217. };
  218. const handleCreatePartition = async (name: string) => {
  219. const param: PartitionManageParam = {
  220. partitionName: name,
  221. collectionName,
  222. type: ManageRequestMethods.CREATE,
  223. };
  224. await PartitionHttp.managePartition(param);
  225. openSnackBar(successTrans('create', { name: t('partition') }));
  226. handleCloseDialog();
  227. // refresh partitions
  228. fetchPartitions(collectionName);
  229. };
  230. return (
  231. <section className={classes.wrapper}>
  232. <MilvusGrid
  233. toolbarConfigs={toolbarConfigs}
  234. colDefinitions={colDefinitions}
  235. rows={partitionList}
  236. rowCount={total}
  237. primaryKey="id"
  238. openCheckBox={true}
  239. showHoverStyle={true}
  240. selected={selectedPartitions}
  241. setSelected={handleSelectChange}
  242. page={currentPage}
  243. onChangePage={handlePageChange}
  244. rowsPerPage={pageSize}
  245. setRowsPerPage={handlePageSize}
  246. isLoading={loading}
  247. />
  248. </section>
  249. );
  250. };
  251. export default Partitions;