• 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';
17import { type LitTable } from '../../../../../base-ui/table/lit-table';
18import { type Dma } from '../../../../bean/AbilityMonitor';
19import { ns2s } from '../../../../database/ui-worker/ProcedureWorkerCommon';
20import { SpSystemTrace } from '../../../SpSystemTrace';
21import { Utils } from '../../base/Utils';
22import { getTabDmaAbilityClickData } from '../../../../database/sql/Dma.sql';
23
24@element('tabpane-dma-selection-ability')
25export class TabPaneDmaSelectAbility extends BaseElement {
26  private damClickTable: LitTable | null | undefined;
27  private dmaClickSource: Array<Dma> = [];
28  private tableThead: HTMLDivElement | undefined | null;
29  private table: HTMLDivElement | undefined | null;
30
31  initElements(): void {
32    this.damClickTable = this.shadowRoot?.querySelector<LitTable>('#damClickTable');
33    this.tableThead = this.damClickTable?.shadowRoot?.querySelector('.thead') as HTMLDivElement;
34    this.damClickTable!.addEventListener('column-click', (e) => {
35      // @ts-ignore
36      this.sortDmaByColumn(e.detail.key, e.detail.sort);
37    });
38  }
39
40  private init(): void {
41    const thTable = this.tableThead!.querySelector('.th');
42    const dmaSelectTblNodes = thTable!.querySelectorAll('div');
43    if (this.tableThead!.hasAttribute('sort')) {
44      this.tableThead!.removeAttribute('sort');
45      dmaSelectTblNodes.forEach((item) => {
46        item.querySelectorAll('svg').forEach((svg) => {
47          svg.style.display = 'none';
48        });
49      });
50    }
51  }
52
53  connectedCallback(): void {
54    super.connectedCallback();
55    new ResizeObserver(() => {
56      if (this.parentElement?.clientHeight !== 0 && this.damClickTable) {
57        // @ts-ignore
58        this.damClickTable.shadowRoot.querySelector('.table').style.height =
59          this.parentElement!.clientHeight - 18 + 'px';
60        this.parentElement!.style.overflow = 'hidden';
61        this.damClickTable?.reMeauseHeight();
62      }
63    }).observe(this.parentElement!);
64  }
65
66  queryDmaClickDataByDB(startNs: number): void {
67    this.init();
68    getTabDmaAbilityClickData(startNs).then((data) => {
69      if (data.length !== null && data.length > 0) {
70        data.forEach((item) => {
71          item.bufName = SpSystemTrace.DATA_DICT.get(item.bufName as number) || '-';
72          item.expName = SpSystemTrace.DATA_DICT.get(item.expName as number) || '-';
73          item.expTaskComm = SpSystemTrace.DATA_DICT.get(item.expTaskComm as number) || '-';
74          if (item.processName !== null) {
75            item.process = `${item.processName}(${item.processId})`;
76          } else {
77            item.process = `Process(${item.processId})`;
78          }
79          item.sizes = Utils.getBinaryByteWithUnit(item.size);
80          item.timeStamp = ns2s(item.startNs);
81          // @ts-ignore
82          this.damClickTable!.getItemTextColor = (dmaItem: Dma): unknown => {
83            if (dmaItem.flag === 1) {
84              return '#d4b550';
85            } else if (dmaItem.flag === 2) {
86              return '#f86b6b';
87            } else {
88              return '#000000';
89            }
90          };
91        });
92        this.damClickTable!.snapshotDataSource = data.sort(function (
93          dmaAbilityLeftData: Dma,
94          dmaAbilityRightData: Dma
95        ) {
96          return dmaAbilityRightData.size - dmaAbilityLeftData.size;
97        });
98        this.dmaClickSource = data;
99      } else {
100        this.damClickTable!.snapshotDataSource = [];
101        this.dmaClickSource = [];
102      }
103    });
104  }
105
106  initHtml(): string {
107    return `
108<style>
109.damClickTable{
110    height: auto;
111}
112:host{
113    display: flex;
114    padding: 10px 10px;
115    flex-direction: column;
116}
117</style>
118<lit-table id="damClickTable" class="damClickTable">
119    <lit-table-column order title="TimeStamp" data-index="timeStamp" key="startNs" align="flex-start" width="1fr" >
120    </lit-table-column>
121    <lit-table-column order title="Process(pid)" data-index="process" key="process" align="flex-start" width="2fr" >
122    </lit-table-column>
123    <lit-table-column order title="Fd" data-index="fd" key="fd" align="flex-start" width="1fr" >
124    </lit-table-column>
125    <lit-table-column order title="Size" data-index="sizes" key="size" align="flex-start" width="1fr" >
126    </lit-table-column>
127    <lit-table-column order title="Ino" data-index="ino" key="ino" align="flex-start" width="1fr" >
128    </lit-table-column>
129    <lit-table-column order title="ExpPid" data-index="expPid" key="expPid" align="flex-start" width="1fr" >
130    </lit-table-column>
131    <lit-table-column order title="ExpTaskComm" data-index="expTaskComm" key="expTaskComm" align="flex-start" width="2fr" >
132    </lit-table-column>
133    <lit-table-column order title="BufName" data-index="bufName" key="bufName" align="flex-start" width="2fr" >
134    </lit-table-column>
135    <lit-table-column order title="ExpName" data-index="expName" key="expName" align="flex-start" width="2fr" >
136    </lit-table-column>
137    <lit-table-column order title="Flag" data-index="flag" key="flag" align="flex-start" width="1fr" >
138    </lit-table-column>
139</lit-table>
140        `;
141  }
142
143  sortDmaByColumn(column: string, sort: number): void {
144    const sortFunction = function (leftData: unknown, rightData: unknown, sortType: number, property: string): number {
145      if (sortType === 1) {
146        // @ts-ignore
147        return typeof leftData[property] === 'string' // @ts-ignore
148          ? `${leftData[property]}`.localeCompare(`${rightData[property]}`) // @ts-ignore
149          : leftData[property] - rightData[property];
150      } else {
151        // @ts-ignore
152        return typeof rightData[property] === 'string' // @ts-ignore
153          ? `${rightData[property]}`.localeCompare(`${leftData[property]}`) // @ts-ignore
154          : rightData[property] - leftData[property];
155      }
156    };
157
158    switch (sort) {
159      case 0:
160        this.damClickTable!.recycleDataSource = this.dmaClickSource;
161        break;
162      default:
163        this.sortByColumn(column, sort, sortFunction);
164        break;
165    }
166  }
167
168  sortByColumn(column: string, sort: number, sortFunction: Function): void {
169    let array = [...this.dmaClickSource];
170    switch (column) {
171      case 'process':
172      case 'expTaskComm':
173      case 'bufName':
174      case 'expName':
175        this.damClickTable!.recycleDataSource = array.sort((leftData, rightData) =>
176          sortFunction(leftData, rightData, sort, column)
177        );
178        break;
179      case 'startNs':
180      case 'fd':
181      case 'size':
182      case 'ino':
183      case 'expPid':
184      case 'flag':
185        this.damClickTable!.recycleDataSource = array.sort((leftData, rightData) =>
186          sortFunction(leftData, rightData, sort, column)
187        );
188        break;
189    }
190  }
191}
192