forked from certinia/debug-log-analyzer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDMLView.ts
More file actions
510 lines (449 loc) · 14.5 KB
/
DMLView.ts
File metadata and controls
510 lines (449 loc) · 14.5 KB
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
/*
* Copyright (c) 2022 Certinia Inc. All rights reserved.
*/
import {
provideVSCodeDesignSystem,
vsCodeButton,
vsCodeCheckbox,
} from '@vscode/webview-ui-toolkit';
import { LitElement, css, html, render, unsafeCSS, type PropertyValues } from 'lit';
import { customElement, property, state } from 'lit/decorators.js';
import { Tabulator, type GroupComponent, type RowComponent } from 'tabulator-tables';
// tabulator custom modules
import { GroupCalcs } from '../../datagrid/groups/GroupCalcs.js';
import { GroupSort } from '../../datagrid/groups/GroupSort.js';
import * as CommonModules from '../../datagrid/module/CommonModules.js';
import { RowKeyboardNavigation } from '../../datagrid/module/RowKeyboardNavigation.js';
import { RowNavigation } from '../../datagrid/module/RowNavigation.js';
import { Find, formatter } from '../calltree-view/module/Find.js';
// tabulator others
import NumberAccessor from '../../datagrid/dataaccessor/Number.js';
import Number from '../../datagrid/format/Number.js';
import dataGridStyles from '../../datagrid/style/DataGrid.scss';
// others
import { DatabaseAccess } from '../../Database.js';
import { ApexLog, DMLBeginLine } from '../../parsers/ApexLogParser.js';
import { vscodeMessenger } from '../../services/VSCodeExtensionMessenger.js';
import codiconStyles from '../../styles/codicon.css';
import { globalStyles } from '../../styles/global.styles.js';
import { isVisible } from '../../Util.js';
import databaseViewStyles from './DatabaseView.scss';
// lit components
import '../CallStack.js';
import './DatabaseSection.js';
provideVSCodeDesignSystem().register(vsCodeButton(), vsCodeCheckbox());
@customElement('dml-view')
export class DMLView extends LitElement {
@property()
timelineRoot: ApexLog | null = null;
@property()
highlightIndex: number = 0;
@property()
oldIndex: number = 0;
@state()
dmlLines: DMLBeginLine[] = [];
dmlTable: Tabulator | null = null;
holder: HTMLElement | null = null;
table: HTMLElement | null = null;
findArgs: { text: string; count: number; options: { matchCase: boolean } } = {
text: '',
count: 0,
options: { matchCase: false },
};
findMap: { [key: number]: RowComponent } = {};
totalMatches = 0;
constructor() {
super();
document.addEventListener('lv-find', this._findEvt);
document.addEventListener('lv-find-close', this._findEvt);
}
updated(changedProperties: PropertyValues): void {
if (
this.timelineRoot &&
changedProperties.has('timelineRoot') &&
!changedProperties.get('timelineRoot')
) {
this._appendTableWhenVisible();
}
if (changedProperties.has('highlightIndex')) {
this._highlightMatches(this.highlightIndex);
}
}
static styles = [
unsafeCSS(dataGridStyles),
unsafeCSS(databaseViewStyles),
unsafeCSS(codiconStyles),
globalStyles,
css`
:host {
display: flex;
flex-direction: column;
width: 100%;
}
#dml-table-container {
height: 100%;
}
#db-dml-table {
overflow: hidden;
table-layout: fixed;
width: 100%;
margin-bottom: 1rem;
}
`,
];
render() {
const dmlSkeleton = !this.timelineRoot ? html`<grid-skeleton></grid-skeleton>` : ``;
return html`
<database-section title="DML Statements" .dbLines="${this.dmlLines}"></database-section>
<datagrid-filter-bar>
<div slot="filters">
<strong>Group by</strong>
<div>
<vscode-checkbox @change="${this._dmlGroupBy}" checked>DML</vscode-checkbox>
</div>
</div>
<div slot="actions">
<vscode-button
appearance="icon"
aria-label="Export to CSV"
title="Export to CSV"
@click=${this._exportToCSV}
>
<span class="codicon codicon-desktop-download"></span>
</vscode-button>
<vscode-button
appearance="icon"
aria-label="Copy to clipboard"
title="Copy to clipboard"
@click=${this._copyToClipboard}
>
<span class="codicon codicon-copy"></span>
</vscode-button>
</div>
</datagrid-filter-bar>
<div id="dml-table-container">
${dmlSkeleton}
<div id="db-dml-table"></div>
</div>
`;
}
_copyToClipboard() {
this.dmlTable?.copyToClipboard('all');
}
_exportToCSV() {
this.dmlTable?.download('csv', 'dml.csv', { bom: true, delimiter: ',' });
}
_findEvt = ((event: FindEvt) => this._find(event)) as EventListener;
_dmlGroupBy(event: Event) {
const target = event.target as HTMLInputElement;
//@ts-expect-error This is a custom function added in the GroupSort custom module
this.dmlTable?.setSortedGroupBy(target.checked ? 'dml' : '');
}
get _dmlTableWrapper(): HTMLDivElement | null {
return this.renderRoot?.querySelector('#db-dml-table') ?? null;
}
_appendTableWhenVisible() {
if (this.dmlTable) {
return;
}
isVisible(this).then(async (isVisible) => {
const treeRoot = this.timelineRoot;
const tableWrapper = this._dmlTableWrapper;
if (tableWrapper && treeRoot && isVisible) {
const dbAccess = await DatabaseAccess.create(treeRoot);
this.dmlLines = dbAccess.getDMLLines() || [];
Tabulator.registerModule(Object.values(CommonModules));
Tabulator.registerModule([
RowKeyboardNavigation,
RowNavigation,
Find,
GroupCalcs,
GroupSort,
]);
this._renderDMLTable(tableWrapper, this.dmlLines);
}
});
}
_highlightMatches(highlightIndex: number) {
if (!this.dmlTable?.element?.clientHeight) {
return;
}
this.findArgs.count = highlightIndex;
const currentRow = this.findMap[highlightIndex];
const rows = [currentRow, this.findMap[this.oldIndex]];
rows.forEach((row) => {
row?.reformat();
});
if (currentRow) {
//@ts-expect-error This is a custom function added in by RowNavigation custom module
this.dmlTable.goToRow(currentRow, { scrollIfVisible: false, focusRow: false });
}
this.oldIndex = highlightIndex;
}
_find(e: CustomEvent<{ text: string; count: number; options: { matchCase: boolean } }>) {
const isTableVisible = !!this.dmlTable?.element?.clientHeight;
if (!isTableVisible && !this.totalMatches) {
return;
}
const newFindArgs = JSON.parse(JSON.stringify(e.detail));
if (!isTableVisible) {
newFindArgs.text = '';
}
const newSearch =
newFindArgs.text !== this.findArgs.text ||
newFindArgs.options.matchCase !== this.findArgs.options?.matchCase;
this.findArgs = newFindArgs;
const clearHighlights =
e.type === 'lv-find-close' || (!isTableVisible && newFindArgs.count === 0);
if (clearHighlights) {
newFindArgs.text = '';
}
if (newSearch || clearHighlights) {
//@ts-expect-error This is a custom function added in by Find custom module
const result = this.dmlTable.find(this.findArgs);
this.totalMatches = result.totalMatches;
this.findMap = result.matchIndexes;
if (!clearHighlights) {
document.dispatchEvent(
new CustomEvent('db-find-results', {
detail: { totalMatches: result.totalMatches, type: 'dml' },
}),
);
}
}
}
_renderDMLTable(dmlTableContainer: HTMLElement, dmlLines: DMLBeginLine[]) {
const dmlData: DMLRow[] = [];
if (dmlLines) {
for (const dml of dmlLines) {
dmlData.push({
dml: dml.text,
rowCount: dml.rowCount.self,
timeTaken: dml.duration.total,
timestamp: dml.timestamp,
_children: [{ timestamp: dml.timestamp, isDetail: true }],
});
}
}
const dmlText = this.sortByFrequency(dmlData || [], 'dml');
this.dmlTable = new Tabulator(dmlTableContainer, {
height: '100%',
clipboard: true,
downloadEncoder: this.downlodEncoder('dml.csv'),
downloadRowRange: 'all',
downloadConfig: {
columnHeaders: true,
columnGroups: true,
rowGroups: true,
columnCalcs: false,
dataTree: true,
},
//@ts-expect-error types need update array is valid
keybindings: { copyToClipboard: ['ctrl + 67', 'meta + 67'] },
clipboardCopyRowRange: 'all',
rowKeyboardNavigation: true,
data: dmlData, //set initial table data
layout: 'fitColumns',
placeholder: 'No DML statements found',
columnCalcs: 'table',
groupCalcs: true,
groupSort: true,
groupClosedShowCalcs: true,
groupStartOpen: false,
groupValues: [dmlText],
groupBy: ['dml'],
groupToggleElement: false,
selectableRowsCheck: function (row: RowComponent) {
return !row.getData().isDetail;
},
selectableRows: 'highlight',
dataTree: true,
dataTreeBranchElement: false,
columnDefaults: {
title: 'default',
resizable: true,
headerSortStartingDir: 'desc',
headerTooltip: true,
headerWordWrap: true,
},
headerSortElement: function (column, dir) {
switch (dir) {
case 'asc':
return "<div class='sort-by--top'></div>";
break;
case 'desc':
return "<div class='sort-by--bottom'></div>";
break;
default:
return "<div class='sort-by'><div class='sort-by--top'></div><div class='sort-by--bottom'></div></div>";
}
},
columns: [
{
title: 'DML',
field: 'dml',
sorter: 'string',
bottomCalc: () => {
return 'Total';
},
headerSortTristate: true,
cssClass: 'datagrid-textarea datagrid-code-text',
variableHeight: true,
formatter: (cell, _formatterParams, _onRendered) => {
const data = cell.getData() as DMLRow;
return `<call-stack
timestamp="${data.timestamp}"
startDepth="0"
endDepth="1"
></call-stack>`;
},
},
{
title: 'Row Count',
field: 'rowCount',
sorter: 'number',
width: 90,
bottomCalc: 'sum',
hozAlign: 'right',
headerHozAlign: 'right',
},
{
title: 'Time Taken (ms)',
field: 'timeTaken',
sorter: 'number',
width: 110,
hozAlign: 'right',
headerHozAlign: 'right',
formatter: Number,
formatterParams: {
thousand: false,
precision: 3,
},
accessorDownload: NumberAccessor,
bottomCalcFormatter: Number,
bottomCalc: 'sum',
bottomCalcFormatterParams: { precision: 3 },
},
],
rowFormatter: (row) => {
const data = row.getData();
if (data.isDetail && data.timestamp) {
const detailContainer = this.createDetailPanel(data.timestamp);
row.getElement().replaceChildren(detailContainer);
row.normalizeHeight();
}
formatter(row, this.findArgs);
},
});
this.dmlTable.on('dataFiltering', () => {
this._resetFindWidget();
this._clearSearchHighlights();
});
this.dmlTable.on('groupClick', (e: UIEvent, group: GroupComponent) => {
const { type } = window.getSelection() ?? {};
if (type === 'Range') {
return;
}
this.dmlTable?.blockRedraw();
group.toggle();
if (!group.isVisible()) {
this.dmlTable?.getRows().forEach((row) => {
if (!row.isTreeExpanded()) {
row.treeExpand();
}
});
}
this.dmlTable?.restoreRedraw();
});
this.dmlTable.on('rowClick', function (e, row) {
const { type } = window.getSelection() ?? {};
if (type === 'Range') {
return;
}
const data = row.getData();
if (!(data.timestamp && data.dml)) {
return;
}
const origRowHeight = row.getElement().offsetHeight;
row.treeToggle();
row.getCell('dml').getElement().style.height = origRowHeight + 'px';
});
this.dmlTable.on('renderStarted', () => {
const holder = this._getTableHolder();
holder.style.minHeight = holder.clientHeight + 'px';
holder.style.overflowAnchor = 'none';
});
this.dmlTable.on('renderComplete', () => {
const holder = this._getTableHolder();
const table = this._getTable();
holder.style.minHeight = Math.min(holder.clientHeight, table.clientHeight) + 'px';
});
}
_resetFindWidget() {
document.dispatchEvent(
new CustomEvent('db-find-results', {
detail: { totalMatches: 0, type: 'dml' },
}),
);
}
private _clearSearchHighlights() {
this._find(
new CustomEvent('lv-find', {
detail: { text: '', count: 0, options: { matchCase: false } },
}),
);
}
_getTable() {
this.table ??= this.dmlTable?.element.querySelector('.tabulator-table') as HTMLElement;
return this.table;
}
_getTableHolder() {
this.holder = this.dmlTable?.element.querySelector('.tabulator-tableholder') as HTMLElement;
return this.holder;
}
createDetailPanel(timestamp: number) {
const detailContainer = document.createElement('div');
detailContainer.className = 'row__details-container';
render(html`<call-stack timestamp=${timestamp}></call-stack>`, detailContainer);
return detailContainer;
}
sortByFrequency(dataArray: DMLRow[], field: keyof DMLRow) {
const map = new Map<unknown, number>();
dataArray.forEach((row) => {
const val = row[field];
map.set(val, (map.get(val) || 0) + 1);
});
const newMap = new Map([...map.entries()].sort((a, b) => b[1] - a[1]));
return [...newMap.keys()];
}
downlodEncoder(defaultFileName: string) {
return function (fileContents: string, mimeType: string) {
const vscode = vscodeMessenger.getVsCodeAPI();
if (vscode) {
vscodeMessenger.send<VSCodeSaveFile>('saveFile', {
fileContent: fileContents,
options: {
defaultFileName: defaultFileName,
},
});
return false;
}
return new Blob([fileContents], { type: mimeType });
};
}
}
type VSCodeSaveFile = {
fileContent: string;
options: {
defaultFileName: string;
};
};
interface DMLRow {
dml?: string;
rowCount?: number;
timeTaken?: number;
timestamp: number;
isDetail?: boolean;
_children?: DMLRow[];
}
type FindEvt = CustomEvent<{ text: string; count: number; options: { matchCase: boolean } }>;