-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathutils.ts
More file actions
92 lines (80 loc) · 3.21 KB
/
utils.ts
File metadata and controls
92 lines (80 loc) · 3.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import React from 'react';
import {skipToken} from '@reduxjs/toolkit/query';
import {StringParam, useQueryParam} from 'use-query-params';
import {clustersApi} from '../../store/reducers/clusters/clusters';
import {nodesApi} from '../../store/reducers/nodes/nodes';
import {isClusterInfoV2} from '../../types/api/cluster';
import type {TClusterInfo} from '../../types/api/cluster';
import type {VersionToColorMap} from '../../types/versions';
import {getVersionColors, getVersionMap} from '../../utils/clusterVersionColors';
import {useTypedSelector} from '../../utils/hooks';
import {
parseNodeGroupsToVersionsValues,
parseNodesToVersionsValues,
parseVersionsToVersionToColorMap,
} from '../../utils/versions';
interface UseGetVersionValuesProps {
cluster?: TClusterInfo;
versionToColor?: VersionToColorMap;
clusterLoading?: boolean;
}
export const useGetVersionValues = ({
cluster,
versionToColor,
clusterLoading,
}: UseGetVersionValuesProps) => {
const {currentData} = nodesApi.useGetNodesQuery(
isClusterInfoV2(cluster) || clusterLoading
? skipToken
: {
tablets: false,
fieldsRequired: ['SystemState', 'SubDomainKey'],
group: 'Version',
},
);
const versionsValues = React.useMemo(() => {
if (isClusterInfoV2(cluster) && cluster.MapVersions) {
const groups = Object.entries(cluster.MapVersions).map(([version, count]) => ({
name: version,
count,
}));
return parseNodeGroupsToVersionsValues(groups, versionToColor, cluster.NodesTotal);
}
if (!currentData) {
return [];
}
if (Array.isArray(currentData.NodeGroups)) {
return parseNodeGroupsToVersionsValues(
currentData.NodeGroups,
versionToColor,
cluster?.NodesTotal,
);
}
return parseNodesToVersionsValues(currentData.Nodes, versionToColor);
}, [currentData, versionToColor, cluster]);
return versionsValues;
};
export function useVersionToColorMap(cluster?: TClusterInfo) {
const getVersionToColorMap = useGetClusterVersionToColorMap();
return React.useMemo(() => {
if (getVersionToColorMap) {
return getVersionToColorMap();
}
return parseVersionsToVersionToColorMap(cluster?.Versions);
}, [cluster?.Versions, getVersionToColorMap]);
}
/** For multi-cluster version - with using meta handlers */
function useGetClusterVersionToColorMap(): (() => VersionToColorMap) | undefined {
const [clusterName] = useQueryParam('clusterName', StringParam);
const singleClusterMode = useTypedSelector((state) => state.singleClusterMode);
const {data} = clustersApi.useGetClustersListQuery(undefined, {skip: singleClusterMode});
return React.useMemo(() => {
if (singleClusterMode) {
return undefined;
}
const clusters = data || [];
const info = clusters.find((cluster) => cluster.name === clusterName);
const versions = info?.versions || [];
return () => getVersionColors(getVersionMap(versions));
}, [singleClusterMode, data, clusterName]);
}