// @ts-nocheck /* * 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.js"; import {LitTable} from "../../../../base-ui/table/lit-table.js"; import {Counter, SelectionData, SelectionParam} from "../../../bean/BoxSelection.js"; import {getTabCounters} from "../../../database/SqlLite.js"; @element('tabpane-counter') export class TabPaneCounter extends BaseElement { private tbl: LitTable | null | undefined; private range: HTMLLabelElement | null | undefined; private source: Array = [] set data(val: SelectionParam | any) { this.range!.textContent = "Selected range: " + parseFloat(((val.rightNs - val.leftNs) / 1000000.0).toFixed(5)) + " ms" getTabCounters(val.trackIds, val.rightNs).then((result) => { if (result != null && result.length > 0) { let dataSource: Array = []; let collect = this.groupByTrackIdToMap(result); let sumCount = 0; for (let key of collect.keys()) { let counters = collect.get(key); let list = counters!.filter((item) => item.startTime > val.leftNs); if (list.length > 0) { let index = counters!.indexOf(list[0]); if (index > 0) { result.splice(0, 0, counters![index - 1]); } } else { list.push(counters![counters!.length - 1]); } let sd = this.createSelectCounterData(list, val.leftNs, val.rightNs); sumCount += Number.parseInt(sd.count); dataSource.push(sd); } let sumData = new SelectionData(); sumData.count = sumCount; sumData.process = " "; dataSource.splice(0, 0, sumData); this.source = dataSource console.log(dataSource) this.tbl!.dataSource = dataSource } else { this.source = []; this.tbl!.dataSource = this.source } }); } initElements(): void { this.tbl = this.shadowRoot?.querySelector('#tb-counter'); this.range = this.shadowRoot?.querySelector('#time-range'); this.tbl!.addEventListener('column-click', (evt) => { this.sortByColumn(evt.detail) }); } initHtml(): string { return ` `; } groupByTrackIdToMap(arr: Array): Map> { let map = new Map>(); for (let counter of arr) { if (map.has(counter.trackId)) { map.get(counter.trackId)!.push(counter); } else { let list: Array = []; list.push(counter); map.set(counter.trackId, list); } } return map; } createSelectCounterData(list: Array, leftNs: number, rightNs: number): SelectionData { let selectData = new SelectionData(); if (list.length > 0) { let range = rightNs - leftNs; let first = list[0]; selectData.trackId = first.trackId; selectData.name = first.name; selectData.first = first.value + ""; selectData.count = list.length + ""; selectData.last = list[list.length - 1].value + ""; selectData.delta = (selectData.last - selectData.first) + ""; selectData.rate = (selectData.delta / (range * 1.0 / 1000000000)).toFixed(4); selectData.min = "0"; selectData.max = "0"; let weightAvg = 0.0; for (let i = 0; i < list.length; i++) { let counter = list[i]; if (counter.value < parseInt(selectData.min)) { selectData.min = counter.value.toString(); } if (counter.value > parseInt(selectData.max)) { selectData.max = counter.value.toString(); } let start = i == 0 ? leftNs : counter.startTime let end = i == list.length - 1 ? rightNs : list[i + 1].startTime weightAvg += counter.value * ((end - start) * 1.0 / range); } selectData.avgWeight = weightAvg.toFixed(2) } return selectData; } sortByColumn(detail) { function compare(property, sort, type) { return function (a: SelectionData, b: SelectionData) { if (a.process == " " || b.process == " ") { return 0; } if (type === 'number') { return sort === 2 ? parseFloat(b[property]) - parseFloat(a[property]) : parseFloat(a[property]) - parseFloat(b[property]); } else { if (b[property] > a[property]) { return sort === 2 ? 1 : -1; } else if (b[property] == a[property]) { return 0; } else { return sort === 2 ? -1 : 1; } } } } if (detail.key === 'name') { this.source.sort(compare(detail.key, detail.sort, 'string')) } else { this.source.sort(compare(detail.key, detail.sort, 'number')) } this.tbl!.dataSource = this.source; } }