1// @ts-nocheck 2/* 3 * Copyright (C) 2022 Huawei Device Co., Ltd. 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17import {BaseElement, element} from "../../../../base-ui/BaseElement.js"; 18import {LitTable} from "../../../../base-ui/table/lit-table.js"; 19import {Counter, SelectionData, SelectionParam} from "../../../bean/BoxSelection.js"; 20import {getTabCounters} from "../../../database/SqlLite.js"; 21 22@element('tabpane-counter') 23export class TabPaneCounter extends BaseElement { 24 private tbl: LitTable | null | undefined; 25 private range: HTMLLabelElement | null | undefined; 26 private source: Array<SelectionData> = [] 27 28 set data(val: SelectionParam | any) { 29 this.range!.textContent = "Selected range: " + parseFloat(((val.rightNs - val.leftNs) / 1000000.0).toFixed(5)) + " ms" 30 getTabCounters(val.trackIds, val.rightNs).then((result) => { 31 if (result != null && result.length > 0) { 32 let dataSource: Array<SelectionData> = []; 33 let collect = this.groupByTrackIdToMap(result); 34 let sumCount = 0; 35 for (let key of collect.keys()) { 36 let counters = collect.get(key); 37 let list = counters!.filter((item) => item.startTime > val.leftNs); 38 if (list.length > 0) { 39 let index = counters!.indexOf(list[0]); 40 if (index > 0) { 41 result.splice(0, 0, counters![index - 1]); 42 } 43 } else { 44 list.push(counters![counters!.length - 1]); 45 } 46 let sd = this.createSelectCounterData(list, val.leftNs, val.rightNs); 47 sumCount += Number.parseInt(sd.count); 48 dataSource.push(sd); 49 } 50 let sumData = new SelectionData(); 51 sumData.count = sumCount; 52 sumData.process = " "; 53 dataSource.splice(0, 0, sumData); 54 this.source = dataSource 55 console.log(dataSource) 56 this.tbl!.dataSource = dataSource 57 } else { 58 this.source = []; 59 this.tbl!.dataSource = this.source 60 } 61 }); 62 } 63 64 initElements(): void { 65 this.tbl = this.shadowRoot?.querySelector<LitTable>('#tb-counter'); 66 this.range = this.shadowRoot?.querySelector('#time-range'); 67 this.tbl!.addEventListener('column-click', (evt) => { 68 this.sortByColumn(evt.detail) 69 }); 70 } 71 72 initHtml(): string { 73 return ` 74<style> 75:host{ 76 display: flex; 77 flex-direction: column; 78 padding: 10px 10px; 79} 80</style> 81<label id="time-range" style="width: 100%;height: 20px;text-align: end;font-size: 10pt;margin-bottom: 5px">Selected range:0.0 ms</label> 82<lit-table id="tb-counter" style="height: auto"> 83 <lit-table-column width="25%" title="Name" data-index="name" key="name" align="flex-start" order></lit-table-column> 84 <lit-table-column width="1fr" title="Delta value" data-index="delta" key="delta" align="flex-start" order ></lit-table-column> 85 <lit-table-column width="1fr" title="Rate /s" data-index="rate" key="rate" align="flex-start" order ></lit-table-column> 86 <lit-table-column width="1fr" title="Weighted avg value" data-index="avgWeight" key="avgWeight" align="flex-start" order ></lit-table-column> 87 <lit-table-column width="1fr" title="Count" data-index="count" key="count" align="flex-start" order ></lit-table-column> 88 <lit-table-column width="1fr" title="First value" data-index="first" key="first" align="flex-start" order ></lit-table-column> 89 <lit-table-column width="1fr" title="Last value" data-index="last" key="last" align="flex-start" order ></lit-table-column> 90 <lit-table-column width="1fr" title="Min value" data-index="min" key="min" align="flex-start" order ></lit-table-column> 91 <lit-table-column width="1fr" title="Max value" data-index="max" key="max" align="flex-start" order ></lit-table-column> 92</lit-table> 93 `; 94 } 95 96 groupByTrackIdToMap(arr: Array<Counter>): Map<number, Array<Counter>> { 97 let map = new Map<number, Array<Counter>>(); 98 for (let counter of arr) { 99 if (map.has(counter.trackId)) { 100 map.get(counter.trackId)!.push(counter); 101 } else { 102 let list: Array<Counter> = []; 103 list.push(counter); 104 map.set(counter.trackId, list); 105 } 106 } 107 return map; 108 } 109 110 createSelectCounterData(list: Array<Counter>, leftNs: number, rightNs: number): SelectionData { 111 let selectData = new SelectionData(); 112 if (list.length > 0) { 113 let range = rightNs - leftNs; 114 let first = list[0]; 115 selectData.trackId = first.trackId; 116 selectData.name = first.name; 117 selectData.first = first.value + ""; 118 selectData.count = list.length + ""; 119 selectData.last = list[list.length - 1].value + ""; 120 selectData.delta = (selectData.last - selectData.first) + ""; 121 selectData.rate = (selectData.delta / (range * 1.0 / 1000000000)).toFixed(4); 122 selectData.min = "0"; 123 selectData.max = "0"; 124 let weightAvg = 0.0; 125 for (let i = 0; i < list.length; i++) { 126 let counter = list[i]; 127 if (counter.value < parseInt(selectData.min)) { 128 selectData.min = counter.value.toString(); 129 } 130 if (counter.value > parseInt(selectData.max)) { 131 selectData.max = counter.value.toString(); 132 } 133 let start = i == 0 ? leftNs : counter.startTime 134 let end = i == list.length - 1 ? rightNs : list[i + 1].startTime 135 weightAvg += counter.value * ((end - start) * 1.0 / range); 136 } 137 selectData.avgWeight = weightAvg.toFixed(2) 138 } 139 return selectData; 140 } 141 142 sortByColumn(detail) { 143 function compare(property, sort, type) { 144 return function (a: SelectionData, b: SelectionData) { 145 if (a.process == " " || b.process == " ") { 146 return 0; 147 } 148 if (type === 'number') { 149 return sort === 2 ? parseFloat(b[property]) - parseFloat(a[property]) : parseFloat(a[property]) - parseFloat(b[property]); 150 } else { 151 if (b[property] > a[property]) { 152 return sort === 2 ? 1 : -1; 153 } else if (b[property] == a[property]) { 154 return 0; 155 } else { 156 return sort === 2 ? -1 : 1; 157 } 158 } 159 } 160 } 161 162 if (detail.key === 'name') { 163 this.source.sort(compare(detail.key, detail.sort, 'string')) 164 } else { 165 this.source.sort(compare(detail.key, detail.sort, 'number')) 166 } 167 this.tbl!.dataSource = this.source; 168 } 169 170}