Skip to content

Commit

Permalink
feat(Databases): use balancer + /node/:id as backend endpoint (#1418)
Browse files Browse the repository at this point in the history
  • Loading branch information
ValeraS authored Oct 7, 2024
1 parent 90a3403 commit f8a0db1
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 9 deletions.
15 changes: 11 additions & 4 deletions src/containers/AppWithClusters/ExtendedCluster/ExtendedCluster.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ import type {
AdditionalClusterProps,
AdditionalTenantsProps,
AdditionalVersionsProps,
NodeAddress,
} from '../../../types/additionalProps';
import type {MetaClusterVersion} from '../../../types/api/meta';
import type {ETenantType} from '../../../types/api/tenant';
import {getVersionColors, getVersionMap} from '../../../utils/clusterVersionColors';
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';

Expand Down Expand Up @@ -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;
Expand All @@ -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) {
Expand Down
16 changes: 13 additions & 3 deletions src/containers/Tenants/Tenants.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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<PreparedTenant>[] = [
Expand Down
1 change: 1 addition & 0 deletions src/store/reducers/tenants/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 4 additions & 1 deletion src/store/reducers/tenants/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -188,6 +190,7 @@ export const prepareTenants = (tenants: TTenant[], useNodeAsBackend: boolean): P

backend,
sharedTenantName,
sharedNodeIds,
controlPlaneName,
cpu,
memory,
Expand Down
2 changes: 1 addition & 1 deletion src/types/additionalProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down

0 comments on commit f8a0db1

Please sign in to comment.