-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
Copy pathreplicaMatrix.tsx
194 lines (171 loc) · 5.12 KB
/
replicaMatrix.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
import _ from "lodash";
import React, { Component } from "react";
import classNames from "classnames";
import {
TreeNode,
TreePath,
layoutTree,
flatten,
sumValuesUnderPaths,
deepIncludes,
LayoutNode,
FlattenedNode,
} from "./tree";
import { cockroach } from "src/js/protos";
import NodeDescriptor$Properties = cockroach.roachpb.NodeDescriptor$Properties;
import "./replicaMatrix.styl";
const DOWN_ARROW = "▼";
const SIDE_ARROW = "▶";
interface ReplicaMatrixState {
collapsedRows: TreePath[];
collapsedCols: TreePath[];
}
interface ReplicaMatrixProps {
cols: TreeNode<NodeDescriptor$Properties>;
rows: TreeNode<SchemaObject>;
getValue: (rowPath: TreePath, colPath: TreePath) => number;
}
const ROW_TREE_INDENT_PX = 18;
class ReplicaMatrix extends Component<ReplicaMatrixProps, ReplicaMatrixState> {
constructor(props: ReplicaMatrixProps) {
super(props);
this.state = {
collapsedRows: [["system"]],
collapsedCols: [],
};
}
expandRow = (path: TreePath) => {
this.setState({
collapsedRows: this.state.collapsedRows.filter((tp) => !_.isEqual(tp, path)),
});
}
collapseRow = (path: TreePath) => {
this.setState({
collapsedRows: [...this.state.collapsedRows, path],
});
}
expandCol = (path: TreePath) => {
this.setState({
collapsedCols: this.state.collapsedCols.filter((tp) => !_.isEqual(tp, path)),
});
}
collapseCol = (path: TreePath) => {
this.setState({
collapsedCols: [...this.state.collapsedCols, path],
});
}
colLabel(col: LayoutNode<NodeDescriptor$Properties>, colIsCollapsed: boolean): string {
if (col.isPlaceholder) {
return null;
}
if (col.depth === 1) {
return `n${col.data.node_id}`;
}
const arrow = colIsCollapsed ? SIDE_ARROW : DOWN_ARROW;
const localityLabel = col.path.length === 0 ? "Cluster" : col.path[col.path.length - 1];
return `${arrow} ${localityLabel}`;
}
rowLabel(row: FlattenedNode<SchemaObject>): string {
if (row.isLeaf) {
return row.data.tableName;
}
const arrow = row.isCollapsed ? SIDE_ARROW : DOWN_ARROW;
const label = row.data ? `DB: ${row.data.dbName}` : "Cluster";
return `${arrow} ${label}`;
}
render() {
const {
cols,
rows,
getValue,
} = this.props;
const {
collapsedRows,
collapsedCols,
} = this.state;
const flattenedRows = flatten(rows, collapsedRows, true /* includeNodes */);
const headerRows = layoutTree(cols, collapsedCols);
const flattenedCols = flatten(cols, collapsedCols, false /* includeNodes */);
return (
<table className="matrix">
<thead>
{headerRows.map((row, idx) => (
<tr key={idx}>
{idx === 0
? <th className="matrix__metric-label"># Replicas</th>
: <th />}
{row.map((col) => {
const colIsCollapsed = deepIncludes(collapsedCols, col.path);
return (
<th
key={col.path.join("/")}
colSpan={col.width}
className={classNames(
"matrix__column-header",
{ "matrix__column-header--node": col.depth > 1 },
)}
onClick={() => (
colIsCollapsed
? this.expandCol(col.path)
: this.collapseCol(col.path)
)}
>
{this.colLabel(col, colIsCollapsed)}
</th>
);
})}
</tr>
))}
</thead>
<tbody>
{flattenedRows.map((row) => {
return (
<tr
key={JSON.stringify(row)}
className={classNames("matrix__row", { "matrix__row--node": !row.isLeaf })}
onClick={() => (
row.isCollapsed
? this.expandRow(row.path)
: this.collapseRow(row.path)
)}
>
<th
className={classNames(
"matrix__row-label",
{ "matrix__row-label--node": !row.isLeaf },
)}
style={{ paddingLeft: row.depth * ROW_TREE_INDENT_PX + 5 }}
>
{this.rowLabel(row)}
</th>
{flattenedCols.map((col) => {
return (
<td
key={col.path.join("/")}
className="matrix__cell-value"
>
{row.isLeaf || row.isCollapsed
? emptyIfZero(sumValuesUnderPaths(rows, cols, row.path, col.path, getValue))
: null}
</td>
);
})}
</tr>
);
})}
</tbody>
</table>
);
}
}
function emptyIfZero(n: number): string {
if (n === 0) {
return "";
}
return `${n}`;
}
export default ReplicaMatrix;
export interface SchemaObject {
dbName?: string;
tableName?: string;
}