-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathclusterVersionColors.ts
78 lines (67 loc) · 2.89 KB
/
clusterVersionColors.ts
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
import uniqBy from 'lodash/uniqBy';
import type {MetaClusterVersion} from '../types/api/meta';
import type {VersionToColorMap} from '../types/versions';
import {COLORS, DEFAULT_COLOR, getMinorVersion, hashCode} from './versions';
const UNDEFINED_COLOR_INDEX = '__no_color__';
type VersionsMap = Map<number | string, Set<string>>;
export const getVersionMap = (
versions: MetaClusterVersion[],
initialMap: VersionsMap = new Map(),
) => {
versions.forEach(
({version, version_base_color_index: versionBaseColorIndex = UNDEFINED_COLOR_INDEX}) => {
const minorVersion = getMinorVersion(version);
if (!initialMap.has(versionBaseColorIndex)) {
initialMap.set(versionBaseColorIndex, new Set());
}
initialMap.get(versionBaseColorIndex)?.add(minorVersion);
},
);
return initialMap;
};
export const getVersionColors = (versionMap: VersionsMap) => {
const versionToColor: VersionToColorMap = new Map();
for (const [baseColorIndex, item] of versionMap) {
Array.from(item)
// descending by version name: newer versions come first,
// so the newer version gets the brighter color
.sort((a, b) => hashCode(b) - hashCode(a))
.forEach((minor, minorIndex) => {
if (baseColorIndex === UNDEFINED_COLOR_INDEX) {
versionToColor.set(minor, DEFAULT_COLOR);
} else {
// baseColorIndex is numeric as we check if it is UNDEFINED_COLOR_INDEX before
const currentColorIndex = Number(baseColorIndex) % COLORS.length;
const minorQuantity = item.size;
const majorColor = COLORS[currentColorIndex];
const opacityPercent = Math.max(100 - minorIndex * (100 / minorQuantity), 20);
const hexOpacity = Math.round((opacityPercent * 255) / 100).toString(16);
const versionColor = `${majorColor}${hexOpacity}`;
versionToColor.set(minor, versionColor);
}
});
}
return versionToColor;
};
export interface ExtendedMetaClusterVersion extends MetaClusterVersion {
minorVersion: string;
color: string | undefined;
}
export const prepareClusterVersions = (
clusterVersions: MetaClusterVersion[] = [],
versionToColor: VersionToColorMap,
) => {
const filteredVersions = clusterVersions.filter((item) => item.version);
const preparedVersions = uniqBy(filteredVersions, 'version').map((item) => {
return {
...item,
minorVersion: getMinorVersion(item.version),
};
});
const versionsColors = preparedVersions.reduce<ExtendedMetaClusterVersion[]>((acc, item) => {
const color = versionToColor.get(item.minorVersion);
acc.push({...item, color});
return acc;
}, []);
return versionsColors;
};