Dashboard.tsx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import React, { useMemo } from 'react';
  2. import PageHeading from 'components/common/PageHeading/PageHeading';
  3. import * as Metrics from 'components/common/Metrics';
  4. import { Tag } from 'components/common/Tag/Tag.styled';
  5. import Switch from 'components/common/Switch/Switch';
  6. import { useClusters } from 'lib/hooks/api/clusters';
  7. import { Cluster, ResourceType, ServerStatus } from 'generated-sources';
  8. import { ColumnDef } from '@tanstack/react-table';
  9. import Table, { SizeCell } from 'components/common/NewTable';
  10. import useBoolean from 'lib/hooks/useBoolean';
  11. import { clusterNewConfigPath } from 'lib/paths';
  12. import { GlobalSettingsContext } from 'components/contexts/GlobalSettingsContext';
  13. import { ActionCanButton } from 'components/common/ActionComponent';
  14. import { useGetUserInfo } from 'lib/hooks/api/roles';
  15. import * as S from './Dashboard.styled';
  16. import ClusterName from './ClusterName';
  17. import ClusterTableActionsCell from './ClusterTableActionsCell';
  18. const Dashboard: React.FC = () => {
  19. const { data } = useGetUserInfo();
  20. const clusters = useClusters();
  21. const { value: showOfflineOnly, toggle } = useBoolean(false);
  22. const appInfo = React.useContext(GlobalSettingsContext);
  23. const config = React.useMemo(() => {
  24. const clusterList = clusters.data || [];
  25. const offlineClusters = clusterList.filter(
  26. ({ status }) => status === ServerStatus.OFFLINE
  27. );
  28. return {
  29. list: showOfflineOnly ? offlineClusters : clusterList,
  30. online: clusterList.length - offlineClusters.length,
  31. offline: offlineClusters.length,
  32. };
  33. }, [clusters, showOfflineOnly]);
  34. const columns = React.useMemo<ColumnDef<Cluster>[]>(() => {
  35. const initialColumns: ColumnDef<Cluster>[] = [
  36. { header: 'Cluster name', accessorKey: 'name', cell: ClusterName },
  37. { header: 'Version', accessorKey: 'version' },
  38. { header: 'Brokers count', accessorKey: 'brokerCount' },
  39. { header: 'Partitions', accessorKey: 'onlinePartitionCount' },
  40. { header: 'Topics', accessorKey: 'topicCount' },
  41. { header: 'Production', accessorKey: 'bytesInPerSec', cell: SizeCell },
  42. { header: 'Consumption', accessorKey: 'bytesOutPerSec', cell: SizeCell },
  43. ];
  44. if (appInfo.hasDynamicConfig) {
  45. initialColumns.push({
  46. header: '',
  47. id: 'actions',
  48. cell: ClusterTableActionsCell,
  49. });
  50. }
  51. return initialColumns;
  52. }, []);
  53. const isApplicationConfig = useMemo(() => {
  54. return !!data?.userInfo?.permissions.some(
  55. (permission) => permission.resource === ResourceType.APPLICATIONCONFIG
  56. );
  57. }, [data]);
  58. return (
  59. <>
  60. <PageHeading text="Dashboard" />
  61. <Metrics.Wrapper>
  62. <Metrics.Section>
  63. <Metrics.Indicator label={<Tag color="green">Online</Tag>}>
  64. <span>{config.online || 0}</span>{' '}
  65. <Metrics.LightText>clusters</Metrics.LightText>
  66. </Metrics.Indicator>
  67. <Metrics.Indicator label={<Tag color="gray">Offline</Tag>}>
  68. <span>{config.offline || 0}</span>{' '}
  69. <Metrics.LightText>clusters</Metrics.LightText>
  70. </Metrics.Indicator>
  71. </Metrics.Section>
  72. </Metrics.Wrapper>
  73. <S.Toolbar>
  74. <div>
  75. <Switch
  76. name="switchRoundedDefault"
  77. checked={showOfflineOnly}
  78. onChange={toggle}
  79. />
  80. <label>Only offline clusters</label>
  81. </div>
  82. {appInfo.hasDynamicConfig && (
  83. <ActionCanButton
  84. buttonType="primary"
  85. buttonSize="M"
  86. to={clusterNewConfigPath}
  87. canDoAction={isApplicationConfig}
  88. >
  89. Configure new cluster
  90. </ActionCanButton>
  91. )}
  92. </S.Toolbar>
  93. <Table
  94. columns={columns}
  95. data={config?.list}
  96. enableSorting
  97. emptyMessage={clusters.isFetched ? 'No clusters found' : 'Loading...'}
  98. />
  99. </>
  100. );
  101. };
  102. export default Dashboard;