Query.tsx 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. import { FC, useEffect, useState, useRef, useMemo, useContext } from 'react';
  2. import { useTranslation } from 'react-i18next';
  3. import { rootContext } from '../../context/Root';
  4. import EmptyCard from '../../components/cards/EmptyCard';
  5. import icons from '../../components/icons/Icons';
  6. import CustomButton from '../../components/customButton/CustomButton';
  7. import MilvusGrid from '../../components/grid/Grid';
  8. import { ToolBarConfig } from '../../components/grid/Types';
  9. import { getQueryStyles } from './Styles';
  10. import Filter from '../../components/advancedSearch';
  11. import { CollectionHttp } from '../../http/Collection';
  12. import { FieldHttp } from '../../http/Field';
  13. import { usePaginationHook } from '../../hooks/Pagination';
  14. // import { useTimeTravelHook } from '../../hooks/TimeTravel';
  15. import CopyButton from '../../components/advancedSearch/CopyButton';
  16. import DeleteTemplate from '../../components/customDialog/DeleteDialogTemplate';
  17. import CustomToolBar from '../../components/grid/ToolBar';
  18. // import { CustomDatePicker } from '../../components/customDatePicker/CustomDatePicker';
  19. import { saveAs } from 'file-saver';
  20. import { generateCsvData } from '../../utils/Format';
  21. import { DataTypeStringEnum } from '../collections/Types';
  22. const Query: FC<{
  23. collectionName: string;
  24. }> = ({ collectionName }) => {
  25. const [fields, setFields] = useState<any[]>([]);
  26. const [expression, setExpression] = useState('');
  27. const [tableLoading, setTableLoading] = useState<any>();
  28. const [queryResult, setQueryResult] = useState<any>();
  29. const [selectedDatas, setSelectedDatas] = useState<any[]>([]);
  30. const [primaryKey, setPrimaryKey] = useState<string>('');
  31. const { setDialog, handleCloseDialog, openSnackBar } =
  32. useContext(rootContext);
  33. const VectorSearchIcon = icons.vectorSearch;
  34. const ResetIcon = icons.refresh;
  35. const { t: dialogTrans } = useTranslation('dialog');
  36. const { t: successTrans } = useTranslation('success');
  37. const { t: searchTrans } = useTranslation('search');
  38. const { t: collectionTrans } = useTranslation('collection');
  39. const { t: btnTrans } = useTranslation('btn');
  40. const { t: commonTrans } = useTranslation();
  41. const copyTrans = commonTrans('copy');
  42. const classes = getQueryStyles();
  43. // const { timeTravel, setTimeTravel, timeTravelInfo, handleDateTimeChange } =
  44. // useTimeTravelHook();
  45. // Format result list
  46. const queryResultMemo = useMemo(
  47. () =>
  48. queryResult?.map((resultItem: { [key: string]: any }) => {
  49. // Iterate resultItem keys, then format vector(array) items.
  50. const tmp = Object.keys(resultItem).reduce(
  51. (prev: { [key: string]: any }, item: string) => {
  52. if (Array.isArray(resultItem[item])) {
  53. const list2Str = JSON.stringify(resultItem[item]);
  54. prev[item] = (
  55. <div className={classes.vectorTableCell}>
  56. <div>{list2Str}</div>
  57. <CopyButton
  58. label={copyTrans.label}
  59. value={list2Str}
  60. className={classes.copyBtn}
  61. />
  62. </div>
  63. );
  64. } else {
  65. prev[item] = `${resultItem[item]}`;
  66. }
  67. return prev;
  68. },
  69. {}
  70. );
  71. return tmp;
  72. }),
  73. [queryResult, classes.vectorTableCell, classes.copyBtn, copyTrans.label]
  74. );
  75. const csvDataMemo = useMemo(() => {
  76. const headers: string[] = fields?.map(i => i.name);
  77. if (headers?.length && queryResult?.length) {
  78. return generateCsvData(headers, queryResult);
  79. }
  80. return '';
  81. }, [fields, queryResult]);
  82. const {
  83. pageSize,
  84. handlePageSize,
  85. currentPage,
  86. handleCurrentPage,
  87. total,
  88. data: result,
  89. order,
  90. orderBy,
  91. handleGridSort,
  92. } = usePaginationHook(queryResultMemo || []);
  93. const handlePageChange = (e: any, page: number) => {
  94. handleCurrentPage(page);
  95. };
  96. const getFields = async (collectionName: string) => {
  97. const schemaList = await FieldHttp.getFields(collectionName);
  98. const nameList = schemaList.map(v => ({
  99. name: v.name,
  100. type: v.data_type,
  101. }));
  102. const primaryKey =
  103. schemaList.find(v => v._isPrimaryKey === true)?._fieldName || '';
  104. setPrimaryKey(primaryKey);
  105. // Temporarily hide bool field due to incorrect return from SDK.
  106. const fieldWithoutBool = nameList.filter(
  107. i => i.type !== DataTypeStringEnum.Bool
  108. );
  109. setFields(fieldWithoutBool);
  110. };
  111. // Get fields at first or collection name changed.
  112. useEffect(() => {
  113. collectionName && getFields(collectionName);
  114. }, [collectionName]);
  115. const filterRef = useRef();
  116. const handleFilterReset = () => {
  117. const currentFilter: any = filterRef.current;
  118. currentFilter?.getReset();
  119. setExpression('');
  120. setTableLoading(null);
  121. setQueryResult(null);
  122. };
  123. const handleFilterSubmit = (expression: string) => {
  124. setExpression(expression);
  125. setQueryResult(null);
  126. };
  127. const handleQuery = async () => {
  128. setTableLoading(true);
  129. try {
  130. const res = await CollectionHttp.queryData(collectionName, {
  131. expr: expression,
  132. output_fields: fields.map(i => i.name),
  133. // travel_timestamp: timeTravelInfo.timestamp,
  134. });
  135. const result = res.data;
  136. setQueryResult(result);
  137. } catch (err) {
  138. setQueryResult([]);
  139. } finally {
  140. setTableLoading(false);
  141. }
  142. };
  143. const handleSelectChange = (value: any) => {
  144. setSelectedDatas(value);
  145. };
  146. const handleDelete = async () => {
  147. await CollectionHttp.deleteEntities(collectionName, {
  148. expr: `${primaryKey} in [${selectedDatas.map(v => v.id).join(',')}]`,
  149. });
  150. handleCloseDialog();
  151. openSnackBar(successTrans('delete', { name: collectionTrans('entites') }));
  152. handleQuery();
  153. };
  154. const toolbarConfigs: ToolBarConfig[] = [
  155. {
  156. type: 'iconBtn',
  157. onClick: () => {
  158. setDialog({
  159. open: true,
  160. type: 'custom',
  161. params: {
  162. component: (
  163. <DeleteTemplate
  164. label={btnTrans('delete')}
  165. title={dialogTrans('deleteTitle', {
  166. type: collectionTrans('entites'),
  167. })}
  168. text={collectionTrans('deleteDataWarning')}
  169. handleDelete={handleDelete}
  170. />
  171. ),
  172. },
  173. });
  174. },
  175. label: collectionTrans('delete'),
  176. icon: 'delete',
  177. // tooltip: collectionTrans('deleteTooltip'),
  178. disabledTooltip: collectionTrans('deleteTooltip'),
  179. disabled: () => selectedDatas.length === 0,
  180. },
  181. {
  182. type: 'iconBtn',
  183. onClick: () => {
  184. const csvData = new Blob([csvDataMemo.toString()], {
  185. type: 'text/csv;charset=utf-8',
  186. });
  187. saveAs(csvData, 'query_result.csv');
  188. },
  189. label: collectionTrans('delete'),
  190. icon: 'download',
  191. tooltip: collectionTrans('download'),
  192. disabledTooltip: collectionTrans('downloadTooltip'),
  193. disabled: () => !queryResult?.length,
  194. },
  195. ];
  196. return (
  197. <div className={classes.root}>
  198. <CustomToolBar toolbarConfigs={toolbarConfigs} />
  199. <div className={classes.toolbar}>
  200. <div className="left">
  201. {/* <div className="expression"> */}
  202. <div>{`${expression || collectionTrans('exprPlaceHolder')}`}</div>
  203. <Filter
  204. ref={filterRef}
  205. title="Advanced Filter"
  206. fields={fields}
  207. filterDisabled={false}
  208. onSubmit={handleFilterSubmit}
  209. showTitle={false}
  210. showTooltip={false}
  211. />
  212. {/* </div> */}
  213. {/* <CustomDatePicker
  214. label={timeTravelInfo.label}
  215. onChange={handleDateTimeChange}
  216. date={timeTravel}
  217. setDate={setTimeTravel}
  218. /> */}
  219. </div>
  220. <div className="right">
  221. <CustomButton className="btn" onClick={handleFilterReset}>
  222. <ResetIcon classes={{ root: 'icon' }} />
  223. {btnTrans('reset')}
  224. </CustomButton>
  225. <CustomButton
  226. variant="contained"
  227. disabled={!expression}
  228. onClick={() => handleQuery()}
  229. >
  230. {btnTrans('query')}
  231. </CustomButton>
  232. </div>
  233. </div>
  234. {tableLoading || queryResult?.length ? (
  235. <MilvusGrid
  236. toolbarConfigs={[]}
  237. colDefinitions={fields.map(i => ({
  238. id: i.name,
  239. align: 'left',
  240. disablePadding: false,
  241. label: i.name,
  242. }))}
  243. primaryKey={fields.find(i => i.is_primary_key)?.name}
  244. openCheckBox={true}
  245. isLoading={!!tableLoading}
  246. rows={result}
  247. rowCount={total}
  248. selected={selectedDatas}
  249. setSelected={handleSelectChange}
  250. page={currentPage}
  251. onChangePage={handlePageChange}
  252. rowsPerPage={pageSize}
  253. setRowsPerPage={handlePageSize}
  254. orderBy={orderBy}
  255. order={order}
  256. handleSort={handleGridSort}
  257. />
  258. ) : (
  259. <EmptyCard
  260. wrapperClass={`page-empty-card ${classes.emptyCard}`}
  261. icon={<VectorSearchIcon />}
  262. text={
  263. queryResult?.length === 0
  264. ? searchTrans('empty')
  265. : collectionTrans('startTip')
  266. }
  267. />
  268. )}
  269. </div>
  270. );
  271. };
  272. export default Query;