Metrics.tsx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 * as Informers from 'components/common/Metrics';
  10. import ProgressBar from 'components/common/ProgressBar/ProgressBar';
  11. import {
  12. List,
  13. Label,
  14. } from 'components/common/PropertiesList/PropertiesList.styled';
  15. import BytesFormatted from 'components/common/BytesFormatted/BytesFormatted';
  16. import { calculateTimer, formatTimestamp } from 'lib/dateTimeHelpers';
  17. import { Action, ResourceType } from 'generated-sources';
  18. import { ActionButton } from 'components/common/ActionComponent';
  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 params = useAppParams<RouteParamsClusterTopic>();
  25. const [isAnalyzing, setIsAnalyzing] = useState(true);
  26. const analyzeTopic = useAnalyzeTopic(params);
  27. const cancelTopicAnalysis = useCancelTopicAnalysis(params);
  28. const { data } = useTopicAnalysis(params, isAnalyzing);
  29. 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. <S.ProgressPct> {Math.floor(data.progress.completenessPercent || 0)}%</S.ProgressPct>
  41. <S.ProgressBarWrapper>
  42. <ProgressBar completed={data.progress.completenessPercent || 0} />
  43. </S.ProgressBarWrapper>
  44. <ActionButton
  45. onClick={async () => {
  46. await cancelTopicAnalysis.mutateAsync();
  47. setIsAnalyzing(true);
  48. }}
  49. buttonType="secondary"
  50. buttonSize="M"
  51. permission={{
  52. resource: ResourceType.TOPIC,
  53. action: Action.MESSAGES_READ,
  54. value: params.topicName,
  55. }}
  56. >
  57. Stop Analysis
  58. </ActionButton>
  59. <List>
  60. <Label>Started at</Label>
  61. <span>
  62. {formatTimestamp(data.progress.startedAt, {
  63. hour: 'numeric',
  64. minute: 'numeric',
  65. second: 'numeric',
  66. })}
  67. </span>
  68. <Label>Passed since start</Label>
  69. <span>{calculateTimer(data.progress.startedAt as number)}</span>
  70. <Label>Scanned messages</Label>
  71. <span>{data.progress.msgsScanned}</span>
  72. <Label>Scanned size</Label>
  73. <span>
  74. <BytesFormatted value={data.progress.bytesScanned} />
  75. </span>
  76. </List>
  77. </S.ProgressContainer>
  78. );
  79. }
  80. if (!data.result) {
  81. return null;
  82. }
  83. const totalStats = data.result.totalStats || {};
  84. const partitionStats = data.result.partitionStats || [];
  85. return (
  86. <>
  87. <S.ActionsBar>
  88. <S.CreatedAt>{formatTimestamp(data?.result?.finishedAt)}</S.CreatedAt>
  89. <ActionButton
  90. onClick={async () => {
  91. await analyzeTopic.mutateAsync();
  92. setIsAnalyzing(true);
  93. }}
  94. buttonType="primary"
  95. buttonSize="S"
  96. permission={{
  97. resource: ResourceType.TOPIC,
  98. action: Action.MESSAGES_READ,
  99. value: params.topicName,
  100. }}
  101. >
  102. Restart Analysis
  103. </ActionButton>
  104. </S.ActionsBar>
  105. <Informers.Wrapper>
  106. <Total {...totalStats} />
  107. {totalStats.keySize && (
  108. <SizeStats stats={totalStats.keySize} title="Key size" />
  109. )}
  110. {totalStats.valueSize && (
  111. <SizeStats stats={totalStats.valueSize} title="Value size" />
  112. )}
  113. </Informers.Wrapper>
  114. <PartitionTable data={partitionStats} />
  115. </>
  116. );
  117. };
  118. export default Metrics;