ClustersWidget.tsx 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import React from 'react';
  2. import * as Metrics from 'components/common/Metrics';
  3. import { Tag } from 'components/common/Tag/Tag.styled';
  4. import Switch from 'components/common/Switch/Switch';
  5. import { useClusters } from 'lib/hooks/api/clusters';
  6. import { Cluster, ServerStatus } from 'generated-sources';
  7. import { ColumnDef } from '@tanstack/react-table';
  8. import Table, { SizeCell } from 'components/common/NewTable';
  9. import * as S from './ClustersWidget.styled';
  10. import ClusterName from './ClusterName';
  11. const ClustersWidget: React.FC = () => {
  12. const { data } = useClusters();
  13. const [showOfflineOnly, setShowOfflineOnly] = React.useState<boolean>(false);
  14. const config = React.useMemo(() => {
  15. const clusters = data || [];
  16. const offlineClusters = clusters.filter(
  17. ({ status }) => status === ServerStatus.OFFLINE
  18. );
  19. return {
  20. list: showOfflineOnly ? offlineClusters : clusters,
  21. online: clusters.length - offlineClusters.length,
  22. offline: offlineClusters.length,
  23. };
  24. }, [data, showOfflineOnly]);
  25. const columns = React.useMemo<ColumnDef<Cluster>[]>(
  26. () => [
  27. { header: 'Cluster name', accessorKey: 'name', cell: ClusterName },
  28. { header: 'Version', accessorKey: 'version' },
  29. { header: 'Brokers count', accessorKey: 'brokerCount' },
  30. { header: 'Partitions', accessorKey: 'onlinePartitionCount' },
  31. { header: 'Topics', accessorKey: 'topicCount' },
  32. { header: 'Production', accessorKey: 'bytesInPerSec', cell: SizeCell },
  33. { header: 'Consumption', accessorKey: 'bytesOutPerSec', cell: SizeCell },
  34. ],
  35. []
  36. );
  37. const handleSwitch = () => setShowOfflineOnly(!showOfflineOnly);
  38. return (
  39. <>
  40. <Metrics.Wrapper>
  41. <Metrics.Section>
  42. <Metrics.Indicator label={<Tag color="green">Online</Tag>}>
  43. <span>{config.online}</span>{' '}
  44. <Metrics.LightText>clusters</Metrics.LightText>
  45. </Metrics.Indicator>
  46. <Metrics.Indicator label={<Tag color="gray">Offline</Tag>}>
  47. <span>{config.offline}</span>{' '}
  48. <Metrics.LightText>clusters</Metrics.LightText>
  49. </Metrics.Indicator>
  50. </Metrics.Section>
  51. </Metrics.Wrapper>
  52. <S.SwitchWrapper>
  53. <Switch
  54. name="switchRoundedDefault"
  55. checked={showOfflineOnly}
  56. onChange={handleSwitch}
  57. />
  58. <label>Only offline clusters</label>
  59. </S.SwitchWrapper>
  60. <Table
  61. columns={columns}
  62. data={config?.list}
  63. enableSorting
  64. emptyMessage="Disk usage data not available"
  65. />
  66. </>
  67. );
  68. };
  69. export default ClustersWidget;