/* * 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 { SystemNetworkSummary } from '../../../../bean/AbilityMonitor'; import { Utils } from '../../base/Utils'; import { ColorUtils } from '../../base/ColorUtils'; import { log } from '../../../../../log/Log'; import { resizeObserver } from '../SheetUtils'; import { getTabNetworkAbilityData } from '../../../../database/sql/Ability.sql'; @element('tabpane-network-ability') export class TabPaneNetworkAbility extends BaseElement { private networkAbilityTbl: LitTable | null | undefined; private networkAbilitySource: Array = []; private float: HTMLDivElement | null | undefined; private queryResult: Array = []; private search: HTMLInputElement | undefined | null; set data(networkAbilityValue: SelectionParam | any) { if (this.networkAbilityTbl) { // @ts-ignore this.networkAbilityTbl.shadowRoot.querySelector('.table').style.height = this.parentElement!.clientHeight - 45 + 'px'; } this.queryDataByDB(networkAbilityValue); } initElements(): void { this.networkAbilityTbl = this.shadowRoot?.querySelector('#tb-network-ability'); this.networkAbilityTbl!.addEventListener('column-click', (evt) => { // @ts-ignore this.sortByColumn(evt.detail); }); } connectedCallback() { super.connectedCallback(); resizeObserver(this.parentElement!, this.networkAbilityTbl!); } filterData() { if (this.queryResult.length > 0) { let filterNetwork = this.queryResult.filter((item) => { let array = this.toNetWorkAbilityArray(item); let isInclude = array.filter((value) => value.indexOf(this.search!.value) > -1); return isInclude.length > 0; }); if (filterNetwork.length > 0) { this.networkAbilitySource = filterNetwork; this.networkAbilityTbl!.recycleDataSource = this.networkAbilitySource; } else { this.networkAbilitySource = []; this.networkAbilityTbl!.recycleDataSource = []; } } } toNetWorkAbilityArray(systemNetworkSummary: SystemNetworkSummary): any[] { let array: Array = []; array.push(systemNetworkSummary.startTimeStr); array.push(systemNetworkSummary.durationStr); array.push(systemNetworkSummary.dataReceivedStr); array.push(systemNetworkSummary.dataReceivedSecStr); array.push(systemNetworkSummary.dataSendSecStr); array.push(systemNetworkSummary.dataSendStr); array.push(systemNetworkSummary.packetsIn.toString()); array.push(systemNetworkSummary.packetsOut.toString()); array.push(systemNetworkSummary.packetsOutSec.toString()); return array; } queryDataByDB(val: SelectionParam | any) { getTabNetworkAbilityData(val.leftNs, val.rightNs).then((item) => { log('getTabNetworkAbilityData result size : ' + item.length); if (item.length != null && item.length > 0) { for (const systemNetworkSummary of item) { if (systemNetworkSummary.startTime == 0) { systemNetworkSummary.startTimeStr = '0:000.000.000'; } else { systemNetworkSummary.startTimeStr = Utils.getTimeStampHMS(systemNetworkSummary.startTime); } systemNetworkSummary.durationStr = Utils.getDurString(systemNetworkSummary.duration); systemNetworkSummary.dataReceivedStr = Utils.getBinaryByteWithUnit(systemNetworkSummary.dataReceived); systemNetworkSummary.dataReceivedSecStr = Utils.getBinaryByteWithUnit(systemNetworkSummary.dataReceivedSec); systemNetworkSummary.dataSendStr = Utils.getBinaryByteWithUnit(systemNetworkSummary.dataSend); systemNetworkSummary.dataSendSecStr = Utils.getBinaryByteWithUnit(systemNetworkSummary.dataSendSec); systemNetworkSummary.packetsInStr = ColorUtils.formatNumberComma(systemNetworkSummary.packetsIn); systemNetworkSummary.packetsInSecStr = systemNetworkSummary.packetsInSec.toFixed(2); systemNetworkSummary.packetsOutStr = ColorUtils.formatNumberComma(systemNetworkSummary.packetsOut); systemNetworkSummary.packetsOutSecStr = systemNetworkSummary.packetsOutSec.toFixed(2); } this.networkAbilitySource = item; this.queryResult = item; this.networkAbilityTbl!.recycleDataSource = this.networkAbilitySource; } else { this.networkAbilitySource = []; this.queryResult = []; this.networkAbilityTbl!.recycleDataSource = []; } }); } initHtml(): string { return ` `; } compare(property: string, sort: number, type: string) { let getProperty = this.getPropertyByType(property, type); return this.compareFunction(sort, getProperty); } compareFunction = (sort: number, getProperty: (data: SystemNetworkSummary) => number | string) => (networkAbilityLeftData: SystemNetworkSummary, networkAbilityRightData: SystemNetworkSummary) => { let leftValue = getProperty(networkAbilityLeftData); let rightValue = getProperty(networkAbilityRightData); let result = 0; if (leftValue > rightValue) { result = sort === 2 ? -1 : 1; } else if (leftValue < rightValue) { result = sort === 2 ? 1 : -1; } return result; }; getPropertyByType = (property: string, type: string) => (data: SystemNetworkSummary): number | string => { let typeMap = { // @ts-ignore number: parseFloat(data[property]), durationStr: data.duration, dataReceivedStr: data.dataReceived, dataReceivedSecStr: data.dataReceivedSec, dataSendStr: data.dataSend, dataSendSecStr: data.dataSendSec, packetsInStr: data.packetsIn, packetsInSecStr: data.packetsInSec, packetsOutStr: data.packetsOut, packetsOutSecStr: data.packetsOutSec }; // @ts-ignore return typeMap[type] || data[property]; }; sortByColumn(detail: any) { // @ts-ignore if (detail.key === 'startTime') { this.networkAbilitySource.sort(this.compare(detail.key, detail.sort, 'string')); } else if (detail.key === 'durationStr') { this.networkAbilitySource.sort(this.compare(detail.key, detail.sort, 'durationStr')); } else if (detail.key === 'dataReceivedStr') { this.networkAbilitySource.sort(this.compare(detail.key, detail.sort, 'dataReceivedStr')); } else if (detail.key === 'dataReceivedSecStr') { this.networkAbilitySource.sort(this.compare(detail.key, detail.sort, 'dataReceivedSecStr')); } else if (detail.key === 'dataSendStr') { this.networkAbilitySource.sort(this.compare(detail.key, detail.sort, 'dataSendStr')); } else if (detail.key === 'dataSendSecStr') { this.networkAbilitySource.sort(this.compare(detail.key, detail.sort, 'dataSendSecStr')); } else if (detail.key === 'packetsInStr') { this.networkAbilitySource.sort(this.compare(detail.key, detail.sort, 'packetsInStr')); } else if (detail.key === 'packetsInSecStr') { this.networkAbilitySource.sort(this.compare(detail.key, detail.sort, 'packetsInSecStr')); } else if (detail.key === 'packetsOutStr') { this.networkAbilitySource.sort(this.compare(detail.key, detail.sort, 'packetsOutStr')); } else if (detail.key === 'packetsOutSecStr') { this.networkAbilitySource.sort(this.compare(detail.key, detail.sort, 'packetsOutSecStr')); } else { this.networkAbilitySource.sort(this.compare(detail.key, detail.sort, 'number')); } this.networkAbilityTbl!.recycleDataSource = this.networkAbilitySource; } }