• 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 */
15import { BaseElement, element } from '../../../../../base-ui/BaseElement';
16import { LitTable } from '../../../../../base-ui/table/lit-table';
17import { SpSystemTrace } from '../../../SpSystemTrace';
18import { Utils } from '../../base/Utils';
19import { SnapshotStruct } from '../../../../database/ui-worker/ProcedureWorkerSnapshot';
20import { MemoryConfig } from '../../../../bean/MemoryConfig';
21import { ns2s } from '../../../../database/ui-worker/ProcedureWorkerCommon';
22import { queryVmTrackerShmSelectionData } from '../../../../database/sql/Memory.sql';
23import { TabPaneVmTrackerShmSelectionHtml } from './TabPaneVmTrackerShmSelection.html';
24
25@element('tabpane-vmtracker-shm-selection')
26export class TabPaneVmTrackerShmSelection extends BaseElement {
27  private TableEl: LitTable | undefined | null;
28  private shmData: Array<unknown> = [];
29  private memoryConfig: MemoryConfig = MemoryConfig.getInstance();
30  private tabTitle: HTMLDivElement | undefined | null;
31
32  setShmData(data: SnapshotStruct, dataList: Array<SnapshotStruct>): void {
33    this.init();
34    this.clear();
35    this.queryDataByDB(data);
36  }
37
38  initElements(): void {
39    this.TableEl = this.shadowRoot!.querySelector<LitTable>('#tb-shm-selection') as LitTable;
40    this.tabTitle = this.TableEl!.shadowRoot?.querySelector('.thead') as HTMLDivElement;
41    this.TableEl!.addEventListener('column-click', (evt) => {
42      // @ts-ignore
43      this.sortByColumn(evt.detail.key, evt.detail.sort);
44    });
45  }
46
47  queryDataByDB(data: SnapshotStruct): void {
48    queryVmTrackerShmSelectionData(data.startNs, this.memoryConfig.iPid).then((result) => {
49      if (result.length > 0) {
50        for (let filter of result) {
51          //@ts-ignore
52          filter.name = SpSystemTrace.DATA_DICT.get(filter.name)?.split('/'); //@ts-ignore
53          filter.ts = ns2s(filter.startNS); //@ts-ignore
54          filter.sizeStr = Utils.getBinaryByteWithUnit(filter.size);
55          // @ts-ignore
56          this.TableEl!.getItemTextColor = (filter): unknown => {
57            // @ts-ignore
58            if (filter.flag === 1) {
59              return '#d4b550'; // @ts-ignore
60            } else if (filter.flag === 2) {
61              return '#f86b6b';
62            } else {
63              return '#000000';
64            }
65          };
66        } //@ts-ignore
67        this.shmData = result.sort((a, b) => b.size - a.size);
68        this.TableEl!.recycleDataSource = this.shmData;
69      }
70    });
71  }
72
73  clear(): void {
74    this.TableEl!.recycleDataSource = [];
75  }
76
77  private init(): void {
78    const thTable = this.tabTitle!.querySelector('.th');
79    const list = thTable!.querySelectorAll('div');
80    if (this.tabTitle!.hasAttribute('sort')) {
81      this.tabTitle!.removeAttribute('sort');
82      list.forEach((item) => {
83        item.querySelectorAll('svg').forEach((svg) => {
84          svg.style.display = 'none';
85        });
86      });
87    }
88  }
89
90  private compareValues(a: unknown, b: unknown, sort: number): number {
91    if (sort === 1) {
92      // @ts-ignore
93      return a > b ? 1 : a < b ? -1 : 0;
94    } else {
95      // @ts-ignore
96      return a < b ? 1 : a > b ? -1 : 0;
97    }
98  }
99
100  sortByColumn(column: string, sort: number): void {
101    const comparisonFunctions: { [key: string]: (a: unknown, b: unknown) => number } = {
102      // @ts-ignore
103      ts: (a, b) => this.compareValues(a.startNS, b.startNS, sort),
104      // @ts-ignore
105      fd: (a, b) => this.compareValues(a.fd, b.fd, sort),
106      // @ts-ignore
107      sizeStr: (a, b) => this.compareValues(a.size, b.size, sort),
108      // @ts-ignore
109      adj: (a, b) => this.compareValues(a.adj, b.adj, sort),
110      // @ts-ignore
111      name: (a, b) => this.compareValues(a.name, b.name, sort),
112      // @ts-ignore
113      id: (a, b) => this.compareValues(a.id, b.id, sort),
114      // @ts-ignore
115      time: (a, b) => this.compareValues(a.time, b.time, sort),
116      // @ts-ignore
117      count: (a, b) => this.compareValues(a.count, b.count, sort),
118      // @ts-ignore
119      purged: (a, b) => this.compareValues(a.purged, b.purged, sort),
120      // @ts-ignore
121      flag: (a, b) => this.compareValues(a.flag, b.flag, sort),
122    };
123
124    if (sort === 0) {
125      this.TableEl!.snapshotDataSource = this.shmData;
126    } else {
127      const array = [...this.shmData];
128      const comparisonFunction = comparisonFunctions[column] || ((): number => 0);
129      this.TableEl!.snapshotDataSource = array.sort(comparisonFunction);
130    }
131  }
132
133  initHtml(): string {
134    return TabPaneVmTrackerShmSelectionHtml;
135  }
136}
137