From f8a0db18fa01b9e9198e7c1ad4a656ba17de5922 Mon Sep 17 00:00:00 2001 From: Valerii Sidorenko Date: Mon, 7 Oct 2024 14:37:40 +0200 Subject: [PATCH] feat(Databases): use balancer + /node/:id as backend endpoint (#1418) --- .../ExtendedCluster/ExtendedCluster.tsx | 15 +++++++++++---- src/containers/Tenants/Tenants.tsx | 16 +++++++++++++--- src/store/reducers/tenants/types.ts | 1 + src/store/reducers/tenants/utils.ts | 5 ++++- src/types/additionalProps.ts | 2 +- 5 files changed, 30 insertions(+), 9 deletions(-) diff --git a/src/containers/AppWithClusters/ExtendedCluster/ExtendedCluster.tsx b/src/containers/AppWithClusters/ExtendedCluster/ExtendedCluster.tsx index 0a261b1cb..70e42e4cf 100644 --- a/src/containers/AppWithClusters/ExtendedCluster/ExtendedCluster.tsx +++ b/src/containers/AppWithClusters/ExtendedCluster/ExtendedCluster.tsx @@ -5,6 +5,7 @@ import type { AdditionalClusterProps, AdditionalTenantsProps, AdditionalVersionsProps, + NodeAddress, } from '../../../types/additionalProps'; import type {MetaClusterVersion} from '../../../types/api/meta'; import type {ETenantType} from '../../../types/api/tenant'; @@ -12,7 +13,7 @@ import {getVersionColors, getVersionMap} from '../../../utils/clusterVersionColo import {cn} from '../../../utils/cn'; import type {GetMonitoringClusterLink, GetMonitoringLink} from '../../../utils/monitoring'; import {getCleanBalancerValue, removeViewerPathname} from '../../../utils/parseBalancer'; -import {getBackendFromNodeHost} from '../../../utils/prepareBackend'; +import {getBackendFromNodeHost, getBackendFromRawNodeData} from '../../../utils/prepareBackend'; import type {Cluster} from '../../Cluster/Cluster'; import {useClusterData} from '../useClusterData'; @@ -76,7 +77,9 @@ const getAdditionalTenantsProps = ( ) => { const additionalTenantsProps: AdditionalTenantsProps = {}; - additionalTenantsProps.prepareTenantBackend = (nodeHost: string | undefined) => { + additionalTenantsProps.prepareTenantBackend = ( + nodeHostOrAddress: string | NodeAddress | undefined, + ) => { // Proxy received from balancer value, so it's necessary if (!balancer) { return undefined; @@ -86,11 +89,15 @@ const getAdditionalTenantsProps = ( return removeViewerPathname(balancer); } - if (!nodeHost) { + if (!nodeHostOrAddress) { return undefined; } - return getBackendFromNodeHost(nodeHost, balancer); + if (typeof nodeHostOrAddress === 'string') { + return getBackendFromNodeHost(nodeHostOrAddress, balancer); + } + + return getBackendFromRawNodeData(nodeHostOrAddress, balancer, true) ?? undefined; }; if (monitoring && getMonitoringLink) { diff --git a/src/containers/Tenants/Tenants.tsx b/src/containers/Tenants/Tenants.tsx index 12d6a8679..0547e94db 100644 --- a/src/containers/Tenants/Tenants.tsx +++ b/src/containers/Tenants/Tenants.tsx @@ -27,7 +27,7 @@ import { } from '../../store/reducers/tenants/selectors'; import {setSearchValue, tenantsApi} from '../../store/reducers/tenants/tenants'; import type {PreparedTenant} from '../../store/reducers/tenants/types'; -import type {AdditionalTenantsProps} from '../../types/additionalProps'; +import type {AdditionalTenantsProps, NodeAddress} from '../../types/additionalProps'; import {cn} from '../../utils/cn'; import {DEFAULT_TABLE_SETTINGS} from '../../utils/constants'; import { @@ -93,8 +93,18 @@ export const Tenants = ({additionalTenantsProps}: TenantsProps) => { const renderTable = () => { const getTenantBackend = (tenant: PreparedTenant) => { - const backend = tenant.MonitoringEndpoint ?? tenant.backend; - return additionalTenantsProps?.prepareTenantBackend?.(backend); + if (typeof additionalTenantsProps?.prepareTenantBackend !== 'function') { + return undefined; + } + + let backend: string | NodeAddress | undefined = + tenant.MonitoringEndpoint ?? tenant.backend; + const nodeIds = tenant.NodeIds ?? tenant.sharedNodeIds; + if (!backend && nodeIds && nodeIds.length > 0) { + const index = Math.floor(Math.random() * nodeIds.length); + backend = {NodeId: nodeIds[index]}; + } + return additionalTenantsProps.prepareTenantBackend(backend); }; const columns: Column[] = [ diff --git a/src/store/reducers/tenants/types.ts b/src/store/reducers/tenants/types.ts index 81d665d1a..9734c0063 100644 --- a/src/store/reducers/tenants/types.ts +++ b/src/store/reducers/tenants/types.ts @@ -6,6 +6,7 @@ import type {METRIC_STATUS} from './contants'; export interface PreparedTenant extends TTenant { backend: string | undefined; sharedTenantName: string | undefined; + sharedNodeIds: number[] | undefined; controlPlaneName: string; cpu: number | undefined; memory: number | undefined; diff --git a/src/store/reducers/tenants/utils.ts b/src/store/reducers/tenants/utils.ts index 755cdf936..155938f56 100644 --- a/src/store/reducers/tenants/utils.ts +++ b/src/store/reducers/tenants/utils.ts @@ -178,7 +178,9 @@ const calculateTenantEntities = (tenant: TTenant) => { export const prepareTenants = (tenants: TTenant[], useNodeAsBackend: boolean): PreparedTenant[] => { return tenants.map((tenant) => { const backend = useNodeAsBackend ? getTenantBackend(tenant) : undefined; - const sharedTenantName = tenants.find((item) => item.Id === tenant.ResourceId)?.Name; + const sharedDatabase = tenants.find((item) => item.Id === tenant.ResourceId); + const sharedTenantName = sharedDatabase?.Name; + const sharedNodeIds = sharedDatabase?.NodeIds; const controlPlaneName = getControlPlaneValue(tenant); const {cpu, memory, blobStorage} = calculateTenantMetrics(tenant); const {nodesCount, groupsCount} = calculateTenantEntities(tenant); @@ -188,6 +190,7 @@ export const prepareTenants = (tenants: TTenant[], useNodeAsBackend: boolean): P backend, sharedTenantName, + sharedNodeIds, controlPlaneName, cpu, memory, diff --git a/src/types/additionalProps.ts b/src/types/additionalProps.ts index 3ef358b5e..64712d96f 100644 --- a/src/types/additionalProps.ts +++ b/src/types/additionalProps.ts @@ -19,7 +19,7 @@ export interface AdditionalClusterProps { } export interface AdditionalTenantsProps { - prepareTenantBackend?: (backend: string | undefined) => string | undefined; + prepareTenantBackend?: (backend: string | NodeAddress | undefined) => string | undefined; getMonitoringLink?: (name?: string, type?: ETenantType) => React.ReactNode; }