• 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 { MemoryConfig } from '../../../../bean/MemoryConfig';
20import { SpSystemTrace } from '../../../SpSystemTrace';
21import { Utils } from '../../base/Utils';
22import { ns2s } from '../../TimerShaftElement';
23import {getTabDmaVMTrackerClickData} from "../../../../database/sql/Dma.sql";
24
25@element('tabpane-dma-selection-vmtracker')
26export class TabPaneDmaSelectVmTracker extends BaseElement {
27  private damClickTable: LitTable | null | undefined;
28  private dmaClickSource: Array<Dma> = [];
29  private tableThead: 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  connectedCallback(): void {
41    super.connectedCallback();
42    new ResizeObserver(() => {
43      if (this.parentElement?.clientHeight !== 0) {
44        if (this.damClickTable) {
45          // @ts-ignore
46          this.damClickTable.shadowRoot.querySelector('.table').style.height =
47            this.parentElement!.clientHeight - 18 + 'px';
48          this.damClickTable.reMeauseHeight();
49        }
50        this.parentElement!.style.overflow = 'hidden';
51      }
52    }).observe(this.parentElement!);
53  }
54
55  private init(): void {
56    const thTable = this.tableThead!.querySelector('.th');
57    const dmaSelectVmTrackerTblNodes = thTable!.querySelectorAll('div');
58    if (this.tableThead!.hasAttribute('sort')) {
59      this.tableThead!.removeAttribute('sort');
60      dmaSelectVmTrackerTblNodes.forEach((item) => {
61        item.querySelectorAll('svg').forEach((svg) => {
62          svg.style.display = 'none';
63        });
64      });
65    }
66  }
67
68  queryDmaVmTrackerClickDataByDB(startNs: number): void {
69    this.init();
70    getTabDmaVMTrackerClickData(startNs, MemoryConfig.getInstance().iPid).then((data) => {
71      if (data.length !== null && data.length > 0) {
72        data.forEach((item) => {
73          item.bufName = SpSystemTrace.DATA_DICT.get(item.bufName as number) || '-';
74          item.expName = SpSystemTrace.DATA_DICT.get(item.expName as number) || '-';
75          item.expTaskComm = SpSystemTrace.DATA_DICT.get(item.expTaskComm as number) || '-';
76          item.timeStamp = ns2s(item.startNs);
77          item.sizes = Utils.getBinaryByteWithUnit(item.size);
78          this.damClickTable!.getItemTextColor = (item: Dma): any => {
79            if (item.flag === 1) {
80              return '#d4b550';
81            } else if (item.flag === 2) {
82              return '#f86b6b';
83            } else {
84              return '#000000';
85            }
86          };
87        });
88        this.damClickTable!.recycleDataSource = data.sort(function (dmaVmLeftData: Dma, dmaVmRightData: Dma) {
89          return dmaVmRightData.size - dmaVmLeftData.size;
90        });
91        this.dmaClickSource = data;
92      } else {
93        this.damClickTable!.recycleDataSource = [];
94        this.dmaClickSource = [];
95      }
96    });
97  }
98
99  initHtml(): string {
100    return `
101<style>
102.damClickTable{
103    height: auto;
104}
105:host{
106    display: flex;
107    flex-direction: column;
108    padding: 10px 10px;
109}
110</style>
111<lit-table id="damClickTable" class="damClickTable">
112    <lit-table-column order title="TimeStamp" data-index="timeStamp" key="startNs" align="flex-start" width="1fr" >
113    </lit-table-column>
114    <lit-table-column order title="Fd" data-index="fd" key="fd" align="flex-start" width="1fr" >
115    </lit-table-column>
116    <lit-table-column order title="Size" data-index="sizes" key="size" align="flex-start" width="1fr" >
117    </lit-table-column>
118    <lit-table-column order title="Ino" data-index="ino" key="ino" align="flex-start" width="1fr" >
119    </lit-table-column>
120    <lit-table-column order title="ExpPid" data-index="expPid" key="expPid" align="flex-start" width="1fr" >
121    </lit-table-column>
122    <lit-table-column order title="ExpTaskComm" data-index="expTaskComm" key="expTaskComm" align="flex-start" width="1fr" >
123    </lit-table-column>
124    <lit-table-column order title="BufName" data-index="bufName" key="bufName" align="flex-start" width="1fr" >
125    </lit-table-column>
126    <lit-table-column order title="ExpName" data-index="expName" key="expName" align="flex-start" width="1fr" >
127    </lit-table-column>
128    <lit-table-column order title="Flag" data-index="flag" key="flag" align="flex-start" width="1fr" >
129    </lit-table-column>
130</lit-table>
131        `;
132  }
133
134  private compareValues(a: any, b: any, sort: number): number {
135    if (sort === 1) {
136      return a > b ? 1 : a < b ? -1 : 0;
137    } else {
138      return a < b ? 1 : a > b ? -1 : 0;
139    }
140  }
141
142  sortDmaByColumn(column: string, sort: number): void {
143    const comparisonFunctions: { [key: string]: (a: any, b: any) => number } = {
144      'startNs': (a, b) => this.compareValues(a.startNs, b.startNs, sort),
145      'expTaskComm': (a, b) => this.compareValues(`${a.expTaskComm}`, `${b.expTaskComm}`, sort),
146      'fd': (a, b) => this.compareValues(a.fd, b.fd, sort),
147      'size': (a, b) => this.compareValues(a.size, b.size, sort),
148      'ino': (a, b) => this.compareValues(a.ino, b.ino, sort),
149      'expPid': (a, b) => this.compareValues(a.expPid, b.expPid, sort),
150      'flag': (a, b) => this.compareValues(a.flag, b.flag, sort),
151      'bufName': (a, b) => this.compareValues(`${a.bufName}`, `${b.bufName}`, sort),
152      'expName': (a, b) => this.compareValues(`${a.expName}`, `${b.expName}`, sort)
153    };
154
155    if (sort === 0) {
156      this.damClickTable!.recycleDataSource = this.dmaClickSource;
157    } else {
158      const array = [...this.dmaClickSource];
159      const comparisonFunction = comparisonFunctions[column] || (() => 0);
160      this.damClickTable!.recycleDataSource = array.sort(comparisonFunction);
161    }
162  }
163}
164