-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
statementsTable.tsx
384 lines (368 loc) · 12.1 KB
/
statementsTable.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
// 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 classNames from "classnames/bind";
import {
FixLong,
longToInt,
StatementSummary,
StatementStatistics,
} from "src/util";
import {
countBarChart,
rowsReadBarChart,
bytesReadBarChart,
rowsWrittenBarChart,
latencyBarChart,
contentionBarChart,
maxMemUsageBarChart,
networkBytesBarChart,
retryBarChart,
workloadPctBarChart,
} from "src/barCharts";
import { ActivateDiagnosticsModalRef } from "src/statementsDiagnostics";
import {
ColumnDescriptor,
longListWithTooltip,
SortedTable,
} from "src/sortedtable";
import { cockroach } from "@cockroachlabs/crdb-protobuf-client";
import { StatementTableCell } from "./statementsTableContent";
import {
statisticsTableTitles,
NodeNames,
StatisticType,
formatAggregationIntervalColumn,
} from "../statsTableUtil/statsTableUtil";
type IStatementDiagnosticsReport = cockroach.server.serverpb.IStatementDiagnosticsReport;
type ICollectedStatementStatistics = cockroach.server.serverpb.StatementsResponse.ICollectedStatementStatistics;
import styles from "./statementsTable.module.scss";
const cx = classNames.bind(styles);
function makeCommonColumns(
statements: AggregateStatistics[],
totalWorkload: number,
nodeRegions: { [nodeId: string]: string },
statType: StatisticType,
): ColumnDescriptor<AggregateStatistics>[] {
const defaultBarChartOptions = {
classes: {
root: cx("statements-table__col--bar-chart"),
label: cx("statements-table__col--bar-chart__label"),
},
};
const sampledExecStatsBarChartOptions = {
classes: defaultBarChartOptions.classes,
displayNoSamples: (d: ICollectedStatementStatistics) => {
return longToInt(d.stats.exec_stats?.count) == 0;
},
};
const countBar = countBarChart(statements, defaultBarChartOptions);
const rowsReadBar = rowsReadBarChart(statements, defaultBarChartOptions);
const bytesReadBar = bytesReadBarChart(statements, defaultBarChartOptions);
const rowsWrittenBar = rowsWrittenBarChart(
statements,
defaultBarChartOptions,
);
const latencyBar = latencyBarChart(statements, defaultBarChartOptions);
const contentionBar = contentionBarChart(
statements,
sampledExecStatsBarChartOptions,
);
const maxMemUsageBar = maxMemUsageBarChart(
statements,
sampledExecStatsBarChartOptions,
);
const networkBytesBar = networkBytesBarChart(
statements,
sampledExecStatsBarChartOptions,
);
const retryBar = retryBarChart(statements, defaultBarChartOptions);
return [
{
name: "aggregationInterval",
title: statisticsTableTitles.aggregationInterval(statType),
className: cx("statements-table__interval_time"),
cell: (stmt: AggregateStatistics) =>
formatAggregationIntervalColumn(
stmt.aggregatedTs,
stmt.aggregationInterval,
),
sort: (stmt: AggregateStatistics) => stmt.aggregatedTs,
},
{
name: "executionCount",
title: statisticsTableTitles.executionCount(statType),
className: cx("statements-table__col-count"),
cell: countBar,
sort: (stmt: AggregateStatistics) => FixLong(Number(stmt.stats.count)),
},
{
name: "database",
title: statisticsTableTitles.database(statType),
className: cx("statements-table__col-database"),
cell: (stmt: AggregateStatistics) => stmt.database,
sort: (stmt: AggregateStatistics) => stmt.database,
showByDefault: false,
},
{
name: "rowsRead",
title: statisticsTableTitles.rowsRead(statType),
className: cx("statements-table__col-rows-read"),
cell: rowsReadBar,
sort: (stmt: AggregateStatistics) =>
FixLong(Number(stmt.stats.rows_read.mean)),
},
{
name: "bytesRead",
title: statisticsTableTitles.bytesRead(statType),
cell: bytesReadBar,
sort: (stmt: AggregateStatistics) =>
FixLong(Number(stmt.stats.bytes_read.mean)),
},
{
name: "rowsWritten",
title: statisticsTableTitles.rowsWritten(statType),
cell: rowsWrittenBar,
sort: (stmt: AggregateStatistics) =>
FixLong(Number(stmt.stats.rows_written?.mean)),
showByDefault: false,
},
{
name: "time",
title: statisticsTableTitles.time(statType),
className: cx("statements-table__col-latency"),
cell: latencyBar,
sort: (stmt: AggregateStatistics) => stmt.stats.service_lat.mean,
},
{
name: "contention",
title: statisticsTableTitles.contention(statType),
cell: contentionBar,
sort: (stmt: AggregateStatistics) =>
FixLong(Number(stmt.stats.exec_stats.contention_time.mean)),
},
{
name: "maxMemUsage",
title: statisticsTableTitles.maxMemUsage(statType),
cell: maxMemUsageBar,
sort: (stmt: AggregateStatistics) =>
FixLong(Number(stmt.stats.exec_stats.max_mem_usage.mean)),
},
{
name: "networkBytes",
title: statisticsTableTitles.networkBytes(statType),
cell: networkBytesBar,
sort: (stmt: AggregateStatistics) =>
FixLong(Number(stmt.stats.exec_stats.network_bytes.mean)),
},
{
name: "retries",
title: statisticsTableTitles.retries(statType),
className: cx("statements-table__col-retries"),
cell: retryBar,
sort: (stmt: AggregateStatistics) =>
longToInt(stmt.stats.count) - longToInt(stmt.stats.first_attempt_count),
},
{
name: "workloadPct",
title: statisticsTableTitles.workloadPct(statType),
cell: workloadPctBarChart(
statements,
defaultBarChartOptions,
totalWorkload,
),
sort: (stmt: AggregateStatistics) =>
(stmt.stats.service_lat.mean * longToInt(stmt.stats.count)) /
totalWorkload,
},
{
name: "regionNodes",
title: statisticsTableTitles.regionNodes(statType),
className: cx("statements-table__col-regions"),
cell: (stmt: AggregateStatistics) => {
return longListWithTooltip(stmt.regionNodes.sort().join(", "), 50);
},
sort: (stmt: AggregateStatistics) => stmt.regionNodes.sort().join(", "),
hideIfTenant: true,
},
];
}
export interface AggregateStatistics {
aggregatedFingerprintID: string;
// label is either shortStatement (StatementsPage) or nodeId (StatementDetails).
label: string;
// summary exists only for SELECT/INSERT/UPSERT/UPDATE statements, and is
// replaced with shortStatement otherwise.
summary: string;
aggregatedTs: number;
aggregationInterval: number;
implicitTxn: boolean;
fullScan: boolean;
database: string;
stats: StatementStatistics;
drawer?: boolean;
firstCellBordered?: boolean;
diagnosticsReports?: cockroach.server.serverpb.IStatementDiagnosticsReport[];
// totalWorkload is the sum of service latency of all statements listed on the table.
totalWorkload?: Long;
regionNodes?: string[];
}
export class StatementsSortedTable extends SortedTable<AggregateStatistics> {}
export function shortStatement(summary: StatementSummary, original: string) {
switch (summary.statement) {
case "update":
return "UPDATE " + summary.table;
case "insert":
return "INSERT INTO " + summary.table;
case "select":
return "SELECT FROM " + summary.table;
case "delete":
return "DELETE FROM " + summary.table;
case "create":
return "CREATE TABLE " + summary.table;
case "set":
return "SET " + summary.table;
default:
return original;
}
}
export function makeStatementFingerprintColumn(
statType: StatisticType,
selectedApp: string,
search?: string,
onStatementClick?: (statement: string) => void,
): ColumnDescriptor<AggregateStatistics> {
return {
name: "statements",
title: statisticsTableTitles.statements(statType),
className: cx("cl-table__col-query-text"),
cell: StatementTableCell.statements(search, selectedApp, onStatementClick),
sort: stmt => stmt.label,
alwaysShow: true,
};
}
export function makeStatementsColumns(
statements: AggregateStatistics[],
selectedApp: string,
// totalWorkload is the sum of service latency of all statements listed on the table.
totalWorkload: number,
nodeRegions: { [nodeId: string]: string },
statType: StatisticType,
isTenant: boolean,
hasViewActivityRedactedRole: boolean,
search?: string,
activateDiagnosticsRef?: React.RefObject<ActivateDiagnosticsModalRef>,
onSelectDiagnosticsReportDropdownOption?: (
report: IStatementDiagnosticsReport,
) => void,
onStatementClick?: (statement: string) => void,
): ColumnDescriptor<AggregateStatistics>[] {
const columns: ColumnDescriptor<AggregateStatistics>[] = [
makeStatementFingerprintColumn(
statType,
selectedApp,
search,
onStatementClick,
),
];
columns.push(
...makeCommonColumns(statements, totalWorkload, nodeRegions, statType),
);
if (activateDiagnosticsRef && !isTenant && !hasViewActivityRedactedRole) {
const diagnosticsColumn: ColumnDescriptor<AggregateStatistics> = {
name: "diagnostics",
title: statisticsTableTitles.diagnostics(statType),
cell: StatementTableCell.diagnostics(
activateDiagnosticsRef,
onSelectDiagnosticsReportDropdownOption,
),
sort: stmt => {
if (stmt.diagnosticsReports?.length > 0) {
// Perform sorting by first diagnostics report as only
// this one can be either in ready or waiting status.
return stmt.diagnosticsReports[0].completed ? "READY" : "WAITING";
}
return null;
},
alwaysShow: true,
titleAlign: "right",
};
columns.push(diagnosticsColumn);
}
return columns;
}
export function makeNodesColumns(
statements: AggregateStatistics[],
nodeNames: NodeNames,
totalWorkload: number,
nodeRegions: { [nodeId: string]: string },
): ColumnDescriptor<AggregateStatistics>[] {
const original: ColumnDescriptor<AggregateStatistics>[] = [
{
name: "nodes",
title: null,
cell: StatementTableCell.nodeLink(nodeNames),
},
];
return original.concat(
makeCommonColumns(statements, totalWorkload, nodeRegions, "statement"),
);
}
/**
* For each statement, generate the list of regions and nodes it was
* executed on. Each node is assigned to only one region and a region can
* have multiple nodes.
* E.g. of one element of the list: `gcp-us-east1 (n1, n2, n3)`
* @param statements: list of statements containing details about which
* node it was executed on.
* @param nodeRegions: object with keys being the node id and the value
* which region it belongs to.
* @param isTenant: boolean indicating if the cluster is tenant, since
* node information doesn't need to be populated on this case.
*/
export function populateRegionNodeForStatements(
statements: AggregateStatistics[],
nodeRegions: { [p: string]: string },
isTenant: boolean,
): void {
statements.forEach(stmt => {
if (isTenant) {
stmt.regionNodes = [];
return;
}
const regions: { [region: string]: Set<number> } = {};
// For each region, populate a list of all nodes where the statement was executed.
// E.g. {"gcp-us-east1" : [1,3,4]}
if (stmt.stats.nodes) {
stmt.stats.nodes.forEach(node => {
if (Object.keys(regions).includes(nodeRegions[node.toString()])) {
regions[nodeRegions[node.toString()]].add(longToInt(node));
} else {
regions[nodeRegions[node.toString()]] = new Set([longToInt(node)]);
}
});
}
// Create a list nodes/regions where a statement was executed on, with
// format: region (node1,node2)
const regionNodes: string[] = [];
Object.keys(regions).forEach(region => {
regionNodes.push(
region +
" (" +
Array.from(regions[region])
.sort()
.map(n => "n" + n)
.toString() +
")",
);
});
stmt.regionNodes = regionNodes;
});
}