summaryrefslogtreecommitdiff
path: root/chromium/v8/tools/system-analyzer/view/list-panel.mjs
blob: 85e3cd47e28f360c86291a57c48cd59a129bbe5e (plain)
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
// Copyright 2020 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import {Script, SourcePosition} from '../../profile.mjs';
import {LogEntry} from '../log/log.mjs';

import {FocusEvent} from './events.mjs';
import {groupBy, LazyTable} from './helper.mjs';
import {DOM, V8CustomElement} from './helper.mjs';

DOM.defineCustomElement('view/list-panel',
                        (templateText) =>
                            class ListPanel extends V8CustomElement {
  _selectedLogEntries = [];
  _displayedLogEntries = [];
  _timeline;

  _detailsClickHandler = this._handleDetailsClick.bind(this);
  _logEntryClickHandler = this._handleLogEntryClick.bind(this);

  constructor() {
    super(templateText);
    this.groupKey.addEventListener('change', e => this.update());
    this.showAllRadio.onclick = _ => this._showEntries(this._timeline);
    this.showTimerangeRadio.onclick = _ =>
        this._showEntries(this._timeline.selectionOrSelf);
    this.showSelectionRadio.onclick = _ =>
        this._showEntries(this._selectedLogEntries);
  }

  static get observedAttributes() {
    return ['title'];
  }

  attributeChangedCallback(name, oldValue, newValue) {
    if (name == 'title') {
      this.$('#title').innerHTML = newValue;
    }
  }

  set timeline(timeline) {
    console.assert(timeline !== undefined, 'timeline undefined!');
    this._timeline = timeline;
    this.$('.panel').style.display = timeline.isEmpty() ? 'none' : 'inherit';
    this._initGroupKeySelect();
  }

  set selectedLogEntries(entries) {
    if (entries === this._timeline) {
      this.showAllRadio.click();
    } else if (entries === this._timeline.selection) {
      this.showTimerangeRadio.click();
    } else {
      this._selectedLogEntries = entries;
      this.showSelectionRadio.click();
    }
  }

  get entryClass() {
    return this._timeline.at(0)?.constructor;
  }

  get groupKey() {
    return this.$('#group-key');
  }

  get table() {
    return this.$('#table');
  }

  get showAllRadio() {
    return this.$('#show-all');
  }
  get showTimerangeRadio() {
    return this.$('#show-timerange');
  }
  get showSelectionRadio() {
    return this.$('#show-selection');
  }

  get _propertyNames() {
    return this.entryClass?.propertyNames ?? [];
  }

  _initGroupKeySelect() {
    const select = this.groupKey;
    select.options.length = 0;
    for (const propertyName of this._propertyNames) {
      const option = DOM.element('option');
      option.text = propertyName;
      select.add(option);
    }
  }

  _showEntries(entries) {
    this._displayedLogEntries = entries;
    this.update();
  }

  _update() {
    if (this._timeline.isEmpty()) return;
    DOM.removeAllChildren(this.table);
    if (this._displayedLogEntries.length == 0) return;
    const propertyName = this.groupKey.selectedOptions[0].text;
    const groups =
        groupBy(this._displayedLogEntries, each => each[propertyName], true);
    this._render(groups, this.table);
  }

  createSubgroups(group) {
    const map = new Map();
    for (let propertyName of this._propertyNames) {
      map.set(
          propertyName,
          groupBy(group.entries, each => each[propertyName], true));
    }
    return map;
  }

  _handleLogEntryClick(e) {
    const group = e.currentTarget.group;
    this.dispatchEvent(new FocusEvent(group.key));
  }

  _handleDetailsClick(event) {
    event.stopPropagation();
    const tr = event.target.parentNode;
    const group = tr.group;
    // Create subgroup in-place if the don't exist yet.
    if (tr.groups === undefined) {
      const groups = tr.groups = this.createSubgroups(group);
      this.renderDrilldown(groups, tr);
    }
    const detailsTr = tr.nextSibling;
    if (tr.classList.contains('open')) {
      tr.classList.remove('open');
      detailsTr.style.display = 'none';
    } else {
      tr.classList.add('open');
      detailsTr.style.display = 'table-row';
    }
  }

  renderDrilldown(groups, previousSibling) {
    const tr = DOM.tr('entry-details');
    tr.style.display = 'none';
    // indent by one td.
    tr.appendChild(DOM.td());
    const td = DOM.td();
    td.colSpan = 3;
    groups.forEach((group, key) => {
      this.renderDrilldownGroup(td, group, key);
    });
    tr.appendChild(td);
    // Append the new TR after previousSibling.
    previousSibling.parentNode.insertBefore(tr, previousSibling.nextSibling);
  }

  renderDrilldownGroup(td, groups, key) {
    const div = DOM.div('drilldown-group-title');
    div.textContent = `Grouped by ${key}: ${groups[0]?.parentTotal ?? 0}#`;
    td.appendChild(div);
    const table = DOM.table();
    this._render(groups, table, false)
    td.appendChild(table);
  }

  _render(groups, table) {
    let last;
    new LazyTable(table, groups, group => {
      if (last && last.count < group.count) {
        console.log(last, group);
      }
      last = group;
      const tr = DOM.tr();
      tr.group = group;
      const details = tr.appendChild(DOM.td('', 'toggle'));
      details.onclick = this._detailsClickHandler;
      tr.appendChild(DOM.td(`${group.percent.toFixed(2)}%`, 'percentage'));
      tr.appendChild(DOM.td(group.count, 'count'));
      const valueTd = tr.appendChild(DOM.td(`${group.key}`, 'key'));
      if (this._isClickable(group.key)) {
        tr.onclick = this._logEntryClickHandler;
        valueTd.classList.add('clickable');
      }
      return tr;
    }, 10);
  }

  _isClickable(object) {
    if (typeof object !== 'object') return false;
    if (object instanceof LogEntry) return true;
    if (object instanceof SourcePosition) return true;
    if (object instanceof Script) return true;
    return false;
  }
});