Segments.tsx 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. import { useEffect, useState, useContext } from 'react';
  2. import { useTranslation } from 'react-i18next';
  3. import { useParams } from 'react-router-dom';
  4. import { SegmentService } from '@/http';
  5. import { usePaginationHook } from '@/hooks';
  6. import { rootContext } from '@/context';
  7. import AttuGrid from '@/components/grid/Grid';
  8. import CustomToolBar from '@/components/grid/ToolBar';
  9. import CompactDialog from '@/pages/dialogs/CompactDialog';
  10. import FlushDialog from '@/pages/dialogs/FlushDialog';
  11. import { getQueryStyles } from '../data/Styles';
  12. import { getLabelDisplayedRows } from '../../../search/Utils';
  13. import type { ColDefinitionsType } from '@/components/grid/Types';
  14. import type { ToolBarConfig } from '@/components/grid/Types';
  15. import type { Segment } from './Types';
  16. const Segments = () => {
  17. const { collectionName = '' } = useParams<{ collectionName: string }>();
  18. const classes = getQueryStyles();
  19. const { setDialog } = useContext(rootContext);
  20. const [segments, setSegments] = useState<Segment[]>([]);
  21. const { t: collectionTrans } = useTranslation('collection');
  22. const { t: btnTrans } = useTranslation('btn');
  23. const { t: commonTrans } = useTranslation();
  24. const [loading, setLoading] = useState<boolean>(true);
  25. const fetchSegments = async () => {
  26. setLoading(true);
  27. const psegments = await SegmentService.getPSegments(collectionName);
  28. const qsegments = await SegmentService.getQSegments(collectionName);
  29. const combinedArray = psegments.map(p => {
  30. const q: any = qsegments.find(q => q.segmentID === p.segmentID)! || {};
  31. return {
  32. ...p,
  33. ...Object.keys(q).reduce((acc, key) => {
  34. acc[`q_${key}`] = q[key];
  35. return acc;
  36. }, {} as any),
  37. };
  38. });
  39. setSegments(combinedArray);
  40. setLoading(false);
  41. };
  42. const onCompactExecuted = async () => {
  43. await fetchSegments();
  44. };
  45. const toolbarConfigs: ToolBarConfig[] = [
  46. {
  47. type: 'button',
  48. btnVariant: 'text',
  49. onClick: () => {
  50. setDialog({
  51. open: true,
  52. type: 'custom',
  53. params: {
  54. component: (
  55. <CompactDialog
  56. collectionName={collectionName}
  57. cb={onCompactExecuted}
  58. />
  59. ),
  60. },
  61. });
  62. },
  63. label: btnTrans('compact'),
  64. icon: 'compact',
  65. },
  66. {
  67. type: 'button',
  68. btnVariant: 'text',
  69. onClick: () => {
  70. setDialog({
  71. open: true,
  72. type: 'custom',
  73. params: {
  74. component: (
  75. <FlushDialog
  76. collectionName={collectionName}
  77. cb={onCompactExecuted}
  78. />
  79. ),
  80. },
  81. });
  82. },
  83. label: btnTrans('flush'),
  84. icon: 'flush',
  85. },
  86. {
  87. type: 'button',
  88. btnVariant: 'text',
  89. onClick: () => {
  90. fetchSegments();
  91. },
  92. label: btnTrans('refresh'),
  93. icon: 'refresh',
  94. },
  95. ];
  96. const colDefinitions: ColDefinitionsType[] = [
  97. {
  98. id: 'segmentID',
  99. align: 'left',
  100. disablePadding: false,
  101. needCopy: true,
  102. label: 'ID',
  103. getStyle: () => {
  104. return {
  105. minWidth: 190,
  106. };
  107. },
  108. },
  109. {
  110. id: 'level',
  111. align: 'left',
  112. disablePadding: false,
  113. label: 'Level',
  114. getStyle: () => {
  115. return {};
  116. },
  117. },
  118. {
  119. id: 'partitionID',
  120. align: 'left',
  121. disablePadding: false,
  122. needCopy: true,
  123. label: collectionTrans('partitionID'),
  124. getStyle: () => {
  125. return {
  126. minWidth: 190,
  127. };
  128. },
  129. },
  130. {
  131. id: 'state',
  132. align: 'left',
  133. disablePadding: false,
  134. label: collectionTrans('segPState'),
  135. getStyle: () => {
  136. return {
  137. minWidth: 200,
  138. };
  139. },
  140. },
  141. {
  142. id: 'num_rows',
  143. align: 'left',
  144. disablePadding: false,
  145. label: collectionTrans('num_rows'),
  146. getStyle: () => {
  147. return {
  148. minWidth: 100,
  149. };
  150. },
  151. },
  152. {
  153. id: 'q_nodeIds',
  154. align: 'left',
  155. disablePadding: false,
  156. label: collectionTrans('q_nodeIds'),
  157. formatter(data, cellData, cellIndex) {
  158. return cellData.join(',');
  159. },
  160. getStyle: () => {
  161. return {
  162. minWidth: 140,
  163. };
  164. },
  165. },
  166. {
  167. id: 'q_state',
  168. align: 'left',
  169. disablePadding: false,
  170. label: collectionTrans('q_state'),
  171. getStyle: () => {
  172. return {
  173. minWidth: 200,
  174. };
  175. },
  176. },
  177. // {
  178. // id: 'q_index_name',
  179. // align: 'left',
  180. // disablePadding: false,
  181. // label: collectionTrans('q_index_name'),
  182. // },
  183. ];
  184. useEffect(() => {
  185. fetchSegments();
  186. }, [collectionName]);
  187. const {
  188. pageSize,
  189. handlePageSize,
  190. currentPage,
  191. handleCurrentPage,
  192. total,
  193. data,
  194. order,
  195. orderBy,
  196. handleGridSort,
  197. } = usePaginationHook(segments);
  198. const handlePageChange = (e: any, page: number) => {
  199. handleCurrentPage(page);
  200. };
  201. return (
  202. <div className={classes.root}>
  203. <CustomToolBar toolbarConfigs={toolbarConfigs} />
  204. <AttuGrid
  205. toolbarConfigs={[]}
  206. colDefinitions={colDefinitions}
  207. rows={data}
  208. rowCount={total}
  209. primaryKey="name"
  210. showPagination={true}
  211. openCheckBox={false}
  212. page={currentPage}
  213. tableHeaderHeight={46}
  214. rowHeight={40}
  215. onPageChange={handlePageChange}
  216. rowsPerPage={pageSize}
  217. setRowsPerPage={handlePageSize}
  218. isLoading={loading}
  219. order={order}
  220. orderBy={orderBy}
  221. handleSort={handleGridSort}
  222. labelDisplayedRows={getLabelDisplayedRows(
  223. commonTrans(data.length > 1 ? 'grid.segments' : 'grid.segment')
  224. )}
  225. />
  226. </div>
  227. );
  228. };
  229. export default Segments;