-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
Copy pathredux.ts
194 lines (178 loc) · 5.94 KB
/
redux.ts
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
// 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 { RouteComponentProps } from "react-router";
import { createSelector } from "reselect";
import { LocalSetting } from "src/redux/localsettings";
import _ from "lodash";
import {
DatabaseDetailsPageData,
util,
ViewMode,
} from "@cockroachlabs/cluster-ui";
import { cockroach } from "src/js/protos";
import {
generateTableID,
refreshDatabaseDetails,
refreshTableDetails,
refreshTableStats,
} from "src/redux/apiReducers";
import { AdminUIState } from "src/redux/state";
import { databaseNameAttr } from "src/util/constants";
import { FixLong } from "src/util/fixLong";
import { getMatchParamByName } from "src/util/query";
import {
nodeRegionsByIDSelector,
selectIsMoreThanOneNode,
} from "src/redux/nodes";
import { getNodesByRegionString } from "../utils";
import moment from "moment";
const {
DatabaseDetailsRequest,
TableDetailsRequest,
TableStatsRequest,
} = cockroach.server.serverpb;
function normalizeRoles(raw: string[]): string[] {
const rolePrecedence: Record<string, number> = {
root: 1,
admin: 2,
public: 3,
};
// Once we have an alphabetized list of roles, we sort it again, promoting
// root, admin, and public to the head of the list. (We rely on _.sortBy to
// be stable.)
const alphabetizedRoles = _.sortBy(_.uniq(_.filter(raw)));
return _.sortBy(alphabetizedRoles, role => rolePrecedence[role] || 100);
}
function normalizePrivileges(raw: string[]): string[] {
const privilegePrecedence: Record<string, number> = {
ALL: 1,
CREATE: 2,
DROP: 3,
GRANT: 4,
SELECT: 5,
INSERT: 6,
UPDATE: 7,
DELETE: 8,
};
return _.sortBy(
_.uniq(_.map(_.filter(raw), _.toUpper)),
privilege => privilegePrecedence[privilege] || 100,
);
}
const sortSettingTablesLocalSetting = new LocalSetting(
"sortSetting/DatabasesDetailsTablesPage",
(state: AdminUIState) => state.localSettings,
{ ascending: true, columnTitle: "name" },
);
const sortSettingGrantsLocalSetting = new LocalSetting(
"sortSetting/DatabasesDetailsGrantsPage",
(state: AdminUIState) => state.localSettings,
{ ascending: true, columnTitle: "name" },
);
const viewModeLocalSetting = new LocalSetting(
"viewMode/DatabasesDetailsPage",
(state: AdminUIState) => state.localSettings,
ViewMode.Tables,
);
export const mapStateToProps = createSelector(
(_state: AdminUIState, props: RouteComponentProps): string =>
getMatchParamByName(props.match, databaseNameAttr),
state => state.cachedData.databaseDetails,
state => state.cachedData.tableDetails,
state => state.cachedData.tableStats,
state => nodeRegionsByIDSelector(state),
state => selectIsMoreThanOneNode(state),
state => viewModeLocalSetting.selector(state),
state => sortSettingTablesLocalSetting.selector(state),
state => sortSettingGrantsLocalSetting.selector(state),
(
database,
databaseDetails,
tableDetails,
tableStats,
nodeRegions,
showNodeRegionsColumn,
viewMode,
sortSettingTables,
sortSettingGrants,
): DatabaseDetailsPageData => {
return {
loading: !!databaseDetails[database]?.inFlight,
loaded: !!databaseDetails[database]?.valid,
name: database,
showNodeRegionsColumn,
viewMode,
sortSettingTables,
sortSettingGrants,
tables: _.map(databaseDetails[database]?.data?.table_names, table => {
const tableId = generateTableID(database, table);
const details = tableDetails[tableId];
const stats = tableStats[tableId];
const roles = normalizeRoles(_.map(details?.data?.grants, "user"));
const grants = normalizePrivileges(
_.flatMap(details?.data?.grants, "privileges"),
);
const nodes = stats?.data?.node_ids || [];
const numIndexes = _.uniq(
_.map(details?.data?.indexes, index => index.name),
).length;
return {
name: table,
details: {
loading: !!details?.inFlight,
loaded: !!details?.valid,
columnCount: details?.data?.columns?.length || 0,
indexCount: numIndexes,
userCount: roles.length,
roles: roles,
grants: grants,
statsLastUpdated: util.TimestampToMoment(
details?.data?.stats_last_created_at,
moment.utc("0001-01-01"),
),
},
stats: {
loading: !!stats?.inFlight,
loaded: !!stats?.valid,
replicationSizeInBytes: FixLong(
stats?.data?.approximate_disk_bytes || 0,
).toNumber(),
rangeCount: FixLong(stats?.data?.range_count || 0).toNumber(),
nodesByRegionString: getNodesByRegionString(nodes, nodeRegions),
},
};
}),
};
},
);
export const mapDispatchToProps = {
refreshDatabaseDetails: (database: string) => {
return refreshDatabaseDetails(
new DatabaseDetailsRequest({ database, include_stats: true }),
);
},
refreshTableDetails: (database: string, table: string) => {
return refreshTableDetails(new TableDetailsRequest({ database, table }));
},
refreshTableStats: (database: string, table: string) => {
return refreshTableStats(new TableStatsRequest({ database, table }));
},
onViewModeChange: (viewMode: ViewMode) => viewModeLocalSetting.set(viewMode),
onSortingTablesChange: (columnName: string, ascending: boolean) =>
sortSettingTablesLocalSetting.set({
ascending: ascending,
columnTitle: columnName,
}),
onSortingGrantsChange: (columnName: string, ascending: boolean) =>
sortSettingGrantsLocalSetting.set({
ascending: ascending,
columnTitle: columnName,
}),
};