1/* 2 * Copyright (C) 2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16import {BaseElement, element} from "../../../../../base-ui/BaseElement.js"; 17import {SpHiSysEventChart} from "../../../chart/SpHiSysEventChart.js"; 18import {LitTable} from "../../../../../base-ui/table/lit-table.js"; 19 20import { 21 queryAnomalyDetailedData 22} from "../../../../database/SqlLite.js"; 23import {SelectionParam} from "../../../../bean/BoxSelection.js"; 24import {EnergyAnomalyStruct} from "../../../../bean/EnergyStruct.js"; 25 26@element('tabpane-anomaly-details') 27export class TabPaneEnergyAnomaly extends BaseElement { 28 private tbl: LitTable | null | undefined; 29 private tableObserver: MutationObserver | undefined 30 31 set data(selection: SelectionParam) { 32 let div: HTMLElement | null | undefined = this?.shadowRoot?.querySelector("#anomaly-details"); 33 let htmlText = ""; 34 if(selection){ 35 this.queryAnomalyTableData(selection.leftNs, selection.rightNs).then((bean) => { 36 let filterAppMap = new Map(); 37 for(let index = 0;index < bean.length;index++){ 38 let findAppNameIndex = -1; 39 let values = Object.values(bean[index]); 40 if(values[3]){ 41 let apps = values[3].split(','); 42 for(let appIndex = 0;appIndex < apps.length;appIndex++){ 43 if(apps.indexOf(SpHiSysEventChart.app_name) != -1){ 44 findAppNameIndex = apps.indexOf(SpHiSysEventChart.app_name); 45 filterAppMap.set(values[0] + values[1], findAppNameIndex); 46 break; 47 } 48 } 49 if(values[2] == 'APPNAME'){ 50 //ts+eventName : appNameIndex 51 filterAppMap.set(values[0] + values[1], findAppNameIndex); 52 } 53 } 54 } 55 let set = new Set(); 56 for(let index = 0;index < bean.length;index++){ 57 let values = Object.values(bean[index]); 58 let findAppNameIndex = -1; 59 if(filterAppMap.get(values[0] + values[1]) == -1){ 60 continue 61 }else { 62 findAppNameIndex = filterAppMap.get(values[0] + values[1]); 63 } 64 if(!set.has(values[0])){ 65 set.add(values[0]); 66 htmlText += "<div><table" + 67 " style='border:none;table-layout:fixed;word-break:break-all' cellspacing=\"5\"; cellpadding=\"5\"><tbody>" + 68 "<tr><td colspan=\"5\" style='font-weight: 700;font-size: 14px'>"+values[1]+"</td></tr>"; 69 } 70 if(set.has(Object.values(bean[index])[0])){ 71 let appValues = values[3].split(','); 72 htmlText += "<tr><td style='font-weight: 400;font-size: 14px;opacity:0.9;width:150px;'>"+values[2] 73 +"</td><td style='font-weight: 400;font-size: 14px;opacity:0.6;width:250px;'>"+ 74 (findAppNameIndex >= 0 ? appValues.length > 1 ? appValues[findAppNameIndex] : values[3] : values[3]) 75 +"</td><td style='width:100px'></td>"; 76 } 77 if(index + 1 < bean.length){ 78 let nextValues = Object.values(bean[index + 1]); 79 let appValues = nextValues[3].split(','); 80 if(set.has(nextValues[0])){ 81 htmlText += "<td style='font-weight: 400;font-size: 14px;opacity:0.9;width:150px;'>" + nextValues[2] 82 + "</td><td style='font-weight: 400;font-size: 14px;opacity:0.6;width:250px;'>" + 83 (findAppNameIndex >= 0 ? appValues.length > 1 ? appValues[findAppNameIndex] : nextValues[3] : nextValues[3]) 84 +"</td></tr>"; 85 } else { 86 htmlText += "</tr>"; 87 htmlText += "</tbody></table></div>"; 88 continue; 89 } 90 index++; 91 } 92 } 93 div!.innerHTML = htmlText; 94 }) 95 } 96 } 97 98 /** 99 * 查询出 异常详细信息 100 * @param data 101 */ 102 async queryAnomalyTableData(startTime: number, endTime: number) : Promise<Array<EnergyAnomalyStruct>> { 103 let anomalyTableData = await queryAnomalyDetailedData(startTime, endTime) 104 return anomalyTableData; 105 } 106 107 initElements(): void { 108 this.tbl = this.shadowRoot?.querySelector<LitTable>('#anomalyselectionTbl'); 109 this.tbl?.addEventListener("column-click", (ev: any) => { 110 }) 111 this.addTableObserver() 112 } 113 114 connectedCallback() { 115 super.connectedCallback(); 116 new ResizeObserver((entries) => { 117 if (this.parentElement?.clientHeight != 0) { 118 // @ts-ignore 119 this.tbl!.shadowRoot.querySelector(".table").style.height = (this.parentElement.clientHeight - 45) + "px" 120 this.tbl!.reMeauseHeight() 121 } 122 }).observe(this.parentElement!); 123 } 124 125 addTableObserver() { 126 let MutationObserver = window.MutationObserver 127 this.tableObserver = new MutationObserver((list) => { 128 if (this.tbl) { 129 let width = getComputedStyle(this.tbl).getPropertyValue("width") 130 let height = getComputedStyle(this.tbl).getPropertyValue("height") 131 } 132 }) 133 let selector = this.shadowRoot?.querySelector(".left-table"); 134 this.tableObserver?.observe(selector!, {attributes: true, attributeFilter: ['style'], attributeOldValue: true}) 135 } 136 137 initHtml(): string { 138 return ` 139 <style> 140 .current-title{ 141 width: 95%; 142 display: flex; 143 top: 0; 144 background: var(--dark-background,#ffffff); 145 position: sticky; 146 } 147 .current-title h2{ 148 width: 50%; 149 padding: 0 10px; 150 font-size: 16px; 151 font-weight: 400; 152 visibility: visible; 153 } 154 .bottom-scroll-area{ 155 display: flex; 156 height: auto; 157 overflow-y: auto; 158 } 159 .left-table{ 160 width: 50%; 161 padding: 0 10px; 162 } 163 </style> 164 <div style="width: 100%;height: auto;position: relative"> 165 <div id="anomaly-details" class="current-title" style="margin-left: 12px;display: block"> 166 <h2 id="leftTitle"></h2> 167 </div> 168 <div class="bottom-scroll-area"> 169 <div class="left-table"> 170 <lit-table id="anomalyselectionTbl" no-head style="height: auto"> 171 <lit-table-column title="name" data-index="name" key="name" align="flex-start" width="180px"> 172 </lit-table-column> 173 <lit-table-column title="value" data-index="value" key="value" align="flex-start" > 174 </lit-table-column> 175 </lit-table> 176 </div> 177 </div> 178 </div> 179 `; 180 } 181} 182