-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
Copy pathstatementsPage.selectors.ts
239 lines (218 loc) · 7.35 KB
/
statementsPage.selectors.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
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
// 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 { createSelector } from "reselect";
import {
aggregateStatementStats,
appAttr,
combineStatementStats,
ExecutionStatistics,
flattenStatementStats,
formatDate,
queryByName,
statementKey,
StatementStatistics,
TimestampToMoment,
unset,
} from "src/util";
import { cockroach } from "@cockroachlabs/crdb-protobuf-client";
import { RouteComponentProps } from "react-router-dom";
import { AppState } from "src/store";
import { selectDiagnosticsReportsPerStatement } from "../store/statementDiagnostics";
import { AggregateStatistics } from "../statementsTable";
import { sqlStatsSelector } from "../store/sqlStats/sqlStats.selector";
import { SQLStatsState } from "../store/sqlStats";
import { localStorageSelector } from "../store/utils/selectors";
type ICollectedStatementStatistics =
cockroach.server.serverpb.StatementsResponse.ICollectedStatementStatistics;
export interface StatementsSummaryData {
statementFingerprintID: string;
statementFingerprintHexID: string;
statement: string;
statementSummary: string;
aggregatedTs: number;
aggregationInterval: number;
implicitTxn: boolean;
fullScan: boolean;
database: string;
stats: StatementStatistics[];
}
// selectApps returns the array of all apps with statement statistics present
// in the data.
export const selectApps = createSelector(sqlStatsSelector, sqlStatsState => {
if (!sqlStatsState.data || !sqlStatsState.valid) {
return [];
}
let sawBlank = false;
const apps: { [app: string]: boolean } = {};
sqlStatsState.data.statements.forEach(
(statement: ICollectedStatementStatistics) => {
const isNotInternalApp =
sqlStatsState.data.internal_app_name_prefix &&
!statement.key.key_data.app.startsWith(
sqlStatsState.data.internal_app_name_prefix,
);
if (
sqlStatsState.data.internal_app_name_prefix == undefined ||
isNotInternalApp
) {
if (statement.key.key_data.app) {
apps[statement.key.key_data.app] = true;
} else {
sawBlank = true;
}
}
},
);
return [].concat(sawBlank ? [unset] : []).concat(Object.keys(apps).sort());
});
// selectDatabases returns the array of all databases with statement statistics present
// in the data.
export const selectDatabases = createSelector(
sqlStatsSelector,
sqlStatsState => {
if (!sqlStatsState.data) {
return [];
}
return Array.from(
new Set(
sqlStatsState.data.statements.map(s =>
s.key.key_data.database ? s.key.key_data.database : unset,
),
),
)
.filter((dbName: string) => dbName !== null && dbName.length > 0)
.sort();
},
);
// selectTotalFingerprints returns the count of distinct statement fingerprints
// present in the data.
export const selectTotalFingerprints = createSelector(
sqlStatsSelector,
state => {
if (!state.data) {
return 0;
}
const aggregated = aggregateStatementStats(state.data.statements);
return aggregated.length;
},
);
// selectLastReset returns a string displaying the last time the statement
// statistics were reset.
export const selectLastReset = createSelector(sqlStatsSelector, state => {
if (!state.data) {
return "";
}
return formatDate(TimestampToMoment(state.data.last_reset));
});
export const selectStatements = createSelector(
sqlStatsSelector,
(_: AppState, props: RouteComponentProps) => props,
selectDiagnosticsReportsPerStatement,
(
state: SQLStatsState,
props: RouteComponentProps<any>,
diagnosticsReportsPerStatement,
): AggregateStatistics[] => {
// State is valid if we successfully fetched data, and the data has not yet been invalidated.
if (!state.data || !state.valid) {
return null;
}
let statements = flattenStatementStats(state.data.statements);
const app = queryByName(props.location, appAttr);
const isInternal = (statement: ExecutionStatistics) =>
statement.app.startsWith(state.data.internal_app_name_prefix);
if (app && app !== "All") {
const criteria = decodeURIComponent(app).split(",");
let showInternal = false;
if (criteria.includes(state.data.internal_app_name_prefix)) {
showInternal = true;
}
if (criteria.includes(unset)) {
criteria.push("");
}
statements = statements.filter(
(statement: ExecutionStatistics) =>
(showInternal && isInternal(statement)) ||
criteria.includes(statement.app),
);
} else {
// We don't want to show internal statements by default.
statements = statements.filter(
(statement: ExecutionStatistics) => !isInternal(statement),
);
}
const statsByStatementKey: {
[statement: string]: StatementsSummaryData;
} = {};
statements.forEach(stmt => {
const key = statementKey(stmt);
if (!(key in statsByStatementKey)) {
statsByStatementKey[key] = {
statementFingerprintID: stmt.statement_fingerprint_id?.toString(),
statementFingerprintHexID:
stmt.statement_fingerprint_id?.toString(16),
statement: stmt.statement,
statementSummary: stmt.statement_summary,
aggregatedTs: stmt.aggregated_ts,
aggregationInterval: stmt.aggregation_interval,
implicitTxn: stmt.implicit_txn,
fullScan: stmt.full_scan,
database: stmt.database,
stats: [],
};
}
statsByStatementKey[key].stats.push(stmt.stats);
});
return Object.keys(statsByStatementKey).map(key => {
const stmt = statsByStatementKey[key];
return {
aggregatedFingerprintID: stmt.statementFingerprintID,
aggregatedFingerprintHexID: stmt.statementFingerprintID,
label: stmt.statement,
summary: stmt.statementSummary,
aggregatedTs: stmt.aggregatedTs,
aggregationInterval: stmt.aggregationInterval,
implicitTxn: stmt.implicitTxn,
fullScan: stmt.fullScan,
database: stmt.database,
stats: combineStatementStats(stmt.stats),
diagnosticsReports: diagnosticsReportsPerStatement[stmt.statement],
};
});
},
);
export const selectStatementsLastError = createSelector(
sqlStatsSelector,
state => state.lastError,
);
export const selectColumns = createSelector(
localStorageSelector,
// return array of columns if user have customized it or `null` otherwise
localStorage =>
localStorage["showColumns/StatementsPage"]
? localStorage["showColumns/StatementsPage"].split(",")
: null,
);
export const selectTimeScale = createSelector(
localStorageSelector,
localStorage => localStorage["timeScale/SQLActivity"],
);
export const selectSortSetting = createSelector(
localStorageSelector,
localStorage => localStorage["sortSetting/StatementsPage"],
);
export const selectFilters = createSelector(
localStorageSelector,
localStorage => localStorage["filters/StatementsPage"],
);
export const selectSearch = createSelector(
localStorageSelector,
localStorage => localStorage["search/StatementsPage"],
);