-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
Copy pathnodeConstraints.tsx
212 lines (183 loc) · 5.88 KB
/
nodeConstraints.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import _ from "lodash";
import * as React from "react";
import * as ReactDom from "react-dom";
import * as d3 from "d3";
import { connect } from "react-redux";
import * as protos from "../js/protos";
import { AdminUIState } from "../redux/state";
import { refreshConstraints } from "../redux/apiReducers";
import { KeyedCachedDataReducerState } from "../redux/cachedDataReducer";
interface Datum {
name?: string;
attrs?: string[];
children?: Datum[];
candidate?: cockroach.server.serverpb.Candidate;
}
interface ConstraintsData {
state: KeyedCachedDataReducerState<cockroach.server.serverpb.ConstraintsResponseMessage>;
}
/**
* RangesMainActions are the action dispatchers which should be passed to the
* RangesMain container.
*/
interface ConstraintsActions {
refreshConstraints: typeof refreshConstraints;
}
interface ConstraintsState {
constraints: string;
}
/**
* ConstraintsProps is the type of the props object that must be passed to
* Constraints component.
*/
type ConstraintsProps = ConstraintsData & ConstraintsActions;
/**
* Renders the main content of the help us page.
*/
class Constraints extends React.Component<ConstraintsProps, ConstraintsState> {
state: ConstraintsState = {
constraints: "",
};
chart: d3.Selection<Datum>;
debounceRefreshConstraints = _.debounce(this.refreshConstraints, 300);
refreshConstraints() {
this.props.refreshConstraints(new protos.cockroach.server.serverpb.ConstraintsRequest({
constraints: this.state.constraints,
}));
}
componentDidMount() {
this.refreshConstraints();
const node = ReactDom.findDOMNode(this);
this.chart = d3.select(node).select("svg");
setTimeout(this.updateChart.bind(this), 1);
}
componentDidUpdate() {
this.updateChart();
}
structuredStores(): Datum {
const state = this.props.state[this.state.constraints];
if (!state || !state.valid) {
return {};
}
const candidates: {[id: number]: cockroach.server.serverpb.Candidate} = {};
for (let candidate of state.data.candidates) {
candidates[candidate.desc.store_id] = candidate;
}
const nodes: {[id: number]: cockroach.roachpb.StoreDescriptor[]} = {};
for (let store of state.data.stores) {
let stores = nodes[store.node.node_id];
if (!stores) {
stores = [];
nodes[store.node.node_id] = stores;
}
stores.push(store);
}
const datums: {[name: string]: Datum} = {};
const root: Datum = {name: "Cluster", children: []};
_.each(nodes, (stores: cockroach.roachpb.StoreDescriptor[], id: string) => {
const nodeDesc = stores[0].node;
const node = {
name: `node=${id}`,
attrs: nodeDesc.attrs.attrs,
children: _.map(stores, (store: cockroach.roachpb.StoreDescriptor): Datum => {
return {
name: `store=${store.store_id}`,
attrs: store.attrs.attrs,
candidate: candidates[store.store_id],
};
}),
};
let container: Datum = root;
let prefix: string;
const tiers = stores[0].locality.tiers;
_.each(tiers, (tier: cockroach.roachpb.Tier) => {
const name = `${tier.key}=${tier.value}`;
const tierName = `${prefix}:${name}`;
let c = datums[tierName];
if (!c) {
c = {
name: name,
children: [],
};
container.children.push(c);
datums[tierName] = c;
}
prefix = tierName;
container = c;
});
container.children.push(node);
});
return root;
}
updateChartHeight(): number {
const state = this.props.state[this.state.constraints];
const height = (state && state.valid) ? state.data.stores.length * 50 : 0;
this.chart.attr("height", height);
return height;
}
updateChart() {
const root = this.structuredStores();
this.chart.select("g").remove();
const {width} = (this.chart.node() as any).getBoundingClientRect();
const height = this.updateChartHeight();
const cluster = d3.layout.cluster()
.size([height, width - 260]);
const g = this.chart.append("g")
.attr("transform", "translate(10,0)");
const nodes = cluster.nodes(root);
const diagonal = d3.svg.diagonal()
.projection((d) => {
return [d.y, d.x];
});
g.selectAll("path.link")
.data(cluster.links(nodes))
.enter().append("path")
.attr("class", "link")
.attr("d", diagonal)
.style("stroke", "#555")
.style("stroke-opacity", "0.2")
.style("stroke-width", "1px")
.style("fill", "transparent");
const gnode = g.selectAll("g.node")
.data(nodes)
.enter().append("g")
.attr("class", "node")
.attr("transform", (d) => "translate(" + d.y + "," + d.x + ")")
.attr("opacity", (d: Datum) => d.candidate ? 1 : 0.5);
gnode.append("circle")
.attr("r", 4.5)
.style("fill", (d: Datum) => d.candidate ? "green" : "black");
const text = gnode.append("text");
text.append("tspan")
.attr("x", 8)
.text((d: Datum) => `${d.name} ${d.candidate ? "score=" + d.candidate.score : ""}`);
text.append("tspan")
.attr("x", 8)
.attr("dy", "1em")
.text((d: Datum) => d.attrs ? `attrs=${d.attrs.join(",")}` : "");
}
handleChange(event: MouseEvent) {
this.setState({constraints: (event.target as HTMLInputElement).value});
this.debounceRefreshConstraints();
}
render() {
return <div className="section node">
<div>
<b>Filter by constraints</b>
<input type="text" placeholder="Ex. +mem,rack=3" value={this.state.constraints} onChange={this.handleChange.bind(this)}/>
</div>
<svg className="d3" width="100%"></svg>
</div>;
}
}
// Connect to the redux store.
export default connect(
(state: AdminUIState) => {
return {
state: state.cachedData.constraints,
};
},
{
refreshConstraints,
}
)(Constraints);