Metrics.tsx 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import React, { useEffect, useState } 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 { calculateTimer } from 'lib/dateTimeHelpers';
  19. import * as S from './Statistics.styles';
  20. import Total from './Indicators/Total';
  21. import SizeStats from './Indicators/SizeStats';
  22. import PartitionTable from './PartitionTable';
  23. const Metrics: React.FC = () => {
  24. const formatTimestamp = useTimeFormat();
  25. const params = useAppParams<RouteParamsClusterTopic>();
  26. const [isAnalyzing, setIsAnalyzing] = useState(true);
  27. const analyzeTopic = useAnalyzeTopic(params);
  28. const cancelTopicAnalysis = useCancelTopicAnalysis(params);
  29. const { data } = useTopicAnalysis(params, isAnalyzing);
  30. useEffect(() => {
  31. if (data && !data.progress) {
  32. setIsAnalyzing(false);
  33. }
  34. }, [data]);
  35. if (!data) {
  36. return null;
  37. }
  38. if (data.progress) {
  39. return (
  40. <S.ProgressContainer>
  41. <S.ProgressBarWrapper>
  42. <ProgressBar completed={data.progress.completenessPercent || 0} />
  43. <span> {Math.floor(data.progress.completenessPercent || 0)} %</span>
  44. </S.ProgressBarWrapper>
  45. <Button
  46. onClick={async () => {
  47. await cancelTopicAnalysis.mutateAsync();
  48. setIsAnalyzing(true);
  49. }}
  50. buttonType="primary"
  51. buttonSize="M"
  52. >
  53. Stop Analysis
  54. </Button>
  55. <List>
  56. <Label>Started at</Label>
  57. <span>{formatTimestamp(data.progress.startedAt, 'hh:mm:ss a')}</span>
  58. <Label>Passed since start</Label>
  59. <span>{calculateTimer(data.progress.startedAt as number)}</span>
  60. <Label>Scanned messages</Label>
  61. <span>{data.progress.msgsScanned}</span>
  62. <Label>Scanned size</Label>
  63. <span>
  64. <BytesFormatted value={data.progress.bytesScanned} />
  65. </span>
  66. </List>
  67. </S.ProgressContainer>
  68. );
  69. }
  70. if (!data.result) {
  71. return null;
  72. }
  73. const totalStats = data.result.totalStats || {};
  74. const partitionStats = data.result.partitionStats || [];
  75. return (
  76. <>
  77. <S.ActionsBar>
  78. <S.CreatedAt>{formatTimestamp(data?.result?.finishedAt)}</S.CreatedAt>
  79. <Button
  80. onClick={async () => {
  81. await analyzeTopic.mutateAsync();
  82. setIsAnalyzing(true);
  83. }}
  84. buttonType="primary"
  85. buttonSize="S"
  86. >
  87. Restart Analysis
  88. </Button>
  89. </S.ActionsBar>
  90. <Informers.Wrapper>
  91. <Total {...totalStats} />
  92. {totalStats.keySize && (
  93. <SizeStats stats={totalStats.keySize} title="Key size" />
  94. )}
  95. {totalStats.valueSize && (
  96. <SizeStats stats={totalStats.valueSize} title="Value size" />
  97. )}
  98. </Informers.Wrapper>
  99. <PartitionTable data={partitionStats} />
  100. </>
  101. );
  102. };
  103. export default Metrics;