Partitions.tsx 9.3 KB

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