/* * Copyright (C) 2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import { BaseElement, element } from '../../../../../base-ui/BaseElement'; import { LitTable } from '../../../../../base-ui/table/lit-table'; import { SelectionParam } from '../../../../bean/BoxSelection'; import { Utils } from '../../base/Utils'; import { log } from '../../../../../log/Log'; import { Smaps, TYPE_STRING } from '../../../../bean/SmapsStruct'; import { MemoryConfig } from '../../../../bean/MemoryConfig'; import { SpSystemTrace } from '../../../SpSystemTrace'; import { getTabSmapsData, getTabSmapsSampleData } from '../../../../database/sql/Smaps.sql'; @element('tabpane-smaps-sample') export class TabPaneSmapsSample extends BaseElement { private tblSmapsSample: LitTable | null | undefined; private sourceSmapsSample: Array = []; private querySmapsSampleResult: Array = []; private isClick = false; private tabTitle: HTMLDivElement | undefined | null; set data(valSmapsSample: SelectionParam) { this.parentElement!.style.overflow = 'unset'; this.isClick = valSmapsSample.smapsType.length === 0; this.init(); this.tblSmapsSample!.loading = true; if (!this.isClick) { if (valSmapsSample.smapsType.length > 0) { this.queryDataByDB(valSmapsSample); } } else { this.setSmaps(valSmapsSample); } } initElements(): void { this.tblSmapsSample = this.shadowRoot?.querySelector('#tb-smaps-record'); this.tabTitle = this.tblSmapsSample!.shadowRoot?.querySelector('.thead') as HTMLDivElement; this.tblSmapsSample!.addEventListener('column-click', (evt) => { // @ts-ignore this.sortByColumn(evt.detail); }); } connectedCallback(): void { super.connectedCallback(); new ResizeObserver(() => { if (this.parentElement?.clientHeight !== 0) { // @ts-ignore this.tblSmapsSample?.shadowRoot?.querySelector('.table').style.height = this.parentElement!.clientHeight - 15 + 'px'; this.tblSmapsSample?.reMeauseHeight(); } }).observe(this.parentElement!); } queryDataByDB(srVal: SelectionParam | unknown): void { // @ts-ignore getTabSmapsData(srVal.leftNs, srVal.rightNs, (MemoryConfig.getInstance().interval * 1000_000) / 5).then( (result) => { log('getTabSmapsData size :' + result.length); this.tblSmapsSample!.loading = false; this.filteredData(result); } ); } setSmaps(data: SelectionParam): void { getTabSmapsSampleData(data.leftNs).then((result) => { this.tblSmapsSample!.loading = false; this.filteredData(result); }); } private init(): void { const thTable = this.tabTitle!.querySelector('.th'); const smapsSampleTblNodes = thTable!.querySelectorAll('div'); if (this.tabTitle!.hasAttribute('sort')) { this.tabTitle!.removeAttribute('sort'); smapsSampleTblNodes.forEach((item) => { item.querySelectorAll('svg').forEach((svg) => { svg.style.display = 'none'; }); }); } } filteredData(result: unknown): void { // @ts-ignore if (result.length !== null && result.length > 0) { // @ts-ignore for (const smaps of result) { smaps.typeName = TYPE_STRING[smaps.type]; smaps.address = smaps.startAddr + ' - ' + smaps.endAddr; smaps.swapStr = Utils.getBinaryByteWithUnit(smaps.swap); smaps.rssStr = Utils.getBinaryByteWithUnit(smaps.rss); smaps.pssStr = Utils.getBinaryByteWithUnit(smaps.pss); smaps.sizeStr = Utils.getBinaryByteWithUnit(smaps.size); smaps.sharedCleanStr = Utils.getBinaryByteWithUnit(smaps.sharedClean); smaps.sharedDirtyStr = Utils.getBinaryByteWithUnit(smaps.sharedDirty); smaps.privateCleanStr = Utils.getBinaryByteWithUnit(smaps.privateClean); smaps.privateDirtyStr = Utils.getBinaryByteWithUnit(smaps.privateDirty); smaps.swapPssStr = Utils.getBinaryByteWithUnit(smaps.swapPss); smaps.time = Utils.getTimeString(smaps.startNs); smaps.path = SpSystemTrace.DATA_DICT.get(smaps.path)?.split('/'); smaps.permission = SpSystemTrace.DATA_DICT.get(smaps.pid)?.split('/'); let resideS = smaps.reside.toFixed(2); if (resideS === '0.00') { smaps.resideStr = '0%'; } else { smaps.resideStr = resideS + '%'; } } // @ts-ignore this.sourceSmapsSample = result; // @ts-ignore this.querySmapsSampleResult = result; this.tblSmapsSample!.recycleDataSource = this.sourceSmapsSample; } else { this.sourceSmapsSample = []; this.querySmapsSampleResult = []; this.tblSmapsSample!.recycleDataSource = []; } } initHtml(): string { return ` `; } sortByColumn(detail: unknown): void { // @ts-ignore function compare(property, sort, type) { return function (aSmapsSample: Smaps, bSmapsSample: Smaps) { if (type === 'number') { // @ts-ignore return sort === 2 ? // @ts-ignore parseFloat(bSmapsSample[property]) - parseFloat(aSmapsSample[property]) : // @ts-ignore parseFloat(aSmapsSample[property]) - parseFloat(bSmapsSample[property]); } else { // @ts-ignore if (bSmapsSample[property] > aSmapsSample[property]) { return sort === 2 ? 1 : -1; } else { // @ts-ignore if (bSmapsSample[property] === aSmapsSample[property]) { return 0; } else { return sort === 2 ? -1 : 1; } } } }; } // @ts-ignore if (detail.key === 'rssStr' || detail.key === 'sizeStr' || detail.key === 'resideStr') { // @ts-ignore let key = detail.key.substring(0, detail.key.indexOf('Str')); // @ts-ignore this.sourceSmapsSample.sort(compare(key, detail.sort, 'number')); } else { // @ts-ignore this.sourceSmapsSample.sort(compare(detail.key, detail.sort, 'string')); } this.tblSmapsSample!.recycleDataSource = this.sourceSmapsSample; } }