/* * 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 { SystemMemorySummary } from '../../../../bean/AbilityMonitor'; import { Utils } from '../../base/Utils'; import { log } from '../../../../../log/Log'; import { resizeObserver } from '../SheetUtils'; import { queryStartTime } from '../../../../database/sql/SqlLite.sql'; import { getTabMemoryAbilityData } from '../../../../database/sql/Ability.sql'; @element('tabpane-memory-ability') export class TabPaneMemoryAbility extends BaseElement { private memoryAbilityTbl: LitTable | null | undefined; private memoryAbilitySource: Array = []; private queryMemoryResult: Array = []; private search: HTMLInputElement | undefined | null; set data(memoryAbilityValue: SelectionParam | unknown) { if (this.memoryAbilityTbl) { // @ts-ignore this.memoryAbilityTbl.shadowRoot?.querySelector('.table').style.height = `${ this.parentElement!.clientHeight - 45 }px`; } this.queryDataByDB(memoryAbilityValue); } initElements(): void { this.memoryAbilityTbl = this.shadowRoot?.querySelector('#tb-memory-ability'); this.memoryAbilityTbl!.addEventListener('column-click', (evt): void => { // @ts-ignore this.sortByColumn(evt.detail); }); } connectedCallback(): void { super.connectedCallback(); resizeObserver(this.parentElement!, this.memoryAbilityTbl!); } filterData(): void { if (this.queryMemoryResult.length > 0) { let filterMemory = this.queryMemoryResult.filter((item): boolean => { let array = this.toMemoryAbilityArray(item); // @ts-ignore let isInclude = array.filter((value): boolean => value.indexOf(this.search!.value) > -1); return isInclude.length > 0; }); if (filterMemory.length > 0) { this.memoryAbilitySource = filterMemory; this.memoryAbilityTbl!.recycleDataSource = this.memoryAbilitySource; } else { this.memoryAbilitySource = []; this.memoryAbilityTbl!.recycleDataSource = []; } } } toMemoryAbilityArray(systemMemorySummary: SystemMemorySummary): unknown[] { let array: Array = []; array.push(systemMemorySummary.startTimeStr); array.push(systemMemorySummary.durationStr); array.push(systemMemorySummary.memoryTotal); array.push(systemMemorySummary.cached); array.push(systemMemorySummary.swapTotal); return array; } getMemoryKeys(): unknown { return { 'sys.mem.total': 'memoryTotal', 'sys.mem.free': 'memFree', 'sys.mem.buffers': 'buffers', 'sys.mem.cached': 'cached', 'sys.mem.shmem': 'shmem', 'sys.mem.slab': 'slab', 'sys.mem.swap.total': 'swapTotal', 'sys.mem.swap.free': 'swapFree', 'sys.mem.mapped': 'mapped', 'sys.mem.vmalloc.used': 'vmallocUsed', 'sys.mem.page.tables': 'pageTables', 'sys.mem.kernel.stack': 'kernelStack', 'sys.mem.active': 'active', 'sys.mem.inactive': 'inactive', 'sys.mem.unevictable': 'unevictable', 'sys.mem.vmalloc.total': 'vmallocTotal', 'sys.mem.slab.unreclaimable': 'sUnreclaim', 'sys.mem.cma.total': 'cmaTotal', 'sys.mem.cma.free': 'cmaFree', 'sys.mem.kernel.reclaimable': 'kReclaimable', 'sys.mem.zram': 'zram', }; } queryDataByDB(val: SelectionParam | unknown): void { queryStartTime().then((res): void => { //@ts-ignore let startTime = res[0].start_ts; // @ts-ignore getTabMemoryAbilityData(val.leftNs + startTime, val.rightNs + startTime).then((items): void => { log(`getTabMemoryAbilityData result size : ${items.length}`); this.memoryAbilitySource = []; this.queryMemoryResult = []; if (items.length !== null && items.length > 0) { let lastTime = 0; for (const item of items) { let systemMemorySummary = new SystemMemorySummary(); systemMemorySummary.startTimeStr = item.startTime - startTime <= 0 ? '0:000.000.000' : Utils.getTimeStampHMS(item.startTime - startTime); systemMemorySummary.durationNumber = lastTime !== 0 ? item.startTime - lastTime : 0; systemMemorySummary.durationStr = lastTime !== 0 ? Utils.getDurString(systemMemorySummary.durationNumber) : '-'; lastTime = item.startTime; let memorys = item.value.split(','); let names = item.name.split(','); if (memorys.length !== names.length) { continue; } // @ts-ignore let memoryKeys: { [key: string]: string } = this.getMemoryKeys(); for (let i = 0; i < names.length; i++) { let key = memoryKeys[names[i]]; if (key) { // @ts-ignore systemMemorySummary[key] = Utils.getBinaryKBWithUnit(Number(memorys[i])); } } this.memoryAbilitySource.push(systemMemorySummary); } this.memoryAbilityTbl!.recycleDataSource = this.memoryAbilitySource; } else { this.memoryAbilitySource = []; this.memoryAbilityTbl!.recycleDataSource = []; } }); }); if (this.memoryAbilityTbl) { let th = this.memoryAbilityTbl.shadowRoot?.querySelector('.th'); if (th) { th.style.gridColumnGap = '5px'; } } } initHtml(): string { return ` `; } sortByColumn(detail: unknown): void { // @ts-ignore function compare(property, sort, type) { return function ( memoryAbilityLeftData: SystemMemorySummary, memoryAbilityRightData: SystemMemorySummary ): number { if (type === 'number') { return sort === 2 // @ts-ignore ? parseFloat(memoryAbilityRightData[property]) - parseFloat(memoryAbilityLeftData[property]) // @ts-ignore : parseFloat(memoryAbilityLeftData[property]) - parseFloat(memoryAbilityRightData[property]); } else if (type === 'durationStr') { return sort === 2 ? memoryAbilityRightData.durationNumber - memoryAbilityLeftData.durationNumber : memoryAbilityLeftData.durationNumber - memoryAbilityRightData.durationNumber; } else { // @ts-ignore if (memoryAbilityRightData[property] > memoryAbilityLeftData[property]) { return sort === 2 ? 1 : -1; } else { // @ts-ignore if (memoryAbilityRightData[property] === memoryAbilityLeftData[property]) { return 0; } else { return sort === 2 ? -1 : 1; } } } }; } // @ts-ignore if (detail.key === 'startTime') { // @ts-ignore this.memoryAbilitySource.sort(compare(detail.key, detail.sort, 'string')); // @ts-ignore } else if (detail.key === 'durationStr') { // @ts-ignore this.memoryAbilitySource.sort(compare(detail.key, detail.sort, 'durationStr')); } else { // @ts-ignore this.memoryAbilitySource.sort(compare(detail.key, detail.sort, 'number')); } this.memoryAbilityTbl!.recycleDataSource = this.memoryAbilitySource; } }