Partitions.tsx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. import { makeStyles, Theme } from '@material-ui/core';
  2. import { FC, useContext, useEffect, useState } from 'react';
  3. import { useSearchParams } from 'react-router-dom';
  4. import { PartitionManageParam, PartitionView } from './Types';
  5. import AttuGrid from '../../components/grid/Grid';
  6. import { ColDefinitionsType, ToolBarConfig } from '../../components/grid/Types';
  7. import { useTranslation } from 'react-i18next';
  8. import { usePaginationHook } from '../../hooks/Pagination';
  9. import icons from '../../components/icons/Icons';
  10. import CustomToolTip from '../../components/customToolTip/CustomToolTip';
  11. import { rootContext } from '../../context/Root';
  12. import CreatePartition from './Create';
  13. import { PartitionHttp } from '../../http/Partition';
  14. import { ManageRequestMethods } from '../../types/Common';
  15. import DeleteTemplate from '../../components/customDialog/DeleteDialogTemplate';
  16. import Highlighter from 'react-highlight-words';
  17. import { useInsertDialogHook } from '../../hooks/Dialog';
  18. import InsertContainer from '../../components/insert/Container';
  19. import { CollectionHttp } from '../../http/Collection';
  20. import { FieldHttp } from '../../http/Field';
  21. import { Field } from '../schema/Types';
  22. import { InsertDataParam } from '../collections/Types';
  23. import { MilvusHttp } from '../../http/Milvus';
  24. const useStyles = makeStyles((theme: Theme) => ({
  25. wrapper: {
  26. height: '100%',
  27. },
  28. icon: {
  29. fontSize: '20px',
  30. marginLeft: theme.spacing(0.5),
  31. },
  32. highlight: {
  33. color: theme.palette.primary.main,
  34. backgroundColor: 'transparent',
  35. },
  36. }));
  37. let timer: NodeJS.Timeout | null = null;
  38. // get init search value from url
  39. // const { search = '' } = parseLocationSearch(window.location.search);
  40. const Partitions: FC<{
  41. collectionName: string;
  42. }> = ({ collectionName }) => {
  43. const classes = useStyles();
  44. const { t } = useTranslation('partition');
  45. const { t: successTrans } = useTranslation('success');
  46. const { t: btnTrans } = useTranslation('btn');
  47. const { t: dialogTrans } = useTranslation('dialog');
  48. const [searchParams] = useSearchParams();
  49. const [search, setSearch] = useState<string>(
  50. (searchParams.get('search') as string) || ''
  51. );
  52. const InfoIcon = icons.info;
  53. const { handleInsertDialog } = useInsertDialogHook();
  54. const [selectedPartitions, setSelectedPartitions] = useState<PartitionView[]>(
  55. []
  56. );
  57. const [partitions, setPartitions] = useState<PartitionView[]>([]);
  58. const [searchedPartitions, setSearchedPartitions] = useState<PartitionView[]>(
  59. []
  60. );
  61. const {
  62. pageSize,
  63. handlePageSize,
  64. currentPage,
  65. handleCurrentPage,
  66. total,
  67. data: partitionList,
  68. order,
  69. orderBy,
  70. handleGridSort,
  71. } = usePaginationHook(searchedPartitions);
  72. const [loading, setLoading] = useState<boolean>(true);
  73. const { setDialog, handleCloseDialog, openSnackBar } =
  74. useContext(rootContext);
  75. const fetchPartitions = async (collectionName: string) => {
  76. try {
  77. const res = await PartitionHttp.getPartitions(collectionName);
  78. setLoading(false);
  79. setPartitions(res);
  80. } catch (err) {
  81. setLoading(false);
  82. }
  83. };
  84. const fetchCollectionDetail = async (name: string) => {
  85. const res = await CollectionHttp.getCollection(name);
  86. return res;
  87. };
  88. useEffect(() => {
  89. fetchPartitions(collectionName);
  90. }, [collectionName]);
  91. useEffect(() => {
  92. if (timer) {
  93. clearTimeout(timer);
  94. }
  95. // add loading manually
  96. setLoading(true);
  97. timer = setTimeout(() => {
  98. const searchWords = [search];
  99. const list = search
  100. ? partitions.filter(p => p._formatName.includes(search))
  101. : partitions;
  102. const highlightList = list.map(c => {
  103. Object.assign(c, {
  104. _nameElement: (
  105. <Highlighter
  106. textToHighlight={c._formatName}
  107. searchWords={searchWords}
  108. highlightClassName={classes.highlight}
  109. />
  110. ),
  111. });
  112. return c;
  113. });
  114. setLoading(false);
  115. setSearchedPartitions(highlightList);
  116. }, 300);
  117. }, [search, partitions]);
  118. const handleDelete = async () => {
  119. for (const partition of selectedPartitions) {
  120. const param: PartitionManageParam = {
  121. partitionName: partition._name,
  122. collectionName,
  123. type: ManageRequestMethods.DELETE,
  124. };
  125. await PartitionHttp.managePartition(param);
  126. }
  127. openSnackBar(successTrans('delete', { name: t('partition') }));
  128. fetchPartitions(collectionName);
  129. handleCloseDialog();
  130. };
  131. const handleSearch = (value: string) => {
  132. setSearch(value);
  133. };
  134. const handleInsert = async (
  135. collectionName: string,
  136. partitionName: string,
  137. fieldData: any[]
  138. ): Promise<{ result: boolean; msg: string }> => {
  139. const param: InsertDataParam = {
  140. partition_names: [partitionName],
  141. fields_data: fieldData,
  142. };
  143. try {
  144. await CollectionHttp.insertData(collectionName, param);
  145. await MilvusHttp.flush(collectionName);
  146. // update partitions
  147. fetchPartitions(collectionName);
  148. return { result: true, msg: '' };
  149. } catch (err: any) {
  150. const {
  151. response: {
  152. data: { message },
  153. },
  154. } = err;
  155. return { result: false, msg: message || '' };
  156. }
  157. };
  158. const toolbarConfigs: ToolBarConfig[] = [
  159. {
  160. label: t('create'),
  161. onClick: () => {
  162. setDialog({
  163. open: true,
  164. type: 'custom',
  165. params: {
  166. component: (
  167. <CreatePartition
  168. handleCreate={handleCreatePartition}
  169. handleClose={handleCloseDialog}
  170. />
  171. ),
  172. },
  173. });
  174. },
  175. icon: 'add',
  176. },
  177. {
  178. label: btnTrans('insert'),
  179. onClick: async () => {
  180. const collection = await fetchCollectionDetail(collectionName);
  181. const schema = collection.schema.fields.map(
  182. (f: Field) => new FieldHttp(f)
  183. );
  184. handleInsertDialog(
  185. <InsertContainer
  186. schema={schema}
  187. defaultSelectedCollection={collectionName}
  188. defaultSelectedPartition={
  189. selectedPartitions.length === 1
  190. ? selectedPartitions[0]._formatName
  191. : ''
  192. }
  193. partitions={partitions}
  194. handleInsert={handleInsert}
  195. />
  196. );
  197. },
  198. /**
  199. * insert validation:
  200. * 1. At least 1 available partition
  201. * 2. selected partition quantity shouldn't over 1
  202. */
  203. disabled: () => partitions.length === 0 || selectedPartitions.length > 1,
  204. btnVariant: 'outlined',
  205. },
  206. {
  207. type: 'iconBtn',
  208. onClick: () => {
  209. setDialog({
  210. open: true,
  211. type: 'custom',
  212. params: {
  213. component: (
  214. <DeleteTemplate
  215. label={btnTrans('drop')}
  216. title={dialogTrans('deleteTitle', { type: t('partition') })}
  217. text={t('deleteWarning')}
  218. handleDelete={handleDelete}
  219. />
  220. ),
  221. },
  222. });
  223. },
  224. label: '',
  225. icon: 'delete',
  226. // can't delete default partition
  227. disabled: () =>
  228. selectedPartitions.length === 0 ||
  229. selectedPartitions.some(p => p._name === '_default'),
  230. tooltip: selectedPartitions.some(p => p._name === '_default')
  231. ? t('deletePartitionError')
  232. : '',
  233. },
  234. {
  235. label: 'Search',
  236. icon: 'search',
  237. searchText: search,
  238. onSearch: (value: string) => {
  239. handleSearch(value);
  240. },
  241. },
  242. ];
  243. const colDefinitions: ColDefinitionsType[] = [
  244. {
  245. id: '_nameElement',
  246. align: 'left',
  247. disablePadding: false,
  248. label: t('name'),
  249. },
  250. {
  251. id: '_createdTime',
  252. align: 'left',
  253. disablePadding: false,
  254. label: t('createdTime'),
  255. },
  256. // {
  257. // id: '_statusElement',
  258. // align: 'left',
  259. // disablePadding: false,
  260. // label: t('status'),
  261. // },
  262. {
  263. id: '_rowCount',
  264. align: 'left',
  265. disablePadding: false,
  266. label: (
  267. <span className="flex-center">
  268. {t('rowCount')}
  269. <CustomToolTip title={t('tooltip')}>
  270. <InfoIcon classes={{ root: classes.icon }} />
  271. </CustomToolTip>
  272. </span>
  273. ),
  274. },
  275. // {
  276. // id: 'action',
  277. // align: 'center',
  278. // disablePadding: false,
  279. // label: '',
  280. // showActionCell: true,
  281. // isHoverAction: true,
  282. // actionBarConfigs: [
  283. // {
  284. // onClick: (e: React.MouseEvent, row: PartitionView) => {
  285. // const cb =
  286. // row._status === StatusEnum.unloaded ? handleLoad : handleRelease;
  287. // handleAction(row, cb);
  288. // },
  289. // icon: 'load',
  290. // label: 'load',
  291. // showIconMethod: 'renderFn',
  292. // getLabel: (row: PartitionView) =>
  293. // row._status === StatusEnum.loaded ? 'release' : 'load',
  294. // renderIconFn: (row: PartitionView) =>
  295. // row._status === StatusEnum.loaded ? <ReleaseIcon /> : <LoadIcon />,
  296. // },
  297. // ],
  298. // },
  299. ];
  300. const handleSelectChange = (value: PartitionView[]) => {
  301. setSelectedPartitions(value);
  302. };
  303. const handlePageChange = (e: any, page: number) => {
  304. handleCurrentPage(page);
  305. setSelectedPartitions([]);
  306. };
  307. const handleCreatePartition = async (name: string) => {
  308. const param: PartitionManageParam = {
  309. partitionName: name,
  310. collectionName,
  311. type: ManageRequestMethods.CREATE,
  312. };
  313. await PartitionHttp.managePartition(param);
  314. openSnackBar(successTrans('create', { name: t('partition') }));
  315. handleCloseDialog();
  316. // refresh partitions
  317. fetchPartitions(collectionName);
  318. };
  319. return (
  320. <section className={classes.wrapper}>
  321. <AttuGrid
  322. toolbarConfigs={toolbarConfigs}
  323. colDefinitions={colDefinitions}
  324. rows={partitionList}
  325. rowCount={total}
  326. primaryKey="id"
  327. selected={selectedPartitions}
  328. setSelected={handleSelectChange}
  329. page={currentPage}
  330. onChangePage={handlePageChange}
  331. rowsPerPage={pageSize}
  332. setRowsPerPage={handlePageSize}
  333. isLoading={loading}
  334. order={order}
  335. orderBy={orderBy}
  336. handleSort={handleGridSort}
  337. />
  338. </section>
  339. );
  340. };
  341. export default Partitions;