-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
Copy pathdatabasesPage.tsx
359 lines (331 loc) · 10.5 KB
/
databasesPage.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
// Copyright 2021 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
import React from "react";
import { Link, RouteComponentProps } from "react-router-dom";
import { Tooltip } from "antd";
import classNames from "classnames/bind";
import _ from "lodash";
import { StackIcon } from "src/icon/stackIcon";
import { Pagination, ResultsPerPageLabel } from "src/pagination";
import { BooleanSetting } from "src/settings/booleanSetting";
import {
ColumnDescriptor,
ISortedTablePagination,
SortSetting,
SortedTable,
} from "src/sortedtable";
import * as format from "src/util/format";
import styles from "./databasesPage.module.scss";
import sortableTableStyles from "src/sortedtable/sortedtable.module.scss";
import {
baseHeadingClasses,
statisticsClasses,
} from "src/transactionsPage/transactionsPageClasses";
import { syncHistory } from "../util";
import classnames from "classnames/bind";
import booleanSettingStyles from "../settings/booleanSetting.module.scss";
const cx = classNames.bind(styles);
const sortableTableCx = classNames.bind(sortableTableStyles);
const booleanSettingCx = classnames.bind(booleanSettingStyles);
// We break out separate interfaces for some of the nested objects in our data
// both so that they can be available as SortedTable rows and for making
// (typed) test assertions on narrower slices of the data.
//
// The loading and loaded flags help us know when to dispatch the appropriate
// refresh actions.
//
// The overall structure is:
//
// interface DatabasesPageData {
// loading: boolean;
// loaded: boolean;
// sortSetting: SortSetting;
// databases: { // DatabasesPageDataDatabase[]
// loading: boolean;
// loaded: boolean;
// name: string;
// sizeInBytes: number;
// tableCount: number;
// rangeCount: number;
// nodesByRegionString: string;
// missingTables: { // DatabasesPageDataMissingTable[]
// loading: boolean;
// name: string;
// }[];
// }[];
// }
export interface DatabasesPageData {
loading: boolean;
loaded: boolean;
databases: DatabasesPageDataDatabase[];
sortSetting: SortSetting;
automaticStatsCollectionEnabled: boolean;
showNodeRegionsColumn?: boolean;
}
export interface DatabasesPageDataDatabase {
loading: boolean;
loaded: boolean;
name: string;
sizeInBytes: number;
tableCount: number;
rangeCount: number;
missingTables: DatabasesPageDataMissingTable[];
// String of nodes grouped by region in alphabetical order, e.g.
// regionA(n1,n2), regionB(n3)
nodesByRegionString?: string;
}
// A "missing" table is one for which we were unable to gather size and range
// count information during the backend call to fetch DatabaseDetails. We
// expose it here so that the component has the opportunity to try to refresh
// those properties for the table directly.
export interface DatabasesPageDataMissingTable {
// Note that we don't need a "loaded" property here because we expect
// the reducers supplying our properties to remove a missing table from
// the list once we've loaded its data.
loading: boolean;
name: string;
}
export interface DatabasesPageActions {
refreshDatabases: () => void;
refreshDatabaseDetails: (database: string) => void;
refreshTableStats: (database: string, table: string) => void;
refreshSettings: () => void;
refreshNodes?: () => void;
onSortingChange?: (
name: string,
columnTitle: string,
ascending: boolean,
) => void;
}
export type DatabasesPageProps = DatabasesPageData &
DatabasesPageActions &
RouteComponentProps<unknown>;
interface DatabasesPageState {
pagination: ISortedTablePagination;
}
class DatabasesSortedTable extends SortedTable<DatabasesPageDataDatabase> {}
export class DatabasesPage extends React.Component<
DatabasesPageProps,
DatabasesPageState
> {
constructor(props: DatabasesPageProps) {
super(props);
this.state = {
pagination: {
current: 1,
pageSize: 20,
},
};
const { history } = this.props;
const searchParams = new URLSearchParams(history.location.search);
const ascending = (searchParams.get("ascending") || undefined) === "true";
const columnTitle = searchParams.get("columnTitle") || undefined;
const sortSetting = this.props.sortSetting;
if (
this.props.onSortingChange &&
columnTitle &&
(sortSetting.columnTitle != columnTitle ||
sortSetting.ascending != ascending)
) {
this.props.onSortingChange("Databases", columnTitle, ascending);
}
}
componentDidMount(): void {
this.refresh();
}
componentDidUpdate(): void {
this.refresh();
}
private refresh(): void {
if (this.props.refreshNodes != null) {
this.props.refreshNodes();
}
if (this.props.refreshSettings != null) {
this.props.refreshSettings();
}
if (!this.props.loaded && !this.props.loading) {
return this.props.refreshDatabases();
}
_.forEach(this.props.databases, database => {
if (!database.loaded && !database.loading) {
return this.props.refreshDatabaseDetails(database.name);
}
_.forEach(database.missingTables, table => {
if (!table.loading) {
return this.props.refreshTableStats(database.name, table.name);
}
});
});
}
changePage = (current: number): void => {
this.setState({ pagination: { ...this.state.pagination, current } });
};
changeSortSetting = (ss: SortSetting): void => {
syncHistory(
{
ascending: ss.ascending.toString(),
columnTitle: ss.columnTitle,
},
this.props.history,
);
if (this.props.onSortingChange) {
this.props.onSortingChange("Databases", ss.columnTitle, ss.ascending);
}
};
private columns: ColumnDescriptor<DatabasesPageDataDatabase>[] = [
{
title: (
<Tooltip placement="bottom" title="The name of the database.">
Databases
</Tooltip>
),
cell: database => (
<Link
to={`/database/${database.name}`}
className={cx("icon__container")}
>
<StackIcon className={cx("icon--s", "icon--primary")} />
{database.name}
</Link>
),
sort: database => database.name,
className: cx("databases-table__col-name"),
name: "name",
},
{
title: (
<Tooltip
placement="bottom"
title="The approximate total disk size across all table replicas in the database."
>
Size
</Tooltip>
),
cell: database => format.Bytes(database.sizeInBytes),
sort: database => database.sizeInBytes,
className: cx("databases-table__col-size"),
name: "size",
},
{
title: (
<Tooltip
placement="bottom"
title="The total number of tables in the database."
>
Tables
</Tooltip>
),
cell: database => database.tableCount,
sort: database => database.tableCount,
className: cx("databases-table__col-table-count"),
name: "tableCount",
},
{
title: (
<Tooltip
placement="bottom"
title="The total number of ranges across all tables in the database."
>
Range count
</Tooltip>
),
cell: database => database.rangeCount,
sort: database => database.rangeCount,
className: cx("databases-table__col-range-count"),
name: "rangeCount",
},
{
title: (
<Tooltip
placement="bottom"
title="Regions/nodes on which the database tables are located."
>
Regions/nodes
</Tooltip>
),
cell: database => database.nodesByRegionString || "None",
sort: database => database.nodesByRegionString,
className: cx("databases-table__col-node-regions"),
name: "nodeRegions",
hideIfTenant: true,
},
];
render(): React.ReactElement {
this.columns.find(
c => c.name === "nodeRegions",
).showByDefault = this.props.showNodeRegionsColumn;
const displayColumns = this.columns.filter(
col => col.showByDefault !== false,
);
const tipText = (
<span>
{" "}
Automatic statistics can help improve query performance. Learn how to{" "}
<a
className={booleanSettingCx("crl-hover-text__link-text")}
href="https://www.cockroachlabs.com/docs/stable/cost-based-optimizer#control-automatic-statistics"
>
manage statistics collection
</a>
.
</span>
);
return (
<div>
<div className={baseHeadingClasses.wrapper}>
<h3 className={baseHeadingClasses.tableName}>Databases</h3>
<BooleanSetting
text={"Auto stats collection"}
enabled={this.props.automaticStatsCollectionEnabled}
tooltipText={tipText}
/>
</div>
<section className={sortableTableCx("cl-table-container")}>
<div className={statisticsClasses.statistic}>
<h4 className={statisticsClasses.countTitle}>
<ResultsPerPageLabel
pagination={{
...this.state.pagination,
total: this.props.databases.length,
}}
pageName={
this.props.databases.length == 1 ? "database" : "databases"
}
/>
</h4>
</div>
<DatabasesSortedTable
className={cx("databases-table")}
data={this.props.databases}
columns={displayColumns}
sortSetting={this.props.sortSetting}
onChangeSortSetting={this.changeSortSetting}
pagination={this.state.pagination}
loading={this.props.loading}
renderNoResult={
<div
className={cx("databases-table__no-result", "icon__container")}
>
<StackIcon className={cx("icon--s")} />
This cluster has no databases.
</div>
}
/>
</section>
<Pagination
pageSize={this.state.pagination.pageSize}
current={this.state.pagination.current}
total={this.props.databases.length}
onChange={this.changePage}
/>
</div>
);
}
}