-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
databasesPage.tsx
781 lines (707 loc) · 22.3 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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
// 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 "antd/lib/tooltip/style";
import classNames from "classnames/bind";
import classnames from "classnames/bind";
import { Anchor } from "src/anchor";
import { StackIcon } from "src/icon/stackIcon";
import { Pagination } from "src/pagination";
import { BooleanSetting } from "src/settings/booleanSetting";
import { PageConfig, PageConfigItem } from "src/pageConfig";
import {
ColumnDescriptor,
handleSortSettingFromQueryString,
ISortedTablePagination,
SortedTable,
SortSetting,
} from "src/sortedtable";
import * as format from "src/util/format";
import { EncodeDatabaseUri } from "src/util/format";
import styles from "./databasesPage.module.scss";
import sortableTableStyles from "src/sortedtable/sortedtable.module.scss";
import { baseHeadingClasses } from "src/transactionsPage/transactionsPageClasses";
import { syncHistory, tableStatsClusterSetting, unique } from "src/util";
import booleanSettingStyles from "../settings/booleanSetting.module.scss";
import { CircleFilled } from "../icon";
import LoadingError from "../sqlActivity/errorComponent";
import { Loading } from "../loading";
import { Search } from "../search";
import {
calculateActiveFilters,
defaultFilters,
Filter,
Filters,
handleFiltersFromQueryString,
} from "../queryFilter";
import { merge } from "lodash";
import { UIConfigState } from "src/store";
import { TableStatistics } from "../tableStatistics";
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;
// lastError: Error;
// sortSetting: SortSetting;
// search: string;
// filters: Filters;
// nodeRegions: { [nodeId: string]: string };
// isTenant: boolean;
// databases: { // DatabasesPageDataDatabase[]
// loading: boolean;
// loaded: boolean;
// name: string;
// sizeInBytes: number;
// tableCount: number;
// rangeCount: number;
// nodes: number[];
// nodesByRegionString: string;
// missingTables: { // DatabasesPageDataMissingTable[]
// loading: boolean;
// name: string;
// }[];
// }[];
// }
export interface DatabasesPageData {
loading: boolean;
loaded: boolean;
lastError: Error;
databases: DatabasesPageDataDatabase[];
sortSetting: SortSetting;
search: string;
filters: Filters;
nodeRegions: { [nodeId: string]: string };
isTenant?: UIConfigState["isTenant"];
automaticStatsCollectionEnabled?: boolean;
showNodeRegionsColumn?: boolean;
}
export interface DatabasesPageDataDatabase {
loading: boolean;
loaded: boolean;
lastError: Error;
name: string;
sizeInBytes: number;
tableCount: number;
rangeCount: number;
missingTables: DatabasesPageDataMissingTable[];
// Array of node IDs used to unambiguously filter by node and region.
nodes?: number[];
// String of nodes grouped by region in alphabetical order, e.g.
// regionA(n1,n2), regionB(n3). Used for display in the table's
// "Regions/Nodes" column.
nodesByRegionString?: string;
numIndexRecommendations: number;
}
// 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;
onFilterChange?: (value: Filters) => void;
onSearchComplete?: (query: string) => void;
onSortingChange?: (
name: string,
columnTitle: string,
ascending: boolean,
) => void;
}
export type DatabasesPageProps = DatabasesPageData &
DatabasesPageActions &
RouteComponentProps<unknown>;
interface DatabasesPageState {
pagination: ISortedTablePagination;
filters?: Filters;
activeFilters?: number;
lastDetailsError: Error;
}
class DatabasesSortedTable extends SortedTable<DatabasesPageDataDatabase> {}
// filterBySearchQuery returns true if the search query matches the database name.
function filterBySearchQuery(
database: DatabasesPageDataDatabase,
search: string,
): boolean {
const matchString = database.name.toLowerCase();
if (search.startsWith('"') && search.endsWith('"')) {
search = search.substring(1, search.length - 1);
return matchString.includes(search);
}
return search
.toLowerCase()
.split(" ")
.every(val => matchString.includes(val));
}
const tablePageSize = 20;
const disableTableSortSize = tablePageSize * 2;
export class DatabasesPage extends React.Component<
DatabasesPageProps,
DatabasesPageState
> {
constructor(props: DatabasesPageProps) {
super(props);
this.state = {
filters: defaultFilters,
pagination: {
current: 1,
pageSize: tablePageSize,
},
lastDetailsError: null,
};
const stateFromHistory = this.getStateFromHistory();
this.state = merge(this.state, stateFromHistory);
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);
}
}
getStateFromHistory = (): Partial<DatabasesPageState> => {
const {
history,
search,
sortSetting,
filters,
onFilterChange,
onSearchComplete,
onSortingChange,
} = this.props;
const searchParams = new URLSearchParams(history.location.search);
const searchQuery = searchParams.get("q") || undefined;
if (onSearchComplete && searchQuery && search != searchQuery) {
onSearchComplete(searchQuery);
}
handleSortSettingFromQueryString(
"Databases",
history.location.search,
sortSetting,
onSortingChange,
);
const latestFilter = handleFiltersFromQueryString(
history,
filters,
onFilterChange,
);
return {
filters: latestFilter,
activeFilters: calculateActiveFilters(latestFilter),
};
};
componentDidMount(): void {
this.refresh();
}
updateQueryParams(): void {
const { history, search } = this.props;
// Search
const searchParams = new URLSearchParams(history.location.search);
const searchQueryString = searchParams.get("q") || "";
if (search && search != searchQueryString) {
syncHistory(
{
q: search,
},
history,
);
}
}
componentDidUpdate(): void {
this.updateQueryParams();
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 &&
this.props.lastError === undefined
) {
return this.props.refreshDatabases();
}
let lastDetailsError: Error;
// load everything by default
let filteredDbs = this.props.databases;
console.log("refresh: " + JSON.stringify(this.props.sortSetting));
// Loading only the first page if there are more than
// 40 dbs. If there is more than 40 dbs sort will be disabled.
if (this.props.databases.length > disableTableSortSize) {
let startIndex = 0;
startIndex =
this.state.pagination.pageSize * (this.state.pagination.current - 1);
// Result maybe filtered so get db names from filtered results
if (this.props.search && this.props.search.length > 0) {
filteredDbs = this.filteredDatabasesData();
}
if (!filteredDbs || filteredDbs.length === 0) {
return;
}
// sort the filtered results to make sure to get the correct list of dbs
const asc = this.props.sortSetting?.ascending ?? true;
filteredDbs = filteredDbs.sort(function (a, b) {
if (asc) {
return a.name > b.name ? 1 : -1; // sort in ascending order
}
return a.name > b.name ? -1 : 1; // sort in descending order
});
// Only load the first page
filteredDbs = filteredDbs.slice(
startIndex,
startIndex + this.state.pagination.pageSize,
);
}
filteredDbs.forEach((database, i, arr) => {
if (database.lastError !== undefined) {
lastDetailsError = database.lastError;
}
if (
lastDetailsError &&
this.state.lastDetailsError?.name != lastDetailsError?.name
) {
this.setState({ lastDetailsError: lastDetailsError });
}
if (
!database.loaded &&
!database.loading &&
(database.lastError === undefined ||
database.lastError?.name === "GetDatabaseInfoError")
) {
this.props.refreshDatabaseDetails(database.name);
return;
}
database.missingTables.forEach(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);
}
};
resetPagination = (): void => {
this.setState(prevState => {
return {
pagination: {
current: 1,
pageSize: prevState.pagination.pageSize,
},
};
});
};
onClearSearchField = (): void => {
if (this.props.onSearchComplete) {
this.props.onSearchComplete("");
}
syncHistory(
{
q: undefined,
},
this.props.history,
);
};
onClearFilters = (): void => {
if (this.props.onFilterChange) {
this.props.onFilterChange(defaultFilters);
}
this.setState({
filters: defaultFilters,
activeFilters: 0,
});
this.resetPagination();
syncHistory(
{
regions: undefined,
nodes: undefined,
},
this.props.history,
);
};
onSubmitSearchField = (search: string): void => {
if (this.props.onSearchComplete) {
this.props.onSearchComplete(search);
}
this.resetPagination();
syncHistory(
{
q: search,
},
this.props.history,
);
};
onSubmitFilters = (filters: Filters): void => {
if (this.props.onFilterChange) {
this.props.onFilterChange(filters);
}
this.setState({
filters: filters,
activeFilters: calculateActiveFilters(filters),
});
this.resetPagination();
syncHistory(
{
regions: filters.regions,
nodes: filters.nodes,
},
this.props.history,
);
};
// Returns a list of databses to the display based on input from the search
// box and the applied filters.
filteredDatabasesData = (): DatabasesPageDataDatabase[] => {
const { search, databases, filters, nodeRegions } = this.props;
// The regions and nodes selected from the filter dropdown.
const regionsSelected =
filters.regions.length > 0 ? filters.regions.split(",") : [];
const nodesSelected =
filters.nodes.length > 0 ? filters.nodes.split(",") : [];
return databases
.filter(db => (search ? filterBySearchQuery(db, search) : true))
.filter(db => {
if (regionsSelected.length == 0 && nodesSelected.length == 0)
return true;
let foundRegion = regionsSelected.length == 0;
let foundNode = nodesSelected.length == 0;
db.nodes?.forEach(node => {
if (
foundRegion ||
regionsSelected.includes(nodeRegions[node.toString()])
) {
foundRegion = true;
}
if (foundNode || nodesSelected.includes("n" + node.toString())) {
foundNode = true;
}
if (foundNode && foundRegion) return true;
});
return foundRegion && foundNode;
});
};
private renderIndexRecommendations = (
database: DatabasesPageDataDatabase,
): React.ReactNode => {
const text =
database.numIndexRecommendations > 0
? `${database.numIndexRecommendations} index ${
database.numIndexRecommendations > 1
? "recommendations"
: "recommendation"
}`
: "None";
const classname =
database.numIndexRecommendations > 0
? "index-recommendations-icon__exist"
: "index-recommendations-icon__none";
return (
<div>
<CircleFilled className={cx(classname)} />
<span>{text}</span>
</div>
);
};
checkInfoAvailable = (
database: DatabasesPageDataDatabase,
cell: React.ReactNode,
): React.ReactNode => {
if (
database.lastError &&
database.lastError.name !== "GetDatabaseInfoError"
) {
return "(unavailable)";
}
return cell;
};
private columns: ColumnDescriptor<DatabasesPageDataDatabase>[] = [
{
title: (
<Tooltip placement="bottom" title="The name of the database.">
Databases
</Tooltip>
),
cell: database => (
<Link
to={EncodeDatabaseUri(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 =>
this.checkInfoAvailable(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 => this.checkInfoAvailable(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 => this.checkInfoAvailable(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."
>
{this.props.isTenant ? "Regions" : "Regions/Nodes"}
</Tooltip>
),
cell: database =>
this.checkInfoAvailable(
database,
database.nodesByRegionString || "None",
),
sort: database => database.nodesByRegionString,
className: cx("databases-table__col-node-regions"),
name: "nodeRegions",
hideIfTenant: true,
},
{
title: (
<Tooltip
placement="bottom"
title="Index recommendations will appear if the system detects improper index usage, such as the occurrence of unused indexes. Following index recommendations may help improve query performance."
>
Index Recommendations
</Tooltip>
),
cell: this.renderIndexRecommendations,
sort: database => database.numIndexRecommendations,
className: cx("databases-table__col-node-regions"),
name: "numIndexRecommendations",
},
];
render(): React.ReactElement {
this.columns.find(c => c.name === "nodeRegions").showByDefault =
this.props.showNodeRegionsColumn;
const displayColumns = this.columns.filter(
col => col.showByDefault !== false,
);
const { filters, search, nodeRegions, isTenant } = this.props;
const { pagination } = this.state;
const databasesToDisplay = this.filteredDatabasesData();
const activeFilters = calculateActiveFilters(filters);
const nodes = Object.keys(nodeRegions)
.map(n => Number(n))
.sort();
const regions = unique(Object.values(nodeRegions));
const showNodes = !isTenant && nodes.length > 1;
const showRegions = regions.length > 1;
// Only show the databases filter if at least one drop-down is shown.
const databasesFilter =
showNodes || showRegions ? (
<PageConfigItem>
<Filter
hideAppNames={true}
regions={regions}
hideTimeLabel={true}
nodes={nodes.map(n => "n" + n.toString())}
activeFilters={activeFilters}
filters={defaultFilters}
onSubmitFilters={this.onSubmitFilters}
showNodes={showNodes}
showRegions={showRegions}
/>
</PageConfigItem>
) : (
<></>
);
return (
<div>
<div className={baseHeadingClasses.wrapper}>
<h3 className={baseHeadingClasses.tableName}>Databases</h3>
{this.props.automaticStatsCollectionEnabled != null && (
<BooleanSetting
text={"Auto stats collection"}
enabled={this.props.automaticStatsCollectionEnabled}
tooltipText={
<span>
{" "}
Automatic statistics can help improve query performance. Learn
how to{" "}
<Anchor
href={tableStatsClusterSetting}
target="_blank"
className={booleanSettingCx("crl-hover-text__link-text")}
>
manage statistics collection
</Anchor>
.
</span>
}
/>
)}
</div>
<section className={sortableTableCx("cl-table-container")}>
<PageConfig>
<PageConfigItem>
<Search
onSubmit={this.onSubmitSearchField}
onClear={this.onClearSearchField}
defaultValue={search}
placeholder={"Search Databases"}
/>
</PageConfigItem>
{databasesFilter}
</PageConfig>
<TableStatistics
pagination={pagination}
totalCount={databasesToDisplay.length}
arrayItemName="databases"
activeFilters={activeFilters}
onClearFilters={this.onClearFilters}
/>
<Loading
loading={this.props.loading}
page={"databases"}
error={this.props.lastError}
render={() => (
<DatabasesSortedTable
className={cx("databases-table")}
data={databasesToDisplay}
columns={displayColumns}
sortSetting={this.props.sortSetting}
onChangeSortSetting={this.changeSortSetting}
pagination={this.state.pagination}
loading={this.props.loading}
disableSortForLargeData={disableTableSortSize}
renderNoResult={
<div
className={cx(
"databases-table__no-result",
"icon__container",
)}
>
<StackIcon className={cx("icon--s")} />
This cluster has no databases.
</div>
}
/>
)}
renderError={() =>
LoadingError({
statsType: "databases",
timeout: this.props.lastError?.name
?.toLowerCase()
.includes("timeout"),
})
}
/>
{!this.props.loading && (
<Loading
loading={this.props.loading}
page={"databases"}
error={this.state.lastDetailsError}
render={() => <></>}
renderError={() =>
LoadingError({
statsType: "part of the information",
timeout: this.state.lastDetailsError?.name
?.toLowerCase()
.includes("timeout"),
error: this.state.lastDetailsError,
})
}
/>
)}
</section>
<Pagination
pageSize={this.state.pagination.pageSize}
current={this.state.pagination.current}
total={this.props.databases.length}
onChange={this.changePage}
/>
</div>
);
}
}