Segments.tsx 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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 { ColDefinitionsType } from '@/components/grid/Types';
  9. import { ToolBarConfig } from '@/components/grid/Types';
  10. import CustomToolBar from '@/components/grid/ToolBar';
  11. import CompactDialog from '@/pages/dialogs/CompactDialog';
  12. import FlushDialog from '@/pages/dialogs/FlushDialog';
  13. import { getQueryStyles } from '../query/Styles';
  14. import { Segment } from './Types';
  15. import { getLabelDisplayedRows } from '../search/Utils';
  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 gridTrans = commonTrans('grid');
  25. const [loading, setLoading] = useState<boolean>(true);
  26. const fetchSegments = async () => {
  27. setLoading(true);
  28. const psegments = await SegmentService.getPSegments(collectionName);
  29. const qsegments = await SegmentService.getQSegments(collectionName);
  30. const combinedArray = psegments.map(p => {
  31. const q: any = qsegments.find(q => q.segmentID === p.segmentID)! || {};
  32. return {
  33. ...p,
  34. ...Object.keys(q).reduce((acc, key) => {
  35. acc[`q_${key}`] = q[key];
  36. return acc;
  37. }, {} as any),
  38. };
  39. });
  40. setSegments(combinedArray);
  41. setLoading(false);
  42. };
  43. const onCompactExecuted = async () => {
  44. await fetchSegments();
  45. };
  46. const toolbarConfigs: ToolBarConfig[] = [
  47. {
  48. type: 'button',
  49. btnVariant: 'text',
  50. onClick: () => {
  51. setDialog({
  52. open: true,
  53. type: 'custom',
  54. params: {
  55. component: (
  56. <CompactDialog
  57. collectionName={collectionName}
  58. cb={onCompactExecuted}
  59. />
  60. ),
  61. },
  62. });
  63. },
  64. label: btnTrans('compact'),
  65. icon: 'compact',
  66. },
  67. {
  68. type: 'button',
  69. btnVariant: 'text',
  70. onClick: () => {
  71. setDialog({
  72. open: true,
  73. type: 'custom',
  74. params: {
  75. component: (
  76. <FlushDialog
  77. collectionName={collectionName}
  78. cb={onCompactExecuted}
  79. />
  80. ),
  81. },
  82. });
  83. },
  84. label: btnTrans('flush'),
  85. icon: 'saveAs',
  86. },
  87. {
  88. type: 'button',
  89. btnVariant: 'text',
  90. onClick: () => {
  91. fetchSegments();
  92. },
  93. label: btnTrans('refresh'),
  94. icon: 'refresh',
  95. },
  96. ];
  97. const colDefinitions: ColDefinitionsType[] = [
  98. {
  99. id: 'segmentID',
  100. align: 'left',
  101. disablePadding: false,
  102. needCopy: true,
  103. label: collectionTrans('segmentID'),
  104. },
  105. {
  106. id: 'partitionID',
  107. align: 'left',
  108. disablePadding: false,
  109. needCopy: true,
  110. label: collectionTrans('partitionID'),
  111. },
  112. {
  113. id: 'state',
  114. align: 'left',
  115. disablePadding: false,
  116. label: collectionTrans('segPState'),
  117. },
  118. {
  119. id: 'num_rows',
  120. align: 'left',
  121. disablePadding: false,
  122. label: collectionTrans('num_rows'),
  123. },
  124. {
  125. id: 'q_nodeIds',
  126. align: 'left',
  127. disablePadding: false,
  128. label: collectionTrans('q_nodeIds'),
  129. formatter(data, cellData, cellIndex) {
  130. return cellData.join(',');
  131. },
  132. },
  133. {
  134. id: 'q_state',
  135. align: 'left',
  136. disablePadding: false,
  137. label: collectionTrans('q_state'),
  138. },
  139. // {
  140. // id: 'q_index_name',
  141. // align: 'left',
  142. // disablePadding: false,
  143. // label: collectionTrans('q_index_name'),
  144. // },
  145. ];
  146. useEffect(() => {
  147. fetchSegments();
  148. }, [collectionName]);
  149. const {
  150. pageSize,
  151. handlePageSize,
  152. currentPage,
  153. handleCurrentPage,
  154. total,
  155. data,
  156. order,
  157. orderBy,
  158. handleGridSort,
  159. } = usePaginationHook(segments);
  160. const handlePageChange = (e: any, page: number) => {
  161. handleCurrentPage(page);
  162. };
  163. return (
  164. <div className={classes.root}>
  165. <CustomToolBar toolbarConfigs={toolbarConfigs} />
  166. <AttuGrid
  167. toolbarConfigs={[]}
  168. colDefinitions={colDefinitions}
  169. rows={data}
  170. rowCount={total}
  171. primaryKey="name"
  172. showPagination={true}
  173. openCheckBox={false}
  174. page={currentPage}
  175. onPageChange={handlePageChange}
  176. rowsPerPage={pageSize}
  177. setRowsPerPage={handlePageSize}
  178. isLoading={loading}
  179. order={order}
  180. orderBy={orderBy}
  181. handleSort={handleGridSort}
  182. labelDisplayedRows={getLabelDisplayedRows(
  183. gridTrans[data.length > 1 ? 'segments' : 'segment']
  184. )}
  185. />
  186. </div>
  187. );
  188. };
  189. export default Segments;