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<any> = []; 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 filter.name = SpSystemTrace.DATA_DICT.get(filter.name)?.split('/'); 52 filter.ts = ns2s(filter.startNS); 53 filter.sizeStr = Utils.getBinaryByteWithUnit(filter.size); 54 this.TableEl!.getItemTextColor = (filter): any => { 55 if (filter.flag === 1) { 56 return '#d4b550'; 57 } else if (filter.flag === 2) { 58 return '#f86b6b'; 59 } else { 60 return '#000000'; 61 } 62 }; 63 } 64 this.shmData = result.sort((a, b) => b.size - a.size); 65 this.TableEl!.recycleDataSource = this.shmData; 66 } 67 }); 68 } 69 70 clear(): void { 71 this.TableEl!.recycleDataSource = []; 72 } 73 74 private init(): void { 75 const thTable = this.tabTitle!.querySelector('.th'); 76 const list = thTable!.querySelectorAll('div'); 77 if (this.tabTitle!.hasAttribute('sort')) { 78 this.tabTitle!.removeAttribute('sort'); 79 list.forEach((item) => { 80 item.querySelectorAll('svg').forEach((svg) => { 81 svg.style.display = 'none'; 82 }); 83 }); 84 } 85 } 86 87 private compareValues(a: any, b: any, sort: number): number { 88 if (sort === 1) { 89 return a > b ? 1 : a < b ? -1 : 0; 90 } else { 91 return a < b ? 1 : a > b ? -1 : 0; 92 } 93 } 94 95 sortByColumn(column: string, sort: number): void { 96 const comparisonFunctions: { [key: string]: (a: any, b: any) => number } = { 97 'ts': (a, b) => this.compareValues(a.startNS, b.startNS, sort), 98 'fd': (a, b) => this.compareValues(a.fd, b.fd, sort), 99 'sizeStr': (a, b) => this.compareValues(a.size, b.size, sort), 100 'adj': (a, b) => this.compareValues(a.adj, b.adj, sort), 101 'name': (a, b) => this.compareValues(a.name, b.name, sort), 102 'id': (a, b) => this.compareValues(a.id, b.id, sort), 103 'time': (a, b) => this.compareValues(a.time, b.time, sort), 104 'count': (a, b) => this.compareValues(a.count, b.count, sort), 105 'purged': (a, b) => this.compareValues(a.purged, b.purged, sort), 106 'flag': (a, b) => this.compareValues(a.flag, b.flag, sort) 107 }; 108 109 if (sort === 0) { 110 this.TableEl!.snapshotDataSource = this.shmData; 111 } else { 112 const array = [...this.shmData]; 113 const comparisonFunction = comparisonFunctions[column] || (() => 0); 114 this.TableEl!.snapshotDataSource = array.sort(comparisonFunction); 115 } 116 } 117 118 initHtml(): string { 119 return TabPaneVmTrackerShmSelectionHtml; 120 } 121} 122