Metrics.tsx 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import React from 'react';
  2. import {
  3. useAnalyzeTopic,
  4. useCancelTopicAnalysis,
  5. useTopicAnalysis,
  6. } from 'lib/hooks/api/topics';
  7. import useAppParams from 'lib/hooks/useAppParams';
  8. import { RouteParamsClusterTopic } from 'lib/paths';
  9. import { Button } from 'components/common/Button/Button';
  10. import * as Informers from 'components/common/Metrics';
  11. import ProgressBar from 'components/common/ProgressBar/ProgressBar';
  12. import {
  13. List,
  14. Label,
  15. } from 'components/common/PropertiesList/PropertiesList.styled';
  16. import BytesFormatted from 'components/common/BytesFormatted/BytesFormatted';
  17. import { useTimeFormat } from 'lib/hooks/useTimeFormat';
  18. import * as S from './Statistics.styles';
  19. import Total from './Indicators/Total';
  20. import SizeStats from './Indicators/SizeStats';
  21. import PartitionTable from './PartitionTable';
  22. const Metrics: React.FC = () => {
  23. const formatTimestamp = useTimeFormat();
  24. const params = useAppParams<RouteParamsClusterTopic>();
  25. const [isAnalyzing, setIsAnalyzing] = React.useState(true);
  26. const analyzeTopic = useAnalyzeTopic(params);
  27. const cancelTopicAnalysis = useCancelTopicAnalysis(params);
  28. const { data } = useTopicAnalysis(params, isAnalyzing);
  29. React.useEffect(() => {
  30. if (data && !data.progress) {
  31. setIsAnalyzing(false);
  32. }
  33. }, [data]);
  34. if (!data) {
  35. return null;
  36. }
  37. if (data.progress) {
  38. return (
  39. <S.ProgressContainer>
  40. <ProgressBar completed={data.progress.completenessPercent || 0} />
  41. <Button
  42. onClick={async () => {
  43. await cancelTopicAnalysis.mutateAsync();
  44. setIsAnalyzing(true);
  45. }}
  46. buttonType="primary"
  47. buttonSize="M"
  48. >
  49. Stop Analysis
  50. </Button>
  51. <List>
  52. <Label>Started at</Label>
  53. <span>{formatTimestamp(data.progress.startedAt, 'hh:mm:ss a')}</span>
  54. <Label>Scanned messages</Label>
  55. <span>
  56. {data.progress.msgsScanned} /{' '}
  57. <BytesFormatted value={data.progress.bytesScanned} />
  58. </span>
  59. </List>
  60. </S.ProgressContainer>
  61. );
  62. }
  63. if (!data.result) {
  64. return null;
  65. }
  66. const totalStats = data.result.totalStats || {};
  67. const partitionStats = data.result.partitionStats || [];
  68. return (
  69. <>
  70. <S.ActionsBar>
  71. <S.CreatedAt>{formatTimestamp(data?.result?.finishedAt)}</S.CreatedAt>
  72. <Button
  73. onClick={async () => {
  74. await analyzeTopic.mutateAsync();
  75. setIsAnalyzing(true);
  76. }}
  77. buttonType="primary"
  78. buttonSize="S"
  79. >
  80. Restart Analysis
  81. </Button>
  82. </S.ActionsBar>
  83. <Informers.Wrapper>
  84. <Total {...totalStats} />
  85. {totalStats.keySize && (
  86. <SizeStats stats={totalStats.keySize} title="Key size" />
  87. )}
  88. {totalStats.valueSize && (
  89. <SizeStats stats={totalStats.valueSize} title="Value size" />
  90. )}
  91. </Informers.Wrapper>
  92. <PartitionTable data={partitionStats} />
  93. </>
  94. );
  95. };
  96. export default Metrics;