• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (C) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16import { BaseElement, element } from '../../../../../base-ui/BaseElement.js';
17import { LitTable } from '../../../../../base-ui/table/lit-table.js';
18import { BoxJumpParam, SelectionData } from '../../../../bean/BoxSelection.js';
19import { getTabBoxChildData } from '../../../../database/SqlLite.js';
20import { Utils } from '../../base/Utils.js';
21import { SPTChild } from '../../../../bean/StateProcessThread.js';
22import { resizeObserver } from '../SheetUtils.js';
23
24@element('tabpane-box-child')
25export class TabPaneBoxChild extends BaseElement {
26  private boxChildTbl: LitTable | null | undefined;
27  private boxChildRange: HTMLLabelElement | null | undefined;
28  private boxChildSource: Array<SPTChild> = [];
29
30  set data(boxChildValue: BoxJumpParam) {
31    // @ts-ignore
32    this.boxChildTbl?.shadowRoot?.querySelector('.table')?.style?.height = this.parentElement!.clientHeight - 45 + 'px';
33    this.boxChildRange!.textContent =
34      'Selected range: ' + parseFloat(((boxChildValue.rightNs - boxChildValue.leftNs) / 1000000.0).toFixed(5)) + ' ms';
35    if (
36      boxChildValue.state != null &&
37      boxChildValue.state != undefined &&
38      boxChildValue.processId &&
39      boxChildValue.threadId
40    ) {
41      this.boxChildTbl!.recycleDataSource = [];
42      this.getDataByDB(boxChildValue);
43    }
44  }
45
46  initElements(): void {
47    this.boxChildTbl = this.shadowRoot?.querySelector<LitTable>('#tb-cpu-thread');
48    this.boxChildRange = this.shadowRoot?.querySelector('#time-range');
49    this.boxChildTbl!.addEventListener('column-click', (evt) => {
50      // @ts-ignore
51      this.sortByColumn(evt.detail);
52    });
53  }
54
55  connectedCallback() {
56    super.connectedCallback();
57    resizeObserver(this.parentElement!, this.boxChildTbl!);
58  }
59
60  getDataByDB(val: BoxJumpParam) {
61    this.boxChildTbl!.loading = true;
62    getTabBoxChildData(val.leftNs, val.rightNs, val.state, val.processId, val.threadId).then((result) => {
63      this.boxChildTbl!.loading = false;
64      if (result.length != null && result.length > 0) {
65        result.map((e) => {
66          e.startTime = Utils.getTimeString(e.startNs);
67          e.absoluteTime = ((window as any).recordStartNS + e.startNs) / 1000000000;
68          e.state = Utils.getEndState(e.state)!;
69          e.prior = e.priority == undefined || e.priority == null ? '-' : e.priority + '';
70          e.core = e.cpu == undefined || e.cpu == null ? '-' : 'CPU' + e.cpu;
71          e.processName =
72            (e.process == undefined || e.process == null ? 'process' : e.process) + '(' + e.processId + ')';
73          e.threadName = (e.thread == undefined || e.thread == null ? 'thread' : e.thread) + '(' + e.threadId + ')';
74          e.note = '-';
75        });
76        this.boxChildSource = result;
77        // @ts-ignore
78        this.boxChildTbl?.recycleDataSource = result;
79      } else {
80        this.boxChildSource = [];
81        // @ts-ignore
82        this.boxChildTbl?.recycleDataSource = [];
83      }
84    });
85  }
86
87  initHtml(): string {
88    return `
89        <style>
90        .box-child-label{
91          text-align: end;
92          width: 100%;
93          height: 20px;
94        }
95        :host{
96            padding: 10px 10px;
97            display: flex;
98            flex-direction: column;
99        }
100        </style>
101        <label id="time-range" class="box-child-label" style="font-size: 10pt;margin-bottom: 5px">Selected range:0.0 ms</label>
102        <lit-table id="tb-cpu-thread" style="height: auto">
103            <lit-table-column order title="StartTime(Relative)" width="15%" data-index="startTime" key="startTime" align="flex-start" order >
104            </lit-table-column>
105            <lit-table-column order title="StartTime(Absolute)" width="15%" data-index="absoluteTime" key="absoluteTime" align="flex-start" order >
106            </lit-table-column>
107            <lit-table-column order width="20%" data-index="processName" key="processName" title="Process" align="flex-start" order >
108            </lit-table-column>
109            <lit-table-column order width="20%" data-index="threadName" key="threadName" align="flex-start" order title="Thread">
110            </lit-table-column>
111            <lit-table-column order width="1fr" data-index="state" key="state" align="flex-start" order title="State">
112            </lit-table-column>
113            <lit-table-column order width="1fr"data-index="core"  title="Core" key="core" align="flex-start" order >
114            </lit-table-column>
115            <lit-table-column order width="1fr" data-index="prior" title="Priority" key="prior" align="flex-start" order >
116            </lit-table-column>
117            <lit-table-column order width="1fr" data-index="note" key="note" align="flex-start" title="Note">
118            </lit-table-column>
119        </lit-table>
120        `;
121  }
122
123  sortByColumn(detail: any) {
124    // @ts-ignore
125    function compare(property, sort, type) {
126      return function (boxChildLeftData: SelectionData, boxChildRightData: SelectionData) {
127        if (type === 'number') {
128          return sort === 2
129            ? // @ts-ignore
130              parseFloat(boxChildRightData[property]) - parseFloat(boxChildLeftData[property])
131            : // @ts-ignore
132              parseFloat(boxChildLeftData[property]) - parseFloat(boxChildRightData[property]);
133        } else {
134          // @ts-ignore
135          if (boxChildRightData[property] > boxChildLeftData[property]) {
136            return sort === 2 ? 1 : -1;
137          } else {
138            // @ts-ignore
139            if (boxChildRightData[property] == boxChildLeftData[property]) {
140              return 0;
141            } else {
142              return sort === 2 ? -1 : 1;
143            }
144          }
145        }
146      };
147    }
148
149    // @ts-ignore
150    this.boxChildSource.sort(compare(detail.key, detail.sort, 'string'));
151    this.boxChildTbl!.recycleDataSource = this.boxChildSource;
152  }
153}
154