2
0

Collections.tsx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. import { useCallback, useContext, useEffect, useMemo, useState } from 'react';
  2. import { Link, useSearchParams } from 'react-router-dom';
  3. import { makeStyles, Theme } from '@material-ui/core';
  4. import { useTranslation } from 'react-i18next';
  5. import { useNavigationHook } from '../../hooks/Navigation';
  6. import { ALL_ROUTER_TYPES } from '../../router/Types';
  7. import AttuGrid from '../../components/grid/Grid';
  8. import CustomToolBar from '../../components/grid/ToolBar';
  9. import { CollectionView, InsertDataParam } from './Types';
  10. import { ColDefinitionsType, ToolBarConfig } from '../../components/grid/Types';
  11. import { usePaginationHook } from '../../hooks/Pagination';
  12. import icons from '../../components/icons/Icons';
  13. import EmptyCard from '../../components/cards/EmptyCard';
  14. import Status from '../../components/status/Status';
  15. import { ChildrenStatusType } from '../../components/status/Types';
  16. import StatusIcon from '../../components/status/StatusIcon';
  17. import CustomToolTip from '../../components/customToolTip/CustomToolTip';
  18. import { rootContext } from '../../context/Root';
  19. import CreateCollectionDialog from '../dialogs/CreateCollectionDialog';
  20. import { CollectionHttp } from '../../http/Collection';
  21. import LoadCollectionDialog from '../dialogs/LoadCollectionDialog';
  22. import ReleaseCollectionDialog from '../dialogs/ReleaseCollectionDialog';
  23. import DropCollectionDialog from '../dialogs/DropCollectionDialog';
  24. import Highlighter from 'react-highlight-words';
  25. import InsertDialog from '../dialogs/insert/Dialog';
  26. import ImportSampleDialog from '../dialogs/ImportSampleDialog';
  27. import { MilvusHttp } from '../../http/Milvus';
  28. import { LOADING_STATE } from '../../consts/Milvus';
  29. import { webSokcetContext } from '../../context/WebSocket';
  30. import { WS_EVENTS, WS_EVENTS_TYPE } from '../../consts/Http';
  31. import { checkIndexBuilding, checkLoading } from '../../utils/Validation';
  32. import Aliases from './Aliases';
  33. const useStyles = makeStyles((theme: Theme) => ({
  34. emptyWrapper: {
  35. marginTop: theme.spacing(2),
  36. },
  37. icon: {
  38. fontSize: '20px',
  39. marginLeft: theme.spacing(0.5),
  40. },
  41. dialogContent: {
  42. lineHeight: '24px',
  43. fontSize: '16px',
  44. },
  45. link: {
  46. color: theme.palette.common.black,
  47. },
  48. highlight: {
  49. color: theme.palette.primary.main,
  50. backgroundColor: 'transparent',
  51. },
  52. }));
  53. const Collections = () => {
  54. useNavigationHook(ALL_ROUTER_TYPES.COLLECTIONS);
  55. const [searchParams] = useSearchParams();
  56. const [search, setSearch] = useState<string>(
  57. (searchParams.get('search') as string) || ''
  58. );
  59. const [loading, setLoading] = useState<boolean>(false);
  60. const [selectedCollections, setSelectedCollections] = useState<
  61. CollectionView[]
  62. >([]);
  63. const { setDialog, openSnackBar } = useContext(rootContext);
  64. const { collections, setCollections } = useContext(webSokcetContext);
  65. const { t: collectionTrans } = useTranslation('collection');
  66. const { t: btnTrans } = useTranslation('btn');
  67. const { t: successTrans } = useTranslation('success');
  68. const classes = useStyles();
  69. const LoadIcon = icons.load;
  70. const ReleaseIcon = icons.release;
  71. const InfoIcon = icons.info;
  72. const SourceIcon = icons.source;
  73. const fetchData = useCallback(async () => {
  74. try {
  75. setLoading(true);
  76. const res = await CollectionHttp.getCollections();
  77. const hasLoadingOrBuildingCollection = res.some(
  78. v => checkLoading(v) || checkIndexBuilding(v)
  79. );
  80. // if some collection is building index or loading, start pulling data
  81. if (hasLoadingOrBuildingCollection) {
  82. MilvusHttp.triggerCron({
  83. name: WS_EVENTS.COLLECTION,
  84. type: WS_EVENTS_TYPE.START,
  85. });
  86. }
  87. setCollections(res);
  88. } finally {
  89. setLoading(false);
  90. }
  91. }, [setCollections]);
  92. useEffect(() => {
  93. fetchData();
  94. }, [fetchData]);
  95. const formatCollections = useMemo(() => {
  96. const filteredCollections = search
  97. ? collections.filter(collection => collection._name.includes(search))
  98. : collections;
  99. const data = filteredCollections.map(v => {
  100. // const indexStatus = statusRes.find(item => item._name === v._name);
  101. Object.assign(v, {
  102. nameElement: (
  103. <Link to={`/collections/${v._name}`} className={classes.link}>
  104. <Highlighter
  105. textToHighlight={v._name}
  106. searchWords={[search]}
  107. highlightClassName={classes.highlight}
  108. />
  109. </Link>
  110. ),
  111. statusElement: (
  112. <Status status={v._status} percentage={v._loadedPercentage} />
  113. ),
  114. indexCreatingElement: (
  115. <StatusIcon type={v._indexState || ChildrenStatusType.FINISH} />
  116. ),
  117. _aliasElement: (
  118. <Aliases
  119. aliases={v._aliases}
  120. collectionName={v._name}
  121. onCreate={fetchData}
  122. onDelete={fetchData}
  123. />
  124. ),
  125. });
  126. return v;
  127. });
  128. return data;
  129. }, [search, collections]);
  130. const {
  131. pageSize,
  132. handlePageSize,
  133. currentPage,
  134. handleCurrentPage,
  135. total,
  136. data: collectionList,
  137. handleGridSort,
  138. order,
  139. orderBy,
  140. } = usePaginationHook(formatCollections);
  141. const handleInsert = async (
  142. collectionName: string,
  143. partitionName: string,
  144. fieldData: any[]
  145. ): Promise<{ result: boolean; msg: string }> => {
  146. const param: InsertDataParam = {
  147. partition_names: [partitionName],
  148. fields_data: fieldData,
  149. };
  150. try {
  151. await CollectionHttp.insertData(collectionName, param);
  152. await MilvusHttp.flush(collectionName);
  153. // update collections
  154. fetchData();
  155. return { result: true, msg: '' };
  156. } catch (err: any) {
  157. const {
  158. response: {
  159. data: { message },
  160. },
  161. } = err;
  162. return { result: false, msg: message || '' };
  163. }
  164. };
  165. const onCreate = () => {
  166. openSnackBar(
  167. successTrans('create', { name: collectionTrans('collection') })
  168. );
  169. fetchData();
  170. };
  171. const onRelease = async () => {
  172. openSnackBar(
  173. successTrans('release', { name: collectionTrans('collection') })
  174. );
  175. fetchData();
  176. };
  177. const onLoad = () => {
  178. openSnackBar(successTrans('load', { name: collectionTrans('collection') }));
  179. fetchData();
  180. };
  181. const onDelete = () => {
  182. openSnackBar(
  183. successTrans('delete', { name: collectionTrans('collection') })
  184. );
  185. fetchData();
  186. setSelectedCollections([]);
  187. };
  188. const handleSearch = (value: string) => {
  189. setSearch(value);
  190. };
  191. const toolbarConfigs: ToolBarConfig[] = [
  192. {
  193. label: collectionTrans('create'),
  194. onClick: () => {
  195. setDialog({
  196. open: true,
  197. type: 'custom',
  198. params: {
  199. component: <CreateCollectionDialog onCreate={onCreate} />,
  200. },
  201. });
  202. },
  203. icon: 'add',
  204. },
  205. {
  206. label: btnTrans('insert'),
  207. onClick: () => {
  208. setDialog({
  209. open: true,
  210. type: 'custom',
  211. params: {
  212. component: (
  213. <InsertDialog
  214. collections={formatCollections}
  215. defaultSelectedCollection={
  216. selectedCollections.length === 1
  217. ? selectedCollections[0]._name
  218. : ''
  219. }
  220. // user can't select partition on collection page, so default value is ''
  221. defaultSelectedPartition={''}
  222. handleInsert={handleInsert}
  223. />
  224. ),
  225. },
  226. });
  227. },
  228. /**
  229. * insert validation:
  230. * 1. At least 1 available collection
  231. * 2. selected collections quantity shouldn't over 1
  232. */
  233. disabled: () =>
  234. collectionList.length === 0 || selectedCollections.length > 1,
  235. btnVariant: 'outlined',
  236. },
  237. {
  238. type: 'iconBtn',
  239. onClick: () => {
  240. fetchData();
  241. },
  242. label: collectionTrans('delete'),
  243. icon: 'refresh',
  244. },
  245. {
  246. type: 'iconBtn',
  247. onClick: () => {
  248. setDialog({
  249. open: true,
  250. type: 'custom',
  251. params: {
  252. component: (
  253. <DropCollectionDialog
  254. onDelete={onDelete}
  255. collections={selectedCollections}
  256. />
  257. ),
  258. },
  259. });
  260. },
  261. label: collectionTrans('delete'),
  262. icon: 'delete',
  263. // tooltip: collectionTrans('deleteTooltip'),
  264. disabledTooltip: collectionTrans('deleteTooltip'),
  265. disabled: data => data.length === 0,
  266. },
  267. {
  268. label: 'Search',
  269. icon: 'search',
  270. searchText: search,
  271. onSearch: (value: string) => {
  272. handleSearch(value);
  273. },
  274. },
  275. ];
  276. const colDefinitions: ColDefinitionsType[] = [
  277. {
  278. id: 'nameElement',
  279. align: 'left',
  280. disablePadding: true,
  281. sortBy: '_name',
  282. label: collectionTrans('name'),
  283. },
  284. {
  285. id: 'statusElement',
  286. align: 'left',
  287. disablePadding: false,
  288. sortBy: '_status',
  289. label: collectionTrans('status'),
  290. },
  291. {
  292. id: '_rowCount',
  293. align: 'left',
  294. disablePadding: false,
  295. label: (
  296. <span className="flex-center">
  297. {collectionTrans('rowCount')}
  298. <CustomToolTip title={collectionTrans('entityCountInfo')}>
  299. <InfoIcon classes={{ root: classes.icon }} />
  300. </CustomToolTip>
  301. </span>
  302. ),
  303. },
  304. {
  305. id: '_aliasElement',
  306. align: 'left',
  307. disablePadding: false,
  308. label: (
  309. <span className="flex-center">
  310. {collectionTrans('alias')}
  311. <CustomToolTip title={collectionTrans('aliasInfo')}>
  312. <InfoIcon classes={{ root: classes.icon }} />
  313. </CustomToolTip>
  314. </span>
  315. ),
  316. },
  317. {
  318. id: 'consistency_level',
  319. align: 'left',
  320. disablePadding: false,
  321. label: (
  322. <span className="flex-center">
  323. {collectionTrans('consistencyLevel')}
  324. <CustomToolTip title={collectionTrans('consistencyLevelInfo')}>
  325. <InfoIcon classes={{ root: classes.icon }} />
  326. </CustomToolTip>
  327. </span>
  328. ),
  329. },
  330. {
  331. id: '_desc',
  332. align: 'left',
  333. disablePadding: false,
  334. label: collectionTrans('desc'),
  335. },
  336. {
  337. id: '_createdTime',
  338. align: 'left',
  339. disablePadding: false,
  340. label: collectionTrans('createdTime'),
  341. },
  342. // {
  343. // id: 'indexCreatingElement',
  344. // align: 'left',
  345. // disablePadding: false,
  346. // label: '',
  347. // },
  348. {
  349. id: 'action',
  350. align: 'center',
  351. disablePadding: false,
  352. label: '',
  353. showActionCell: true,
  354. isHoverAction: true,
  355. actionBarConfigs: [
  356. {
  357. onClick: (e: React.MouseEvent, row: CollectionView) => {
  358. setDialog({
  359. open: true,
  360. type: 'custom',
  361. params: {
  362. component:
  363. row._status === LOADING_STATE.UNLOADED ? (
  364. <LoadCollectionDialog
  365. collection={row._name}
  366. onLoad={onLoad}
  367. />
  368. ) : (
  369. <ReleaseCollectionDialog
  370. collection={row._name}
  371. onRelease={onRelease}
  372. />
  373. ),
  374. },
  375. });
  376. e.preventDefault();
  377. },
  378. icon: 'load',
  379. label: 'load',
  380. showIconMethod: 'renderFn',
  381. getLabel: (row: CollectionView) =>
  382. row._status === LOADING_STATE.UNLOADED ? 'load' : 'release',
  383. renderIconFn: (row: CollectionView) =>
  384. row._status === LOADING_STATE.UNLOADED ? (
  385. <LoadIcon />
  386. ) : (
  387. <ReleaseIcon />
  388. ),
  389. },
  390. ],
  391. },
  392. {
  393. id: 'import',
  394. align: 'center',
  395. disablePadding: false,
  396. label: '',
  397. showActionCell: true,
  398. isHoverAction: true,
  399. actionBarConfigs: [
  400. {
  401. onClick: (e: React.MouseEvent, row: CollectionView) => {
  402. setDialog({
  403. open: true,
  404. type: 'custom',
  405. params: {
  406. component: <ImportSampleDialog collection={row._name} />,
  407. },
  408. });
  409. },
  410. icon: 'source',
  411. label: 'Import',
  412. showIconMethod: 'renderFn',
  413. getLabel: () => 'Import sample data',
  414. renderIconFn: (row: CollectionView) => <SourceIcon />,
  415. },
  416. ],
  417. },
  418. ];
  419. const handleSelectChange = (value: any) => {
  420. setSelectedCollections(value);
  421. };
  422. const handlePageChange = (e: any, page: number) => {
  423. handleCurrentPage(page);
  424. setSelectedCollections([]);
  425. };
  426. const CollectionIcon = icons.navCollection;
  427. return (
  428. <section className="page-wrapper">
  429. {collections.length > 0 || loading ? (
  430. <AttuGrid
  431. toolbarConfigs={toolbarConfigs}
  432. colDefinitions={colDefinitions}
  433. rows={collectionList}
  434. rowCount={total}
  435. primaryKey="_name"
  436. selected={selectedCollections}
  437. setSelected={handleSelectChange}
  438. page={currentPage}
  439. onChangePage={handlePageChange}
  440. rowsPerPage={pageSize}
  441. setRowsPerPage={handlePageSize}
  442. isLoading={loading}
  443. handleSort={handleGridSort}
  444. order={order}
  445. orderBy={orderBy}
  446. />
  447. ) : (
  448. <>
  449. <CustomToolBar toolbarConfigs={toolbarConfigs} />
  450. <EmptyCard
  451. wrapperClass={`page-empty-card ${classes.emptyWrapper}`}
  452. icon={<CollectionIcon />}
  453. text={collectionTrans('noData')}
  454. />
  455. </>
  456. )}
  457. </section>
  458. );
  459. };
  460. export default Collections;