Metrics.tsx 3.8 KB

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