-
Notifications
You must be signed in to change notification settings - Fork 81
/
NodesGridDials.tsx
180 lines (153 loc) · 4.53 KB
/
NodesGridDials.tsx
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import PropTypes from "prop-types";
import * as React from "react";
import createReactClass from "create-react-class";
import { routerShape } from "react-router";
import { Tooltip } from "reactjs-components";
import { Trans } from "@lingui/macro";
import { Icon } from "@dcos/ui-kit";
import { SystemIcons } from "@dcos/ui-kit/dist/packages/icons/dist/system-icons-enum";
import { greyDark } from "@dcos/ui-kit/dist/packages/design-tokens/build/js/designTokens";
import Chart from "#SRC/js/components/charts/Chart";
import DialChart from "#SRC/js/components/charts/DialChart";
import * as ResourcesUtil from "#SRC/js/utils/ResourcesUtil";
const colors = {
error: 2,
unused: "unused"
};
const NodesGridDials = createReactClass({
displayName: "NodesGridDials",
propTypes: {
hosts: PropTypes.array.isRequired,
selectedResource: PropTypes.string.isRequired,
serviceColors: PropTypes.object.isRequired,
resourcesByFramework: PropTypes.object.isRequired
},
contextTypes: {
router: routerShape
},
handleDialClick(nodeID) {
// Using handler, since Link in arrays cannot get router context
this.context.router.push(`/nodes/${nodeID}`);
},
getServiceSlicesConfig(node) {
const config = [];
const props = this.props;
const resourcesByFramework = props.resourcesByFramework[node.get("id")];
if (!resourcesByFramework) {
return config;
}
Object.keys(resourcesByFramework).forEach(frameworkID => {
const used =
resourcesByFramework[frameworkID][props.selectedResource] || 0;
const total = node.getUsageStats(props.selectedResource).total || 0;
const percentage = total === 0 ? 0 : (used * 100) / total;
config.push({
colorIndex: props.serviceColors[frameworkID],
name: frameworkID,
percentage
});
});
return config;
},
getActiveSliceData(node) {
const config = this.getServiceSlicesConfig(node);
let percentage = config.reduce((memo, slice) => {
memo += slice.percentage;
return memo;
}, 0);
percentage = Math.round(percentage);
config.push({
colorIndex: colors.unused,
name: "Unused",
percentage: 100 - percentage
});
return {
data: config,
usedPercentage: percentage
};
},
getInactiveSliceData() {
return [
{
colorIndex: colors.error,
name: "Error",
percentage: 100
}
];
},
getDialConfig(node) {
const { selectedResource } = this.props;
const resourceLabel = ResourcesUtil.getResourceLabel(selectedResource);
if (node.isActive()) {
const sliceData = this.getActiveSliceData(node);
return {
data: sliceData.data,
description: [
<span className="unit" key={"unit"}>
{sliceData.usedPercentage}%
</span>,
<span className="unit-label text-muted" key={"unit-label"}>
{resourceLabel}
</span>
]
};
}
return {
data: this.getInactiveSliceData(),
description: (
<span className="error">
<Icon shape={SystemIcons.Yield} color={greyDark} />
</span>
)
};
},
getDials() {
return this.props.hosts.map(node => {
const config = this.getDialConfig(node);
let description = <div className="description">{config.description}</div>;
if (!node.isActive()) {
description = (
<Tooltip
content={<Trans render="span">Connection to node lost</Trans>}
wrapperClassName="tooltip-wrapper text-align-center description"
>
{config.description}
</Tooltip>
);
}
return (
<a
className="nodes-grid-dials-item clickable"
onClick={this.handleDialClick.bind(this, node.get("id"))}
key={node.get("id")}
>
<div className="chart">
<Chart calcHeight={w => w}>
<DialChart data={config.data} value="percentage">
{description}
</DialChart>
</Chart>
</div>
</a>
);
});
},
// Zero-height spacer items force dial charts in the last line of the flex layout
// not to spread themselves across the line.
getSpacers() {
return Array(30)
.fill()
.map((v, index) => (
<div className="nodes-grid-dials-spacer" key={index} />
));
},
render() {
return (
<div className="nodes-grid-dials">
{this.getDials()}
{this.getSpacers()}
</div>
);
}
});
export default NodesGridDials;